From 33c4229848aca69a56aca8a45fa0f9990c503aa1 Mon Sep 17 00:00:00 2001 From: DanailDR Date: Sun, 27 Sep 2026 12:53:24 +0300 Subject: [PATCH] Add startup confirmation to disk utilities --- HDD/erase-storage.md | 6 +++++- HDD/erase-storage.sh | 5 +++++ HDD/repair-hdd.md | 6 ++++++ HDD/repair-hdd.sh | 5 +++++ lib/common.sh | 27 +++++++++++++++++++++++++++ 5 files changed, 48 insertions(+), 1 deletion(-) diff --git a/HDD/erase-storage.md b/HDD/erase-storage.md index 41c1d02..0ef4e42 100644 --- a/HDD/erase-storage.md +++ b/HDD/erase-storage.md @@ -20,7 +20,11 @@ Erase, Overwrite. За ATA избира Enhanced Security Erase, ако устр потвърждения, като второто е точното име на устройството. **Изтриването е необратимо и при режим за цял диск засяга всички дялове.** -Не прекъсвай захранването или връзката с диска. При прекъсване на ATA +В началото скриптът предупреждава, че работата може да продължи часове, и +изисква `yes` за продължаване или `no` за прекратяване. Не изключвай, +рестартирай или приспивай компютъра и не прекъсвай захранването или връзката +с диска. За дял следва отделно потвърждение с точния device path; за цял диск +следват две допълнителни потвърждения. При прекъсване на ATA Security Erase устройството може да остане заключено с временната парола, посочена в съобщението за грешка. Secure Erase/Sanitize зависи от коректната реализация във firmware на устройството; скриптът не може да гарантира diff --git a/HDD/erase-storage.sh b/HDD/erase-storage.sh index c2a564b..0a1c921 100755 --- a/HDD/erase-storage.sh +++ b/HDD/erase-storage.sh @@ -15,6 +15,8 @@ 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. +At startup, asks you to confirm that you can keep the computer and drive +powered and connected for the hours the operation may take. Arguments: DEVICE Partition (for example /dev/sda1) or whole disk @@ -51,6 +53,9 @@ 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 common::require_cmd "$required_command" done diff --git a/HDD/repair-hdd.md b/HDD/repair-hdd.md index a27d0d7..67715cd 100644 --- a/HDD/repair-hdd.md +++ b/HDD/repair-hdd.md @@ -16,6 +16,12 @@ Ако дъщерно устройство не може да се демонтира, скриптът прекратява операцията. +В началото скриптът предупреждава, че работата може да продължи часове, и +изисква отговор `yes` за продължаване или `no` за прекратяване. По време на +целия процес не изключвай компютъра, не го рестартирай/приспивай и не +прекъсвай захранването или връзката с диска. Преди самия запис има отделно +потвърждение с `YES`. + Използвай само с правилно идентифициран диск, който можеш безопасно да изтриеш. Изисква пряка SATA връзка; `smartctl` не използва `-d sat`, затова USB адаптер може да изисква ръчна промяна на извикванията в скрипта. diff --git a/HDD/repair-hdd.sh b/HDD/repair-hdd.sh index b467f07..0fc044d 100755 --- a/HDD/repair-hdd.sh +++ b/HDD/repair-hdd.sh @@ -16,6 +16,8 @@ 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. +At startup, asks you to confirm that you can keep the computer and drive +powered and connected for the hours this operation may take. Arguments: DEVICE Whole-disk block device, for example /dev/sdX @@ -55,6 +57,9 @@ 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 common::require_cmd "$required_command" done diff --git a/lib/common.sh b/lib/common.sh index 063500f..902f5c7 100644 --- a/lib/common.sh +++ b/lib/common.sh @@ -54,6 +54,33 @@ common::log() { echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*" >&2 } +# common::confirm returns success only when the user answers yes. +# A no answer or end-of-input cancels the caller; invalid answers are retried. +common::confirm() { + local prompt="$1" + local answer + + while true; do + if ! read -r -p "${prompt} [yes/no]: " answer; then + common::log "No response received; cancelling." + return 1 + fi + + case "${answer,,}" in + yes) + return 0 + ;; + no) + common::log "Cancelled by user." + return 1 + ;; + *) + printf "Please answer 'yes' or 'no'.\n" >&2 + ;; + esac + done +} + # common::require_cmd fails fast with a clear message if a required # external command is missing from PATH, instead of letting the script # fail later with a cryptic "command not found".