-
-
Save radupotop/8d6e6d300fb6d41f619dc71bd4f4316c to your computer and use it in GitHub Desktop.
Mount your encrypted LUKS drives by uuid over SSH
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 | |
# rluks.sh: Mount your encrypted LUKS drives by uuid over SSH | |
# Copyright (C) 2016+ James Shubin, AGPLv3+ | |
# Written by James Shubin <[email protected]> | |
# You probably want to modify the following globals to match your needs... | |
SERVER='server.example.com' # expected server for running script | |
HOSTNAME='myserver' # expected hostname for running locally | |
MEDIA='/media/' # mount/media directory, eg: /media/ | |
declare -A MAP # create an associative array | |
MAP[music]='01234567-89ab-cdef-0123-456789abcdef' | |
MAP[files]='12345678-9abc-def0-1234-56789abcdef0' | |
MAP[movies]='23456789-abcd-ef01-2345-6789abcdef01' | |
if [ `hostname` != "$HOSTNAME" ]; then | |
#echo "connecting to: $SERVER via ssh" | |
ssh -t "$SERVER" "$(< $0)" # magic! | |
exit $? | |
fi | |
echo "Running on: `hostname`..." | |
sudo -v || exit 1 # warm sudo | |
read -t 42 -p "Mount/Unmount [m/u] ? " action | |
if [ "$action" != "m" ] && [ "$action" != "u" ]; then | |
echo 'Invalid action!' | |
exit 1 | |
fi | |
if [ "$action" = "u" ]; then | |
echo "Unmounting..." | |
else | |
echo "Mounting..." | |
fi | |
for K in "${!MAP[@]}"; do | |
V=${MAP[$K]} | |
#echo $K --- $V | |
if [ "$action" = "u" ]; then # unmount | |
if findmnt --output 'SOURCE,TARGET' --target "${MEDIA}$K" &>/dev/null; then | |
sudo umount "${MEDIA}$K" || exit 1 | |
echo "$K: umount ✓" | |
fi | |
if [ -L "/dev/mapper/$K" ]; then | |
sudo cryptsetup luksClose "$K" || exit 1 | |
echo "$K: luksClose ✓" | |
fi | |
if [ -d "${MEDIA}$K" ]; then | |
sudo rmdir "${MEDIA}$K" || exit 1 | |
echo "$K: rmdir ✓" | |
fi | |
else # mount | |
# is dir missing | |
if [ ! -d "${MEDIA}$K" ]; then | |
sudo mkdir "${MEDIA}$K" || exit 1 | |
echo "$K: mkdir ✓" | |
fi | |
# is luks already open? | |
if [ ! -L "/dev/mapper/$K" ]; then | |
fail=0 | |
# get password | |
if [ -z "$lukspassword" ]; then | |
read -s -p "LUKS Password: " lukspassword | |
echo | |
fi | |
# open luks | |
(echo "$lukspassword" | sudo cryptsetup luksOpen "/dev/disk/by-uuid/$V" "$K") || fail=1 | |
if [ "$fail" = "1" ]; then | |
read -s -p "LUKS Password: " lukspassword | |
echo | |
(echo "$lukspassword" | sudo cryptsetup luksOpen "/dev/disk/by-uuid/$V" "$K") || exit 1 | |
fi | |
fail=0 | |
echo "$K: luksOpen ✓" | |
fi | |
# are we already mounted ? | |
if ( ! findmnt --output 'SOURCE,TARGET' --source "/dev/mapper/$K" &>/dev/null ) && ( ! findmnt --output 'SOURCE,TARGET' --target "${MEDIA}$K" &>/dev/null ); then | |
# mount | |
sudo mount "/dev/mapper/$K" "${MEDIA}$K" || exit 1 | |
echo "$K: mount ✓" | |
fi | |
fi | |
done | |
echo 'Done!' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment