Parse options and add an option to specify if dry run

This commit is contained in:
Yifan Liao 2017-08-31 15:17:10 -07:00
parent 28cd95c52c
commit 1f1adb8047

110
disk-burnin.sh Normal file → Executable file
View file

@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash
########################################################################
#
# 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:
@ -32,7 +32,7 @@
# 4> Run SMART short test
# 5> 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.
#
@ -42,10 +42,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
@ -57,28 +57,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
@ -96,7 +96,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.
@ -104,16 +104,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.
#
@ -121,23 +121,69 @@
# 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.
#
#
# Modified by Yifan Liao
#
########################################################################
USAGE="Usage: $(basename "$0") [-h|--help] [--notest] drive-device-specifier
Run SMART tests and burn-in tests on a drive.
...
Options:
-h, --help
Display this help and exit
--notest
Needed to actually run the tests, By default $0 will just do a dry run.
...
"
getopt --test > /dev/null
if [[ $? -ne 4 ]]; then
echo "I’m sorry, `getopt --test` failed in this environment."
exit 1
fi
SHORT=h
LONG=help,notest
PARSED=$(getopt --options $SHORT --longoptions $LONG --name "$0" -- "$@")
if [[ $? -ne 0 ]]; then
exit 2
fi
eval set -- "$PARSED"
Dry_Run=1
while [ $# -gt 0 ]; do
case "$1" in
--notest)
Dry_Run=0
exit 0
;;
-h | --help)
echo "$USAGE"
exit 0
;;
--)
shift
break
;;
esac
shift
done
if [ $# -ne 1 ]; then
echo "Error: not enough arguments!"
echo "Usage is: $0 drive_device_specifier"
exit 2
echo "Error: not enough arguments!"
echo "$USAGE"
exit 4
fi
Drive=$1
# Set Dry_Run to a non-zero value to test out the script without actually
# running any tests: set it to zero when you are ready to burn-in disks.
Dry_Run=1
# Directory specifiers for log and badblocks data files. Leave off the
# Directory specifiers for log and badblocks data files. Leave off the
# trailing slash:
Log_Dir="."
@ -215,7 +261,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
@ -223,7 +269,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
@ -243,7 +289,7 @@ poll_selftest_complete()
done
return $l_rv
}
}
run_short_test()
{