+++
title = "Archlinux install with btrfs, systemd-boot, and full-disk encryption"
date = 2021-04-11T19:19:51-05:00
draft = false
+++
:toc:
:toclevels: 4

== Installing btrfs, systemd-boot, and LUKS with suspend to disk on encrypted swap partition.

Ive been using archlinux for several years now.
Of course, my first installs were... blunderous, as i wanted to do full disk encryption from the get-go, and I didnt know what I was doing.
After those first one or two installs, I generally settled on LVM on LUKS with a GRUB bootloader and my swap on an LVM volume,
mostly because it makes it much easier to setup hibernation/suspend to disk vs, say, a swap file.
(with a swap file, you have to deal with file offsets, and I have never gotten a satisfactory answer as to whether its possible for the filesystem to just *move* a file to a different disk sector in the process of, say, defragging with a very full hard drive.)

With my newest laptop, I decided to try out btrfs, in large part due to its snapshot system and ability to transfer those snapshots over a network.
(Im hoping to make a lightweight filesystem backup using this, on top of the data-level backups I currently use.)

However, suspend-to-disk is also quite important to me, and the archwiki is really only clear on how to do that with unencrypted partitions, LVM on LUKS, and on swapfiles.
The archwiki has some info on how to do it for the encrypt hook with a custom mkinitcpio hook, or with sd-encrypt hooks by just specifying multiple devices, but I didnt want to be writing a ton of custom config for the encrypt hook, and the section on sd-encrypt was not very clear at all, so I decided to do some experimentation and write up what worked for me.

=== A note on security and risk profiles

The encryption schema I am setting up in this guide is only meant to protect your data from theft of your physical device when it is turned off or suspended to the disk.
Full disk encryption will not protect you from anything while you laptop is powered on. After boot, the encryption is completely transparent to userspace.

Also, I did am not encrypting the boot partition, and Im not setting up any sort of secure boot.
This means that an attacker could hypothetically replace your boot partition or firmware and keylog your password, so if you suspect your computer has been tampered with, *DONT* boot it up.

To reiterate, this setup by itself only protect your data if your powered down machine is stolen. It does not protect you data from being stolen in any scenario where your laptop is powered on.

=== Things you should do first

Because some things are quite dependent on your system and network, as well as the type of system you end up with,
I will not be detailing some of the early setup steps, such as creating and booting from the arch ISO, or the final steps,
such as setting up a graphical environment.
Also, some of the middle steps require some modification depending on what sort of final setup you want, and your hardware.
I will call out those modifications in the relevant steps.

All this said, I would discourage you from blindly following this guide if its your first time installing arch (or a similarly diy distro like gentoo).
You should clearly understand what most of these commands do before typing them in.

Anyway, start by booting up the arch ISO...

=== Installing via ssh

Sometimes, you dont want to be switching from the computer you are installing linux on and the computer with the documentation and a search engine on it,
and Ive found the best way to avoid that is to set up a simple ssh session from the arch ISO to the computer with the documentation on it.

[IMPORTANT]
====
On a normal, already installed machine, *NEVER* use just a password for SSH. *ESPECIALLY* if it is internet-facing or connected to a public network. 
We are only doing this because we are (hopefully) on a personal network, and the password-based SSH session only exists on the Arch ISO, so as soon as you boot into your fresh system, the SSH session will be gone.
====

On the installee, make a password for the root account

{{<highlight console "linenos=false">}}
$ passwd
{{</highlight>}}

Enable SSH using

{{<highlight console "linenos=false">}}
$ systemctl start sshd.service
{{</highlight>}}

Find the ip adress with

{{<highlight console "linenos=false">}}
$ ip addr show
{{</highlight>}}

you are looking for a line like

{{<highlight console "linenos=false">}}
inet 192.168.1.162/24 brd 192.168.1.255 scope global dynamic enp0s25
{{</highlight>}}

in this case, my LAN IP is 192.168.1.162

now, on the pc you are going to be SSHing from,

{{<highlight console "linenos=false">}}
$ ssh root@[ip we just found on installee]
{{</highlight>}}

and type in the password you set on the installee

now lets continue with the installation.

=== inital setup

verify you are connected to the internet

{{<highlight console "linenos=false">}}
$ ping 1.1.1.1
{{</highlight>}}

turn on ntp

{{<highlight console "linenos=false">}}
$ timedatectl set-ntp true
{{</highlight>}}

=== Partitioning

create partitions using the tools of your choice. I will be using the following partition map.

an EFI partition of 512M

a swap partition with a size equal to your RAM.

a btrfs partition containing the rest of the space.

=== Encryption

Encrypt the btrfs parition with

{{<highlight console "linenos=false">}}
$ cryptsetup luksFormat /dev/sda3

$ cryptsetup config --label="btrfs" /dev/sda3
{{</highlight>}}

and enter the encryption passkey. I recommend making it a full sentence for security.

Encrypt the swap partition. Use the same password as last time.

{{<highlight console "linenos=false">}}
$ cryptsetup luksFormat /dev/sda2

$ cryptsetup config --label="swap" /dev/sda2
{{</highlight>}}

now open the newly encrypted partitions

{{<highlight console "linenos=false">}}
$ cryptsetup open /dev/sda2 swap

$ cryptsetup open /dev/sda3 btrfs
{{</highlight>}}

=== Filesystem creation

format the EFI partition with FAT32 and give it the label EFI (label can be something else.)

{{<highlight console "linenos=false">}}
$ mkfs.vfat -F32 -n EFI /dev/sda1
{{</highlight>}}

format the swap partition as swap

{{<highlight console "linenos=false">}}
$ mkswap /dev/mapper/swap
{{</highlight>}}

format the root partition with btrfs and give the label root (label can be something else.)

{{<highlight console "linenos=false">}}
$ mkfs.btrfs -L btrfs /dev/mapper/btrfs
{{</highlight>}}

=== Creating and mounting subvolumes

{{<highlight console "linenos=false">}}
$ mount /dev/mapper/btrfs /mnt           

$ btrfs subvolume create /mnt/root

$ btrfs subvolume create /mnt/home

$ umount /mnt
{{</highlight>}}

mount subvols and EFI partition

{{<highlight console "linenos=false">}}
$ mount -o noatime,nodiratime,compress=zstd,ssd,discard,subvol=root /dev/mapper/btrfs /mnt

$ mkdir /mnt/home

$ mount -o noatime,nodiratime,compress=zstd,ssd,discard,subvol=home /dev/mapper/btrfs /mnt/home

$ mkdir /mnt/boot

$ mount /dev/sda1 /mnt/boot

$ swapon /dev/mapper/swap
{{</highlight>}}

=== installing base system, generating *stab

install the base packages. ajust the package list to suit your needs. change intel-ucode to amd-ucode if using an AMD processor.

{{<highlight console "linenos=false">}}
$ pacstrap /mnt linux linux-firmware base base-devel btrfs-progs zsh neovim git stow tmux connman wpa_supplicant openvpn fzf htop rsync tig tree xdg-user-dirs units python tree openssh w3m curl intel-ucode
{{</highlight>}}

generate an fstab

{{<highlight console "linenos=false">}}
$ genfstab -U /mnt > /mnt/etc/fstab
{{</highlight>}}

make /mnt/etc/crypttab.initramfs containing:

{{<highlight console>}}
$our swap device
swap LABEL=swap

$our main device
btrfs LABEL=btrfs
{{</highlight>}}

=== system config

chroot into the new system

{{<highlight console "linenos=false">}}
$ arch-chroot /mnt/
{{</highlight>}}

set time zone.
 
{{<highlight console "linenos=false">}}
$ ln -sf /usr/share/zoneinfo/Region/City /etc/localtime
{{</highlight>}}

run hwclock

{{<highlight console "linenos=false">}}
$ hwclock --systohc
{{</highlight>}}

uncomment needed locales in /etc/locale.gen (you always need to at least uncomment en_US.UTF-8 UTF-8.)

gen locales

{{<highlight console "linenos=false">}}
$ locale-gen
{{</highlight>}}

set LANG variable

{{<highlight console "linenos=false">}}
$ echo 'LANG=en_US.UTF-8' > /etc/locale.conf
{{</highlight>}}

create the hostname file

{{<highlight console "linenos=false">}}
$ echo '[myhostname]' > /etc/hostname
{{</highlight>}}

and add matching entries to /etc/hosts, like so (if static ip, use that. if dynamic, use 127)

{{<highlight console>}}
127.0.0.1	localhost
::1		localhost
127.0.1.1	myhostname.localdomain	myhostname
{{</highlight>}}

=== installing the boot loader

edit /etc/mkinitcpio.conf so the HOOKS line looks like this:

{{<highlight console "linenos=false">}}
HOOKS=(base systemd udev autodetect modconf block sd-encrypt btrfs resume filesystems keyboard fsck)
{{</highlight>}}

and regen the initramfs

{{<highlight console "linenos=false">}}
$ mkinitcpio -p linux
{{</highlight>}}

install systemd-boot

{{<highlight console "linenos=false">}}
$ bootctl install
{{</highlight>}}

create /boot/loader/entries/arch.conf containing:

{{<highlight console>}}
title Arch Linux
linux /vmlinuz-linux
initrd /intel-ucode.img
initrd /initramfs-linux.img
options root=/dev/mapper/btrfs rootflags=subvol=/root resume=/dev/mapper/swap
{{</highlight>}}

edit /boot/loader/loader.conf and add:

{{<highlight console>}}
default arch.conf
timeout 2
console-mode max
editor no
{{</highlight>}}

=== system config

set root password

{{<highlight console "linenos=false">}}
$ passwd
{{</highlight>}}

exit and shutdown the system

{{<highlight console "linenos=false">}}
$ exit

$ shutdown now
{{</highlight>}}

remove the install media, and boot back up. make sure everything boots. from now on, configure the system as normal.

=== configuring userspace

add a non-root user

{{<highlight console "linenos=false">}}
$ useradd -m -G wheel -s /bin/sh your_username
$ passwd your_username
{{</highlight>}}

symlink neovim to vi (assuming you installed neovim but not vi. modify as your installed packages call for.)

{{<highlight console "linenos=false">}}
$ ln -s /usr/bin/nvim /usr/bin/vi
{{</highlight>}}

configure sudo

{{<highlight console "linenos=false">}}
$ visudo
{{</highlight>}}

uncomment the line that reads

{{<highlight console "linenos=false">}}
%wheel ALL=(ALL) ALL
{{</highlight>}}

enable multilib: uncomment the following lines in /ec/pacman.conf

{{<highlight console "linenos=false">}}
[multilib]
Include = /etc/pacman.d/mirrorlist
{{</highlight>}}

Congrats! you now have a barebones, but functional, encrypted arch install!

