Btrfs Subvolume Layout for VMs and Databases


“Btrfs is great for snapshotting and rolling back data. By planning your subvolume layout, you can fine tune your snapshot, protecting virtual machines and databases from system-wide rollbacks. Subvolumes are a great way to manage important data.”

Flat Layout

When you carry out a typical Ubuntu set up the usage of btrfs on one partition, the installer creates and mounts two subvolumes: @ (positioned at /) and @house (positioned at /house). These are fixed at boot by means of /and many others/fstab. This is named a flat format and makes snapshots simple to regulate.

Snapshots and Rollbacks

Assuming the Ubuntu usual btrfs setup, mount your machine partition at /mnt:

$ sudo mount /dev/sdX /mnt.

You’ll see your two subvolumes there:

The complete root filesystem is contained inside @. To take a snapshot of @ referred to as @.snapshot, we do:

To rollback a subvolume, merely transfer/rename the previous subvolume, then transfer/rename the snapshot you want to use onto the previous subvolume’s location:

$ sudo mv /mnt/@ /mnt/@.damaged
$ sudo mv /mnt/@.snapshot /mnt/@

After a reboot, you’re going to be the usage of the snapshot you took.

Note sooner than reboot: /and many others/fstab has entries for @ and @house like so:

UUID=XXXXX  /   btrfs   mount_options,subvol=@  0 0
UUID=XXXXX  /house   btrfs   mount_options,subvol=@house  0 0

If your /and many others/fstab entries have subvolid within the mount choices like this, that possibility should be got rid of.

UUID=XXXXX  /   btrfs   mount_options,subvol=@,subvolid=XXXX    0 0

A subvolume identification is a singular identifier for a subvolume. When you exchange a subvolume, the subvolume indexed in that mount level can have the similar identify, however it’s going to have a unique identification. If the identification isn’t like the only in /and many others/fstab, it’s going to no longer mount. It is protected to make use of handiest names when mounting subvolumes in /and many others/fstab as long as your subvolumes are uniquely named.


When the usage of Mariadb, your database recordsdata will are living in “/var/lib/mariadb.” By developing a brand new subvolume for this information, you offer protection to your database from being touched in a machine rollback when reverting @ to a prior state.

First, mount your top-level btrfs:

$ sudo mount /dev/sdX /mnt

Create the brand new subvolume:

$ sudo btrfs subvolume create /mnt/@mariadb

Then mount it:

$ sudo mount -o  subvol=@mariadb /dev/sdX /var/lib/mariadb

Add this access on your fstab, and set up the database:

$ sudo vim /and many others/fstab

UUID=XXXXX  /var/lib/mariadb    btrfs   mount_options,subvol=@mariadb   0 0

$ sudo apt set up mariadb-server


Similarly, isolate all KVM machines into one subvolume at “/var/lib/libvirt/machines:”

$ sudo btrfs subvolume create /mnt/@kvm
$ sudo mount -o subvol=@kvm /dev/sdX /var/lib/libvirt/machines
$ sudo vim /and many others/fstab

UUID=XXXX       /var/lib/libvirt/machines   btrfs   mount_options,subvol=@kvm   0 0

If you have already got information to your digital device and database directories, you’ll be able to again it up, exchange the unique listing with the subvolume you made, after which repair the knowledge from the backup. Don’t omit to mend permissions the place essential as all newly created subvolumes are owned by way of root:

$ sudo chown -R mysql:mysql /var/lib/mariadb


Now, when you wish to have to rollback @ to a prior state, your database and digital machines shall be preserved in separate subvolumes. The probabilities for subvolume layouts are unending, and what I’ve demonstrated right here is only one manner to make use of btrfs to give protection to and maintain your information. Whatever your use case, the versatility of btrfs subvolumes can reinforce the options of your server or workstation.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More