Need help: USB unlock LUKS on Alpine Linux
Need help: USB unlock LUKS on Alpine Linux
Hi folks,
I have Alpine Linux installed in an encrypted LUKS partition. I came across this tutorial which shows how to setup a key in a USB drive and when the drive is inserted and the computer booted, the LUKS partition auto-unlocks with the key on the USB drive.
I would like to setup the same thing but I do not have Alpine linux installed on ZFS, so I'm looking for ways to adapt the instructions.
So far, what I've done is:
- I've setup the key on the usb stick and I can unlock the LUKS partition with that key.
- create a
/etc/mkinitfs/features.d/usb-unlock.sh
script with the following content:
(the echo
to /dev/kmesg
was to check whether the script did indeed run at boot by trying to print to the kernel messages but I can't find anything in the kernel messages).
sh
#!/bin/sh echo "usb-unlock script starting..." > /dev/kmsg USB_MOUNT="/mnt/my-usb-key" # The USB stick mounting point LUKS_KEY_FILE="awesome.key" # The name of your keyfile on the USB stick # Search for the USB stick with the key for device in $(ls /dev/disk/by-uuid/*); do mount $device $USB_MOUNT 2>/dev/null if [ -f "$USB_MOUNT/$LUKS_KEY_FILE" ]; then # Unlock the LUKS partition cryptsetup luksOpen /dev/sda3 cryptroot \ --key-file "$USB_MOUNT/$LUKS_KEY_FILE" && exit 0 fi umount $USB_MOUNT done echo "No USB key found, falling back to password prompt." # this message never appears, despite not having found the key on the usb stick echo "usb-unlock script ending." > /dev/kmsg
- I added
usb-unlock
to thefeatures
inmkinitfs.conf
:
mytestalpine:~# cat /etc/mkinitfs/mkinitfs.conf features="ata base ide scsi usb virtio ext4 cryptsetup keymap usb-unlock"
- run
mkinitfs
to rebuild the initramfs. Then reboot to test the implementation, which was unsuccessful.
What am I missing / doing wrong? Thank you for your help!
Edit: forgot to add step 4
mkinitfs
doesn't support running custom shell hooks.mkinitfs
is very, very, very bare-bones custom code and the whole features concept exists only to pull extra files and kernel modules into the initramfs, not for extra logic.You'd either have to customize the init script itself (not impossible, it's 1000 lines) and pass
-i
/setinit=
in the .conf, or install Dracut/Booster instead (which should "just work" if youapk add
them, but I've had no need to do so).It seems you might be right. There is so little documentation for initramfs in Alpine Linux (the wiki page is very barebones), but I did manage to find this open issue:
https://gitlab.alpinelinux.org/alpine/mkinitfs/-/issues/18
So I guess this confirms that it is not yet possible.
Could you expand on your suggestion with customizing the init script? Where is this file located, and would you have some pointers of how to get started to customize it for my use case?
You'd be looking for
/usr/share/mkinitfs/initramfs-init
. I've never customized that myself, but it looks like there's already some support for a keyfile if you look forKOPT_cryptroot
and check that block of code. That looks like it's mostly set up for a keyfile embedded into the initramfs, but I guess it should be possible to replace that code with something that grabs the keyfile off an USB drive.I suppose you'd make a copy of it, put it somewhere in /etc or whatever and change the
mkinitfs.conf
to point to it.init="/etc/whatever/myinitramfs-init"
should do the trick since the config file just gets sourced in. That said you're definitively heading into unknown territory here. It might be easier to just use Dracut or the like instead.