• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

How to Create and Mount Btrfs Snapshots

by admin

Btrfs is an open-source, general-purpose file system for Linux. The name derives from the use of B-trees to store internal file system structures. Different names are used for the file system, including “Butter F S” and “B-tree F S.” Development of Btrfs began at Oracle in 2007, and now a number of companies (including Red Hat, Fujitsu, Intel, SUSE, and many others) are contributing to the development effort. Btrfs is included in the mainline Linux kernel.

Btrfs Snapshots

Btrfs subvolumes can be snapshotted and cloned, which creates additional B-trees. A snapshot starts as a copy of a subvolume taken at a point in time. You can make a snapshot writable and use it as an evolving clone of the original subvolume. Or you can use the snapshot as a stable image of a subvolume for backup purposes or for migration to other systems. Snapshots can be created quickly and they initially consume very little disk space.

Use the btrfs subvolume snapshot command to create a writable/readable snapshot of a subvolume. The following example creates a snapshot of the SV1 subvolume:

# btrfs subvolume snapshot /btrfs/SV1 /btrfs/SV1/SV1-snap
Create a snapshot of '/btrfs/SV1' in '/btrfs/SV1/SV1-snap'

Use the btrfs subvolume snapshot -r option to create a read-only snapshot:

# btrfs subvolume snapshot -r /btrfs/SV1 /btrfs/SV1-rosnap
Create a readonly snapshot of '/btrfs/SV1' in '/btrfs/SV1-rosnap'

The snapshots appear as a regular directory when the ls command is used. Snapshots also appear in the output of the btrfs subvolume list command.

# btrfs subvolume list /btrfs
ID 257 gen 10 top level 5 path SV1
ID 258 gen 9 top level 257 path SV1/SV1-snap
ID 259 gen 10 top level 5 path SV1-rosnap

Taking a Snapshot of a File

You can use the “cp –reflink” command to take a snapshot of a file. With this option, the file system does not create a new link pointing to an existing inode, but instead creates a new inode that shares the same disk blocks as the original copy. The new file appears to be a copy of the original file but the data blocks are not duplicated. This allows the copy to be almost instantaneous and also saves disk space. As the file’s content diverges over time, its amount of required storage grows. One restriction is that this operation can work only within the boundaries of the same file system and within the same subvolume.

The following example copies a file by using the cp –reflink command. The space used is given both before and after the copy operation. Note that the space used does not increase.

# df -hP /btrfs
Filesystem       Size  Used   Avail  Use%   Mounted on
/dev/sda         10G   23M    8.0G    1%    /btrfs
# cp --reflink /btrfs/SV1/vmlinuz-3.10.0-693.17.1.el7.x86_64 /btrfs/SV1/copy_of_vmlinuz
# df -h /btrfs
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda         10G   23M  8.0G   1% /btrfs

Mounting a Subvolume or Snapshot

By default, Linux mounts the parent Btrfs volume, which has an ID of 0. In this example, the following mount command was issued before creating any subvolumes and snapshots:

# mount /dev/sdb /btrfs

The subvolume SV1 was created in /btrfs. The ls command shows the subvolume:

# ls -l /btrfs
total 0
drwxr-xr-x. 1 root root 84 Mar 29 11:01 SV1

The following example copies files into SV1, creates a snapshot of SV1, and verifies that both the subvolume and the snapshot contain the same files:

# cp -p /boot/vmlinuz-3.10.0-693.17.1.el7.x86_64 /btrfs/SV1
# btrfs sub snapshot /btrfs/SV1 /btrfs/SV1-snap
Create a snapshot of '/btrfs/SV1' in '/btrfs/SV1-snap'
# ls /btrfs/SV1*
/btrfs/SV1:
vmlinuz-3.10.0-693.17.1.el7.x86_64

/btrfs/SV1-snap:
vmlinuz-3.10.0-693.17.1.el7.x86_64

If you unmount /btrfs and remount it, the parent Btrfs volume is mounted by default:

# ls /btrfs/
SV1  SV1-snap
# umount /btrfs
# mount /dev/sda /btrfs/
# ls /btrfs/
SV1  SV1-snap

You can, however, mount a btrfs subvolume or snapshot as though it were a disk device. If you mount a snapshot instead of its parent subvolume, you effectively roll back the state of the file system to the time that the snapshot was taken. The following example copies a file to SV1 so that the content is different from SV1-snap:

# cp testfile /btrfs/SV1
# ls -l /btrfs/SV1*
/btrfs/SV1:
total 5760
-rw-r--r--. 1 root root      19 Mar 29 11:44 testfile
-rwxr-xr-x. 1 root root 5890720 Jan 25 20:26 vmlinuz-3.10.0-693.17.1.el7.x86_64

/btrfs/SV1-snap:
total 5756
-rwxr-xr-x. 1 root root 5890720 Jan 25 20:26 vmlinuz-3.10.0-693.17.1.el7.x86_64

To mount a subvolume or snapshot, you must first determine the ID number of the subvolume that you want to mount. Use the btrfs subvolume list command to display the ID numbers. In the following example, the ID of the root subvolume is 257:

# btrfs sub list /btrfs
ID 257 gen 12 top level 5 path SV1
ID 258 gen 9 top level 5 path SV1-snap

Use the btrfs “subvolume set-default” command to set the default subvolume of a file system. For example, to mount the SV1 Btrfs subvolume, which has an ID of 257:

# btrfs subvolume set-default 257 /btrfs

You then need to unmount and remount the Btrfs file system. The root level then contains the contents of the SV1 subvolume and the root subvolume is no longer visible:

# ls -l /btrfs
total 5760
-rw-r--r--. 1 root root      19 Mar 29 11:44 testfile
-rwxr-xr-x. 1 root root 5890720 Jan 25 20:26 vmlinuz-3.10.0-693.17.1.el7.x86_64

You can also use the “-o subvolid” option to the mount command to mount the root subvolume or a subvolume or snapshot. For example, to mount the root subvolume:

# umount /btrfs
# mount -o subvolid=5 /dev/sda /btrfs
# ls -l /btrfs
total 0
drwxr-xr-x. 1 root root 84 Mar 29 11:44 SV1
drwxr-xr-x. 1 root root 68 Mar 29 11:17 SV1-snap
How to create and mount Btrfs file system (explained with examples)
How to Resize / Expand a Btrfs Volume / Filesystem

Filed Under: CentOS/RHEL 6, CentOS/RHEL 7, Linux

Some more articles you might also be interested in …

  1. Understanding /etc/security/limits.conf file
  2. How to Find the Original Installation OS Version in CentOS/RHEL
  3. engrampa Command Examples in Linux
  4. CentOS / RHEL 7 : How to boot into Rescue Mode or Emergency Mode
  5. How to Configure Automatic Package Updates on the Server in CentOS/RHEL 8
  6. CentOS / RHEL 6 : How to Boot into single user mode
  7. How to use the “screen” command in Linux
  8. How to disable write access to USB devices using “hdparm” tool
  9. How to Disable os-prober in CentOS/RHEL 7
  10. Understanding dm-Multipath Identifiers in Linux

You May Also Like

Primary Sidebar

Recent Posts

  • raw: command not found
  • raw Command Examples in Linux
  • rankmirrors Command Examples in Linux
  • radeontop: command not found

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright