forked from Spearfoot/disk-burnin-and-testing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
disk-burnin.sh
executable file
·625 lines (539 loc) · 20 KB
/
disk-burnin.sh
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
#!/bin/sh
################################################################################
#
# disk-burnin.sh
#
################################################################################
################################################################################
# PRE-EXECUTION VALIDATION
################################################################################
# Check if running as root
if [ "$(id -u)" -ne 0 ]; then
echo "ERROR: Must be run as root" >&2
exit 2
fi
# Check required dependencies
readonly DEPENDENCIES="awk badblocks grep sed sleep smartctl"
for dependency in ${DEPENDENCIES}; do
if ! command -v "${dependency}" > /dev/null 2>&1; then
echo "ERROR: Command '${dependency}' not found" >&2
exit 2
fi
done
readonly USAGE=\
"NAME
$(basename "$0") -- disk burn-in program
SYNOPSIS
$(basename "$0") [-h] [-b <block_size>] [-c <num_blocks>] [-e] [-f] [-o <directory>] [-x] <disk>
DESCRIPTION
A script to simplify the process of burning-in disks. Only intended for use
on disks which do not contain any data, such as new disks or disks which
are being tested or re-purposed.
The script runs in dry-run mode by default, so you can check the sleep
durations and to insure that the sequence of commands suits your needs. In
dry-run mode the script does not actually perform any SMART tests or invoke
the sleep or badblocks programs.
In order to perform tests on drives, you will need to provide the -f option.
OPTIONS
-h Show help text
-e Show extended help text
-b <block_size> Override block size (defaults to 8192)
-c <num_blocks> Override concurrent number of blocks tested
-f Force script to run in destructive mode
ALL DATA ON THE DISK WILL BE LOST!
-o <directory> Write log files to <directory> (default: $(pwd))
-x Run full pass of badblocks instead of exiting on first error
<disk> Disk to burn-in (/dev/ may be omitted)
EXAMPLES
$(basename "$0") sda
run in dry-run mode on disk /dev/sda
$(basename "$0") -f /dev/sdb
run in destructive, non-dry mode on disk /dev/sdb
$(basename "$0") -f -o ~/burn-in-logs sdc
run in destructive, non-dry mode on disk /dev/sdc and
write the log files to ~/burn-in-logs directory
"
readonly USAGE_2=\
"EXIT STATUS
exit 0: script finishes successfully
exit 2: dependencies are missing
not running as 'root'
illegal options are provided
NOTES
Be warned that:
1> The script runs badblocks in destructive mode, which erases any data
on the disk.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!! ALL DATA ON THE DISK WILL BE LOST! BE CAREFUL! !!!!
!!!! DO NOT RUN THIS SCRIPT ON DISKS CONTAINING VALUABLE DATA !!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2> Run times for large disks can be several days. Use tmux or screen
to test multiple disks in parallel.
3> Must be run as 'root'.
4> The script has the following dependencies:
smartmontools, available at https://www.smartmontools.org
Uses: grep, awk, sed, sleep, badblocks
Performs this test sequence:
1> Run SMART short test
2> Run badblocks
3> Run SMART extended test
The script sleeps after starting each SMART test, using a duration based on
the polling interval reported by the disk, after which the script will poll
the disk to verify the self-test has completed.
Full SMART information is pulled after each SMART test. All output except
for the sleep command is written to both stdout and the log.
You should monitor the burn-in progress and watch for errors, particularly
any errors reported by badblocks, or these SMART errors:
5 Reallocated_Sector_Ct
196 Reallocated_Event_Count
197 Current_Pending_Sector
198 Offline_Uncorrectable
These indicate possible problems with the drive. You therefore may wish to
abort the remaining tests and proceed with an RMA exchange for new drives or
discard old ones. Please note that this list is not exhaustive.
The script extracts the drive model and serial number and forms a log file-
name of the form 'burnin-[model]_[serial number].log'.
badblocks is invoked with a block size of 8192, the -wsv options, and the
-o option to instruct it to write the list of bad blocks found (if any) to
a file named 'burnin-[model]_[serial number].bb'.
Before using the script on FreeBSD systems (including FreeNAS) you must
first execute this sysctl command to alter the kernel's geometry debug
flags. This allows badblocks to write to the entire disk:
sysctl kern.geom.debugflags=0x10
Also note that badblocks may issue the following warning under FreeBSD /
FreeNAS, which can safely be ignored as it has no effect on testing:
set_o_direct: Inappropiate ioctl for device
Tested operating systems:
FreeNAS 9.10.2 (FreeBSD 10.3-STABLE)
FreeNAS 11.1-U7 (FreeBSD 11.1-STABLE)
FreeNAS 11.2-U8 (FreeBSD 11.2-STABLE)
Ubuntu Server 16.04.2 LTS
CentOS 7.0
Tiny Core Linux 11.1
Tested disks:
Intel
DC S3700 SSD
Model 320 Series SSD
HGST
Deskstar NAS (HDN724040ALE640)
Ultrastar 7K4000 (HUS724020ALE640)
Ultrastar He10
Ultrastar He12
Western Digital
Black (WD6001FZWX)
Gold
Re (WD4000FYYZ)
Seagate
IronWolf NAS HDD 12TB (ST12000VN0008)
VERSIONS
Written by Keith Nash, March 2017
KN, 8 Apr 2017:
Added minimum test durations because some devices don't return accurate
values.
Added code to clean up the log file, removing copyright notices, etc.
No longer echo 'smartctl -t' output to log file as it imparts no useful
information.
Emit test results after tests instead of full 'smartctl -a' output.
Emit full 'smartctl -x' output at the end of all testing.
Minor changes to log output and formatting.
KN, 12 May 2017:
Added code to poll the disk and check for completed self-tests.
As noted above, some disks don't report accurate values for the short
and extended self-test intervals, sometimes by a significant amount.
The original approach using 'fudge' factors wasn't reliable and the
script would finish even though the SMART self-tests had not completed.
The new polling code helps insure that this doesn't happen.
Fixed code to work around annoying differences between sed's behavior
on Linux and FreeBSD.
KN, 8 Jun 2017
Modified parsing of short and extended test durations to accommodate the
values returned by larger drives; we needed to strip out the '(' and ')'
characters surrounding the integer value in order to fetch it reliably.
KN, 19 Aug 2020
Changed DRY_RUN value so that dry runs are no longer the default
setting.
Changed badblocks call to exit immediately on first error.
Set logging directory to current working directory using pwd command.
Reduced default tests so that we run:
1> Short SMART test
2> badblocks
3> Extended SMART test
MS, 9 Sep 2020
Add .editorconfig to streamlime editor behavior for developers.
Remove dependencies on pcregrep and tr.
Add documentation to functions and complex statements.
Reduce code duplication, simplify and decouple code where possible.
Improve portability and resiliency.
Check availability of dependencies during runtim.
Check for root privileges during runtime.
Add option parsing, most notably (-h)elp and -f for non-dry-run mode.
Add dry_run_wrapper() function.
Add disk type detection to skip badblocks for non-mechanical drives.
KN, 5 Oct 2020
Added -x option to control the badblocks -e option, allowing extended testing.
Added smartctl to the list of dependencies.
Changed disk type detection so that we assume all drives are mechanical drives
unless they explicitly return 'Solid State Drive' for Rotational Rate.
Removed datestamp from every line of log output, only emitting it in log headers.
Minor reformatting.
KY, 30 May 2022
Added -b & -c options to control respective badblocks options."
# badblocks default -e option is 1, stop testing if a single error occurs
BB_E_ARG=1
# badblocks default -b option is 1024, but we default to 8192. This allows overriding if desired.
BB_B_ARG=8192
# badblocks default -c option is 64, and this allows overriding
BB_C_ARG=64
# parse options
while getopts ':hefo:b:c:x' option; do
case "${option}" in
h) echo "${USAGE}"
exit
;;
e) echo "${USAGE}"
echo "${USAGE_2}"
exit
;;
f) readonly DRY_RUN=0
;;
o) LOG_DIR="${OPTARG}"
;;
b) BB_B_ARG="${OPTARG}"
;;
c) BB_C_ARG="${OPTARG}"
;;
x) BB_E_ARG=0
;;
:) printf 'Missing argument for -%s\n' "${OPTARG}" >&2
echo "${USAGE}" >&2
exit 2
;;
\?) printf 'Illegal option: -%s\n' "${OPTARG}" >&2
echo "${USAGE}" >&2
exit 2
;;
esac
done
shift $(( OPTIND - 1 ))
if [ -z "$1" ]; then
echo "ERROR: Missing disk argument" >&2
echo "${USAGE}" >&2
exit 2
fi
################################################################################
# CONSTANTS
################################################################################
readonly BB_E_ARG
readonly BB_B_ARG
readonly BB_C_ARG
# Drive to burn-in
DRIVE="$1"
# prepend /dev/ if necessary
if ! printf '%s' "${DRIVE}" | grep "/dev/\w*" > /dev/null 2>&1; then
DRIVE="/dev/${DRIVE}"
fi
readonly DRIVE
if [ ! -e "$DRIVE" ]; then
echo "ERROR: Disk does not exist: $DRIVE" >&2
echo "${USAGE}" >&2
exit 2
fi
# Set to working directory if -o <directory> wasn't provided
[ -z "${LOG_DIR}" ] && LOG_DIR="$(pwd)"
# Trim trailing slashes
LOG_DIR="$(printf '%s' "${LOG_DIR}" | awk '{gsub(/\/+$/, ""); printf $1}')"
readonly LOG_DIR
# System information
readonly HOSTNAME="$(hostname)"
readonly OS_FLAVOR="$(uname)"
# SMART static information
readonly SMART_INFO="$(smartctl --info "${DRIVE}")"
readonly SMART_CAPABILITIES="$(smartctl --capabilities "${DRIVE}")"
##################################################
# Get SMART information value.
# Globals:
# SMART_INFO
# Arguments:
# value identifier:
# !!! Only TWO WORD indentifiers are supported !!!
# !!! Querying e.g. "ATA Version is" will fail !!!
# - Device Model
# - Model Family
# - Serial Number
# Outputs:
# Write value to stdout.
##################################################
get_smart_info_value() {
# $1=$2=""; select all but first two columns
# gsub(/^[ \t]+|[ \t]+$/, ""); replace leading and trailing whitespace
# gsub(/ /, "_"); replace remaining spaces with underscores
# printf $1 print result without newline at the end
printf '%s' "${SMART_INFO}" \
| grep "$1" \
| awk '{$1=$2=""; gsub(/^[ \t]+|[ \t]+$/, ""); gsub(/ /, "_"); printf $1}'
}
##################################################
# Get SMART recommended test duration, in minutes.
# Globals:
# SMART_CAPABILITIES
# Arguments:
# test type:
# - Short
# - Extended
# - Conveyance
# Outputs:
# Write duration to stdout.
##################################################
get_smart_test_duration() {
# '/'"$1"' self-test routine/ match duration depending on test type arg
# getline; jump to next line
# gsub(/\(|\)/, ""); remove parantheses
# printf $4 print 4th column without newline at the end
printf '%s' "${SMART_CAPABILITIES}" \
| awk '/'"$1"' self-test routine/{getline; gsub(/\(|\)/, ""); printf $4}'
}
# Get disk model
DISK_MODEL="$(get_smart_info_value "Device Model")"
[ -z "${DISK_MODEL}" ] && DISK_MODEL="$(get_smart_info_value "Model Family")"
[ -z "${DISK_MODEL}" ] && DISK_MODEL="$(get_smart_info_value "Model Number")"
readonly DISK_MODEL
# Get disk type; unless we get 'Solid State Device' as return value, assume
# we have a mechanical drive.
DISK_TYPE="$(get_smart_info_value "Rotation Rate")"
if printf '%s' "${DISK_TYPE}" | grep -i "solid_state_device" > /dev/null 2>&1; then
DISK_TYPE="SSD"
fi
readonly DISK_TYPE
# Get disk serial number
readonly SERIAL_NUMBER="$(get_smart_info_value "Serial Number")"
# SMART short test duration
readonly SHORT_TEST_MINUTES="$(get_smart_test_duration "Short")"
readonly SHORT_TEST_SECONDS="$(( SHORT_TEST_MINUTES * 60))"
# SMART extended test duration
readonly EXTENDED_TEST_MINUTES="$(get_smart_test_duration "Extended")"
readonly EXTENDED_TEST_SECONDS="$(( EXTENDED_TEST_MINUTES * 60 ))"
# Maximum duration the completion status is polled
readonly POLL_TIMEOUT_HOURS=4
readonly POLL_TIMEOUT_SECONDS="$(( POLL_TIMEOUT_HOURS * 60 * 60))"
# Sleep interval between completion status polls
readonly POLL_INTERVAL_SECONDS=15
# Form log file names
readonly LOG_FILE="${LOG_DIR}/burnin-${DISK_MODEL}_${SERIAL_NUMBER}.log"
readonly BB_File="${LOG_DIR}/burnin-${DISK_MODEL}_${SERIAL_NUMBER}.bb"
################################################################################
# FUNCTIONS
################################################################################
##################################################
# Log informational message.
# Globals:
# LOG_FILE
# Arguments:
# Message to log.
# Outputs:
# Write message to stdout and log file.
##################################################
log_info() {
# now="$(date +"%F %T %Z")"
# printf "%s\n" "[${now}] $1" | tee -a "${LOG_FILE}"
printf "%s\n" "$1" | tee -a "${LOG_FILE}"
}
##################################################
# Log emphasized header message.
# Arguments:
# Message to log.
##################################################
log_header() {
log_info "+-----------------------------------------------------------------------------"
log_info "+ $1: $(date)"
log_info "+-----------------------------------------------------------------------------"
}
##################################################
# Ensure log directory exists and remove old logs.
# Globals:
# LOG_DIR
# LOG_FILE
# Arguments:
# None
##################################################
init_log() {
mkdir -p -- "${LOG_DIR}" || exit 2
[ -e "${LOG_FILE}" ] && rm -- "${LOG_FILE}"
}
##################################################
# Remove redundant messages from log.
# Globals:
# LOG_FILE
# OS_FLAVOR
# Arguments:
# None
##################################################
cleanup_log() {
if [ "${OS_FLAVOR}" = "Linux" ]; then
sed -i -e '/Copyright/d' "${LOG_FILE}"
sed -i -e '/=== START OF READ/d' "${LOG_FILE}"
sed -i -e '/SMART Attributes Data/d' "${LOG_FILE}"
sed -i -e '/Vendor Specific SMART/d' "${LOG_FILE}"
sed -i -e '/SMART Error Log Version/d' "${LOG_FILE}"
fi
if [ "${OS_FLAVOR}" = "FreeBSD" ]; then
sed -i '' -e '/Copyright/d' "${LOG_FILE}"
sed -i '' -e '/=== START OF READ/d' "${LOG_FILE}"
sed -i '' -e '/SMART Attributes Data/d' "${LOG_FILE}"
sed -i '' -e '/Vendor Specific SMART/d' "${LOG_FILE}"
sed -i '' -e '/SMART Error Log Version/d' "${LOG_FILE}"
fi
}
##################################################
# Log command in dry-run mode, run command otherwise.
# Globals:
# DRY_RUN
# Arguments:
# Command to run.
##################################################
dry_run_wrapper() {
if [ -z "$DRY_RUN" ]; then
log_info "DRY RUN: $*"
return 0
fi
eval "$@"
}
##################################################
# Log runtime information about current burn-in.
# Globals:
# HOSTNAME
# OS_FLAVOR
# DRIVE
# DISK_TYPE
# DISK_MODEL
# SERIAL_NUMBER
# SHORT_TEST_MINUTES
# SHORT_TEST_SECONDS
# EXTENDED_TEST_MINUTES
# EXTENDED_TEST_SECONDS
# LOG_FILE
# BB_File
# Arguments:
# None
##################################################
log_runtime_info() {
log_info "Host: ${HOSTNAME}"
log_info "OS: ${OS_FLAVOR}"
log_info "Drive: ${DRIVE}"
log_info "Disk Type: ${DISK_TYPE}"
log_info "Drive Model: ${DISK_MODEL}"
log_info "Serial Number: ${SERIAL_NUMBER}"
log_info "Short test duration: ${SHORT_TEST_MINUTES} minutes"
log_info " ${SHORT_TEST_SECONDS} seconds"
log_info "Extended test duration: ${EXTENDED_TEST_MINUTES} minutes"
log_info " ${EXTENDED_TEST_SECONDS} seconds"
log_info "Log file: ${LOG_FILE}"
log_info "Bad blocks file: ${BB_File}"
}
##################################################
# Poll repeatedly whether SMART self-test has completed.
# Globals:
# DRIVE
# POLL_INTERVAL_SECONDS
# POLL_TIMEOUT_SECONDS
# Arguments:
# None
# Returns:
# 0 if success or failure.
# 1 if timeout threshold exceeded.
##################################################
poll_selftest_complete() {
l_poll_duration_seconds=0
while [ "${l_poll_duration_seconds}" -lt "${POLL_TIMEOUT_SECONDS}" ]; do
smartctl --all "${DRIVE}" \
| grep -i "The previous self-test routine completed" > /dev/null 2>&1
l_status="$?"
if [ "${l_status}" -eq 0 ]; then
log_info "SMART self-test succeeded"
return 0
fi
smartctl --all "${DRIVE}" \
| grep -i "of the test failed\." > /dev/null 2>&1
l_status="$?"
if [ "${l_status}" -eq 0 ]; then
log_info "SMART self-test failed"
return 0
fi
sleep "${POLL_INTERVAL_SECONDS}"
l_poll_duration_seconds="$(( l_poll_duration_seconds + POLL_INTERVAL_SECONDS ))"
done
log_info "SMART self-test timeout threshold exceeded"
return 1
}
##################################################
# Run SMART test and log results.
# Globals:
# DRIVE
# LOG_FILE
# Arguments:
# Test type:
# - short
# - long
# Test duration in seconds.
##################################################
run_smart_test() {
log_header "Running SMART $1 test"
dry_run_wrapper "smartctl --test=\"$1\" \"${DRIVE}\""
log_info "SMART $1 test started, awaiting completion for $2 seconds ..."
dry_run_wrapper "sleep \"$2\""
dry_run_wrapper "poll_selftest_complete"
dry_run_wrapper "smartctl --log=selftest \"${DRIVE}\" | tee -a \"${LOG_FILE}\""
log_info "Finished SMART $1 test"
}
##################################################
# Run badblocks test.
# !!! ALL DATA ON THE DISK WILL BE LOST !!!
# Globals:
# BB_File
# DISK_TYPE
# DRIVE
# Arguments:
# None
##################################################
run_badblocks_test() {
log_header "Running badblocks test"
if [ "${DISK_TYPE}" != "SSD" ]; then
dry_run_wrapper "badblocks -b ${BB_B_ARG} -wsv -c ${BB_C_ARG} -e ${BB_E_ARG} -o \"${BB_File}\" \"${DRIVE}\""
else
log_info "SKIPPED: badblocks for ${DISK_TYPE} device"
fi
log_info "Finished badblocks test"
}
##################################################
# Log extensive SMART and non-SMART drive information.
# Globals:
# DRIVE
# LOG_FILE
# Arguments:
# None
##################################################
log_full_device_info() {
log_header "Drive information"
dry_run_wrapper "smartctl --xall --vendorattribute=7,hex48 \"${DRIVE}\" | tee -a \"${LOG_FILE}\""
}
##################################################
# Main function of script.
# Globals:
# SHORT_TEST_SECONDS
# EXTENDED_TEST_SECONDS
# Arguments:
# None
##################################################
main() {
init_log
log_header "Started burn-in"
log_runtime_info
# test sequence
run_smart_test "short" "${SHORT_TEST_SECONDS}"
run_badblocks_test
run_smart_test "long" "${EXTENDED_TEST_SECONDS}"
log_full_device_info
log_header "Finished burn-in"
cleanup_log
}
# Entrypoint
main