fix: improve mtm-ddwipe argument handling and timing output

This commit is contained in:
2026-04-27 23:17:16 +02:00
parent 08d6ccde0d
commit b01e72cba6
Executable → Regular
+46 -42
View File
@@ -1,14 +1,13 @@
#!/bin/bash #!/bin/bash
declare -r DEV="${1}" VERSION="0.0.3"
declare STARTDATE="" STARTDATE=""
declare STARTDATESTRING="" STARTDATESTRING=""
show_help() { show_help() {
echo "Usage: mtm-ddwipe DEVICE" echo "Usage: mtm-ddwipe DEVICE"
echo ""
echo "Wipe a block device." echo "Wipe a block device."
echo "Version: 0.0.3" echo "Version: ${VERSION}"
} }
check_args() { check_args() {
@@ -21,6 +20,11 @@ check_args() {
show_help show_help
exit 1 exit 1
;; ;;
-*)
echo "Invalid option."
show_help
exit 1
;;
esac esac
} }
@@ -43,43 +47,16 @@ confirm_wipe() {
echo "" echo ""
read -r -p "Wipe ${1} (y/[n])? " CHOICE read -r -p "Wipe ${1} (y/[n])? " CHOICE
case "${CHOICE}" in case "${CHOICE}" in
y|Y ) echo "";; y|Y)
* ) echo ""
;;
*)
echo "Canceled" echo "Canceled"
exit 1 exit 1
;; ;;
esac esac
} }
wipe_dev() {
STARTDATE=$(date +%s)
STARTDATESTRING="$(date)"
echo "Begin wiping device ${1}"
echo ""
echo "Start date :"
echo "${STARTDATESTRING}"
echo ""
echo "blkdiscard secure"
if ! blkdiscard -f -p 500M -s -v "${1}"; then
echo ""
echo "blkdiscard zero"
if ! blkdiscard -f -p 500M -z -v "${1}"; then
echo ""
echo "dd zero"
if ! dd if=/dev/zero of="${1}" bs=1M status=progress; then
# Need check if dd has all writed, if yes, return no error
# echo "Error wiping device ${1}, use phisical destroy !"
echo "Wiped with dd, check if full size is writed."
echo "Otherwise use a mechanical destruction of the device."
print_time
exit 1
fi
fi
fi
echo ""
echo "Device ${1} wiped."
}
print_time() { print_time() {
echo "" echo ""
echo "Start date :" echo "Start date :"
@@ -96,9 +73,36 @@ print_time() {
date -d@${CALCTIME} -u +%H:%M:%S date -d@${CALCTIME} -u +%H:%M:%S
} }
check_args "${DEV}" wipe_dev() {
check_dev_exist "${DEV}" STARTDATE=$(date +%s)
check_dev_block "${DEV}" STARTDATESTRING="$(date)"
confirm_wipe "${DEV}" echo "Begin wiping device ${1}"
wipe_dev "${DEV}" echo ""
echo "Start date :"
echo "${STARTDATESTRING}"
echo ""
echo "blkdiscard secure"
if ! blkdiscard -f -p 500M -s -v "${1}"; then
echo ""
echo "blkdiscard zero"
if ! blkdiscard -f -p 500M -z -v "${1}"; then
echo ""
echo "dd zero"
if ! dd if=/dev/zero of="${1}" bs=1M status=progress; then
echo "Wiped with dd, check if full size is writed."
echo "Otherwise use a mechanical destruction of the device."
print_time
exit 1
fi
fi
fi
echo ""
echo "Device ${1} wiped."
}
check_args "${1}"
check_dev_exist "${1}"
check_dev_block "${1}"
confirm_wipe "${1}"
wipe_dev "${1}"
print_time print_time