Difference between revisions of "Btrfs"
Kynikos.bot (talk | contribs) (Template:Keypress is deprecated in favor of Template:ic, see Help talk:Style/Migration to new Code formatting templates#Template:Keypress) |
m (→Partitioning: Minor change on commas to periods.) |
||
(15 intermediate revisions by 6 users not shown) | |||
Line 9: | Line 9: | ||
{{Article summary end}} | {{Article summary end}} | ||
− | Btrfs is an abbreviation for | + | Btrfs is an abbreviation for ''B-tree file system'' and is also known as ''Butter FS'' or ''Better FS''. Btrfs is a copy-on-write (COW) file system written from the ground up for Linux. It is aimed at implementing advanced features while focusing on fault tolerance, repair and easy administration. Jointly developed by Oracle, Red Hat, Fujitsu, Intel, SUSE and many others, Btrfs is licensed under the GPL and open for contribution from anyone. |
== Installation == | == Installation == | ||
− | + | Btrfs is included in the default kernel and its tools ({{Pkg|btrfs-progs}}) are available in the official repositories. [[GRUB]], [[mkinitcpio]], and [[Syslinux]] have support for Btrfs and require no additional configuration. | |
− | + | * On 2012-03-28, {{Pkg|btrfs-progs}} includes ''btrfsck'', a tool that can fix errors on Btrfs filesystems. | |
+ | * {{AUR|mkinitcpio-btrfs}}: for roll-back abilities (currently unmaintained). | ||
− | + | {{Note|It is recommended that users use the most up-to-date version of of userspace utils as possible. Consider using {{AUR|btrfs-progs-git}} from the AUR and remember to rebuild it periodically or consider pulling btrfs-progs from the testing repo at a minimum.}} | |
+ | |||
+ | == General administration of BTRFS == | ||
=== Creating a new file system === | === Creating a new file system === | ||
− | To format | + | A Btrfs file system can either be newly created or have one converted. |
+ | |||
+ | To format a partition do: | ||
− | # mkfs.btrfs /dev/ | + | # mkfs.btrfs -L ''mylabel'' /dev/''partition'' |
− | {{Note|As | + | {{Note|1=As [https://git.kernel.org/cgit/linux/kernel/git/mason/btrfs-progs.git/commit/?id=c652e4efb8e2dd76ef1627d8cd649c6af5905902 this] commit (November 2013), Btrfs default blocksize is 16KB.}} |
− | To use a larger blocksize for data/meta data, specify a value for the leafsize via the -l switch as shown in this example using | + | To use a larger blocksize for data/meta data, specify a value for the leafsize via the {{ic|-l}} switch as shown in this example using 16KB blocks: |
− | # mkfs.btrfs -l 16k /dev/ | + | # mkfs.btrfs -L ''mylabel'' -l 16k /dev/''partition'' |
− | Multiple devices can be entered to create a RAID. Supported RAID levels include RAID 0, RAID 1 and RAID 10. | + | Multiple devices can be entered to create a RAID. Supported RAID levels include RAID 0, RAID 1 and RAID 10. By default the metadata is mirrored and data is striped. |
− | # mkfs.btrfs [options] /dev/ | + | # mkfs.btrfs [''options''] /dev/''part1'' /dev/''part2'' |
=== Convert from Ext3/4 === | === Convert from Ext3/4 === | ||
Line 39: | Line 44: | ||
Boot from an install CD, then convert by doing: | Boot from an install CD, then convert by doing: | ||
− | # btrfs-convert /dev/ | + | # btrfs-convert /dev/''partition'' |
Mount the partion and test the conversion by checking the files. Be sure to change the {{ic|/etc/fstab}} to reflect the change ('''type''' to btrfs and '''fs_passno''' [the last field] to 0 as Btrfs does not do a file system check on boot). Also note that the UUID of the partition will have changed, so update your fstab accordingly if you use UUIDs. {{ic|chroot}} into the system and rebuild the GRUB menu list (see [[Install from Existing Linux]] and [[GRUB]] articles). | Mount the partion and test the conversion by checking the files. Be sure to change the {{ic|/etc/fstab}} to reflect the change ('''type''' to btrfs and '''fs_passno''' [the last field] to 0 as Btrfs does not do a file system check on boot). Also note that the UUID of the partition will have changed, so update your fstab accordingly if you use UUIDs. {{ic|chroot}} into the system and rebuild the GRUB menu list (see [[Install from Existing Linux]] and [[GRUB]] articles). | ||
Line 47: | Line 52: | ||
# rm /ext2_saved/* | # rm /ext2_saved/* | ||
# btrfs subvolume delete /ext2_saved | # btrfs subvolume delete /ext2_saved | ||
+ | |||
+ | === Displaying used/free space === | ||
+ | |||
+ | General linux userspace tools such as {{ic|/usr/bin/df}} will inaccurately report free space on a Btrfs partition since it does not take into account space allocated for and used by the metadata. It is recommended to use {{ic|/usr/bin/btrfs}} to query a btrfs partition. Below is an illustration of this effect, first querying using df, and then using btrfs fi df: | ||
+ | |||
+ | {{hc|# df -h /| | ||
+ | Filesystem Size Used Avail Use% Mounted on | ||
+ | /dev/sda3 119G 3.0G 116G 3% / | ||
+ | }} | ||
+ | |||
+ | {{hc|# btrfs fi df /|2= | ||
+ | Data: total=3.01GB, used=2.73GB | ||
+ | System: total=4.00MB, used=16.00KB | ||
+ | Metadata: total=1.01GB, used=181.83MB | ||
+ | }} | ||
+ | |||
+ | Notice that {{ic|df -h}} reports 3.0GB used but {{ic|btrfs fi df}} reports 2.73GB for the data. This is due to the way BTRFS allocates space into the pool. The true disk usage is the sum of all three 'used' values which is inferior to 3.0GB as reported by {{ic|df -h}}. | ||
+ | |||
+ | Another useful command to show a less verbose readout of used space is {{ic|btrfs fi show}}: | ||
+ | |||
+ | {{hc|# btrfs fi show /dev/sda3| | ||
+ | failed to open /dev/sr0: No medium found | ||
+ | Label: 'arch64' uuid: 02ad2ea2-be12-2233-8765-9e0a48e9303a | ||
+ | Total devices 1 FS bytes used 2.91GB | ||
+ | devid 1 size 118.95GB used 4.02GB path /dev/sda2 | ||
+ | |||
+ | Btrfs v0.20-rc1-358-g194aa4a-dirty | ||
+ | }} | ||
== Limitations == | == Limitations == | ||
Line 54: | Line 87: | ||
=== Encryption === | === Encryption === | ||
− | Btrfs has no built-in encryption support (this may come in future), but you can encrypt the partition before running | + | Btrfs has no built-in encryption support (this may come in future), but you can encrypt the partition before running {{ic|mkfs.btrfs}}. See [[Dm-crypt with LUKS]]. |
− | + | If you've already created a Btrfs file system, you can also use something like [[EncFS]] or [[TrueCrypt]], though perhaps without some of Btrfs' features. | |
=== Swap file === | === Swap file === | ||
− | Btrfs does not support swap files. This is due to swap files requiring a function that Btrfs doesn't have for possibility of corruptions.<sup>[https://btrfs.wiki.kernel.org/index.php/FAQ#Does_btrfs_support_swap_files.3F link]</sup> A swap file can be mounted on a loop device with poorer performance but will not be able to hibernate. A systemd service file is available {{AUR|systemd-loop-swapfile}}. | + | Btrfs does not support swap files. This is due to swap files requiring a function that Btrfs doesn't have for possibility of corruptions.<sup>[https://btrfs.wiki.kernel.org/index.php/FAQ#Does_btrfs_support_swap_files.3F link]</sup> A swap file can be mounted on a loop device with poorer performance but will not be able to hibernate. A ''systemd'' service file is available {{AUR|systemd-loop-swapfile}}. |
=== GRUB2 and core.img === | === GRUB2 and core.img === | ||
− | [[GRUB | + | [[GRUB]] can boot Btrfs partitions however the module is larger than e.g. ext4 and the {{ic|core.img}} file made by ''grub-install'' may not fit between the MBR and the first partition. This can be solved by using GPT or by putting an extra 1 or 2 MB of free space before the first partition. |
If you get the following: {{ic|1=error no such device: root}} when booting from a RAID style setup then edit /usr/share/grub/grub-mkconfig_lib and remove both quotes from the line {{ic|1=echo " search --no-floppy --fs-uuid --set=root ${hints} ${fs_uuid}"}}. Regenerate the config for grub and your system should boot without an error. | If you get the following: {{ic|1=error no such device: root}} when booting from a RAID style setup then edit /usr/share/grub/grub-mkconfig_lib and remove both quotes from the line {{ic|1=echo " search --no-floppy --fs-uuid --set=root ${hints} ${fs_uuid}"}}. Regenerate the config for grub and your system should boot without an error. | ||
Line 73: | Line 106: | ||
=== Copy-On-Write (CoW) === | === Copy-On-Write (CoW) === | ||
− | |||
− | System-wide settings also affect commit intervals. | + | The resolution at which data are written to the filesystem is dictated by Btrfs itself and by system-wide settings. Btrfs defaults to a 30 seconds checkpoint interval in which new data are committed to the filesystem. As of Btrfs v0.20-rc1-253-g7854c8b users cannot tweak this without recompiling a [http://www.mail-archive.com/linux-btrfs@vger.kernel.org/msg26090.html patched version of fs/btrfs/disk-io.c]. On 01-Aug-2013, David Sterba submitted [http://www.mail-archive.com/linux-btrfs@vger.kernel.org/msg26116.html this patch] to make this a formal mount time tuneable. |
+ | |||
+ | System-wide settings also affect commit intervals. They include the files under {{ic|/proc/sys/vm/*}} and are out-of-scope of this wiki article. The kernel documentation for them resides in {{ic|Documentation/sysctl/vm.txt}}. | ||
CoW comes with some advantages, but can negatively affect performance with large files that have small random writes. It is recommended to disable CoW for database files and virtual machine images. | CoW comes with some advantages, but can negatively affect performance with large files that have small random writes. It is recommended to disable CoW for database files and virtual machine images. | ||
− | You can disable CoW for the entire block device by mounting it with | + | You can disable CoW for the entire block device by mounting it with {{ic|nodatacow}} option. However, this will disable CoW for the entire file system. |
To disable CoW for single files/directories do: | To disable CoW for single files/directories do: | ||
− | # chattr +C | + | # chattr +C ''/dir/file'' |
Note, from chattr man page: For btrfs, the 'C' flag should be set on new or empty files. If it is set on a file which already has data blocks, it is undefined when the blocks assigned to the file will be fully stable. If the 'C' flag is set on a directory, it will have no effect on the directory, but new files created in that directory will have the No_COW attribute. | Note, from chattr man page: For btrfs, the 'C' flag should be set on new or empty files. If it is set on a file which already has data blocks, it is undefined when the blocks assigned to the file will be fully stable. If the 'C' flag is set on a directory, it will have no effect on the directory, but new files created in that directory will have the No_COW attribute. | ||
Line 90: | Line 124: | ||
# cp --reflink source dest | # cp --reflink source dest | ||
− | As dest file is changed, only those blocks that are changed from source will be written to the disk. One might consider aliasing aliasing cp to | + | As dest file is changed, only those blocks that are changed from source will be written to the disk. One might consider aliasing aliasing ''cp'' to {{ic|1=cp --reflink=auto}}. |
=== Multi-device filesystem and RAID feature === | === Multi-device filesystem and RAID feature === | ||
− | ====Multi-device filesystem==== | + | |
+ | ==== Multi-device filesystem ==== | ||
When creating a ''btrfs'' filesystem, you can pass as many partitions or disk devices as you want to ''mkfs.btrfs''. The filesystem will be created across these devices. You can '''"'''pool'''"''' this way, multiple partitions or devices to get a big ''btrfs'' filesystem. | When creating a ''btrfs'' filesystem, you can pass as many partitions or disk devices as you want to ''mkfs.btrfs''. The filesystem will be created across these devices. You can '''"'''pool'''"''' this way, multiple partitions or devices to get a big ''btrfs'' filesystem. | ||
Line 100: | Line 135: | ||
A multi-device ''btrfs'' filesystem (also called a btrfs volume) is not recognized until | A multi-device ''btrfs'' filesystem (also called a btrfs volume) is not recognized until | ||
− | + | # btrfs device scan | |
− | has been run. This is the purpose of the ''btrfs'' mkinitcpio hook | + | has been run. This is the purpose of the ''btrfs'' mkinitcpio hook. |
− | ====RAID features==== | + | ==== RAID features ==== |
When creating multi-device filesystem, you can also specify to use RAID0, RAID1 or RAID10 across the devices you have added to the filesystem. RAID levels can be applied independently to data and meta data. By default, meta data is duplicated on single volumes or RAID1 on multi-disk sets. | When creating multi-device filesystem, you can also specify to use RAID0, RAID1 or RAID10 across the devices you have added to the filesystem. RAID levels can be applied independently to data and meta data. By default, meta data is duplicated on single volumes or RAID1 on multi-disk sets. | ||
Line 109: | Line 144: | ||
btrfs works in block-pairs for raid0, raid1, and raid10. This means: | btrfs works in block-pairs for raid0, raid1, and raid10. This means: | ||
− | raid0 - block-pair stripped across 2 devices | + | raid0 - block-pair stripped across 2 devices |
+ | |||
raid1 - block-pair written to 2 devices | raid1 - block-pair written to 2 devices | ||
For 2 disk sets, this matches raid levels as defined in md-raid (mdadm). For 3+ disk-sets, the result is entirely different than md-raid. | For 2 disk sets, this matches raid levels as defined in md-raid (mdadm). For 3+ disk-sets, the result is entirely different than md-raid. | ||
− | For example: | + | For example: |
− | |||
− | |||
− | + | 3 1TB disks in an md based raid1 yields a {{ic|/dev/md0}} with 1 TB free space and the ability to safely loose 2 disks without losing data. | |
+ | 3 1TB disks in a Btrfs volume with data=raid1 will allow the storage of approximately 1.5 TB of data before reporting full. Only 1 disk can safely be lost without losing data. | ||
+ | |||
+ | Btrfs uses a round-robin scheme to decide how block-pairs are spread among disks. As of Linux 3.0, a quasi-round-robin scheme is used which prefers larger disks when distributing block pairs. This allows raid0 and raid1 to take advantage of most (and sometimes all) space in a disk set made of multiple disks. For example, a set consisting of a 1TB disk and 2 500GB disks with data=raid1 will place a copy of every block on the 1TB disk and alternate (round-robin) placing blocks on each of the 500GB disks. Full space utilization will be made. A set made from a 1TB disk, a 750GB disk, and a 500GB disk will work the same, but the filesystem will report full with 250GB unusable on the 750GB disk. To always take advantage of the full space (even in the last example), use data=single. (data=single is akin to JBOD defined by some raid controllers) See [https://btrfs.wiki.kernel.org/index.php/FAQ#How_much_space_do_I_get_with_unequal_devices_in_RAID-1_mode.3F the BTRFS FAQ] for more info. | ||
=== Sub-volumes === | === Sub-volumes === | ||
Line 126: | Line 163: | ||
To create a sub-volume: | To create a sub-volume: | ||
− | # btrfs subvolume create [ | + | # btrfs subvolume create [''dest''/] |
For increased flexibility, install your system into a dedicated sub-volume, and, in the kernel boot parameters, use: | For increased flexibility, install your system into a dedicated sub-volume, and, in the kernel boot parameters, use: | ||
− | + | rootflags=subvol=''whatever you called the subvol'' | |
This makes system rollbacks possible. | This makes system rollbacks possible. | ||
Line 140: | Line 177: | ||
To create a snapshot: | To create a snapshot: | ||
− | # btrfs subvolume snapshot | + | # btrfs subvolume snapshot ''source'' [''dest''/]''name'' |
Snapshots are not recursive, this means that every subvolume inside subvolume will be an empty directory inside the snapshot. | Snapshots are not recursive, this means that every subvolume inside subvolume will be an empty directory inside the snapshot. | ||
Line 158: | Line 195: | ||
=== Compression === | === Compression === | ||
− | Btrfs supports transparent compression, which means every file on the partition is automatically compressed. This does not only reduce the size of those files, but also [http://www.phoronix.com/scan.php?page=article&item=btrfs_compress_2635&num=1 improves performance], in particular if using the [http://www.phoronix.com/scan.php?page=article&item=btrfs_lzo_2638&num=1 lzo algorithm]. Compression is enabled using the {{ic|1=compress=gzip}} or {{ic|1=compress=lzo}} mount options. Only files created or modified after the mount option is added will be compressed, so to fully benefit from compression it should be enabled during installation. | + | Btrfs supports transparent compression, which means every file on the partition is automatically compressed. This does not only reduce the size of those files, but also [http://www.phoronix.com/scan.php?page=article&item=btrfs_compress_2635&num=1 improves performance], in particular if using the [http://www.phoronix.com/scan.php?page=article&item=btrfs_lzo_2638&num=1 lzo algorithm], in some specific use cases (e.g. single tread with heavy file IO), while obviously harming performance on other cases (e.g. multithreaded and/or cpu intensive tasks with large file IO). Compression is enabled using the {{ic|1=compress=gzip}} or {{ic|1=compress=lzo}} mount options. Only files created or modified after the mount option is added will be compressed, so to fully benefit from compression it should be enabled during installation. |
− | However, it can quite easily be applied to a subvolume using the defragment -czlib (or whichever algorithm you so choose) command (the same command above could be used, by adding the -czlib and such, to recursively apply). Also keep in mind that for future files to be compressed, a simple | + | However, it can quite easily be applied to a subvolume using the defragment -czlib (or whichever algorithm you so choose) command (the same command above could be used, by adding the -czlib and such, to recursively apply). Also keep in mind that for future files to be compressed, a simple {{ic|chattr +c}} should be applied to some directories, so as to automatically compress new files as they come. |
After [[Beginners%27_Guide#Prepare_the_storage_drive|preparing the storage drive]], simply switch to another terminal ({{ic|Ctrl+Alt+number}}), and run the following command: | After [[Beginners%27_Guide#Prepare_the_storage_drive|preparing the storage drive]], simply switch to another terminal ({{ic|Ctrl+Alt+number}}), and run the following command: | ||
− | # mount -o remount,compress=lzo /dev/ | + | # mount -o remount,compress=lzo /dev/sd''XY'' /mnt/target |
After the installation is finished, add {{ic|1=compress=lzo}} to the mount options of the root filesystem in {{ic|/etc/[[fstab]]}}. | After the installation is finished, add {{ic|1=compress=lzo}} to the mount options of the root filesystem in {{ic|/etc/[[fstab]]}}. | ||
+ | |||
+ | === Checkpoint Interval === | ||
+ | |||
+ | Starting with Linux 3.12, users are able to change the checkpoint interval from the default 30 s to any value by appending the '''commit''' mount flag in {{ic|/etc/fstab}} for the btrfs partition. | ||
+ | |||
+ | LABEL=arch64 / btrfs defaults,noatime,ssd,compress=lzo,commit=120 0 0 | ||
=== Partitioning === | === Partitioning === | ||
− | Btrfs can occupy | + | Btrfs can occupy an entire data storage device and replace the [[MBR]] or [[GPT]] partitioning schemes. You can use [[Btrfs#Sub-volumes|subvolumes]] to simulate partitions. There are some limitations to this approach if you use only a single disk: |
− | * You can not use different [[File_Systems|file systems]] for different [[fstab|mount points]] | + | * You can not use different [[File_Systems|file systems]] for different [[fstab|mount points]]. |
− | * You can not use [[Swap|swap area]] as Btrfs does not support [[Swap#Swap_file|swap files]] and there is no place to create [[Swap#Swap_partition|swap partition]] | + | * You can not use [[Swap|swap area]] as Btrfs does not support [[Swap#Swap_file|swap files]] and there is no place to create [[Swap#Swap_partition|swap partition]]. |
− | * You can not use [[ | + | * You can not use [[UEFI]] to boot. |
− | To | + | To overwrite the existing partition table with Btrfs, run the following command: |
− | # mkfs.btrfs /dev/ | + | # mkfs.btrfs /dev/sd''X'' |
− | + | Do not specify {{ic|/dev/sda''X''}} or it will format an existing partition instead of replacing the entire partitioning scheme. | |
− | # grub-install --recheck /dev/ | + | |
+ | Install your [[Bootloaders|boot loader]] the same way you install it for a data storage device with a [[MBR|Master Boot Record]]. For example: | ||
+ | # grub-install --recheck /dev/sd''X'' | ||
for [[Grub#Install_to_440-byte_MBR_boot_code_region|GRUB]]. | for [[Grub#Install_to_440-byte_MBR_boot_code_region|GRUB]]. | ||
− | == | + | == See also == |
* [https://btrfs.wiki.kernel.org/ Btrfs Wiki] | * [https://btrfs.wiki.kernel.org/ Btrfs Wiki] | ||
Line 189: | Line 234: | ||
* [http://www.phoronix.com/scan.php?page=news_item&px=MTA0ODU Avi Miller presenting BTRFS] at SCALE 10x. Jan/2012. | * [http://www.phoronix.com/scan.php?page=news_item&px=MTA0ODU Avi Miller presenting BTRFS] at SCALE 10x. Jan/2012. | ||
* [http://www.phoronix.com/scan.php?page=news_item&px=MTA4Mzc Summary of Chris Mason's talk from LFCS 2012] | * [http://www.phoronix.com/scan.php?page=news_item&px=MTA4Mzc Summary of Chris Mason's talk from LFCS 2012] | ||
− | |||
− | |||
− |
Revision as of 10:01, 18 November 2013
zh-CN:Btrfs Template:Article summary start Template:Article summary text Template:Article summary heading Template:Article summary wiki Template:Article summary wiki Template:Article summary end
Btrfs is an abbreviation for B-tree file system and is also known as Butter FS or Better FS. Btrfs is a copy-on-write (COW) file system written from the ground up for Linux. It is aimed at implementing advanced features while focusing on fault tolerance, repair and easy administration. Jointly developed by Oracle, Red Hat, Fujitsu, Intel, SUSE and many others, Btrfs is licensed under the GPL and open for contribution from anyone.
Installation
Btrfs is included in the default kernel and its tools (btrfs-progs) are available in the official repositories. GRUB, mkinitcpio, and Syslinux have support for Btrfs and require no additional configuration.
- On 2012-03-28, btrfs-progs includes btrfsck, a tool that can fix errors on Btrfs filesystems.
- mkinitcpio-btrfsAUR: for roll-back abilities (currently unmaintained).
General administration of BTRFS
Creating a new file system
A Btrfs file system can either be newly created or have one converted.
To format a partition do:
# mkfs.btrfs -L mylabel /dev/partition
To use a larger blocksize for data/meta data, specify a value for the leafsize via the -l
switch as shown in this example using 16KB blocks:
# mkfs.btrfs -L mylabel -l 16k /dev/partition
Multiple devices can be entered to create a RAID. Supported RAID levels include RAID 0, RAID 1 and RAID 10. By default the metadata is mirrored and data is striped.
# mkfs.btrfs [options] /dev/part1 /dev/part2
Convert from Ext3/4
Boot from an install CD, then convert by doing:
# btrfs-convert /dev/partition
Mount the partion and test the conversion by checking the files. Be sure to change the /etc/fstab
to reflect the change (type to btrfs and fs_passno [the last field] to 0 as Btrfs does not do a file system check on boot). Also note that the UUID of the partition will have changed, so update your fstab accordingly if you use UUIDs. chroot
into the system and rebuild the GRUB menu list (see Install from Existing Linux and GRUB articles).
To complete, delete the saved image, delete the sub-volume that image is on, then balance the drive to reclaim the space.
# rm /ext2_saved/* # btrfs subvolume delete /ext2_saved
Displaying used/free space
General linux userspace tools such as /usr/bin/df
will inaccurately report free space on a Btrfs partition since it does not take into account space allocated for and used by the metadata. It is recommended to use /usr/bin/btrfs
to query a btrfs partition. Below is an illustration of this effect, first querying using df, and then using btrfs fi df:
# df -h /
Filesystem Size Used Avail Use% Mounted on /dev/sda3 119G 3.0G 116G 3% /
# btrfs fi df /
Data: total=3.01GB, used=2.73GB System: total=4.00MB, used=16.00KB Metadata: total=1.01GB, used=181.83MB
Notice that df -h
reports 3.0GB used but btrfs fi df
reports 2.73GB for the data. This is due to the way BTRFS allocates space into the pool. The true disk usage is the sum of all three 'used' values which is inferior to 3.0GB as reported by df -h
.
Another useful command to show a less verbose readout of used space is btrfs fi show
:
# btrfs fi show /dev/sda3
failed to open /dev/sr0: No medium found Label: 'arch64' uuid: 02ad2ea2-be12-2233-8765-9e0a48e9303a Total devices 1 FS bytes used 2.91GB devid 1 size 118.95GB used 4.02GB path /dev/sda2 Btrfs v0.20-rc1-358-g194aa4a-dirty
Limitations
A few limitations should be known before trying.
Encryption
Btrfs has no built-in encryption support (this may come in future), but you can encrypt the partition before running mkfs.btrfs
. See Dm-crypt with LUKS.
If you've already created a Btrfs file system, you can also use something like EncFS or TrueCrypt, though perhaps without some of Btrfs' features.
Swap file
Btrfs does not support swap files. This is due to swap files requiring a function that Btrfs doesn't have for possibility of corruptions.link A swap file can be mounted on a loop device with poorer performance but will not be able to hibernate. A systemd service file is available systemd-loop-swapfileAUR.
GRUB2 and core.img
GRUB can boot Btrfs partitions however the module is larger than e.g. ext4 and the core.img
file made by grub-install may not fit between the MBR and the first partition. This can be solved by using GPT or by putting an extra 1 or 2 MB of free space before the first partition.
If you get the following: error no such device: root
when booting from a RAID style setup then edit /usr/share/grub/grub-mkconfig_lib and remove both quotes from the line echo " search --no-floppy --fs-uuid --set=root ${hints} ${fs_uuid}"
. Regenerate the config for grub and your system should boot without an error.
Features
Various features are available and can be adjusted.
Copy-On-Write (CoW)
The resolution at which data are written to the filesystem is dictated by Btrfs itself and by system-wide settings. Btrfs defaults to a 30 seconds checkpoint interval in which new data are committed to the filesystem. As of Btrfs v0.20-rc1-253-g7854c8b users cannot tweak this without recompiling a patched version of fs/btrfs/disk-io.c. On 01-Aug-2013, David Sterba submitted this patch to make this a formal mount time tuneable.
System-wide settings also affect commit intervals. They include the files under /proc/sys/vm/*
and are out-of-scope of this wiki article. The kernel documentation for them resides in Documentation/sysctl/vm.txt
.
CoW comes with some advantages, but can negatively affect performance with large files that have small random writes. It is recommended to disable CoW for database files and virtual machine images.
You can disable CoW for the entire block device by mounting it with nodatacow
option. However, this will disable CoW for the entire file system.
To disable CoW for single files/directories do:
# chattr +C /dir/file
Note, from chattr man page: For btrfs, the 'C' flag should be set on new or empty files. If it is set on a file which already has data blocks, it is undefined when the blocks assigned to the file will be fully stable. If the 'C' flag is set on a directory, it will have no effect on the directory, but new files created in that directory will have the No_COW attribute.
Likewise, to save space by forcing CoW when copying files use:
# cp --reflink source dest
As dest file is changed, only those blocks that are changed from source will be written to the disk. One might consider aliasing aliasing cp to cp --reflink=auto
.
Multi-device filesystem and RAID feature
Multi-device filesystem
When creating a btrfs filesystem, you can pass as many partitions or disk devices as you want to mkfs.btrfs. The filesystem will be created across these devices. You can "pool" this way, multiple partitions or devices to get a big btrfs filesystem.
You can also add or remove device from an existing btrfs filesystem (caution is mandatory).
A multi-device btrfs filesystem (also called a btrfs volume) is not recognized until
# btrfs device scan
has been run. This is the purpose of the btrfs mkinitcpio hook.
RAID features
When creating multi-device filesystem, you can also specify to use RAID0, RAID1 or RAID10 across the devices you have added to the filesystem. RAID levels can be applied independently to data and meta data. By default, meta data is duplicated on single volumes or RAID1 on multi-disk sets.
btrfs works in block-pairs for raid0, raid1, and raid10. This means:
raid0 - block-pair stripped across 2 devices
raid1 - block-pair written to 2 devices
For 2 disk sets, this matches raid levels as defined in md-raid (mdadm). For 3+ disk-sets, the result is entirely different than md-raid.
For example:
3 1TB disks in an md based raid1 yields a /dev/md0
with 1 TB free space and the ability to safely loose 2 disks without losing data.
3 1TB disks in a Btrfs volume with data=raid1 will allow the storage of approximately 1.5 TB of data before reporting full. Only 1 disk can safely be lost without losing data.
Btrfs uses a round-robin scheme to decide how block-pairs are spread among disks. As of Linux 3.0, a quasi-round-robin scheme is used which prefers larger disks when distributing block pairs. This allows raid0 and raid1 to take advantage of most (and sometimes all) space in a disk set made of multiple disks. For example, a set consisting of a 1TB disk and 2 500GB disks with data=raid1 will place a copy of every block on the 1TB disk and alternate (round-robin) placing blocks on each of the 500GB disks. Full space utilization will be made. A set made from a 1TB disk, a 750GB disk, and a 500GB disk will work the same, but the filesystem will report full with 250GB unusable on the 750GB disk. To always take advantage of the full space (even in the last example), use data=single. (data=single is akin to JBOD defined by some raid controllers) See the BTRFS FAQ for more info.
Sub-volumes
One of the features of Btrfs is the use of sub-volumes. Sub-volumes are basically a named btree that holds files and directories. They have inodes inside the tree of tree roots and can have non-root owners and groups. Sub-volumes can optionally be given a quota of blocks. All of the blocks and file extents inside of sub-volumes are reference counted to allow snapshotting. This is similar to the dynamically expanding storage of a virtual machine that will only use as much space on a device as needed, eliminating several half-filled partitions. One can also mount the sub-volumes with different mount options, giving more flexibility in security.
To create a sub-volume:
# btrfs subvolume create [dest/]
For increased flexibility, install your system into a dedicated sub-volume, and, in the kernel boot parameters, use:
rootflags=subvol=whatever you called the subvol
This makes system rollbacks possible.
If using for the root partition, it is advisable to add crc32c (or crc32c-intel for Intel machines) to the modules array in /etc/mkinitcpio.conf
.
Snapshots
To create a snapshot:
# btrfs subvolume snapshot source [dest/]name
Snapshots are not recursive, this means that every subvolume inside subvolume will be an empty directory inside the snapshot.
Defragmentation
Btrfs supports online defragmentation. To defragment the metadata of the root folder do:
# btrfs filesystem defragment /
This will not defragment the entire system. For more information read this page on the btrfs wiki.
To defragment the entire system verbosely do:
# find / -xdev -type f -print -exec btrfs filesystem defrag '{}' \;
Compression
Btrfs supports transparent compression, which means every file on the partition is automatically compressed. This does not only reduce the size of those files, but also improves performance, in particular if using the lzo algorithm, in some specific use cases (e.g. single tread with heavy file IO), while obviously harming performance on other cases (e.g. multithreaded and/or cpu intensive tasks with large file IO). Compression is enabled using the compress=gzip
or compress=lzo
mount options. Only files created or modified after the mount option is added will be compressed, so to fully benefit from compression it should be enabled during installation.
However, it can quite easily be applied to a subvolume using the defragment -czlib (or whichever algorithm you so choose) command (the same command above could be used, by adding the -czlib and such, to recursively apply). Also keep in mind that for future files to be compressed, a simple chattr +c
should be applied to some directories, so as to automatically compress new files as they come.
After preparing the storage drive, simply switch to another terminal (Ctrl+Alt+number
), and run the following command:
# mount -o remount,compress=lzo /dev/sdXY /mnt/target
After the installation is finished, add compress=lzo
to the mount options of the root filesystem in /etc/fstab
.
Checkpoint Interval
Starting with Linux 3.12, users are able to change the checkpoint interval from the default 30 s to any value by appending the commit mount flag in /etc/fstab
for the btrfs partition.
LABEL=arch64 / btrfs defaults,noatime,ssd,compress=lzo,commit=120 0 0
Partitioning
Btrfs can occupy an entire data storage device and replace the MBR or GPT partitioning schemes. You can use subvolumes to simulate partitions. There are some limitations to this approach if you use only a single disk:
- You can not use different file systems for different mount points.
- You can not use swap area as Btrfs does not support swap files and there is no place to create swap partition.
- You can not use UEFI to boot.
To overwrite the existing partition table with Btrfs, run the following command:
# mkfs.btrfs /dev/sdX
Do not specify /dev/sdaX
or it will format an existing partition instead of replacing the entire partitioning scheme.
Install your boot loader the same way you install it for a data storage device with a Master Boot Record. For example:
# grub-install --recheck /dev/sdX
for GRUB.
See also
- Btrfs Wiki
- BTRFS Problem FAQ - Official FAQ
- Funtoo Btrfs wiki entry - Very well-written article
- Avi Miller presenting BTRFS at SCALE 10x. Jan/2012.
- Summary of Chris Mason's talk from LFCS 2012