How to convert a root file system on a Raspberry Pi 4 b running Ubuntu 20.04 64 bit to Btrfs in a few simple steps.
Why ๐
First, why should I care about doing this.
- Btrfs is SSD-aware, even on a SD card. The copy on write file system will ensure that your writes will be spread out and decrease wear leveling.
- You can enable a CPU friendly compression, zstd, that decreases the amount of data written to your SD card.
How ๐
- Flash Ubuntu on the SD card as usual.
- Mount the partitions and go to where the partitions are mounted, example on Fedora.
cd /run/media/${USER}
- Tell boot to read from a Btrfs file system on next boot
sudo sed -i 's/ext4/btrfs/' system-boot/cmdline.txt
- Update
/etc/fstab
with new file system, and ensure minimum writes to SD card.sudo sed -E -i 's|(.*)ext4.*|\1btrfs compress=zstd:1,noatime,nodiratime 0 0|' writable/etc/fstab
- Unmount the file system.
sudo umount system-boot sudo umount writable
- Convert the file system on the SD card. If the SD cards
ext4
partition are/dev/sda2
then the command will be as follows. Let the command finish.sudo btrfs-convert -p /dev/sda2
- Put the SD card in the Raspberry Pi and boot. Voila! You have a Btrfs root file system.
- Log into the Raspberry Pi for some cleanup.
- Remove the old
ext2
backup.sudo btrfs sub del /ext2_saved
- Defrag and compress already existing OS.
sudo btrfs fi def -czstd -r -v /
Congratulations! Now you have a Btrfs root file system with compression. Writes will take less space, and wear leveling is down. Note that you also have saved space by running a df -h /
. Enjoy a SD card that will last longer than you are used to.