Ramblings of an aging IT geek
← Ramblings of an aging IT geek
linux

a btrfs snapshot bought back my saturday

A botched package upgrade left a box unbootable, and a pre-upgrade btrfs snapshot rolled it back in under a minute.

A terminal showing a Linux shell prompt

I did the thing you are not supposed to do on a Saturday morning: a full system upgrade on a box I actually care about, before coffee, without reading the changelog. A kernel and an initramfs tool both moved, the regeneration silently produced something wrong, and the next reboot dropped me into a dracut emergency shell with the rootfs unmountable.

Six months ago this would have been my entire weekend. A rescue USB, chroot, hunt for the broken package, hope. Instead it was about forty seconds, because the box runs btrfs and my upgrade hook takes a read-only snapshot of the root subvolume before anything touches the system.

btrfs subvolume snapshot -r / /.snapshots/pre-upgrade-$(date +%F)

From the emergency shell I mounted the top-level volume, deleted the broken default subvolume, and set the snapshot as the new default. Reboot, and I was back exactly where I had been before the upgrade, every config and package intact.

btrfs subvolume set-default <snap-id> /mnt/top

The clever part is not btrfs being magical. It is that the rollback is atomic and near-instant because the snapshot already existed and shared all its blocks with the live system. No copying, no chroot gymnastics, no praying. I retried the upgrade that afternoon, read the changelog this time, regenerated the initramfs by hand, and it was fine.

If you run btrfs and you are not snapshotting before upgrades, you are leaving the best feature on the table. It is one line in a pacman or apt hook, and one day it gives you your Saturday back.