Wish you create snapshots of data on your Linux PC just in case anything gets deleted? With the BtrFS file system, you can. Before you can create a BtrFS snapshot, you must first have a block device on Linux that is using the BtrFS file system. If you’re looking to turn an existing drive into a BtrFS drive for misc data, follow the instructions below. Want to use BtrFS as your root file system instead? Be sure to select “custom installation” during the installation process, and select BtrFS as your file system of choice.
In addition to having a block device with the BtrFS file system formatted to it, the user must also install the btrfs-progs package.
Note: you may need to run the lsblk command to determine what block device to format.
BtrFS On Linux
To format a drive, first back up all the data on it and then open up a terminal. In the terminal gain root using: sudo -s. With root access, use the mkfs command to make a new file system on the device. In this example, we’ll use /dev/sdd1
mkfs.btrfs -f /dev/sdd1
Drive not working after formatting to BtrFS? You may need to create a new partition table. Here’s how to do it:
parted /dev/sdd mklabel gpt mkpart primary btrfs 1MiB 100%
Exit the parted tool with:
RECOMMENDED FOR YOU
Then, re-run the format command.
mkfs.btrfs -f /dev/sdd1
Create Snapshots With BtrFS
Before taking snapshots, you’ll need to make some sub-volumes. Main Linux file system is BtrFS? Skip this step, and go straight to setting up sub-volumes. Only follow this part of the process if you are using a secondary drive with the BtrFS file system, you’ll need to mount it. In this example, the hard drive will be /dev/sdd.
sudo -s mkdir /btrfs mount -t btrfs /dev/sdd1 /btrfs
The main BtrFS folder has been created, mounted, and is ready to go. It’s time to create a subvolume. What is a subvolume? It’s a special feature that comes with the BtrFS file system that allows created volumes to work similar to a block device (think /dev/sda1, and etc).
The reality is that they’re not actually block devices. Instead, they’re fancy directories that take advantage of a technology called “name spaces”. Without getting too complicated, the BtrFS takes advantage of this name space technology to allow users to create a bunch of individual, mountable sub volumes that act and behave much like block devices.
To create your own subvolume, follow these steps. In this example, we’ll show off a basic subvolume structure that starts out in the main /btrfs folder. Keep in mind that you can make your own volumes wherever you want, so long as it is done on a BtrFS file system.
First, gain a root shell. These commands could be done with sudo privileges, but given that interacting with the file system (as root) can be tedious, it’s best to just get root right away. Use sudo -s or log in to root using su. Then, do:
btrfs subvolume create /btrfs/vol_a
The first command creates a BtrFS subvolume under /mnt/btrfs on our /dev/sdd1 example drive. If you wanted, you could stop here and use this as a volume to save all important data. Alternatively, you can create sub-sub volumes (as many as you want). To create a volume within a volume, you’d do:
btrfs subvolume create /btrfs/vol_a/vol_b
To create a snapshot, first move/copy all important data into a subvolume. For example:
cp -R ~/Documents /btrfs/vol_a/
then, use the file system to make a snapshot.
btrfs subvolume snapshot /btrfs/vol_a/ /btrfs/vol_a/backup_1
Note: replace “backup_1” with whatever you’d like to call your snapshot. Label them clearly so you can sort through them later.
Restore any snapshot with:
mv /btrfs/vol_a/ /mnt/btrfs/vol_a/backup_1
Mounting Subvolumes As Block Devices
Part of the great thing about subvolumes is the ability to mount them all individually, as independent devices. To do this, first list out all known volumes using the btrfs command.
btrfs subvolume list /btrfs
Go through the list, and find the ID number to the subvolume, and then mount it with the mount command. Then, unmount it once you’ve got the ID.
mount -o subvolid=XXX /dev/sdX /btrfs
Replace XXX with the ID, and /dev/sdX with the drive where the volume to mount is. Just keep in mind that you can’t mount a subvolume to the same partition as the top level partition. Instead mount to another BtrFS formatted partition or drive. For example:
mount -o subvolid=38 /dev/sdc1 /btrfs
Subvol 38 is located on /dev/sdd1, but /dev/sdc1 is also BtrFS, so snapshots can be mounted there. This doesn’t need to be done on individual drives. Instead, you might mount it to /dev/sdc2, and etc.
Set The Default Subvolume
Setting the default BtrFS subvolume is useful, as it makes interacting with it easier. To set a default, first use the subvolume list command to find the ID. Take the ID, and plug it into the set-default command:
btrfs subvolume set-default XXX /btrfs
To get rid of a default you’ve set, use the same command, except instead of using a volume ID from subvolume list, use the number 0. This will set it back to factory settings, and then you’ll be free to use other subvolumes as defaults.
btrfs subvolume set-default 0 /btrfs
Delete A Subvolume
If at any time you’d like to get rid of a volume, you can delete it using BtrFS. Please, do not delete the subvolume manually from the hard drive, with commands like “rmdir” or “rm -rf”. It is better to use the BtrFS file system tools.
To delete a volume, do:
btrfs subvolume delete /btrfs/volume-name