How to automatically unmount encrypted loop device at shutdown?

1

1

I'm using debian testing, and I have a file, it's a LUKS container with ext4 filesystem. My system mounts it with every boot. I had to use the three following files in order to do so:

/etc/crypttab

sda2_crypt     UUID=727fa348-8804-4773-ae3d-f3e176d12dac   none        luks
crypt_dropbox   /media/Server/Dropbox/luks_dropbox          sda2_crypt  luks,keyscript=/lib/cryptsetup/scripts/decrypt_derived,noauto

/etc/fstab

# dropbox
UUID=0d959e74-ec19-43bf-b779-60134c676aef   /media/Dropbox  ext4    defaults,noauto,user,nofail,noatime,commit=20   0 2

/etc/rc.local

cryptdisks_start crypt_dropbox
mount /media/Dropbox

The container can't be opened and mounted automatically at boot via /etc/crypttab + /etc/fstab because the dropbox volume is on a partition and has to be opened before the partition is mounted. So, that's why there's noauto in /etc/crypttab . The filesystem inside that container can't also be mounted via mount -a (at least at boot) because there's no device yet. The option nofail exists to suppress the "no device" error. And the first phase is completed. The second phase is when boot is done, that's why I used /etc/rc.local file. The first line opens without password (because of settings in /etc/crypttab) the dropbox volume, the second mounts it via /etc/fstab . And that works.

The error occurs while the system tries to unmount the partition where the dropbox volume is. I don't know exactly why but it could be because the filesystem of the dropbox volume is mounded, or maybe the container is opened, or even because of the loop0 device is still active. In order to close the dropbox volume completely, I have to use the following commands:

umount /media/Dropbox
cryptdisks_stop crypt_dropbox
losetup -d /dev/loop0

Is there a way to do it only when system goes down but before unmounting any other partition? Is there a file where I can insert these commands, similar to /etc/rc.local ?

Mikhail Morfikov

Posted 2013-11-23T22:18:19.030

Reputation: 781

Answers

2

The specifics will depend on exactly which distribution you are running and how it is set up, but painting with a very broad brush, when a Linux system goes down it enters either runlevel 0 (shutdown) or 6 (reboot).

When switching runlevels, the init process will execute "kill" and "start" scripts for the entered runlevel.

You can add such scripts of your own to execute arbitrary commands when entering a given runlevel. For example, to add a script on Debian that runs as the system is preparing to reboot, create a shell script /etc/init.d/my-reboot-prepare-script with whatever commands you like (look at the other scripts in that directory for some details on how to do it), and then add a symbolic link to it from /etc/rc6.d/K00my-reboot-prepare-script. The K at the beginning will cause the script to be called to "stop" the process in question, with the parameter stop.

a CVn

Posted 2013-11-23T22:18:19.030

Reputation: 26 553