The documentation about disk encryption which is part of Slackware allows you to boot your Slackware computer from a LUKS-encrypted partition containing the root ('/') filesystem, but it still requires you to keep an un-encrypted '/boot' filesystem on a separate partition.
Recent Grub versions in Slackware have the capability to let the bootloader read information from a LUKS-encrypted volume. That is the key (pun intended) to configure your Slackware computer with full-disk encryption; meaning that all of your partitions and filesystems can be LUKS-encrypted. Even though Slackware's own documentation does not mention this, it is trivial to configure, essentially it needs a single line in "/etc/default/grub" - see step (4) at the end of this article.
The sequence in a regular Slackware is then as follows:
- Grub will ask for a LUKS passphrase when it starts.
- After you enter the passphrase, Grub will read the kernel and initrd files from the LUKS-encrypted /boot directory (which can now be part of the root filesystem and does not have to be on a separate partition anymore) and load these into RAM, then instructs the kernel to boot up.
- The kernel will load the initrd image file in turn;
- The embedded "init" script will detect that your root filesystem is encrypted. But since it can not talk to Grub, the "init" script needs your help again, and it will ask for a LUKS passphrase - the second time that you have to enter that string manually.
That's a nuisance which can be overcome, Slackware has all the tools on board already. In this article I will show you how.
In short
The basic steps are:
- create a new LUKS key - which will be a file, not a passphrase
- add that key file to the /boot/initrd-tree directory
- tell geninitrd and mkinitrd that we don't want /boot/initrd-tree to be deleted all the time, and that we added a LUKS key in there
- tell grub that it needs to load support for LUKS volumes
The outcome is that you are asked only once to enter a LUKS key - by the Grub bootloader.
The details are a bit more convoluted, but the above four steps are what you need to keep in mind.
In detail
The Slackware init script which sits embedded in the initrd image needs a means to unlock your LUKS-encrypted root filesystem. We want that to happen un-attended (avoid typing a passphrase) and we achieve that by using an embedded LUKS key which we copy into the initrd image. We must have added this key to the LUKS header of the encrypted volume first (using "cryptsetup luksAddKey", see below).
OK, let us start with step (1): creating a LUKS key file - using "dd" and "urandom"!
The command results in what is essentially a 2 KB file filled with random data, and we're going to tell LUKS that this random sequence of bytes should be accepted as a key to unlock the LUKS encryption layer.
# dd bs=512 count=4 if=/dev/urandom of=/root/cryptkey.bin
# chmod 600 /root/cryptkey.bin
# cryptsetup luksAddKey /dev/your_luks_partition /root/cryptkey.bin
That "your_luks_partition" needs to be changed into the actual device name of your own LUKS-encrypted partition. Something like "nvme0n1p4" or "sda1".
When you run the "cryptsetup luksAddKey" command, you will be prompted to provide a LUKS passphrase that is already present in the LUKS header of the partition - for instance the passphrase you entered when you created the volume. If you enter the correct string, then cryptsetup will add your 2 KB of random bytes to the LUKS header as a new encryption key. LUKS2 can store 32 keys, so you should have room to spare.
Step (2) tells us to add this key file to the initrd image. We can't modify that image file directly, so we copy the key file into the directory "/boot/initrd-tree/" instead. When mkinitrd runs it will pack that complete directory, with everything in it, into a new "/boot/initrd-<kernel_version>.img" file.
I wrote a small script "extract_initramfs.sh" with which you can easily extract the contents of an existing initrd image into a new directory. Let's download and install it:
# cd /usr/local/sbin
# wget http://www.slackware.com/~alien/tools/extract_initramfs.sh
# chown +x extract_initramfs.sh
# ln -s extract_initramfs.sh create_initramfs.sh
Let's then add this key to the initrd-tree. Since Slackware's geninitrd default action is to delete the "/boot/initrd-tree" directory after an initrd image has been created, we change that default to "keep that directory please" (see further down) but to prevent confusion when our new configuration runs for the first time, let's simply extract the most recent existing initrd image to get a "/boot/initrd-tree" populated.
# ls -l /boot/vmlinuz-*.img |tail -1
# extract_initramfs.sh -h
# extract_initramfs.sh /boot/initrd-<your_kernel_version>.img /boot/initrd-tree/
# cp /root/cryptkey.bin /boot/initrd-tree/
For step (3), make sure that your "/etc/default/geninitrd" contains this line. If the current value is "true" then change it to "false":
AUTO_REMOVE_INITRD_TREE=false
In "/etc/mkinitrd.conf" ensure that "CLEAR_TREE" and "LUKSKEY" exist and have the shown values - this instructs the "mkinitrd" command to never delete the "/boot/initrd-tree" directory and also tells the init script where to find the LUKS key when it needs it during boot (note that it says "/cryptkey.bin" because init searches it from the root of the extracted initrd image):
CLEAR_TREE="0"
LUKSKEY="/cryptkey.bin"
Why do we need to keep that /boot/initrd-tree instead of allowing it to be deleted? The geninitrd default is to delete the directory to save space, every time you install or upgrade a kernel-generic package. Makes sense, right?
Well no! Simply because we are going to add our LUKS key file in there and we don't want to lose it during the next kernel upgrade.
When geninitrd creates an initrd image file (through an invocation of "mkinitrrd"), it copies all (and only those) kernel modules into "/boot/initrd-tree" that are required to mount the root filesystem. When that directory is never deleted, old kernel modules are staying behind. To prevent the initrd image file to bloat with useless old kernel modules we remove the kernel modules that belonged to the older kernel, prior to running "mkinitrd".
Geninitrd supports a "pre-install" script that allows us to do exactly that. Copy this text into a new file, inside directory/etc/geninitrd.d/pre-install/, and name it something like "remove-kmods-from-and-install-lukskey-to-initrd-tree.sh" (the name really does not matter).
Then make the script executable:
# chmod +x /etc/geninitrd.d/pre-install/remove-kmods-from-and-install-lukskey-to-initrd-tree.sh
#!/bin/bash
# To prevent the initrd to bloat with old kernel modules,
# we remove them prior to running mkinitrd.
rm -rf /boot/initrd-tree/lib/modules/*
# And to be safe, copy the LUKS key into the tree every time:
cp -a /root/cryptkey.bin /boot/initrd-tree/
Step (4) finally, makes Grub aware of LUKS encrypted volumes.
Check "/etc/default/grub" and make sure that this line is present - it enables Grub to load kernel and initrd from a LUKS-enrypted filesystem:
GRUB_ENABLE_CRYPTODISK=y
Summarizing
You see, that was not really a lot of work! All the building blocks are already part of Slackware-current (to become Slackware 16.0), it just needs a bit of configuration.
Every time you install a new kernel, these scripts will ensure that the LUKS key stays inside the generated initrd image. When your computer boots, Grub will ask you once for a LUKS passphrase so that it can load kernel and initrd files into RAM, but the rest is automatic.
I hope this helps! Eric
Recent comments