dotfiles/dotlocal/bin/dmount

104 lines
2.4 KiB
Text
Raw Permalink Normal View History

2024-07-20 22:10:54 +02:00
#!/usr/bin/bash
set -e
Version="1.1.1"
Usage="dmount PARAMETER
Script which permit to mount/unmount an LVM on LUKS using dmcrypt, doas and fstab.
Actually works only on cryptdata, my extern SSD.
PARAMETRES
-h, --help Print this help and exit
-v, --version Print the version and exit
-m, --mount Mount cryptdata
-u, --umount Unmount cryptdata
"
Mountpoint_cours=/home/primardj/cours
Mountpoint_doc=/home/primardj/Documents
Mountpoint_virt_machine=/home/primardj/virtual_machine
Mountpoint_data=/home/primardj/data
force_remove_vgdevice() {
echo "force remove vg cryptdata"
#doas dmsetup info --columns
doas dmsetup remove /dev/mapper/cryptdata-*
doas dmsetup remove /dev/mapper/cryptdata
}
mount_lvm_on_luks() {
echo "unlocking device cryptdata"
#doas rc-service dmcrypt restart
doas /etc/rc/sysinit/35-cryptsetup start
#doas vgchange -a y cryptdata
doas mount -a
}
umount_devices() {
echo "umount devices"
doas umount -R $Mountpoint_cours || lsof +D $Mountpoint_cours
doas umount -R $Mountpoint_doc || lsof +D $Mountpoint_doc
doas umount -R $Mountpoint_virt_machine || lsof +D $Mountpoint_virt_machine
doas umount -R $Mountpoint_data || lsof +D $Mountpoint_data
}
error_umount() {
echo "Error, can't umount all devices, need some manual help." >&2
lsblk
exit 1
}
error_deactivate_vg() {
echo "Error, impossible de deactive vg." >&2
echo "Try to force remove vgdisk from memory." >&2
if force_remove_vgdevice
then
echo "could at list remove vgdevice."
exit 0
else
lsblk
doas dmsetup info --columns
echo "couldn't remove vgdevice. need manual intervention." >&2
exit 1
fi
}
umount_lvm_on_luks() {
echo "locking device cryptdata"
umount_devices || error_umount
doas lvchange -a n cryptdata || error_deactivate_vg
doas cryptsetup close --deferred cryptdata
}
for i in "$@"; do
case "$i" in
"-h" | "--help")
echo "${Usage}"
exit 0
;;
"-v" | "--version")
echo "dmount v.${Version}"
exit 0
;;
"-m" | "--mount")
mount_lvm_on_luks
exit 0
;;
"-u" | "--umount")
umount_lvm_on_luks
exit 0
;;
*)
echo "Error, uknown args $i" >&2
exit 1
;;
esac
done