this post was submitted on 06 Oct 2024
23 points (100.0% liked)

Linux

47597 readers
861 users here now

From Wikipedia, the free encyclopedia

Linux is a family of open source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991 by Linus Torvalds. Linux is typically packaged in a Linux distribution (or distro for short).

Distributions include the Linux kernel and supporting system software and libraries, many of which are provided by the GNU Project. Many Linux distributions use the word "Linux" in their name, but the Free Software Foundation uses the name GNU/Linux to emphasize the importance of GNU software, causing some controversy.

Rules

Related Communities

Community icon by Alpár-Etele Méder, licensed under CC BY 3.0

founded 5 years ago
MODERATORS
 

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.

https://askubuntu.com/questions/1414617/configure-ubuntu-22-04-zfs-for-automatic-luks-unlock-on-boot-via-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:

  1. I've setup the key on the usb stick and I can unlock the LUKS partition with that key.
  2. 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).

#!/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
  1. I added usb-unlock to the features in mkinitfs.conf:
mytestalpine:~# cat /etc/mkinitfs/mkinitfs.conf 
features="ata base ide scsi usb virtio ext4 cryptsetup keymap usb-unlock"
  1. 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

top 12 comments
sorted by: hot top controversial new old
[–] CMahaff@lemmy.world 2 points 12 hours ago* (last edited 12 hours ago) (1 children)

More of a debugging step, but have you tried running lsinitrd on the initramfs afterwards to verify your script actually got added?

You theoretically could decompress the entire image to look around as well. I don't know the specifics for alpine, but presumably there would be a file present somewhere that should be calling your custom script.

EDIT: Could it also be failing because the folder you are trying to mount to does not exist? Don't you need a mkdir somewhere in your script?

[–] TheHobbyist@lemmy.zip 1 points 7 hours ago

From my understanding, features always refer to components from within /etc/mkinitfs/features.d/

[–] ClemaX@lemm.ee 3 points 14 hours ago (1 children)

I think you may want to use for device in /dev/disk/by-uuid/*

That doesn't explain why you aren't seeing messages. I see there is a shebang at the start of the script. Can you confirm that the script has the executable bit set for the root user?

[–] TheHobbyist@lemmy.zip 1 points 7 hours ago

Yes it does.

[–] sun_is_ra@sh.itjust.works 2 points 14 hours ago* (last edited 14 hours ago) (1 children)

Seems that the file /etc/mkinitfs/features.d/ is only linux alphine thing so creating it for another linux distro does nothing.

https://wiki.alpinelinux.org/wiki/Initramfs_init

I would create a systemd service instead if your distro is using systemd https://www.slingacademy.com/article/ubuntu-how-to-create-a-custom-systemd-service/#Introduction

Edit: Sorry please ignore my comment. Your entire system is encrypted so that won't work. I'll see if there is another solution and post it

Edit2: Maybe you need to place the file here instead /usr/share/initramfs- tools/scripts/ ? https://manpages.ubuntu.com/manpages/bionic/en/man8/initramfs-tools.8.html

[–] TheHobbyist@lemmy.zip 1 points 7 hours ago (1 children)

Could it be? I don't have that directory. Maybe this is Ubuntu specific? Not sure.

[–] sun_is_ra@sh.itjust.works 0 points 5 hours ago (1 children)

would be easier if you tell us which distro are you running mkinitfs on

[–] TheHobbyist@lemmy.zip 2 points 5 hours ago* (last edited 5 hours ago)

This is about Alpine linux, as I wrote in the title and twice in the post.

[–] Wingless@mstdn.social 0 points 16 hours ago (1 children)

@TheHobbyist isn't it better to find the plugged USB flash drive by parsing the output of dmesg?

[–] TheHobbyist@lemmy.zip 1 points 16 hours ago (1 children)

That may be an option, but for the time being, I'm not even sure how to start debugging this. I have no idea where to start looking. I don't even know if the usb-unlock.sh script is even running at boot. Any thoughts?

[–] wesker@lemmy.sdf.org 2 points 14 hours ago (1 children)

Just a sanity check because I've totally done this before: did you make the script file executable?

[–] TheHobbyist@lemmy.zip 2 points 7 hours ago

Good point. Yes it is.