Installing Arch Linux with LVM and cryptsetup
Tags: archlinux
Published: 2021-03-29 15:54:20

As a software developer, you probably already know that anyone with a small amount of programming knowledge should be using Arch Linux (that's a joke).

Honestly, I'm writing this post in order to have a quick guide for myself whenever I like to install Arch Linux. I hope you find it useful.

Some disclaimers before we start. First, this tutorial is almost a copy and paste from the original Arch Linux [wiki](https://wiki.archlinux.org/index.php/Installation_guide). I think it suits my needs better than the Wiki and I hope you think it does that too. Second, this post is much more like a `script` than a tutorial which is concerned to explain the meaning and consequences of every single command. For that I think you'll find better answers inside the [Arch Linux Wiki] (https://wiki.archlinux.org/).

## Downloading

Download the latest version of Arch Linux [here](https://archlinux.org/download/).

Create a bootable USB with [Rufus](https://rufus.ie/) if you're using Windows, or with [`dd`](https://wiki.archlinux.org/index.php/USB_flash_installation_medium)

## Pre-Installing

1) Load a custom key map. For me, it's the Brazilian standard (ABNT).

```shell
loadkeys br-abnt
```

2) Verify you're connected to the internet

```shell
ping dev.to
```

3) Update system time and date

```shell
timedatectl set-ntp true
```

4) Partition the disk

Verify your current disk state with `fdisk -l`
![alt text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/872azuox1nnwg3aw4ihs.png)
 
In my case, the target disk is mounted at `/dev/sda`. Make sure to take note of your mount point, otherwise you may format the wrong disk and loose all your data.

Execute `fdisk /dev/sdX`, where `X` is the letter of your disk.

![alt text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8qdh4t28h2dmq6uos98l.png)

You be redirected to another console where you'll be able to format and create partitions on the selected disk.

First, we'll execute `g` to create a GPT disklabel.

![alt text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wdgoi8lfw7lqyo28e59y.png)

Next, we have to create two partitions: 1) One for your boot files and 2) another one to hold all the OS data.

![alt text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cxfye8s4mm90xg51uvlj.png)
 
To create the first partition, we'll type `n` and press `Enter`. We'll press enter to next two options (partition number and first sector) in order to select the default values, and finally write `+500M` in the third option in order to allocate 500Mb of space to it.

To create the second partition, we'll again execute the `n` command and select the default values (pressing `Enter`) to all the options since we want all the available space to the second partition.

Finally, by pressing `t` and selecting `1` for the first partition and `1`, which is the code for `EFI System`, we are able to replace the default partition type of our first partition for the one we need.

To __write__ the modifications press `w`.

5) Encrypting the partitions

Once we have the partitions set, we can [encrypt](https://wiki.archlinux.org/index.php/dm-crypt/Device_encryption) the one which will hold the OS and all our data. 

![alt text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bxsvcpzybql8kgxs2af8.png)

In my case, it's the `/dev/sda2`. 

```shell
cryptsetup luksFormat /dev/sdX
```

Remember that if you forget your password, you'll loose all the data stored in the partition forever.

```shell
cryptsetup open /dev/sda2 cryptlvm
```

6) Create the [LVM](https://wiki.archlinux.org/index.php/LVM#Volume_operations)

```shell
pvcreate /dev/mapper/cryptlvm

vgcreate CryptDisk /dev/mapper/cryptlvm

lvcreate -L 8G CryptDisk -n swap

lvcreate -l 100%FREE CryptDisk -n root
```

7) Format the partitions

Replace the `XX` with the letter and number of your boot partition (the 500Mb one)

```shell
mkfs.msdos -F32 /dev/sdXX

mkfs.ext4 /dev/CryptDisk/root

mkswap /dev/CryptDisk/swap
```

8) Mount the partitions

```shell
mount /dev/CryptDisk/root /mnt

swapon /dev/CryptDisk/swap

mkdir /mnt/boot

mount /dev/sdXX /mnt/boot # Replace XX with your 500Mb partition
```

Congratulations, you're ready to install Arch Linux!!!

## Installing

If you don't live in US or Europe the mirrors used to download the Arch Linux are not the best for you. Thus, I recommend you to rank the mirrors in order to have the best performance when downloading the OS.

```shell
pacman -S pacman-contrib

cp /etc/pacman.d/mirrorlist /etc/pacman.d/mirrorlist.backup

rankmirrors -n 6 /etc/pacman.d/mirrorlist.backup > /etc/pacman.d/mirrorlist
```

It usually takes around 5 to 10 minutes to run this process.

Finally, it's time to install Arch Linux!!!

```shell
pacstrap /mnt base base-devel linux linux-firmware
```

## Configuration

Since we'll have to edit some configuration files, I start by installing `vim`, but you may use another text editor of your choice.

```shell
pacman -Sy vim
```

1) Generate `fstab` configuration file.

```shell
genfstab -U /mnt >> /mnt/etc/fstab
```

2) Login into the installed system

```shell
arch-chroot /mnt
```

3) Time Zone

```shell
ln -sf /usr/share/zoneinfo/America/Sao_Paulo /etc/localtime

hwclock --systohc
```
 
4) Locales

Edit `/etc/locale.gen` and uncomment `en_US.UTF-8 UTF-8` and any other locale that you might need.

```shell
vim /etc/locale.gen

locale-gen

echo "LANG=en_US.UTF-8" > /etc/locale.conf

localectl set-keymap --no-convert br-abnt # Replace "br-abnt" with your keyboard type
```

5) Network

```shell
echo "rafael-dell" > /etc/hostname
```

Modify the `/etc/hosts` file.

```shell
127.0.0.1	localhost
::1		localhost
127.0.1.1	rafael-dell.localdomain	rafael-dell
```

6) `initramfs`

Since we're using custom configurations (LVM and ecrypted disk), we have to modify the `/etc/mkinitcpio.conf` so the machine can be properly booted.

```shell
vim /etc/mkinitcpio.conf
```

Add the followings commands in the `HOOKS`: `encrypt lvm2 keyboard`

7) Install the bootloader

```shell
pacman -S grub

grub-install --target=x86_64-efi --efi-directory=/boot --bootloader-id=GRUB
```

Since we have an encrypted disk, we need to set a few grub variables before generating the grub configuration files.

Edit the file `/etc/default/grub` and modify the following lines:

```shell
GRUB_CMDLINE_LINUX="cryptdevice=UUID=<device-UUID>:cryptlvm root=/dev/CryptDisk/root rw"
GRUB_ENABLE_CRYPTODISK=y
```

You'll have to replace the `<device-UUID>` with the encrypted partition UUID. If you're using `vim`, you can execute `read ! blkid /dev/sdXX` to paste the partition's UUID, otherwise you can run `lsblk -f` and get it.

8) Set up a root password

```shell
passwd
```

And we are done. Now you can `reboot` and enjoy your