Skip to content

Btrfs Subvolumes

Btrfs Subvolumes are a powerful feature for organizing data and enabling snapshot-based backups. A subvolume acts as a virtual directory within a Btrfs filesystem, allowing independent mounting, snapshots, and permission management. This section covers how to create, manage, and leverage subvolumes for structured storage administration.


Creating Subvolumes

To create a new subvolume, use the btrfs subvol create command. Subvolumes are typically created under the root subvolume (which is automatically created when the filesystem is initialized).

Example:

sudo btrfs subvol create /mnt/btrfs/data_home
This creates a subvolume named data_home under /mnt/btrfs.

Note: Subvolumes must reside within a Btrfs filesystem. Ensure the mount point is correctly specified.


Managing Subvolumes

Use the btrfs subvol command to rename, delete, or inspect subvolumes.

Renaming a Subvolume:

sudo btrfs subvol rename /mnt/btrfs/old_name /mnt/btrfs/new_name

Deleting a Subvolume:

sudo btrfs subvol delete /mnt/btrfs/deprecated_data
Caution: This permanently removes the subvolume and its contents.

Listing Subvolumes:

sudo btrfs subvol list /mnt/btrfs
This shows all subvolumes under the specified mount point, including their IDs and paths.


Mounting Subvolumes

Subvolumes can be mounted independently using the mount command with the subvol option.

Mounting a Subvolume:

sudo mount -o subvol=data_home /dev/sdb1 /mnt/btrfs_mount
This mounts the data_home subvolume from /dev/sdb1 to /mnt/btrfs_mount.

For persistent mounts, add an entry to /etc/fstab:

/dev/sdb1 /mnt/btrfs_mount btrfs subvol=data_home 0 0


Snapshots and Subvolumes

Subvolumes are essential for creating snapshots. Use btrfs subvol snapshot to generate read-only copies.

Creating a Snapshot:

sudo btrfs subvol snapshot -r /mnt/btrfs/data_home /mnt/btrfs/data_home_snapshot
The -r flag ensures the snapshot is read-only.

Restoring from a Snapshot:
Unmount the original subvolume and replace it with the snapshot:

sudo umount /mnt/btrfs/data_home
sudo mount -o subvol=data_home_snapshot /dev/sdb1 /mnt/btrfs_mount


Permissions and Access Control

Subvolumes inherit standard Unix permissions but can be isolated for security. Use chown or chmod to adjust ownership and access rights. For granular control, combine subvolumes with Access Control Lists (ACLs):

sudo setfacl -m u:admin:rwx /mnt/btrfs/data_home


Troubleshooting Tips

  • Permission Errors: Ensure the user has execute permissions on the parent directory of the subvolume.
  • Snapshot Failures: Verify the source subvolume exists and the target path is writable.
  • Mount Issues: Check if the subvolume ID matches the one in /etc/fstab or the mount command.

Key takeaways

  • Subvolumes enable logical organization and independent management of Btrfs data.
  • Use btrfs subvol create and btrfs subvol snapshot for structured storage workflows.
  • Mount subvolumes via mount -o subvol=... or /etc/fstab for persistent access.
  • Snapshots on subvolumes simplify backups and rollbacks.
  • Permissions and ACLs can isolate subvolumes for security or access control.