Btrfs – subvolumes, snapshots, quotas, and more
Btrfs can do so much more than the older XFS technology. This includes subvolumes, snapshots, and quotas. Btrfs subvolumes are an exceptional tool that allows users to create multiple snapshots or subfilesystems within a single Btrfs filesystem. These subvolumes are displayed as distinct directories in the filesystem hierarchy, but they utilize the same storage space and can be managed independently.
The flexibility and versatility of subvolumes make them ideal for various purposes, such as creating backups or isolating different parts of the filesystem for easier management. Snapshots are particularly useful since they offer read-only copies of the filesystem at a specific point in time. With snapshots, users can restore files or entire subvolumes to a previous state or easily create replicable backups that can be moved to another system.
Subvolumes also enable users to manage disk space more efficiently. For example, users can create a subvolume for a specific application or project and restrict its disk usage to a certain amount to prevent it from using up too much space on the filesystem. Additionally, subvolumes can be used to implement access controls by assigning different permissions to different subvolumes or creating separate subvolumes for different users or groups. This recipe will go over how to do all of this.
Getting ready
This recipe will require a Btrfs filesystem, and will use the data filesystem created in the previous recipe for the examples.
How to do it…
In this recipe, we will do the following:
- Create a subvolume in /data and mount it.
- Set a quota on the subvolume.
- Create a snapshot.
- Enable compression.
Creating a subvolume is straightforward and is done by using the btrfs command and specifying the full path to the subvolume:
btrfs subvolume create /data/vol1
Once created, simply add it to fstab, this time declaring the subvolume name in the fourth column. This is seen in the following screenshot, where /data/vol1 is mounted using subvolume vol1:
Figure 4.15 – Subvolume in fstab
Now that we have the subvolume mounted, let’s add a quota to limit it to 5 GB. To do this, we first need to enable quotas for the volume. This is done with the following command:
btrfs quota enable /data
Next, we need to assign a quota-group limit to the subvolume. This will restrict the subvolume to the size defined. This is done using the limit option, as seen in the following command:
btrfs qgroup limit 5g /data/vol1
You can see what quotas are defined in a volume using the btrfs qgroup show command with the -reF option:
btrfs qgroup show -reF /data/vol1
The command and its output are shown in the following screenshot:
Figure 4.16 – Set quotas
Now that we have a quota set, let’s create a snapshot for backups. We do need a place for the backups, so let’s create a subvolume for the backups with the following command:
btrfs subvolume create /data/backup
Btrfs snapshots are highly useful copies of a Btrfs filesystem, capturing a specific point in time. Through a seamless copy-on-write process, these snapshots separate any changes made to the filesystem from the snapshot itself. This makes them ideal for different purposes, such as creating backups, testing software configurations, and providing an effortless way to undo system updates. Additionally, backups can be created rapidly and with minimal storage space, as the snapshots only store the differences between the current state of the filesystem and the state at the moment the snapshot was taken. To create a Btrfs snapshot, you can use the btrfs subvolume snapshot command, specifying the subvolume you want to snapshot and the name and location of the snapshot:
btrfs subvolume snapshot /data/vol1 /data/backup/vol1_backup1
This command creates a read-only snapshot of the /data/vol1 subvolume and saves it as a separate subvolume in the /data/backup directory.
Don’t worry if you’ve already taken a snapshot – you can easily revert the filesystem to its exact state at that time using the powerful Btrfs rollback feature. With Btrfs snapshot rollback, you can restore your Btrfs filesystem to a previous state by simply selecting a snapshot. Rolling back to a snapshot discards all changes made to the filesystem since the snapshot was taken and restores the filesystem to the exact state it was in when the snapshot was created.
To roll back a Btrfs snapshot, you can use the btrfs subvolume snapshot command with the -r option, which specifies that the snapshot should be used for a rollback:
btrfs subvolume snapshot -r /data/backups/vol1_backup1 /data/vol1
This command rolls back /data/vol1 to the state it was in when the /data/backup/vol1_backlup1 snapshot was created.
It is important to note that rolling back a snapshot will discard any changes made to the filesystem since the snapshot was taken. However, this feature is extremely useful when you want to fully revert the filesystem to a previous state. Btrfs snapshots provide a simple and effective solution for data management and protection, allowing for effortless backup creation and easy restoration of prior filesystem versions.