From 0b36a8658a9abe2520633bb96797ac2a183a02b1 Mon Sep 17 00:00:00 2001 From: Michael Schnerring <3743342+schnerring@users.noreply.github.com> Date: Tue, 8 Sep 2020 16:00:05 +0200 Subject: [PATCH 01/14] add .editorconfig; remove trailing whitespace add myself to license.txt --- .editorconfig | 12 ++++++++++++ disk-burnin.sh | 47 +++++++++++++++++++++++------------------------ license.txt | 3 ++- 3 files changed, 37 insertions(+), 25 deletions(-) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..d4f3a25 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,12 @@ +root = true + +[*] +charset = utf-8 +end_of_line = lf +indent_size = 2 +indent_style = space +insert_final_newline = true +trim_trailing_whitespace = true + +[*.md] +trim_trailing_whitespace = false \ No newline at end of file diff --git a/disk-burnin.sh b/disk-burnin.sh index 768056f..7792f11 100644 --- a/disk-burnin.sh +++ b/disk-burnin.sh @@ -18,10 +18,10 @@ # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! # # 2> Run times for large disks can take several days to complete, so it -# is a good idea to use tmux sessions to prevent mishaps. +# is a good idea to use tmux sessions to prevent mishaps. # # 3> Must be run as 'root'. -# +# # 4> Tests of large drives can take days to complete: use tmux! # # Performs these steps: @@ -30,7 +30,7 @@ # 2> Run badblocks # 3> Run SMART extended test # -# The script sleeps after starting each SMART test, using a duration +# 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. # @@ -40,10 +40,10 @@ # 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 +# 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 @@ -55,28 +55,28 @@ # # badblocks is invoked with a block size of 4096, 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'. -# +# any) to a file named 'burnin-[model]_[serial number].bb'. +# # The only required command-line argument is the device specifier, e.g.: # -# ./disk-burnin.sh sda +# ./disk-burnin.sh sda # # ...will run the burn-in test on device /dev/sda # # You can run the script in 'dry run mode' (see below) to check the sleep # duration calculations and to insure that the sequence of commands suits -# your needs. In 'dry runs' the script does not actually perform any +# your needs. In 'dry runs' the script does not actually perform any # SMART tests or invoke the sleep or badblocks programs. The script is # distributed with 'dry runs' enabled, so you will need to edit the # Dry_Run variable below, setting it to 0, in order to actually perform # tests on drives. -# +# # 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 -# +# # Tested under: # FreeNAS 9.10.2 (FreeBSD 10.3-STABLE) # Ubuntu Server 16.04.2 LTS @@ -94,7 +94,7 @@ # Uses: grep, pcregrep, awk, sed, tr, sleep, badblocks # # 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. @@ -102,16 +102,16 @@ # 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 +# 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. # @@ -128,7 +128,7 @@ # 1> Short SMART test # 2> badblocks # 3> Extended SMART test -# +# ######################################################################## if [ $# -ne 1 ]; then @@ -144,7 +144,7 @@ Drive=$1 Dry_Run=0 -# Directory specifiers for log and badblocks data files. Leave off the +# Directory specifiers for log and badblocks data files. Leave off the # trailing slash if you specify a value. Default is the current working # directory. @@ -227,7 +227,7 @@ poll_selftest_complete() # Return 0 if the test has completed, 1 if we exceed our polling timeout interval while [ $l_done -eq 0 ]; - do + do smartctl -a /dev/"$Drive" | grep -i "The previous self-test routine completed" > /dev/null 2<&1 l_status=$? if [ $l_status -eq 0 ]; then @@ -235,7 +235,7 @@ poll_selftest_complete() l_rv=0 l_done=1 else - # Check for failure + # Check for failure smartctl -a /dev/"$Drive" | grep -i "of the test failed." > /dev/null 2<&1 l_status=$? if [ $l_status -eq 0 ]; then @@ -255,7 +255,7 @@ poll_selftest_complete() done return $l_rv -} +} run_short_test() { @@ -367,4 +367,3 @@ if [ "${osflavor}" = "FreeBSD" ]; then sed -i '' -e '/Vendor Specific SMART/d' "${Log_File}" sed -i '' -e '/SMART Error Log Version/d' "${Log_File}" fi - diff --git a/license.txt b/license.txt index 791fc35..dfd4a46 100644 --- a/license.txt +++ b/license.txt @@ -1,6 +1,7 @@ MIT License -Copyright (c) 2017 by Keith Nash +Original work Copyright (c) 2017 by Keith Nash +Modified work Copyright (c) 2020 by Michael Schnerring Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal From 8744bbd5d02116cd3f6ec9fa026d1d5ba9146c9a Mon Sep 17 00:00:00 2001 From: Michael Schnerring <3743342+schnerring@users.noreply.github.com> Date: Tue, 8 Sep 2020 16:00:29 +0200 Subject: [PATCH 02/14] fix markdownlint issues in README.md --- README.md | 76 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 43 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index a9c86e1..ed85bbb 100644 --- a/README.md +++ b/README.md @@ -1,26 +1,30 @@ # Shell script for burn-in and testing of drives + ## Purpose -`disk-burnin.sh` is a POSIX-compliant shell script I wrote to simplify the process of burning-in disks. It is intended for use only on disks which do not contain data, such as new disks or disks which are being tested or re-purposed. I was inspired by the ["How To: Hard Drive Burn-In Testing"](https://forums.freenas.org/index.php?threads/how-to-hard-drive-burn-in-testing.21451/) thread on the FreeNAS forum and I want to give full props to the good folks who contributed to that thread. + +`disk-burnin.sh` is a POSIX-compliant shell script I wrote to simplify the process of burning-in disks. It is intended for use only on disks which do not contain data, such as new disks or disks which are being tested or re-purposed. I was inspired by the ["How To: Hard Drive Burn-In Testing"](https://forums.freenas.org/index.php?threads/how-to-hard-drive-burn-in-testing.21451/) thread on the FreeNAS forum and I want to give full props to the good folks who contributed to that thread. ## Warnings -Be warned that: - + +Be warned that: + * This script runs the `badblocks` program in destructive mode, which erases any data on the disk. Therefore, please be careful! __Do not run this script on disks containing data you value!__ -* Run times for large disks can take several days to a week or more to complete, so it is a good idea to use `tmux` sessions to prevent mishaps. -* Must be run as 'root', so either log on using the root account or use the `sudo` command, for example: `sudo ./disk_burnin.sh sda` - -## Tests -Performs these steps: - +* Run times for large disks can take several days to a week or more to complete, so it is a good idea to use `tmux` sessions to prevent mishaps. +* Must be run as 'root', so either log on using the root account or use the `sudo` command, for example: `sudo ./disk_burnin.sh sda` + +## Tests + +Performs these steps: + 1. Run SMART short test -1. Run `badblocks` -1. Run SMART extended test +2. Run `badblocks` +3. Run SMART extended test The script calls `sleep` after starting each SMART test, using a duration based on the polling interval reported by the disk, after which it polls for test completion. -Full SMART information is pulled after each SMART test. All output except for the `sleep` command is echoed to both the screen and log file. - -You should periodically monitor the burn-in progress and check for errors, particularly any errors reported by `badblocks`, or these SMART errors: +Full SMART information is pulled after each SMART test. All output except for the `sleep` command is echoed to both the screen and log file. + +You should periodically monitor the burn-in progress and check for errors, particularly any errors reported by `badblocks`, or these SMART errors: |ID|Attribute Name| |---:|---| @@ -28,32 +32,34 @@ You should periodically monitor the burn-in progress and check for errors, parti |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. Also please note that this list is not exhaustive. - + The script extracts the drive model and serial number and creates a log filename of the form `burnin-[model]_[serial number].log`. ## `badblocks` Options + `badblocks` is invoked with the following options: -- `-b 4096` : Use a block size of 4096 -- `-e 1` : Abort the test if an error is found (remove this option for full testing of drives) -- `-v` : Verbose mode -- `-o` : Write list of bad blocks found (if any) to a file named `burnin-[model]_[serial number].bb` -- `-s` : Show progress -- `-w` : Write-mode test, writes four patterns (0xaa, 0x55, 0x44, 0x00) on every disk block - + +* `-b 4096` : Use a block size of 4096 +* `-e 1` : Abort the test if an error is found (remove this option for full testing of drives) +* `-v` : Verbose mode +* `-o` : Write list of bad blocks found (if any) to a file named `burnin-[model]_[serial number].bb` +* `-s` : Show progress +* `-w` : Write-mode test, writes four patterns (0xaa, 0x55, 0x44, 0x00) on every disk block + The only required command-line argument is the device specifier, e.g.: - + `./disk-burnin.sh sda` - + ...will run the burn-in test on device /dev/sda - + ## Dry Run Mode The script supports a 'dry run mode' which lets you check the sleep duration calculations and insure that the sequence of commands suits your needs without actually performing any operations on disks. In 'dry runs' the script does not perform any SMART tests or invoke the `sleep` or `badblocks` programs. The script was formerly distributed with 'dry run mode' enabled by default, but this is no longer the case. You will have to edit the script and set the `Dry_Run` variable to a non-zero value to enable 'dry runs'. - + ## `smartctl` Device Type Some users with atypical hardware environments may need to modify the script and specify the `smartctl` command device type explictly with the `-d` option. User __bcmryan__ reports success using `-d sat` with a Western Digital MyBook 8TB external drive enclosure. @@ -70,26 +76,30 @@ Also note that `badblocks` may issue the following warning under FreeBSD/FreeNAS ## Operating System Compatibility -Tested under: +Tested under: + * FreeNAS 9.10.2-U1 (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 +* Ubuntu Server 16.04.2 LTS * CentOS 7.0 ## Drive Models Tested -The script should run successfully on any SATA disk with SMART capabilities, which includes just about all modern drives. It has been tested on these particular devices: +The script should run successfully on any SATA disk with SMART capabilities, which includes just about all modern drives. It has been tested on these particular devices: + * HGST Deskstar NAS, UltraStar, UltraStar He10, and UltraStar He12 models * Western Digital Gold, Black, and Re models ## Prerequisites -Requires the smartmontools, available at https://www.smartmontools.org - + +Requires the smartmontools, available at [www.smartmontools.org](https://www.smartmontools.org) + Uses: `grep`, `pcregrep`, `awk`, `sed`, `tr`, `sleep`, `badblocks` -Tested with the static analysis tool at https://www.shellcheck.net to insure that the code is POSIX-compliant and free of issues. +Tested with the static analysis tool at [www.shellcheck.net](https://www.shellcheck.net) to insure that the code is POSIX-compliant and free of issues. ## Author + Written by Keith Nash, March 2017. Modified on 19 August 2020. From 492e4ca429f2807bf1bbb1654773d7e488ce6e0e Mon Sep 17 00:00:00 2001 From: Michael Schnerring <3743342+schnerring@users.noreply.github.com> Date: Tue, 8 Sep 2020 21:16:36 +0200 Subject: [PATCH 03/14] remove dependency on pcregrep and tr move SMART info querying to well documented functions for readability --- README.md | 2 +- disk-burnin.sh | 65 +++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 55 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index ed85bbb..484fdf0 100644 --- a/README.md +++ b/README.md @@ -95,7 +95,7 @@ The script should run successfully on any SATA disk with SMART capabilities, whi Requires the smartmontools, available at [www.smartmontools.org](https://www.smartmontools.org) -Uses: `grep`, `pcregrep`, `awk`, `sed`, `tr`, `sleep`, `badblocks` +Uses: `grep`, `awk`, `sed`, `sleep`, `badblocks` Tested with the static analysis tool at [www.shellcheck.net](https://www.shellcheck.net) to insure that the code is POSIX-compliant and free of issues. diff --git a/disk-burnin.sh b/disk-burnin.sh index 7792f11..fe43a8b 100644 --- a/disk-burnin.sh +++ b/disk-burnin.sh @@ -91,7 +91,7 @@ # # Requires the smartmontools, available at https://www.smartmontools.org # -# Uses: grep, pcregrep, awk, sed, tr, sleep, badblocks +# Uses: grep, awk, sed, sleep, badblocks # # Written by Keith Nash, March 2017 # @@ -161,15 +161,40 @@ BB_Dir=$(pwd) # ######################################################################## -# Obtain the disk model and serial number: +# SMART static information +readonly SMART_INFO="$(smartctl --info "/dev/${Drive}")" +readonly SMART_CAPABILITIES="$(smartctl --capabilities "/dev/${Drive}")" -Disk_Model=$(smartctl -i /dev/"$Drive" | grep "Device Model" | awk '{print $3, $4, $5}' | sed -e 's/^[ \t]*//;s/[ \t]*$//' | sed -e 's/ /_/') +################################################## +# 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}' +} -if [ -z "$Disk_Model" ]; then - Disk_Model=$(smartctl -i /dev/"$Drive" | grep "Model Family" | awk '{print $3, $4, $5}' | sed -e 's/^[ \t]*//;s/[ \t]*$//' | sed -e 's/ /_/') -fi +# Get disk model +Disk_Model="$(get_smart_info_value "Device Model")" +[ -z "${Disk_Model}" ] && Disk_Model="$(get_smart_info_value "Model Family")" -Serial_Number=$(smartctl -i /dev/"$Drive" | grep -i "Serial Number" | awk '{print $3}' | sed -e 's/ /_/') +# Get disk serial number +Serial_Number="$(get_smart_info_value "Serial Number")" # Form the log and bad blocks data filenames: @@ -179,13 +204,31 @@ Log_File=$Log_Dir/$Log_File BB_File="burnin-${Disk_Model}_${Serial_Number}.bb" BB_File=$BB_Dir/$BB_File -# Query the short and extended test duration, in minutes. Use the values to -# calculate how long we should sleep after starting the SMART tests: +################################################## +# 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}' +} -Short_Test_Minutes=$(smartctl -c /dev/"$Drive" | pcregrep -M "Short self-test routine.*\n.*recommended polling time:" | sed -e 's/)//;s/(//' | awk '{print $4}' | tr -d '\n') +Short_Test_Minutes="$(get_smart_test_duration "Short")" #printf "Short_Test_Minutes=[%s]\n" ${Short_Test_Minutes} -Extended_Test_Minutes=$(smartctl -c /dev/"$Drive" | pcregrep -M "Extended self-test routine.*\n.*recommended polling time:" | sed -e 's/)//;s/(//' | awk '{print $4}' | tr -d '\n') +Extended_Test_Minutes="$(get_smart_test_duration "Extended")" #printf "Extended_Test_Minutes=[%s]\n" ${Extended_Test_Minutes} Short_Test_Sleep=$((Short_Test_Minutes*60)) From 40e114da98241add35eb03c145a6013f9411ad37 Mon Sep 17 00:00:00 2001 From: Michael Schnerring <3743342+schnerring@users.noreply.github.com> Date: Tue, 8 Sep 2020 21:17:26 +0200 Subject: [PATCH 04/14] rename duration parameters for expressiveness add more detailed comments to duration parameters --- disk-burnin.sh | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/disk-burnin.sh b/disk-burnin.sh index fe43a8b..5e44020 100644 --- a/disk-burnin.sh +++ b/disk-burnin.sh @@ -225,23 +225,23 @@ get_smart_test_duration() { | awk '/'"$1"' self-test routine/{getline; gsub(/\(|\)/, ""); printf $4}' } +# The script initially sleeps for a duration after a test is started. +# Afterwards the completion status is repeatedly polled. + +# SMART short test duration Short_Test_Minutes="$(get_smart_test_duration "Short")" -#printf "Short_Test_Minutes=[%s]\n" ${Short_Test_Minutes} +Short_Test_Seconds="$(( Short_Test_Minutes * 60))" +# SMART extended test duration Extended_Test_Minutes="$(get_smart_test_duration "Extended")" -#printf "Extended_Test_Minutes=[%s]\n" ${Extended_Test_Minutes} +Extended_Test_Seconds="$(( Extended_Test_Minutes * 60 ))" -Short_Test_Sleep=$((Short_Test_Minutes*60)) -Extended_Test_Sleep=$((Extended_Test_Minutes*60)) - -# Selftest polling timeout interval, in hours +# Maximum duration the completion status is polled Poll_Timeout_Hours=4 +Poll_Timeout_Seconds="$(( Poll_Timeout_Hours * 60 * 60))" -# Calculate the selftest polling timeout interval in seconds -Poll_Timeout=$((Poll_Timeout_Hours*60*60)) - -# Polling sleep interval, in seconds: -Poll_Interval=15 +# Sleep interval between completion status polls +Poll_Interval_Seconds=15 ######################################################################## # @@ -286,12 +286,12 @@ poll_selftest_complete() l_rv=0 l_done=1 else - if [ $l_pollduration -ge "${Poll_Timeout}" ]; then + if [ $l_pollduration -ge "${Poll_Timeout_Seconds}" ]; then echo_str "Timeout polling for SMART self-test status" l_done=1 else - sleep ${Poll_Interval} - l_pollduration=$((l_pollduration+Poll_Interval)) + sleep ${Poll_Interval_Seconds} + l_pollduration=$((l_pollduration+Poll_Interval_Seconds)) fi fi fi @@ -307,12 +307,12 @@ run_short_test() push_header if [ "${Dry_Run}" -eq 0 ]; then smartctl -t short /dev/"$Drive" - echo_str "Short test started, sleeping ${Short_Test_Sleep} seconds until it finishes" - sleep ${Short_Test_Sleep} + echo_str "Short test started, sleeping ${Short_Test_Seconds} seconds until it finishes" + sleep ${Short_Test_Seconds} poll_selftest_complete smartctl -l selftest /dev/"$Drive" | tee -a "$Log_File" else - echo_str "Dry run: would start the SMART short test and sleep ${Short_Test_Sleep} seconds until the test finishes" + echo_str "Dry run: would start the SMART short test and sleep ${Short_Test_Seconds} seconds until the test finishes" fi echo_str "Finished SMART short test on drive /dev/${Drive}: $(date)" } @@ -324,12 +324,12 @@ run_extended_test() push_header if [ "${Dry_Run}" -eq 0 ]; then smartctl -t long /dev/"$Drive" - echo_str "Extended test started, sleeping ${Extended_Test_Sleep} seconds until it finishes" - sleep ${Extended_Test_Sleep} + echo_str "Extended test started, sleeping ${Extended_Test_Seconds} seconds until it finishes" + sleep ${Extended_Test_Seconds} poll_selftest_complete smartctl -l selftest /dev/"$Drive" | tee -a "$Log_File" else - echo_str "Dry run: would start the SMART extended test and sleep ${Extended_Test_Sleep} seconds until the test finishes" + echo_str "Dry run: would start the SMART extended test and sleep ${Extended_Test_Seconds} seconds until the test finishes" fi echo_str "Finished SMART extended test on drive /dev/${Drive}: $(date)" } @@ -368,9 +368,9 @@ echo_str "Host: $(hostname)" echo_str "Drive Model: ${Disk_Model}" echo_str "Serial Number: ${Serial_Number}" echo_str "Short test duration: ${Short_Test_Minutes} minutes" -echo_str "Short test sleep duration: ${Short_Test_Sleep} seconds" +echo_str "Short test sleep duration: ${Short_Test_Seconds} seconds" echo_str "Extended test duration: ${Extended_Test_Minutes} minutes" -echo_str "Extended test sleep duration: ${Extended_Test_Sleep} seconds" +echo_str "Extended test sleep duration: ${Extended_Test_Seconds} seconds" echo_str "Log file: ${Log_File}" echo_str "Bad blocks file: ${BB_File}" From cfaedeac28b1046783a0ff955561c03fced7f39d Mon Sep 17 00:00:00 2001 From: Michael Schnerring <3743342+schnerring@users.noreply.github.com> Date: Tue, 8 Sep 2020 21:18:11 +0200 Subject: [PATCH 05/14] simplify and document poll_selftest_complete() --- disk-burnin.sh | 62 +++++++++++++++++++++++--------------------------- 1 file changed, 28 insertions(+), 34 deletions(-) diff --git a/disk-burnin.sh b/disk-burnin.sh index 5e44020..e2db351 100644 --- a/disk-burnin.sh +++ b/disk-burnin.sh @@ -259,45 +259,39 @@ push_header() echo_str "+-----------------------------------------------------------------------------" } +################################################## +# 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_rv=1 - l_status=0 - l_done=0 - l_pollduration=0 - -# Check SMART results for "The previous self-test routine completed" -# Return 0 if the test has completed, 1 if we exceed our polling timeout interval - - while [ $l_done -eq 0 ]; - do - smartctl -a /dev/"$Drive" | grep -i "The previous self-test routine completed" > /dev/null 2<&1 + l_poll_duration_seconds=0 + while [ "${l_poll_duration_seconds}" -lt "${Poll_Timeout_Seconds}" ]; do + smartctl --all "/dev/${Drive}" | grep -i "The previous self-test routine completed" > /dev/null 2<&1 l_status=$? - if [ $l_status -eq 0 ]; then - echo_str "SMART self-test complete" - l_rv=0 - l_done=1 - else - # Check for failure - smartctl -a /dev/"$Drive" | grep -i "of the test failed." > /dev/null 2<&1 - l_status=$? - if [ $l_status -eq 0 ]; then - echo_str "SMART self-test failed" - l_rv=0 - l_done=1 - else - if [ $l_pollduration -ge "${Poll_Timeout_Seconds}" ]; then - echo_str "Timeout polling for SMART self-test status" - l_done=1 - else - sleep ${Poll_Interval_Seconds} - l_pollduration=$((l_pollduration+Poll_Interval_Seconds)) - fi - fi + if [ "${l_status}" -eq 0 ]; then + echo_str "SMART self-test succeeded" + return 0 fi + smartctl --all "/dev/${Drive}" | grep -i "of the test failed." > /dev/null 2<&1 + l_status=$? + if [ "${l_status}" -eq 0 ]; then + echo_str "SMART self-test failed" + return 0 + fi + sleep "${Poll_Interval_Seconds}" + l_poll_duration_seconds="$(( l_poll_duration_seconds + Poll_Interval_Seconds ))" done - - return $l_rv + echo_str "SMART self-test timeout threshold exceeded" + return 1 } run_short_test() From 8b3c16cebfde60436577841884cecc55029a4749 Mon Sep 17 00:00:00 2001 From: Michael Schnerring <3743342+schnerring@users.noreply.github.com> Date: Tue, 8 Sep 2020 21:22:57 +0200 Subject: [PATCH 06/14] implement dependency check --- disk-burnin.sh | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/disk-burnin.sh b/disk-burnin.sh index e2db351..b9f31f0 100644 --- a/disk-burnin.sh +++ b/disk-burnin.sh @@ -131,6 +131,16 @@ # ######################################################################## +# Check required dependencies +readonly DEPENDENCIES="awk badblocks grep sed sleep" +for dependency in ${DEPENDENCIES}; do + if ! command -v "${dependency}" > /dev/null 2>&1 ; then + echo "Command '${dependency}' not found. Exiting ..." + exit 2 + fi +done + +# Check script arguments if [ $# -ne 1 ]; then echo "Error: not enough arguments!" echo "Usage is: $0 drive_device_specifier" From 3a21b21eca924e563a9522302a44c1833e396df7 Mon Sep 17 00:00:00 2001 From: Michael Schnerring <3743342+schnerring@users.noreply.github.com> Date: Tue, 8 Sep 2020 21:45:19 +0200 Subject: [PATCH 07/14] refactor logging use printf instead of echo to improve portability rename echo_str to log_info create log_header function to reduce code duplication --- disk-burnin.sh | 89 ++++++++++++++++++++++++++------------------------ 1 file changed, 47 insertions(+), 42 deletions(-) diff --git a/disk-burnin.sh b/disk-burnin.sh index b9f31f0..db8274a 100644 --- a/disk-burnin.sh +++ b/disk-burnin.sh @@ -259,14 +259,31 @@ Poll_Interval_Seconds=15 # ######################################################################## -echo_str() +################################################## +# Log informational message. +# Globals: +# Log_File +# Arguments: +# Message to log. +# Outputs: +# Write message to stdout and log file. +################################################## +log_info() { - echo "$1" | tee -a "$Log_File" + now="$(date +"%F %T %Z")" + printf "%s\n" "[${now}] $1" | tee -a "${Log_File}" } -push_header() +################################################## +# Log emphasized header message. +# Arguments: +# Message to log. +################################################## +log_header() { - echo_str "+-----------------------------------------------------------------------------" + log_info "+-----------------------------------------------------------------------------" + log_info "+ $1" + log_info "+-----------------------------------------------------------------------------" } ################################################## @@ -288,70 +305,64 @@ poll_selftest_complete() smartctl --all "/dev/${Drive}" | grep -i "The previous self-test routine completed" > /dev/null 2<&1 l_status=$? if [ "${l_status}" -eq 0 ]; then - echo_str "SMART self-test succeeded" + log_info "SMART self-test succeeded" return 0 fi smartctl --all "/dev/${Drive}" | grep -i "of the test failed." > /dev/null 2<&1 l_status=$? if [ "${l_status}" -eq 0 ]; then - echo_str "SMART self-test failed" + 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 - echo_str "SMART self-test timeout threshold exceeded" + log_info "SMART self-test timeout threshold exceeded" return 1 } run_short_test() { - push_header - echo_str "+ Run SMART short test on drive /dev/${Drive}: $(date)" - push_header + log_header "Run SMART short test on drive /dev/${Drive}" if [ "${Dry_Run}" -eq 0 ]; then smartctl -t short /dev/"$Drive" - echo_str "Short test started, sleeping ${Short_Test_Seconds} seconds until it finishes" + log_info "Short test started, sleeping ${Short_Test_Seconds} seconds until it finishes" sleep ${Short_Test_Seconds} poll_selftest_complete smartctl -l selftest /dev/"$Drive" | tee -a "$Log_File" else - echo_str "Dry run: would start the SMART short test and sleep ${Short_Test_Seconds} seconds until the test finishes" + log_info "Dry run: would start the SMART short test and sleep ${Short_Test_Seconds} seconds until the test finishes" fi - echo_str "Finished SMART short test on drive /dev/${Drive}: $(date)" + log_info "Finished SMART short test on drive /dev/${Drive}: $(date)" } run_extended_test() { - push_header - echo_str "+ Run SMART extended test on drive /dev/${Drive}: $(date)" - push_header + log_header "Run SMART extended test on drive /dev/${Drive}" if [ "${Dry_Run}" -eq 0 ]; then smartctl -t long /dev/"$Drive" - echo_str "Extended test started, sleeping ${Extended_Test_Seconds} seconds until it finishes" + log_info "Extended test started, sleeping ${Extended_Test_Seconds} seconds until it finishes" sleep ${Extended_Test_Seconds} poll_selftest_complete smartctl -l selftest /dev/"$Drive" | tee -a "$Log_File" else - echo_str "Dry run: would start the SMART extended test and sleep ${Extended_Test_Seconds} seconds until the test finishes" + log_info "Dry run: would start the SMART extended test and sleep ${Extended_Test_Seconds} seconds until the test finishes" fi - echo_str "Finished SMART extended test on drive /dev/${Drive}: $(date)" + log_info "Finished SMART extended test on drive /dev/${Drive}: $(date)" } run_badblocks_test() { - push_header - echo_str "+ Run badblocks test on drive /dev/${Drive}: $(date)" - push_header + log_header "Run badblocks test on drive /dev/${Drive}" if [ "${Dry_Run}" -eq 0 ]; then # # This is the command which erases all data on the disk: # badblocks -b 4096 -wsv -e 1 -o "$BB_File" /dev/"$Drive" else - echo_str "Dry run: would run badblocks -b 4096 -wsv -e 1 -o ${BB_File} /dev/${Drive}" + log_info "Dry run: would run badblocks -b 4096 -wsv -e 1 -o ${BB_File} /dev/${Drive}" fi - echo_str "Finished badblocks test on drive /dev/${Drive}: $(date)" + log_info "Finished badblocks test on drive /dev/${Drive}: $(date)" } ######################################################################## @@ -364,19 +375,17 @@ if [ -e "$Log_File" ]; then rm "$Log_File" fi -push_header -echo_str "+ Started burn-in of /dev/${Drive} : $(date)" -push_header +log_header "Started burn-in of /dev/${Drive}" -echo_str "Host: $(hostname)" -echo_str "Drive Model: ${Disk_Model}" -echo_str "Serial Number: ${Serial_Number}" -echo_str "Short test duration: ${Short_Test_Minutes} minutes" -echo_str "Short test sleep duration: ${Short_Test_Seconds} seconds" -echo_str "Extended test duration: ${Extended_Test_Minutes} minutes" -echo_str "Extended test sleep duration: ${Extended_Test_Seconds} seconds" -echo_str "Log file: ${Log_File}" -echo_str "Bad blocks file: ${BB_File}" +log_info "Host: $(hostname)" +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 sleep duration: ${Short_Test_Seconds} seconds" +log_info "Extended test duration: ${Extended_Test_Minutes} minutes" +log_info "Extended test sleep duration: ${Extended_Test_Seconds} seconds" +log_info "Log file: ${Log_File}" +log_info "Bad blocks file: ${BB_File}" # Run the test sequence: run_short_test @@ -386,14 +395,10 @@ run_badblocks_test run_extended_test # Emit full device information to log: -push_header -echo_str "+ SMART information for drive /dev/${Drive}: $(date)" -push_header +log_header "SMART information for drive /dev/${Drive}" smartctl -x -v 7,hex48 /dev/"$Drive" | tee -a "$Log_File" -push_header -echo_str "+ Finished burn-in of /dev/${Drive} : $(date)" -push_header +log_header "Finished burn-in of /dev/${Drive}" # Clean up the log file: From fb842ea1276c702e606c60b84184fb29a0daf8c4 Mon Sep 17 00:00:00 2001 From: Michael Schnerring <3743342+schnerring@users.noreply.github.com> Date: Tue, 8 Sep 2020 22:50:45 +0200 Subject: [PATCH 08/14] use long smartctl options to improve readability run smartctl --test in --captive mode remove smart test code duplication add function documentation --- disk-burnin.sh | 66 +++++++++++++++++++++++++------------------------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/disk-burnin.sh b/disk-burnin.sh index db8274a..aba0f96 100644 --- a/disk-burnin.sh +++ b/disk-burnin.sh @@ -321,48 +321,50 @@ poll_selftest_complete() return 1 } -run_short_test() +################################################## +# Run SMART test and log results. +# Globals: +# Drive +# Log_File +# Arguments: +# Test type: +# - short +# - long +# Test duration in seconds. +################################################## +run_smart_test() { - log_header "Run SMART short test on drive /dev/${Drive}" + log_header "Run SMART $1 test on drive /dev/${Drive}" if [ "${Dry_Run}" -eq 0 ]; then - smartctl -t short /dev/"$Drive" - log_info "Short test started, sleeping ${Short_Test_Seconds} seconds until it finishes" - sleep ${Short_Test_Seconds} + smartctl --test="$1" --captive "/dev/${Drive}" + log_info "SMART $1 test started, awaiting completion for $2 seconds ..." + sleep "$2" poll_selftest_complete - smartctl -l selftest /dev/"$Drive" | tee -a "$Log_File" + smartctl --log=selftest "/dev/${Drive}" | tee -a "${Log_File}" else - log_info "Dry run: would start the SMART short test and sleep ${Short_Test_Seconds} seconds until the test finishes" + log_info "Dry run: would start the SMART $1 test and sleep $2 seconds until the test finishes" fi - log_info "Finished SMART short test on drive /dev/${Drive}: $(date)" -} - -run_extended_test() -{ - log_header "Run SMART extended test on drive /dev/${Drive}" - if [ "${Dry_Run}" -eq 0 ]; then - smartctl -t long /dev/"$Drive" - log_info "Extended test started, sleeping ${Extended_Test_Seconds} seconds until it finishes" - sleep ${Extended_Test_Seconds} - poll_selftest_complete - smartctl -l selftest /dev/"$Drive" | tee -a "$Log_File" - else - log_info "Dry run: would start the SMART extended test and sleep ${Extended_Test_Seconds} seconds until the test finishes" - fi - log_info "Finished SMART extended test on drive /dev/${Drive}: $(date)" + log_info "Finished SMART short test on drive /dev/${Drive}" } +################################################## +# Run badblocks test. +# !!! THIS WILL ERASE ALL DATA ON THE DISK !!!! +# Globals: +# BB_File +# Drive +# Arguments: +# None +################################################## run_badblocks_test() { log_header "Run badblocks test on drive /dev/${Drive}" if [ "${Dry_Run}" -eq 0 ]; then -# -# This is the command which erases all data on the disk: -# - badblocks -b 4096 -wsv -e 1 -o "$BB_File" /dev/"$Drive" + badblocks -b 4096 -wsv -e 1 -o "${BB_File}" "/dev/${Drive}" else log_info "Dry run: would run badblocks -b 4096 -wsv -e 1 -o ${BB_File} /dev/${Drive}" fi - log_info "Finished badblocks test on drive /dev/${Drive}: $(date)" + log_info "Finished badblocks test on drive /dev/${Drive}" } ######################################################################## @@ -388,15 +390,13 @@ log_info "Log file: ${Log_File}" log_info "Bad blocks file: ${BB_File}" # Run the test sequence: -run_short_test -#run_extended_test +run_smart_test "short" "${Short_Test_Seconds}" run_badblocks_test -#run_short_test -run_extended_test +run_smart_test "long" "${Extended_Test_Seconds}" # Emit full device information to log: log_header "SMART information for drive /dev/${Drive}" -smartctl -x -v 7,hex48 /dev/"$Drive" | tee -a "$Log_File" +smartctl --xall --vendorattribute=7,hex48 "/dev/${Drive}" | tee -a "$Log_File" log_header "Finished burn-in of /dev/${Drive}" From 0dcbdcf12cb2c720da96100d539360f6359d6e73 Mon Sep 17 00:00:00 2001 From: Michael Schnerring <3743342+schnerring@users.noreply.github.com> Date: Tue, 8 Sep 2020 22:51:29 +0200 Subject: [PATCH 09/14] make constants readonly --- disk-burnin.sh | 51 +++++++++++++++++++++++--------------------------- 1 file changed, 23 insertions(+), 28 deletions(-) diff --git a/disk-burnin.sh b/disk-burnin.sh index aba0f96..44076f3 100644 --- a/disk-burnin.sh +++ b/disk-burnin.sh @@ -147,23 +147,21 @@ if [ $# -ne 1 ]; then exit 2 fi -Drive=$1 +# Drive to burn-in +readonly Drive="$1" # Set Dry_Run to a non-zero value to test out the script without actually # running any tests; leave it set to zero to burn-in disks. +readonly Dry_Run=0 -Dry_Run=0 - -# Directory specifiers for log and badblocks data files. Leave off the -# trailing slash if you specify a value. Default is the current working -# directory. - -Log_Dir=$(pwd) -BB_Dir=$(pwd) +# Directory specifiers for log and badblocks data files. Leave off the trailing +# slash if you specify a value. Default is the current working directory. +readonly Log_Dir="$(pwd)" +readonly BB_Dir="$(pwd)" # Alternative: -#Log_Dir="." -#BB_Dir="." +#readonly Log_Dir="." +#readonly BB_Dir="." ######################################################################## # @@ -202,17 +200,14 @@ get_smart_info_value() { # Get disk model Disk_Model="$(get_smart_info_value "Device Model")" [ -z "${Disk_Model}" ] && Disk_Model="$(get_smart_info_value "Model Family")" +readonly Disk_Model # Get disk serial number -Serial_Number="$(get_smart_info_value "Serial Number")" +readonly Serial_Number="$(get_smart_info_value "Serial Number")" # Form the log and bad blocks data filenames: - -Log_File="burnin-${Disk_Model}_${Serial_Number}.log" -Log_File=$Log_Dir/$Log_File - -BB_File="burnin-${Disk_Model}_${Serial_Number}.bb" -BB_File=$BB_Dir/$BB_File +readonly Log_File="${Log_Dir}/burnin-${Disk_Model}_${Serial_Number}.log" +readonly BB_File="${BB_Dir}/burnin-${Disk_Model}_${Serial_Number}.bb" ################################################## # Get SMART recommended test duration, in minutes. @@ -239,19 +234,19 @@ get_smart_test_duration() { # Afterwards the completion status is repeatedly polled. # SMART short test duration -Short_Test_Minutes="$(get_smart_test_duration "Short")" -Short_Test_Seconds="$(( Short_Test_Minutes * 60))" +readonly Short_Test_Minutes="$(get_smart_test_duration "Short")" +readonly Short_Test_Seconds="$(( Short_Test_Minutes * 60))" # SMART extended test duration -Extended_Test_Minutes="$(get_smart_test_duration "Extended")" -Extended_Test_Seconds="$(( Extended_Test_Minutes * 60 ))" +readonly Extended_Test_Minutes="$(get_smart_test_duration "Extended")" +readonly Extended_Test_Seconds="$(( Extended_Test_Minutes * 60 ))" # Maximum duration the completion status is polled -Poll_Timeout_Hours=4 -Poll_Timeout_Seconds="$(( Poll_Timeout_Hours * 60 * 60))" +readonly Poll_Timeout_Hours=4 +readonly Poll_Timeout_Seconds="$(( Poll_Timeout_Hours * 60 * 60))" # Sleep interval between completion status polls -Poll_Interval_Seconds=15 +readonly Poll_Interval_Seconds=15 ######################################################################## # @@ -303,13 +298,13 @@ poll_selftest_complete() l_poll_duration_seconds=0 while [ "${l_poll_duration_seconds}" -lt "${Poll_Timeout_Seconds}" ]; do smartctl --all "/dev/${Drive}" | grep -i "The previous self-test routine completed" > /dev/null 2<&1 - l_status=$? + l_status="$?" if [ "${l_status}" -eq 0 ]; then log_info "SMART self-test succeeded" return 0 fi smartctl --all "/dev/${Drive}" | grep -i "of the test failed." > /dev/null 2<&1 - l_status=$? + l_status="$?" if [ "${l_status}" -eq 0 ]; then log_info "SMART self-test failed" return 0 @@ -402,7 +397,7 @@ log_header "Finished burn-in of /dev/${Drive}" # Clean up the log file: -osflavor=$(uname) +osflavor="$(uname)" if [ "${osflavor}" = "Linux" ]; then sed -i -e '/Copyright/d' "${Log_File}" From 0dc4614b3a72e4e0aaea62f21e07252e4a6a0c59 Mon Sep 17 00:00:00 2001 From: Michael Schnerring <3743342+schnerring@users.noreply.github.com> Date: Tue, 8 Sep 2020 22:52:01 +0200 Subject: [PATCH 10/14] adjust informational output for readability remove duplicate output of drive information add OS_FLAVOR and HOSTNAME constants --- disk-burnin.sh | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/disk-burnin.sh b/disk-burnin.sh index 44076f3..5214b48 100644 --- a/disk-burnin.sh +++ b/disk-burnin.sh @@ -154,6 +154,10 @@ readonly Drive="$1" # running any tests; leave it set to zero to burn-in disks. readonly Dry_Run=0 +# System information +readonly HOSTNAME="$(hostname)" +readonly OS_FLAVOR="$(uname)" + # Directory specifiers for log and badblocks data files. Leave off the trailing # slash if you specify a value. Default is the current working directory. readonly Log_Dir="$(pwd)" @@ -329,7 +333,7 @@ poll_selftest_complete() ################################################## run_smart_test() { - log_header "Run SMART $1 test on drive /dev/${Drive}" + log_header "Run SMART $1 test" if [ "${Dry_Run}" -eq 0 ]; then smartctl --test="$1" --captive "/dev/${Drive}" log_info "SMART $1 test started, awaiting completion for $2 seconds ..." @@ -339,7 +343,7 @@ run_smart_test() else log_info "Dry run: would start the SMART $1 test and sleep $2 seconds until the test finishes" fi - log_info "Finished SMART short test on drive /dev/${Drive}" + log_info "Finished SMART short test" } ################################################## @@ -353,13 +357,13 @@ run_smart_test() ################################################## run_badblocks_test() { - log_header "Run badblocks test on drive /dev/${Drive}" + log_header "Running badblocks test" if [ "${Dry_Run}" -eq 0 ]; then badblocks -b 4096 -wsv -e 1 -o "${BB_File}" "/dev/${Drive}" else log_info "Dry run: would run badblocks -b 4096 -wsv -e 1 -o ${BB_File} /dev/${Drive}" fi - log_info "Finished badblocks test on drive /dev/${Drive}" + log_info "Finished badblocks test" } ######################################################################## @@ -372,17 +376,17 @@ if [ -e "$Log_File" ]; then rm "$Log_File" fi -log_header "Started burn-in of /dev/${Drive}" +log_header "Started burn-in" -log_info "Host: $(hostname)" -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 sleep duration: ${Short_Test_Seconds} seconds" -log_info "Extended test duration: ${Extended_Test_Minutes} minutes" -log_info "Extended test sleep duration: ${Extended_Test_Seconds} seconds" -log_info "Log file: ${Log_File}" -log_info "Bad blocks file: ${BB_File}" +log_info "Host: ${HOSTNAME}" +log_info "OS Flavor: ${OS_FLAVOR}" +log_info "Drive: /dev/${Drive}" +log_info "Drive Model: ${Disk_Model}" +log_info "Serial Number: ${Serial_Number}" +log_info "Short test duration: ${Short_Test_Minutes} minutes / ${Short_Test_Seconds} seconds" +log_info "Extended test duration: ${Extended_Test_Minutes} minutes / ${Extended_Test_Seconds} seconds" +log_info "Log file: ${Log_File}" +log_info "Bad blocks file: ${BB_File}" # Run the test sequence: run_smart_test "short" "${Short_Test_Seconds}" @@ -390,16 +394,14 @@ run_badblocks_test run_smart_test "long" "${Extended_Test_Seconds}" # Emit full device information to log: -log_header "SMART information for drive /dev/${Drive}" +log_header "SMART and non-SMART information" smartctl --xall --vendorattribute=7,hex48 "/dev/${Drive}" | tee -a "$Log_File" -log_header "Finished burn-in of /dev/${Drive}" +log_header "Finished burn-in" # Clean up the log file: -osflavor="$(uname)" - -if [ "${osflavor}" = "Linux" ]; then +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}" @@ -407,7 +409,7 @@ if [ "${osflavor}" = "Linux" ]; then sed -i -e '/SMART Error Log Version/d' "${Log_File}" fi -if [ "${osflavor}" = "FreeBSD" ]; then +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}" From 4a8d44e6a8df96d238c1094a91d45189161f28be Mon Sep 17 00:00:00 2001 From: Michael Schnerring <3743342+schnerring@users.noreply.github.com> Date: Tue, 8 Sep 2020 23:00:35 +0200 Subject: [PATCH 11/14] check if running as root --- disk-burnin.sh | 6 ++++++ 1 file changed, 6 insertions(+) mode change 100644 => 100755 disk-burnin.sh diff --git a/disk-burnin.sh b/disk-burnin.sh old mode 100644 new mode 100755 index 5214b48..e7df303 --- a/disk-burnin.sh +++ b/disk-burnin.sh @@ -140,6 +140,12 @@ for dependency in ${DEPENDENCIES}; do fi done +# Check if running as root +if [ "$(id -u)" -ne 0 ]; then + echo "Please run as root. Exiting ..." + exit 2 +fi + # Check script arguments if [ $# -ne 1 ]; then echo "Error: not enough arguments!" From 57f8dd52275c194cf0bba7cbd5f96b658b353d1a Mon Sep 17 00:00:00 2001 From: Michael Schnerring <3743342+schnerring@users.noreply.github.com> Date: Wed, 9 Sep 2020 00:31:33 +0200 Subject: [PATCH 12/14] implement option parsing * -h for help text * -f for non-dry mode * -o to specify log directory --- disk-burnin.sh | 79 ++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 58 insertions(+), 21 deletions(-) diff --git a/disk-burnin.sh b/disk-burnin.sh index e7df303..fd70c3e 100755 --- a/disk-burnin.sh +++ b/disk-burnin.sh @@ -135,44 +135,81 @@ readonly DEPENDENCIES="awk badblocks grep sed sleep" for dependency in ${DEPENDENCIES}; do if ! command -v "${dependency}" > /dev/null 2>&1 ; then - echo "Command '${dependency}' not found. Exiting ..." + echo "Command '${dependency}' not found" >&2 exit 2 fi done # Check if running as root if [ "$(id -u)" -ne 0 ]; then - echo "Please run as root. Exiting ..." + echo "Please run as root" >&2 exit 2 fi -# Check script arguments -if [ $# -ne 1 ]; then - echo "Error: not enough arguments!" - echo "Usage is: $0 drive_device_specifier" +readonly USAGE=\ +"$(basename "$0") -- program to burn-in disks + +Usage: + $(basename "$0") [-h] [-f] [-o ] + +By default the program runs in dry mode and no data will be lost. + +Options: + -h show help text + -f run in destructive, non-dry mode + ALL DATA ON THE DISK WILL BE LOST! + -o write log files to + default: $(pwd) + disk to burn-in: /dev/ + e.g. specify 'sda' to burn-in '/dev/sda'" + +while getopts ':hfo:' option; do + case "${option}" in + h) echo "${USAGE}" + exit + ;; + f) Dry_Run=0 + ;; + o) LOG_DIR="${OPTARG}" + ;; + :) 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 "Missing option: " >&2 + echo "${USAGE}" >&2 exit 2 fi # Drive to burn-in readonly Drive="$1" -# Set Dry_Run to a non-zero value to test out the script without actually -# running any tests; leave it set to zero to burn-in disks. -readonly Dry_Run=0 +# Run in dry mode if -f wasn't provided +[ -z "${Dry_Run}" ] && Dry_Run=1 +readonly Dry_Run + +# Set to working directory if -o 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 + +# Create directory if it doesn't exist +mkdir -p -- "${LOG_DIR}" || exit 2 # System information readonly HOSTNAME="$(hostname)" readonly OS_FLAVOR="$(uname)" -# Directory specifiers for log and badblocks data files. Leave off the trailing -# slash if you specify a value. Default is the current working directory. -readonly Log_Dir="$(pwd)" -readonly BB_Dir="$(pwd)" - -# Alternative: -#readonly Log_Dir="." -#readonly BB_Dir="." - ######################################################################## # # Prologue @@ -216,8 +253,8 @@ readonly Disk_Model readonly Serial_Number="$(get_smart_info_value "Serial Number")" # Form the log and bad blocks data filenames: -readonly Log_File="${Log_Dir}/burnin-${Disk_Model}_${Serial_Number}.log" -readonly BB_File="${BB_Dir}/burnin-${Disk_Model}_${Serial_Number}.bb" +readonly Log_File="${LOG_DIR}/burnin-${Disk_Model}_${Serial_Number}.log" +readonly BB_File="${LOG_DIR}/burnin-${Disk_Model}_${Serial_Number}.bb" ################################################## # Get SMART recommended test duration, in minutes. @@ -354,7 +391,7 @@ run_smart_test() ################################################## # Run badblocks test. -# !!! THIS WILL ERASE ALL DATA ON THE DISK !!!! +# !!! ALL DATA ON THE DISK WILL BE LOST !!! # Globals: # BB_File # Drive From 5d702df0a6a22350e33d751249053c7c0e7ed2cb Mon Sep 17 00:00:00 2001 From: Michael Schnerring <3743342+schnerring@users.noreply.github.com> Date: Wed, 9 Sep 2020 01:52:03 +0200 Subject: [PATCH 13/14] change constant names to uppercase regroup constants logically together --- disk-burnin.sh | 172 ++++++++++++++++++++++++------------------------- 1 file changed, 85 insertions(+), 87 deletions(-) diff --git a/disk-burnin.sh b/disk-burnin.sh index fd70c3e..196c2ae 100755 --- a/disk-burnin.sh +++ b/disk-burnin.sh @@ -68,7 +68,7 @@ # your needs. In 'dry runs' the script does not actually perform any # SMART tests or invoke the sleep or badblocks programs. The script is # distributed with 'dry runs' enabled, so you will need to edit the -# Dry_Run variable below, setting it to 0, in order to actually perform +# DRY_RUN variable below, setting it to 0, in order to actually perform # tests on drives. # # Before using the script on FreeBSD systems (including FreeNAS) you must @@ -121,7 +121,7 @@ # 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 DRY_RUN value so that dry runs are no longer the default setting. # Changed badblocks call to exit immediately on first error. # Set logging directoryto current working directory using pwd command. # Reduced default tests so that we run: @@ -129,7 +129,11 @@ # 2> badblocks # 3> Extended SMART test # -######################################################################## +################################################################################ + +################################################################################ +# PRE-EXECUTION VALIDATION +################################################################################ # Check required dependencies readonly DEPENDENCIES="awk badblocks grep sed sleep" @@ -168,7 +172,7 @@ while getopts ':hfo:' option; do h) echo "${USAGE}" exit ;; - f) Dry_Run=0 + f) DRY_RUN=0 ;; o) LOG_DIR="${OPTARG}" ;; @@ -190,12 +194,16 @@ if [ -z "$1" ]; then exit 2 fi +################################################################################ +# CONSTANTS +################################################################################ + # Drive to burn-in -readonly Drive="$1" +readonly DRIVE="$1" # Run in dry mode if -f wasn't provided -[ -z "${Dry_Run}" ] && Dry_Run=1 -readonly Dry_Run +[ -z "${DRY_RUN}" ] && DRY_RUN=1 +readonly DRY_RUN # Set to working directory if -o wasn't provided [ -z "${LOG_DIR}" ] && LOG_DIR="$(pwd)" @@ -203,22 +211,13 @@ readonly Dry_Run LOG_DIR="$(printf '%s' "${LOG_DIR}" | awk '{gsub(/\/+$/, ""); printf $1}')" readonly LOG_DIR -# Create directory if it doesn't exist -mkdir -p -- "${LOG_DIR}" || exit 2 - # System information readonly HOSTNAME="$(hostname)" readonly OS_FLAVOR="$(uname)" -######################################################################## -# -# Prologue -# -######################################################################## - # SMART static information -readonly SMART_INFO="$(smartctl --info "/dev/${Drive}")" -readonly SMART_CAPABILITIES="$(smartctl --capabilities "/dev/${Drive}")" +readonly SMART_INFO="$(smartctl --info "/dev/${DRIVE}")" +readonly SMART_CAPABILITIES="$(smartctl --capabilities "/dev/${DRIVE}")" ################################################## # Get SMART information value. @@ -244,18 +243,6 @@ get_smart_info_value() { | awk '{$1=$2=""; gsub(/^[ \t]+|[ \t]+$/, ""); gsub(/ /, "_"); printf $1}' } -# Get disk model -Disk_Model="$(get_smart_info_value "Device Model")" -[ -z "${Disk_Model}" ] && Disk_Model="$(get_smart_info_value "Model Family")" -readonly Disk_Model - -# Get disk serial number -readonly Serial_Number="$(get_smart_info_value "Serial Number")" - -# Form the log and bad blocks data filenames: -readonly Log_File="${LOG_DIR}/burnin-${Disk_Model}_${Serial_Number}.log" -readonly BB_File="${LOG_DIR}/burnin-${Disk_Model}_${Serial_Number}.bb" - ################################################## # Get SMART recommended test duration, in minutes. # Globals: @@ -277,34 +264,44 @@ get_smart_test_duration() { | 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")" +readonly DISK_MODEL + +# Get disk serial number +readonly SERIAL_NUMBER="$(get_smart_info_value "Serial Number")" + # The script initially sleeps for a duration after a test is started. # Afterwards the completion status is repeatedly polled. # SMART short test duration -readonly Short_Test_Minutes="$(get_smart_test_duration "Short")" -readonly Short_Test_Seconds="$(( Short_Test_Minutes * 60))" +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 ))" +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))" +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 +readonly POLL_INTERVAL_SECONDS=15 -######################################################################## -# -# Local functions -# -######################################################################## +# 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 +# LOG_FILE # Arguments: # Message to log. # Outputs: @@ -313,7 +310,7 @@ readonly Poll_Interval_Seconds=15 log_info() { now="$(date +"%F %T %Z")" - printf "%s\n" "[${now}] $1" | tee -a "${Log_File}" + printf "%s\n" "[${now}] $1" | tee -a "${LOG_FILE}" } ################################################## @@ -331,9 +328,9 @@ log_header() ################################################## # Poll repeatedly whether SMART self-test has completed. # Globals: -# Drive -# Poll_Interval_Seconds -# Poll_Timeout_Seconds +# DRIVE +# POLL_INTERVAL_SECONDS +# POLL_TIMEOUT_SECONDS # Arguments: # None # Returns: @@ -343,21 +340,21 @@ log_header() poll_selftest_complete() { l_poll_duration_seconds=0 - while [ "${l_poll_duration_seconds}" -lt "${Poll_Timeout_Seconds}" ]; do - smartctl --all "/dev/${Drive}" | grep -i "The previous self-test routine completed" > /dev/null 2<&1 + while [ "${l_poll_duration_seconds}" -lt "${POLL_TIMEOUT_SECONDS}" ]; do + smartctl --all "/dev/${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 "/dev/${Drive}" | grep -i "of the test failed." > /dev/null 2<&1 + smartctl --all "/dev/${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 ))" + 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 @@ -366,8 +363,8 @@ poll_selftest_complete() ################################################## # Run SMART test and log results. # Globals: -# Drive -# Log_File +# DRIVE +# LOG_FILE # Arguments: # Test type: # - short @@ -377,12 +374,12 @@ poll_selftest_complete() run_smart_test() { log_header "Run SMART $1 test" - if [ "${Dry_Run}" -eq 0 ]; then - smartctl --test="$1" --captive "/dev/${Drive}" + if [ "${DRY_RUN}" -eq 0 ]; then + smartctl --test="$1" --captive "/dev/${DRIVE}" log_info "SMART $1 test started, awaiting completion for $2 seconds ..." sleep "$2" poll_selftest_complete - smartctl --log=selftest "/dev/${Drive}" | tee -a "${Log_File}" + smartctl --log=selftest "/dev/${DRIVE}" | tee -a "${LOG_FILE}" else log_info "Dry run: would start the SMART $1 test and sleep $2 seconds until the test finishes" fi @@ -394,68 +391,69 @@ run_smart_test() # !!! ALL DATA ON THE DISK WILL BE LOST !!! # Globals: # BB_File -# Drive +# DRIVE # Arguments: # None ################################################## run_badblocks_test() { log_header "Running badblocks test" - if [ "${Dry_Run}" -eq 0 ]; then - badblocks -b 4096 -wsv -e 1 -o "${BB_File}" "/dev/${Drive}" + if [ "${DRY_RUN}" -eq 0 ]; then + badblocks -b 4096 -wsv -e 1 -o "${BB_File}" "/dev/${DRIVE}" else - log_info "Dry run: would run badblocks -b 4096 -wsv -e 1 -o ${BB_File} /dev/${Drive}" + log_info "Dry run: would run badblocks -b 4096 -wsv -e 1 -o ${BB_File} /dev/${DRIVE}" fi log_info "Finished badblocks test" } -######################################################################## -# -# Action begins here -# -######################################################################## +################################################################################ +# ACTIONS BEGINS HERE +################################################################################ -if [ -e "$Log_File" ]; then - rm "$Log_File" +# Create log directory if it doesn't exist +mkdir -p -- "${LOG_DIR}" || exit 2 + +if [ -e "${LOG_FILE}" ]; then + rm "${LOG_FILE}" fi log_header "Started burn-in" log_info "Host: ${HOSTNAME}" log_info "OS Flavor: ${OS_FLAVOR}" -log_info "Drive: /dev/${Drive}" -log_info "Drive Model: ${Disk_Model}" -log_info "Serial Number: ${Serial_Number}" -log_info "Short test duration: ${Short_Test_Minutes} minutes / ${Short_Test_Seconds} seconds" -log_info "Extended test duration: ${Extended_Test_Minutes} minutes / ${Extended_Test_Seconds} seconds" -log_info "Log file: ${Log_File}" +log_info "Drive: /dev/${DRIVE}" +log_info "Drive Model: ${DISK_MODEL}" +log_info "Serial Number: ${SERIAL_NUMBER}" +log_info "Short test duration: ${SHORT_TEST_MINUTES} minutes / ${SHORT_TEST_SECONDS} seconds" +log_info "Extended test duration: ${EXTENDED_TEST_MINUTES} minutes / ${EXTENDED_TEST_SECONDS} seconds" +log_info "Log file: ${LOG_FILE}" log_info "Bad blocks file: ${BB_File}" # Run the test sequence: -run_smart_test "short" "${Short_Test_Seconds}" +run_smart_test "short" "${SHORT_TEST_SECONDS}" run_badblocks_test -run_smart_test "long" "${Extended_Test_Seconds}" +run_smart_test "long" "${EXTENDED_TEST_SECONDS}" # Emit full device information to log: log_header "SMART and non-SMART information" -smartctl --xall --vendorattribute=7,hex48 "/dev/${Drive}" | tee -a "$Log_File" +smartctl --xall --vendorattribute=7,hex48 "/dev/${DRIVE}" | tee -a "${LOG_FILE}" log_header "Finished burn-in" # Clean up the log file: 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}" + 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}" + 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 From d31b60bfcde9b14de190600997e247866939859c Mon Sep 17 00:00:00 2001 From: Michael Schnerring <3743342+schnerring@users.noreply.github.com> Date: Wed, 9 Sep 2020 02:19:26 +0200 Subject: [PATCH 14/14] remove --captive option from smart tests --- disk-burnin.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/disk-burnin.sh b/disk-burnin.sh index 196c2ae..40cb8ff 100755 --- a/disk-burnin.sh +++ b/disk-burnin.sh @@ -375,7 +375,7 @@ run_smart_test() { log_header "Run SMART $1 test" if [ "${DRY_RUN}" -eq 0 ]; then - smartctl --test="$1" --captive "/dev/${DRIVE}" + smartctl --test="$1" "/dev/${DRIVE}" log_info "SMART $1 test started, awaiting completion for $2 seconds ..." sleep "$2" poll_selftest_complete