93 lines
2.3 KiB
Bash
93 lines
2.3 KiB
Bash
#!/bin/bash
|
|
# mountc
|
|
#
|
|
# Mount luks-encrypted filesystems
|
|
#
|
|
# USAGE: mountc <partition> <mountpoint> [luks.key]
|
|
# EXAMPLE: mountc sdf1 mydrive
|
|
#
|
|
# Copyright © 2021 Keith Irwin (www.ki9.us)
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
# Check for root
|
|
if [[ $EUID -ne 0 ]]; then
|
|
echo "This script must be run as root"
|
|
exit 1
|
|
fi
|
|
|
|
# Get args
|
|
me="$(basename $0)"
|
|
if [[ $# -eq 3 ]]; then
|
|
part="$1"
|
|
mp="$2"
|
|
kf="$3"
|
|
elif [[ $# -eq 2 ]]; then
|
|
device="$1"
|
|
name="$2"
|
|
kf=""
|
|
else
|
|
echo "USAGE: $me <partition> <mountpoint> [keyfile]"
|
|
exit 2
|
|
fi
|
|
name="$(basename $mp)"
|
|
|
|
# Quit if given "no" at prompt
|
|
bail(){ [[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1; }
|
|
|
|
|
|
## RUNTIME ##
|
|
|
|
# Where keys are stored
|
|
KEYDIR="/root/luks"
|
|
|
|
# Initial output
|
|
echo " mountc Copyright © 2021 Keith Irwin (www.ki9.us)"
|
|
echo " This program comes with ABSOLUTELY NO WARRANTY; for details"
|
|
echo " see the LICENSE.md that came with it"
|
|
echo " This is free software, and you are welcome to redistribute it"
|
|
echo " under certain conditions"
|
|
echo
|
|
|
|
# Check that mountpoint is directory
|
|
if [ ! -d "$mp" ]; then
|
|
echo "$mp is not a directory"
|
|
exit 2
|
|
fi
|
|
# TODO: Check that mountpoint is not already mounted
|
|
# TODO: Check that mountpoint is empty
|
|
|
|
# Check if already decrypted
|
|
if [ -h "/dev/mapper/$name" ]; then
|
|
echo "/dev/mapper/$name already exists. Attempting to mount it..."
|
|
else
|
|
|
|
# Decrypt
|
|
if [ -f "$kf" ]; then
|
|
echo "Decrypting /dev/$part with $kf..."
|
|
cryptsetup luksOpen "/dev/$part" "$name" --key-file="$kf"
|
|
else
|
|
echo "Decrypting /dev/$part with password..."
|
|
cryptsetup luksOpen "/dev/$part" "$name"
|
|
fi
|
|
|
|
fi
|
|
|
|
# Mount decrypted filesystem
|
|
echo "Mounting /dev/mapper/$name filesystem to $mp..."
|
|
chmod 755 "$mp"
|
|
mount "/dev/mapper/$name" "$mp" && \
|
|
echo "DONE!"
|