utils/elm-mgmt.sh
Irrlicht b40cee0c85 [elm-mgmt] Improve logging messages, special error code
- Include more information in log messages and use more constistently
  the work "device" instead of "disk".
- Return error code 5 for the impossible case of an undefined mode
  process by the script. An undefined mode is supposed to be identified
  at the option parsing pass.
2025-09-15 20:48:52 +02:00

194 lines
4.9 KiB
Bash
Executable file

#!/sbin/bash
# Steamline use of backup external hardrive.
#
# The backup hard drive is assumed to encrypted using cryptsetup luks mode.
# The script is used to issue the desired command on mount and umount.
# Two modes are available : mount and umount
#
# Mount mode :
# Issue the crypt setup command and then mount the then created mapper. Default behavior is to assume a fstab entry exists and mount behaves correctly. If the mountpoint in fstab doesn't exists, it is created
#
# Umount mode:
# Umounts the mapper, closes the cryptsetup device and then
# Mount is used to issue the cryptsetup
# This script is meant to be run as root
# Script fails if any command fails
set -e
DEVICE=
UMOUNTMODE=0
MOUNTMODE=0
print_help() {
echo "elm-mgmt [OPTIONS…]"
echo ""
echo "Script managing Elm, an external hard drive for backups. The device is assumed to be a LUKS encrypted device."
echo ""
echo "You need to specify the disk and partition to be opened and mounted. This can be achieve via command line option -i and -p or thourgh environment variables:"
echo " - BACKUP_DEVICE_UUID, the UUID of the disk that will be open with cryptsetup, assumed to be a LUKS device."
echo " - BACKUP_PART_UUID, the UUID of the partition to be mounted of the device."
echo ""
echo "-c, --cryptname NAME Call the mapped device NAME when opening the LUKS partition."
echo "-d, --device DEVICE Path DEVICE to the device where reside the LUKS partition."
echo "-h, help Print this help message."
echo "-m, --mount Mount mode: open the LUKS device and mount the partition. This is the default behavior."
echo "-u, --umount Umount mode: un-mounts the partition, close the LUKS device and instruct the disk to spin down using hdparm."
echo ""
echo "EXIT CODES"
echo "0 Successfully made the requested operation."
echo "1 An error occured."
echo "2 Impossible to find the device."
echo "124 Test exit code."
}
while [ $# -gt 0 ]
do
case $1 in
-d|--device)
DEVICE="$2"
shift # past argument
if [ $# -eq 0 ]; then
echo "You must specify a device"
exit 1
fi
shift # past value
;;
-h|--help)
print_help
exit 0
;;
-i|--disk-uuid)
shift # pass argument
BACKUP_DEVICE_UUID="$1"
shift # pass value
;;
-p|--part-uuid)
shift # pass argument
BACKUP_PART_UUID="$1"
shift # pass value
;;
-m|--mount)
MOUNTMODE=1
shift # pass argument
;;
-u|--umount)
UMOUNTMODE=1
shift # pass argument
;;
-c|cryptname)
CRYPTNAME="$2"
shift # pass argument
if [ $# -eq 0 ]; then
echo "You must specify the name of the crypt mapping"
exit 1
fi
shift # pass value
;;
-*|--*)
echo "Unknown option $1"
exit 1
;;
*)
echo "Extra useless argument: $1"
exit 1
;;
esac
done
if [ -z "$BACKUP_DEVICE_UUID" ]
then
echo "No backup disk UUID specified, stopping"
print_help
exit 31
fi
if [ -z "$BACKUP_PART_UUID" ]
then
echo "No backup part UUID specified, stopping"
print_help
exit 32
fi
BACKUP_DEVICE_PATH="/dev/disk/by-uuid/$BACKUP_DEVICE_UUID"
MAPPER_PATH="/dev/disk/by-uuid/$BACKUP_PART_UUID"
if [ -z "$CRYPTNAME" ]
then
CRYPTNAME="crypt-$BACKUP_DEVICE_UUID"
fi
if [ "$MOUNTMODE" -eq 0 ] && [ "$UMOUNTMODE" -eq 0 ]
then
MOUNTMODE=1
fi
if [ -z "$DEVICE" ]
then
DEVICE_NAME=$(lsblk -o UUID,NAME | awk -v uuid="$BACKUP_DEVICE_UUID" '$1 == uuid {print $2}')
DEVICE="/dev/$DEVICE_NAME"
if [ -z "$DEVICE" ]
then
echo "Couldn't find device with UUID: $BACKUP_DEVICE_UUID"
exit 1
fi
else
LUKS_UUID=$(lsblk -Adno UUID /dev/sda)
echo "Found UUID: $LUKS_UUID"
exit 124
fi
if [ ! -b "$DEVICE" ]
then
echo "Device doesn't exists or isn't a block device: $DEVICE"
exit 0
fi
if [ "$MOUNTMODE" -eq 1 ]
then
echo "Mount mode…"
# To be conservative and to less tests, start by checking if the device is already mounted
MOUNT_POINT=$(lsblk -o UUID,MOUNTPOINT | awk -v uuid="$BACKUP_PART_UUID" '$1 == uuid {print $2}')
if [ -n "$MOUNT_POINT" ]
then
echo "device opened and mapper mounted, nothing to do, exiting"
exit 0
fi
IS_LUKS=$(cryptsetup isLuks "$BACKUP_DEVICE_PATH")
if [ ! IS_LUKS ]
then
echo "Device $LUKS_UUID is not a LUKS device"
exit 1
else
echo "Device $LUKS_UUID is a luks device"
fi
# Check that Elm is available
if [ ! -b "$BACKUP_DEVICE_PATH" ]
then
echo "Error: couldn't find disk $BACKUP_DEVICE_UUID"
echo "Maybe backup disk isn't appear to be plugged in"
exit 1
else
echo "Backup device found"
fi
# Check if the luks device is already opened
if [ -b "/dev/disk/by-uuid/$BACKUP_PART_UUID" ]
then
echo "LUKS device is already opened, continue"
else
cryptsetup open --type luks "$BACKUP_DEVICE_PATH" "$CRYPTNAME"
echo "Device opened successfully"
fi
mount -m "$MAPPER_PATH"
exit 0
elif [ "$UMOUNTMODE" -eq 1 ]
then
echo "Umount mode"
umount "/dev/mapper/$CRYPTNAME"
cryptsetup close "$CRYPTNAME"
hdparm -y "$DEVICE"
exit 0
else
echo "Unknown mode, this shouldn't happen"
exit 5
fi