LVM On LUKS - Alpine Linux
LVM On LUKS - Alpine Linux
wiki.alpinelinux.org/wiki/LVM_on_LUKS
Introduction
This documentation describes how to set up Alpine Linux on a fully encrypted disk (apart
from the bootloader partition). We will have an LVM container installed inside an
encrypted partition. To encrypt the partition containing the LVM volume group, dm-crypt
(which is managed by the cryptsetup command) and its LUKS subsystem is used.
Note that your /boot/ partition must be non-encrypted to work with Syslinux. When using
GRUB2 it is possible to boot from an encrypted partition to provide a layer of protection
from Evil Maid attacks, but Syslinux doesn't support that.
The following documentation uses the /dev/sda device as installation destination. If your
environment uses a different name for your storage device, use the corresponding device
name in the examples.
Boot the latest Alpine Linux Installation CD. At the login prompt, use the root user
without a password to log in. Now we will follow the Setup-alpine script and make our
changes along the way.
# setup-keymap
# setup-hostname
# setup-interfaces
# rc-service networking start
1/12
If you are configuring static networking (i.e. you didn't configure any interfaces to use
DHCP), run setup-dns.
If you are using Wi-Fi you may need to do run rc-update add wpa_supplicant boot.
# passwd
# setup-timezone
# rc-update add networking boot
# rc-update add seedrng boot
# rc-update add acpid default
# rc-service acpid start
Edit your /etc/hosts to look like this, replacing <hostname> with your hostname and
<domain> with your TLD (if you don't have a TLD, use 'localdomain':
Contents of /etc/hosts
# setup-ntp
# setup-apkrepos
# apk update
# setup-sshd
Note: The parted partition editor is needed for advanced partitioning and GPT disklabels.
BusyBox fdisk is a very stripped-down version with minimal functionality
Optionally, if you want to overwrite your storage with random data first, install haveged,
which is a random number generator based on hardware events and has a higher
throughput than /dev/urandom:
2/12
We'll be partitioning the storage device with a non-encrypted /boot partition for use with
the Syslinux bootloader. Syslinux is meant for use with legacy BIOS and an MSDOS MBR
partition table.
Syslinux does support GPT partition tables but GRUB2 is the better option for UEFI (UEFI
is possible only with GPT).
+---------------------------+------------------------+-----------------------+
| Partition name | Partition purpose | Filesystem type |
+---------------------------+------------------------+-----------------------+
| /dev/sda1 | Boot partition | ext4 |
| /dev/sda2 | LUKS container | LUKS |
| |-> /dev/mapper/lvmcrypt | LVM container | LVM |
| |-> /dev/vg01/root | Root partition | ext4 |
| |-> /dev/vg01/swap | Swap partition | swap |
+---------------------------+------------------------+-----------------------+
Warning: This will delete an existing partition table and make your data very hard to
recover. If you want to dual boot, stop here and ask an expert.
Create a partition of approximately 100MB to boot from, then assign the rest of the space
to your LUKS partition.
# parted -a optimal
(parted) mklabel msdos
(parted) mkpart primary ext4 0% 100M
(parted) set 1 boot on
(parted) mkpart primary ext4 100M 100%
To view your partition table, type print while still in parted. Your results should look
something like this:
(parted) print
Model: ATA TOSHIBA ******** (scsi)
Disk /dev/sda: 1000GB
Sector size (logical/physical): 512B/4096B
Partition Table: msdos
Disk Flags:
3/12
installing a rootkit (or bootkit) in your boot partition while your computer is not unlocked.
The partitioning scheme will look like this:
+---------------------------+------------------------+-----------------------+
| Partition name | Partition purpose | Filesystem type |
+---------------------------+------------------------+-----------------------+
| /dev/sda1 | EFI system partition | fat32 |
| /dev/sda2 | LUKS container | LUKS |
| |-> /dev/mapper/lvmcrypt | LVM container | LVM |
| |-> /dev/vg01/root | Root partition | ext4 |
| |-> /dev/vg01/boot | Boot partition | ext4 |
| |-> /dev/vg01/swap | Swap partition | swap |
+---------------------------+------------------------+-----------------------+
Warning: This will delete an existing partition table and make your data very hard to
recover. If you want to dual boot, stop here and ask an expert.
Create an EFI system partition of approximately 200MB, then assign the rest of the space
to your LUKS partition.
# parted -a optimal
(parted) mklabel gpt
(parted) mkpart primary fat32 0% 200M
(parted) name 1 esp
(parted) set 1 esp on
(parted) mkpart primary ext4 200M 100%
(parted) name 2 crypto-luks
# haveged -n 0 | dd of=/dev/sda2
4/12
Default settings:
Then make sure all keys use pbkdf2 by adding a new key with:
Now you can try the conversion, although it may not work.
# pvcreate /dev/mapper/lvmcrypt
5/12
# lvcreate -L 2G vg0 -n swap
# lvcreate -l 100%FREE vg0 -n root
The LVs created in the previous steps are automatically marked active. To verify, enter:
# lvscan
This will create a 2GB swap partition, a 2GB boot partition and a root partition which
takes up the rest of the space. This setup is for those who do not need to use the
hibernate/suspend to disk state. If you do need to suspend to disk, create a swap partition
slightly larger than the size of your RAM (change the size after # lvcreate -L).
The LVs created in the previous steps are automatically marked active. To verify, enter:
# lvscan
# mkfs.ext4 /dev/vg0/root
# mkswap /dev/vg0/swap
Before you can install Alpine Linux, you must mount the partitions and LVs. Mount the
root LV to the /mnt/ directory:
Next format your boot partition, create a mount point, then mount it:
# mkfs.ext4 /dev/sda1
# mkdir -v /mnt/boot
# mount -t ext4 /dev/sda1 /mnt/boot
6/12
Lastly, activate your swap partition:
# swapon /dev/vg0/swap
The installer downloads the latest packages to install the base installation. Additionally,
the installer automatically creates the entries for the mount points in /etc/fstab file, which
is currently mounted in the /mnt/ directory.
Note: The automatic writing of the master boot record (MBR) fails in this step. Later, you'll
manually write the MBR to the disk.
The swap LV is not automatically added to the fstab file. so we need to add the following
line to the /mnt/etc/fstab file:
Edit the /mnt/etc/mkinitfs/mkinitfs.conf file and append the cryptsetup module to the
features parameter:
features="... cryptsetup"
If you are using GRUB with an encrypted /boot you must add the cryptkey feature so
that Alpine can use a keyfile for decryption on boot.
Note: Alpine Linux uses the en-us keyboard mapping by default when prompting for the
password to decrypt the partition at boot time. If you changed the keyboard mapping in
the temporary environment and want to use it at the boot password prompt, be sure to
add the keymap feature to the list above.
Note: Check the output of mkinitfs -L and add the features necessary for your system
to boot. You may need to add kms in order to see a password prompt at boot. You may
also need: usb, lvm, ext4, nvme...
The command uses the settings from the mkinitfs.conf file set in the -c parameter to
generate the RAM disk. The command is executed in the /mnt/ directory and the RAM
disk is generated using the modules for the installed kernel. Without setting the kernel
version using the $(ls /mnt/lib/modules/) option, mkinitfs tries to generate the RAM
disk using the kernel version installed in the temporary environment, which can differ from
the latest one installed by the setup-disk utility.
7/12
Installing a bootloader
To get the UUID of your storage device into a file for later use, run this command:
Tip: To easily read the UUID into a file so you don't have to type it manually, open the file
in vi, then type :r /root/uuid to load the UUID onto a new line.
The cryptroot parameter sets the ID of the device/partition that contains encrypted
volumes, and the cryptdm parameter uses the name of the mapping we have already
configured a few lines above.
We can also double check if modules and root are set correctly, eg:
modules=sd-mod,usb-storage,ext4,cryptsetup,keymap,cryptkey,kms,lvm
root=UUID=<UUID of /dev/mapper/vg0-root>
Because the update-extlinux utility operates only on the /boot/ directory, temporarily
change the root to the /mnt/ directory and update the boot loader configuration:
# chroot /mnt/
# update-extlinux
# exit
Because we didn't mount /dev nor /proc inside our /mnt/ chroot, some errors may occur
when we run update-extlinux command. But you can most likely ignore these.
To avoid having to type your decryption password twice every boot (once for GRUB and
once for Alpine), add a keyfile to your LUKS partition. The filename is important.
# touch /mnt/crypto_keyfile.bin
# chmod 600 /mnt/crypto_keyfile.bin
# dd bs=512 count=4 if=/dev/urandom of=/mnt/crypto_keyfile.bin
# cryptsetup luksAddKey /dev/sda2 /mnt/crypto_keyfile.bin
8/12
This keyfile is stored encrypted (it is in your LUKS partition), so its presence does not
affect system security.
Mount the required filesystems for the Grub EFI installer to the installation:
# chroot /mnt
# source /etc/profile
# export PS1="(chroot) $PS1"
The cryptroot parameter sets the ID of the device/partition that contains encrypted
volumes, and the cryptdm parameter uses the name of the mapping we configured a few
lines above. The cryptkey parameter indicates the existence of the file
/crypto_keyfile.bin you created previously.
To enable GRUB to decrypt LUKS partitions and read LVM volumes add:
Luks1
Luks2
Note: The method is still experimental and you may lose your access to you OS at the
next OS update
9/12
set crypto_uuid=00001
cryptomount -u $crypto_uuid
set root='lvmid/00002/00003'
set prefix=($root)/boot/grub
insmod normal
normal
00001 with blkid and find the uuid of your encrypted disk, i.e /dev/nvme0n1p2
remove hyphens from the UUID
00002 with vgdisplay & VG UUID
00003 with lvdisplay & LV UUID of the root partition /
# cd
# umount -l /mnt/dev
# umount -l /mnt/proc
# umount -l /mnt/sys
# umount /mnt/boot/efi
# umount /mnt/boot
# swapoff /dev/vg0/swap
# umount /mnt
# vgchange -a n
# cryptsetup luksClose lvmcrypt
# reboot
Troubleshooting
General Procedure
In case your system fails to boot, you can verify the settings and fix incorrect
configurations.
Reboot and do the steps in Prepare the temporary installation environment again.
10/12
Mount the file systems
Verify that you run the steps described in the Installing Alpine Linux section correctly.
Update the configuration if necessary, unmount the partitions, then reboot.
Secure boot
If secure boot complains of an unsigned bootloader, you can either disable it or adapt this
guide to sign GRUB. If you're using Syslinux, then secure boot should be automatically
disabled when you enable legacy boot mode.
Hardening
To harden, you should disable DMA[1][Dead Link] and install a hardened version of
AES (TRESOR[2] or Loop-Amnesia[3]) since by default cryptsetup with luks uses
AES by default.
Disable DMA in the BIOS and set the password for the BIOS according to
Wikipedia.[4]
Blacklist kernel modules that use DMA and any unused expansion modules
(FireWire, CardBus, ExpressCard, Thunderbolt, USB 3.0, PCI Express and hotplug
modules) that use DMA.
Note: This does not apply for volumes within your main encrypted partition /dev/sda2
11/12
For the purposes of these instructions we will say /dev/sdb1 contains an LVM volume
that should be mounted at /home.
Alpine, like Gentoo, uses the dmcrypt service rather than /etc/crypttab. Add the
following lines to /etc/conf.d/dmcrypt:
target=crypt-home
source='/dev/sdb1'
key='/root/crypt-home-keyfile.bin'
Add an entry to /etc/fstab, changing vg1 to the name of your LVM volume group:
12/12