-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsg_formatter
374 lines (349 loc) · 12.4 KB
/
sg_formatter
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#!/bin/bash
die() {
SCRIPTNAME=$(basename $(readlink -f $0))
if [ -t 2 ] ; then
for LN in "$@"; do
printf "%s\n" "$LN" >&2
done
else
for LN in "$@"; do
printf "%-15s %-10s %s\n" "$(date +'%F %T')" "${FACILITY:-$SCRIPTNAME}" "$LN" >&2
done
fi
exit 1
}
check_root() {
if [ $(id -g) -eq 0 ]; then
are_root=true
else
are_root=false
fi
[ -z "$1" ] && {
$are_root
return
}
eval $1 && {
eval $are_root || die "Must be run as root"
return
}
eval $1 || {
eval $are_root && die "Must NOT be run as root"
return
}
}
check_commands() {
MSTS=true
for i in $@; do
type -p $i >/dev/null 2>&1 || {
MSTS=false
echo "$i missing"
}
done
$MSTS
}
gen_list() {
sg_map | sort -k 1.8n| while read DEV DEV_BLOCK; do
sg_turs $DEV > /dev/null 2>&1 || continue
eval $(sg_inq -u $DEV |awk -v FS== '{print $1 "=\"" $2 "\""}')
[ "${SCSI_TYPE:-null}" == "disk" ] || continue
[ "${SCSI_MODEL:-null}" == "LOGICAL_VOLUME" ] && continue
[ "${SCSI_MODEL:-null}" == "Virtual_disk" ] && continue
eval $(sg_inq -u --page=0x80 $DEV |awk -v FS== '{print $1 "=\"" $2 "\""}')
read DEV_BLOCKS DEV_BSZ <<< $(sg_readcap -l -b $DEV 2>/dev/null) || continue
DEV_SIZE=$(echo $(($DEV_BLOCKS * $DEV_BSZ))|numfmt --to=iec-i --suffix=B)
DEV_STATE="$(sg_turs --progress $DEV)"
printf "%-6s %-6s %4d %7s %-10s %-20s %-22s %-8s %s\n" \
"${DEV##/dev/}" "${DEV_BLOCK##/dev/}" "$DEV_BSZ" "$DEV_SIZE" "$SCSI_VENDOR" \
"$SCSI_MODEL" "$SCSI_IDENT_SERIAL" "$SCSI_REVISION" "$DEV_STATE"
done
}
list_devices() {
printf "%-6s %-6s %4s %7s %-10s %-20s %-22s %-8s %s\n" \
"#Dev" "BDev" "Bsz" "Size" "Vendor" "Model" "Serial" "Rev" "State"
printf "%-6s %-6s %4s %7s %-10s %-20s %-22s %-8s %s\n" \
"#-----" "------" "---" "-------" "----------" "--------------------" \
"----------------------" "--------" "--------------------"
gen_list |awk 'BEGIN{c=0};{c++;print};END{print "# Total Devices: " c}'
}
parse_input() {
echo "Enter list of devices, one per line, e.g. sg1"
echo "blank lines are ignored, as are those that begin with #"
while read DEV JUNK;do
[ "${DEV:0:1}" = "#" ] && continue
[ -z "$DEV" ] && continue
DEV=$(basename $DEV) ## strip off leading path if needed
if [ ! -e "/dev/$DEV" ]; then
>&2 echo "Error: $DEV is not found"
continue
fi
if [ -b "/dev/$DEV" ] ; then ## convert to /block device format
DEV2=$(sg_map26 /dev/$DEV) || {
>&2 echo "Could not map $DEV to sg device"
continue
}
DEV="$(basename $DEV2)"
fi
eval $(sg_inq -u "/dev/$DEV" |awk -v FS== '{print $1 "=\"" $2 "\""}')
[ "${SCSI_TYPE:-null}" == "disk" ] || {
>&2 echo "Error: $DEV is not a disk"
continue
}
[ "${SCSI_MODEL:-null}" == "LOGICAL_VOLUME" ] && {
>&2 echo "Error: $DEV is a RAID logical volume"
continue
}
[ "${SCSI_MODEL:-null}" == "Virtual_disk" ] && {
>&2 echo "Error: $DEV is a VMware virtual disk"
continue
}
if [ -b /dev/$DEV -o -c /dev/$DEV ]; then
DRIVE_LIST+=($(basename $DEV))
else
>&2 echo "Error: $DEV is not a character or block special device"
continue
fi
done
}
wipe_drives() {
CMD="nwipe $WIPE_OPTS"
if [ -n "$WIPE_LOG" ] ; then
touch "$WIPE_LOG" || die "Error: could not write to wipe log: $WIPE_LOG"
CMD="$CMD --logfile=$WIPE_LOG"
fi
if [ "$CONFIRM_ACTION" != "true" ]; then
echo "Ready to wipe:"
echo "${DRIVE_LIST[@]}"
read -p "Are you sure you want to wipe (y/N): " ANS
typeset -l CHK=${ANS:0:1}
if [ "$CHK" != "y" ]; then
return
fi
fi
${WIPE_GUI:-false} || CMD="${CMD} --nogui --nowait"
declare -a DRIVE_PATHS
for i in ${DRIVE_LIST[@]}; do
dpath=$(sg_map26 /dev/$i)
if [ -b $dpath ]; then
DRIVE_PATHS+=($dpath)
else
2>&1 echo "Warning: /dev/$i could not be mapped to a block device, skipping"
fi
done
CMD="$CMD ${DRIVE_PATHS[*]}"
printf "Wiping:\n"
printf "Drives: \n%s\n" "${DRIVE_LIST[*]}"
printf "Command: \n%s\n" "$CMD"
if [ "${DRY_RUN:-true}" = "false" ]; then
$CMD
fi
exit 0
}
remove_drives() {
for dev in ${DRIVE_LIST[@]}; do
devpath="/sys/class/scsi_generic/${dev}/device/delete"
if [ "$CONFIRM_ACTION" != "true" ]; then
read -p "Are you sure you want to remove $dev (y/N): " ANS
typeset -l CHK=${ANS:0:1}
if [ "$CHK" != "y" ]; then
continue
fi
fi
echo "Removing $dev using $devpath"
if [ "${DRY_RUN:-true}" = "false" ]; then
echo "1" > $devpath
fi
done
exit 0
}
scan_drives() {
if [ -n "${SCAN_LIST}" ]; then
printf "%-10s %-15s %-15s\n" "Bus" "Driver" "Firmware"
printf "%-10s %-15s %-15s\n" "----------" "---------------" "---------------"
fi
for bus in $(find /sys/class/scsi_host -type l -name 'host*' -printf '%f\n'|sort); do
if [ -n "$SCAN_BUS" ]; then
echo "$SCAN_BUS" | grep -q "$bus" || continue
fi
driver=$(</sys/class/scsi_host/${bus}/proc_name)
[ "$driver" = "usb-storage" ] && continue
[ -f /sys/class/scsi_host/${bus}/firmware_revision ] && fwver=$(</sys/class/scsi_host/${bus}/firmware_revision)
[ -f /sys/class/scsi_host/${bus}/version_fw ] && fwver=$(</sys/class/scsi_host/${bus}/version_fw)
if [ -n "${SCAN_LIST}" ]; then
printf "%-10s %-15s %-15s\n" $bus $driver "$fwver"
continue
else
printf "Rescanning %-10s %-15s %-15s" $bus $driver "$fwver"
fi
if [ "${DRY_RUN:-true}" = "false" ]; then
echo "- - -" > /sys/class/scsi_host/${bus}/scan
echo "(done)"
else
echo "(skipped, dry-run)"
fi
done
exit 0
}
format_drives() {
for dev in ${DRIVE_LIST[@]}; do
CMD="sg_format --format --six --early --quick --size=${FORMAT_BLOCK}"
if [ "$FORMAT_MODE" = "quick" ]; then
CMD="$CMD --ffmt=1"
fi
if [ "$CONFIRM_ACTION" != "true" ]; then
read -p "Are you sure you want to format $dev (y/N): " ANS
typeset -l CHK=${ANS:0:1}
if [ "$CHK" != "y" ]; then
continue
fi
fi
CMD="$CMD /dev/$dev"
echo "Formatting $dev: $CMD"
if [ "${DRY_RUN:-true}" = "false" ]; then
$CMD
fi
done
}
monitor_status() {
FORMAT_COLUMNS=1
FORMAT_WIDTH=$(tput cols)
(( ${FORMAT_WIDTH:-0} >= 80 )) && FORMAT_COLUMNS=2
(( ${FORMAT_WIDTH:-0} >= 120 )) && FORMAT_COLUMNS=3
while true; do
clear
echo "Press Ctrl-C to cancel monitoring..."
echo ""
echo "$(date)"
echo ""
for dev in ${DRIVE_LIST[@]}; do
dev_sts="$(sg_turs --progress /dev/$dev)"
printf "%-5s %s\n" "$dev" "${dev_sts:-Complete}"
done|pr -t --columns $FORMAT_COLUMNS -w ${COLUMNS:-80}
sleep ${MONITOR_TIMER:-10}
done
}
show_help() {
cat <<EOD
Option:
-l List Disks (default)
-f Format disks
-w Wipe disks using nwipe - can not be used with Format or Monitor
-m Monitor
-R Remove disks from kernel
-S Rescan all buses for devices (reverses effects of -R)
Format options:
-d Dry Run: does not do the format, displays commands
-y Do not confirm after parsing the disk list.
-b <size> Blocksize to format, default is 512
-q Option to Format using the quick SCSI format mode (-ffmt=1)
Not supported on all drives
Wipe options:
-d Dry Run: does not do the format, displays commands
-y Do not confirm after parsing the disk list.
-g Use GUI mode.
-L file Log file name
-o <string> options for nwipe. Defaults are
--autonuke --verify=off --sync -m prng -p isaac -r 1 --noblank
Remove options:
-d Dry Run: does not do the format, displays commands
-y Do not confirm after parsing disk list
Scan options:
-b Specifies the bus to rescan.
-a Just lists available device hosts to scan
-d Dry Run: does not do the format, displays commands
Monitor options:
-m With format, monitor status after starting
-t With monitor, sets the sleep interval between refreshes
Reads list of disks from stdin, one per line. Lines beginning with # are ignored.
The first word on the line is expected to be a disk name (e.g. sg1).
Anything after the disk name is ignored, so -l can be piped back into sg_formatter
Note: if you pipe a list of devices into sg_formatter, you must use -f
EOD
}
check_root true
check_commands sg_readcap sg_inq sg_format nwipe || die "Install missing commands (usually nwipe and sg3-utils) and retry"
CONFIRM_ACTION="false"
DRY_RUN="false"
MONITOR_TIMER=10
WIPE_REQUESTED="false"
WIPE_GUI="false"
WIPE_LOG=""
WIPE_OPTS="--autonuke --verify=off --sync -m prng -p isaac -r 1 --noblank"
SCAN_BUS=""
SCAN_LIST=""
FORMAT_MODE="normal"
FORMAT_BLOCK=512
FORMAT_MONITOR="false"
FORMAT_REQUESTED="false"
declare -a DRIVE_LIST
if [ -z "$1" ]; then
list_devices
exit 0
fi
while [ -n "$1" ]; do
case "$1" in
-d) DRY_RUN="true"
;;
-y) CONFIRM_ACTION="true"
;;
-l) list_devices
exit 0
;;
-w) WIPE_REQUESTED="true"
${FORMAT_REQUESTED:-false} && die "Format and wipe are incompatible"
${MONITOR_REQUESTED:-false} && die "Monitor and wipe are incompatible"
${SCAN_REQUESTED:-false} && die "Wipe and scan are incompatible"
${REMOVE_REQUESTED:-false} && die "Wipe and remove are incompatible"
;;
-f) FORMAT_REQUESTED="true"
${WIPE_REQUESTED:-false} && die "Format and wipe are incompatible"
${SCAN_REQUESTED:-false} && die "Format and scan are incompatible"
${REMOVE_REQUESTED:-false} && die "Format and remove are incompatible"
;;
-m) FORMAT_MONITOR="true"
${WIPE_REQUESTED:-false} && die "Monitor and wipe are incompatible"
${SCAN_REQUESTED:-false} && die "Monitor and scan are incompatible"
${REMOVE_REQUESTED:-false} && die "Monitor and remove are incompatible"
;;
-S) SCAN_REQUESTED="true"
${REMOVE_REQUESTED:-false} && die "Scan and remove are incompatible"
${WIPE_REQUESTED:-false} && die "Scan and wipe are incompatible"
${FORMAT_REQUESTED:-false} && die "Scan and format are incompatible"
${MONITOR_REQUESTED:-false} && die "Scan and monitor are incompatible"
;;
-R) REMOVE_REQUESTED="true"
${SCAN_REQUESTED:-false} && die "Scan and remove are incompatible"
${WIPE_REQUESTED:-false} && die "Remove and wipe are incompatible"
${FORMAT_REQUESTED:-false} && die "Remove and format are incompatible"
${MONITOR_REQUESTED:-false} && die "Remove and monitor are incompatible"
;;
-a) SCAN_LIST="true"
;;
-o) WIPE_OPTS="$2"
shift
;;
-L) WIPE_LOG="$2"
shift
;;
-b) FORMAT_BLOCK="$2"
SCAN_BUS="$2"
shift
;;
-g) WIPE_GUI="true"
;;
-q) FORMAT_MODE="quick"
;;
-t) MONITOR_TIMER="$2"
shift
;;
*) show_help
exit 0
;;
esac
shift
done
${SCAN_REQUESTED:-false} && scan_drives
parse_input
${REMOVE_REQUESTED:-false} && remove_drives
${WIPE_REQUESTED:-false} && wipe_drives
${FORMAT_REQUESTED:-false} && format_drives
${FORMAT_MONITOR:-false} && monitor_status