1
0
mirror of https://github.com/mail-in-a-box/mailinabox.git synced 2025-04-05 00:27:25 +00:00

Avoid loop device naming conflicts with snaps by optaining an available name from the system instead of using a static device name ("/dev/loop0").

This commit is contained in:
downtownallday 2020-06-02 09:22:24 -04:00
parent 66c9401254
commit 8d847ae9a9
5 changed files with 57 additions and 41 deletions

View File

@ -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 <size-in-gb>"
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

20
setup/ehdd/ehdd_funcs.sh Normal file
View File

@ -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 }"
}

View File

@ -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"

View File

@ -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

View File

@ -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)