A Btrfs field test and workshop
In the Hot Seat
Volume @
Having a root filesystem on Btrfs and using apt-btrfs-snapshot can take the fear out of major system upgrades or extensive program installations – but with one stumbling block. Because the Btrfs used for this article was a converted system that does not follow the same conventions as a freshly installed Ubuntu 12.04, you need to make some adjustments. A test up front with
apt-btrfs-snapshot supported plugins
checks the system's snapshot capabilities and indicates that a specific subvolume (@) is expected for the root filesystem. This is not configurable, but the workaround is quite easy:
- Reboot the system into a rescue environment
- Create a subvolume in the top-level volume named
@ - Mount the top-level volume and the new root subvolume
- Copy the entire root filesystem to the new subvolume
Now change the entry in /etc/fstab so the root subvolume @ is mounted in the future:
/dev/mapper/maibutter-root / btrfs defaults,subvol=@ 0 1
A call to update-grub in Ubuntu 12.04 extends the boot command line in GRUB to rootflags=subvol=@.
Safe Updates
After making these preparations and rebooting, you can install no fewer than 62 updates for this test system (Figure 13). A list of existing Btrfs subvolumes shows that a new snapshot was created (with snapshot creation time in its name), and you can use the command that came with the Apt plugin for snapshots (Figure 14):
Figure 13: A number of updates have already accumulated. A snapshot created directly before installing the update allows for a simple rollback if you are not convinced of the update's stability.
btrfs sub list -p / apt-btrfs-snapshot list
Figure 14: After updating a series of packages, a new snapshot with a descriptive name is available. If necessary, you can quickly roll back the update.
If the /boot directory is a separate ext2 filesystem on an extra partition, as was the case with the test system, all the files there are excluded from a Btrfs snapshot and a rollback. If you want to include these files in the snapshot to make kernel upgrades easier to undo, this directory must reside directly on the root subvolume. A current GRUB 2 makes this possible.
Back to the Future
If the latest update causes more harm than good, a rollback is now possible. To do this, you need to modify the /etc/fstab file to point to the desired snapshot, rather than to subvol=@ (here, this is @apt-snapshot-2012-07-23_08:52:34). One grub-update and one reboot later, the system reverts to the snapshot status and once again offers to update (Figure 15).
Figure 15: Typing /proc/self/mountinfo confirms that the snapshot is now mounted. An update of all packages again shows 62 possible updates.
You can easily delete the obsolete old root volume (called @) and rename the snapshot volume currently in use; you just need to mount the top-level volume because it contains all subvolumes in the form of folders. Thanks to its subvolume ID of 0, the top-level volume is easy to access. Deleting an unused subvolume and subsequently renaming a subvolume can be done on the fly (Figure 16):
mount -o subvolid=0 /dev/dm-0 /mnt && cd /mnt btrfs sub delete @ mv @apt-snapshot-2012-07-23_08\:52\:34/ @ cd && umount /mnt
Figure 16: After mounting the top-level volume, you can delete the old root subvolume and rename the snapshot volume. Everything is done on the fly.
Now you just need to check the entry for the mounted subvol=@ in /etc/fstab and issue an update-grub. This step completes the rollback after the system update.
Buy this article as PDF
(incl. VAT)
Buy ADMIN Magazine
Subscribe to our ADMIN Newsletters
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Most Popular
Focus On Self-Hosting
• Running Your Own Matrix Server with Synapse
• Reliable Network Storage with TrueNAS Community Edition
• Self-Hosted File Syncing with Seafile
• Wiring the Modern Stack with Node-RED
• Self-Hosted Collaboration with Forgejo
• Self-Hosted PaaS with Coolify
• Build and Host Docker Images
• Self-Hosted Pritunl VPN Server with MFA
Support Our Work
ADMIN content is made possible with support from readers like you. Please consider contributing when you've found an article to be beneficial.
