Btrfs in Action on Ubuntu
Creating the initial volume (pool)
Btrfs uses different strategies to store data and for the filesystem metadata
By default the behavior is:
- metadata is replicated on all of the devices. If a single device is used the metadata is duplicated inside this single device (useful in case of corruption or bad sector, there is a higher chance that one of the two copies is clean). To tell btrfs to maintain a single copy of the metadata, just use single. Remember: dead metadata = dead volume with no chance of recovery.
- data is spread amongst all of the devices (this means no redundancy; any data block left on a defective device will be inaccessible)
To create a Btrfs volume made of multiple devices with default options, use:
To create a Btrfs volume made of a single device with a single copy of the metadata (dangerous!), use:
To create a Btrfs volume made of multiple devices with metadata spread amongst all of the devices, use:
To create a Btrfs volume made of multiple devices, with metadata spread amongst all of the devices and data mirrored on all of the devices, use:
To create a fully redundant Btrfs volume (data and metadata mirrored amongst all of the devices), use:
Technically you can use anything as a physical volume: you can have a volume composed of 2 local hard drives, 3 USB keys, 1 loopback device pointing to a file on a NFS share and 3 logical devices accessed through your SAN (you would be an idiot, but you can, nevertheless). Having different physical volume sizes would lead to issues, but it works :-).
320GB HDD with 4 partitions (block devices) as /dev/sdb1 /dev/sdb2 /dev/sdb3 and /dev/sdb4
Create a RAID10 (requires at minimum 4 devices) with these 4 block devices
Checking the initial volume
To verify the devices of which Btrfs volume is composed, just use btrfs-show device (old style) or btrfs filesystem show device (new style). You need to specify one of the devices (the metadata has been designed to keep a track of the what device is linked what other device).
BTRFS wiki mentions that btrfs device scan should be performed.
Mounting the initial volume
Btrfs volumes can be mounted like any other filesystem. The cool stuff at the top on the sundae is that the design of the BTRFS metadata makes it possible to use any of the volume devices. The following commands are equivalent:
For every physical device used for mounting the Btrfs volume df -hT reports the same (in all cases 3 GiB of "free" space is reported):
The following command prints very useful information (like how the BTRFS volume has been created):
By the way, as you can see, for the btrfs command the mount point should be specified, not one of the physical devices.
Shrinking the volume
A common practice in system administration is to leave some head space, instead of using the whole capacity of a storage pool (just in case). With btrfs one can easily shrink volumes. Let's shrink the volume a bit (about 25%):
And yes, it is an on-line resize, there is no need to umount/shrink/mount. So no downtimes! :-) However, a BTRFS volume requires a minimal size... if the shrink is too aggressive the volume won't be resized:
Growing the volume
This is the opposite operation, you can make a Btrfs grow to reach a particular size (e.g. 150 more megabytes):
You can also take an "all you can eat" approach via the max option, meaning all of the possible space will be used for the volume:
Adding a new device to Btrfs volume
Add a new device to the volume
Again, no need to umount the volume first as adding a device is an online operation. The operation is not finished as we MUST tell Btrfs to prepare the new device (i.e. rebalance/mirror the metadata and the data between all devices):
Check pool status using
Removing a device from Btrfs volume
Removing a device from the BTRFS volume
Check pool status
NOTE: Here again removing a device is totally dynamic and can be done as online operation! Note that when a device is removed, its content is transparently redistributed among the other devices.
- ** DO NOT UNPLUG THE DEVICE BEFORE THE END OF THE OPERATION, DATA LOSS WILL RESULT**
- If you have used raid0 in either metadata or data at the BTRFS volume creation you will end in a unusable volume if one of the the devices fails before being properly removed from the volume as some stripes will be lost.
Once you add a new device to the Btrfs volume as a replacement for a removed one, you can clean up the references to the missing device:
Fast device replacement
New in Linux 3.8 Kernel
As a filesystem that expands to multiple devices, Btrfs can remove a disk easily, just in case you want to shrink your storage pool, or just because the device is failing and you want to replace it
But the process is not as fast as it could be. Btrfs has added a explicit device replacement operation which is much faster
The copy usually takes place at 90% of the available platter speed if no additional disk I/O is ongoing during the copy operation. The operation takes place at runtime on a live filesystem, it does not require to unmount it or stop active tasks, and it is safe to crash or lose power during the operation, the process will resume with the next mount. It's also possible to use the command "btrfs replace status" to check the status of the operation, or "btrfs replace cancel" to cancel it. The userspace patches for the btrfs program can be found [git://btrfs.giantdisaster.de/git/btrfs-progs here].
btrfsck - check a Btrfs filesystem
Build the latest btrfsck