[Help] How can I decrypt a luks + btrfs array at boot with a keyfile on a usb?
I have a headless fileserver with an 8 drive Btrfs Raid array. My root partition is on a separate SSD drive which decrypts using a keyfile on a usb at boot. I'd like to be able to encrypt my media drives as well using a usb key (so I don't have to put a password in 8 times). The only tutorials I've seen to do this have you storing the key file on your root partition, but I really don't like the idea of the keyfile sitting on the drive after it's been decrypted. I'd rather just stick the usb in, boot the computer and then remove the drive.
I'm thinking that the way to do this is to write a systemd script that mounts the usb drive before cryptsetup runs and then umounts it when it's done. I've never actually written a systemd script though, and I can't quite figure out how to do it.
If anyone knows how to do this, or can point me torwards some resources, I'd really appreciate it. Thanks in advance!
About this post
- Posted
- Archived
- Score
- 1 · 100% up
- Kind
- Text
- ID
- 3te8bb
7 comments
Apologies if I'm misinterpreting you but can you not generate the keys on the USB and edit /etc/crypttab to point to the USB (using UUID or label is better)
edit: you would have to manually remove the USB device. The USB doesn't need to be mounted (except when generating a key) as long as the kernel can see it
https://wiki.archlinux.org/index.php/Dm-crypt/System_configuration#crypttab
/etc/crypttab
/etc/fstab
In your example, you have crypttab pointed at a mounted partition. Can you do this without mounting it (point to something like /dev/disks/by-id/<UUID>? My understanding is that the key has to be in a partition that is mounted for crypttab to read it.
The wiki says that crypttab is ran before fstab, so I didn't think you could mount the usb in fstab and then have crypttab get the key, but I just tried it and that works. Is there a way to automattically unmount the drive when the machine finishes booting? That way I won't have to ssh in to umount.
I see what you mean. To my knowledge you can't unlock it without the device being mounted as the cryptsetup cannot find the key. I just tested it with one of my encrypted partitions and it was having none of it.
Systemd should be able to do it but I don't know how you'd go about it. If you log into the remote PC with a user you could put an umount option in the {bash,zsh}rc which should work (allow users to unmount in fstab or use sudo)
you could also just have your whole boot on the usb.
do you mean /boot or rootfs on usb? also, would it be possible for the latter system to run if the usb drive is removed?
/boot on USB