/ btrfs

Mounting a BTRFS Subvolume

btrfs | subvolume | nspawn | mount
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
Label: none  uuid: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
        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/
/srv/machines
        Name:                   machines
        UUID:                   YYYYYYYY-YYYY-YYYY-YYYY-YYYYYYYYYYYY
        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:                  -
        Snapshot(s):
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

Profit!


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
[Unit]
Description=Virtual Machine and Container Storage

[Mount]
What=/dev/disk/by-uuid/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
Where=/var/lib/machines
Type=btrfs
Options=subvol=machines,defaults,nodatacow
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
     What: /dev/disk/by-uuid/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
    Tasks: 0 (limit: 8192)
   Memory: 0B
      CPU: 0

TTFN

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.