Skip to content

[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!

1 points7 comments
Open on Reddit
About this post
Posted
Archived
Score
1 · 100% up
Kind
Text
ID
3te8bb

Author

4
posts
22
comments
0
media
View profile

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

#the first field is the name of the mapped device and will be in /dev/mapper/name. The columns are explained in crypttab
partition1  UUID=$UUID  /mnt/usb/keys/partition1

/etc/fstab

#/dev/mapper references are static so you can use them but UUIDs also work I believe
/dev/mapper/partition1  /mnt/partition1  btrfs defaults
3 points

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.

1 points

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.

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.

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.

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)

2 points

Keyboard shortcuts

Move and act

jork
Next / previous post or comment
Enteroro
Open the post
f
Open the media viewer
s
Save
h
Hide (the toast has Undo)
Tab
Right after an action: the toast's Undo
x
Collapse the comment
v
Next layout: cards, compact, grid

Go to

/
Search
gthenh
Home
gthena
All
gthens
Subreddits
gthenl
Library
?
This list
Esc
Close a menu or this list

In the viewer

←or→
Previous / next file
↑or↓
Previous / next post
korj
Previous / next post
Space
Play / pause
m
Mute
z
Zoom
s
Save
o
Open the post (on its own page: back to it)
Esc
Close

Keys do nothing while you type in a box.