Linux Format

Safely swap your filesystem to Btrfs

The Btrfs filing system is well-supported, reliable and packed with modern features, so Michael Reed will show you how to make good use of it.

-

Btrfs is a filing system (see Roundup LXF283) that can be used as an alternativ­e to the more commonly known ext4. You might think it’s risky to entrust all of your data to a relatively unknown filesystem, but in fact the Btrfs project was started in 2007, and it’s the default filesystem in some of the major distributi­ons such as Fedora and SUSE Linux. So, it’s regarded as stable, and in fact, Btrfs has a lot of features that are designed to increase the safety of file storage. Having said all that, for all of the potential advantages, there isn’t anything extra that you have to learn for normal, day-to-day use.

Butter my FS

Why switch over to Btrfs? Well, unlike the ext4 filesystem, it uses ‘copy on write’ file storage, which opens up a number of intriguing possibilit­ies. For instance, you can make a snapshot of your root filesystem, make some changes, decide you don’t like the results and then roll everything back to how it was originally. You could even make a massive change such as adding a different desktop environmen­t and then back out of it later on. This might sound like snapshotti­ng within a VM, and that’s because it offers the same freedom to experiment. You can also make snapshots of other subvolumes, such as the one containing your home directory, or you could apply snapshotti­ng and rollbacks to server-related files.

You can also work with individual files and directorie­s. Think of all of the times that you’ve had to work on a copy of a file while preserving the original file. Under Btrfs, a clone acts just like a copy of a file, and that’s what it appears to be. However, initially, no matter how big the file is, the clone takes up practicall­y no extra disk space. It’s also created almost instantly. The filesystem only begins to store extra informatio­n on the hard disk when you start making changes to that copy.

For example, on a Btrfs volume, you can type code cp --reflink example.txt example_old.txt , to create a copy of the file example.txt. Notice that the code --reflink endcode option is an addition to the standard Linux copy command. However, unlike a regular copy of a file, this new file takes up practicall­y no space until you start making changes to it. This applies to massive files such as virtual machines and video files. So, it’s possible to, almost instantly, make a copy of a large file such as a virtual machine file, make some changes and then swap everything back if you don’t like the outcome.

As we said, you don’t have to know much technical stuff to start using Btrfs. If you can install Linux to a Btrfs-formatted partition, then you can install Timeshift using the package manager and start making use of full system snapshots and rollbacks from a GUI.

If you’re not quite ready to fully commit to a Btrfs system yet, many of the techniques that we’ll look at could be applied to working with Btrfs volumes that have been added to an existing system. If you want to play around with Btrfs and learn what it can do, you might want to install it into a virtual machine (VM) while following this tutorial.

Butter up Ubuntu

We’ll assume that you have some familiarit­y with installing Ubuntu to begin with. That way, we can just concentrat­e on the difference­s when making it a Btrfsbased installati­on. With that in mind, follow the normal Ubuntu installati­on procedure. Options such as language selection and normal and minimal installati­on stay the same as usual. The page where we do something different is called Installati­on Type.

As ever, be aware that, if you’re doing this on real hardware, you could easily wipe partitions. In other

words, be careful!

When you get to this page choose Something Else to invoke manual rather than automatic partitioni­ng. Assuming that you’re working with a blank hard drive, select New Partition Table .... Use the + icon to create the partitions you need within the free space. The partition layout has to be different to the typical one for an ext4based system, and you’ll need two extra partitions in addition to the usual system and swap file ones.

This typical scheme should work in most cases: reserved BIOS boot area (50MB), EFI System Partition (550MB), Linux Swap (32,000MB) and Btrfs (remaining free space, ‘/’ mount point). Click Install Now and carry on with the installati­on as normal. If you’re using real kit as opposed to a VM, measure twice and cut once!

Getting buttered

Assuming you now have a working Ubuntu installati­on, let’s explore some Btrfs commands to see how it works. If you type sudo btrfs subvolume list / into a terminal, you should end up with an output along these lines:

ID 256 gen 88852 top level 5 path @

ID 257 gen 88852 top level 5 path @home

We’ll start by looking at the command we typed in itself. Like other utilities such as Docker, the main Btrfs management command uses subcommand­s for easier organisati­on. So, we’ve invoked the btrfs command and selected the subvolume subcommand, and list is the Btrfs command. We’ve included the path / as we’re interested in subvolumes connected to the root of the filesystem. Having said that, we’ve now touched on a fundamenta­l concept of Btrfs: subvolumes.

Subvolumes have some of the characteri­stics of a folder and some of the characteri­stics of a partition. In this case, Ubuntu has, by default, created two subvolumes and called them @ and @home. These are the root and home subvolumes, which contain the Linux installati­on files and the user home directorie­s, respective­ly. If you think back to the partitioni­ng that we did, we only created one partition for the system; both of these subvolumes reside on that partition. That leads us to one of the advantages which subvolumes have over partitions: the free space on the partition is shared between the root and the home subvolumes, but they can be backed up and replaced separately.

Buttering subvolumes

To get further into what subvolumes can do, we’ll create one. Type mkdir /snapshots to set up an empty directory. Type sudo btrfs subvolume snapshot /home / snapshots , and you should receive a message telling you that the snapshot has been created.

We can verify this in two ways. First, if we reuse the command we typed before, sudo btrfs subvolume list / , we should see the new subvolume listed below the existing ones. Second, if you use cd to navigate into the

/snapshots directory, we should see a directory called home, and within that the contents of the user’s home directory. This was created quickly and takes up very little space on the disk. As we created the snapshot with read/write permission­s, we could begin to alter the files within the snapshot. At that point, disk space will be consumed to store the difference­s between the original file and the version inside the snapshot, but it’s impossible for changes to the snapshot version of the file to affect the original file, or vice versa.

Note, we couldn’t have taken a snapshot of a single file or directory within the home directory, because we can only snapshot subvolumes such as home rather than directorie­s or files. However, you can make clones of those files and directorie­s by using the cp command with the --reflink option as we explained earlier.

For a more impressive example, we can snapshot the entire root subvolume just as easily with the command

sudo btrfs subvolume snapshot / /snapshots/root . One slight difference when snapshotti­ng the root filesystem is that you have to supply a name (root, in this case), because Btrfs can’t call a snapshot /.

Note that subvolumes within other subvolumes are

no more difficult to understand than folders within other folders in normal use, and we could have put the subvolume containing the snapshots into our home folder, for example. However, subvolumes aren’t included in a snapshot of the parent subvolume.

In this case, we created a subvolume by creating a snapshot. This gave us a subvolume with some files within it to examine. But you can create an empty subvolume for your own purposes very simply.

Let’s say that you needed a storage location for a server that you planned to run on your Linux box. You could create a subvolume for that by typing sudo btrfs subvolume create /myserver . As we’ve said before, subvolumes feature some of the properties of both a partition and a directory. In this case, this empty subvolume can be used to hold data for a server applicatio­n, and the subvolume can take advantage of Btrfs features such as snapshotti­ng. So, rather than ‘backing up’ the data, which takes up time and disk space, you can snapshot it before making any changes you’re unsure about. We’ll get onto making an actual backup that leverages the facilities of Btrfs in a moment.

To apply a quota to this subvolume, enable quotas for the subvolume: sudo btrfs quota enable /myserver . Then set a 100GB quota with sudo btrfs qgroup limit 100G /myserver . This should stop an errant server getting too out of control.

Backing up the butter

If you relied purely on snapshotti­ng to do your backing up, you’d run into trouble if the disk with the snapshots were damaged. There’s nothing to stop you from using standard back-up tools on a Btrfs system, but Btrfs has facilities of its own that can help with backups. To begin with, most back-up tools can do incrementa­l backups. These only contain the difference­s since the last backup, to save time and disk space. Unfortunat­ely, those tools have to make guesses about what changes you’ve actually made to individual files since the last backup. However, Btrfs, by its nature, knows what these changes were, and can therefore be more efficient.

btrbk (https://github.com/digint/btrbk) is a wellknown Btrfs back-up tool designed for complex backup regimes. It’s actually a Perl script and it’s controlled by a config file that you alter for your needs. There’s a list of similar tools on the Btrfs wiki (https://btrfs.wiki.kernel. org). However, we present here a simple manual backup example, using a few commands, that will at least give you a concrete backup of a snapshot. This is the routine: create a snapshot of a subvolume (the home subvolume in this case) and then send it to a subvolume on another medium for safety.

For this example, we’ll assume that you have a Btrfs volume mounted on /mnt/extdrive . If we simply attempted to use the snapshot subcommand to specify a destinatio­n on another volume, such as an external drive, it would fail because snapshots don’t normally span volumes and devices. Start by creating a

subvolume to contain the backup with sudo btrfs subvolume create /mnt/extdrive/mybacks . Type sudo

btrfs subvolume create ~/mysnaps to create a subvolume to store snapshots in locally.

Create a snapshot of the home subvolume sudo btrfs subvolume snapshot -r /home ~/mysnaps . The -r option makes the snapshot read only. To make an actual copy of that data, we use the send and receive Btrfs subcommand­s. One command pipes the data to the other command. In our case:

sudo btrfs send mysnaps/home/ | sudo btrfs receive / mnt/extdrive/mybacks will do the job. Note the use of the pipe operator between the commands. Sure enough, if you inspect the contents of /mnt/extdrive/mybacks/, you’ll see that it contains everything that your home directory contains. However, these are actual files that take up space on the disk, and they’re complete copies that can be used independen­tly of the system that they were created on.

Buttershif­t

Timeshift is a GUI front end that enables you to create a complete system snapshot and roll it back if you want to revert the system back to an earlier state. All of the hard work of fiddling with configurat­ion files is done for you. So, you might make a quick snapshot of the system before adding a package, just in case. Bear in mind, you can make massive system changes such as trying out a different desktop environmen­t and then put everything back how it was afterwards.

You can specify, when snapshotti­ng, whether or not you want the subvolume containing your home directory to be included. If so, you can browse the snapshot and examine earlier versions of files within the your home directory structure using the Timeshift interface. Either way, you can decide whether or not to preserve your existing home directory when restoring from a backup. The utility also has some limited features for setting up scheduled snapshotti­ng. So, you can tell it that you want it to snapshot the entire system every hour, every day or every boot.

As before, we’ll assume that you’re using a Ubuntu derivative with a working Btrfs setup for these examples. You can install Timeshift using the package management front-end of your choice or from the command line ( sudo apt install timeshift ). The first time you run Timeshift you’ll see a configurat­ion Wizard. The wizard is well designed with informatio­n about what the options actually do at the bottom of the screen, and at the end, you’re given a plain English summary of what options you’ve chosen. By and large, you can safely accept the default options. On the second page of the wizard, you can specify the automatic snapshot frequency and the number of snapshots to keep. You can also specify whether to include the home subvolume in the snapshot by default.

We recommend doing a reboot once Timeshift is installed and configured. Following this, there are a number of possible circumstan­ces where snapshots could be working, but booting into them will not, and for this reason, it’s worth giving it a quick test.

To prove that it’s working, find a tool that isn’t installed on your system. We chose the KDE text editor Kate. Try to run it, just to check that it isn’t installed. Snapshot the system by clicking the Create icon on the Timeshift GUI so that we have a copy of the system without Kate installed. Install Kate by typing sudo apt install kate into a terminal. Check that it’s installed and can be launched. To test that snapshots are bootable, click the snapshot that you created earlier and click the Restore icon, then reboot the system. When you restore the system, another snapshot is created for extra safety. If everything’s working as it should, it should no longer be possible to run Kate, because you’ve restored the system to the point before you installed it.

Butter is better

Btrfs is ready for mainstream use. The snapshot and rollback features are killer features and easy to use, particular­ly if you’re using Timeshift, and you soon start to wonder how you managed without them.

We’ve only touched on its key features. Btrfs is also capable of advanced functions such as spreading a volume between more than one physical device, transparen­t file compressio­n and online growth and shrinking of a volume. At time of writing it looks like it’s set to become the default filesystem in the majority of desktop Linux distributi­ons. If you haven’t tried it before, it might be time to sample what a cutting-edge Linux filesystem can do for your Linux life?

 ?? ?? Examine your history of system snapshots in the Timeshift GUI, and browse the files from that snapshot by right-clicking a snapshot.
Examine your history of system snapshots in the Timeshift GUI, and browse the files from that snapshot by right-clicking a snapshot.
 ?? ??
 ?? ?? You don’t have to know much to use Btrfs from the desktop, but we’re going to start at the command line to illustrate how some of the features work.
You don’t have to know much to use Btrfs from the desktop, but we’re going to start at the command line to illustrate how some of the features work.
 ?? ?? The man pages contain a wealth of Btrfs informatio­n.
The man pages contain a wealth of Btrfs informatio­n.
 ?? ?? The setup wizard of Timeshift is well designed, with an informatio­n area at the bottom explaining the program’s various options.
The setup wizard of Timeshift is well designed, with an informatio­n area at the bottom explaining the program’s various options.
 ?? ?? Sure enough, the local snapshot of the home volume contains our home directory, including all hidden files and metadata such as the date and timestamps.
Sure enough, the local snapshot of the home volume contains our home directory, including all hidden files and metadata such as the date and timestamps.
 ?? ?? Creating the partitions needed for an Btrfs-based Ubuntu setup. Note that you’ll need a minimum of four partitions.
Creating the partitions needed for an Btrfs-based Ubuntu setup. Note that you’ll need a minimum of four partitions.

Newspapers in English

Newspapers from Australia