5.6 Creating Subvolumes and Snapshots

5.6.1 Cloning Virtual Machine Images and Linux Containers

The top level of a btrfs file system is a subvolume consisting of a named b-tree structure that contains directories, files, and possibly further btrfs subvolumes that are themselves named b-trees that contain directories and files, and so on. To create a subvolume, change directory to the position in the btrfs file system where you want to create the subvolume and enter the following command:

# btrfs subvolume create subvolume_name

Snapshots are a type of subvolume that records 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 btrfs file system means that snapshots are quick to create, 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 the snapshot.

The following table shows how 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 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 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 the time that the snapshot was taken. By default, the operating system mounts the parent btrfs volume, which has an ID of 0, unless you use set-default to change the default subvolume. If you set a new default subvolume, the system will mount that subvolume instead in future. You can override the default setting by specifying either of the following mount options:

Mount Option

Description

subvolid=snapshot_ID

Mount the subvolume or snapshot specified by its subvolume ID instead of the default subvolume.

subvol=pathname/snapshot_path

Mount the subvolume or snapshot 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.

When you have rolled back a file system by mounting a snapshot, you can take snapshots of the snapshot itself to record its state.

When you no longer require a subvolume or snapshot, use the following command to delete it:

# btrfs subvolume delete subvolume_path

Note

Deleting a subvolume deletes all subvolumes that are below 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.

5.6.1 Cloning Virtual Machine Images and Linux Containers

You can use a btrfs file system to provide storage space for virtual machine images and Linux Containers. The ability to quickly clone files and create snapshots of directory structures makes btrfs an ideal candidate for this purpose. For an example of using the snapshot feature of btrfs to implement Linux Containers, see Section 9.2, “Configuring Operating System Containers”.