Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
91 views5 pages

Btrfs: Advanced Linux Filesystem Guide

Btrfs is a next-generation filesystem that provides advanced features like subvolumes, snapshots, dynamic volume resizing, and checksumming. It is becoming the default filesystem for many Linux distributions. The btrfs utility is used to create and manage Btrfs volumes, including adding and removing devices, enabling compression, and taking snapshots. Btrfs supports data mirroring and striping across multiple devices for redundancy and performance.

Uploaded by

shellroot
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views5 pages

Btrfs: Advanced Linux Filesystem Guide

Btrfs is a next-generation filesystem that provides advanced features like subvolumes, snapshots, dynamic volume resizing, and checksumming. It is becoming the default filesystem for many Linux distributions. The btrfs utility is used to create and manage Btrfs volumes, including adding and removing devices, enabling compression, and taking snapshots. Btrfs supports data mirroring and striping across multiple devices for redundancy and performance.

Uploaded by

shellroot
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Features

Btrfs

The Btrfs filesystem provides the capa-


bilities for pooling drives together into a
single and (if specified) redundant RAID
volume; dynamic volume resizing; on-
line defragmentation, checksumming,
and compression; and more. Although
still feature incomplete, Btrfs is enabled
in a default vanilla kernel configuration
and is officially supported in various
Linux distributions, including Fedora
and Ubuntu. If you are using a system
that does not offer Btrfs support, you
need to rebuild the Linux kernel to en-
able Btrfs and build the btrfs-progs tool
kit to manage it.

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.)

The Management Utility


At the time of this writing, Btrfs is not a
bootable filesystem; therefore, it cannot
host the /boot path, where all kernel and
boot images reside.
The two main utilities youll need to
create and manage a Btrfs volume are
Btrfs is winning over the experts with advanced features like mkfs.btrfs(8) and btrfs(8). (Many
Btrfs guides also refer to btrfsctl, an
subvolumes, snapshots, and dynamic volume resizing. older tool that has now been replaced by
btrfs.) The btrfs utility is a general-pur-
By Petros Koutoupis
pose tool you can use to manage Btrfs-
enabled volumes to monitor usage, cre-

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

Errors and fsck cept helps Btrfs support enhanced fea-


tures, such as snapshots and subvol- To discover all multidevice filesystems
The Btrfs wiki [1] contains the following
warning: umes. A volume can refer to one or mul- on the machine, you will have to execute
Note that Btrfs does not yet have a fsck tiple grouped disk devices or partitions. this command after rebooting or reload-
tool that can fix errors. While Btrfs is sta-
ble on a stable machine, it is currently Listing 1: Listing Btrfs Filesystems
possible to corrupt the filesystem irre-
$ sudo btrfs filesystem show
coverably if your machine crashes or
Label: none uuid: f2346c5864fd42a5afdb10e9e134d0a1
loses power on disks that dont handle
Total devices 1 FS bytes used 2.40GB
flush requests correctly. This will be
fixed when the fsck tool is ready. devid 1 size 7.64GB used 4.60GB path /dev/sda6

32 March 2011 Issue 124 linux-magazine.com | Linuxpromagazine.com


Features
Btrfs

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:

$ sudo mkfs.btrfs m raid0 U $ sudo btrfs device delete /dev/sdd /mnt/

/dev/sdb /dev/sdc /dev/sdd

When you invoke the filesystem show


To mirror both metadata and file data option, the btrfs utility will show that a
across all attached disk devices, use: device is missing (Listing 5).
To add a disk device, use:
$ sudo mkfs.btrfs m raid10 d raid10 U
/dev/sdb /dev/sdc /dev/sdd /dev/sde $ sudo btrfs device add /dev/sdd /mnt/

Figure 1: A general layout showing how data


is striped across a RAID 0 volume and how After you create a new Btrfs volume, a Listing 6 shows the updated listing of the
data is mirrored across a RAID 1 volume. new entry is appended to the list of Btrfs filesystem.
filesystems on the machine. To list all At this point, the filesystem includes
ing the btrfs module. To list device Btrfs filesystems and which devices they three devices, but all the metadata and
types (Listing 1), use: include, use the btrfs command. You data are still stored on /dev/sdb and /
will notice the newly created Btrfs vol- dev/sdc. Now you need to balance the
$ sudo btrfs filesystem show ume (Listing 3). filesystem and spread the files across all
To read and write, youll need to of the devices. The whole concept of bal-
Creating a New Volume mount the volume from any of the block ancing re-stripes the allocated extents
To use the new Btrfs filesystem, format device node names specified when the across all of the existing devices.
and label a volume consisting of one or volume was created. Listings 3 and 4 use
more disk devices or partitions. Creating /dev/sdb to signify the entire volume. $ sudo btrfs filesystem balance /mnt/

a new Btrfs-enabled volume is quite sim-


ple. To create a single disk volume, type: Adding and Removing a leading to the configuration in Listing 7.
Device The balancing operation will take
$ sudo mkfs.btrfs /dev/sdb One of the best features of the Btrfs file- some time because it requires that all of
system is being able to add or remove the filesystem data and metadata are
To stripe across multiple disk devices,
type the following command, listing all Listing 2: Formatting Multiple Disk Devices
the disk devices. $ sudo mkfs.btrfs /dev/sdb /dev/sdc /dev/sdd

WARNING! Btrfs Btrfs v0.19 IS EXPERIMENTAL


$ sudo mkfs.btrfs /dev/sdb U
WARNING! see http://btrfs.wiki.kernel.org before using
/dev/sdc /dev/sdd

adding device /dev/sdc id 2

(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

Btrfs and RAID


RAID (Redundant Array of Independent types exist. To date, Btrfs officially supports were to fail, the entire array fails. In a RAID
Disks) is a method by which multiple inde- RAID 0, RAID 1, and RAID 10, and it can 1 (disk mirroring) array, one hard disk
pendent hard disk drives attached to a also duplicate metadata on a single disk. A stores an exact replica of another hard
computer appear as a single disk. Depend- patch does exist to add support for RAID 5 disk. If one drive fails, the second steps in
ing on the RAID type, performance can im- and RAID 6, but it hasnt been officially and resumes where the first left off. Overall
prove dramatically, especially as you merged into the project. drive performance is consistent with the
stripe/balance the data across multiple performance of a single disk. In some RAID
A RAID 0 array stripes data, interleaving
disks, thus removing the bottleneck of a 1 implementations, read performance is
across all drives in the array by writing in a
single disk for write/read operations. tuned to achieve faster speeds through a
round-robin fashion. Both read and write
mechanism known as read-balancing.
Most RAID algorithms offer a form of re- operations access the data the same way.
Read-balancing provides the ability to re-
dundancy that can withstand a finite num- Writing or reading the data concurrently
trieve data from both disks in the mirror.
ber of disk failures. When a disk fails in a across several disks takes less time than
redundant array, the array will operate in a writing it all to one disk, which means that The hybrid RAID 10 uses striping of mir-
degraded mode until it is recovered. The a RAID 0 system can dramatically improve rored sets. Figure 1 shows an example of
technology is capable of rebuilding itself to performance, but RAID 0 offers no form of how data is written in chunks across multi-
a point before the failure. Numerous RAID redundancy. If a hard disk drive in the array ple disk devices in a RAID volume.

linux-magazine.com | Linuxpromagazine.com Issue 124 March 2011 33


Features
Btrfs

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

subvolume when the parent volume is setdefault 256 /mnt

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

Total devices 1 FS bytes used 2.40GB


snapshot_of_root U
/dev/sdb /btrfs_snapshot/
devid 1 size 7.64GB used 4.60GB path /dev/sda6

Label: none uuid: 0fa5bbee6f694d10a316ac373e8b5f64


Full backups of an entire volume can
Total devices 3 FS bytes used 28.00KB
take a long time and use large amounts
devid 1 size 2.00GB used 531.94MB path /dev/sdb
of storage space, even for files that re-
devid 2 size 2.00GB used 212.75MB path /dev/sdc
main unchanged. Also, when perform-
devid 3 size 2.00GB used 519.94MB path /dev/sdd
ing a data backup of entire volumes or
subsets of volumes in a symmetric multi-
Listing 4: Mounting and Verifying processing environment, write opera-
$ sudo mount /dev/sdb /mnt/
tions might continue to modify the file
$ df t btrfs
data on that volume, with the possibility
of data corruption. Several strategies
Filesystem 1Kblocks Used Available Use% Mounted on
provide some protection. For instance,
/dev/sda6 8011776 2745480 5266296 35% /
the volume can be taken offline or
/dev/sdb 6291456 56 6291400 1% /mnt
marked as read-only before the archival
process, but in high-availability produc-
Listing 5: After Removing a Device tion environments, this approach might
$ sudo btrfs filesystem show /dev/sdb
not be practical.
A snapshot provides a more complete
Label: none uuid: 0fa5bbee6f694d10a316ac373e8b5f64
solution. You can use a snapshot to
Total devices 3 FS bytes used 36.00KB
avoid downtime and retain atomicity
devid 1 size 2.00GB used 156.00MB path /dev/sdb
when archiving entire files, directories,
devid 2 size 2.00GB used 136.00MB path /dev/sdc or filesystems. In a production environ-
*** Some devices missing ment, it is not uncommon for a system

34 March 2011 Issue 124 linux-magazine.com | Linuxpromagazine.com


Features
Btrfs

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/

ties involved. Resize '/mnt/' of '1G'

To delete a subvolume, type: $ sudo btrfs subvolume delete U


/mnt/snapshot_of_root/ Now the volume is 5GB in size (see List-
$ sudo btrfs subvolume delete /mnt/subvol ing 12). To return the filesystem to 6GB,
Resizing a Volume type:
When listing all subvolumes of the par- Unlike many filesystem alternatives,
ent volume, you will notice that the re- Btrfs lets you dynamically resize an ex- $ sudo btrfs filesystem resize +1G /mnt/

Resize '/mnt/' of '+1G'

Listing 6: After Adding a Device


$ sudo btrfs filesystem show /dev/sdb
If you want to use the maximum size of
the volume, type:
Label: none uuid: 0fa5bbee6f694d10a316ac373e8b5f64

Total devices 3 FS bytes used 36.00KB


$ sudo btrfs filesystem resize max /mnt
devid 1 size 2.00GB used 156.00MB path /dev/sdb

devid 2 size 2.00GB used 136.00MB path /dev/sdc


Listing 13 shows the df output after re-
devid 3 size 2.00GB used 0.00 path /dev/sdd
sizing back to 6GB (the volumes maxi-
mum capacity).
Listing 7: After Rebalancing The current limitation, I find, is that
$ sudo btrfs filesystem show /dev/sdb
the Btrfs module wont let you resize a
subvolume without affecting the size of
Label: none uuid: 0fa5bbee6f694d10a316ac373e8b5f64
the parent volume. For instance, if the
Total devices 3 FS bytes used 36.00KB
parent volume is mounted at /mnt and
devid 1 size 2.00GB used 352.75MB path /dev/sdb
the subvolume is mounted at /subvol, if
devid 2 size 2.00GB used 204.75MB path /dev/sdc
I attempt to decrease the size of the sub-
devid 3 size 2.00GB used 340.75MB path /dev/sdd
volume to leave more space to create a
new subvolume in the future, the parent
Listing 8: Listing the Contents volume also decreases in total size (List-
$ ls l /mnt/
ing 14). Btrfs doesnt let me keep the
total 0
parent volume constant and liberate
space by shrinking a subvolume. I imag-
drwx 1 root root 0 20101229 15:51 subvol
ine this problem will be addressed in the
near future as Btrfs matures.
Listing 9: Creating a Test File
$ sudo dd if=/dev/zero of=/mnt/test.dat count=4
Commands and
4+0 records in
Configurations
For man pages for both the mkfs.btrfs
4+0 records out
and btrfs utilities, at the command line
2048 bytes (2.0 kB) copied, 0.000383095 s, 5.3 MB/s
enter:
$ ls /mnt/

subvol test.dat
$ man 8 btrfs

Listing 10: Snapshot Comparison Although I mainly relied on the df com-


$ ls l /mnt/snapshot_of_root/
mand to monitor the filesystems overall
total 2
size and usage, the btrfs tool has its
own built-in version that conveniently
drwxrxrx 1 root root 0 20101229 15:57 subvol
displays more detailed usage of the de-
rwrr 1 root root 2048 20101229 15:54 test.dat
sired filesystem.
$ ls l /mnt/

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

36 March 2011 Issue 124 linux-magazine.com | Linuxpromagazine.com


Features
Btrfs

$ 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

rwrr 1 root root 2048 20101229 15:54 test.dat


Or, you can use the UUID (as it is dis-
played in the output of the btrfs file
Listing 12: df Output system show command) followed by the
$ df t btrfs
traditional fstab field entries (Listing 15).
Filesystem 1Kblocks Used Available Use% Mounted on

/dev/sda6 8011776 2804452 5207324 36% /


Summary
The next-generation Linux filesystem is
/dev/sdb 5242880 72 5242808 1% /mnt
full of great features and functionality if
you take the time to get used to the
Listing 13: After Resizing tools. Although Btrfs is still feature in-
$ df t btrfs
complete, the filesystem provides amaz-
Filesystem 1Kblocks Used Available Use% Mounted on
ing flexibility. As Btrfs evolves [3], you
/dev/sda6 8011776 2804456 5207320 36% /
can expect to find official support for
/dev/sdb 6291456 72 6291384 1% /mnt
RAID 5 and RAID 6, online filesystem in-
tegrity checks, and support for data de-
duplication (an ideal solution in virtual-
Listing 14: Resizing a Subvolume ization environments). n n n
$ df t btrfs

Filesystem 1Kblocks Used Available Use% Mounted on


Info
/dev/sda6 8011776 2804768 5207008 36% /
[1] Btrfs wiki:
/dev/sdb 8388608 80 8388528 1% /mnt
https://btrfs.wiki.kernel.org/
/dev/sdc 8388608 80 8388528 1% /subvol
[2] Btrfs mount options:
$ sudo btrfs filesystem resize 1G /mnt/subvol/ https://btrfs.wiki.kernel.org/index.
Resize '/mnt/subvol/' of '1G' php/Getting_started#Mount_Options
$ df t btrfs [3] Wikipedia on Btrfs:
Filesystem 1Kblocks Used Available Use% Mounted on
http://en.wikipedia.org/wiki/Btrfs

/dev/sda6 8011776 2804768 5207008 36% /

/dev/sdb 7340032 80 7339952 1% /mnt Author


/dev/sdc 7340032 80 7339952 1% /subvol Petros Koutoupis is a full-time Linux ker-
nel, device driver, and application devel-
oper for embedded and server platforms.
Listing 15: UUID and fstab Field Entries He has worked in the data storage indus-
01 U
UID0fa5bbee6f694d10a316ac373e8b5f64 /mnt btrfs defaults 0 0 try for more than six years and enjoys
discussing storage technologies.

38 March 2011 Issue 124 linux-magazine.com | Linuxpromagazine.com

You might also like