This simple Bash script functions as a wrapper around dd, for the purpose of wiping a disk or partition while providing progress feedback to the user.
#!/bin/bash | |
# disk_wipe.sh | |
# Author: Kromey (http://kromey.us/) | |
# This simple Bash script leverages the dd utility to provide user feedback | |
# as progress is made, designed for the purpose of wiping hard drives or | |
# partitions. This script reads from /dev/zero to most efficiently zero-out | |
# the target device or partition. | |
# | |
# BEGIN CONFIG | |
# | |
#Commands | |
DD=`which dd` | |
RM=`which rm` | |
KILL=`which kill` | |
#Source file for overwrite | |
SOURCE=/dev/zero | |
#Destination device for suppressed output | |
NULL=/dev/null | |
#Temporary file for the script | |
TMP=/tmp/disk_wipe | |
# | |
# END CONFIG | |
# DO NOT EDIT BELOW THIS LINE! | |
# | |
#Script's first parameter is the disk to wipe | |
DISK=$1 | |
#This function monitors the dd process and provides near-real-time feedback | |
function monitor() { | |
#Sending the USR1 signal to dd causes it to output progress | |
while $KILL -USR1 $1 > $NULL 2>&1 | |
do | |
#Parse out the current progress | |
PROGRESS=`awk '/copied/ {print $1, "bytes", $3, $4}' $TMP` | |
#Empty our temp file | |
echo $NULL > $TMP | |
#Output progress, overwriting the previous progress line | |
printf "\r$PROGRESS" | |
#Pause for a quick breath | |
sleep 0.25 | |
done | |
} | |
#Clean up after ourselves | |
function clean_up() { | |
#Give us a new line | |
echo | |
#Kill the dd command, if it isn't already stopped | |
$KILL $PID_DD > $NULL 2>&1 | |
#Remove our temp file | |
$RM $TMP | |
#Aaaaand we're done | |
exit | |
} | |
#Catch the most common "stop" signals, and clean up | |
trap clean_up SIGHUP SIGINT SIGTERM | |
#Start the disk wipe | |
$DD if=$SOURCE of=$DISK bs=16M > $TMP 2>&1 & PID_DD=$! | |
echo Started dd with PID $PID_DD | |
#Keep an eye on our progress | |
monitor $PID_DD | |
#Clean up after ourselves | |
clean_up |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment