diff --git a/setup/ehdd/create_hdd.sh b/setup/ehdd/create_hdd.sh index 2a5e1fdf..ea0e0912 100755 --- a/setup/ehdd/create_hdd.sh +++ b/setup/ehdd/create_hdd.sh @@ -1,28 +1,22 @@ #!/bin/bash -if [ -s /etc/mailinabox.conf ]; then - source /etc/mailinabox.conf - [ $? -eq 0 ] || exit 1 -else - STORAGE_ROOT="/home/${STORAGE_USER:-user-data}" -fi -EHDD_IMG="$STORAGE_ROOT.HDD" -EHDD_SIZE_GB="$1" -MOUNTPOINT="$STORAGE_ROOT" +source "setup/ehdd/ehdd_funcs.sh" || exit 1 if [ "$1" == "" ]; then echo "usage: $0 " echo -n " hdd image location: $EHDD_IMG" - if [ -e "$EHDD_IMG" ]; then echo " (exists)"; else echo ""; fi + if [ -e "$EHDD_IMG" ]; then echo " (exists!)"; else echo ""; fi exit 1 elif [ "$1" == "-location" ]; then echo "$EHDD_IMG" exit 0 elif [ "$1" == "-mountpoint" ]; then - echo "$MOUNTPOINT" + echo "$EHDD_MOUNTPOINT" exit 0 fi +EHDD_SIZE_GB="$1" + if [ ! -e "$EHDD_IMG" ]; then echo "Creating ${EHDD_SIZE_GB}G encryped drive: $EHDD_IMG" @@ -30,19 +24,20 @@ if [ ! -e "$EHDD_IMG" ]; then [ $count -eq 0 ] && echo "Invalid size" && exit 1 apt-get -q=2 -y install cryptsetup || exit 1 dd if=/dev/zero of="$EHDD_IMG" bs=1M count=$count || exit 1 - losetup /dev/loop0 "$EHDD_IMG" || exit 1 - if ! cryptsetup luksFormat -i 15000 /dev/loop0; then - losetup -d /dev/loop0 + loop=$(find_unused_loop) + losetup $loop "$EHDD_IMG" || exit 1 + if ! cryptsetup luksFormat -i 15000 $loop; then + losetup -d $loop rm -f "$EHDD_IMG" exit 1 fi echo "" echo "NOTE: You will need to reenter your drive encryption password a number of times" - cryptsetup luksOpen /dev/loop0 c1 # map device to /dev/mapper/c1 - mke2fs -j /dev/mapper/c1 - cryptsetup luksClose c1 - losetup -d /dev/loop0 + cryptsetup luksOpen $loop $EHDD_LUKS_NAME # map device to /dev/mapper/NAME + mke2fs -j /dev/mapper/$EHDD_LUKS_NAME + cryptsetup luksClose $EHDD_LUKS_NAME + losetup -d $loop else - echo "$EHDD_IMG already exists..." + echo "ERROR: $EHDD_IMG already exists!" exit 1 fi diff --git a/setup/ehdd/ehdd_funcs.sh b/setup/ehdd/ehdd_funcs.sh new file mode 100644 index 00000000..1276b142 --- /dev/null +++ b/setup/ehdd/ehdd_funcs.sh @@ -0,0 +1,20 @@ + +if [ -s /etc/mailinabox.conf ]; then + source /etc/mailinabox.conf + [ $? -eq 0 ] || exit 1 +else + STORAGE_ROOT="/home/${STORAGE_USER:-user-data}" +fi + +EHDD_IMG="$STORAGE_ROOT.HDD" +EHDD_MOUNTPOINT="$STORAGE_ROOT" +EHDD_LUKS_NAME="c1" + + +find_unused_loop() { + losetup -f +} + +find_inuse_loop() { + losetup -l | awk "\$6 == \"$EHDD_IMG\" { print \$1 }" +} diff --git a/setup/ehdd/mount.sh b/setup/ehdd/mount.sh index 2c9bd90f..58c4d97c 100755 --- a/setup/ehdd/mount.sh +++ b/setup/ehdd/mount.sh @@ -1,31 +1,31 @@ #!/bin/bash -hdd="$(setup/ehdd/create_hdd.sh -location)" -mountpoint="$(setup/ehdd/create_hdd.sh -mountpoint)" +. "setup/ehdd/ehdd_funcs.sh" || exit 1 -if [ ! -e "$hdd" ]; then - echo "NOTE: ecrypted HDD not found at $hdd, not mounting" +if [ ! -e "$EHDD_IMG" ]; then + echo "Warning: ecrypted HDD not found at $EHDD_IMG, not mounting" exit 0 fi -if mount | grep "^/dev/mapper/c1 on $mountpoint" >/dev/null; then - echo "$hdd already mounted" +if mount | grep "^/dev/mapper/$EHDD_LUKS_NAME on $EHDD_MOUNTPOINT" >/dev/null; then + echo "$EHDD_IMG already mounted" exit 0 fi -losetup /dev/loop0 "$hdd" || exit 1 -# map device to /dev/mapper/c1 -cryptsetup luksOpen /dev/loop0 c1 +loop=$(find_unused_loop) +losetup $loop "$EHDD_IMG" || exit 1 +# map device to /dev/mapper/NAME +cryptsetup luksOpen $loop $EHDD_LUKS_NAME code=$? if [ $code -ne 0 ]; then - echo "luksOpen failed ($code) - is $hdd luks formatted?" - losetup -d /dev/loop0 + echo "luksOpen failed ($code) - is $EHDD_IMG luks formatted?" + losetup -d $loop exit 1 fi -if [ ! -e "$mountpoint" ]; then - echo "Creating mount point directory: $mountpoint" - mkdir -p "$mountpoint" || exit 1 +if [ ! -e "$EHDD_MOUNTPOINT" ]; then + echo "Creating mount point directory: $EHDD_MOUNTPOINT" + mkdir -p "$EHDD_MOUNTPOINT" || exit 1 fi -mount /dev/mapper/c1 "$mountpoint" || exit 1 -echo "Success: mounted $mountpoint" +mount /dev/mapper/$EHDD_LUKS_NAME "$EHDD_MOUNTPOINT" || exit 1 +echo "Success: mounted $EHDD_MOUNTPOINT" diff --git a/setup/ehdd/postinstall.sh b/setup/ehdd/postinstall.sh index b5fe3b45..f2d327d4 100755 --- a/setup/ehdd/postinstall.sh +++ b/setup/ehdd/postinstall.sh @@ -1,6 +1,7 @@ #!/bin/bash -EHDD_IMG="$(setup/ehdd/create_hdd.sh -location)" +. "setup/ehdd/ehdd_funcs.sh" || exit 1 + if [ -e "$EHDD_IMG" ]; then if [ -s /etc/mailinabox.conf ]; then diff --git a/setup/ehdd/umount.sh b/setup/ehdd/umount.sh index 2a6ea3ef..6e5e5efc 100755 --- a/setup/ehdd/umount.sh +++ b/setup/ehdd/umount.sh @@ -1,11 +1,11 @@ #!/bin/bash -mountpoint="$(setup/ehdd/create_hdd.sh -mountpoint)" +. "setup/ehdd/ehdd_funcs.sh" || exit 1 -if ! mount | grep "$mountpoint" >/dev/null; then +if ! mount | grep "$EHDD_MOUNTPOINT" >/dev/null; then # not mounted exit 0 fi -umount "$mountpoint" || exit 1 -cryptsetup luksClose c1 -losetup -d /dev/loop0 +umount "$EHDD_MOUNTPOINT" || exit 1 +cryptsetup luksClose $EHDD_LUKS_NAME +losetup -d $(find_inuse_loop)