I have a virtualized CentOS 7 server that needs to mount multiple password-protected encrypted volumes. I cannot automatically map the devices on boot, because I don't have access to the console during the boot process to enter the decryption password. After I reboot the system, I have to manually run
cryptsetup luksOpen <device> <name>
to map each underlying block device to an encrypted device. That requires keeping notes on the UUID of each underlying block device and the name it maps to. Is there an easy way to automate this process? I can add the information to /etc/crypttab
with the noauto
keyword to prevent the devices from mounting on boot. However, I can't get cryptsetup to use the information from this file.
It would be great if there were a command like cryptsetup luksOpen <name>
that would read /etc/crypttab
to find the name of the underlying block device (similar to the way that you can can mount <mountpoint>
if is defined in /etc/fstab
).
Is there any way to get cryptsetup to read the mappings from /etc/crypttab
?
You can use
sudo systemctl start systemd-cryptsetup@<name>
instead of
cryptsetup luksOpen UUID=... <name>
when you have an entry as follows in your /etc/crypttab:
<name> UUID=... none noauto
It will prompt you for the passphrase if needed.
The corresponding unit file is generated automatically by systemd-cryptsetup-generator.
You can list all generated unit files using
systemctl list-unit-files| grep systemd-cryptsetup
Have a look at cryptdisks_start and cryptdisks_stop, they do exactly that.
I think you want to experiment with
systemd-cryptsetup-generator
.Normally this process runs during the initramfs boot, to dynamically generate systemd units that decrypt each block device listed in
/etc/crypttab
. You can then start those units whenever you wish, and you'll be prompted for any necessary passphrases.Since this is a virtual machine, you should have access to the virtual console, meaning you could simply encrypt your filesystems normally and provide the passphrase at boot. Of course, the security of the encrypted filesystems is compromised anyway, simply by being used in a virtual machine, regardless of when you enter the passphrase.
I made a bash script specifically for this case,
it parses
crypttab
to retrieve the uuid of device to open/close,afterwards it uses
fstab
to store mount options.As a convention I mount the encrypted device in the root folder on a directory named like the device node in
/dev/mapper
but capitalized;for example, a device named
xsnl
in crypttab will mount on/Xsnl
.NB: you will need to use
noauto
option in bothfstab
andcrypttab
.gist