Working With a Btrfs root File System

Important:

In Oracle Linux 8, the Btrfs file system and all the features that are documented in this chapter are supported in the Unbreakable Enterprise Kernel (UEK) release only. Working with Btrfs file system features requires that you boot the system by using UEK R6 or later.

You can create a Btrfs root file system during an installation. To do so, you must boot the system by using UEK R6 or later.

To find out the ID of the parent of the root file system subvolume, use the following command:

sudo btrfs subvolume list /

Note in the output of the previous command that the top level ID is set with an ID of 5. The top level of the file system is effectively the root of the file system and can be used to access all the subvolumes within the file system:

ID 256 gen 1591 top level 5 path boot
ID 258 gen 1591 top level 5 path root
ID 259 gen 1514 top level 5 path home
ID 262 gen 1514 top level 258 path var/lib/portables"

In the previous example, the installation root file system subvolume has an ID of 258. The subvolume with ID 258 (root) is mounted as /. The default subvolume (root) with ID 258 is mounted as the active root file system.

The mount command shows the device that's mounted as the root file system and indicates the subvolume ID (258):

sudo mount|grep 'on / '
/dev/sda2 on / type btrfs (rw,relatime,seclabel,space_cache,subvolid=258,subvol=/root)

Note that the top-level file system in the previous output isn't mounted by default. To mount the top-level file system volume, use the following commands:

sudo mkdir /mnt
sudo mount -o subvolid=5 /dev/sda2 /mnt

If you list the contents of /mnt, you can view each of the subvolumes within the file system volume, including the root subvolume, for example:

ls /mnt

Running the previous command displays the following output:

boot  home  root

Note that the contents of / and /mnt/root are identical, as s shown in the following example where a file (foo) that's created in /mnt/root is also visible in /:

sudo touch /mnt/root/foo
ls /

Running the previous command displays the following output:

bin   boot  dev  etc  foo  home  instroot  lib  lib64  media  mnt  opt  
proc  root  run  sbin  srv  sys    tmp  usr  var

Now, list the contents of /mnt/root:

sudo ls /mnt/root
bin   boot  dev  etc  foo  home  instroot  lib  lib64  media  mnt  opt  
proc  root  run  sbin  srv  sys    tmp  usr  var

Remove the /foo directory, then list the contents of /:

sudo rm -f /foo
sudo ls /
bin   boot  dev  etc  home  instroot  lib  lib64  media  mnt  opt  
proc  root  run  sbin  srv  sys    tmp  usr  var

List the contents of /mnt/root again:

sudo ls /mnt/root
bin   boot  dev  etc  home  instroot  lib  lib64  media  mnt  opt  
proc  root  run  sbin  srv  sys    tmp  usr  var

Creating Snapshots of the root File System

To take a snapshot of the current root file system:

  1. Mount the top level of the root file system on a suitable mount point.

    sudo mount -o subvolid=5 /dev/sda2 /mnt
  2. Change directories to the mount point, then take the snapshot. In the following example, the install subvolume is currently mounted as the root file system:

    sudo cd /mnt
    sudo mkdir -p /mnt/snapshots
    sudo btrfs subvolume snapshot root snapshots/root_snapshot_1
  3. Change directories to / and unmount the top level of the file system.

    sudo cd /
    sudo umount /mnt 

    The list of subvolumes now includes the newly created snapshot.

    sudo btrfs subvolume list /
    ID 256 gen 1332 top level 5 path boot
    ID 258 gen 1349 top level 5 path root
    ID 259 gen 1309 top level 5 path home
    ID 261 gen 1309 top level 258 path var/lib/portables
    ID 264 gen 1348 top level 5 path snapshots/root_snapshot_1

Mounting Alternate Snapshots as the root File System

If you want to roll back changes to your system, you can mount a snapshot as the root file system by specifying its ID as the default subvolume:

sudo btrfs subvolume set-default 264 /

To ensure that the GRUB command line does not overwrite your settings, make the following update:

current_grub_kernel=$(sudo grubby --default-kernel);
sudo grubby --remove-args="rootflags=subvol=root" --update-kernel $current_grub_kernel

Reboot the system for the changes to take effect.

Check that the snapshot subvolume ID and subvolume is mounted as the root filesystem:

sudo mount|grep 'on / '
/dev/sda2 on / type btrfs (rw,relatime,seclabel,space_cache,subvolid=264,subvol=/snapshots/root-snapshot1)

Deleting Snapshots of the root File System

Note:

A snapshot cannot be deleted if it is set as the default ID for a subvolume. Deleting a snapshot while it is in use as the root file system may cause system failure and requires a hard physical reset. Before deleting a snapshot that is set as the default subvolume for the root File System, change the default ID and reboot the system, for example:

sudo btrfs subvolume set-default 258 /
reboot

To delete a snapshot, do the following:

  1. Mount the top level of the file system, for example:

    sudo mount -o subvolid=5 /dev/sda2 /mnt
  2. Change directories to the mount point and delete the snapshot.

    $ sudo cd /mnt
    sudo btrfs subvolume delete snapshots/root-snapshot1
  3. Change directories to / and unmount the top level of the file system.

    sudo cd /
    sudo umount /mnt 

    The list of subvolumes now does not include snapshots/root-snapshot1.

    sudo btrfs subvolume list /
    ID 256 gen 1332 top level 5 path boot
    ID 258 gen 1349 top level 5 path root
    ID 259 gen 1309 top level 5 path home
    ID 261 gen 1309 top level 258 path var/lib/portables