docs: update usage and shell style conventions

This commit is contained in:
2026-04-28 01:03:19 +02:00
parent bf9cbe918f
commit ab346342d5
3 changed files with 88 additions and 77 deletions
+2
View File
@@ -22,6 +22,8 @@ description: mtm-ddwipe project conventions
- Do not make new external tools required. - Do not make new external tools required.
- Keep changes minimal and preserve intent. - Keep changes minimal and preserve intent.
- Keep this file aligned and concise. - Keep this file aligned and concise.
- Prefer tabs for indentation.
- Prefer `${var}` notation in Bash.
- If non-interactive support is added, make it an opt-in safety flag. - If non-interactive support is added, make it an opt-in safety flag.
# Project identity # Project identity
+8 -3
View File
@@ -2,13 +2,18 @@
Wipe a block device. Wipe a block device.
Warning: destructive and irreversible. The target must be a real block device, not mounted, and not in use. Warning: destructive and irreversible.
Target must be a real block device, not mounted, and not in use.
Usage: Usage:
mtm-ddwipe DEVICE mtm-ddwipe DEVICE
mtm-ddwipe -h | --help
Options: Options:
- `-h`, `--help` -h, --help Show help.
Optional tools: `blkdiscard`, `ddrescue`, `dd_rescue`, `nvme`, `hdparm`. Notes:
- Interactive by default.
- Confirm exactly before wiping.
- Optional tools: blkdiscard, ddrescue, dd_rescue, nvme, hdparm.
+72 -68
View File
@@ -33,70 +33,74 @@ die() {
check_args() { check_args() {
if [ $# -ne 1 ]; then if [ $# -ne 1 ]; then
usage case "${1:-}" in
exit 1
fi
case "$1" in
-h|--help) -h|--help)
usage usage
exit 0 exit 0
;; ;;
"")
usage
exit 1
;;
-*) -*)
die "Invalid option." die "Invalid option."
;; ;;
esac esac
usage
exit 1
fi
} }
check_device() { check_device() {
local dev="$1" local dev="${1}"
[ -e "$dev" ] || die "Missing device: $dev" [ -e "${dev}" ] || die "Missing device: ${dev}"
[ -b "$dev" ] || die "Not a block device: $dev" [ -b "${dev}" ] || die "Not a block device: ${dev}"
} }
check_device_not_in_use() { check_device_not_in_use() {
local dev="$1" local dev="${1}"
if findmnt -rn --target "$dev" >/dev/null 2>&1; then if findmnt -rn --target "${dev}" >/dev/null 2>&1; then
die "Device is mounted: $dev" die "Device is mounted: ${dev}"
fi fi
if lsblk -nrpo NAME,MOUNTPOINT "$dev" | awk '$2 != "" { found=1 } END { exit !found }'; then if lsblk -nrpo NAME,MOUNTPOINT "${dev}" | awk '$2 != "" { found=1 } END { exit !found }'; then
die "Device or child is mounted: $dev" die "Device or child is mounted: ${dev}"
fi fi
} }
is_nvme_device() { is_nvme_device() {
local dev="$1" local dev="${1}"
local sysdev local sysdev
sysdev="/sys$(lsblk -ndo PATH "$dev" 2>/dev/null | head -n1)" sysdev="/sys$(lsblk -ndo PATH "${dev}" 2>/dev/null | head -n1)"
[ -n "$sysdev" ] || return 1 [ -n "${sysdev}" ] || return 1
[ -e "$sysdev" ] || return 1 [ -e "${sysdev}" ] || return 1
[ -d "$sysdev/device" ] || return 1 [ -d "${sysdev}/device" ] || return 1
[ -e "$sysdev/device/uevent" ] || return 1 [ -e "${sysdev}/device/uevent" ] || return 1
grep -q '^NVME=1$' "$sysdev/device/uevent" grep -q '^NVME=1$' "${sysdev}/device/uevent"
} }
is_ata_device() { is_ata_device() {
local dev="$1" local dev="${1}"
local tran local tran
tran="$(lsblk -ndo TRAN "$dev" 2>/dev/null | head -n1)" tran="$(lsblk -ndo TRAN "${dev}" 2>/dev/null | head -n1)"
[ "$tran" = "sata" ] || [ "$tran" = "ata" ] [ "${tran}" = "sata" ] || [ "${tran}" = "ata" ]
} }
confirm_wipe() { confirm_wipe() {
local dev="$1" local dev="${1}"
local choice="" local choice=""
echo "Device:" echo "Device:"
lsblk -o NAME,SIZE,TYPE,FSTYPE,MOUNTPOINT,MODEL,SERIAL "$dev" lsblk -o NAME,SIZE,TYPE,FSTYPE,MOUNTPOINT,MODEL,SERIAL "${dev}"
echo "" echo ""
echo "Type exactly: WIPE $dev" echo "Type exactly: WIPE ${dev}"
read -r -p "Confirm: " choice read -r -p "Confirm: " choice
[ "$choice" = "WIPE $dev" ] || die "Canceled" [ "${choice}" = "WIPE ${dev}" ] || die "Canceled"
echo "" echo ""
} }
@@ -107,14 +111,14 @@ confirm_root() {
} }
format_duration() { format_duration() {
local total="$1" local total="${1}"
local hours minutes seconds local hours minutes seconds
hours=$((total / 3600)) hours=$((total / 3600))
minutes=$(((total % 3600) / 60)) minutes=$(((total % 3600) / 60))
seconds=$((total % 60)) seconds=$((total % 60))
printf '%02d:%02d:%02d\n' "$hours" "$minutes" "$seconds" printf '%02d:%02d:%02d\n' "${hours}" "${minutes}" "${seconds}"
} }
print_time() { print_time() {
@@ -122,7 +126,7 @@ print_time() {
echo "" echo ""
log "Start date :" log "Start date :"
log "$STARTDATESTRING" log "${STARTDATESTRING}"
enddate=$(date +%s) enddate=$(date +%s)
calctime=$((enddate - STARTDATE)) calctime=$((enddate - STARTDATE))
@@ -133,121 +137,121 @@ print_time() {
echo "" echo ""
log "Total time :" log "Total time :"
format_duration "$calctime" >&2 format_duration "${calctime}" >&2
} }
wipe_with_blkdiscard_secure() { wipe_with_blkdiscard_secure() {
local dev="$1" local dev="${1}"
log "blkdiscard secure" log "blkdiscard secure"
blkdiscard -f -p 500M -s -v "$dev" blkdiscard -f -p 500M -s -v "${dev}"
} }
wipe_with_blkdiscard_zero() { wipe_with_blkdiscard_zero() {
local dev="$1" local dev="${1}"
log "blkdiscard zero" log "blkdiscard zero"
blkdiscard -f -p 500M -z -v "$dev" blkdiscard -f -p 500M -z -v "${dev}"
} }
wipe_with_dd() { wipe_with_dd() {
local dev="$1" local dev="${1}"
log "dd zero" log "dd zero"
dd if=/dev/zero of="$dev" bs=1M status=progress conv=fsync dd if=/dev/zero of="${dev}" bs=1M status=progress conv=fsync
} }
wipe_with_ddrescue() { wipe_with_ddrescue() {
local dev="$1" local dev="${1}"
command -v ddrescue >/dev/null 2>&1 || return 1 command -v ddrescue >/dev/null 2>&1 || return 1
log "ddrescue zero" log "ddrescue zero"
ddrescue -f -n /dev/zero "$dev" ddrescue -f -n /dev/zero "${dev}"
} }
wipe_with_dd_rescue() { wipe_with_dd_rescue() {
local dev="$1" local dev="${1}"
command -v dd_rescue >/dev/null 2>&1 || return 1 command -v dd_rescue >/dev/null 2>&1 || return 1
log "dd_rescue zero" log "dd_rescue zero"
dd_rescue -f /dev/zero "$dev" dd_rescue -f /dev/zero "${dev}"
} }
wipe_with_nvme() { wipe_with_nvme() {
local dev="$1" local dev="${1}"
command -v nvme >/dev/null 2>&1 || return 1 command -v nvme >/dev/null 2>&1 || return 1
is_nvme_device "$dev" || return 1 is_nvme_device "${dev}" || return 1
log "nvme format" log "nvme format"
nvme format "$dev" -s 1 >/dev/null nvme format "${dev}" -s 1 >/dev/null
} }
wipe_with_hdparm() { wipe_with_hdparm() {
local dev="$1" local dev="${1}"
command -v hdparm >/dev/null 2>&1 || return 1 command -v hdparm >/dev/null 2>&1 || return 1
is_ata_device "$dev" || return 1 is_ata_device "${dev}" || return 1
log "hdparm secure erase" log "hdparm secure erase"
hdparm --security-erase NULL "$dev" hdparm --security-erase NULL "${dev}"
} }
wipe_dev() { wipe_dev() {
local dev="$1" local dev="${1}"
STARTDATE=$(date +%s) STARTDATE=$(date +%s)
STARTDATESTRING="$(date)" STARTDATESTRING="$(date)"
log "Begin wiping: $dev" log "Begin wiping: ${dev}"
echo "" echo ""
log "Start date :" log "Start date :"
log "$STARTDATESTRING" log "${STARTDATESTRING}"
echo "" echo ""
if wipe_with_blkdiscard_secure "$dev"; then if wipe_with_blkdiscard_secure "${dev}"; then
echo "" echo ""
log "Device $dev wiped." log "Device ${dev} wiped."
return return
fi fi
echo "" echo ""
if wipe_with_blkdiscard_zero "$dev"; then if wipe_with_blkdiscard_zero "${dev}"; then
echo "" echo ""
log "Device $dev wiped." log "Device ${dev} wiped."
return return
fi fi
echo "" echo ""
if wipe_with_dd "$dev"; then if wipe_with_dd "${dev}"; then
echo "" echo ""
log "Device $dev wiped." log "Device ${dev} wiped."
return return
fi fi
echo "" echo ""
if wipe_with_ddrescue "$dev"; then if wipe_with_ddrescue "${dev}"; then
echo "" echo ""
log "Device $dev wiped." log "Device ${dev} wiped."
return return
fi fi
echo "" echo ""
if wipe_with_dd_rescue "$dev"; then if wipe_with_dd_rescue "${dev}"; then
echo "" echo ""
log "Device $dev wiped." log "Device ${dev} wiped."
return return
fi fi
echo "" echo ""
if wipe_with_nvme "$dev"; then if wipe_with_nvme "${dev}"; then
echo "" echo ""
log "Device $dev wiped." log "Device ${dev} wiped."
return return
fi fi
echo "" echo ""
if wipe_with_hdparm "$dev"; then if wipe_with_hdparm "${dev}"; then
echo "" echo ""
log "Device $dev wiped." log "Device ${dev} wiped."
return return
fi fi
@@ -257,10 +261,10 @@ wipe_dev() {
main() { main() {
check_args "$@" check_args "$@"
confirm_root confirm_root
check_device "$1" check_device "${1}"
check_device_not_in_use "$1" check_device_not_in_use "${1}"
confirm_wipe "$1" confirm_wipe "${1}"
wipe_dev "$1" wipe_dev "${1}"
print_time print_time
} }