Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
#!/bin/sh
COMMAND=$1
FILENAME=$2
FILESIZE=$3
usage() {
echo "Usage: ./simpleluks.sh command [args ...]"
echo " ./simpleluks.sh create some_name size_in_GB"
echo " ./simpleluks.sh open some_name"
echo " ./simpleluks.sh close some_name"
}
# No command? Show help
if [ "$#" -lt 2 ]; then
usage
else
# Accept command
echo "Command: $COMMAND"
if [ "$COMMAND" = "create" ]; then
if [ "$#" -lt 3 ]; then
usage
exit
fi
# create
# Accept filename, size of file in gigabytes, filesystem
echo "Writing zeroes into $FILENAME"
sudo dd if=/dev/zero bs=1G count="$FILESIZE" of="$FILENAME"
echo "Preparing $FILENAME for LUKS"
sudo cryptsetup luksFormat "$FILENAME"
echo "Opening $FILENAME with LUKS"
sudo cryptsetup luksOpen "$FILENAME" "$FILENAME"
echo "Writing ext4 filesystem to $FILENAME"
sudo mkfs.ext4 "/dev/mapper/$FILENAME"
echo "Creating $HOME/cryptoluks/$FILENAME"
mkdir -p "$HOME/cryptoluks/$FILENAME"
echo "Mounting $filename to $HOME/cryptoluks/$FILENAME"
sudo mount -t ext4 "/dev/mapper/$FILENAME" "$HOME/cryptoluks/$FILENAME"
echo "Done!"
echo "You may want to run sudo chown $USER -R $HOME/cryptoluks/$FILENAME"
elif [ "$COMMAND" = "open" ]; then
# open
sudo cryptsetup luksOpen "$FILENAME" "$FILENAME"
mkdir -p "$HOME/cryptoluks/$FILENAME"
sudo mount -t ext4 "/dev/mapper/$FILENAME" "$HOME/cryptoluks/$FILENAME"
echo "Mounted $FILENAME to $HOME/cryptoluks/$FILENAME"
elif [ "$COMMAND" = "close" ]; then
# close
sudo umount "$HOME/cryptoluks/$FILENAME" \
&& sudo cryptsetup luksClose "$FILENAME" \
&& rm -r "$HOME/cryptoluks/$FILENAME"
echo "Unmounted and closed $FILENAME"
else
usage
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment