mirror of
https://github.com/mail-in-a-box/mailinabox.git
synced 2025-04-06 00:37:06 +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:
parent
66c9401254
commit
8d847ae9a9
@ -1,28 +1,22 @@
|
|||||||
#!/bin/bash
|
#!/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"
|
source "setup/ehdd/ehdd_funcs.sh" || exit 1
|
||||||
EHDD_SIZE_GB="$1"
|
|
||||||
MOUNTPOINT="$STORAGE_ROOT"
|
|
||||||
|
|
||||||
if [ "$1" == "" ]; then
|
if [ "$1" == "" ]; then
|
||||||
echo "usage: $0 <size-in-gb>"
|
echo "usage: $0 <size-in-gb>"
|
||||||
echo -n " hdd image location: $EHDD_IMG"
|
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
|
exit 1
|
||||||
elif [ "$1" == "-location" ]; then
|
elif [ "$1" == "-location" ]; then
|
||||||
echo "$EHDD_IMG"
|
echo "$EHDD_IMG"
|
||||||
exit 0
|
exit 0
|
||||||
elif [ "$1" == "-mountpoint" ]; then
|
elif [ "$1" == "-mountpoint" ]; then
|
||||||
echo "$MOUNTPOINT"
|
echo "$EHDD_MOUNTPOINT"
|
||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
EHDD_SIZE_GB="$1"
|
||||||
|
|
||||||
|
|
||||||
if [ ! -e "$EHDD_IMG" ]; then
|
if [ ! -e "$EHDD_IMG" ]; then
|
||||||
echo "Creating ${EHDD_SIZE_GB}G encryped drive: $EHDD_IMG"
|
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
|
[ $count -eq 0 ] && echo "Invalid size" && exit 1
|
||||||
apt-get -q=2 -y install cryptsetup || exit 1
|
apt-get -q=2 -y install cryptsetup || exit 1
|
||||||
dd if=/dev/zero of="$EHDD_IMG" bs=1M count=$count || exit 1
|
dd if=/dev/zero of="$EHDD_IMG" bs=1M count=$count || exit 1
|
||||||
losetup /dev/loop0 "$EHDD_IMG" || exit 1
|
loop=$(find_unused_loop)
|
||||||
if ! cryptsetup luksFormat -i 15000 /dev/loop0; then
|
losetup $loop "$EHDD_IMG" || exit 1
|
||||||
losetup -d /dev/loop0
|
if ! cryptsetup luksFormat -i 15000 $loop; then
|
||||||
|
losetup -d $loop
|
||||||
rm -f "$EHDD_IMG"
|
rm -f "$EHDD_IMG"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
echo ""
|
echo ""
|
||||||
echo "NOTE: You will need to reenter your drive encryption password a number of times"
|
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
|
cryptsetup luksOpen $loop $EHDD_LUKS_NAME # map device to /dev/mapper/NAME
|
||||||
mke2fs -j /dev/mapper/c1
|
mke2fs -j /dev/mapper/$EHDD_LUKS_NAME
|
||||||
cryptsetup luksClose c1
|
cryptsetup luksClose $EHDD_LUKS_NAME
|
||||||
losetup -d /dev/loop0
|
losetup -d $loop
|
||||||
else
|
else
|
||||||
echo "$EHDD_IMG already exists..."
|
echo "ERROR: $EHDD_IMG already exists!"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
20
setup/ehdd/ehdd_funcs.sh
Normal file
20
setup/ehdd/ehdd_funcs.sh
Normal 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 }"
|
||||||
|
}
|
@ -1,31 +1,31 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
hdd="$(setup/ehdd/create_hdd.sh -location)"
|
. "setup/ehdd/ehdd_funcs.sh" || exit 1
|
||||||
mountpoint="$(setup/ehdd/create_hdd.sh -mountpoint)"
|
|
||||||
|
|
||||||
if [ ! -e "$hdd" ]; then
|
if [ ! -e "$EHDD_IMG" ]; then
|
||||||
echo "NOTE: ecrypted HDD not found at $hdd, not mounting"
|
echo "Warning: ecrypted HDD not found at $EHDD_IMG, not mounting"
|
||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if mount | grep "^/dev/mapper/c1 on $mountpoint" >/dev/null; then
|
if mount | grep "^/dev/mapper/$EHDD_LUKS_NAME on $EHDD_MOUNTPOINT" >/dev/null; then
|
||||||
echo "$hdd already mounted"
|
echo "$EHDD_IMG already mounted"
|
||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
losetup /dev/loop0 "$hdd" || exit 1
|
loop=$(find_unused_loop)
|
||||||
# map device to /dev/mapper/c1
|
losetup $loop "$EHDD_IMG" || exit 1
|
||||||
cryptsetup luksOpen /dev/loop0 c1
|
# map device to /dev/mapper/NAME
|
||||||
|
cryptsetup luksOpen $loop $EHDD_LUKS_NAME
|
||||||
code=$?
|
code=$?
|
||||||
if [ $code -ne 0 ]; then
|
if [ $code -ne 0 ]; then
|
||||||
echo "luksOpen failed ($code) - is $hdd luks formatted?"
|
echo "luksOpen failed ($code) - is $EHDD_IMG luks formatted?"
|
||||||
losetup -d /dev/loop0
|
losetup -d $loop
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ ! -e "$mountpoint" ]; then
|
if [ ! -e "$EHDD_MOUNTPOINT" ]; then
|
||||||
echo "Creating mount point directory: $mountpoint"
|
echo "Creating mount point directory: $EHDD_MOUNTPOINT"
|
||||||
mkdir -p "$mountpoint" || exit 1
|
mkdir -p "$EHDD_MOUNTPOINT" || exit 1
|
||||||
fi
|
fi
|
||||||
mount /dev/mapper/c1 "$mountpoint" || exit 1
|
mount /dev/mapper/$EHDD_LUKS_NAME "$EHDD_MOUNTPOINT" || exit 1
|
||||||
echo "Success: mounted $mountpoint"
|
echo "Success: mounted $EHDD_MOUNTPOINT"
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
EHDD_IMG="$(setup/ehdd/create_hdd.sh -location)"
|
. "setup/ehdd/ehdd_funcs.sh" || exit 1
|
||||||
|
|
||||||
if [ -e "$EHDD_IMG" ]; then
|
if [ -e "$EHDD_IMG" ]; then
|
||||||
|
|
||||||
if [ -s /etc/mailinabox.conf ]; then
|
if [ -s /etc/mailinabox.conf ]; then
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
#!/bin/bash
|
#!/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
|
# not mounted
|
||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
umount "$mountpoint" || exit 1
|
umount "$EHDD_MOUNTPOINT" || exit 1
|
||||||
cryptsetup luksClose c1
|
cryptsetup luksClose $EHDD_LUKS_NAME
|
||||||
losetup -d /dev/loop0
|
losetup -d $(find_inuse_loop)
|
||||||
|
Loading…
Reference in New Issue
Block a user