I have an external hard drive which contains different GPT partitions. One of them is a LVM partition. This partition is contained in a LVM volume group (called "volga"), which in turns splits into many different logical volumes, one of them is "data" and is an encrypted partition.
Whenever I plug this external hard drive, I would like my OS to:
- enable the logical volume "data"
- use cryptsetup to create an unencrypted data node in
/dev/mapper/data
using a key file stored in/somewhere/keyfile
- mount
/dev/mapper/data
into/mnt/data
4. bind/mnt/data/Music
into/home/qdii/Music
(and other directories)
Additionally, whenever the device is unplugged I would like my OS to clear everything.
So far, I have 2 scripts. The first one opens and maps the device:
#!/bin/bash
sleep 5
vgchange -aly volga
cryptsetup luksOpen -d /somewhere/keyfile /dev/mapper/volga-data data
mount /dev/mapper/data /mnt/data
for i in Music Videos Ebooks Iso Vbox Games Vrac
do
mount --bind /mnt/data/$i /home/qdii/$i
done
The second one closes and unmaps the device:
#!/bin/bash
umount -l /run/media/qdii/zero
umount -l /home/qdii/{Videos,Ebooks,Iso,Music,Vbox,Games,Vrac}
umount -l /mnt/data
sleep 2
cryptsetup luksClose /dev/mapper/data
lvchange -aln volga/data
To automate the execution of the first script, I wrote a simple udev rule:
SUBSYSTEMS=="usb", ATTRS{serial}=="20120530280019", KERNEL=="sd?2", NAME="%k", SYMLINK+="thomson", GROUP="usb", RUN="/bin/bash /somewhere/open.sh"
It works wonder, but I am not sure how to write the second udev rule, to call the second script upon disconnection (or when the computer goes to hibernate/suspend).