Convert Hetzner Cloud Server to BTRFS

Converts the default ext4 root FS that comes with Hetzner’s Cloud Servers to BTRFS. Tested with Debian 12 “bookworm”.

  1. (optional) Create a snapshot / backup

  2. “Enable rescue & power cycle” in control panel

  3. Login to host:

    ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null root@${HOST}
    

    For security reasons, do not -o StrictHostKeyChecking=no when using a password-based login.

  4. Check ext4 filesystem:

    e2fsck -fvy /dev/sda1
    
  5. Convert to BTRFS:

    btrfs-convert --csum xxhash /dev/sda1
    
  6. (optional) Check FS:

    btrfsck /dev/sda1
    
  7. Mount (and enable space_cache v2):

    mount -o discard=async,space_cache=v2 /dev/sda1 /mnt
    
  8. (optional) Check FS part 2:

    btrfs scrub start -B /dev/sda1
    
  9. Create subvolume for root:

    btrfs subvolume create /mnt/@rootfs
    
  10. Move data to new root subvolume:

    mv /mnt/* /mnt/@rootfs/
    
  11. Make new root the default volume:

    btrfs subvolume set-default /mnt/@rootfs
    
  12. Umount FS:

    umount /mnt
    
  13. Remount with new default subvolume:

    mount -o discard=async,space_cache=v2 /dev/sda1 /mnt
    

    (Remounting to make sure Grub detects location of /boot properly.)

  14. Update /etc/fstab:

    sed -r -i 's!UUID=.*\s/\s!# &!' etc/fstab
    echo "UUID=$(blkid -s UUID -o value /dev/sda1) / btrfs discard=async,noatime 0 1" >>etc/fstab
    
  15. Mount various (virtual) filesystems:

    mount --bind /dev /mnt/dev \
      && mount --bind /sys /mnt/sys \
      && mount --bind /proc /mnt/proc \
      && mount /dev/sda15 /mnt/boot/efi/ \
      && mount --bind /sys/firmware/efi/efivars /mnt/sys/firmware/efi/efivars
    
  16. Update boot loader:

    chroot /mnt update-grub
    chroot /mnt grub-install
    
  17. Reboot:

    reboot
    
  18. Login to host

  19. (possibly with some delay) Clean up / optimize

    Warning

    Consider delaying this step to ensure BTRFS is working properly. After this cleanup, you can no longer easily revert back to ext4 using btrfs-convert --rollback.

    btrfs subvolume delete /ext2_saved/
    btrfs balance start -m .
    btrfs balance start -mconvert=dup .
    
  20. (optional, possibly with some delay) use duplicate data

    Tip

    This duplicates all data, allowing data be repaired automatically from the duplicates as needed. You are generally better of investing in proper backups but it does not hurt to enable this when the server has enough spare space. Use -dconvert=single to revert.

    Warning

    Warning from previous step applies.

    btrfs balance start -dconvert=dup .