Fedora BTRFS snapshot tool

Given that /boot on Fedora is by default an ext4 partition, this makes BTRFS snapshots on Fedora a not so smooth experience. If you restore a snapshot you may have trouble with kernels due to /boot not being restored.

Last time I restored a BTRFS snapshot, I was dropped into an emergency shell. To fix this, I had to boot from an older kernel and run updates.

My idea is not complicated: Use rsync to backup the contents of /boot and restore it so no kernel trouble upon restore while keeping the advantages of btrfs.

So given a disk layout like mine:

nvme0n1                                       259:0    0 232.9G  0 disk  
├─nvme0n1p1                                   259:1    0   600M  0 part  /boot/efi
├─nvme0n1p2                                   259:2    0     1G  0 part  /boot
└─nvme0n1p3                                   259:3    0 231.3G  0 part  
  └─luks-<uuid>                                  253:0    0 231.3G  0 crypt /home

The backup of /boot will be either on a separate subvolume or on the /boot partition in a specific folder.

What are your thoughts? (I hope this is the correct place to ask, or should I move this somewhere else?)

1 Like

I think restoring/fixing the boot and/or efi partitions (maybe even the firmware entries) should all be a part of whatever script or process you use to rollback Btrfs. Just my two cents.

I think you should be able to run something like KERN=$(ls -v /lib/modules | tail -n 1); kernel-install add $KERN /lib/modules/$KERN/vmlinuz after your Btrfs rollback rather than keeping another copy of everything in another directory. You’d just need to make that part of your rollback script.

1 Like

The snapshot takes effect after a reboot, the /boot should be restored before that. So I should chroot into the snapshot?

Also will this command regenerate GRUB entries?

Since Fedora keeps multiple kernel versions around, is this something you are having problems with in practice? If so, it seems easier to increase the number of kernels fedora keeps by default unless you restore very old btrfs root snapshots.

This helps, but I have concerns because last time I was dropped into an emergency shell due to booting latest kernel after restoring a couple days old snapshot.

When that happens, just reboot and pick an older one off the list :wink:

Yeah. Sorry, you would have to use chroot. So it would be a little more complicated if you wanted to restore things that way. But everything you need should already be present. You shouldn’t need to maintain another copy of things. Also, before you chroot, I think you would need to run something like mount --rbind <path-to-current-boot> <path-to-boot-under-snapshot> (and you might need/want to bind mount a few other things like /proc as well). The kernel-install ... command will create the /boot/loader/entries/*.conf files (i.e. the grub menu entries). But you’d probably want to delete the old ones first and then re-create all the ones that are available under your snapshot with something like:

for KERN in /lib/modules/*/vmlinuz; do 
   kernel-install add $(cut -d '/' -f 4 <<< $KERN) $KERN

Note that none of this is tested. I do something sort of similar. But I use ZFS for my root filesystem and I use only one partition for my kernels, initramfses, and bootloader (systemd-boot). So my system is quite different.

1 Like

Yes. That should work as long as the rollback isn’t too far. If the rollback is too far, the corresponding kernel modules under /lib/modules/<kernel-version> might not be present. Most of the time, people probably won’t want/need to roll their system back that far though. And increasing the number of older kernels that you keep on your boot partition just in case isn’t a bad idea either.

1 Like

That’s what I did.

Imagine the case of someone else not aware of this situation. They boot and ends up in an emergency shell, and without your root account enabled you are stuck there until you force a shutdown.

The broken rescue shell is a different problem. There has been some discussion about fixing that: Changes/FixRescueMode - Fedora Project Wiki

1 Like

Sure, it isn’t a user-friendly general solution. But it is far easier than having to chroot in after the rollback and run a script.

If you want a more general solution, you should consider not using a separate /boot. Then when you do the rollback, everything will get rolled back together.

The chroot is a proposed idea (probably a better idea than rsync), I gravitate more to rsync incremental backups stored in a separate subvolume (excluding /boot/efi).

When you want to restore, you call the script and specify the snapshot to restore. It first copies the current /boot, loads the btrfs snapshot, syncs the appropriate /boot version back and reboots.

Is there any specific reason for not having /boot on BTRFS?
Grub2 supports booting from BTRFS.

I don’t know the specific reasons that Fedora chose to do this although I am sure they are documented somewhere.

I can say that there are some limitations when use a btrfs /boot since grub can only read btrfs, it can’t write it. For example, you can’t set grub to remember your last selected boot entry.

1 Like

As far as I know, the comment of @chrismurphy is still up to date as far as it concerns your question.

Unfortunately, on this low level, developments and improvements can take some time, because issues are complicated and interrelated with much.

1 Like