With the availability of relatively cheap, large capacity hard disks, the likelihood of running out of disk space is relatively low. However, we may be in the process of re-organising our data storage by increasing the number, or capacity, of our disk array. Or, we may wish to substitute SSDs for our conventional HDDs.
Anybody who has run out of disk capacity will know just how frustrating it is. Equally, the re-organisation of our data is also time consuming and can be fraught with difficulty. If we have a RAID we can, of course, remove and replace drives on the fly as they become full, or even fail. With LVM we can extend, or shrink, file systems on the fly by changing the capacity of the data pool.
Wouldn't it be great, though, if we could combine all these functions, and more besides, into one set of tools? Furthermore, what if we could have these tools working at the file system level, rather than on a virtual layer above the file system?
Well, Sun Microsystems thought so too and developed the ZFS file system for use in its Solaris OS. Sun is now owned by Oracle but ZFS is still very much alive. Unfortunately, for GNU/Linux users, the CDDL licence that came with ZFS was not compatible with the GPL, so inclusion in the Linux kernel was just not possible. ZFS can be built as a module, but this is not very convenient for a file system, hence, the birth of Btrfs.
Btrfs could be seen as a ZFS for GNU/Linux. It is younger, and therefore not as complete as ZFS, but it is getting there. Linus Torvalds claims that it will be the default GNU/Linux file system when it is ready for production use, so it is worth becoming familiar with it now. The Btrfs file system is built into recent kernels, and additional userland tools, btrfs-progs, are provided for manipulating the file system.
This talk will be a basic introduction for those not yet familiar with the Btrfs file system.
3. Why Btrfs and how does it differ
from other file systems?
● Pooling file system
● Own built-in version of RAID
● Built-in support for SSDs
● Obviates the need for traditional partitions
● Can enable subvolumes and quotas instead of partitions
● Allows snapshots of subvolumes
● Use snapshots for backups
● Use snapshots to roll-back to an earlier state
● Once set up almost everything can be done on the fly
4. Pooling file system – my definition
A pooling file system creates an area of common storage where
data is stored. The pool can be increased in size by adding more
devices, or decreased by removing them, and this can be achieved
on the fly. This is different from the traditional device partitioning
scheme where fixed areas of storage, or partitions, are rigidly
allocated to various parts of the operating system tree.
6. RAID
Btrfs has its own version of RAID built-in
BUT
It differs from MDADM RAID
7. IMPORTANT!
Btrfs RAID 1 keeps only two copies of data and
metadata no matter how many devices you have
in the pool!!!
And those two copies may be held on any two
devices
8. Built-in support for SSDs
Your /etc/fstab mount options could include
ssd,discard
9. Obviates need for traditional
partitioning...
run
mkfs.btrfs /dev/sdx
to format a bare device with the Btrfs file system
11. You can convert an existing partition
1) Unmount the system
umount /dev/sdxx
2) Run a file system check
fsck.extN -f /dev/sdxx
3) Do the conversion
btrfs-convert /dev/sdxx
12. Change your fstab settings
/etc/fstab
new UUID /mount/point btrfs options 0 0
13. Reboot and if all is well delete
ext2_saved
ext2_saved is the subvolume saved for you by
the btrfs-convert tool
Example
btrfs subvolume delete ext2_saved
14. If there is a problem you can
roll-back
So don't delete but rather mount ext2_saved
which has been saved as a subvolume
16. Using a bootable USB flash drive to
boot into a Btrfs RAID 1 rootfs
The boobtable USB flash looks like this from gdisk
Number Start (sector) End (sector) Size Code Name
1 2048 206847 100.0 MiB 8300 Boot
2 206848 19081215 9.0 GiB 8200 Swap
3 19081216 30282974 5.9 GiB 8300 Resc
17. Syslinux can be found here
If anybody is interested
https://www.kernel.org/pub/linux/utils/boot/syslinux/
It's worth it, believe me
18. Download the latest kernel and
btrfs-progs
The latest stable kernel is 3.19.2 from kernel.org
For the latest btrfs-progs run
git clone git://git.kernel.org/pub/scm/linux/kernel/git/mason/btrfs-progs.git
If you don't have git install it first
I guarantee the distro repositories won't have
anything like the latest versions.
19. Let's format our two-device Btrfs
RAID 1 rootfs
mkfs.btrfs -m raid1 -d raid1 /dev/sda /dev/sdb
Clearly, you need to do this from a rescue
system, or externally connect the two devices,
via USB, to a build system, or another system
20. Change your fstab settings again
/etc/fstab
UUID / btrfs device=/dev/sda,device=/dev/sdb 0 0
The mount options where the devices are listed
would be added to with a comma separated list
of your other options. The full list of possible
mount options can be found here
https://btrfs.wiki.kernel.org/index.php/Mount_options
25. Default subvolume
The default subvolume created automatically is
0/5 into which the system boots if no other
subvolume is passed to mount
This can never be deleted
The default subvolume can be toggled with
btrfs subvolume set-default <id> <path>
26. Quotas
Quotas can be applied to subvolumes
Run btrfs quota enable /
Before creating your subvolumes
27. Applying limits to quotas
Example
Run btrfs qgroup limit 1g /var
To limit the size of /var to 1GB
28. Snapshots
Snapshots are just special subvolumes
Basic example for snapshotting
btrfs subvolume snapshot /home/ /home/snapshot
29. Snapshot and backup tools using
Btrfs send/receive
Choose one of these tools if you wish to
automate your backups
https://github.com/masc3d/btrfs-sxbackup # written in python
http://www.digint.ch/btrbk/doc/readme.html # written in perl
https://github.com/ruediste/btrbck # written in java
My favourite is the second written in perl
30. Kernel patch for booting without an
initrd
If you are booting without an initrd you may find
that you need to hack the kernel to stop the
rootfs attempting to load too fast. You can find a
patch here
https://gist.github.com/the8lack8ox/2706705
This will not work with every kernel version, so
you may need to add the appropriate lines of
code manually. The good news is there's not
much to change in the file
31. Testing the file system
After the system boots successfully run
btrfs fi show /
it may return
Label: 'raid 1' uuid: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
Total devices 2 FS bytes used 6.63GiB
devid 2 size 29.82GiB used 8.03GiB path /dev/sda1
*** Some devices missing
32. Getting rid of an annoying message
Run: btrfs device scan
You will find that the message has disappeared
btrfs fi show /
Label: 'raid 1' uuid: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
Total devices 2 FS bytes used 6.63GiB
devid 1 size 29.82GiB used 8.03GiB path /dev/sdb1
devid 2 size 29.82GiB used 8.03GiB path /dev/sda1
You can add btrfs dev scan to a boot script
33. Scrub your file system
Make sure you run this periodically
btrfs scrub start /
You can test the progress with
btrfs scrub status /
You can run this as a cron job
34. Booting a corrupted root file system
Boot with
rootflags=degraded
as a kernel boot parameter on whichever boot
loader you use
36. Device missing?
If you receive a “device missing” warning when
you run
btrfs fi show /
Then run
btrfs device delete missing /
37. Summary of steps necessary to
replace a failed device
1) Mount in degraded mode
2) Add a new device
3) Remove the missing device
4) Rebalance the data and metadata with
btrfs balance start -dconvert=raid1 -mconvert=raid1 /
38. Checking for free disk space
Use this invocation
btrfs fi df <btrfs mountpoint>
And not this
df -h <btrfs mountpoint>
You can test each and see the difference
40. There is no definitive list of shortened forms
You can make them up as you go along
41. A “shortened-form-failed” example
If instead of btrfs subvolume list we attempted
btrfs s l
The parser would return
: ambiguous token 's'
Did you mean one of these ?
subvolume
scrub
send
42. Shortened form corrected
So if we run
btrfs su l
The command completes as expected
The parser operates on a contextual basis
43. Help from the Web
There are many howtos on the web
Here are links to three
https://btrfs.wiki.kernel.org/index.php/Main_Page
https://wiki.archlinux.org/index.php/Btrfs
https://www.howtoforge.com/a-beginners-guide-to-btrfs
44. Acknowledgements
Gerard Beekmans – The man behind Linux From
Scratch who made it possible initially for us to
build our battery-powered solid-state servers
#btrfs on freenode – a great source of help and
advice
Chris Mason – lead developer of Btrfs
This will be a Simple introduction
Anybody used it
Stop me at any time if you have a question
Other options open single device, RAID 0, RAID 10
Too fully featured to cover everything
Not enough time today
But I wanted to start at the beginning as one would when installing Btrfs for the first time. Listing the necessary steps and flagging possible pitfalls.
The first thing that Btrfs does is to pool all devices.
RAID 0 is much the same as MDADM RAID 0.
Data and metadata striped across as many disks as you have but with no redundancy.
Btrfs RAID 10 is built on those precepts.
You could have added 2 disks or you could have added 20 but Btrfs keeps only two copies.
SSD has optimisations built-in
Rotational in /sys should be set at 0
Discard brings back into service discarded blocks that are no longer in use.
You might still need a partition if you wish to:-
Use different file systems on the same device
Have a swap file
Or you might want to just try btrfs out on a spare partition
If you don&apos;t every action to your new system gets mirrored on the backup subvolume
Don&apos;t forget that the saved subvolume is a copy of your original OS and that wasn&apos;t Btrfs, so your trying to maintain two different file systems.
As a point of interest, I always boot my systems from a 16GB USB flash drive.
I create three GPT partitions. I take it everybody knows what I&apos;m talking about when I say GPT?
“Globally Unique Identifiers Partition Table”. It&apos;s the successor to the legacy MBR style partitioning scheme where we&apos;re stuck with only four primary partitions and are forced to use the extended partition kludge to achieve a greater number of logical partitions.
use Syslinux rather than Grub2 because, while I really liked legacy Grub, I think that Grub2 has become excessively bloated.
Syslinux, on the other hand, is small, light, fast and under constant development, and it&apos;s a pleasure to use.
The other benefit here is that once set up the flash drive can be cloned with dd to boot other boxes from flash drives.
I&apos;m not an expert on distros
Btrfs enabled in your kerne –latest kernel
built-in and not as a module
the userspace btrfs-progs – latest from the kernel devs
major distributions already have Btrfs loaded in the kernel, with the btrfs-progs in the repository
I&apos;m no expert on the various distributions.
In fact, come to think of it, I would never refer to myself as an “expert” on anything. Why is that you may ask? Or maybe not. However, the term “expert” has unfortunate connotations. Maybe some of you know this. Many “experts” that I&apos;ve met over the years, and as you can see there have been many of those years. I&apos;ve been around the block a few times. Well, many of those “experts”, unfortunately, have turned out to be of the type where X was the unknown quantity, and spurt was the drip under pressure. I don&apos;t wish to be associated with those people.
Subvolumes are the key to flexibility
This is where the root file system will be mounted
You don&apos;t have to call it root
These will be mounted on the corresponding directories
Once you have your subvolumes with quotas applied they behave very much like a traditional partition.
The main difference is that they can be shrunk, extended, deleted re-created on a running system.
You don&apos;t have to reboot.
I&apos;m a big fan of Erlang.
Erlang is a fault tolerant language developed by Ericsson back in the 1980s for its telephone switches. It has concurrency built in, and Ericcson claims to have a running system in its labs with the elusive nine nines uptime. An Erlang mantra is “let it crash”, meaning processes can crash and be restarted on a running system.
I&apos;m not saying that Btrfs works in the same manne but there are a lot of cool tings you can do on a running system at the file system level
“missing” is a Btrfs term for a specific device
The GNU Coreutils df tool does not return the correct free space.
I think this is really cool
If you have a number of commands to enter it can save a great deal of time
Some may disagree – it&apos;s a free country
Official Btrfs wiki
Archlinux geared towards systemd and we don&apos;t use that.
We generally build our own systems, although we do use Linux Mint – generally as a demonstration for Windows users to show them what a nice environment they could be working in.
Third one is simple and good but with a caveat: the final column of the fstab should be 0 and not 1