Skip to content

Instantly share code, notes, and snippets.

@frizz925
Last active October 23, 2018 16:40
Show Gist options
  • Save frizz925/5b72959892c0a22c98677b7c7427e9df to your computer and use it in GitHub Desktop.
Save frizz925/5b72959892c0a22c98677b7c7427e9df to your computer and use it in GitHub Desktop.
Keyscript for cryptsetup
#!/bin/sh
timeout=30
counter=0
keyfile="$1"
echo_error() {
echo $@ >&2
}
password_fallback() {
echo_error "Falling back to password entry."
/lib/cryptsetup/askpass "Enter password for ${CRYPTTAB_NAME}: "
}
if [ -z "$keyfile" ]; then
echo_error "Keyfile argument not provided."
password_fallback
exit $?
fi
while [ ! -e "$keyfile" ] && [ $counter -lt $timeout ]; do
counter=$(($counter + 1))
sleep 1
done
keyfile=$(readlink -f "$keyfile")
if [ ! -e "$keyfile" ]; then
echo_error "Keyfile unavailable."
password_fallback
elif [ ! -r "$keyfile" ]; then
echo_error "Keyfile is not readable."
password_fallback
elif [ -d "$keyfile" ]; then
echo_error "Keyfile is a directory."
password_fallback
elif [ -b "$keyfile" ]; then
cat "$keyfile"
elif [ ! -s "$keyfile" ]; then
echo_error "Keyfile is empty."
password_fallback
else
cat "$keyfile"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment