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 -5
View File
@@ -9,12 +9,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/erase-storage.sh DEVICE [zero|random]
For a partition, writes one full pass of zeros (default) or pseudorandom data.
For a whole disk, detects and runs a supported ATA Secure Erase or NVMe Sanitize
operation. Whole-disk erasure requires two separate confirmations.
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 the operation may take.
@@ -53,20 +60,17 @@ case "$FILL_MODE" in
;;
esac
common::log "WARNING: Erasing data 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 the operation finishes?" || exit 0
for required_command in findmnt lsblk readlink swapon date dd sleep; do
for required_command in awk findmnt lsblk readlink swapon date dd sleep; do
common::require_cmd "$required_command"
done
[[ "$EUID" -eq 0 ]] || common::die "this script must be run as root (for example, with sudo)"
[[ -b "$DEVICE_INPUT" ]] || common::die "$DEVICE_INPUT does not exist or is not a block device"
DEVICE="$(readlink -f -- "$DEVICE_INPUT")" || common::die "cannot resolve device path: $DEVICE_INPUT"
DEVICE_TYPE="$(lsblk -dnro TYPE -- "$DEVICE")" || common::die "cannot determine device type"
[[ "$DEVICE_TYPE" == "part" || "$DEVICE_TYPE" == "disk" ]] \
|| common::die "only a partition or a whole disk is accepted (detected: $DEVICE_TYPE)"
common::show_block_devices "$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%%\[*}"
@@ -117,6 +121,9 @@ while IFS= read -r child_device; do
done <<< "$ACTIVE_SWAP_DEVICES"
done <<< "$DEVICE_TREE"
common::log "WARNING: Erasing data 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 the operation finishes?" || exit 0
if [[ "$DEVICE_TYPE" == "part" ]]; then
[[ $# -eq 1 || "$FILL_MODE" == "zero" || "$FILL_MODE" == "random" ]] \
|| common::die "invalid fill mode; use 'zero' or 'random'"