Gentoo full disk encryption with dm-crypt/LUKS
This post covers the manual installation procedure of Gentoo Linux with encrypted root and swap partitions using LUKS and dm_crypt on a legacy BIOS system.
Initial setup
The procedure is more or less the same as the one outlined in the Gentoo Linux x86 Handbook. However, when it comes to paritioning the drive, compiling the Kernel and setting the initial ramdisk, several different steps must be carried out.
I went through the whole process inside a Virtual Machine, using VMWare Player
as hypervisor. The Gentoo live image I used is the weekly build
install-x86-minimal-20130820
(sha512: d3135b53). Working “remotely” through ssh is much
more convenient. RSA/DSA ssh keys must be generated with ssh-keygen, a root password
set and sshd daemon started.
Drives configuration
The Gentoo Linux x86 Handbook can be followed up to step 4, which covers hard disks configuration. I will be using /dev/sda both for boot and root partitions. The first step is to create a plain primary boot partition with fdisk and to format it with a Unix-like filesystem, ext4 in this case, with mkfs.ext4. As far as the size is concerned, 256M are enough. The second partition, which will be used as a LVM physical volume with on top two logical volumes for root and swap, can take up all the space left on the device. This second partition must be formatted as a LUKS partition.
By opening the LUKS volume, a mapping with a plaintext device via the device mapper layer is created. This can be done with the following command.
The device mapper creates a /dev/mapper/vault. This becomes the LVM physical volume, which is then added to the volume group.
Now the logical volumes can be created. I used a 4GB LV for swap and a LV for root which takes take up the remaining capacity of the volume group.
The two LVs should appear under /dev/mapper: /dev/mapper/vg-root and /dev/mapper/vg-swap. A root and swap filesystems must be created on top of the LVs.
Now the Gentoo Handbook can be resumed from point 4.f
Kernel compilation
After the precompiled filesystem has been downloaded and the chrooted environment
has been set, the kernel must be compiled. The kernel source code can be
retrieved through Portage, Gentoo package manager, by “emerging” gentoo-sources
.
The version installed with this live image is linux-3.10.7-gentoo-r1
, but
the configuration procedure is highly hardware dependend. Make sure to activate
all the necessary modules to support the underlying hardware. For instance,
a while ago while I was working on a physical machine, I remember having problems
with the SATA controller which was supported by sata_nv module, compiled through
the CONFIG_SATA_NV
configuration option. Now, considering that I am working
on a virtual machine, the i386_defconfig
lacked these options:
CONFIG_FUSION_SPI
for LSI SCSI controller (which is the one emulated by VMPlayer)CONFIG_CRYPTO_SHA256
to support SHA256 algorithm in kernel spaceCONFIG_DM_CRYPT
to support dm_cyrpt frameworkCONFIG_PCNET32
for network support (this is not strictly necessary to set up the environment)
Once the kernel is properly configured, it can be compiled together with the modules.
After having compiled the kernel and copied the bzImage into /mnt/gentoo/boot, Gentoo Handbook can be resumed from Chapter 8. In section 8.a, the fstab file is set up. Since I am using logical volumes, the procedure is slightly different from the one outlined in the guide. My fstab looks like the following:
Bootloader installation
Chapter 10 of the Gentoo Handbook covers the installation of the bootloader. I will use grub legacy (i.e. v0.97), since I am quite familiar with it and it will help speed up the process.
DONT_MOUNT_BOOT
variable prevents grub from trying to mount the boot partition,
already mounted, and consequently failing. When prompted for the
installation directory, just type /boot. grub stage1 and stage1.5 must then be installed
respectively on the MBR and in the DOS compatibility region of /dev/sda.
An alternative way to install grub is to simply use grub-install
on /dev/sda.
update-grub
can normally be used to update menu.lst (or grub.cfg) based on the kernels
available under /boot, but in this case the configuration file is so simple that
it can be populated manually. As a aside note, update-grub relies on the output
of df command, which must report correctly an entry for the boot partition. If /etc/mtab is empty,
an error is raised (df: cannot read table of mounted file systems). A quick workaround
is to manually add to /etc/mtab the following line
Creation of the initrd
The initial ramdisk responsible for mounting the encrypted device must contain cryptsetup tools and all the dependencies listed by ldd. For example:
After leaving the chrooted environment, the following script can be used to
setup the initrd, which will be packaged under $(pwd)/initramfs
.
Code
Final steps
Once created the initrd, grub.conf should be configured to load the kernel image and the initrd.
After umounting /mnt/gentoo/boot, /mnt/gentoo/proc, /mnt/gentoo and rebooting the machine, the initrd should prompt for the password of the encrypted volume and then mount the root filesystem.