├── .editorconfig ├── README.md ├── burnin-SAMSUNG_HD204UI_XXXXXXXXXXXXXX.log ├── disk-burnin.sh └── license.txt /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | charset = utf-8 5 | end_of_line = lf 6 | indent_size = 2 7 | indent_style = space 8 | insert_final_newline = true 9 | trim_trailing_whitespace = true 10 | 11 | [*.md] 12 | trim_trailing_whitespace = false -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Shell script for burn-in and testing of drives 2 | 3 | ## Purpose 4 | 5 | `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. 6 | 7 | ## Warnings 8 | 9 | Be warned that: 10 | 11 | * This script runs `badblocks` in destructive mode, which erases any data on the disk. Therefore, please be careful! __Do not run this script on disks containing valuable data!__ 12 | * Run times for large disks can be several days. Use `tmux` or `screen` to test multiple disks in parallel. 13 | * Must be run as 'root'. 14 | 15 | ## Tests 16 | 17 | Performs these steps: 18 | 19 | 1. Run SMART short test 20 | 2. Run `badblocks` 21 | 3. Run SMART extended test 22 | 23 | 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. 24 | 25 | 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. 26 | 27 | You should periodically monitor the burn-in progress and check for errors, particularly any errors reported by `badblocks`, or these SMART errors: 28 | 29 | |ID|Attribute Name| 30 | |---:|---| 31 | | 5|Reallocated_Sector_Ct| 32 | |196|Reallocated_Event_Count| 33 | |197|Current_Pending_Sector| 34 | |198|Offline_Uncorrectable| 35 | 36 | 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. 37 | 38 | The script extracts the drive model and serial number and creates a log filename of the form `burnin-[model]_[serial number].log`. 39 | 40 | ## `badblocks` Options 41 | 42 | `badblocks` is invoked with the following options: 43 | 44 | * `-b 8192` : Use a block size of 8192 (override this setting with the `-b` option below) 45 | * `-e 1` : Abort the `badblocks` test immediately if an error is found (override this setting with the `-x` option below) 46 | * `-c 64` : Number of concurrent blocks to check. (override this setting with the `-c` option below, but beware of memory use with high values) 47 | * `-v` : Verbose mode 48 | * `-o` : Write list of bad blocks found (if any) to a file named `burnin-[model]_[serial number].bb` 49 | * `-s` : Show progress 50 | * `-w` : Write-mode test, writes four patterns (0xaa, 0x55, 0xff, 0x00) on every disk block 51 | 52 | ## Usage 53 | 54 | `./disk-burnin.sh [-h] [-e] [-b ] [-c ] [-f] [-o ] [-x] ` 55 | 56 | ### Options 57 | 58 | * `-h`: show help text 59 | * `-e`: show extended help text 60 | * `-b`: block size (default: 8192) 61 | * `-c`: number of concurrent blocks to check (default: 64). Higher values will use more memory. 62 | * `-f`: run a full, destructive test. Disables the default 'dry-run mode'. **ALL DATA ON THE DISK WILL BE LOST!** 63 | * `-o `: write log files to `` (default: working directory `$(pwd)`) 64 | * `-x`: perform a full pass of `badblocks`, using the `-e 0` option. 65 | * ``: disk to burn-in (`/dev/` may be omitted) 66 | 67 | ### Examples 68 | 69 | * `./disk-burnin.sh sda`: run in dry-run mode on disk `/dev/sda` 70 | * `./disk-burnin.sh -f /dev/sdb`: run full, destructive test on disk `/dev/sdb` 71 | * `./disk-burnin.sh -f -o ~/burn-in-logs sdc`: run full, destructive test on disk `/dev/sdc` and write the log files to `~/burn-in-logs` directory 72 | 73 | ## Dry-Run Mode 74 | 75 | The script runs in dry-run mode by default, so you can check the sleep durations and 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. 76 | 77 | In order to perform tests on drives, you will need to provide the `-f` option. 78 | 79 | ## `smartctl` Device Type 80 | 81 | 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. 82 | 83 | ## FreeBSD / FreeNAS Notes 84 | 85 | 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: 86 | 87 | `sysctl kern.geom.debugflags=0x10` 88 | 89 | Also note that `badblocks` may issue the following warning under FreeBSD / FreeNAS, which can safely be ignored as it has no effect on testing: 90 | 91 | `set_o_direct: Inappropiate ioctl for device` 92 | 93 | ## Operating System Compatibility 94 | 95 | Tested under: 96 | 97 | * FreeNAS 9.10.2-U1 (FreeBSD 10.3-STABLE) 98 | * FreeNAS 11.1-U7 (FreeBSD 11.1-STABLE) 99 | * FreeNAS 11.2-U8 (FreeBSD 11.2-STABLE) 100 | * Ubuntu Server 16.04.2 LTS 101 | * CentOS 7.0 102 | * Tiny Core Linux 11.1 103 | * Fedora 33 Workstation 104 | 105 | ## Drive Models Tested 106 | 107 | The script should run successfully on any SAS or SATA disk with SMART capabilities, which includes just about all modern drives. It has been tested on these particular devices: 108 | 109 | * Intel 110 | * DC S3700 SSD 111 | * Model 320 Series SSD 112 | * HGST 113 | * Deskstar NAS (HDN724040ALE640) 114 | * Ultrastar 7K4000 (HUS724020ALE640) 115 | * Ultrastar He10 116 | * Ultrastar He12 117 | * Western Digital 118 | * Black (WD6001FZWX) 119 | * Gold 120 | * Re (WD4000FYYZ) 121 | * Green 122 | * Red 123 | * WD140EDFZ 124 | * Seagate 125 | * IronWolf NAS HDD 12TB (ST12000VN0008) 126 | * IronWolf NAS HDD 8TB (ST8000NE001-2M7101) 127 | 128 | ## Prerequisites 129 | 130 | smartmontools, available at [www.smartmontools.org](https://www.smartmontools.org) 131 | 132 | Uses: `grep`, `awk`, `sed`, `sleep`, `badblocks`, `smartctl` 133 | 134 | 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. 135 | 136 | ## Author 137 | 138 | Original author: Keith Nash, March 2017. 139 | Modified on 19 February 2021. 140 | -------------------------------------------------------------------------------- /burnin-SAMSUNG_HD204UI_XXXXXXXXXXXXXX.log: -------------------------------------------------------------------------------- 1 | [2020-09-09 21:58:23 CEST] +----------------------------------------------------------------------------- 2 | [2020-09-09 21:58:23 CEST] + Started burn-in 3 | [2020-09-09 21:58:23 CEST] +----------------------------------------------------------------------------- 4 | [2020-09-09 21:58:23 CEST] Host: arch-desktop 5 | [2020-09-09 21:58:23 CEST] OS Flavor: Linux 6 | [2020-09-09 21:58:23 CEST] Drive: /dev/sdb 7 | [2020-09-09 21:58:23 CEST] Disk Type: mechanical 8 | [2020-09-09 21:58:23 CEST] Drive Model: SAMSUNG_HD204UI 9 | [2020-09-09 21:58:23 CEST] Serial Number: XXXXXXXXXXXXXX 10 | [2020-09-09 21:58:23 CEST] Short test duration: 2 minutes 11 | [2020-09-09 21:58:23 CEST] 120 seconds 12 | [2020-09-09 21:58:23 CEST] Extended test duration: 341 minutes 13 | [2020-09-09 21:58:23 CEST] 20460 seconds 14 | [2020-09-09 21:58:23 CEST] Log file: ./burnin-SAMSUNG_HD204UI_XXXXXXXXXXXXXX.log 15 | [2020-09-09 21:58:23 CEST] Bad blocks file: ./burnin-SAMSUNG_HD204UI_XXXXXXXXXXXXXX.bb 16 | [2020-09-09 21:58:23 CEST] +----------------------------------------------------------------------------- 17 | [2020-09-09 21:58:23 CEST] + Running SMART short test 18 | [2020-09-09 21:58:23 CEST] +----------------------------------------------------------------------------- 19 | [2020-09-09 21:58:23 CEST] DRY RUN: smartctl --test="short" "/dev/sdb" 20 | [2020-09-09 21:58:23 CEST] SMART short test started, awaiting completion for 120 seconds ... 21 | [2020-09-09 21:58:23 CEST] DRY RUN: sleep "120" 22 | [2020-09-09 21:58:23 CEST] DRY RUN: poll_selftest_complete 23 | [2020-09-09 21:58:23 CEST] DRY RUN: smartctl --log=selftest "/dev/sdb" | tee -a "./burnin-SAMSUNG_HD204UI_XXXXXXXXXXXXXX.log" 24 | [2020-09-09 21:58:23 CEST] Finished SMART short test 25 | [2020-09-09 21:58:23 CEST] +----------------------------------------------------------------------------- 26 | [2020-09-09 21:58:23 CEST] + Running badblocks test 27 | [2020-09-09 21:58:23 CEST] +----------------------------------------------------------------------------- 28 | [2020-09-09 21:58:23 CEST] DRY RUN: badblocks -b 4096 -wsv -e 1 -o "./burnin-SAMSUNG_HD204UI_XXXXXXXXXXXXXX.bb" "/dev/sdb" 29 | [2020-09-09 21:58:23 CEST] Finished badblocks test 30 | [2020-09-09 21:58:23 CEST] +----------------------------------------------------------------------------- 31 | [2020-09-09 21:58:23 CEST] + Running SMART long test 32 | [2020-09-09 21:58:23 CEST] +----------------------------------------------------------------------------- 33 | [2020-09-09 21:58:23 CEST] DRY RUN: smartctl --test="long" "/dev/sdb" 34 | [2020-09-09 21:58:23 CEST] SMART long test started, awaiting completion for 20460 seconds ... 35 | [2020-09-09 21:58:23 CEST] DRY RUN: sleep "20460" 36 | [2020-09-09 21:58:23 CEST] DRY RUN: poll_selftest_complete 37 | [2020-09-09 21:58:23 CEST] DRY RUN: smartctl --log=selftest "/dev/sdb" | tee -a "./burnin-SAMSUNG_HD204UI_XXXXXXXXXXXXXX.log" 38 | [2020-09-09 21:58:23 CEST] Finished SMART long test 39 | [2020-09-09 21:58:23 CEST] +----------------------------------------------------------------------------- 40 | [2020-09-09 21:58:23 CEST] + SMART and non-SMART information 41 | [2020-09-09 21:58:23 CEST] +----------------------------------------------------------------------------- 42 | [2020-09-09 21:58:23 CEST] DRY RUN: smartctl --xall --vendorattribute=7,hex48 "/dev/sdb" | tee -a "./burnin-SAMSUNG_HD204UI_XXXXXXXXXXXXXX.log" 43 | [2020-09-09 21:58:23 CEST] +----------------------------------------------------------------------------- 44 | [2020-09-09 21:58:23 CEST] + Finished burn-in 45 | [2020-09-09 21:58:23 CEST] +----------------------------------------------------------------------------- 46 | -------------------------------------------------------------------------------- /disk-burnin.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | ################################################################################ 3 | # 4 | # disk-burnin.sh 5 | # 6 | ################################################################################ 7 | 8 | ################################################################################ 9 | # PRE-EXECUTION VALIDATION 10 | ################################################################################ 11 | 12 | # Check if running as root 13 | if [ "$(id -u)" -ne 0 ]; then 14 | echo "ERROR: Must be run as root" >&2 15 | exit 2 16 | fi 17 | 18 | # Check required dependencies 19 | readonly DEPENDENCIES="awk badblocks grep sed sleep smartctl" 20 | for dependency in ${DEPENDENCIES}; do 21 | if ! command -v "${dependency}" > /dev/null 2>&1; then 22 | echo "ERROR: Command '${dependency}' not found" >&2 23 | exit 2 24 | fi 25 | done 26 | 27 | readonly USAGE=\ 28 | "NAME 29 | $(basename "$0") -- disk burn-in program 30 | 31 | SYNOPSIS 32 | $(basename "$0") [-h] [-b ] [-c ] [-e] [-f] [-o ] [-x] 33 | 34 | DESCRIPTION 35 | A script to simplify the process of burning-in disks. Only intended for use 36 | on disks which do not contain any data, such as new disks or disks which 37 | are being tested or re-purposed. 38 | 39 | The script runs in dry-run mode by default, so you can check the sleep 40 | durations and to insure that the sequence of commands suits your needs. In 41 | dry-run mode the script does not actually perform any SMART tests or invoke 42 | the sleep or badblocks programs. 43 | 44 | In order to perform tests on drives, you will need to provide the -f option. 45 | 46 | OPTIONS 47 | -h Show help text 48 | -e Show extended help text 49 | -b Override block size (defaults to 8192) 50 | -c Override concurrent number of blocks tested 51 | -f Force script to run in destructive mode 52 | ALL DATA ON THE DISK WILL BE LOST! 53 | -o Write log files to (default: $(pwd)) 54 | -x Run full pass of badblocks instead of exiting on first error 55 | Disk to burn-in (/dev/ may be omitted) 56 | 57 | EXAMPLES 58 | $(basename "$0") sda 59 | run in dry-run mode on disk /dev/sda 60 | 61 | $(basename "$0") -f /dev/sdb 62 | run in destructive, non-dry mode on disk /dev/sdb 63 | 64 | $(basename "$0") -f -o ~/burn-in-logs sdc 65 | run in destructive, non-dry mode on disk /dev/sdc and 66 | write the log files to ~/burn-in-logs directory 67 | " 68 | readonly USAGE_2=\ 69 | "EXIT STATUS 70 | exit 0: script finishes successfully 71 | exit 2: dependencies are missing 72 | not running as 'root' 73 | illegal options are provided 74 | 75 | NOTES 76 | Be warned that: 77 | 78 | 1> The script runs badblocks in destructive mode, which erases any data 79 | on the disk. 80 | 81 | !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 82 | !!!! ALL DATA ON THE DISK WILL BE LOST! BE CAREFUL! !!!! 83 | !!!! DO NOT RUN THIS SCRIPT ON DISKS CONTAINING VALUABLE DATA !!!! 84 | !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 85 | 86 | 2> Run times for large disks can be several days. Use tmux or screen 87 | to test multiple disks in parallel. 88 | 89 | 3> Must be run as 'root'. 90 | 91 | 4> The script has the following dependencies: 92 | 93 | smartmontools, available at https://www.smartmontools.org 94 | Uses: grep, awk, sed, sleep, badblocks 95 | 96 | Performs this test sequence: 97 | 98 | 1> Run SMART short test 99 | 2> Run badblocks 100 | 3> Run SMART extended test 101 | 102 | The script sleeps after starting each SMART test, using a duration based on 103 | the polling interval reported by the disk, after which the script will poll 104 | the disk to verify the self-test has completed. 105 | 106 | Full SMART information is pulled after each SMART test. All output except 107 | for the sleep command is written to both stdout and the log. 108 | 109 | You should monitor the burn-in progress and watch for errors, particularly 110 | any errors reported by badblocks, or these SMART errors: 111 | 112 | 5 Reallocated_Sector_Ct 113 | 196 Reallocated_Event_Count 114 | 197 Current_Pending_Sector 115 | 198 Offline_Uncorrectable 116 | 117 | These indicate possible problems with the drive. You therefore may wish to 118 | abort the remaining tests and proceed with an RMA exchange for new drives or 119 | discard old ones. Please note that this list is not exhaustive. 120 | 121 | The script extracts the drive model and serial number and forms a log file- 122 | name of the form 'burnin-[model]_[serial number].log'. 123 | 124 | badblocks is invoked with a block size of 8192, the -wsv options, and the 125 | -o option to instruct it to write the list of bad blocks found (if any) to 126 | a file named 'burnin-[model]_[serial number].bb'. 127 | 128 | Before using the script on FreeBSD systems (including FreeNAS) you must 129 | first execute this sysctl command to alter the kernel's geometry debug 130 | flags. This allows badblocks to write to the entire disk: 131 | 132 | sysctl kern.geom.debugflags=0x10 133 | 134 | Also note that badblocks may issue the following warning under FreeBSD / 135 | FreeNAS, which can safely be ignored as it has no effect on testing: 136 | 137 | set_o_direct: Inappropiate ioctl for device 138 | 139 | Tested operating systems: 140 | 141 | FreeNAS 9.10.2 (FreeBSD 10.3-STABLE) 142 | FreeNAS 11.1-U7 (FreeBSD 11.1-STABLE) 143 | FreeNAS 11.2-U8 (FreeBSD 11.2-STABLE) 144 | Ubuntu Server 16.04.2 LTS 145 | CentOS 7.0 146 | Tiny Core Linux 11.1 147 | 148 | Tested disks: 149 | 150 | Intel 151 | DC S3700 SSD 152 | Model 320 Series SSD 153 | HGST 154 | Deskstar NAS (HDN724040ALE640) 155 | Ultrastar 7K4000 (HUS724020ALE640) 156 | Ultrastar He10 157 | Ultrastar He12 158 | Western Digital 159 | Black (WD6001FZWX) 160 | Gold 161 | Re (WD4000FYYZ) 162 | Seagate 163 | IronWolf NAS HDD 12TB (ST12000VN0008) 164 | 165 | VERSIONS 166 | Written by Keith Nash, March 2017 167 | 168 | KN, 8 Apr 2017: 169 | Added minimum test durations because some devices don't return accurate 170 | values. 171 | Added code to clean up the log file, removing copyright notices, etc. 172 | No longer echo 'smartctl -t' output to log file as it imparts no useful 173 | information. 174 | Emit test results after tests instead of full 'smartctl -a' output. 175 | Emit full 'smartctl -x' output at the end of all testing. 176 | Minor changes to log output and formatting. 177 | 178 | KN, 12 May 2017: 179 | Added code to poll the disk and check for completed self-tests. 180 | 181 | As noted above, some disks don't report accurate values for the short 182 | and extended self-test intervals, sometimes by a significant amount. 183 | The original approach using 'fudge' factors wasn't reliable and the 184 | script would finish even though the SMART self-tests had not completed. 185 | The new polling code helps insure that this doesn't happen. 186 | 187 | Fixed code to work around annoying differences between sed's behavior 188 | on Linux and FreeBSD. 189 | 190 | KN, 8 Jun 2017 191 | Modified parsing of short and extended test durations to accommodate the 192 | values returned by larger drives; we needed to strip out the '(' and ')' 193 | characters surrounding the integer value in order to fetch it reliably. 194 | 195 | KN, 19 Aug 2020 196 | Changed DRY_RUN value so that dry runs are no longer the default 197 | setting. 198 | Changed badblocks call to exit immediately on first error. 199 | Set logging directory to current working directory using pwd command. 200 | Reduced default tests so that we run: 201 | 1> Short SMART test 202 | 2> badblocks 203 | 3> Extended SMART test 204 | 205 | MS, 9 Sep 2020 206 | Add .editorconfig to streamlime editor behavior for developers. 207 | Remove dependencies on pcregrep and tr. 208 | Add documentation to functions and complex statements. 209 | Reduce code duplication, simplify and decouple code where possible. 210 | Improve portability and resiliency. 211 | Check availability of dependencies during runtim. 212 | Check for root privileges during runtime. 213 | Add option parsing, most notably (-h)elp and -f for non-dry-run mode. 214 | Add dry_run_wrapper() function. 215 | Add disk type detection to skip badblocks for non-mechanical drives. 216 | 217 | KN, 5 Oct 2020 218 | Added -x option to control the badblocks -e option, allowing extended testing. 219 | Added smartctl to the list of dependencies. 220 | Changed disk type detection so that we assume all drives are mechanical drives 221 | unless they explicitly return 'Solid State Drive' for Rotational Rate. 222 | Removed datestamp from every line of log output, only emitting it in log headers. 223 | Minor reformatting. 224 | 225 | KY, 30 May 2022 226 | Added -b & -c options to control respective badblocks options." 227 | 228 | # badblocks default -e option is 1, stop testing if a single error occurs 229 | BB_E_ARG=1 230 | 231 | # badblocks default -b option is 1024, but we default to 8192. This allows overriding if desired. 232 | BB_B_ARG=8192 233 | 234 | # badblocks default -c option is 64, and this allows overriding 235 | BB_C_ARG=64 236 | 237 | # parse options 238 | while getopts ':hefo:b:c:x' option; do 239 | case "${option}" in 240 | h) echo "${USAGE}" 241 | exit 242 | ;; 243 | e) echo "${USAGE}" 244 | echo "${USAGE_2}" 245 | exit 246 | ;; 247 | f) readonly DRY_RUN=0 248 | ;; 249 | o) LOG_DIR="${OPTARG}" 250 | ;; 251 | b) BB_B_ARG="${OPTARG}" 252 | ;; 253 | c) BB_C_ARG="${OPTARG}" 254 | ;; 255 | x) BB_E_ARG=0 256 | ;; 257 | :) printf 'Missing argument for -%s\n' "${OPTARG}" >&2 258 | echo "${USAGE}" >&2 259 | exit 2 260 | ;; 261 | \?) printf 'Illegal option: -%s\n' "${OPTARG}" >&2 262 | echo "${USAGE}" >&2 263 | exit 2 264 | ;; 265 | esac 266 | done 267 | shift $(( OPTIND - 1 )) 268 | 269 | if [ -z "$1" ]; then 270 | echo "ERROR: Missing disk argument" >&2 271 | echo "${USAGE}" >&2 272 | exit 2 273 | fi 274 | 275 | ################################################################################ 276 | # CONSTANTS 277 | ################################################################################ 278 | 279 | readonly BB_E_ARG 280 | readonly BB_B_ARG 281 | readonly BB_C_ARG 282 | 283 | # Drive to burn-in 284 | DRIVE="$1" 285 | # prepend /dev/ if necessary 286 | if ! printf '%s' "${DRIVE}" | grep "/dev/\w*" > /dev/null 2>&1; then 287 | DRIVE="/dev/${DRIVE}" 288 | fi 289 | readonly DRIVE 290 | 291 | if [ ! -e "$DRIVE" ]; then 292 | echo "ERROR: Disk does not exist: $DRIVE" >&2 293 | echo "${USAGE}" >&2 294 | exit 2 295 | fi 296 | 297 | # Set to working directory if -o wasn't provided 298 | [ -z "${LOG_DIR}" ] && LOG_DIR="$(pwd)" 299 | # Trim trailing slashes 300 | LOG_DIR="$(printf '%s' "${LOG_DIR}" | awk '{gsub(/\/+$/, ""); printf $1}')" 301 | readonly LOG_DIR 302 | 303 | # System information 304 | readonly HOSTNAME="$(hostname)" 305 | readonly OS_FLAVOR="$(uname)" 306 | 307 | # SMART static information 308 | readonly SMART_INFO="$(smartctl --info "${DRIVE}")" 309 | readonly SMART_CAPABILITIES="$(smartctl --capabilities "${DRIVE}")" 310 | 311 | ################################################## 312 | # Get SMART information value. 313 | # Globals: 314 | # SMART_INFO 315 | # Arguments: 316 | # value identifier: 317 | # !!! Only TWO WORD indentifiers are supported !!! 318 | # !!! Querying e.g. "ATA Version is" will fail !!! 319 | # - Device Model 320 | # - Model Family 321 | # - Serial Number 322 | # Outputs: 323 | # Write value to stdout. 324 | ################################################## 325 | get_smart_info_value() { 326 | # $1=$2=""; select all but first two columns 327 | # gsub(/^[ \t]+|[ \t]+$/, ""); replace leading and trailing whitespace 328 | # gsub(/ /, "_"); replace remaining spaces with underscores 329 | # printf $1 print result without newline at the end 330 | printf '%s' "${SMART_INFO}" \ 331 | | grep "$1" \ 332 | | awk '{$1=$2=""; gsub(/^[ \t]+|[ \t]+$/, ""); gsub(/ /, "_"); printf $1}' 333 | } 334 | 335 | ################################################## 336 | # Get SMART recommended test duration, in minutes. 337 | # Globals: 338 | # SMART_CAPABILITIES 339 | # Arguments: 340 | # test type: 341 | # - Short 342 | # - Extended 343 | # - Conveyance 344 | # Outputs: 345 | # Write duration to stdout. 346 | ################################################## 347 | get_smart_test_duration() { 348 | # '/'"$1"' self-test routine/ match duration depending on test type arg 349 | # getline; jump to next line 350 | # gsub(/\(|\)/, ""); remove parantheses 351 | # printf $4 print 4th column without newline at the end 352 | printf '%s' "${SMART_CAPABILITIES}" \ 353 | | awk '/'"$1"' self-test routine/{getline; gsub(/\(|\)/, ""); printf $4}' 354 | } 355 | 356 | # Get disk model 357 | DISK_MODEL="$(get_smart_info_value "Device Model")" 358 | [ -z "${DISK_MODEL}" ] && DISK_MODEL="$(get_smart_info_value "Model Family")" 359 | [ -z "${DISK_MODEL}" ] && DISK_MODEL="$(get_smart_info_value "Model Number")" 360 | readonly DISK_MODEL 361 | 362 | # Get disk type; unless we get 'Solid State Device' as return value, assume 363 | # we have a mechanical drive. 364 | DISK_TYPE="$(get_smart_info_value "Rotation Rate")" 365 | if printf '%s' "${DISK_TYPE}" | grep -i "solid_state_device" > /dev/null 2>&1; then 366 | DISK_TYPE="SSD" 367 | fi 368 | readonly DISK_TYPE 369 | 370 | # Get disk serial number 371 | readonly SERIAL_NUMBER="$(get_smart_info_value "Serial Number")" 372 | 373 | # SMART short test duration 374 | readonly SHORT_TEST_MINUTES="$(get_smart_test_duration "Short")" 375 | readonly SHORT_TEST_SECONDS="$(( SHORT_TEST_MINUTES * 60))" 376 | 377 | # SMART extended test duration 378 | readonly EXTENDED_TEST_MINUTES="$(get_smart_test_duration "Extended")" 379 | readonly EXTENDED_TEST_SECONDS="$(( EXTENDED_TEST_MINUTES * 60 ))" 380 | 381 | # Maximum duration the completion status is polled 382 | readonly POLL_TIMEOUT_HOURS=4 383 | readonly POLL_TIMEOUT_SECONDS="$(( POLL_TIMEOUT_HOURS * 60 * 60))" 384 | 385 | # Sleep interval between completion status polls 386 | readonly POLL_INTERVAL_SECONDS=15 387 | 388 | # Form log file names 389 | readonly LOG_FILE="${LOG_DIR}/burnin-${DISK_MODEL}_${SERIAL_NUMBER}.log" 390 | readonly BB_File="${LOG_DIR}/burnin-${DISK_MODEL}_${SERIAL_NUMBER}.bb" 391 | 392 | ################################################################################ 393 | # FUNCTIONS 394 | ################################################################################ 395 | 396 | ################################################## 397 | # Log informational message. 398 | # Globals: 399 | # LOG_FILE 400 | # Arguments: 401 | # Message to log. 402 | # Outputs: 403 | # Write message to stdout and log file. 404 | ################################################## 405 | log_info() { 406 | # now="$(date +"%F %T %Z")" 407 | # printf "%s\n" "[${now}] $1" | tee -a "${LOG_FILE}" 408 | printf "%s\n" "$1" | tee -a "${LOG_FILE}" 409 | } 410 | 411 | ################################################## 412 | # Log emphasized header message. 413 | # Arguments: 414 | # Message to log. 415 | ################################################## 416 | log_header() { 417 | log_info "+-----------------------------------------------------------------------------" 418 | log_info "+ $1: $(date)" 419 | log_info "+-----------------------------------------------------------------------------" 420 | } 421 | 422 | ################################################## 423 | # Ensure log directory exists and remove old logs. 424 | # Globals: 425 | # LOG_DIR 426 | # LOG_FILE 427 | # Arguments: 428 | # None 429 | ################################################## 430 | init_log() { 431 | mkdir -p -- "${LOG_DIR}" || exit 2 432 | [ -e "${LOG_FILE}" ] && rm -- "${LOG_FILE}" 433 | } 434 | 435 | ################################################## 436 | # Remove redundant messages from log. 437 | # Globals: 438 | # LOG_FILE 439 | # OS_FLAVOR 440 | # Arguments: 441 | # None 442 | ################################################## 443 | cleanup_log() { 444 | if [ "${OS_FLAVOR}" = "Linux" ]; then 445 | sed -i -e '/Copyright/d' "${LOG_FILE}" 446 | sed -i -e '/=== START OF READ/d' "${LOG_FILE}" 447 | sed -i -e '/SMART Attributes Data/d' "${LOG_FILE}" 448 | sed -i -e '/Vendor Specific SMART/d' "${LOG_FILE}" 449 | sed -i -e '/SMART Error Log Version/d' "${LOG_FILE}" 450 | fi 451 | 452 | if [ "${OS_FLAVOR}" = "FreeBSD" ]; then 453 | sed -i '' -e '/Copyright/d' "${LOG_FILE}" 454 | sed -i '' -e '/=== START OF READ/d' "${LOG_FILE}" 455 | sed -i '' -e '/SMART Attributes Data/d' "${LOG_FILE}" 456 | sed -i '' -e '/Vendor Specific SMART/d' "${LOG_FILE}" 457 | sed -i '' -e '/SMART Error Log Version/d' "${LOG_FILE}" 458 | fi 459 | } 460 | 461 | ################################################## 462 | # Log command in dry-run mode, run command otherwise. 463 | # Globals: 464 | # DRY_RUN 465 | # Arguments: 466 | # Command to run. 467 | ################################################## 468 | dry_run_wrapper() { 469 | if [ -z "$DRY_RUN" ]; then 470 | log_info "DRY RUN: $*" 471 | return 0 472 | fi 473 | eval "$@" 474 | } 475 | 476 | ################################################## 477 | # Log runtime information about current burn-in. 478 | # Globals: 479 | # HOSTNAME 480 | # OS_FLAVOR 481 | # DRIVE 482 | # DISK_TYPE 483 | # DISK_MODEL 484 | # SERIAL_NUMBER 485 | # SHORT_TEST_MINUTES 486 | # SHORT_TEST_SECONDS 487 | # EXTENDED_TEST_MINUTES 488 | # EXTENDED_TEST_SECONDS 489 | # LOG_FILE 490 | # BB_File 491 | # Arguments: 492 | # None 493 | ################################################## 494 | log_runtime_info() { 495 | log_info "Host: ${HOSTNAME}" 496 | log_info "OS: ${OS_FLAVOR}" 497 | log_info "Drive: ${DRIVE}" 498 | log_info "Disk Type: ${DISK_TYPE}" 499 | log_info "Drive Model: ${DISK_MODEL}" 500 | log_info "Serial Number: ${SERIAL_NUMBER}" 501 | log_info "Short test duration: ${SHORT_TEST_MINUTES} minutes" 502 | log_info " ${SHORT_TEST_SECONDS} seconds" 503 | log_info "Extended test duration: ${EXTENDED_TEST_MINUTES} minutes" 504 | log_info " ${EXTENDED_TEST_SECONDS} seconds" 505 | log_info "Log file: ${LOG_FILE}" 506 | log_info "Bad blocks file: ${BB_File}" 507 | } 508 | 509 | ################################################## 510 | # Poll repeatedly whether SMART self-test has completed. 511 | # Globals: 512 | # DRIVE 513 | # POLL_INTERVAL_SECONDS 514 | # POLL_TIMEOUT_SECONDS 515 | # Arguments: 516 | # None 517 | # Returns: 518 | # 0 if success or failure. 519 | # 1 if timeout threshold exceeded. 520 | ################################################## 521 | poll_selftest_complete() { 522 | l_poll_duration_seconds=0 523 | while [ "${l_poll_duration_seconds}" -lt "${POLL_TIMEOUT_SECONDS}" ]; do 524 | smartctl --all "${DRIVE}" \ 525 | | grep -i "The previous self-test routine completed" > /dev/null 2>&1 526 | l_status="$?" 527 | if [ "${l_status}" -eq 0 ]; then 528 | log_info "SMART self-test succeeded" 529 | return 0 530 | fi 531 | smartctl --all "${DRIVE}" \ 532 | | grep -i "of the test failed\." > /dev/null 2>&1 533 | l_status="$?" 534 | if [ "${l_status}" -eq 0 ]; then 535 | log_info "SMART self-test failed" 536 | return 0 537 | fi 538 | sleep "${POLL_INTERVAL_SECONDS}" 539 | l_poll_duration_seconds="$(( l_poll_duration_seconds + POLL_INTERVAL_SECONDS ))" 540 | done 541 | log_info "SMART self-test timeout threshold exceeded" 542 | return 1 543 | } 544 | 545 | ################################################## 546 | # Run SMART test and log results. 547 | # Globals: 548 | # DRIVE 549 | # LOG_FILE 550 | # Arguments: 551 | # Test type: 552 | # - short 553 | # - long 554 | # Test duration in seconds. 555 | ################################################## 556 | run_smart_test() { 557 | log_header "Running SMART $1 test" 558 | dry_run_wrapper "smartctl --test=\"$1\" \"${DRIVE}\"" 559 | log_info "SMART $1 test started, awaiting completion for $2 seconds ..." 560 | dry_run_wrapper "sleep \"$2\"" 561 | dry_run_wrapper "poll_selftest_complete" 562 | dry_run_wrapper "smartctl --log=selftest \"${DRIVE}\" | tee -a \"${LOG_FILE}\"" 563 | log_info "Finished SMART $1 test" 564 | } 565 | 566 | ################################################## 567 | # Run badblocks test. 568 | # !!! ALL DATA ON THE DISK WILL BE LOST !!! 569 | # Globals: 570 | # BB_File 571 | # DISK_TYPE 572 | # DRIVE 573 | # Arguments: 574 | # None 575 | ################################################## 576 | run_badblocks_test() { 577 | log_header "Running badblocks test" 578 | if [ "${DISK_TYPE}" != "SSD" ]; then 579 | dry_run_wrapper "badblocks -b ${BB_B_ARG} -wsv -c ${BB_C_ARG} -e ${BB_E_ARG} -o \"${BB_File}\" \"${DRIVE}\"" 580 | else 581 | log_info "SKIPPED: badblocks for ${DISK_TYPE} device" 582 | fi 583 | log_info "Finished badblocks test" 584 | } 585 | 586 | ################################################## 587 | # Log extensive SMART and non-SMART drive information. 588 | # Globals: 589 | # DRIVE 590 | # LOG_FILE 591 | # Arguments: 592 | # None 593 | ################################################## 594 | log_full_device_info() { 595 | log_header "Drive information" 596 | dry_run_wrapper "smartctl --xall --vendorattribute=7,hex48 \"${DRIVE}\" | tee -a \"${LOG_FILE}\"" 597 | } 598 | 599 | ################################################## 600 | # Main function of script. 601 | # Globals: 602 | # SHORT_TEST_SECONDS 603 | # EXTENDED_TEST_SECONDS 604 | # Arguments: 605 | # None 606 | ################################################## 607 | main() { 608 | init_log 609 | log_header "Started burn-in" 610 | 611 | log_runtime_info 612 | 613 | # test sequence 614 | run_smart_test "short" "${SHORT_TEST_SECONDS}" 615 | run_badblocks_test 616 | run_smart_test "long" "${EXTENDED_TEST_SECONDS}" 617 | 618 | log_full_device_info 619 | 620 | log_header "Finished burn-in" 621 | cleanup_log 622 | } 623 | 624 | # Entrypoint 625 | main 626 | -------------------------------------------------------------------------------- /license.txt: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Original work Copyright (c) 2017 by Keith Nash 4 | Modified work Copyright (c) 2020 by Michael Schnerring 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in all 14 | copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | SOFTWARE. 23 | --------------------------------------------------------------------------------