Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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:

  1. enable the logical volume "data"
  2. use cryptsetup to create an unencrypted data node in /dev/mapper/data using a key file stored in /somewhere/keyfile
  3. 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).

share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.