Created
September 11, 2022 19:46
-
-
Save lemon-sh/a6b056a73f86f68f427f6d0001df500e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
MOUNTPOINT="/media/crypt" | |
CRYPTNAME="cryptroot" | |
set -euo pipefail | |
[[ $(id -u) -ne 0 ]] && { | |
echo "This script must be run as root" | |
exit 1 | |
} | |
mounted=$(findmnt $MOUNTPOINT -oSOURCE | tail -n1) && { | |
if device=$(cryptsetup status "$mounted" 2>/dev/null | grep device | xargs | cut -d' ' -f2) ; then | |
echo -n "Encrypted device $device is mounted on $MOUNTPOINT. Close and umount? [y/n] " | |
read answer | |
if [ "$answer" = "y" ]; then | |
umount "$MOUNTPOINT" | |
cryptsetup close "$mounted" | |
else | |
exit 0 | |
fi | |
else | |
echo "Non-LUKS device $mounted is mounted on $MOUNTPOINT. Unmount it manually and try again." | |
exit 1 | |
fi | |
} | |
[[ $# -ne 1 ]] && exit 0 | |
cryptsetup status "$CRYPTNAME" &>/dev/null && { | |
echo -n "Cryptsetup device '$CRYPTNAME' exists. Close and unmount? [y/n] " | |
read answer | |
if [ "$answer" = "y" ]; then | |
mpoint=$(findmnt /dev/mapper/cryptroot -oTARGET | tail -n1) && umount $mpoint | |
cryptsetup close "$CRYPTNAME" | |
else | |
exit 1 | |
fi | |
} | |
cryptsetup open "$1" "$CRYPTNAME" | |
mount "/dev/mapper/$CRYPTNAME" "$MOUNTPOINT" | |
echo "Done!" | |
# vim:ft=sh |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment