Raspberry Pi 4 - Ubuntu 20.04 w/Btrfs root

ยท 283 words ยท 2 minute read

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 ๐Ÿ”—

  1. Flash Ubuntu on the SD card as usual.
  2. Mount the partitions and go to where the partitions are mounted, example on Fedora.
    cd /run/media/${USER}
    
  3. Tell boot to read from a Btrfs file system on next boot
    sudo sed -i 's/ext4/btrfs/' system-boot/cmdline.txt
    
  4. 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
    
  5. Unmount the file system.
    sudo umount system-boot
    sudo umount writable
    
  6. 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
    
  7. Put the SD card in the Raspberry Pi and boot. Voila! You have a Btrfs root file system.
  8. Log into the Raspberry Pi for some cleanup.
  9. Remove the old ext2 backup.
    sudo btrfs sub del /ext2_saved
    
  10. 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.