Skip to content

Instantly share code, notes, and snippets.

@Nicocchi
Last active August 24, 2022 11:07
Show Gist options
  • Save Nicocchi/063a13e4f53c0eb3249be88c414a346b to your computer and use it in GitHub Desktop.
Save Nicocchi/063a13e4f53c0eb3249be88c414a346b to your computer and use it in GitHub Desktop.
A simple shell to mount, unmount and view status of LUKS disk encryption based parition under Linux.
#!/bin/bash
#
# A simple shell to mount, unmount and view status of disk encryption based
# parition under Linux.
# Tested on Debian, Fedora, and Arch base desktops.
# ----------------------------------------------------------------------------
# Author: Nicocchi
# (c) 2019 Jeremy Boggs under GNU GPL v2.0+
# ----------------------------------------------------------------------------
# Last updated: 01/20/2019
# ----------------------------------------------------------------------------
echo " "
### commands ###
_crypt="/sbin/cryptsetup"
_vg="/sbin/vgscan"
_vgc="/sbin/vgchange"
_mnt="/bin/mount"
### Partition specific settings ###
# Modify the amount of partitions, names and device locations to fit your needs.
# _name = name of the partition
# _mntsA = location for the mapper. This will be created if does not exist.
# _mntdA = location where you want to mount the partition. Does not create
# if does not exist
# ----------------------------------------------------------------------------
### Partition One ###
_deviceA="/dev/sdd"
_nameA="games"
_mntsA="/dev/mapper/games "
_mntdA="/mnt/Games/"
### Partition Two ###
_deviceB="/dev/sdb"
_nameB="library"
_mntsB="/dev/mapper/library "
_mntdB="/mnt/Library/"
### Partition Three ###
_deviceC="/dev/sdc"
_nameC="development"
_mntsC="/dev/mapper/development "
_mntdC="/mnt/Development"
# ----------------------------------------------------------------------------
### Check user arguments ###
# ----------------------------------------------------------------------------
### Default argument ###
if [[ ! $1 ]]; then
echo " "
echo "This script unmounts and mounts LUKS partitions enscribed in the script."
echo "-Author: Nicocchi (aka Jeremy Boggs)"
echo " "
echo "lukdevice options:"
echo " -u | unmount all devices"
echo " -m | mount all devices"
echo " -h | help"
echo " -s | check device status"
echo " "
echo "Type ./luksdevice -h for more information about luksdevice options"
echo "Example: ./luksdevice -u to unmount all devices"
echo " "
### Unmount argument ###
elif [ "$1" = "-u" ]; then
echo " "
echo "Unmounting devices..."
echo " "
sleep 1
### Partition One ###
if grep -qs '/mnt/Games ' /proc/mounts; then
{
umount ${_mntdA}
${_crypt} luksClose ${_mntsA}
echo "Unmount of" ${_deviceA} "successful"
} || {
echo "Unmounting of" ${_deviceA} "failed."
}
else
echo ${_deviceA} "is already unmounted"
fi
### Partition Two ###
if grep -qs '/mnt/Library ' /proc/mounts; then
{
umount ${_mntdB}
${_crypt} luksClose ${_mntsB}
echo "Unmount of" ${_deviceB} "successful"
} || {
echo "Unmounting of" ${_deviceB} "failed."
}
else
echo ${_deviceB} "is already unmounted"
fi
### Partition Three ###
if grep -qs '/mnt/Development ' /proc/mounts; then
{
umount ${_mntdC}
${_crypt} luksClose ${_mntsC}
echo "Unmount of" ${_deviceC} "successful"
} || {
echo "Unmounting of" ${_deviceC} "failed."
}
else
echo ${_deviceC} "is already unmounted"
fi
### Feedback ###
if grep -qs '/mnt/Games ' /proc/mounts && grep -qs '/mnt/Library ' /proc/mounts && grep -qs '/mnt/Development ' /proc/mounts; then
echo " "
echo "All devices are mounted"
else
echo " "
echo "All devices are unmounted"
fi
### Mount argument ###
elif [ "$1" = "-m" ]; then
echo " "
echo "Mounting devices..."
echo " "
sleep 1
### Partition One ###
if grep -qs '/mnt/Games ' /proc/mounts; then
echo ${_deviceA} "is already mounted."
else
{
echo
${_crypt} luksOpen ${_deviceA} $_nameA
echo
${_vg} --mknodes
${_vgc} -ay
echo
${_mnt} ${_mntsA} ${_mntdA}
echo ${_deviceA} "successfully mounted to" ${_mntdA}
} || {
echo "Failed to decrypt/mount" ${_deviceA}
}
fi
### Partition One ###
if grep -qs '/mnt/Library ' /proc/mounts; then
echo ${_deviceB} "is already mounted."
else
{
echo
${_crypt} luksOpen ${_deviceB} $_nameB
echo
${_vg} --mknodes
${_vgc} -ay
echo
${_mnt} ${_mntsB} ${_mntdB}
echo ${_deviceB} "successfully mounted to" ${_mntdB}
} || {
echo "Failed to decrypt/mount" ${_deviceB}
}
fi
### Partition One ###
if grep -qs '/mnt/Development ' /proc/mounts; then
echo ${_deviceC} "is already mounted."
else
{
echo
${_crypt} luksOpen ${_deviceC} $_nameC
echo
${_vg} --mknodes
${_vgc} -ay
echo
${_mnt} ${_mntsC} ${_mntdC}
echo ${_deviceC} "successfully mounted to" ${_mntdC}
} || {
echo "Failed to decrypt/mount" ${_deviceC}
}
fi
### Feedback ###
if grep -qs '/mnt/Games ' /proc/mounts && grep -qs '/mnt/Library ' /proc/mounts && grep -qs '/mnt/Development ' /proc/mounts; then
echo " "
echo "All devices are mounted"
else
echo " "
echo "All devices are unmounted"
fi
### Help argument ###
elif [ "$1" = "-h" ]; then
echo " "
echo "This script unmounts and mounts LUKS partitions enscribed in the script."
echo " "
echo "-Author: Nicocchi (aka Jeremy Boggs)"
echo "lukdevice options:"
echo " -u | unmount all devices"
echo " -m | mount all devices"
echo " -h | help"
echo " -s | check device status"
echo " "
echo "Type luksdevice -h for more information about luksdevice options"
echo "Example: ./luksdevice -u to unmount all devices"
echo " "
### Status argument ###
elif [ "$1" = "-s" ]; then
echo " "
echo "Checking status of devices..."
echo " "
sleep 1
### Partition One ###
if grep -qs '/mnt/Games ' /proc/mounts; then
echo ${_deviceA} "is mounted to" ${_mntdA}
else
echo ${_deviceA} "is not mounted."
fi
### Partition Two ###
if grep -qs '/mnt/Library ' /proc/mounts; then
echo ${_deviceB} "is mounted to" ${_mntdB}
else
echo ${_deviceB} "is not mounted."
fi
### Partition Three ###
if grep -qs '/mnt/Development ' /proc/mounts; then
echo ${_deviceC} "is mounted to" ${_mntdC}
else
echo ${_deviceC} "is not mounted."
fi
echo " "
# If wrong parameter was specific, spout error
else
echo "Unknown parameter"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment