Saturday, August 27, 2022

BTRFS on the Raspberry Pi

Given all the interest in BTRFS as a file system I thought I would see what works on the Pi and what doesn't. In x64 Linux its a simple matter of installing an extra package or two and support is baked into the kernel. With the Raspberry Pi, well its not so good.


My experimental rigs

I tried with a couple of setups. One was a Pi3 with a PiDrive and the other was a Pi4 4GB with a Seagate Expansion 2TB. In both cases I am booting off the SD card with Raspberry Pi OS (64 bit) installed and the Pi's are headless (no screen or keyboard).


Moving the root partition

I wrote about how to move the root partition to external storage in 2016. Those instructions still work today if the external storage is not bootable. A link to that is HERE

Unfortunately the PiDrive nor my Seagate Expansion would boot after putting an image on them, so its back to the old method of booting off the SD card and moving the root partition onto the external storage. This works for ext3 and ext4 formatted drives, but NOT for btrfs formatted drives.


Why would you want to?

"Btrfs is a copy on write filesystem for Linux aimed at implementing advanced features while focusing on fault tolerance, repair and easy administration" to quote kernel.org.

Btrfs offers a number of advantages over the ext4 file system which is most commonly used. It supports RAID configurations, scrubbing, deduplication, defragmentation, snapshots and can detect bit rot. Basically a similar feature set to OpenZFS. However unlike OpenZFS its natively supported in Linux.


Btrfs on external storage

It is possible to partition the external storage as Linux and format it as btrfs on the Pi without issue. I first got the Pi booting off its SD card and then installed the btrfs-progs package with the "sudo apt install btrfs-progs" command. I then partitioned the external drive as above. To format it you use the command "sudo mkfs.btrfs /dev/sda1" assuming your external disk was sda and you only had 1 partition on it. The "sudo mke2fs -f btrfs" command doesn't work.

You can even have the Pi mount the external drive in btrfs format by adding it to /etc/fstab. They recommend you use the UUID or PARTUUID in fstab these day so the easy way to find that out is to do a "lsblk -o name,size,type,partuuid" command. I then cut the uuid and paste it into fstab. You can still use the old way of specifying /dev/sda1 in fstab if you prefer but if you have more than one external drive it can lead to issues.

If you change /boot/config.txt to specify a btrfs partition it will hang when you boot. I have read that it leads to a kernel panic but because I didn't have a screen plugged into the Pi's while testing can't confirm what it does, other than it definitely hangs.

There are various hacks to have the root partition in btrfs format, which fail when there is a kernel update so at the moment the nearest you can reliably get is a drive formatted for btrfs and mounted - just not the root partition.

Btrfs support has been requested a number of times of the Raspberry Pi foundation but I believe the issue is around the customized kernel the Pi runs not having btrfs support when it boots, possibly due to the memory needed. If enough people request it I am sure the foundation would address the issue(s), especially now we're 64 bit and have more memory on the Pi4 and Pi5, but they haven't so far.


Update: Feb 2024

For some reason the Pi5 refuses to mount a btrfs volume using the "sudo mount /dev/sda /mnt/exthdd" format. I managed to get it to work using a UUID and it also works on the Pi4. The command would be "sudo mount UUID=xxx /mnt/exthdd" and you can also use this format in /etc/fstab. To get the UUID if you missed it when doing the mkfs.btrfs command above you can do a "sudo btrfs filesystem show" command and it will list the btrfs disks and their UUID. In my case I had two drives in what btrfs calls single (ie one big volume) so I mount the disk rather than a partition.

No comments: