Add HDD device previews and root guards

This commit is contained in:
dido committed 2026-09-29 09:08:57 +03:00
1 parent 33c4229848
commit cf53a6bb50
5 files changed
+52 -16

No files matched your search

+12 -8
View File
@@ -10,12 +10,19 @@ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=lib/common.sh
source "${SCRIPT_DIR}/../lib/common.sh"
if [[ "$EUID" -ne 0 ]]; then
common::log "WARNING: This script must be run as root (for example, with sudo)."
common::die "stopping before processing arguments or checking dependencies"
fi
SCRIPT_HELP=$(cat <<'HELP'
Usage: sudo ./HDD/repair-hdd.sh DEVICE [zero|null|random]
Overwrites the selected disk, then runs a SMART extended self-test. This
destroys all data on DEVICE. Use only with a directly connected SATA disk;
USB adapters may require adding -d sat to the smartctl calls.
Lists available block devices and mount points before continuing, highlighting
the selected device in red when run in a terminal.
At startup, asks you to confirm that you can keep the computer and drive
powered and connected for the hours this operation may take.
@@ -57,23 +64,17 @@ case "$FILL_MODE" in
;;
esac
common::log "WARNING: This operation may take hours. Do not power off, reboot, or suspend the computer, or disconnect power or the drive."
common::confirm "Can the computer and drive remain powered and connected until this operation finishes?" || exit 0
for required_command in findmnt lsblk readlink swapon mkdir date tee smartctl grep umount dd sync sleep head base64; do
for required_command in awk findmnt lsblk readlink swapon mkdir date tee smartctl grep umount dd sync sleep head base64; do
common::require_cmd "$required_command"
done
if [[ "$EUID" -ne 0 ]]; then
common::die "this script must be run as root (for example, with sudo)"
fi
if [[ ! -b "$DEVICE" ]]; then
common::die "$DEVICE does not exist or is not a block device"
fi
# Refuse the root filesystem device and any block device it depends on.
TARGET_DEVICE="$(readlink -f -- "$DEVICE")" || common::die "cannot resolve device path: $DEVICE"
common::show_block_devices "$TARGET_DEVICE" || common::die "cannot list available block devices"
ROOT_SOURCE="$(findmnt -n -o SOURCE /)" || common::die "cannot determine the root filesystem device"
ROOT_SOURCE="${ROOT_SOURCE%%\[*}"
ROOT_DEVICE="$(readlink -f -- "$ROOT_SOURCE")" || common::die "cannot resolve root device path: $ROOT_SOURCE"
@@ -100,6 +101,9 @@ while IFS= read -r child_device; do
done < <(swapon --show --noheadings --raw --output NAME)
done <<< "$DEVICE_TREE"
common::log "WARNING: This operation may take hours. Do not power off, reboot, or suspend the computer, or disconnect power or the drive."
common::confirm "Can the computer and drive remain powered and connected until this operation finishes?" || exit 0
if [[ "$FILL_MODE" == "random" ]] && ! command -v openssl >/dev/null 2>&1; then
common::log "OpenSSL is unavailable; random mode will use /dev/urandom."
fi