Mounting a BTRFS Subvolume

Mounting a BTRFS Subvolume
Porque Podemos

Let's say there's a BTRFS filesystem happily running things in one location and it becomes desirable to use the same BTRFS filesystem in a different location. Traditionally would require the administrator to create a new partition or logical volume, format it, and mount it in the new location or use a bind mount. However, with BTRFS it's possible to mount a subvolume, with filesystem options, providing the ability to use a subvolume like a partition.

The Setup

  • An existing BTRFS filesystem mounted at /srv
  • A subvolume for "machines" will be created at /srv/machines
  • The resulting subvolume will be mounted at /var/lib/machines.
The Filesystem Overview
# btrfs filesystem show
        Total devices 2 FS bytes used 21.81GiB
        devid    1 size 232.88GiB used 13.01GiB path /dev/sdb
        devid    2 size 232.89GiB used 13.01GiB path /dev/sdc
Make The Subvolume
# btrfs subvolume create /srv/machines
Validate The Subvolume
# btrfs subvolume show /srv/machines/
        Name:                   machines
        Parent UUID:            -
        Received UUID:          -
        Creation time:          2017-12-01 22:30:23 -0600
        Subvolume ID:           264
        Generation:             10531
        Gen at creation:        15
        Parent ID:              5
        Top level ID:           5
        Flags:                  -
Mount The Subvolume
# mount -t btrfs -o subvol=machines,defaults,nodatacow /dev/disk/by-uuid/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX /var/lib/machines
Confirm The Subvolume is Mounted & Functional
# grep machines /proc/mounts  
/dev/sdb /var/lib/machines btrfs rw,relatime,space_cache,subvolid=264,subvol=/machines 0 0


Bonus | Make The Subvolume Mount Persistent

A service file will be created /etc/systemd/service/var-lib-machines.mount and will allow the mounted subvolume to survive reboots.

Create a Systemd Mount Service File
Description=Virtual Machine and Container Storage

Reload The Systemd Daemon
# systemctl daemon-reload 
Enable The Mount
# systemctl enable var-lib-machines.mount
Restart The Mount
# systemctl start var-lib-machines.mount
Verify The Mount is Operational
# systemctl status var-lib-machines.mount 
● var-lib-machines.mount - Virtual Machine and Container Storage
   Loaded: loaded (/lib/systemd/system/var-lib-machines.mount; static; vendor preset: enabled)
   Active: active (mounted) since Sat 2017-11-25 04:48:33 UTC; 1 weeks 4 days ago
    Where: /var/lib/machines
    Tasks: 0 (limit: 8192)
   Memory: 0B
      CPU: 0


With these simple steps, a BTRFS subvolume has been mounted in from /srv/machines to /var/lib/machines and potentially been setup as a persistent mount using a mount unit file.