Creating Subvolumes and Snapshots
The top level of a Btrfs file system is a subvolume consisting of
a named b-tree structure containing directories, files, and
possibly further btrfs
subvolumes that are also
named b-trees, each of which also contains directories and files,
and so on.
To create a subvolume, change directories to the position in the
btrfs
file system for which you want to create
the subvolume, then run the following command:
sudo btrfs subvolume create subvolume_name
Snapshots are a type of subvolume that record the contents of
their parent subvolumes at the time that you took the snapshot. If
you take a snapshot of a btrfs
file system and
do not write to it, the snapshot records the state of the original
file system and forms a stable image from which you can make a
backup. If you make a snapshot writable, you can treat it as a
alternate version of the original file system. The copy-on-write
functionality of a btrfs
file system means that
snapshots are created quickly and consume very little disk space
initially.
Note:
Taking snapshots of a subvolume is not a recursive process. If you create a snapshot of a subvolume, every subvolume or snapshot that the subvolume contains is mapped to an empty directory of the same name inside that snapshot.
The following table describes commands to use to perform some common snapshot operations.
Command | Description |
---|---|
btrfs subvolume snapshot pathname pathname / snapshot_path |
Create a snapshot snapshot_path of a parent subvolume or snapshot specified by pathname, for example: btrfs subvolume snapshot /mybtrfs /mybtrfs/snapshot1 |
btrfs subvolume list pathname |
List the subvolumes or snapshots of a subvolume or snapshot that is specified by pathname, for example: btrfs subvolume list /mybtrfs Note: You can use this command to determine the ID of a subvolume or snapshot. |
btrfs subvolume set-default ID pathname |
By default, mount the snapshot or subvolume that is specified by its ID instead of the parent subvolume, for example: btrfs subvolume set-default 4 /mybtrfs |
btrfs subvolume get-default pathname |
Displays the ID of the default subvolume that is mounted for the specified subvolume, for example: btrfs subvolume get-default /mybtrfs |
You can mount a Btrfs subvolume as though it were a disk device.
If you mount a snapshot instead of its parent subvolume, you
effectively roll back the state of the file system to when the
snapshot was taken. By default, the operating system mounts the
parent Btrfs volume, which has an ID of 0
,
unless you use the set-default option to change
the default subvolume. If you set a new default subvolume, the
system mounts that subvolume going forward. You can override the
default setting by specifying any of the mount
options that are described in the following table.
Mount Option | Description |
---|---|
subvolid= snapshot-ID |
Mount the subvolume or snapshot that is specified by its subvolume ID instead of the default subvolume. |
subvol= pathname / snapshot_path |
Mount the subvolume or snapshot that is specified by its pathname instead of the default subvolume. Note: The subvolume or snapshot must be located in the root of the Btrfs file system. |
After you have rolled back a file system by mounting a snapshot, you can take snapshots of the snapshot to record its state.
When you no longer require a subvolume or snapshot, you can delete it as follows:
sudo btrfs subvolume delete subvolume_path
Deleting a subvolume deletes all of the subvolumes under it in the
b-tree hierarchy. For this reason, you cannot remove the topmost
subvolume of a btrfs
file system, which has an
ID of 0
.
For information about using the snapper command to create and manage Btrfs snapshots, see Automating File System Snapshots With the Snapper Utility.
Attention:
Snapshots record the state of the file system at a moment in time. As such, it is not possible to guarantee file system integrity for transactional processes that may have been in operation at the time when a snapshot was taken. While utilities like the snapper command may help to capture before and after snapshots for particular operations, such as when using the dnf command, these snapshots are still unaware of other processes that may be running on the system at the same time. If you have processes that may have intensive I/O or memory usage, such as database or middleware applications, you should stop these or ensure that all activity is complete before taking a snapshot to help to reduce the likelihood of data integrity or file system corruption issues within the snapshot.