Skip to content

Instantly share code, notes, and snippets.

@fiechr
Created July 17, 2023 10:23
Show Gist options
  • Save fiechr/510113055911a82387767e3bade91f4e to your computer and use it in GitHub Desktop.
Save fiechr/510113055911a82387767e3bade91f4e to your computer and use it in GitHub Desktop.
Simple shell script to overwrite beginning and end of block devices with zeros.
#!/bin/bash
#set -x
if [ "$(id -u)" != "0" ]; then
echo "Sorry, you need to be root to do that."
exit 1
fi
if [ -b "$1" ]; then
DEVICE="$1"
BLOCK_SIZE=$(blockdev --getss "${DEVICE}")
COUNT=4096
echo "This will OVERWRITE the begin and end of ${DEVICE} with 2 MB of zeroes."
echo "ARE YOU SURE?"
read -p "Please re-enter the device name for confirmation: " CONFIRMED_DEVICE
if [ "${CONFIRMED_DEVICE}" != "${DEVICE}" ]; then
echo "Device names don't match! Exiting."
exit 1
fi
dd if=/dev/zero of="${DEVICE}" bs=${BLOCK_SIZE} count=${COUNT}
dd if=/dev/zero of="${DEVICE}" bs=${BLOCK_SIZE} seek=$(( $(blockdev --getsz "${DEVICE}") - ${COUNT} )) count=${COUNT}
echo "Done."
else
echo "Argument needs to be an existing block device. Exiting."
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment