This script is for testing new drives. ALL DATA WILL BE ERASED
#!/bin/sh
if [ ! -e "$1" ]; then
echo "usage: $0 <drive-path>"
exit 127
else
read -p "'$1' is going to be erased, type YES to continue: " ANS
if [ "$ANS" != "YES" ]; then
echo "Aborted"
exit 127
fi
fi
DRIVE="$1"
echo "Performing initial read-only test"
ERRORS=$(badblocks -s -v -e 1 -b 1048576 "$DRIVE")
if [ -n "$ERRORS" ]; then
echo "First read test failed"
exit 127
fi
CRYPT="$(basename "$DRIVE")_crypt"
KEY="$(mktemp)" ||
{ echo "mktemp failed"; exit 127; }
terminate() {
sync
cryptsetup close "$CRYPT" >/dev/null 2>&1
rm -f "$KEY"
}
trap terminate EXIT
openssl rand 64 >> "$KEY" ||
{ echo "openssl failed"; exit 127; }
cryptsetup open --type=plain --cipher=aes-xts-plain64 --key-size=512 --key-file="$KEY" "$DRIVE" "$CRYPT" ||
{ echo "cryptsetup1 failed"; exit 127; }
echo "Writing data..."
dd if=/dev/zero of="/dev/mapper/$CRYPT" bs=1M status=progress oflag=direct
# the device won't close if it's still "in use"
sync; sync
sleep 5
sync; sync
cryptsetup close "$CRYPT"
sync; sync
cryptsetup open --type=plain --cipher=aes-xts-plain64 --key-size=512 --key-file="$KEY" "$DRIVE" "$CRYPT" ||
{ echo "cryptsetup2 failed"; exit 127; }
echo "Verifying data..."
badblocks -s -v -e 1 -b 1048576 -t 0 "/dev/mapper/$CRYPT" >/dev/null