Raspberry PI BTRFS root

btrfs on Raspberry Pi

raspberry pi

Conversion

  1. Boot the pi at least once with a normal flash of raspbian
  2. From the pi:
    1. Add btrfs to /etc/initramfs-tools/modules
    2. Run update-initramfs -u -k $(uname -r)
    3. Append /boot/config.txt to include initramfs initrd.img-$(uname -r) followkernel
    4. Replace ext4 with btrfs in /etc/fstab and /boot/cmdline.txt
  3. From a different system: btrfs-convert -L /dev/sd
  4. From the pi:
    1. btrfs subvol delete /ext2_saved
    2. btrfs fi defrag -r /
    3. btrfs balance start /

Script

#!/bin/bash
set -euo pipefail

grep -q "btrfs" /etc/initramfs-tools/modules || echo btrfs >> /etc/initramfs-tools/modules

update-initramfs -u -k $(uname -r)

sed -i '/followkernel/d' /boot/config.txt
echo "initramfs initrd.img-$(uname -r) follow kernel" >> /boot/config.txt

sed -i 's/ext4/btrfs/g' /etc/fstab
sed -i 's/ext4/btrfs/g' /boot/cmdline.txt
sed -i 's/ quiet//g' /boot/cmdline.txt
sed -i 's/ splash//g' /boot/cmdline.txt
sed -i 's/ rootwait=[^ ]*//;s/$/ rootwait=2/' /boot/cmdline.txt

apt install btrfs-progs

echo "
# default 3000 => 30s , we use 5 minutes = 30000
vm.dirty_expire_centisecs = 30000 
vm.vfs_cache_pressure=500
vm.swappiness=100
vm.dirty_background_ratio=1
vm.dirty_ratio=50
" > /etc/sysctl.d/50-sdcard.conf

# Enable zram-swap
apt install zram-tools
sed -i '/^ALLOCATION=.*$/ALLOCATION=1024/' /etc/default/zramswap

# Disable dphys-swapfile
apt purge dphys-swapfile

References