DEV Community

Rafael Coelho
Rafael Coelho

Posted on • Edited on

Installing Arch Linux with LVM and cryptsetup

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. 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.

Downloading

Download the latest version of Arch Linux here.

Create a bootable USB with Rufus if you're using Windows, or with dd

Pre-Installing

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

loadkeys br-abnt
Enter fullscreen mode Exit fullscreen mode

2) Verify you're connected to the internet

ping dev.to
Enter fullscreen mode Exit fullscreen mode

3) Update system time and date

timedatectl set-ntp true
Enter fullscreen mode Exit fullscreen mode

4) Partition the disk

Verify your current disk state with fdisk -l
alt text

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

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

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

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 the one which will hold the OS and all our data.

alt text

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

cryptsetup luksFormat /dev/sdX
Enter fullscreen mode Exit fullscreen mode

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

cryptsetup open /dev/sda2 cryptlvm
Enter fullscreen mode Exit fullscreen mode

6) Create the LVM

pvcreate /dev/mapper/cryptlvm

vgcreate CryptDisk /dev/mapper/cryptlvm

lvcreate -L 8G CryptDisk -n swap

lvcreate -l 100%FREE CryptDisk -n root
Enter fullscreen mode Exit fullscreen mode

7) Format the partitions

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

mkfs.msdos -F32 /dev/sdXX

mkfs.ext4 /dev/CryptDisk/root

mkswap /dev/CryptDisk/swap
Enter fullscreen mode Exit fullscreen mode

8) Mount the partitions

mount /dev/CryptDisk/root /mnt

swapon /dev/CryptDisk/swap

mkdir /mnt/boot

mount /dev/sdXX /mnt/boot # Replace XX with your 500Mb partition
Enter fullscreen mode Exit fullscreen mode

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.

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
Enter fullscreen mode Exit fullscreen mode

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

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

pacstrap /mnt base base-devel linux linux-firmware
Enter fullscreen mode Exit fullscreen mode

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.

pacman -Sy vim
Enter fullscreen mode Exit fullscreen mode

1) Generate fstab configuration file.

genfstab -U /mnt >> /mnt/etc/fstab
Enter fullscreen mode Exit fullscreen mode

2) Login into the installed system

arch-chroot /mnt
Enter fullscreen mode Exit fullscreen mode

3) Time Zone

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

hwclock --systohc
Enter fullscreen mode Exit fullscreen mode

4) Locales

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

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
Enter fullscreen mode Exit fullscreen mode

5) Network

echo "rafael-dell" > /etc/hostname
Enter fullscreen mode Exit fullscreen mode

Modify the /etc/hosts file.

127.0.0.1   localhost
::1     localhost
127.0.1.1   rafael-dell.localdomain rafael-dell
Enter fullscreen mode Exit fullscreen mode

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.

vim /etc/mkinitcpio.conf
Enter fullscreen mode Exit fullscreen mode

Add the followings commands in the HOOKS: encrypt lvm2 keyboard

7) Install the bootloader

pacman -S grub

grub-install --target=x86_64-efi --efi-directory=/boot --bootloader-id=GRUB
Enter fullscreen mode Exit fullscreen mode

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:

GRUB_CMDLINE_LINUX="cryptdevice=UUID=<device-UUID>:cryptlvm root=/dev/CryptDisk/root rw"
GRUB_ENABLE_CRYPTODISK=y
Enter fullscreen mode Exit fullscreen mode

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

passwd
Enter fullscreen mode Exit fullscreen mode

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

Top comments (1)

Collapse
 
david_kariuki profile image
David Kariuki • Edited

You missed the regenerate grub.cfg file after changing the /etc/default/grub file

grub-mkconfig -o /boot/grub/grub.cfg