Btrfs: Advanced Linux Filesystem Guide
Btrfs: Advanced Linux Filesystem Guide
Btrfs
Smooth
In this article, I look at how to create
and manage a Btrfs filesystem. Along the
Managing storage volumes with Btrfs way, youll get a glimpse at some of the
advanced features exciting the experts.
However, keep in mind that Btrfs is rela-
as Butter
tively new and still a work in progress.
(See the box titled Errors and fsck.)
B
trfs [1] is a next-generation file- Btrfs filesystem was partly inspired by ate subvolumes and snapshots, create
system destined to become the Sun Microsystems (now Oracle) ZFS new volumes, add and remove devices
default filesystem of many fu- filesystem, which has never officially from existing volumes, and enable on-
ture Linux distribution releases. been ported to the Linux kernel because line defragmentation and volume bal-
Btrfs, initially developed by Oracle, is li- of licensing conflicts between the Com- ancing.
censed under the General Public License mon Development and Distribution Li- For instance, if you are logging into a
(GPL) Version 2 and was accepted into cense (CDDL) and the GPL. Linux system and you want to learn
the mainline Linux kernel as of 2.6.29- Btrfs is known as a Copy-On-Write whether the system contains any Btrfs-
rc1. Btrfs is an acronym for B-tree file- (COW) filesystem. When data is modi- labeled devices, you could type the fol-
system, but the word is usually pro- fied, it is never modified in place. A new lowing command:
nounced Butter FS. The innovative data block or series of data blocks are al-
located to store the new data. This con- $ sudo btrfs device scan
ror the metadata across all disk devices disk devices dynamically from an exist-
and stripe the data across all disk de- ing pool. If you have a failed disk device,
vices. To stripe both the metadata and or a disk device to use someplace else,
data (i.e., no mirroring), type: remove the device with:
(For Btrfs RAID options, see the box ti- adding device /dev/sdd id 3
tled Btrfs and RAID.) In Listing 2, fs created label (null) on /dev/sdb
mkfs.btrfs configures the drives to mir- nodesize 4096 leafsize 4096 sectorsize 4096 size 6.00GB
read and rewritten across the full array, Listing 8 shows the contents from the member, this is all made possible by the
including across the newly added device. root of the filesystem. COW design of the Btrfs filesystem.) To
If you want to set a specific subvol- see how the snapshot process works,
Subvolumes and Volume ume as the default of the volume when start by creating a file with touch or dd
Snapshots it is mounted, you need to obtain the (Listing 9).
A single Btrfs volume can contain multi- subvolume ID (as seen in the preceding Now that you have a file, you can cre-
ple subvolumes. If you define a subvol- command) and invoke the btrfs utility ate a snapshot of the filesystem root:
ume as the default for the volume, with the following options:
when it is mounted, the subvolume is $ sudo btrfs subvolume snapshot U
presented as root; you can even mount a $ sudo btrfs subvolume U /mnt /mnt/snapshot_of_root
not mounted. Each subvolume can oper- A listing of all subvolumes for the Btrfs
ate as an independent filesystem. In Alternatively, you can mount the subvol- filesystem will now have the snapshot
Btrfs, all the storage is in the pool, and ume under a separate directory path: appended to it:
subvolumes are created from the pool
you do not need to partition anything. $ sudo mount t btrfs o subvol=subvol U $ sudo btrfs subvolume list /mnt
As long your storage capacity hold out, /dev/sdb /subvol ID 256 top level 5 path subvol
you can create as many subvolumes as ID 257 top level 5 path snapshot_of_root
you want. To create a subvolume, type: Although you might think the data snap-
shot is a subvolume, it differs in imple- A listing of the contents of the newly
$ sudo btrfs subvolume create /mnt/subvol mentation. A snapshot is a single state of created snapshot will display an image
Create subvolume '/mnt/subvol' a storage volume at a particular point in of what the root of the Btrfs volume
time. Snapshots are usually used for data looked like when the snapshot was
To verify that the subvolume exists in archiving. Most traditional volume man- taken, with the exception of the snap
the root of the Btrfs volume, use the list agers require that the snapshot be taken shot subvolume directory (Listing 10).
option: across the entire logical volume. Btrfs, Now create a new file at the root of the
on the other hand, lets you create snap- volume. The snapshot will still preserve
$ sudo btrfs subvolume list /mnt shots on individual files or entire direc- the original data at the time it was taken
ID 256 top level 5 path subvol tories anywhere in the Btrfs volume. (Re- (see the comparison in Listing 11).
To mount the newly created snapshot
Listing 3: A New Btrfs Filesystem to a separate directory path, use:
$ sudo btrfs filesystem show
$ sudo mkdir /btrfs_snapshot
Label: none uuid: f2346c5864fd42a5afdb
$ sudo mount t btrfs o subvol=U
10e9e134d0a1
administrator to create a scheduled cron cently deleted subvolume will not be isting volume. This feature is still some-
job to create hourly, daily, weekly, or listed: what limited and needs more time to
monthly snapshots of various files and mature, but it is still useful for some en-
directories, including the end users $ sudo btrfs subvolume list /mnt vironments. If you want to decrease the
home directory. That way, the admin can ID 257 top level 5 path snapshot_of_root size of a volume from 6 to 5GB, type:
retrieve a recently modified or deleted
file with few or no headaches for all par- To delete a snapshot subvolume use: $ sudo btrfs filesystem resize 1G /mnt/
subvol test.dat
$ man 8 btrfs
total 6
$ btrfs filesystem df /
drxrxrx 1 root root 60 20101229 15:54 snapshot_of_root
Data: total=3.06GB, used=2.24GB
drwx 1 root root 0 20101229 15:51 subvol
Metadata: total=783.19MB, used=220.93MB
rwrr 1 root root 2048 20101229 15:54 test.dat System: total=12.00MB, used=4.00KB
$ btrfs filesystem df /mnt/ As with any other Linux filesystems, (SSD). To enable the SSD optimizations,
Data: total=614.25MB, used=0.00 Btrfs also supports mounting a Btrfs vol- mount with the o ssd option.
Metadata: total=128.00MB, used=32.00KB ume with one or more mount options. Note that, as of v2.6.31-rc1, this
System: total=12.00MB, used=4.00KB For instance, to disable checksumming mount option will be enabled if Btrfs is
CRC-32C while enabling zlib compres- able to detect non-rotating storage. The
As mentioned earlier, Btrfs supports on- sion, you need to type: SSD is going to play a large part in the
line defragmentation. (Note that defrag- future of data storage, and it is nice to
ging takes longer on larger and more $ sudo mount t btrfs o U observe that the Btrfs developers have
data-occupied volumes.) To defragment nodatasum,compress /dev/
sdb /mnt/ been preparing for its arrival. See the
a volume, type: Btrfs wiki page for a full listing of mount
Btrfs also contains optimizations for the options [2].
$ sudo btrfs filesystem defragment /mnt/ popular Flash-based Solid State Disk One last thing worth mentioning is
that, if you want to add an entry into the
Listing 11: Updated Comparison /etc/fstab file to mount the Btrfs vol-
$ ls l /mnt/
ume at system bootup, you can do so by
total 10
entering a new line specifying one of the
drxrxrx 1 root root 60 20101229 15:54 snapshot_of_root
devices, the mount point, the filesystem,
drwx 1 root root 0 20101229 15:51 subvol
and a list of all the devices and options:
rwrr 1 root root 2048 20101229 15:59 test_again.dat
/dev/sdb /mnt btrfs U
rwrr 1 root root 2048 20101229 15:54 test.dat
device=/dev/sdb,device=/dev/sdc,U
$ ls l /mnt/snapshot_of_root/
device=/dev/sdd,U
total 2
device=/dev/sde 0 0
drwxrxrx 1 root root 0 20101229 15:57 subvol