scripts/encryption/close_container.sh

78 lines
2.2 KiB
Bash
Raw Normal View History

2020-03-01 13:02:34 +01:00
#!/bin/bash
# script to close a LUKS encrypted container file (use open_container.sh to open)
# -must be run with root privileges
# -start it without any or with 3 arguments: ./open_container.sh /path/to/container mapper_filename /path/to/mount_point
# written by Christian Ulrich (based on a script from http://wiki.ubuntuusers.de/LUKS/Containerdatei)
# version: 0.2
# date: 2011/09/13
run=1
choice=
if test $# -ne 0; then
if test $# -ne 3; then
echo "Aufruf des Skripts entweder ohne Argumante oder: ./close_container /Pfad/zum/Container mapper_filename /Pfad/zum/Mountpoint"
exit 1
else
SAFE=$1
CRYPTNAME=$2
MNT=$3
fi
else
SAFE=/home/christian/privat/encrypted
CRYPTNAME=container
MNT=/home/christian/mnt
fi
LOOPDEV=`losetup -a | grep "$SAFE" | sed "s/: .*//"`
echo "close_container.sh v0.2 (by Christian Ulrich)
---------------------------------------------"
while [ $run -ne 0 ]; do
echo
echo "Bitte wählen:"
echo "-------------"
echo "[1] Pfad zum Container ändern ($SAFE)"
echo "[2] Dateiname in /dev/mapper/ ändern ($CRYPTNAME)"
echo "[3] Mount-Pfad ändern ($MNT)"
echo "---------------------------------------------------"
echo "[c] Container schließen"
echo "[q] Script beenden"
read choice
case $choice in
1 ) echo "Neuen Pfad eingeben oder <return> um abzubrechen (aktueller Pfad: $SAFE):"
SAFE_tmp=
read SAFE_tmp
if [[ -n "$SAFE_tmp" ]]; then #if only <return> was pressed
SAFE=$SAFE_tmp
fi
;;
2 ) echo "Neuen Dateinamen eingeben oder <return> um abzubrechen (aktueller Dateiname: $CRYPTNAME):"
CRYPTNAME_tmp=
read CRYPTNAME_tmp
if [[ -n "$CRYPTNAME_tmp" ]]; then
CRYPTNAME=$CRYPTNAME_tmp
fi
;;
3 ) echo "Neuen Mount-Pfad eingeben oder <return> um abzubrechen (aktueller Pfad: $MNT):"
MNT_tmp=
read MNT_tmp
if [[ -n "$MNT_tmp" ]]; then
MNT=$MNT_tmp
fi
;;
'c' ) run=0
if [ "`losetup -a | grep -c "$SAFE"`" != "1" ]; then
echo "nicht eingehängt"
exit
fi
umount $MNT
cryptsetup luksClose $CRYPTNAME
losetup -d $LOOPDEV
echo "Container geschlossen"
;;
'q' ) run=0;;
* ) echo "Falsche Eingabe!";;
esac
done