Skip to content

Instantly share code, notes, and snippets.

@aadityabhatia
Created August 8, 2020 21:22
Show Gist options
  • Save aadityabhatia/f931a9ac2b3878af949f1107e35ab66e to your computer and use it in GitHub Desktop.
Save aadityabhatia/f931a9ac2b3878af949f1107e35ab66e to your computer and use it in GitHub Desktop.
wipe head and tail of a block device
#!/bin/bash
set -e
# wipe first and last 10MiB
SECTORS_WIPE=20480
# ensure $1 is block device
test -b $1
# get number of 512-byte sectors
sectors=$(blockdev --getsz $1)
# check size constraints
test "$sectors" -gt "$SECTORS_WIPE"
tailSeek=$(($sectors - $SECTORS_WIPE))
# display disk information
lsblk -o NAME,SIZE,TYPE,VENDOR,MODEL,REV,SERIAL,STATE $1
# display actions
printf "\nWiping $1...\nTotal 512-byte sectors: $sectors\nWiping 0-$SECTORS_WIPE, $tailSeek-$sectors\n\n"
# prompt for user confirmation
read -p "Proceed? [y/N] " -n 1 -r proceed
echo
test "y$proceed" != "yy" && test "y$proceed" != "yY" && echo "Aborted." && exit
printf "\n1: Write random to head\n"; date
echo dd if=/dev/urandom of=$1 count=$SECTORS_WIPE
dd if=/dev/urandom of=$1 count=$SECTORS_WIPE
printf "\n2: Write zeroes to head\n"; date
echo dd if=/dev/zero of=$1 count=$SECTORS_WIPE
dd if=/dev/zero of=$1 count=$SECTORS_WIPE
printf "\n3: Write random to tail\n"; date
echo dd if=/dev/urandom of=$1 seek=$tailSeek count=$SECTORS_WIPE
dd if=/dev/urandom of=$1 seek=$tailSeek count=$SECTORS_WIPE
printf "\n4: Write zeroes to tail\n"; date
echo dd if=/dev/zero of=$1 seek=$tailSeek count=$SECTORS_WIPE
dd if=/dev/zero of=$1 seek=$tailSeek count=$SECTORS_WIPE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment