2022-03-06 15:22:37 +00:00
|
|
|
#!/usr/bin/env dash
|
2022-02-24 12:07:09 +00:00
|
|
|
# USAGE: erase-disk.sh <passes> <device>
|
|
|
|
# Must be run as root
|
|
|
|
|
2022-02-24 18:36:22 +00:00
|
|
|
usage() {
|
|
|
|
echo "Usage: erase-disk.sh PASSES DEVICE"
|
|
|
|
echo "Securely erase DEVICE with PASSES passes"
|
|
|
|
echo
|
|
|
|
echo "Flags:"
|
2022-03-06 15:50:10 +00:00
|
|
|
echo " -h, --help \t\tDisplay this help message"
|
|
|
|
echo " -c, --crypt-prep \tPrepare DEVICE for encryption"
|
2022-02-24 18:36:22 +00:00
|
|
|
echo
|
2022-03-06 15:50:10 +00:00
|
|
|
echo "Options (must come after flags):"
|
|
|
|
echo " DEVICE \t\tThe device to erase"
|
|
|
|
echo " PASSES \t\tHow many times to erase DEVICE"
|
2022-02-24 18:36:22 +00:00
|
|
|
echo
|
|
|
|
echo "This script will securely erase a disk device with the specified amount"
|
|
|
|
echo "of passes (rounds). It does so by overwriting the specified device with"
|
|
|
|
echo "random data on the first pass, and with zeroes on the other passes."
|
|
|
|
echo "Due to the nature of disk device access permissions, the script must"
|
|
|
|
echo "be run as root."
|
|
|
|
echo
|
|
|
|
echo "erase-disk.sh is licensed under The Unlicense."
|
|
|
|
}
|
|
|
|
|
2022-03-06 15:50:10 +00:00
|
|
|
CRYPT=0
|
|
|
|
|
2022-02-24 21:08:14 +00:00
|
|
|
[ "$1" = "-h" -o "$1" = "--help" ] && usage && exit 0
|
2022-03-06 15:50:10 +00:00
|
|
|
[ "$1" = "-c" -o "$1" = "--crypt-prep" ] && CRYPT=1 && shift
|
2022-02-24 12:07:09 +00:00
|
|
|
|
2022-02-24 21:08:14 +00:00
|
|
|
if [ $# -lt 2 ]; then
|
2022-03-06 15:50:10 +00:00
|
|
|
echo "=> ERROR: Not enough options!"
|
2022-02-24 21:08:14 +00:00
|
|
|
echo
|
|
|
|
usage
|
2022-02-24 18:36:22 +00:00
|
|
|
exit 1
|
2022-02-24 21:08:14 +00:00
|
|
|
elif [ $# -gt 2 ]; then
|
2022-03-06 15:50:10 +00:00
|
|
|
echo "=> ERROR: Too many options!"
|
2022-02-24 21:08:14 +00:00
|
|
|
echo
|
|
|
|
usage
|
2022-02-24 18:36:22 +00:00
|
|
|
exit 1
|
2022-02-24 21:08:14 +00:00
|
|
|
elif [ $(id -u) -ne 0 ]; then
|
2022-03-06 15:50:10 +00:00
|
|
|
echo "=> ERROR: Must run as root!"
|
2022-02-24 21:08:14 +00:00
|
|
|
echo
|
|
|
|
usage
|
2022-02-24 18:36:22 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2022-03-06 15:50:10 +00:00
|
|
|
echo "=> Securely erasing the disk device $2"
|
2022-02-24 12:07:09 +00:00
|
|
|
i=1
|
|
|
|
|
|
|
|
while [ $i -le $1 ]; do
|
|
|
|
[ $i -eq 1 ] && if="/dev/urandom" || if="/dev/zero"
|
2022-03-06 15:50:10 +00:00
|
|
|
[ $CRYPT -eq 1 -a $i -eq $1 ] && if="/dev/urandom"
|
2022-02-24 12:07:09 +00:00
|
|
|
|
2022-03-06 15:50:10 +00:00
|
|
|
echo "\n -> Begin pass $i with $if"
|
2022-02-24 12:07:09 +00:00
|
|
|
dd if="$if" of="$2" status="progress"
|
|
|
|
|
2022-03-06 15:50:10 +00:00
|
|
|
echo "\n -> Syncing I/O"
|
2022-02-24 12:07:09 +00:00
|
|
|
sync
|
|
|
|
|
|
|
|
i=$(( i + 1 ))
|
|
|
|
done
|
2022-03-06 15:50:10 +00:00
|
|
|
|
|
|
|
echo -n "\n=> Done! $2 securely erased"
|
|
|
|
[ $CRYPT -eq 1 ] && echo -n " and prepared for encryption"
|
|
|
|
echo "."
|
|
|
|
|