6 Commits

Author SHA1 Message Date
matmoul ab346342d5 docs: update usage and shell style conventions 2026-04-28 01:03:19 +02:00
matmoul bf9cbe918f chore: remove VS Code ignore rules 2026-04-28 00:35:05 +02:00
matmoul 62ac956836 fix: reorder wipe method fallback sequence 2026-04-28 00:33:27 +02:00
matmoul 07c210164a feat: add optional wipe tool fallbacks by device type 2026-04-28 00:28:50 +02:00
matmoul c56aec350d docs: tighten project conventions for mtm-ddwipe 2026-04-27 23:46:51 +02:00
matmoul 956cfd7325 fix: tighten mtm-ddwipe usage and device checks
Add an explicit mounted-device check with findmnt, shorten confirmation prompts, and align the usage text and log wording with the current script name.
2026-04-27 23:40:28 +02:00
4 changed files with 175 additions and 87 deletions
+19 -20
View File
@@ -3,31 +3,30 @@ description: mtm-ddwipe project conventions
--- ---
# Project conventions # Project conventions
- Use English throughout the project. - Use English.
- Keep shell scripts Bash-based. - Use Bash for shell scripts.
- Preserve the current behavior of the main script: `mtm-ddwipe` wipes block devices. - Keep `mtm-ddwipe` focused on wiping block devices.
- Strengthen destructive-action safety checks in `mtm-ddwipe`.
- Keep `mtm-ddwipe` interactive by default. - Keep `mtm-ddwipe` interactive by default.
- Require explicit confirmation before destructive actions. - Require explicit confirmation before destructive actions.
- Validate real block devices and refuse mounted or in-use targets.
- Show clear device details before confirmation. - Show clear device details before confirmation.
- Check that target devices are not mounted or in use before wiping.
- Keep user-facing messages short and clear.
- Keep error and help messages short and clear.
- Prefer minimal, focused changes that preserve intent.
- Keep `.continue/rules/project.md` aligned with project conventions and concise.
- `mtm-ddwipe` is a small Bash script with helper functions.
- Keep the host and line-number removal behavior intact for related output processing.
- `mtm-ddwipe` must print a usage line and support `-h`/`--help`.
- Validate that wipe targets are real block devices before operating on them.
- Keep short, explicit confirmation prompts before destructive operations.
- Prefer confirmation prompts that require typing the target device path or an exact safety phrase.
- Keep help text concise, usage-first, and warning-focused.
- Keep destructive safeguards strict and explicit. - Keep destructive safeguards strict and explicit.
- If adding non-interactive support, make it an opt-in safety flag. - Keep messages short and clear.
- Keep device identification prompts clear and specific. - Keep help concise, usage-first, and warning-focused.
- Preserve the fallback wipe flow: secure discard, zero discard, then zero-fill with `dd`. - Preserve the fallback wipe flow: secure discard, zero discard, then `dd` zero-fill.
- Keep timing and status output short and readable. - Keep optional tools optional.
- Use extra wipe methods only if the command is present.
- Use `ddrescue` or `dd_rescue` only after `dd` fails.
- Keep `nvme` and `hdparm` optional.
- Use `nvme` and `hdparm` only on matching device types.
- Do not make new external tools required.
- Keep changes minimal and preserve intent.
- 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.
# Project identity # Project identity
- Main script: `mtm-ddwipe` - Main script: `mtm-ddwipe`
- License: GNU GPL v3 - License: GNU GPL v3
-14
View File
@@ -1,14 +0,0 @@
# ---> VisualStudioCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
!.vscode/*.code-snippets
# Local History for Visual Studio Code
.history/
# Built Visual Studio Code Extensions
*.vsix
+17
View File
@@ -1,2 +1,19 @@
# mtm-ddwipe # mtm-ddwipe
Wipe a block device.
Warning: destructive and irreversible.
Target must be a real block device, not mounted, and not in use.
Usage:
mtm-ddwipe DEVICE
mtm-ddwipe -h | --help
Options:
-h, --help Show help.
Notes:
- Interactive by default.
- Confirm exactly before wiping.
- Optional tools: blkdiscard, ddrescue, dd_rescue, nvme, hdparm.
+139 -53
View File
@@ -8,14 +8,14 @@ STARTDATESTRING=""
usage() { usage() {
cat <<EOF cat <<EOF
Usage: mtm-ddwipe-2 DEVICE Usage: mtm-ddwipe DEVICE
Wipe a block device. Wipe a block device.
Warnings: Warnings:
- This is destructive and irreversible. - Destructive and irreversible.
- The target device must not be mounted or in use. - Target must be a real block device, not mounted, and not in use.
- blkdiscard support depends on the device and firmware. - Optional tools: blkdiscard, ddrescue, dd_rescue, nvme, hdparm.
- dd fallback may take a long time. - dd fallback may take a long time.
Version: ${VERSION} Version: ${VERSION}
@@ -33,48 +33,74 @@ die() {
check_args() { check_args() {
if [ $# -ne 1 ]; then if [ $# -ne 1 ]; then
case "${1:-}" in
-h|--help)
usage
exit 0
;;
"")
usage
exit 1
;;
-*)
die "Invalid option."
;;
esac
usage usage
exit 1 exit 1
fi fi
case "$1" in
-h|--help)
usage
exit 0
;;
-*)
die "Invalid option."
;;
esac
} }
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 lsblk -nrpo NAME,MOUNTPOINT "$dev" | awk '$2 != "" { found=1 } END { exit !found }'; then if findmnt -rn --target "${dev}" >/dev/null 2>&1; then
die "Device or one of its children is mounted: $dev" die "Device is mounted: ${dev}"
fi
if lsblk -nrpo NAME,MOUNTPOINT "${dev}" | awk '$2 != "" { found=1 } END { exit !found }'; then
die "Device or child is mounted: ${dev}"
fi fi
} }
is_nvme_device() {
local dev="${1}"
local sysdev
sysdev="/sys$(lsblk -ndo PATH "${dev}" 2>/dev/null | head -n1)"
[ -n "${sysdev}" ] || return 1
[ -e "${sysdev}" ] || return 1
[ -d "${sysdev}/device" ] || return 1
[ -e "${sysdev}/device/uevent" ] || return 1
grep -q '^NVME=1$' "${sysdev}/device/uevent"
}
is_ata_device() {
local dev="${1}"
local tran
tran="$(lsblk -ndo TRAN "${dev}" 2>/dev/null | head -n1)"
[ "${tran}" = "sata" ] || [ "${tran}" = "ata" ]
}
confirm_wipe() { confirm_wipe() {
local dev="$1" local dev="${1}"
local choice="" local choice=""
echo "Selected 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 "This will destroy data on: $dev" echo "Type exactly: WIPE ${dev}"
echo "Type exactly: WIPE $dev" read -r -p "Confirm: " choice
echo "" [ "${choice}" = "WIPE ${dev}" ] || die "Canceled"
read -r -p "Confirmation: " choice
[ "$choice" = "WIPE $dev" ] || die "Canceled"
echo "" echo ""
} }
@@ -85,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() {
@@ -100,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))
@@ -111,61 +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
log "Wiped with dd, check if full size is written." }
log "Otherwise use a mechanical destruction of the device."
wipe_with_ddrescue() {
local dev="${1}"
command -v ddrescue >/dev/null 2>&1 || return 1
log "ddrescue zero"
ddrescue -f -n /dev/zero "${dev}"
}
wipe_with_dd_rescue() {
local dev="${1}"
command -v dd_rescue >/dev/null 2>&1 || return 1
log "dd_rescue zero"
dd_rescue -f /dev/zero "${dev}"
}
wipe_with_nvme() {
local dev="${1}"
command -v nvme >/dev/null 2>&1 || return 1
is_nvme_device "${dev}" || return 1
log "nvme format"
nvme format "${dev}" -s 1 >/dev/null
}
wipe_with_hdparm() {
local dev="${1}"
command -v hdparm >/dev/null 2>&1 || return 1
is_ata_device "${dev}" || return 1
log "hdparm secure erase"
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 device $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
fi
echo ""
if wipe_with_ddrescue "${dev}"; then
echo ""
log "Device ${dev} wiped."
return
fi
echo ""
if wipe_with_dd_rescue "${dev}"; then
echo ""
log "Device ${dev} wiped."
return
fi
echo ""
if wipe_with_nvme "${dev}"; then
echo ""
log "Device ${dev} wiped."
return
fi
echo ""
if wipe_with_hdparm "${dev}"; then
echo ""
log "Device ${dev} wiped."
return return
fi fi
@@ -175,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
} }