, 2 min read

Switching from ext4 to btrfs

Hearing all these wonder stories about btrfs I decided to give btrfs a try. Additionally I encrypted my SSD using LUKS.

The relevant entries in the Arch WIKI are: Setting up LUKS and btrfs. Here is the list of commands I used.

1. I saved my data from SSD using tar and saved tar-file on hard-disk:

cd /
time tar cf /mnt/c/home/klm/ssd1.tar bin boot etc home opt root srv usr var

Alternatively one can use cp -a to copy directories with symbolic links. Option -a is the same as -dR --preserve=all.

2. I used gparted to partition the SSD, creating /boot and / (root). For /boot I directly enabled btrfs in gparted.

3. Encrypt the partition.

cryptsetup -y -v -s 512 luksFormat /dev/sdc2

Then open it and give it an arbitrary name:

cryptsetup luksOpen /dev/sdc2 cryptssd

4. Create filesystem using btrfs. This is the reason for all this effort. Although this is the easiest.

mkfs.btrfs /dev/mapper/cryptssd

5. Adapt /etc/fstab, e.g., with genfstab -U /mnt >> /mnt/etc/fstab

UUID=3b8bb70c-390a-4a9e-9245-ea19af509282       /       btrfs   rw,relatime     0 0
UUID=a8d6c185-0769-4ec5-9088-2c7087815346       /boot   ext4    rw,data=ordered,relatime        0 2

Check results with lsblk -if.

6. Chroot into new system using arch-chroot and put GRUB on it, as usual. Add required directories, first.

mkdir boot proc run sys tmp

Then edit the configuraton file for GRUB:

vi /etc/default/grub
GRUB_CMDLINE_LINUX_DEFAULT="cryptdevice=UUID=5a74247e-75e8-4c05-89a7-66454f96f974:cryptssd:allow-discards root=/dev/mapper/cryptssd"

grub-install --target=i386-pc /dev/sdb
grub-mkconfig -o /boot/grub/grub.cfg

Keyword :allow-discards after cryptssd is for SSD TRIM. For a mechanical hard drive this keyword should be omitted.

7. Install btrfs utilities and programs on new system if not already installed. Add the btrfs executable to the initial RAM disk, i.e., set the entry for BINARIES.

pacman -S btrfs-progs

vi /etc/mkinitcpio.conf
. . .
BINARIES="/usr/bin/btrfs"
. . .
mkinitcpio -p linux

8. Extracting back from the tar-file.

time tar xf /mnt/c/home/klm/ssd1.tar

9. Adding TRIM for SSD:

systemctl status fstrim
systemctl enable fstrim.timer
systemctl start fstrim.timer

Show timers (like crontab -l):

systemctl --type=timer

10. A simple benchmark, as indicated by above time before tar, does not show any performance benefits so far. But performance was not the main motivator, but rather the added functionality, especially taking snapshots.