-
Notifications
You must be signed in to change notification settings - Fork 10
/
sadfscheck
executable file
·247 lines (231 loc) · 8.59 KB
/
sadfscheck
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#!/bin/sh -eu
# sadfscheck (part of ossobv/vcutil) // wdoekes/2021,2024 // Public Domain
#
# Checks nvme and zfs configurations for suboptimal/non-osso configuration.
#
nvme_list() {
devs=$(nvme list 2>/dev/null | awk '/^\/dev\//{print $1}')
test -z "$devs" && echo "info: no nvme devices found" >&2
echo $devs
}
nvme_check_best_sector() {
for dev in $(nvme_list); do
local sector_sizes="$(nvme id-ns -H "$dev" | grep '^LBA Format')"
local sector_inuse="$(echo "$sector_sizes" | sed -e '
/(in use)/!d
s/.*Data Size:[[:blank:]]*\([0-9]\+\)[[:blank:]]\+bytes.*/\1/')"
local sector_best="$(echo "$sector_sizes" | sed -e '
/ Best\([[:blank:]]\|$\)/!d
s/.*Data Size:[[:blank:]]*\([0-9]\+\)[[:blank:]]\+bytes.*/\1/')"
test -z "$sector_best" &&
sector_best="$(echo "$sector_sizes" | sed -e '
/ Better\([[:blank:]]\|$\)/!d
s/.*Data Size:[[:blank:]]*\([0-9]\+\)[[:blank:]]\+bytes.*/\1/')"
# Some devices list multiple formats as "Best", so we use grep instead
# of an int comparison.
if ! echo "$sector_best" | grep -q "^$sector_inuse$"; then
echo "$dev: sector size not optimal\
(inuse=$sector_inuse, best="$sector_best")" # multiline->single
fi
done
}
dev_sector_size() {
local dev="$1"
if ! test -b "$dev" && test -b /dev/disk/by-id/${dev#/dev/}; then
# Old ZFS 0.7.5 links to wrong device. Quick hack to fix.
dev=/dev/disk/by-id/${dev#/dev/}
fi
local dev="$(realpath "$dev")"
if test "${dev#/dev/nvme}" != "$dev"; then
local sector_size=$(nvme id-ns -H "$dev" | sed -e '
/^LBA Format/!d;/(in use)/!d
s/.*Data Size:[[:blank:]]*\([0-9]\+\)[[:blank:]]\+bytes.*/\1/')
test -z "$sector_size" && echo "error: No sector size for $1" >&2
echo $sector_size
else
local size=
local assume_4096=
smartctl -i "$dev" | while read -r line; do
case "$line" in
'Rotation Rate:'*)
# Rotation Rate: Solid State Device
test -z "$assume_4096" &&
test "${line%Solid State Device}" != "$line" &&
assume_4096=1 || assume_4096=0;;
'Sector Size:'*)
# Sector Size: 512 bytes logical/physical
size=${line#*:}; size=${size% bytes*}; size=$((size + 0));;
'Sector Sizes:'*)
# Sector Sizes: 512 bytes logical, 4096 bytes physical
size=${line#*: *logical, }; size=${size% bytes*}
assume_4096=0;;
'Logical block size:'*)
size=${line#*:}; size=${size% bytes*}; size=$((size + 0));;
'')
if test -n "$size"; then
if test ${assume_4096:-0} -eq 1; then
echo 4096 # not 512.. probably, maybe even 8192
else
echo $size
fi
break
fi
esac
done
fi
}
dev_sector_shift() {
local sector_size="$(dev_sector_size "$1")"
case $sector_size in
512)
echo 9
;;
4096)
echo 12
;;
8192)
echo 13
;;
*)
echo "unknown-${sector_size}-for-$1" >&2
echo 12
;;
esac
}
_zdb_output=
zdb_output() {
# zdb lists cached info. Sometimes pools are imported with
# cachefile=none or cachefile=/else/where. zdb will not list those.
# So, instead we use -eC to read their "non-imported" values.
# (And because this is slow (not cached), we cache the output for
# ourselves.)
# Note that the zdb -C output is just slightly different from the
# regular zdb output.
if test -z "$_zdb_output"; then
_zdb_output=$(\
zdb
eval $(zpool list -Honame,cachefile | sed -e '
/[[:blank:]]-$/d
s/^\([^[:blank:]]*\)[[:blank:]].*/zdb -eC \1;/')
)
fi
echo "$_zdb_output"
}
zpool_check_ashift() {
zdb_output | while read line; do
local pool
local ashift
local path
local actual_ashift
case "$line" in
name:*)
pool=${line##* }; pool=${pool%\'}; pool=${pool#\'};;
ashift:*)
ashift=${line##* };;
path:*)
path=${line##* }; path=${path%\'}; path=${path#\'};;
features_for_read:)
local dev_ashift=$(dev_sector_shift $path)
if test $ashift -lt $dev_ashift; then
echo "$pool: ashift $ashift on zpool lower\
than disk ashift $dev_ashift"
elif test $ashift -lt 12; then
echo "$pool: unexpected ashift $ashift below 12"
fi
esac
done | uniq
}
zpool_check_autotrim() {
local name autotrim
zpool list -Honame,autotrim | LC_ALL=C sort | while read name autotrim; do
if test "$autotrim" != on; then
echo "$name: autotrim is not on (it is '$autotrim')" >&2
fi
done
}
zfs_check_compression() {
local name compression
zpool list -Honame | LC_ALL=C sort | while read name; do
# We check the main pools only. Children filesystems can have different
# settings.
compression=$(zfs get -Hovalue compression "$name")
if test "$compression" != lz4 && test "$compression" != on; then
echo "$name: compression is not lz4 (it is '$compression')" >&2
fi
done
}
zfs_check_primarycache() {
local name primarycache
zpool list -Honame | LC_ALL=C sort | while read name; do
# We check the main pools only. Children filesystems can have different
# settings.
primarycache=$(zfs get -Hovalue primarycache "$name")
if test "$primarycache" != all; then
echo "$name: primarycache is not all (it is '$primarycache')" >&2
fi
done
}
zpool_check_stripe() {
local pool=
local type1=
local type2=
zdb_output | while read line; do
if test "${line#name:}" != "$line"; then
pool=${line##* }; pool=${pool%\'}; pool=${pool#\'}
type1=
type2=
elif test -z "$type1" && test "${line#type:}" != "$line"; then
type1=${line##* }; type1=${type1%\'}; type1=${type1#\'}
elif test -z "$type2" && test "${line#type:}" != "$line"; then
type2=${line##* }; type2=${type2%\'}; type2=${type2#\'}
if test "$type2" != "mirror" && test "$type2" != "raidz"; then
echo "$pool: unexpected non-mirror/non-raidz,\
striping/single intentional? (hardware raid?)"
fi
fi
done
zdb_names=$(zdb_output | sed -ne "s/^[[:blank:]]\+name: '\([^']*\)'/\1/p" |
LC_ALL=C sort) # '") vimsynfix
zpool_names=$(zpool list -Honame | LC_ALL=C sort)
if test "$zdb_names" != "$zpool_names"; then
echo "error: did not get info about all zpools ("$zdb_names"\
<> "$zpool_names")" >&2
false
fi
}
zfs_check_arc() {
local module_arc_min=$(sed -e \
'/^options zfs .*zfs_arc_min=/!d;s/.*zfs_arc_min=//;s/[^0-9].*//' \
/etc/modprobe.d/*.conf); module_arc_min=${module_arc_min:-0}
local module_arc_max=$(sed -e \
'/^options zfs .*zfs_arc_max=/!d;s/.*zfs_arc_max=//;s/[^0-9].*//' \
/etc/modprobe.d/*.conf); module_arc_max=${module_arc_max:-0}
local param_arc_min=$(cat /sys/module/zfs/parameters/zfs_arc_min)
local param_arc_max=$(cat /sys/module/zfs/parameters/zfs_arc_max)
local current_arc=$(awk '/^size/{print $3}' /proc/spl/kstat/zfs/arcstats)
test "$module_arc_min" -ne "$param_arc_min" &&
echo "zfs-kernel-module: running arc min ($param_arc_min) unequal\
to modprobe config ($module_arc_min)"
test "$module_arc_max" -ne "$param_arc_max" &&
echo "zfs-kernel-module: running arc min ($param_arc_max) unequal\
to modprobe config ($module_arc_max)"
local memory=$(free -m | awk '/^Mem:/{print $2}')
local maxpct=$(awk -v mem=$memory -v max=$module_arc_max \
'BEGIN{print int((max/1024/1024*100)/mem+0.5);exit}')
# Allow the current_arc to overflow max by 128MiB.
if test $((current_arc - 128*1024*1024)) -gt $module_arc_max; then
echo "zfs-kernel-module: current arc size ($current_arc)\
not below modprobe config ($module_arc_max)"
fi
if test $maxpct -lt 3 || test $maxpct -gt 85; then
echo "zfs-kernel-module: arc max modprobe config out of normal bounds\
($maxpct% is not between 3% and 85%)"
fi
}
nvme_check_best_sector
zpool_check_ashift
zpool_check_autotrim
zpool_check_stripe
zfs_check_arc
zfs_check_compression
zfs_check_primarycache