mirror of
https://github.com/prurigro/cryptobox.git
synced 2024-11-24 06:31:26 -05:00
f719c5df60
made, and errors now produce error messages and then exit with a failed state
87 lines
3.9 KiB
Bash
Executable file
87 lines
3.9 KiB
Bash
Executable file
#!/usr/bin/env bash
|
|
|
|
##############################################################
|
|
# #
|
|
# CryptoBox #
|
|
# #
|
|
# A script that wraps cryptsetup, mkfs and losetup to make #
|
|
# it easy to create, mount and unmount encrypted image #
|
|
# files using LUKS. #
|
|
# #
|
|
##############################################################
|
|
|
|
NAME=`echo "$0" | grep -o -e "[^\/]*$"`
|
|
|
|
function errorquit {
|
|
echo -e "Error: $1"
|
|
[[ "$1" = "syntax" ]] && (echo; usage)
|
|
exit 1
|
|
}
|
|
|
|
function usage {
|
|
echo -e "${NAME}: create and mount encrypted images\n"
|
|
echo "Usage: $NAME option arguments"
|
|
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"
|
|
}
|
|
|
|
# Check for root
|
|
[[ "$UID" -ne 0 ]] && errorquit "run with root permission\n"
|
|
|
|
# Check dependencies
|
|
[[ `type -P dd` ]] || errorquit "Error: The 'dd' program is missing"
|
|
[[ `type -P losetup` ]] || errorquit "Error: The 'losetup' program is missing"
|
|
[[ `type -P cryptsetup` ]] || errorquit "Error: The 'cryptsetup' program is missing"
|
|
[[ `type -P mkfs` ]] || errorquit "Error: The 'mkfs' program is missing"
|
|
|
|
# Load modules if they aren't present
|
|
[[ `lsmod | grep loop` ]] || echo "loading 'loop' module"; modprobe loop || errorquit "Error: failed to load 'loop' module"
|
|
[[ `lsmod | grep dm_mod` ]] || echo "loading 'dm_mod' module"; modprobe dm_mod || errorquit "Error: failed to load 'dm_mod' module"
|
|
|
|
if [ -z "$1" ]; then
|
|
usage; exit 1
|
|
elif [ ! "$1" = "c" -a ! "$1" = "m" -a ! "$1" = "u" ]; then
|
|
errorquit "syntax"
|
|
fi
|
|
|
|
case "$1" in
|
|
c)
|
|
if [ -z "$2" -o -z "$3" -o -z "$4" ]; then errorquit "syntax"; fi
|
|
[[ -f "$2" ]] && errorquit "$2 already exists"
|
|
LOOPDEV=`losetup -f`
|
|
CONTAINER=`echo "$2" | sed s/"[^\/]*\/"//g | sed s/"\.".*$//g`
|
|
dd bs=1M count="$4" if=/dev/urandom of="$2" || errorquit "couldn't create create image file"
|
|
losetup "$LOOPDEV" "$2" || errorquit "couldn't setup loop device (${LOOPDEV})"
|
|
cryptsetup -c aes-xts-plain -y -s 512 luksFormat "$LOOPDEV" || errorquit "couldn't encrypt image file"
|
|
cryptsetup luksOpen "$LOOPDEV" "$CONTAINER" || errorquit "couldn't decrypt $CONTAINER"
|
|
mkfs -t "$3" "/dev/mapper/${CONTAINER}" || errorquit "mkfs failed for filesystem type: $3"
|
|
cryptsetup luksClose "$CONTAINER" || errorquit "couldn't close encryption for $CONTAINER"
|
|
sleep 1
|
|
losetup -d "$LOOPDEV" || errorquit "couldn't close loop device (${LOOPDEV})"
|
|
;;
|
|
m)
|
|
if [ -z "$2" -o -z "$3" ]; then errorquit "syntax"; fi
|
|
[[ ! -f "$2" ]] && errorquit "$2 does not exist"
|
|
[[ -d "$3" ]] || errorquit "$3 does not exist"
|
|
LOOPDEV=$(losetup -f)
|
|
CONTAINER=$(echo "$LOOPDEV" | sed s/"[^\/]*\/"//g | sed s/"\.".*$//g)
|
|
losetup "$LOOPDEV" "$2" || errorquit "couldn't setup loop device (${LOOPDEV})"
|
|
cryptsetup luksOpen "$LOOPDEV" "$CONTAINER" || errorquit "couldn't decrypt $CONTAINER"
|
|
mount "/dev/mapper/${CONTAINER}" "$3" || errorquit "couldn't mount /dev/mapper/${CONTAINER} on $3"
|
|
;;
|
|
u)
|
|
if [ -z "$2" ]; then errorquit "syntax"; fi
|
|
MOUNT=`mount | grep $(echo "$2" | sed s/"\/"$//)`
|
|
[[ -z "$MOUNT" ]] && errorquit "$2 is not mounted"
|
|
LOOPDEV=`echo "$MOUNT" | sed s/\ .*//g | sed s/"\/mapper"//`
|
|
CONTAINER=`echo "$LOOPDEV" | sed s/"[^\/]*\/"//g | sed s/"\.".*$//g`
|
|
umount "$2" || errorquit "Couldn't unmount $2"
|
|
cryptsetup luksClose "$CONTAINER" || errorquit "couldn't close encryption for $CONTAINER"
|
|
sleep 1
|
|
losetup -d "$LOOPDEV" || errorquit "couldn't close loop device (${LOOPDEV})"
|
|
;;
|
|
generic)
|
|
usage
|
|
;;
|
|
esac
|