Linux – How to automaticaly mount luks-partition only when disk is plugged in

external hard drivelinuxluks

I have the following scenario:

I want to automatically backup some data from my Laptop(Fedora Core 17) to a external encrypted(luks) hard disk.
The disk can be opened by a key file, which lies on the also encrypted root partition of my laptop.
The hard disk is attached to my docking station and therefore only "present" when I am at home (which is approximately 1/2 of the time the Laptop runs)

I have everything set up the way I want it with one exception.

I don't get a decent way to mount the hard disk automatically at boot if and only if it is present.

If I add it to crypttab and fstab without noauto it tries to mount it at boot and takes a lot(!) of time and error messages when it is not present. If I add noauto, well it does not mount automatically 😉

Is there a way to configure luks/crypttab to do the following:

  • check whether the disk is present
  • if yes: decrypt/mount
  • if no: just don't

Best Answer

I don't have a ready-made answer with exact file names and commands for you, but I would add something to a late-running init script such as /etc/init.d/local that would look in one of the /dev/disk/by-* directories to see if the LUKS partition is available, and proceed if it is.

Perhaps something similar to:

start_luks_backup_disk() {
  cryptdisks_start yourlukspartition && \
  mount /mnt /dev/mapper/yourlukspartition
}

test -b /dev/disk/by-uuid/8317fb0e-0707-11e2-8a07-5b563062136f \
&& start_luks_backup_disk
Related Question