2014-05-28 20:56:12 -04:00
|
|
|
#!/usr/bin/env bash
|
2012-04-05 06:37:03 -04:00
|
|
|
|
2014-05-28 20:56:12 -04:00
|
|
|
##############################################################
|
|
|
|
# #
|
|
|
|
# CryptoBox #
|
|
|
|
# #
|
|
|
|
# A script that wraps cryptsetup, mkfs and losetup to make #
|
|
|
|
# it easy to create, mount and unmount encrypted image #
|
|
|
|
# files using LUKS. #
|
|
|
|
# #
|
|
|
|
##############################################################
|
2012-04-05 06:37:03 -04:00
|
|
|
|
2014-05-28 20:56:12 -04:00
|
|
|
NAME=`echo $0 | grep -o -e "[^\/]*$"`
|
2012-04-05 06:37:03 -04:00
|
|
|
|
2014-05-28 20:56:12 -04:00
|
|
|
# Check for root
|
|
|
|
[[ "$UID" -ne 0 ]] && (echo -e "Error: Please run this script with root permissions\n"; exit 1)
|
|
|
|
|
|
|
|
# Check dependencies
|
|
|
|
[[ `type -P dd` ]] || (echo "Error: The 'dd' program is missing"; exit 1)
|
|
|
|
[[ `type -P losetup` ]] || (echo "Error: The 'losetup' program is missing"; exit 1)
|
|
|
|
[[ `type -P cryptsetup` ]] || (echo "Error: The 'cryptsetup' program is missing"; exit 1)
|
|
|
|
[[ `type -P mkfs` ]] || (echo "Error: The 'mkfs' program is missing"; exit 1)
|
|
|
|
|
|
|
|
# Load modules if they aren't present
|
|
|
|
[[ `lsmod | grep loop` ]] || echo "loading 'loop' module"; modprobe loop || (echo "Error: failed to load 'loop' module"; exit 1)
|
|
|
|
[[ `lsmod | grep dm_mod` ]] || echo "loading 'dm_mod' module"; modprobe dm_mod || (echo "Error: failed to load 'dm_mod' module"; exit 1)
|
2012-04-05 06:37:03 -04:00
|
|
|
|
|
|
|
function usage {
|
2014-05-28 20:56:12 -04:00
|
|
|
echo -e "${NAME}: create and mount encrypted images\n"
|
|
|
|
echo "Usage: ${NAME} option arguments"
|
2012-04-05 06:37:03 -04:00
|
|
|
echo -e "\tc (create) -> $NAME c filename.img filesystem size-in-mb"
|
|
|
|
echo -e "\tm (mount) -> $NAME m filename.img /mount/point"
|
|
|
|
echo -e "\tu (umount) -> $NAME u /mount/point"
|
|
|
|
}
|
|
|
|
|
|
|
|
if [ -z "$1" ]; then
|
|
|
|
usage
|
|
|
|
exit 1
|
|
|
|
elif [ ! "$1" = "c" -a ! "$1" = "m" -a ! "$1" = "u" ]; then
|
|
|
|
echo "syntax error"
|
|
|
|
usage
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
case "$1" in
|
|
|
|
"c")
|
|
|
|
if [ -z "$2" -o -z "$3" -o -z "$4" ]; then
|
|
|
|
echo "syntax error"
|
|
|
|
usage
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
if [ -f "$2" ]; then
|
|
|
|
echo "error: $2 already exists"
|
|
|
|
exit 1
|
|
|
|
fi
|
2014-05-28 20:56:12 -04:00
|
|
|
LOOPDEV=`losetup -f`
|
|
|
|
CONTAINER=`echo "$2" | sed s/"[^\/]*\/"//g | sed s/"\.".*$//g`
|
2012-04-05 06:37:03 -04:00
|
|
|
dd bs=1M count="$4" if=/dev/urandom of="$2"
|
|
|
|
losetup $LOOPDEV "$2"
|
|
|
|
cryptsetup -c aes-xts-plain -y -s 512 luksFormat $LOOPDEV
|
|
|
|
cryptsetup luksOpen $LOOPDEV $CONTAINER
|
|
|
|
mkfs -t "$3" /dev/mapper/$CONTAINER || echo "error: mkfs failed for filesystem type: $3"
|
|
|
|
cryptsetup luksClose $CONTAINER
|
|
|
|
losetup -d $LOOPDEV
|
|
|
|
;;
|
|
|
|
"m")
|
2014-05-28 20:56:12 -04:00
|
|
|
if [ -z "$2" -o -z "$3" ]; then echo -e "Error: syntax\n"; usage; exit 1; fi
|
|
|
|
[[ ! -f "$2" ]] && (echo "Error: $2 does not exist"; exit 1)
|
|
|
|
[[ -d "$3" ]] || (echo "Error: $3 does not exist"; exit 1)
|
2012-04-05 06:37:03 -04:00
|
|
|
LOOPDEV=$(losetup -f)
|
|
|
|
CONTAINER=$(echo $LOOPDEV | sed s/"[^\/]*\/"//g | sed s/"\.".*$//g)
|
|
|
|
losetup $LOOPDEV "$2"
|
|
|
|
cryptsetup luksOpen $LOOPDEV $CONTAINER
|
|
|
|
mount /dev/mapper/$CONTAINER "$3"
|
|
|
|
;;
|
|
|
|
"u")
|
|
|
|
if [ -z "$2" ]; then
|
|
|
|
echo "syntax error"
|
|
|
|
usage
|
|
|
|
exit 1
|
|
|
|
fi
|
2014-05-28 20:56:12 -04:00
|
|
|
MOUNT=`mount | grep $(echo "$2" | sed s/"\/"$//)`
|
2012-04-05 06:37:03 -04:00
|
|
|
if [ -z "$MOUNT" ]; then
|
|
|
|
echo "error: $2 is not mounted"
|
|
|
|
exit 1
|
|
|
|
fi
|
2014-05-28 20:56:12 -04:00
|
|
|
LOOPDEV=`echo $MOUNT | sed s/\ .*//g | sed s/"\/mapper"//`
|
|
|
|
CONTAINER=`echo $LOOPDEV | sed s/"[^\/]*\/"//g | sed s/"\.".*$//g`
|
2012-04-05 06:37:03 -04:00
|
|
|
umount "$2"
|
|
|
|
cryptsetup luksClose $CONTAINER
|
|
|
|
losetup -d $LOOPDEV
|
|
|
|
;;
|
|
|
|
generic)
|
|
|
|
usage
|
|
|
|
;;
|
|
|
|
esac
|