2022-02-16 20:10:29 +00:00
# ArchLinux install encrypted btrfs
# Install Arch Linux on EFI system with full filesystem (including /boot) encrypted and on btrfs partition
Official guide for basic install: [https://wiki.archlinux.org/index.php/Installation\_Guide ](https://wiki.archlinux.org/index.php/Installation_Guide )
2020-10-06 15:03:28 +00:00
it is always good to consult with official guide, cause arch config might change in time
2022-02-16 20:10:29 +00:00
For setting up different locale, or better explanations check out Arch Wiki
## 1. Boot ISO
#### Download the ISO file from [https://www.archlinux.org](https://www.archlinux.org/)
#### Put on pendrive
2021-07-25 14:20:59 +00:00
```bash
2022-02-16 20:10:29 +00:00
dd if=archlinux.img of=/dev/sdX bs=16M & & sync
2021-07-25 14:20:59 +00:00
```
2022-02-16 20:10:29 +00:00
#### Boot from the USB.
#### Optional (**experimental** approach to have desktop environment during install):
##### Extend writable space so you can install basic desktop in live environment and for example use gparted for partitioning or open this tutorial in web browser or whatever you want.
< p class = "callout warning" > Remember this area is saved in your RAM, so make sure you have enough of it< / p >
```
mount -o remount,size=5G /run/archiso/cowspace
pacman -Syy plasma-desktop glibc konsole xorg
pacman -Scc
startplasma-wayland
```
#### Set key map
2021-07-25 14:20:59 +00:00
```bash
loadkeys pl
```
2022-02-16 20:10:29 +00:00
#### Update clock
2021-07-25 14:20:59 +00:00
```bash
timedatectl set-ntp true
```
2022-02-16 20:10:29 +00:00
#### Optionally (recommended) update mirrorlist
2021-07-25 14:20:59 +00:00
```bash
reflector --country 'Poland' --age 24 --verbose --sort rate --save /etc/pacman.d/mirrorlist
```
2020-10-06 15:03:28 +00:00
2022-02-16 20:10:29 +00:00
## 2. Prepare Disk
#### Update btrfs-progs
2021-07-25 14:20:59 +00:00
```bash
2022-02-16 20:10:29 +00:00
pacman -Syy btrfs-progs
2021-07-25 14:20:59 +00:00
```
2022-02-16 20:10:29 +00:00
#### Display disks and partitions
2021-07-25 14:20:59 +00:00
```bash
2022-02-16 20:10:29 +00:00
lsblk
2021-07-25 14:20:59 +00:00
```
2022-02-16 20:10:29 +00:00
#### Create partitions (if you have not already)
2021-07-25 14:20:59 +00:00
```bash
fdisk /dev/sdX
```
2022-02-16 20:10:29 +00:00
1. 100MB EFI partition
2. 100% size partiton # ( encrypted optionally) for BTRFS partition, this partition will require formatting AFTER encryption if you do encryption
##### Swap will bin in file with CoW disabled, which will be prepared later
#### Format EFI partition
```Bash
mkfs.vfat -F32 /dev/sdX1
2021-07-25 14:20:59 +00:00
```
2022-02-16 20:10:29 +00:00
##### ----------------- encryption (optional) ------------------
#### Setup the encryption of the system,
< p class = "callout info" > Don't use regional letters (not in en-us keyboard) like ąęć etc. for password. This requires additional steps, which are not covered by this tutorial.< / p >
#### Grub have some kind of support for luks2, but not entirely, so for more fail-safe setup use luks1
2021-07-25 14:20:59 +00:00
```bash
cryptsetup -c=aes-xts-plain64 --key-size=512 --hash=sha512 --iter-time=3000 --pbkdf=pbkdf2 --use-random luksFormat --type=luks1 /dev/sdX2
2020-10-06 15:03:28 +00:00
2022-02-16 20:10:29 +00:00
cryptsetup luksOpen /dev/sdX2 MainPart
2021-07-25 14:20:59 +00:00
```
2022-02-16 20:10:29 +00:00
### Formatting as btrfs now when it is already encrypted
2021-07-25 14:20:59 +00:00
```bash
mkfs.btrfs -L "Arch Linux" /dev/mapper/MainPart
```
2022-02-16 20:10:29 +00:00
##### ---------------- end of encryption ------------------------
#### Format the partition if not yet formatted:
2021-07-25 14:20:59 +00:00
```bash
pacman -Syy btrfs-progs
2020-10-06 15:03:28 +00:00
2021-07-25 14:20:59 +00:00
mkfs.btrfs -L "Arch Linux" /dev/sdX2
```
2022-02-16 20:10:29 +00:00
#### Mount partition to be able to create btrfs subvolumes
##### If using encryption, change **/dev/sdX2** to **/dev/mapper/MainPart**:
2021-07-25 14:20:59 +00:00
```bash
mount /dev/sdX2 /mnt
```
2022-02-16 20:10:29 +00:00
#### Create subvolumes
##### This scheme can be adjusted to your needs, I'd suggest at least one subvolume for root (@) and one for snapshots (@snapshots). varlog and tmp are created to easily disable Copy on Write on` /var/log` and `/tmp`.
2021-07-25 14:20:59 +00:00
```bash
btrfs su cr /mnt/@
2020-10-06 15:03:28 +00:00
2021-07-25 14:20:59 +00:00
btrfs su cr /mnt/@home
2020-10-06 15:03:28 +00:00
2022-02-16 20:10:29 +00:00
btrfs su cr /mnt/@varlog
2020-10-06 15:03:28 +00:00
2021-07-25 14:20:59 +00:00
btrfs su cr /mnt/@tmp
2020-10-06 15:03:28 +00:00
2021-07-25 14:20:59 +00:00
btrfs su cr /mnt/@snapshots
2022-02-16 20:10:29 +00:00
2021-07-25 14:20:59 +00:00
```
2022-02-16 20:10:29 +00:00
##### Disable copy on write on `/var/log` and `/tmp`
2021-07-25 14:20:59 +00:00
```bash
2022-02-16 20:10:29 +00:00
chattr +C /mnt/@varlog
2021-07-25 14:20:59 +00:00
chattr +C /mnt/@tmp
umount /mnt
2022-02-16 20:10:29 +00:00
2021-07-25 14:20:59 +00:00
```
2022-02-16 20:10:29 +00:00
#### If using encryption, change **/dev/sdX2** to **/dev/mapper/MainPart**:
2021-07-25 14:20:59 +00:00
```bash
mount -o defaults,noatime,discard,ssd,subvol=@ /dev/sdX2 /mnt
2020-10-06 15:03:28 +00:00
2021-07-25 14:20:59 +00:00
mkdir /mnt/home
2020-10-06 15:03:28 +00:00
2022-02-16 20:10:29 +00:00
mkdir -p /mnt/var/log
2020-10-06 15:03:28 +00:00
2021-07-25 14:20:59 +00:00
mkdir /mnt/tmp
2020-10-06 15:03:28 +00:00
2021-07-25 14:20:59 +00:00
mkdir /mnt/snapshots
2020-10-06 15:03:28 +00:00
2021-07-25 14:20:59 +00:00
mkdir /mnt/efi # for EFI partition /dev/sdX1
2022-02-16 20:10:29 +00:00
```
2020-10-07 07:41:58 +00:00
2022-02-16 20:10:29 +00:00
#### Discard and ssd options and are for ssd disks only
#### If using encryption, change **/dev/sdX2** to **/dev/mapper/MainPart**
```bash
mount -o defaults,noatime,discard,ssd,subvol=@home /dev/sdX2 /mnt/home
2020-10-07 07:41:58 +00:00
2022-02-16 20:10:29 +00:00
mount -o defaults,noatime,discard,ssd,subvol=@varlog /dev/sdX2 /mnt/var/log
2020-10-07 07:41:58 +00:00
2022-02-16 20:10:29 +00:00
mount -o defaults,noatime,discard,ssd,subvol=@tmp /dev/sdX2 /mnt/tmp
2020-10-07 07:41:58 +00:00
2022-02-16 20:10:29 +00:00
mount -o defaults,noatime,discard,ssd,subvol=@snapshots /dev/sdX2 /mnt/snapshots
2020-10-07 07:41:58 +00:00
2022-02-16 20:10:29 +00:00
mount /dev/sdX1 /mnt/efi
2021-07-25 14:20:59 +00:00
```
2022-02-16 20:10:29 +00:00
# 3. Install Arch Linux
#### Select the mirror to be used if not updated with reflector on start
2021-07-25 14:20:59 +00:00
```bash
2022-02-16 20:10:29 +00:00
vim /etc/pacman.d/mirrorlist
2021-07-25 14:20:59 +00:00
```
2022-02-16 20:10:29 +00:00
#### Install base system:
##### This command can be customized with additional packages (**btrfs-progs is necessary to let the system boot up from btrfs partition !**)
2021-07-25 14:20:59 +00:00
```bash
2022-02-16 20:10:29 +00:00
pacstrap /mnt/ base base-devel git btrfs-progs efibootmgr linux linux-headers linux-firmware mkinitcpio dhcpcd bash-completion sudo
```
#### Generate fstab:
##### Use genfstab with -U parameter if no encryption
2021-07-25 14:20:59 +00:00
```bash
2022-02-16 20:10:29 +00:00
genfstab /mnt >> /mnt/etc/fstab
```
####
# 4. Configure the system
#### Switch to installed system root user
2020-10-06 15:03:28 +00:00
2021-07-25 14:20:59 +00:00
```bash
2022-02-16 20:10:29 +00:00
arch-chroot /mnt /bin/bash
2021-07-25 14:20:59 +00:00
```
2022-02-16 20:10:29 +00:00
#### Setup system clock
2021-07-25 14:20:59 +00:00
```bash
ln -s /usr/share/zoneinfo/Europe/Warsaw /etc/localtime
2021-11-09 08:48:56 +00:00
hwclock --systohc --utc
2021-07-25 14:20:59 +00:00
```
2022-02-16 20:10:29 +00:00
#### Set the hostname in `/etc/hostname`
2021-07-25 14:20:59 +00:00
```test
2022-02-16 20:10:29 +00:00
myhostname
```
#### Edit vconsole in `/etc/vconsole.conf`
2021-07-25 14:20:59 +00:00
```text
KEYMAP=pl
FONT=Lat2-Terminus16.psfu.gz
FONT_MAP=8859-2
2022-02-16 20:10:29 +00:00
2021-07-25 14:20:59 +00:00
```
2022-02-16 20:10:29 +00:00
#### Setup locale
##### Uncomment pl\_PL.UTF-8 in /etc/locale.gen and then run:
2021-07-25 14:20:59 +00:00
```bash
2022-02-16 20:10:29 +00:00
locale-gen
```
#### Update locale in `etc/locale.conf`
2021-07-25 14:20:59 +00:00
```text
LANG=en_US.UTF-8
LC_COLLATE=pl_PL.UTF-8
LC_MEASUREMENT=pl_PL.UTF-8
LC_MONETARY=pl_PL.UTF-8
LC_NUMERIC=pl_PL.UTF-8
LC_TIME=pl_PL.UTF-8
2022-02-16 20:10:29 +00:00
```
#### Hosts in `/etc/hosts`
2021-07-25 14:20:59 +00:00
```text
127.0.0.1 localhost
::1 localhost
127.0.1.1 myhostname.localdomain myhostname
2022-02-16 20:10:29 +00:00
```
#### Now create empty (with 0 size) swap file:
2022-02-18 19:33:36 +00:00
#### Create separate subvolume for swapfile. This subvolume is needed to let you make snapshot of `/`, which would not be possible with any file in it with CoW disabled!
2022-02-16 20:10:29 +00:00
```
2022-02-18 19:33:36 +00:00
btrfs su create /swap
2022-02-16 20:10:29 +00:00
chattr +C /swap
```
#### Copy on Write should always be disabled on swap file, so it will be done in the next step
2021-07-25 14:20:59 +00:00
```bash
touch /swap/swapfile
```
2022-02-16 20:10:29 +00:00
#### Check if C attribute is enabled (should be already if created in folder with disabled CoW attribute)
2021-07-25 14:20:59 +00:00
```bash
lsattr /swap/swapfile'
```
2022-02-16 20:10:29 +00:00
#### If not then disable CoW for swapfile manually:
2021-07-25 14:20:59 +00:00
```bash
chattr +C /swap/swapfile
2022-02-16 20:10:29 +00:00
```
#### Expanding empty file to 4GiB swap file
2021-07-25 14:20:59 +00:00
```bash
dd if=/dev/zero of=/swap/swapfile bs=1024K count=4096
2022-02-16 20:10:29 +00:00
2021-07-25 14:20:59 +00:00
chmod 600 /swap/swapfile
2022-02-16 20:10:29 +00:00
```
#### Format the swap file.
2021-07-25 14:20:59 +00:00
```bash
mkswap /swap/swapfile
2022-02-16 20:10:29 +00:00
```
#### Turn swap file on.
2021-07-25 14:20:59 +00:00
```bash
swapon /swap/swapfile
2022-02-16 20:10:29 +00:00
```
#### You also need to update `/etc/fstab` to mount swapfile on boot:
2021-07-25 14:20:59 +00:00
```text
/swap/swapfile none swap sw 0 0
2022-02-16 20:10:29 +00:00
```
#### Set password for root
2021-07-25 14:20:59 +00:00
```bash
passwd
```
2022-02-16 20:10:29 +00:00
#### Add real user an set password for him
2021-07-25 14:20:59 +00:00
```bash
useradd -m MYUSERNAME
2022-02-16 20:10:29 +00:00
2021-07-25 14:20:59 +00:00
passwd MYUSERNAME
2022-02-16 20:10:29 +00:00
```
### Configure mkinitcpio with modules needed for the initrd image
2021-07-25 14:20:59 +00:00
```bash
vim /etc/mkinitcpio.conf
```
2022-02-16 20:10:29 +00:00
#### Add 'keyboard', 'keymap', 'encrypt' and 'btrfs' to HOOKS before filesystems:
```
HOOKS=(base udev autodetect keyboard keymap modconf block btrfs filesystems keyboard fsck)
```
#### Add btrfsck to binaries:
```
BINARIES=(btrfsck)
```
#### **With encryption:** also add encrypt before btrfs:
2021-07-25 14:20:59 +00:00
```text
HOOKS=(... keyboard keymap block encrypt btrfs ... filesystems ...)
2022-02-16 20:10:29 +00:00
```
######
#### Regenerate initrd images
2021-07-25 14:20:59 +00:00
```bash
mkinitcpio -P
2022-02-16 20:10:29 +00:00
```
# 5. Install bootloader
#### Setup grub (UEFI)
2021-07-25 14:20:59 +00:00
```bash
pacman -S grub efibootmgr os-prober dosfstools mtools
2022-02-16 20:10:29 +00:00
```
#### -------------encryption only---------------------
#### edit `/etc/default/grub`
2021-07-25 14:20:59 +00:00
```text
2022-02-16 20:10:29 +00:00
GRUB_ENABLE_CRYPTODISK=y
2021-07-25 14:20:59 +00:00
```
2022-02-16 20:10:29 +00:00
#### Find UUID (UUID for /dev/sdX2) of crypto partition so we can add it to grub config
2021-07-25 14:20:59 +00:00
```bash
2022-02-16 20:10:29 +00:00
blkid
2021-07-25 14:20:59 +00:00
```
2022-02-16 20:10:29 +00:00
#### Now set this line including proper UUID in place of "<device-UUID>":
#### (temporarly you can use /dev/sdX2 in place of "UUID=<device-UUID>" and change it later easy in gui mode)
##### edit `/etc/default/grub`
2021-07-25 14:20:59 +00:00
```text
2022-02-16 20:10:29 +00:00
GRUB_CMDLINE_LINUX="cryptdevice=UUID=< device-UUID > :MainPart:allow-discards"
2021-07-25 14:20:59 +00:00
```
2022-02-16 20:10:29 +00:00
##### allow-discards is only for ssd to let trim work with encryption enabled
#### Generate key so grub don't ask twice for password on boot
2021-07-25 14:20:59 +00:00
```bash
dd bs=512 count=4 if=/dev/random of=/crypto_keyfile.bin iflag=fullblock
chmod 600 /crypto_keyfile.bin
chmod 600 /boot/initramfs-linux*
cryptsetup luksAddKey /dev/sdX2 /crypto_keyfile.bin
```
2022-02-16 20:10:29 +00:00
#### If you change name of key file there is need to add kernel parameter like cryptkey=rootfs:path
#### Crypto\_keyfile.bin is the default name that kernel will guess anyway
#### Now add this file to `/etc/mkinitcpio.conf`
2021-07-25 14:20:59 +00:00
```text
2022-02-16 20:10:29 +00:00
FILES=(/crypto_keyfile.bin)
2021-07-25 14:20:59 +00:00
```
2022-02-16 20:10:29 +00:00
2021-07-25 14:20:59 +00:00
then run:
2022-02-16 20:10:29 +00:00
2021-07-25 14:20:59 +00:00
```bash
mkinitcpio -P
```
2022-02-16 20:10:29 +00:00
#### -------------encryption end---------------------
#### Install grub for
2021-07-25 14:20:59 +00:00
```bash
grub-install --target=x86_64-efi --efi-directory=/efi --bootloader-id=GRUB
grub-mkconfig -o /boot/grub/grub.cfg
2022-02-16 20:10:29 +00:00
```
#### Exit new system
2021-07-25 14:20:59 +00:00
```bash
exit
2022-02-16 20:10:29 +00:00
```
#### Unmount all partitions
2021-07-25 14:20:59 +00:00
```bash
swapoff -a
umount -R /mnt
```
2022-02-16 20:10:29 +00:00
#### Reboot into the new system, don't forget to remove the pendrive
2021-07-25 14:20:59 +00:00
```bash
reboot
```
2022-02-16 20:10:29 +00:00
#### or
2021-07-25 14:20:59 +00:00
```bash
shutdown now
2022-02-16 20:10:29 +00:00
```
### 6. Addtitional tips:
#### Install AUR helper (git and base-devel packages needed to do so):
```
git clone https://aur.archlinux.org/yay.git
cd yay
makepkg -si
```
#### To get proper locale and keymap, check:
2021-07-25 14:20:59 +00:00
```bash
2022-02-16 20:10:29 +00:00
localectl status
2021-07-25 14:20:59 +00:00
```
2022-02-16 20:10:29 +00:00
#### On KDE plasma , also set settings > ... > keyboard layout && regional settings