Script Internals
This page provides an overview of the internal functions within the disk-burnin.sh
script. This is intended for developers or advanced users who wish to understand or modify the script's behavior.
Core Functions
get_smart_info_value()
This function queries the initial smartctl --info
output to extract specific details about the drive.
- Arguments: A string identifier for the value to retrieve (e.g., "Device Model", "Serial Number").
- Action: It uses
grep
andawk
to parse the cached SMART info and return the corresponding value, cleaned and formatted.
get_smart_test_duration()
This function parses the drive's reported capabilities to find the recommended duration for SMART self-tests.
- Arguments: The test type ("Short" or "Extended").
- Action: It parses the cached
smartctl --capabilities
output to find the estimated test time in minutes.
log_info()
and log_header()
These are logging helper functions.
log_info()
: Prints a standard log message to bothstdout
and the main log file.log_header()
: Prints an emphasized, sectioned header with a timestamp to delineate major stages of the burn-in process.
init_log()
Prepares the logging environment.
- Action: Ensures the output directory (from the
-o
option or current directory) exists. It also removes any old log files for the same drive to ensure a clean run.
cleanup_log()
This function runs at the end of the script to remove redundant or unhelpful lines from the final SMART data dump in the log file, making it more readable.
- Action: Uses
sed
to remove boilerplate lines like copyright notices from thesmartctl
output. It contains conditional logic to handle syntax differences between Linux and FreeBSDsed
.
dry_run_wrapper()
This is a crucial function that controls whether commands are executed or just printed.
- Arguments: A command to be executed.
- Action: If the
-f
flag was not provided (i.e.,DRY_RUN
is not set to 0), it prints the command prefixed withDRY RUN:
. Otherwise, it executes the command usingeval
.
poll_selftest_complete()
After starting a SMART test, this function waits for it to finish.
- Action: It enters a loop, periodically running
smartctl --all
and checking the output to see if the self-test has completed. This is more reliable than relying solely on the estimated duration, as actual test times can vary. It has a timeout to prevent it from running indefinitely.
run_smart_test()
Manages the execution of a SMART self-test.
- Arguments: Test type (
short
orlong
) and the estimated duration in seconds. - Action: It logs the start of the test, calls
dry_run_wrapper()
to initiate the test viasmartctl
, waits for the estimated duration, and then callspoll_selftest_complete()
to confirm completion. Finally, it logs the self-test results.
run_badblocks_test()
Executes the destructive badblocks
test.
- Action: It checks if the drive is an SSD. If not, it constructs and runs the
badblocks
command viadry_run_wrapper()
, incorporating the block size (-b
), concurrency (-c
), and exit-on-error (-e
) arguments.
log_full_device_info()
Logs the final, comprehensive SMART data at the end of the script.
- Action: Runs
smartctl --xall
to capture all available SMART and non-SMART information about the drive and appends it to the log file.
main()
This is the main entry point of the script that orchestrates the entire process.
- Action: It calls the functions in the correct order:
init_log
,log_runtime_info
,run_smart_test "short"
,run_badblocks_test
,run_smart_test "long"
,log_full_device_info
, andcleanup_log
.