Hi.
There’s little bash script to create/open LUKS2 encrypted disk image files.
It operates in user directory, so it need to be saved in /home/user/bin/ - if not existent then create it.
It creates/open image files from /home/user/.vdi/ and mount them in /home/user/Desktop/.
Every needed directories are created.
User needs to type password for LUKS2 - it need to be minimum 8 chars and it can’t be 12345678 or similar easy passwords because LUKS2 will exit with error.
For usage, run it without any options or with -h or --help option.
#!/bin/bash
set -e
# initialize variables
HELP=
DEBUG=
OPEN=
PATH2IMG="/home/user/.vdi/"
PATH2MNT="/home/user/Desktop/"
FILE=".data.img"
SIZE="1G"
CHOSENFS="btrfs"
USAGE="Usage: mntimage [file-name] [file-size]
-h|--help - this help
-d|--debug - it will show chosen parameters without doing anything
-o|--open - open/create LUKS2 image file
-b|--btrfs - format created file in BTRFS (default)
-e|--ext4 - format created file in EXT4
-x|--xfs - firmat created file in XFS
-f|--file <file-name> - name of the file to be mounted/created, (def:.data.img)
-s|--size <file-size> - size of file to create, (def: 1G)
Hardcoded paths:
path to disk images: $PATH2IMG
path to mount disks: $PATH2MNT
"
# set options for getopt
OPTIONS=$(getopt -a -o hdobexf:s: -l help,debug,open,btrfs,ext4,xfs,file:,size: -- "$@")
if [ $? -ne 0 ]; then
echo "$USAGE"
exit 1
fi
eval set -- "$OPTIONS"
while true; do
case "$1" in
-h|--help) HELP=true; shift; break;;
-d|--debug) DEBUG=true; shift;;
-o|--open) OPEN=true; shift;;
-b|--btrfs) CHOSENFS="btrfs"; shift;;
-e|--ext4) CHOSENFS="ext4"; shift;;
-x|--xfs) CHOSENFS="xfs"; shift;;
-f|--file) FILE="$2"; shift 2;;
-s|--size) SIZE="$2"; shift 2;;
--) shift; break;;
*) echo "$USAGE";;
esac
done
if [[ $DEBUG ]]; then
echo "
File to open/create: $PATH2IMG$FILE
Size of the file to create: $SIZE
Path to mounted file: $PATH2MNT${FILE%\.img}
Chosen filesystem: $CHOSENFS
"
exit 1
fi
if [[ ! $OPEN ]]; then HELP=true; fi
if [[ $HELP ]]; then
echo "$USAGE"
exit 1
fi
sleep 1
if [ ! -d $PATH2IMG ]; then mkdir $PATH2IMG; fi
if [ ! -f $PATH2IMG$FILE ]; then
fallocate -l $SIZE $PATH2IMG$FILE
echo
echo "Creating LUKS2 container - chose minimum 8 characters password (if invalid then it will fail)"
sudo cryptsetup -y luksFormat $PATH2IMG$FILE
echo
echo "Openning LUKS2 container - provide chosen password"
sudo cryptsetup luksOpen $PATH2IMG$FILE "enc${FILE%\.img}"
echo
echo "Making filesystem on LUKS2 device"
sudo mkfs.${CHOSENFS} "/dev/mapper/enc${FILE%\.img}"
echo
echo "Mounting filesystem in $PATH2MNT${FILE%\.img}"
if [ ! -d "$PATH2MNT${FILE%\.img}" ]; then mkdir "$PATH2MNT${FILE%\.img}"; fi
sudo mount "/dev/mapper/enc${FILE%\.img}" "$PATH2MNT${FILE%\.img}"
sudo chown -R user:user "$PATH2MNT${FILE%\.img}"
echo
echo "Done"
echo
exit 1
elif [ ! -e "/dev/mapper/enc${FILE%\.img}" ]; then
echo
echo "Openning LUKS2 container - provide chosen password"
sudo cryptsetup luksOpen $PATH2IMG$FILE "enc${FILE%\.img}"
echo
echo "Mounting filesystem in $PATH2MNT${FILE%\.img}"
if [ ! -d "$PATH2MNT${FILE%\.img}" ]; then mkdir "$PATH2MNT${FILE%\.img}"; fi
sudo mount "/dev/mapper/enc${FILE%\.img}" "$PATH2MNT${FILE%\.img}"
sudo chown -R user:user "$PATH2MNT${FILE%\.img}"
echo
echo "Done"
echo
exit 1
fi
if mount |grep "$PATH2MNT${FILE%\.img}" 1>/dev/null; then
echo
echo "Unmounting filesystem"
sudo umount "$PATH2MNT${FILE%\.img}"
echo
echo "Closing LUKS2 container"
sudo cryptsetup luksClose "enc${FILE%\.img}"
echo
echo "Removing mount directory $PATH2MNT${FILE%\.img}"
rm -R "$PATH2MNT${FILE%\.img}"
echo
echo "Done"
echo
exit 1
else
if [ -e "/dev/mapper/enc${FILE%\.img}" ]; then
echo
echo "IMG file not mounted but LUKS2 container opened"
echo "Closing LUKS2 container"
sudo cryptsetup luksClose "enc${FILE%\.img}"
if [ ! -d "$PATH2MNT${FILE%\.img}" ]; then
echo
echo "Removing mount directory $PATH2MNT${FILE%\.img}"
rm -R "$PATH2MNT${FILE%\.img}"
fi
else
echo "There's nothing here"
fi
fi