Btrfs: Difference between revisions

From ArchWiki
m (style)
(→‎Creating a subvolume: sync the "--parents" description with the btrfs-subvolume(8) man page (the destination path must not exist and is always created regardless of this option))
 
(248 intermediate revisions by 81 users not shown)
Line 1: Line 1:
[[Category:File systems]]
[[Category:File systems]]
[[de:Arch_auf_BtrFS]]
[[de:Arch auf BtrFS]]
[[fr:Btrfs]]
[[es:Btrfs]]
[[it:Btrfs]]
[[ja:Btrfs]]
[[ja:Btrfs]]
[[pt:Btrfs]]
[[pt:Btrfs]]
[[ru:Btrfs]]
[[zh-hans:Btrfs]]
[[zh-hans:Btrfs]]
{{Related articles start}}
{{Related articles start}}
{{Related|File systems}}
{{Related|File systems}}
{{Related|Snapper}}
{{Related|Snapper}}
{{Related|dm-crypt/Encrypting an entire system#Btrfs subvolumes with swap}}
{{Related|Timeshift}}
{{Related|Yabsnap}}
{{Related articles end}}
{{Related articles end}}


From [https://btrfs.wiki.kernel.org/index.php/Main_Page Btrfs Wiki]:
From the [https://btrfs.readthedocs.io/en/latest/Introduction.html Btrfs Documentation]:
:Btrfs is a modern copy on write (COW) file system for Linux aimed at implementing advanced features while also focusing on fault tolerance, repair and easy administration.


:Btrfs is a modern copy on write (CoW) filesystem for Linux aimed at implementing advanced features while also focusing on fault tolerance, repair and easy administration. Jointly developed at multiple companies, Btrfs is licensed under the GPL and open for contribution from anyone.
{{Note|Like some other file systems, Btrfs is under continuous development. This means that specific features may not be ready for everyday use yet. To see if your use case might be affected, check the Btrfs Documentation’s [https://btrfs.readthedocs.io/en/latest/Status.html Status] and the [[#Known issues]] section of this article.}}
 
{{Warning|Btrfs has some features that are unstable. See the Btrfs Wiki's [https://btrfs.wiki.kernel.org/index.php/Status Status], [https://btrfs.wiki.kernel.org/index.php/FAQ#Is_btrfs_stable.3F Is Btrfs stable?] and [https://btrfs.wiki.kernel.org/index.php/Getting_started Getting started] for more detailed information. See the [[#Known issues]] section.}}  


== Preparation ==
== Preparation ==


For user space utilities [[install]] the {{Pkg|btrfs-progs}} package, which is required for basic operations.
For user space utilities, [[install]] the {{Pkg|btrfs-progs}} package that is required for basic operations.


If you need to boot from a Btrfs file system (i.e., your kernel and initramfs reside on a Btrfs partition), check if your [[boot loader]] supports Btrfs.
If you need to boot from a Btrfs file system (i.e., your kernel and initramfs reside on a Btrfs partition), check if your [[boot loader]] supports Btrfs.
Line 25: Line 27:
== File system creation ==
== File system creation ==


The following shows how to create a new Btrfs [[file system]]. To convert an ext3/4 partition to Btrfs, see [[#Ext3/4 to Btrfs conversion]]. To use a partitionless setup, see [[#Partitionless Btrfs disk]].
The following shows how to create a new Btrfs [[file system]]. To convert an Ext3/4 partition to Btrfs, see [[#Ext3/4 to Btrfs conversion]]. To use a partitionless setup, see [[#Partitionless Btrfs disk]].


See {{man|8|mkfs.btrfs}} for more information.
See {{man|8|mkfs.btrfs}} for more information.
Line 31: Line 33:
=== File system on a single device ===
=== File system on a single device ===


To create a Btrfs filesystem on partition {{ic|/dev/''partition''}}:
To create a Btrfs file system on partition {{ic|/dev/''partition''}}:


  # mkfs.btrfs -L ''mylabel'' /dev/''partition''
  # mkfs.btrfs -L ''mylabel'' /dev/''partition''


The Btrfs default nodesize for metadata is 16KB, while the default sectorsize for data is equal to page size and autodetected. To use a larger nodesize for metadata (must be a multiple of sectorsize, up to 64KB is allowed), specify a value for the {{ic|nodesize}} via the {{ic|-n}} switch as shown in this example using 32KB blocks:
The Btrfs default nodesize for metadata is 16 KiB, while the default sectorsize for data is equal to page size and autodetected. To use a larger nodesize for metadata (must be a multiple of sectorsize, up to 64 KiB is allowed), specify a value for the {{ic|nodesize}} via the {{ic|-n}} switch as shown in this example using 32 KiB blocks:


  # mkfs.btrfs -L ''mylabel'' -n 32k /dev/''partition''
  # mkfs.btrfs -L ''mylabel'' -n 32k /dev/''partition''
{{Note|According to {{man|8|mkfs.btrfs|OPTIONS}}, "[a] smaller node size increases fragmentation but leads to taller b-trees which in turn leads to lower locking contention. Higher node sizes give better packing and less fragmentation at the cost of more expensive memory operations while updating the metadata blocks".}}


=== Multi-device file system ===
=== Multi-device file system ===


{{Warning|The RAID 5 and RAID 6 modes of Btrfs are fatally flawed, and should not be used for "anything but testing with throw-away data." [https://lore.kernel.org/linux-btrfs/20200627032414.GX10769@hungrycats.org/ List of known problems and partial workarounds.] See [https://btrfs.wiki.kernel.org/index.php/RAID56 the Btrfs page on RAID5 and RAID6] for status updates (seems to not be updated).}}
{{Warning|The RAID 5 and RAID 6 modes of Btrfs are fatally flawed, and should not be used for "anything but testing with throw-away data." [https://lore.kernel.org/linux-btrfs/20200627032414.GX10769@hungrycats.org/ List of known problems and partial workarounds]. See {{man|5|btrfs|RAID56 STATUS AND RECOMMENDED PRACTICES}} for status updates.}}
 
Multiple devices can be used to create a RAID. Supported RAID levels include RAID 0, RAID 1, RAID 10, RAID 5 and RAID 6. Starting from kernel 5.5 RAID1c3 and RAID1c4 for 3- and 4- copies of RAID 1 level. The RAID levels can be configured separately for data and metadata using the {{ic|-d}} and {{ic|-m}} options respectively. By default the data is striped ({{ic|raid0}}) and the metadata is mirrored ({{ic|raid1}}). See [https://btrfs.wiki.kernel.org/index.php/Using_Btrfs_with_Multiple_Devices Using Btrfs with Multiple Devices] for more information about how to create a Btrfs RAID volume.


# mkfs.btrfs -d raid0 -m raid1 /dev/''part1'' /dev/''part2'' ...
Multiple devices can be used to create a RAID. Supported RAID levels include RAID 0, RAID 1, RAID 10, RAID 5 and RAID 6. Starting from kernel 5.5 RAID1c3 and RAID1c4 for 3- and 4- copies of RAID 1 level. The RAID levels can be configured separately for data and metadata using the {{ic|-d}} and {{ic|-m}} options respectively. By default, the data has one copy ({{ic|single}}) and the metadata is mirrored ({{ic|raid1}}). This is similar to
creating a [[W:JBOD|JBOD configuration]], where disks are seen as one file system, but files are not duplicated. See [https://archive.kernel.org/oldwiki/btrfs.wiki.kernel.org/index.php/Using_Btrfs_with_Multiple_Devices.html Using Btrfs with Multiple Devices] for more information about how to create a Btrfs RAID volume.


You can create a [[W:JBOD|JBOD configuration]], where disks are seen as one filesystem, but files are not duplicated, using {{ic|-d single -m raid1}}.
# mkfs.btrfs -d single -m raid1 /dev/''part1'' /dev/''part2'' ...


You must include either the {{ic|udev}} hook or the {{ic|btrfs}} hook in {{ic|/etc/mkinitcpio.conf}} in order to use multiple Btrfs devices in a pool. See the [[Mkinitcpio#Common hooks]] article for more information.
You must include either the {{ic|udev}} hook, {{ic|systemd}} hook or the {{ic|btrfs}} hook in {{ic|/etc/mkinitcpio.conf}} in order to use multiple Btrfs devices in a pool. See the [[Mkinitcpio#Common hooks]] article for more information.


{{Note|
{{Note|
* It is possible to add devices to a multiple-device filesystem later on. See the [https://btrfs.wiki.kernel.org/index.php/Using_Btrfs_with_Multiple_Devices Btrfs wiki article] for more information.
* It is possible to add devices to a multiple-device file system later on. See the [https://archive.kernel.org/oldwiki/btrfs.wiki.kernel.org/index.php/Using_Btrfs_with_Multiple_Devices.html Btrfs wiki article] for more information.
* Devices can be of different sizes. However, if one drive in a RAID configuration is bigger than the others, this extra space will not be used.  
* Devices can be of different sizes. However, if one drive in a RAID configuration is bigger than the others, this extra space will not be used.
* Some [[boot loader]]s such as [[Syslinux]] do not support multi-device file systems.
* Some [[boot loader]]s such as [[Syslinux]] do not support multi-device file systems.
* Btrfs does not automatically read from the fastest device, so mixing different kinds of disks results in inconsistent performance. See [https://stackoverflow.com/a/55408367 this Stack Overflow answer] for details.
* Btrfs does not automatically read from the fastest device, so mixing different kinds of disks results in inconsistent performance. See [https://stackoverflow.com/a/55408367] for details.
}}
}}


Line 64: Line 67:
=== Copy-on-Write (CoW) ===
=== Copy-on-Write (CoW) ===


By default, Btrfs uses [[Wikipedia:copy-on-write|copy-on-write]] for all files all the time. See the [https://btrfs.wiki.kernel.org/index.php/SysadminGuide#Copy_on_Write_.28CoW.29 Btrfs Sysadmin Guide section] for implementation details, as well as advantages and disadvantages.
By default, Btrfs uses [[Wikipedia:copy-on-write|copy-on-write]] for all files all the time. Writes do not overwrite data in place; instead, a modified copy of the block is written to a new location, and metadata is updated to point at the new location. See the [https://archive.kernel.org/oldwiki/btrfs.wiki.kernel.org/index.php/SysadminGuide.html#Copy_on_Write_.28CoW.29 Btrfs Sysadmin Guide section] for implementation details, as well as advantages and disadvantages.


==== Disabling CoW ====
==== Disabling CoW ====
{{Warning|Disabling CoW in Btrfs also disables checksums. Btrfs will not be able to detect corrupted {{ic|nodatacow}} files. When combined with RAID 1, power outages or other sources of corruption can cause the data to become out of sync.}}


To disable copy-on-write for newly created files in a mounted subvolume, use the {{ic|nodatacow}} mount option. This will only affect newly created files. Copy-on-write will still happen for existing files. The {{ic|nodatacow}} option also disables compression. See {{man|5|btrfs}} for details.
To disable copy-on-write for newly created files in a mounted subvolume, use the {{ic|nodatacow}} mount option. This will only affect newly created files. Copy-on-write will still happen for existing files. The {{ic|nodatacow}} option also disables compression. See {{man|5|btrfs}} for details.


{{Note|From {{man|5|btrfs|MOUNT OPTIONS}}: "within a single file system, it is not possible to mount some subvolumes with {{ic|nodatacow}} and others with {{ic|datacow}}. The mount option of the first mounted subvolume applies to any other subvolumes."}}
{{Note|From {{man|5|btrfs|MOUNT OPTIONS}}:


To disable copy-on-write for single files/directories do:
:within a single file system, it is not possible to mount some subvolumes with {{ic|nodatacow}} and others with {{ic|datacow}}. The mount option of the first mounted subvolume applies to any other subvolumes.
}}
 
To disable copy-on-write for single files/directories, do:


  $ chattr +C ''/dir/file''
  $ chattr +C ''/dir/file''


This will disable copy-on-write for those operation in which there is only one reference to the file. If there is more than one reference (e.g. through {{ic|1=cp --reflink=always}} or because of a filesystem snapshot), copy-on-write still occurs.
This will disable copy-on-write for those operation in which there is only one reference to the file. If there is more than one reference, e.g. due to file clones or lightweight clones or file system snapshots, copy-on-write still occurs. Note that as of {{Pkg|coreutils}} 9.0, {{ic|cp}} attempts to perform lightweight copies by default — see {{man|1|cp}} for more details.


{{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 {{man|1|chattr}}:


{{Tip|In accordance with the note above, you can use the following trick to disable copy-on-write on existing files in a directory:
:For Btrfs, the '{{ic|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 '{{ic|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 {{ic|No_COW}} attribute.
}}
 
{{Tip|1=In accordance with the note above, you can use the following trick to disable copy-on-write on existing files in a directory:


  $ mv ''/path/to/dir'' ''/path/to/dir''_old
  $ mv ''/path/to/dir'' ''/path/to/dir''_old
  $ mkdir ''/path/to/dir''
  $ mkdir ''/path/to/dir''
  $ chattr +C ''/path/to/dir''
  $ chattr +C ''/path/to/dir''
  $ cp -a ''/path/to/dir''_old/* ''/path/to/dir''
  $ cp -a --reflink=never ''/path/to/dir''_old/. ''/path/to/dir''
  $ rm -rf ''/path/to/dir''_old
  $ rm -rf ''/path/to/dir''_old


Make sure that the data are not used during this process. Also note that {{ic|mv}} or {{ic|cp --reflink}} as described below will not work.
Make sure that the data are not used during this process. Also note that {{ic|mv}} or {{ic|cp}} without {{ic|1=--reflink=never}} as described below will not work.
}}
}}


==== Creating lightweight copies ====
===== Effect on snapshots =====


By default, when copying files on a Btrfs filesystem with {{ic|cp}}, actual copies are created. To create a lightweight copy referencing to the original data, use the ''reflink'' option:
If a file has copy-on-write disabled (NOCOW) and a [[#Snapshots|snapshot]] is taken, the first write to a file block after the snapshot [https://www.spinics.net/lists/linux-btrfs/msg33090.html will be a COW operation] because the snapshot locks the old file blocks in place. However, the file will retain the NOCOW attribute and any subsequent writes to the same file block will be in-place until the next snapshot.


$ cp --reflink ''source'' ''dest''
Frequent snapshots can reduce the effectiveness of NOCOW, as COW is still required on the first write. To avoid copy-on-write for such files altogether, put them in a separate subvolume and do not take snapshots of that subvolume.


See the man page on {{man|1|cp}} for more details on the {{ic|--reflink}} flag.
=== Compression ===


=== Compression ===
Btrfs supports [https://btrfs.readthedocs.io/en/latest/Compression.html transparent and automatic compression]. This reduces the size of files as well as significantly increases the lifespan of flash-based media by reducing write amplification. See [[Fedora:Changes/BtrfsByDefault#Compression]], [https://lists.fedoraproject.org/archives/list/devel@lists.fedoraproject.org/message/NTV77NFF6NDZM3QTPUM2TQZ5PCM6GOO2/], and [https://pagure.io/fedora-btrfs/project/issue/36#comment-701551]. It can also [https://www.phoronix.com/scan.php?page=article&item=btrfs_compress_2635&num=1 improve performance], in some cases (e.g. single thread with heavy file I/O), while obviously harming performance in other cases (e.g. multi-threaded and/or CPU intensive tasks with large file I/O). Better performance is generally achieved with the fastest compress algorithms, ''zstd'' and ''lzo'', and some [https://www.phoronix.com/scan.php?page=article&item=btrfs-zstd-compress benchmarks] provide detailed comparisons.
 
LZO has a fixed compression level, whereas zlib and zstd have a range of levels from 1 (low compression) to 9 (zlib) or 15 (zstd); see {{man|5|btrfs|COMPRESSION}}. Changing the levels will affect CPU and I/O throughput differently, so they should be checked / benchmarked before and after changing.


Btrfs supports transparent and automatic compression. This not only reduces the size of files, but can also [http://www.phoronix.com/scan.php?page=article&item=btrfs_compress_2635&num=1 improve performance], in some specific use cases (e.g. single thread with heavy file I/O), while obviously harming performance in other cases (e.g. multithreaded and/or cpu intensive tasks with large file I/O). Better performance is generally achieved with the fastest compress algorithms, ''zstd'' and ''lzo'', and some [https://www.phoronix.com/scan.php?page=article&item=btrfs-zstd-compress benchmarks] provide detailed comparisons.
The {{ic|1=compress=''alg[:level]''}} mount option enables automatically considering every file for compression, where {{ic|''alg''}} is either {{ic|zlib}}, {{ic|lzo}}, {{ic|zstd}}, or {{ic|no}} (for no compression). Using this option, Btrfs will check if compressing the first portion of the data shrinks it. If it does, the entire write to that file will be compressed. If it does not, none of it is compressed.  With this option, if the first portion of the write does not shrink, no compression will be applied to the write even if the rest of the data would shrink tremendously. [https://btrfs.readthedocs.io/en/latest/Compression.html#incompressible-data] This is done to prevent making the disk wait to start writing until all of the data to be written is fully given to Btrfs and compressed.


The {{ic|1=compress=''alg''}} mount option enables automatically considering every file for compression, where {{ic|''alg''}} is either {{ic|zlib}}, {{ic|lzo}}, {{ic|zstd}}, or {{ic|no}} (for no compression). Using this option, btrfs will check if compressing the first portion of the data shrinks it.  If it does, the entire write to that file will be compressed.  If it does not, none of it is compressed.  With this option, if the first portion of the write does not shrink, no compression will be applied to the write even if the rest of the data would shrink tremendously. [https://btrfs.wiki.kernel.org/index.php/Compression#What_happens_to_incompressible_files.3F]  This is done to prevent making the disk wait to start writing until all of the data to be written is fully given to btrfs and compressed.
{{Expansion|Missing reference for the "empirical testing" mentioned in the following paragraph.}}


The {{ic|1=compress-force=''alg''}} mount option can be used instead, which makes btrfs skip checking if compression shrinks the first portion, and enables automatic compression for every file.  In a worst-case scenario, this can cause (slightly) more space to be used, and CPU usage for no purpose. However, empirical testing on multiple mixed-use systems showed a significant improvement of about 10% disk compression from using {{ic|1=compress-force=''zstd''}} over just {{ic|1=compress=''zstd''}} (which also had 10% disk compression), resulting in a total effective disk space saving of 20%.
The {{ic|1=compress-force=''alg[:level]''}} mount option can be used instead, which makes Btrfs skip checking if compression shrinks the first portion, and enables automatic compression try for every file.  In a worst-case scenario, this can cause (slightly) more CPU usage for no purpose. However, empirical testing on multiple mixed-use systems showed a significant improvement of about 10% disk compression from using {{ic|1=compress-force=''zstd''}} over just {{ic|1=compress=''zstd''}}, which also had 10% disk compression. However, keep in mind that forcing compression is against [https://btrfs.readthedocs.io/en/latest/Compression.html#incompressible-data official Btrfs guidelines].


Only files created or modified after the mount option is added will be compressed.
Only files created or modified after the mount option is added will be compressed.
Line 112: Line 125:


  # btrfs filesystem defragment -r -v -czstd /
  # btrfs filesystem defragment -r -v -czstd /
{{Warning| Defragmenting a file which has a COW copy (either a snapshot copy or one made with {{ic|cp}} or bcp) plus using the {{ic|-c}} switch with a compression algorithm may result in two unrelated files effectively increasing the disk usage.}}


To enable compression when installing Arch to an empty Btrfs partition, use the {{ic|compress}} option when [[mounting]] the file system: {{ic|1=mount -o compress=zstd /dev/sd''xY'' /mnt/}}. During configuration, add {{ic|1=compress=zstd}} to the mount options of the root file system in [[fstab]].
To enable compression when installing Arch to an empty Btrfs partition, use the {{ic|compress}} option when [[mounting]] the file system: {{ic|1=mount -o compress=zstd /dev/sd''xY'' /mnt/}}. During configuration, add {{ic|1=compress=zstd}} to the mount options of the root file system in [[fstab]].


{{Tip|Compression can also be enabled per-file without using the {{ic|compress}} mount option; to do so apply {{ic|chattr +c}} to the file. When applied to directories, it will cause new files to be automatically compressed as they come.}}
{{Tip|Compression can also be enabled per-file without using the {{ic|compress}} mount option; to do so, apply {{ic|chattr +c}} '''or''' {{ic|btrfs property set ''file'' compression ''alg''}} to the file. The first command is using legacy interface of file attributes inherited from ext2 filesystem and is not flexible, so by default the {{ic|zlib}} compression is set. The other command sets a property on the file with the given algorithm. (Note: setting level that way is not yet implemented.) When applied to directories, it will cause new files to be automatically compressed as they come.}}


{{Warning|
{{Warning|
* Systems using older kernels or {{pkg|btrfs-progs}} without {{ic|zstd}} support may be unable to read or repair your filesystem if you use this option.
* Systems using older kernels or {{pkg|btrfs-progs}} without {{ic|zstd}} support may be unable to read or repair your file system if you use this option.
* [[GRUB]] introduced ''zstd'' support in 2.04.  Make sure you have actually upgraded the bootloader installed in your MBR/ESP since then, by running {{ic|grub-install}} with the appropriate options for your BIOS/UEFI setup, since that is not done automatically. See {{Bug|63235}}.
* [[GRUB]] introduced ''zstd'' support in 2.04.  Make sure you have actually upgraded the bootloader installed in your MBR/ESP since then, by running {{ic|grub-install}} with the appropriate options for your BIOS/UEFI setup, since that is not done automatically. See {{Bug|63235}}.
* [[rEFInd]] before version 0.11.4 lacks support for ''zstd'', either switch to {{AUR|refind-git}}, use a separate boot partition without ''zstd'', or reset compression of boot files to something supported using for example the command: {{bc|$ btrfs filesystem defragment -v -clzo /boot/*}}
}}
}}


==== View compression types and ratios ====
==== View compression types and ratios ====


{{pkg|compsize}} takes a list of files (or an entire btrfs filesystem) and measures compression types used and effective compression ratios.  Uncompressed size may not match the number given by other programs such as {{ic|du}}, because every extent is counted once, even if it is reflinked several times, and even if part of it is no longer used anywhere but has not been garbage collected.  The {{ic|-x}} option keeps it on a single filesystem, which is useful in situations like {{ic|compsize -x /}} to avoid it from attempting to look in non-btrfs subdirectories and fail the entire run.
{{pkg|compsize}} takes a list of files (or an entire Btrfs file system) and measures compression types used and effective compression ratios.  Uncompressed size may not match the number given by other programs such as {{man|1|du}}, because every extent is counted once, even if it is reflinked several times, and even if a part of it is no longer used anywhere but has not been garbage collected.  The {{ic|-x}} option keeps it on a single file system, which is useful in situations like {{ic|compsize -x /}} to avoid it from attempting to look in non-Btrfs subdirectories and fail the entire run.


=== Subvolumes ===
=== Subvolumes ===


"A btrfs subvolume is not a block device (and cannot be treated as one) instead, a btrfs subvolume can be thought of as a POSIX file namespace. This namespace can be accessed via the top-level subvolume of the filesystem, or it can be mounted in its own right." [https://btrfs.wiki.kernel.org/index.php/SysadminGuide#Subvolumes]
"A Btrfs subvolume is not a block device (and cannot be treated as one) instead, a Btrfs subvolume can be thought of as a POSIX file namespace. This namespace can be accessed via the top-level subvolume of the file system, or it can be mounted in its own right." [https://archive.kernel.org/oldwiki/btrfs.wiki.kernel.org/index.php/SysadminGuide.html#Subvolumes]


Each Btrfs file system has a top-level subvolume with ID 5. It can be mounted as {{ic|/}} (by default), or another subvolume can be [[#Mounting subvolumes|mounted]] instead. Subvolumes can be moved around in the filesystem and are rather identified by their id than their path.
Each Btrfs file system has a top-level subvolume with ID 5. This subvolume cannot be removed or replaced by another subvolume. The top-level subvolume has path {{ic|/}} on the file system and other subvolumes are ''nested'' below the top-level subvolume. However, subvolumes can be moved around in the file system and their path may change, whereas their ID cannot.
 
By default, the top-level subvolume is mounted when mounting the file system. Options allow to [[#Mounting subvolumes|mount a specific subvolume]] instead.
 
A major use case for subvolumes are [[#Snapshots|snapshots]].


See the following links for more details:
See the following links for more details:


* [https://btrfs.wiki.kernel.org/index.php/SysadminGuide#Subvolumes Btrfs Wiki SysadminGuide#Subvolumes]
* [https://btrfs.readthedocs.io/en/latest/Subvolumes.html Btrfs Documentation]
* [https://btrfs.wiki.kernel.org/index.php/Getting_started#Basic_Filesystem_Commands Btrfs Wiki Getting started#Basic Filesystem Commands]
* [https://archive.kernel.org/oldwiki/btrfs.wiki.kernel.org/index.php/SysadminGuide.html#Subvolumes Btrfs Wiki SysadminGuide#Subvolumes]
* [https://btrfs.wiki.kernel.org/index.php/Trees Btrfs Wiki Trees]  
* [https://archive.kernel.org/oldwiki/btrfs.wiki.kernel.org/index.php/Trees.html Btrfs Wiki Trees]


==== Creating a subvolume ====
==== Creating a subvolume ====


To create a subvolume:
To create a subvolume, the Btrfs file system must be mounted. The subvolume's name is set using the last argument.


  # btrfs subvolume create ''/path/to/subvolume''
  # btrfs subvolume create ''/path/to/subvolume''
{{Note|You can use {{ic|--parents}} to automatically create parent directories if they do not exist.}}


==== Listing subvolumes ====
==== Listing subvolumes ====
Line 157: Line 177:
  # btrfs subvolume delete ''/path/to/subvolume''
  # btrfs subvolume delete ''/path/to/subvolume''


Since Linux 4.18, one can also delete a subvolume like a regular directory ({{ic|<nowiki>rm -r</nowiki>}}, {{ic|<nowiki>rmdir</nowiki>}}).
Alternatively, a subvolume can be deleted like a regular directory ({{ic|rm -r}}, {{ic|rmdir}}).


==== Mounting subvolumes ====
==== Mounting subvolumes ====


Subvolumes can be mounted like file system partitions using the {{ic|1=subvol=''/path/to/subvolume''}} or {{ic|1=subvolid=''objectid''}} mount flags. For example, you could have a subvolume named {{ic|subvol_root}} and mount it as {{ic|/}}.  One can mimic traditional file system partitions by creating various subvolumes under the top level of the file system and then mounting them at the appropriate mount points. Thus one can easily restore a file system (or part of it) to a previous state using [[#Snapshots]].
Subvolumes can be mounted like file system partitions using the {{ic|1=subvol=''/path/to/subvolume''}} or {{ic|1=subvolid=''objectid''}} mount flags. For example, you could have a subvolume named {{ic|subvol_root}} and mount it as {{ic|/}}.  One can mimic traditional file system partitions by creating various subvolumes under the top level of the file system and then mounting them at the appropriate mount points. It is preferable to mount using {{ic|1=subvol=''/path/to/subvolume''}}, rather than the subvolid, as the subvolid may change when restoring [[#Snapshots]], requiring a change of mount configuration.
 
{{Tip|1= Changing subvolume layouts is made simpler by not using the top-level subvolume (ID=5) as {{ic|/}} (which is done by default). Instead, consider creating a subvolume to house your actual data and mounting it as {{ic|/}}.}}


{{Tip|1= Changing subvolume layouts is made simpler by not using the toplevel subvolume (ID=5) as {{ic|/}} (which is done by default). Instead, consider creating a subvolume to house your actual data and mounting it as {{ic|/}}.}}
{{Note|From {{man|5|btrfs|MOUNT OPTIONS}}:
:Most mount options apply to the '''whole file system''', and only the options for the first subvolume to be mounted will take effect. This is due to lack of implementation and may change in the future.


{{Note|From {{man|5|btrfs|MOUNT OPTIONS}}: "Most mount options apply to the '''whole filesystem''', and only the options for the first subvolume to be mounted will take effect. This is due to lack of implementation and may change in the future.". See the [https://btrfs.wiki.kernel.org/index.php/FAQ#Can_I_mount_subvolumes_with_different_mount_options.3F Btrfs Wiki FAQ] for which mount options can be used per subvolume.}}
See the [https://archive.kernel.org/oldwiki/btrfs.wiki.kernel.org/index.php/FAQ.html#Can_I_mount_subvolumes_with_different_mount_options.3F Btrfs Wiki FAQ] for which mount options can be used per subvolume.
}}


See [[Snapper#Suggested filesystem layout]], [https://btrfs.wiki.kernel.org/index.php/SysadminGuide#Managing_Snapshots Btrfs SysadminGuide#Managing Snapshots], and [https://btrfs.wiki.kernel.org/index.php/SysadminGuide#Layout Btrfs SysadminGuide#Layout] for example file system layouts using subvolumes.
See [[Snapper#Suggested filesystem layout]], [https://archive.kernel.org/oldwiki/btrfs.wiki.kernel.org/index.php/SysadminGuide.html#Managing_Snapshots Btrfs SysadminGuide#Managing Snapshots], and [https://archive.kernel.org/oldwiki/btrfs.wiki.kernel.org/index.php/SysadminGuide.html#Layout SysadminGuide#Layout] for example file system layouts using subvolumes.


See {{man|5|btrfs}} for a full list of btrfs-specific mount options.
See {{man|5|btrfs}} for a full list of Btrfs-specific mount options.


==== Mounting subvolume as root ====
==== Mounting subvolume as root ====


To use a subvolume as the root mountpoint specify the subvolume via a [[Kernel parameters#Configuration|kernel parameter]] using {{ic|1=rootflags=subvol=''/path/to/subvolume''}}. Edit the root mountpoint in {{ic|/etc/fstab}} and specify the mount option {{ic|1=subvol=}}. Alternatively the subvolume can be specified with its id, {{ic|1=rootflags=subvolid=''objectid''}} as kernel parameter and {{ic|1=subvolid=''objectid''}} as mount option in {{ic|/etc/fstab}}.
To use a subvolume as the root mountpoint, either make it the [[#Changing the default sub-volume|default subvolume]], or specify the subvolume via a [[Kernel parameters#Configuration|kernel parameter]] using {{ic|1=rootflags=subvol=''/path/to/subvolume''}}. Edit the root mountpoint in {{ic|/etc/fstab}} and specify the mount option {{ic|1=subvol=}}. Alternatively, the subvolume can be specified with its id, {{ic|1=rootflags=subvolid=''objectid''}} as kernel parameter and {{ic|1=subvolid=''objectid''}} as mount option in {{ic|/etc/fstab}}. It is preferable to mount using {{ic|1=subvol=''/path/to/subvolume''}}, rather than the subvolid, as the subvolid may change when restoring [[#Snapshots]], requiring a change of mount configuration, or else the system will not boot.


==== Changing the default sub-volume ====
==== Changing the default sub-volume ====
Line 185: Line 209:
{{Note|1=After changing the default subvolume on a system with [[GRUB]], you should run {{ic|grub-install}} again to notify the bootloader of the changes. See [https://bbs.archlinux.org/viewtopic.php?pid=1615373 this forum thread].}}
{{Note|1=After changing the default subvolume on a system with [[GRUB]], you should run {{ic|grub-install}} again to notify the bootloader of the changes. See [https://bbs.archlinux.org/viewtopic.php?pid=1615373 this forum thread].}}


Changing the default subvolume with {{ic|btrfs subvolume set-default}} will make the top level of the filesystem inaccessible, except by use of the {{ic|1=subvol=/}} or {{ic|1=subvolid=5}} mount options [https://btrfs.wiki.kernel.org/index.php/SysadminGuide].
Changing the default subvolume with {{ic|btrfs subvolume set-default}} will make the top level of the file system inaccessible, except by use of the {{ic|1=subvol=/}} or {{ic|1=subvolid=5}} mount options [https://btrfs.readthedocs.io/en/latest/Administration.html].


=== Quota ===
=== Quota ===


{{warning|Qgroup is not stable yet and combining quota with (too many) snapshots of subvolumes can cause performance problems, for example when deleting snapshots. Plus there are several more [https://btrfs.wiki.kernel.org/index.php/Quota_support#Known_issues known issues].}}
{{warning|Qgroup is not stable yet and combining quota with (too many) snapshots of subvolumes can cause performance problems, for example when deleting snapshots. Plus there are several more [https://archive.kernel.org/oldwiki/btrfs.wiki.kernel.org/index.php/Quota_support.html#Known_issues known issues].}}


Quota support in Btrfs is implemented at a subvolume level by the use of quota groups or qgroup: Each subvolume is assigned a quota groups in the form of ''0/subvolume_id'' by default. However it is possible to create a quota group using any number if desired.
Quota support in Btrfs is implemented at a subvolume level by the use of quota groups or qgroup: Each subvolume is assigned a quota groups in the form of ''0/subvolume_id'' by default. However, it is possible to create a quota group using any number if desired.


To use qgroups you need to enable quota first using
To use qgroups, you need to enable quota first using


  # btrfs quota enable ''path''
  # btrfs quota enable ''path''


From this point onwards newly created subvolumes will be controlled by those groups.
From this point onwards, newly created subvolumes will be controlled by those groups.
In order to retrospectively enable them for already existing subvolumes, enable quota normally, then create a qgroup (quota group) for each of those subvolume using their ''subvolume_id'' and rescan them:
In order to retrospectively enable them for already existing subvolumes, enable quota normally, then create a qgroup (quota group) for each of those subvolume using their ''subvolume_id'' and rescan them:


Line 205: Line 229:
Quota groups in Btrfs form a tree hierarchy, whereby qgroups are attached to subvolumes. The size limits are set per qgroup and apply when any limit is reached in tree that contains a given subvolume.
Quota groups in Btrfs form a tree hierarchy, whereby qgroups are attached to subvolumes. The size limits are set per qgroup and apply when any limit is reached in tree that contains a given subvolume.


Limits on quota groups can be applied either to the total data usage, un-shared data usage, compressed data usage or both. File copy and file deletion may both affect limits since the unshared limit of another qgroup can change if the original volume's files are deleted and only one copy is remaining. For example a fresh snapshot shares almost all the blocks with the original subvolume, new writes to either subvolume will raise towards the exclusive limit, deletions of common data in one volume raises towards the exclusive limit in the other one.
Limits on quota groups can be applied either to the total data usage, un-shared data usage, compressed data usage or both. File copy and file deletion may both affect limits since the unshared limit of another qgroup can change if the original volume's files are deleted and only one copy is remaining. For example, a fresh snapshot shares almost all the blocks with the original subvolume, new writes to either subvolume will raise towards the exclusive limit, deletions of common data in one volume raises towards the exclusive limit in the other one.


To apply a limit to a qgroup, use the command {{ic|btrfs qgroup limit}}. Depending on your usage either use a total limit, unshared limit ({{ic|-e}}) or compressed limit ({{ic|-c}}).
To apply a limit to a qgroup, use the command {{ic|btrfs qgroup limit}}. Depending on your usage, either use a total limit, unshared limit ({{ic|-e}}) or compressed limit ({{ic|-c}}).
To show usage and limits for a given path within a filesystem use
To show usage and limits for a given path within a file system, use


  # btrfs qgroup show -reF ''path''
  # btrfs qgroup show -reF ''path''
Line 214: Line 238:
=== Commit interval ===
=== Commit interval ===


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. This can be changed by appending the {{ic|commit}} mount option in {{ic|/etc/fstab}} for the btrfs partition.
The resolution at which data are written to the file system 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 file system. This can be changed by appending the {{ic|commit}} mount option in {{ic|/etc/fstab}} for the Btrfs partition.


  LABEL=arch64 / btrfs defaults,noatime,compress=lzo,commit=120 0 0
  LABEL=arch64 / btrfs defaults,compress=zstd,commit=120 0 0


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}}.
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 is available at https://docs.kernel.org/admin-guide/sysctl/vm.html.


=== SSD TRIM ===
=== SSD TRIM ===


A Btrfs filesystem is able to free unused blocks from an SSD drive supporting the TRIM command. Starting with kernel version 5.6 there is asynchronous discard support, enabled with mount option {{ic|1=discard=async}}. Freed extents are not discarded immediately, but grouped together and trimmed later by a separate worker thread, improving commit latency.
A Btrfs file system is able to free unused blocks from an SSD drive supporting the TRIM command. Asynchronous discard support is available with mount option {{ic|1=discard=async}}, and is enabled by default as of {{pkg|linux}} 6.2. Freed extents are not discarded immediately, but grouped together and trimmed later by a separate worker thread, improving commit latency.
 
Asynchronous discard can safely be used alongside periodic trim [https://lists.fedoraproject.org/archives/list/devel@lists.fedoraproject.org/thread/MLZIPQUXMJFRVSFJS6B2ACDKTYNSX3AX/].


More information about enabling and using TRIM can be found in [[Solid State Drives#TRIM]].
More information about enabling and using TRIM can be found in [[Solid State Drives#TRIM]].
Line 230: Line 256:
=== Swap file ===
=== Swap file ===


[[Swap file]]s in Btrfs are supported since Linux kernel 5.0.[https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=ed46ff3d423780fa5173b38a844bf0fdb210a2a7] The proper way to initialize a swap file is described in [[Swap file#Swap file creation]]. Configuring hibernation to a swap file is described in [[Power management/Suspend and hibernate#Hibernation into swap file on Btrfs]].
{{Note|Swap files on file systems spanning multiple devices are not supported. See {{man|5|btrfs|SWAPFILE SUPPORT}} for all limitations.}}
 
To properly initialize a swap file, first create a ''non-snapshotted'' subvolume to host the file, e.g.
 
# btrfs subvolume create /swap
 
{{Tip|Consider creating the subvolume directly below the top-level subvolume, e.g. {{ic|@swap}}. Then, make sure the subvolume is [[#Mounting subvolumes|mounted]] to {{ic|/swap}} (or any other accessible location).}}
 
Create the swap file:
 
# btrfs filesystem mkswapfile --size 4g --uuid clear /swap/swapfile
 
If {{ic|--size}} is omitted, the default of 2GiB is used.
 
Activate the swap file:


{{Note|1=For kernels version 5.0+, Btfrs has native swap file support with some limitations:
# swapon /swap/swapfile


* The swap file cannot be on a snapshotted subvolume. The proper procedure is to create a new subvolume to place the swap file in.
Finally, edit the [[fstab]] configuration to add an entry for the swap file:
* It does not support swap files on file systems that span multiple devices. See [https://btrfs.wiki.kernel.org/index.php/FAQ#Does_btrfs_support_swap_files.3F Btrfs wiki: Does btrfs support swap files?] and [https://bbs.archlinux.org/viewtopic.php?pid=1849371#p1849371 Arch forums discussion].
 
{{hc|/etc/fstab|
/swap/swapfile none swap defaults 0 0
}}
}}


{{Warning|Linux kernels before version 5.0 do not support swap files. Using a swap file with Btrfs and a kernel prior to 5.0 can lead to file system corruption.}}
For additional information, see [[fstab#Usage]].
 
{{Note|You can also create the swap file manually, by [[#Disabling CoW|setting the]] {{ic|No_COW}} attribute on the whole subvolume with {{ic|chattr}}, then following the steps in [[Swap#Swap file creation]]. See {{man|5|btrfs|SWAPFILE SUPPORT}} for an alternative method.}}
 
To use a swap file for hibernation, you might need to additionally follow the procedures described in [[Hibernation]].


=== Displaying used/free space ===
=== Displaying used/free space ===


General linux userspace tools such as {{ic|df}} will inaccurately report free space on a Btrfs partition. It is recommended to use {{ic|btrfs filesystem usage}} to query Btrfs partitions. For example:
General linux userspace tools such as {{man|1|df}} will inaccurately report free space on a Btrfs partition. It is recommended to use {{ic|btrfs filesystem usage}} to query Btrfs partitions. For example, for a full breakdown of device allocation and usage stats:


  # btrfs filesystem usage /
  # btrfs filesystem usage /
Line 248: Line 294:
{{Note|The {{ic|btrfs filesystem usage}} command does not currently work correctly with {{ic|RAID5/RAID6}} RAID levels.}}
{{Note|The {{ic|btrfs filesystem usage}} command does not currently work correctly with {{ic|RAID5/RAID6}} RAID levels.}}


See [https://btrfs.wiki.kernel.org/index.php/FAQ#How_much_free_space_do_I_have.3F] for more information.
Alternatively, {{ic|btrfs filesystem df}} allows a quick check on usage of allocated space without the requirement to run as root:
 
$ btrfs filesystem df /
 
See [https://archive.kernel.org/oldwiki/btrfs.wiki.kernel.org/index.php/FAQ.html#How_much_free_space_do_I_have.3F] for more information.
 
The same limitations apply to tools which analyze space usage for some subset of the file system, such as {{man|1|du}} or {{man|1|ncdu}}, as they do not take into account reflinks, snapshots and compression. Instead, see {{AUR|btdu}} and [[#View compression types and ratios|compsize]] for Btrfs-aware alternatives.


=== Defragmentation ===
=== Defragmentation ===


Btrfs supports online defragmentation through the mount option {{ic|autodefrag}}, see {{man|5|btrfs|MOUNT OPTIONS}}. To manually defragment your root, use:
Btrfs supports online defragmentation through the mount option {{ic|autodefrag}}; see {{man|5|btrfs|MOUNT OPTIONS}}. To manually defragment your root, use:


  # btrfs filesystem defragment -r /
  # btrfs filesystem defragment -r /
Line 258: Line 310:
Using the above command without the {{ic|-r}} switch will result in only the metadata held by the subvolume containing the directory being defragmented. This allows for single file defragmentation by simply specifying the path.
Using the above command without the {{ic|-r}} switch will result in only the metadata held by the subvolume containing the directory being defragmented. This allows for single file defragmentation by simply specifying the path.


Defragmenting a file which has a COW copy (either a snapshot copy or one made with {{ic|cp --reflink}} or bcp) plus using the {{ic|-c}} switch with a compression algorithm may result in two unrelated files effectively increasing the disk usage.
{{Warning|Defragmenting a file which has a COW copy (either a snapshot copy or one made with {{ic|cp}} or bcp) plus using the {{ic|-c}} switch with a compression algorithm may result in two unrelated files effectively increasing the disk usage.}}


=== RAID ===
=== RAID ===


Btrfs offers native "RAID" for [[#Multi-device file system]]s. Notable features which set btrfs RAID apart from [[mdadm]] are self-healing redundant arrays and online balancing. See [https://btrfs.wiki.kernel.org/index.php/Using_Btrfs_with_Multiple_Devices the Btrfs wiki page] for more information. The Btrfs sysadmin page also [https://btrfs.wiki.kernel.org/index.php/SysadminGuide#RAID_and_data_replication has a section] with some more technical background.
Btrfs offers native "RAID" for [[#Multi-device file system]]s. Notable features which set Btrfs RAID apart from [[mdadm]] are self-healing redundant arrays and online balancing. See [https://archive.kernel.org/oldwiki/btrfs.wiki.kernel.org/index.php/Using_Btrfs_with_Multiple_Devices.html the Btrfs wiki page] for more information. The Btrfs sysadmin page also [https://archive.kernel.org/oldwiki/btrfs.wiki.kernel.org/index.php/SysadminGuide.html#RAID_and_data_replication has a section] with some more technical background.


{{Warning| Parity RAID (RAID 5/6) code has multiple serious data-loss bugs in it. See the Btrfs Wiki's [https://btrfs.wiki.kernel.org/index.php/RAID56 RAID5/6 page] and a bug report on [https://www.mail-archive.com/linux-btrfs@vger.kernel.org/msg55161.html linux-btrfs mailing list] for more detailed information. In June 2020, somebody posted a [https://lore.kernel.org/linux-btrfs/20200627030614.GW10769@hungrycats.org/ comprehensive list of current issues] and a [https://lore.kernel.org/linux-btrfs/20200627032414.GX10769@hungrycats.org/ helpful recovery guide]. }}
{{Warning|Parity RAID (RAID 5/6) code has multiple serious data-loss bugs in it. See the Btrfs Wiki's [https://btrfs.readthedocs.io/en/latest/btrfs-man5.html#raid56-status-and-recommended-practices RAID5/6 page] and a bug report on [https://lore.kernel.org/linux-btrfs/8695beeb-f991-28c4-cf6b-8c92339e468f@inwind.it/ linux-btrfs mailing list] for more detailed information. In June 2020, somebody posted a [https://lore.kernel.org/linux-btrfs/20200627030614.GW10769@hungrycats.org/ comprehensive list of current issues] and a [https://lore.kernel.org/linux-btrfs/20200627032414.GX10769@hungrycats.org/ helpful recovery guide]. }}


=== Scrub ===
==== Scrub ====


The [https://btrfs.wiki.kernel.org/index.php/Glossary Btrfs Wiki Glossary] says that Btrfs scrub is "[a]n online filesystem checking tool. Reads all the data and metadata on the filesystem, and uses checksums and the duplicate copies from RAID storage to identify and repair any corrupt data."
{{Expansion|[https://unix.stackexchange.com/questions/556816/does-btrfs-scrub-examine-subvolume-or-the-device-that-subvolume-resides-on Does Btrfs scrub examine subvolume or the device that subvolume resides on?]}}


{{Warning|A running scrub process will prevent the system from suspending, see [http://comments.gmane.org/gmane.comp.file-systems.btrfs/33106 this thread]{{Dead link|2020|02|23}} for details.}}
The [https://btrfs.readthedocs.io/en/latest/Glossary.html Btrfs Wiki Glossary] says that Btrfs scrub is "[a]n online file system checking tool. Reads all the data and metadata on the file system and uses checksums and the duplicate copies from RAID storage to identify and repair any corrupt data."


==== Start manually ====
{{Note|A running scrub process will prevent the system from suspending, see [https://lore.kernel.org/linux-btrfs/20140227190656.GA28338@merlins.org/ this thread] for details.}}


To start a (background) scrub on the filesystem which contains {{ic|/}}:
===== Start manually =====
 
To start a (background) scrub on the file system which contains {{ic|/}}:


  # btrfs scrub start /
  # btrfs scrub start /
Line 282: Line 336:
  # btrfs scrub status /
  # btrfs scrub status /


==== Start with a service or timer ====
===== Start with a service or timer =====


The {{Pkg|btrfs-progs}} package brings the {{ic|btrfs-scrub@.timer}} unit for monthly scrubbing the specified mountpoint. [[Enable]] the timer with an escaped path, e.g. {{ic|btrfs-scrub@-.timer}} for {{ic|/}} and {{ic|btrfs-scrub@home.timer}} for {{ic|/home}}. You can use {{ic|systemd-escape -p ''/path/to/mountpoint''}} to escape the path, see {{man|1|systemd-escape}} for details.
The {{Pkg|btrfs-progs}} package brings the {{ic|btrfs-scrub@.timer}} unit for monthly scrubbing the specified mountpoint. [[Enable]] the timer with an escaped path, e.g. {{ic|btrfs-scrub@-.timer}} for {{ic|/}} and {{ic|btrfs-scrub@home.timer}} for {{ic|/home}}. You can use {{ic|systemd-escape -p ''/path/to/mountpoint''}} to escape the path; see {{man|1|systemd-escape}} for details.


You can also run the scrub by [[starting]] {{ic|btrfs-scrub@.service}} (with the same encoded path). The advantage of this over {{ic|btrfs scrub}} (as the root user) is that the results of the scrub will be logged in the [[systemd journal]].
You can also run the scrub by [[starting]] {{ic|btrfs-scrub@.service}} (with the same encoded path). The advantage of this over {{ic|btrfs scrub}} (as the root user) is that the results of the scrub will be logged in the [[systemd journal]].


=== Balance ===
On large NVMe drives with insufficient cooling (e.g. in a laptop), scrubbing can read the drive fast enough and long enough to get it very hot. If you are running scrubs with systemd, you can easily limit the rate of scrubbing with the {{ic|IOReadBandwidthMax}} option described in {{man|5|systemd.resource-control}} by using a [[drop-in file]].


"A balance passes all data in the filesystem through the allocator again. It is primarily intended to rebalance the data in the filesystem across the devices when a device is added or removed. A balance will regenerate missing copies for the redundant RAID levels, if a device has failed." [https://btrfs.wiki.kernel.org/index.php/Glossary] See [https://btrfs.wiki.kernel.org/index.php/FAQ#What_does_.22balance.22_do.3F Upstream FAQ page].
==== Balance ====


On a single-device filesystem a balance may be also useful for (temporarily) reducing the amount of allocated but unused (meta)data chunks. Sometimes this is needed for fixing [https://btrfs.wiki.kernel.org/index.php/FAQ#Help.21_Btrfs_claims_I.27m_out_of_space.2C_but_it_looks_like_I_should_have_lots_left.21 "filesystem full" issues].
"A balance passes all data in the file system through the allocator again. It is primarily intended to rebalance the data in the file system across the devices when a device is added or removed. A balance will regenerate missing copies for the redundant RAID levels, if a device has failed." [https://btrfs.readthedocs.io/en/latest/Glossary.html] See [https://archive.kernel.org/oldwiki/btrfs.wiki.kernel.org/index.php/FAQ.html#What_does_.22balance.22_do.3F Upstream FAQ page].


  # btrfs balance start /
On a single-device filesystem, a balance may be also useful for (temporarily) reducing the amount of allocated but unused (meta)data chunks. Sometimes this is needed for fixing [https://archive.kernel.org/oldwiki/btrfs.wiki.kernel.org/index.php/FAQ.html#Help.21_Btrfs_claims_I.27m_out_of_space.2C_but_it_looks_like_I_should_have_lots_left.21 "file system full" issues].
 
  # btrfs balance start --bg /
  # btrfs balance status /
  # btrfs balance status /


=== Snapshots ===
=== Snapshots ===


"A snapshot is simply a subvolume that shares its data (and metadata) with some other subvolume, using btrfs's COW capabilities." See [https://btrfs.wiki.kernel.org/index.php/SysadminGuide#Snapshots Btrfs Wiki SysadminGuide#Snapshots] for details.
"A snapshot is simply a subvolume that shares its data (and metadata) with some other subvolume, using Btrfs's COW capabilities." See [https://archive.kernel.org/oldwiki/btrfs.wiki.kernel.org/index.php/SysadminGuide.html#Snapshots Btrfs Wiki SysadminGuide#Snapshots] for details.


To create a snapshot:
To create a snapshot:
Line 305: Line 361:
  # btrfs subvolume snapshot ''source'' [''dest''/]''name''
  # btrfs subvolume snapshot ''source'' [''dest''/]''name''


To create a readonly snapshot add the {{ic|-r}} flag. To create writable version of a readonly snapshot, simply create a snapshot of it.
To create a readonly snapshot, add the {{ic|-r}} flag. To create a writable version of a readonly snapshot, simply create a snapshot of it.


{{Note|Snapshots are not recursive. Every nested subvolume will be an empty directory inside the snapshot.}}
{{Note|
* It is possible for a snapshot to be converted in-place from readonly to writeable with {{ic|btrfs property set -f -ts '/path/to/snapshot' ro false}}. However, this is not recommended because it [https://lore.kernel.org/linux-btrfs/06e92a0b-e71b-eb21-edb5-9d2a5513b718@gmail.com/ causes issues] with any future incremental send/receive. Making a new writeable snapshot prevents such issues.
* Snapshots are not recursive. Every nested subvolume will be an empty directory inside the snapshot.
}}


=== Send/receive ===
=== Send/receive ===


A subvolume can be sent to stdout or a file using the {{ic|send}} command. This is usually most useful when piped to a Btrfs {{ic|receive}} command. For example, to send a snapshot named {{ic|/root_backup}} (perhaps of a snapshot you made of {{ic|/}} earlier) to {{ic|/backup}} you would do the following:
A subvolume can be sent to stdout or a file using the {{ic|send}} command. This is usually most useful when piped to a Btrfs {{ic|receive}} command. For example, to send a snapshot named {{ic|/root_backup}} (perhaps of a snapshot you made of {{ic|/}} earlier) to {{ic|/backup}}, you would do the following:


  # btrfs send /root_backup | btrfs receive /backup
  # btrfs send /root_backup | btrfs receive /backup
Line 317: Line 376:
The snapshot that is sent ''must'' be readonly. The above command is useful for copying a subvolume to an external device (e.g. a USB disk mounted at {{ic|/backup}} above).
The snapshot that is sent ''must'' be readonly. The above command is useful for copying a subvolume to an external device (e.g. a USB disk mounted at {{ic|/backup}} above).


You can also send only the difference between two snapshots. For example, if you have already sent a copy of {{ic|root_backup}} above and have made a new readonly snapshot on your system named {{ic|root_backup_new}}, then to send only the incremental difference to {{ic|/backup}} do:
The subvolume will be created on the receiving end. It does not need to be created manually.
 
Another example which creates: {{ic|/mnt/arch-v2/subvolumes/@var}}:
 
# btrfs send --proto 2 --compressed-data '/mnt/arch/snapshots/@var' | btrfs receive '/mnt/arch-v2/subvolumes/'
 
The parameters {{ic|--proto 2}} and {{ic|--compressed-data}} used in the example might be useful for more efficient sending (assumes compressed data).
 
You can also send only the difference between two snapshots. For example, if you have already sent a copy of {{ic|root_backup}} above and have made a new readonly snapshot on your system named {{ic|root_backup_new}}, then to send only the incremental difference to {{ic|/backup}}, do:


  # btrfs send -p /root_backup /root_backup_new | btrfs receive /backup
  # btrfs send -p /root_backup /root_backup_new | btrfs receive /backup


Now a new subvolume named {{ic|root_backup_new}} will be present in {{ic|/backup}}.
Now, a new subvolume named {{ic|root_backup_new}} will be present in {{ic|/backup}}.


See [https://btrfs.wiki.kernel.org/index.php/Incremental_Backup Btrfs Wiki's Incremental Backup page] on how to use this for incremental backups and for tools that automate the process.
See [https://archive.kernel.org/oldwiki/btrfs.wiki.kernel.org/index.php/Incremental_Backup.html Btrfs Wiki's Incremental Backup page] and [[#Incremental backup to external drive]] on how to use this for incremental backups and for tools that automate the process.


=== Deduplication ===
=== Deduplication ===


Using copy-on-write, Btrfs is able to copy files or whole subvolumes without actually copying the data. However whenever a file is altered a new ''proper'' copy is created. Deduplication takes this a step further, by actively identifying blocks of data which share common sequences and combining them into an extent with the same copy-on-write semantics.
Using copy-on-write, Btrfs is able to copy files or whole subvolumes without actually copying the data. However, whenever a file is altered, a new ''proper'' copy is created. Deduplication takes this a step further by actively identifying blocks of data which share common sequences and combining them into an extent with the same copy-on-write semantics.


Tools dedicated to deduplicate a Btrfs formatted partition include {{Pkg|duperemove}}, {{aur|bedup}} and ''btrfs-dedup''. One may also want to merely deduplicate data on a file based level instead using e.g. {{pkg|rmlint}}, {{aur|jdupes}} or {{aur|dduper-git}}. For an overview of available features of those programs and additional information have a look at the [https://btrfs.wiki.kernel.org/index.php/Deduplication#Batch upstream Wiki entry].
Tools dedicated to deduplicate a Btrfs formatted partition include {{Pkg|duperemove}} and {{Pkg|bees}}. One may also want to merely deduplicate data on a file based level instead using e.g. {{pkg|rmlint}}, {{aur|jdupes}} or {{aur|dduper-git}}. For an overview of available features of those programs and additional information, have a look at the [https://btrfs.readthedocs.io/en/latest/Deduplication.html upstream Wiki entry].


Furthermore Btrfs developers are working on inband (also known as synchronous or inline) deduplication, meaning deduplication done when writing new data to the filesystem. Currently it is still an experiment which is developed out-of-tree. Users willing to test the new feature should read the [https://btrfs.wiki.kernel.org/index.php/User_notes_on_dedupe appropriate kernel wiki page].
=== Resizing ===
 
{{Warning|To avoid data loss, ensure that you back up your data before you begin any resizing task.}}
 
You can grow a file system to the maximum space available on the device, or specify an exact size. Ensure that you grow the size of the device or logical volume before you attempt to increase the size of the file system.
When specifying an exact size for the file system on a device, either increasing or decreasing, ensure that the new size satisfies the following conditions:
 
* The new size must be greater than the size of the existing data; otherwise, data loss occurs.
* The new size must be equal to or less than the current device size because the file system size cannot extend beyond the space available.
 
{{Note|If you plan to also decrease the size of the logical volume that holds the file system, ensure that you decrease the size of the file system before you attempt to decrease the size of the device or logical volume.}}
 
To extend the file system size to the maximum available size of the device:
 
# btrfs filesystem resize max /
 
To extend the file system to a specific size:
 
# btrfs filesystem resize ''size'' /
 
Replace {{ic|''size''}} with the desired size in bytes. You can also specify units on the value, such as K (kibibytes), M (mebibytes), or G (gibibytes). Alternatively, you can specify an increase or decrease to the current size by prefixing the value with a plus (+) or a minus (-) sign, respectively:
 
# btrfs filesystem resize +''size'' /
# btrfs filesystem resize -''size'' /


== Known issues ==
== Known issues ==
Line 339: Line 429:
=== Encryption ===
=== Encryption ===


Btrfs has no built-in encryption support, but this [https://lwn.net/Articles/700487/ may] come in the future. Users can encrypt the partition before running {{ic|mkfs.btrfs}}. See [[dm-crypt/Encrypting an entire system#Btrfs subvolumes with swap]].  
Btrfs has no built-in encryption support, but this [https://lwn.net/Articles/700487/ may] come in the future. Users can encrypt the partition before running {{ic|mkfs.btrfs}}. See [[dm-crypt/Encrypting an entire system]].


Existing Btrfs file systems can use something like [[EncFS]] or [[TrueCrypt]], though perhaps without some of Btrfs' features.
Existing Btrfs file systems can use something like [[EncFS]] or [[TrueCrypt]], though perhaps without some of Btrfs features.


=== TLP ===
=== btrfs check issues ===


Using TLP requires special precautions in order to avoid filesystem corruption. Refer to the [[TLP#Btrfs|according TLP section]] for more information.
The tool {{ic|btrfs check}} has known issues and should not be run without further reading; see section [[#btrfs check]].
 
=== btrfs check issues ===
The tool {{ic|btrfs check}} has known issues and should not be run without further reading, see section [[#btrfs check]].


== Tips and tricks ==
== Tips and tricks ==
Line 354: Line 441:
=== Partitionless Btrfs disk ===
=== Partitionless Btrfs disk ===


{{Warning|Most users do not want this type of setup and instead should install Btrfs on a regular partition. Furthermore, GRUB strongly discourages installation to a partitionless disk. Consider using {{ic|--alloc-start}} for mkfs.btrfs to give larger space to GRUB.}}
{{Warning|Most users do not want this type of setup and instead should install Btrfs on a regular partition. Furthermore, GRUB strongly discourages installation to a partitionless disk.}}


Btrfs can occupy an entire data storage device, replacing the [[MBR]] or [[GPT]] partitioning schemes, using [[#Subvolumes|subvolumes]] to simulate partitions. However, using a partitionless setup is not required to simply [[#File system creation|create a Btrfs filesystem]] on an existing [[partition]] that was created using another method. There are some limitations to partitionless single disk setups:
Btrfs can occupy an entire data storage device, replacing the [[MBR]] or [[GPT]] partitioning schemes, using [[#Subvolumes|subvolumes]] to simulate partitions. However, using a partitionless setup is not required to simply [[#File system creation|create a Btrfs file system]] on an existing [[partition]] that was created using another method. There are some limitations to partitionless single disk setups:


* Cannot place other [[file systems]] on another partition on the same disk.
* Cannot place other [[file systems]] on another partition on the same disk.
* If using a Linux kernel version before 5.0, you cannot use [[Swap|swap area]] as Btrfs did not support [[Swap#Swap_file|swap files]] pre-5.0 and there is no place to create [[Swap#Swap_partition|swap partition]]
* Due to the previous point, having an [[ESP]] on this disk is not possible. Another device is necessary for [[UEFI]] boot.
* Cannot use [[UEFI]] to boot.


To overwrite the existing partition table with Btrfs, run the following command:
To overwrite the existing partition table with Btrfs, run the following command:
Line 368: Line 454:
For example, use {{ic|/dev/sda}} rather than {{ic|/dev/sda1}}. The latter would format an existing partition instead of replacing the entire partitioning scheme. Because the root partition is Btrfs, make sure {{ic|btrfs}} is compiled into the kernel, or put {{ic|btrfs}} into [[mkinitcpio.conf#MODULES]] and [[regenerate the initramfs]].
For example, use {{ic|/dev/sda}} rather than {{ic|/dev/sda1}}. The latter would format an existing partition instead of replacing the entire partitioning scheme. Because the root partition is Btrfs, make sure {{ic|btrfs}} is compiled into the kernel, or put {{ic|btrfs}} into [[mkinitcpio.conf#MODULES]] and [[regenerate the initramfs]].


Install the [[boot loader]] like you would for a data storage device with a [[Master Boot Record]]. See [[Syslinux#Manual install]] or [[GRUB/Tips and tricks#Install to partition or partitionless disk]]. If your kernel does not boot due to {{ic|Failed to mount /sysroot.}}, please add {{ic|1=GRUB_PRELOAD_MODULES="btrfs"}} in {{ic|/etc/default/grub}} and generate the grub configuration ([[GRUB#Generate the main configuration file]]).
Install the [[boot loader]] like you would for a data storage device with a [[Master Boot Record]]. See [[Syslinux#Manually]] or [[GRUB/Tips and tricks#Install to partition or partitionless disk]]. If your kernel does not boot due to {{ic|Failed to mount /sysroot.}}, please add {{ic|1=GRUB_PRELOAD_MODULES="btrfs"}} in {{ic|/etc/default/grub}} and [[GRUB#Generate the main configuration file|generate]] the grub configuration.


=== Ext3/4 to Btrfs conversion ===
=== Ext3/4 to Btrfs conversion ===


{{Warning|There are many reports on the btrfs mailing list about incomplete/corrupt/broken conversions. Make sure you have ''working'' backups of any data you cannot afford to lose. See [https://btrfs.wiki.kernel.org/index.php/Conversion_from_Ext3 Conversion from Ext3] on the btrfs wiki for more information.}}
{{Warning|There are many reports on the btrfs mailing list about incomplete/corrupt/broken conversions. Make sure you have ''working'' backups of any data you cannot afford to lose. See the [https://btrfs.readthedocs.io/en/latest/Convert.html Convert] page on the Btrfs wiki for more information.
{{Warning|There is a bug in btrfs-progs 5.6.1 and before, that will yield a btrfs filesystem with wrong size for the last block group, thus preventing to mount the newly converted btrfs. This bug is fixed in btrfs-progs 5.7 in [https://github.com/kdave/btrfs-progs/commit/0ff7a9b5210723bd4ad0d9d78dbbb18ee8fdd2b1#diff-31168275dcaac634489082b54c4c66d0 this commit]. Please use btrfs-convert from btrfs-progs 5.7-1 and above.}}
}}


Boot from an install CD, then convert by doing:
Boot from an install CD, then convert by doing:
Line 379: Line 465:
  # btrfs-convert /dev/''partition''
  # 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 {{ic|btrfs}} and '''fs_passno''' [the last field] to {{ic|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 fstab accordingly when using UUIDs. {{ic|chroot}} into the system and rebuild the GRUB menu list (see [[Install from existing Linux]] and [[GRUB]] articles). If converting a root filesystem, while still chrooted run {{ic|mkinitcpio -p linux}} to regenerate the initramfs or the system will not successfully boot. If you get stuck in grub with 'unknown filesystem' try reinstalling grub with {{ic|grub-install /dev/''partition''}} and regenerate the config as well {{ic|grub-mkconfig -o /boot/grub/grub.cfg}}.
Mount the partition and test the conversion by checking the files.  Be sure to change the {{ic|/etc/fstab}} to reflect the change ('''type''' to {{ic|btrfs}} and '''fs_passno''' [the last field] to {{ic|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 fstab accordingly when using UUIDs. {{ic|chroot}} into the system and rebuild your boot loader's menu list (see [[Install from existing Linux]]). If converting a root file system, while still chrooted, run {{ic|mkinitcpio -p linux}} to regenerate the initramfs or the system will not successfully boot.


{{Note| If there is anything wrong, either unable to mount or write files to the newly converted btrfs, there is always the option to rollback as long as the backup subvolume {{ic|/ext2_saved}} is still there. Use {{ic|btrfs-convert -r /dev/''partition''}} command to rollback, this will discard any modifications to the newly converted btrfs filesystem.}}
{{Note|If there is anything wrong, either unable to mount or write files to the newly converted Btrfs; there is always the option to rollback as long as the backup subvolume {{ic|/ext2_saved}} is still there. Use the {{ic|btrfs-convert -r /dev/''partition''}} command to rollback; this will discard any modifications to the newly converted Btrfs file system.}}


After confirming that there are no problems, complete the conversion by deleting the backup {{ic|ext2_saved}} sub-volume. Note that you cannot revert back to ext3/4 without it.
After confirming that there are no problems, complete the conversion by deleting the backup {{ic|ext2_saved}} sub-volume. Note that you cannot revert back to ext3/4 without it.
Line 387: Line 473:
  # btrfs subvolume delete /ext2_saved
  # btrfs subvolume delete /ext2_saved


Finally [[#Balance|balance]] the file system to reclaim the space.
Finally, [[#Balance|balance]] the file system to reclaim the space.


Remember that some applications which were installed prior have to be adapted to Btrfs. Notably [[TLP#Btrfs]] needs special care to avoid filesystem corruption but other applications may profit from certain features as well.
Remember that some applications which were installed prior have to be adapted to Btrfs.


=== Checksum hardware acceleration ===
{{Note|Ext3/4 to Btrfs conversion is a time consuming operation. For a 4 TiB file system and a regular HDD, it can take up to 10 hours.}}
 
CRC32 is a new instruction in Intel SSE4.2. To verify if Btrfs checksum is hardware accelerated:
 
{{hc|$ dmesg {{!}} grep crc32c|2=
Btrfs loaded, crc32c=crc32c-intel
}}
 
If you see {{ic|1=crc32c=crc32c-generic}}, it is probably because your root partition is Btrfs, and you will have to compile {{ic|crc32c-intel}} into the kernel to make it work. Putting {{ic|crc32c-intel}} into [[mkinitcpio.conf]] does ''not'' work.


=== Corruption recovery ===
=== Corruption recovery ===
Line 408: Line 486:


{{hc|/etc/mkinitcpio.conf|output=
{{hc|/etc/mkinitcpio.conf|output=
BINARIES=("/usr/bin/btrfs")
BINARIES=(btrfs)
}}
}}


Line 415: Line 493:
Then if there is a problem booting, the utility is available for repair.
Then if there is a problem booting, the utility is available for repair.


{{Note|If the fsck process has to invalidate the space cache (and/or other caches?) then it is normal for a subsequent boot to hang up for a while (it may give console messages about btrfs-transaction being hung). The system should recover from this after a while.}}
{{Note|If the fsck process has to invalidate the space cache (and/or other caches?), it is normal for a subsequent boot to hang up for a while (it may give console messages about btrfs-transaction being hung). The system should recover from this after a while.}}


See the [https://btrfs.wiki.kernel.org/index.php/Btrfsck Btrfs Wiki page] for more information.
See {{man|8|btrfs-check}} for more information.


=== Booting into snapshots ===
=== Booting into snapshots ===


In order to boot into a snapshot, the same procedure applies as for mounting a subvolume as your root parition, as given in section [[#Mounting subvolume as root|mounting a subvolume as your root partition]], because snapshots can be mounted like subvolumes.
In order to boot into a snapshot, the same procedure applies as for mounting a subvolume as your root partition, as given in section [[#Mounting subvolume as root|mounting a subvolume as your root partition]], because snapshots can be mounted like subvolumes.
If using GRUB you can automatically populate your boot menu with btrfs snapshots when regenerating the configuration file with the help of {{Pkg|grub-btrfs}} or {{AUR|grub-btrfs-git}}.
 
* If using [[GRUB]], you can automatically populate your boot menu with Btrfs snapshots when regenerating the configuration file with the help of {{Pkg|grub-btrfs}} or {{AUR|grub-btrfs-git}}.
* If using [[rEFInd]], you can automatically populate your boot menu with Btrfs snapshots with the help of {{AUR|refind-btrfs}}, after [[enabling]] {{ic|refind-btrfs.service}}.


=== Use Btrfs subvolumes with systemd-nspawn ===
=== Use Btrfs subvolumes with systemd-nspawn ===


See the [[Systemd-nspawn#Use Btrfs subvolume as container root]] and [[Systemd-nspawn#Use temporary Btrfs snapshot of container]] articles.
See the [[Systemd-nspawn#Use Btrfs subvolume as container root]] and [[Systemd-nspawn#Use temporary Btrfs snapshot of container]] articles.
=== Reducing access time metadata updates ===
Because of the copy-on-write nature of Btrfs, simply accessing files can trigger the metadata copy and writing. Reducing the frequency of access time updates may eliminate this unexpected disk usage and increase performance. See [[fstab#atime options]] for the available options.
=== Incremental backup to external drive ===
The following packages use {{ic|btrfs send}} and {{ic|btrfs receive}} to send backups incrementally to an external drive. Refer to their documentation to see differences in implementation, features, and requirements.
* {{App|btrbk|Tool for creating snapshots and remote backups of Btrfs subvolumes.|https://github.com/digint/btrbk|{{Pkg|btrbk}}}}
* {{App|snap-sync|Use [[Snapper]] snapshots to backup to external drive or remote machine.|https://github.com/wesbarnett/snap-sync|{{Pkg|snap-sync}}}}
* {{App|snapsync|A synchronization tool for [[Snapper]].|https://github.com/doudou/snapsync|{{AUR|ruby-snapsync}}}}
The following package allows backing up snapper snapshots to non-Btrfs file systems.
* {{App|snapborg|borgmatic-like tool which integrates snapper snapshots with {{Pkg|borg}} backups.|https://github.com/enzingerm/snapborg|{{AUR|snapborg}}}}
=== Automatic snapshots ===
For the managing and automatic creation of Btrfs snapshot, one may use a snapshot manager such as [[Snapper]], [[Timeshift]] or [[Yabsnap]].


== Troubleshooting ==
== Troubleshooting ==


See the [https://btrfs.wiki.kernel.org/index.php/Problem_FAQ Btrfs Problem FAQ] for general troubleshooting.
See the [https://btrfs.readthedocs.io/en/latest/trouble-index.html Btrfs Troubleshooting pages] and [https://archive.kernel.org/oldwiki/btrfs.wiki.kernel.org/index.php/Problem_FAQ.html Btrfs Problem FAQ] for general troubleshooting.


=== GRUB ===
=== GRUB ===
Line 445: Line 544:


Users experiencing 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 the system should boot without an error.
Users experiencing 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 the system should boot without an error.
=== Mounting timed out ===
Sometimes, especially with large RAID1 arrays, mounting might time out during boot with a journal message such as:
{{bc|1=
Jan 25 18:05:12 host systemd[1]: storage.mount: Mounting timed out. Terminating.
Jan 25 18:05:46 host systemd[1]: storage.mount: Mount process exited, code=killed, status=15/TERM
Jan 25 18:05:46 host systemd[1]: storage.mount: Failed with result 'timeout'.
Jan 25 18:05:46 host systemd[1]: Failed to mount /storage.
Jan 25 18:05:46 host systemd[1]: Startup finished in 32.943s (firmware) + 3.097s (loader) + 7.247s (kernel)>
Jan 25 18:05:46 host kernel: BTRFS error (device sda): open_ctree failed
}}
This can easily be worked around by providing a longer timeout via the systemd-specific mount option {{ic|x-systemd.mount-timeout}} in [[fstab]]. For example:
/dev/sda                /storage    btrfs       rw,relatime,x-systemd.mount-timeout=5min  0 0


=== BTRFS: open_ctree failed ===
=== BTRFS: open_ctree failed ===


As of November 2014 there seems to be a bug in [[systemd]] or [[mkinitcpio]] causing the following error on systems with multi-device Btrfs filesystem using the {{ic|btrfs}} hook in {{ic|mkinitcpio.conf}}:
{{Accuracy|The status is old. Replacing udev hook with systemd makes no sense since they both use systemd-udevd.}}
 
As of November 2014, there seems to be a bug in [[systemd]] or [[mkinitcpio]] causing the following error on systems with multi-device Btrfs file system using the {{ic|btrfs}} hook in {{ic|mkinitcpio.conf}}:


{{bc|<nowiki>
{{bc|<nowiki>
Line 454: Line 572:
mount: wrong fs type, bad option, bad superblock on /dev/sdb2, missing codepage or helper program, or other error
mount: wrong fs type, bad option, bad superblock on /dev/sdb2, missing codepage or helper program, or other error


In some cases useful info is found in syslog - try dmesg|tail or so.
In some cases, useful info is found in syslog - try dmesg|tail or so.


You are now being dropped into an emergency shell.
You are now being dropped into an emergency shell.
Line 461: Line 579:
A workaround is to remove {{ic|btrfs}} from the {{ic|HOOKS}} array in {{ic|/etc/mkinitcpio.conf}} and instead add {{ic|btrfs}} to the {{ic|MODULES}} array. Then [[regenerate the initramfs]] and reboot.
A workaround is to remove {{ic|btrfs}} from the {{ic|HOOKS}} array in {{ic|/etc/mkinitcpio.conf}} and instead add {{ic|btrfs}} to the {{ic|MODULES}} array. Then [[regenerate the initramfs]] and reboot.


You will get the same error if you try to mount a raid array without one of the devices. In that case you must add the {{ic|degraded}} mount option to {{ic|/etc/fstab}}. If your root resides on the array, you must also add {{ic|1=rootflags=degraded}} to your [[kernel parameters]].
You will get the same error if you try to mount a raid array without one of the devices. In that case, you must add the {{ic|degraded}} mount option to {{ic|/etc/fstab}}. If your root resides on the array, you must also add {{ic|1=rootflags=degraded}} to your [[kernel parameters]].


As of August 2016, a potential workaround for this bug is to mount the array by a single drive only in {{ic|/etc/fstab}}, and allow btrfs to discover and append the other drives automatically. Group-based identifiers such as UUID and LABEL appear to contribute to the failure. For example, a two-device RAID1 array consisting of 'disk1' and disk2' will have a UUID allocated to it, but instead of using the UUID, use only {{ic|/dev/mapper/disk1}} in {{ic|/etc/fstab}}. For a more detailed explanation, see the following [https://blog.samcater.com/fix-for-btrfs-open_ctree-failed-when-running-root-fs-on-raid-1-or-raid10-arch-linux/ blog post]{{Dead link|2020|05|24|status=404}}.
As of August 2016, a potential workaround for this bug is to mount the array by a single drive only in {{ic|/etc/fstab}}, and allow Btrfs to discover and append the other drives automatically. Group-based identifiers such as UUID and LABEL appear to contribute to the failure. For example, a two-device RAID1 array consisting of 'disk1' and disk2' will have a UUID allocated to it, but instead of using the UUID, use only {{ic|/dev/mapper/disk1}} in {{ic|/etc/fstab}}. For a more detailed explanation, see the following [https://web.archive.org/web/20161108175034/http://blog.samcater.com/fix-for-btrfs-open_ctree-failed-when-running-root-fs-on-raid-1-or-raid10-arch-linux/ blog post].


Another possible workaround is to remove the {{ic|udev}} hook in [[mkinitcpio.conf]] and replace it with the {{ic|systemd}} hook. In this case, {{ic|btrfs}} should ''not'' be in the {{ic|HOOKS}} or {{ic|MODULES}} arrays.
Another possible workaround is to remove the {{ic|udev}} hook in [[mkinitcpio.conf]] and replace it with the {{ic|systemd}} hook. In this case, {{ic|btrfs}} should ''not'' be in the {{ic|HOOKS}} or {{ic|MODULES}} arrays.
Line 471: Line 589:
=== btrfs check ===
=== btrfs check ===


{{Warning|Since Btrfs is under heavy development, especially the {{ic|btrfs check}} command, it is highly recommended to create a '''backup''' and consult the [https://btrfs.wiki.kernel.org/index.php/Btrfsck Btrfsck documentation] before executing {{ic|btrfs check}} with the {{ic|--repair}} switch.}}
{{Out of date|The "heavy development" status is old.}}
 
{{Warning|Since Btrfs is under heavy development, especially the {{ic|btrfs check}} command, it is highly recommended to create a [[backup]] and consult {{man|8|btrfs-check}} before executing {{ic|btrfs check}} with the {{ic|--repair}} switch.}}
 
The {{man|8|btrfs-check}} command can be used to check or repair an unmounted Btrfs file system. However, this repair tool is still immature and not able to repair certain file system errors even those that do not render the file system unmountable.
 
=== Constant drive activity ===
 
Since the [[kernel]] version 6.2, {{ic|1=discard=async}} {{man|8|mount}} option is set by default. This [https://lore.kernel.org/linux-btrfs/Y%2F%2Bn1wS%2F4XAH7X1p@nz/#r has been reported] to cause constant drive activity on some drives even while idle, as the discard queue is filled faster than it is processed. This can cause increased power usage, especially on NVMe-based drives.
 
As of kernel version 6.3, the default discard {{ic|iops_limit}} has been changed from 100 to 1000 to address this issue. You can manually set it to a desired value on an old kernel version, e.g.
 
# echo 1000 > /sys/fs/btrfs/''uuid''/discard/iops_limit
 
Where {{ic|''uuid''}} is the UUID of the Btrfs file system. The limit of {{ic|1000}} will need to be tuned experimentally.
 
To set the parameter on boot, [[systemd-tmpfiles]] may be used, e.g. by creating the following file:
 
{{hc|/etc/tmpfiles.d/btrfs-discard.conf|
w /sys/fs/btrfs/''uuid''/discard/iops_limit - - - - 1000
}}


The ''[https://btrfs.wiki.kernel.org/index.php/Manpage/btrfs-check btrfs check]'' command can be used to check or repair an unmounted Btrfs filesystem. However, this repair tool is still immature and not able to repair certain filesystem errors even those that do not render the filesystem unmountable.
Alternatively, one can disable asynchronous discard altogether by mounting using the {{ic|nodiscard}} mount option in [[fstab]], and instead use [[Solid state drive#Periodic TRIM|Periodic TRIM]].


== See also ==
== See also ==


* '''Official site'''
* '''Official site'''
** [https://btrfs.wiki.kernel.org/ Btrfs Wiki]
** [https://btrfs.readthedocs.io Btrfs Documentation]
** [https://archive.kernel.org/oldwiki/btrfs.wiki.kernel.org/ Archived Btrfs Wiki]
* '''Performance related'''
* '''Performance related'''
** [https://superuser.com/questions/432188/should-i-put-my-multi-device-btrfs-filesystem-on-disk-partitions-or-raw-devices Btrfs on raw disks?]
** [https://superuser.com/questions/432188/should-i-put-my-multi-device-btrfs-filesystem-on-disk-partitions-or-raw-devices Btrfs on raw disks?]
** [https://www.spinics.net/lists/linux-btrfs/msg18652.html Varying leafsize and nodesize in Btrfs]
** [https://lore.kernel.org/linux-btrfs/CAKcLGm_MKEdTiHFBd-b-v2sN5gJmgFRqsykzWRXTVqUw4O6Acw@mail.gmail.com/ Varying leafsize and nodesize in Btrfs]
** [http://comments.gmane.org/gmane.comp.file-systems.btrfs/15646 Btrfs support for efficient SSD operation (data blocks alignment)]{{Dead link|2020|02|23}}
** [https://lore.kernel.org/linux-btrfs/jgui4j$th5$1@dough.gmane.org/ Btrfs support for efficient SSD operation (data blocks alignment)]
** [https://btrfs.wiki.kernel.org/index.php/FAQ#Is_Btrfs_optimized_for_SSD.3F Is Btrfs optimized for SSDs?]
** [https://archive.kernel.org/oldwiki/btrfs.wiki.kernel.org/index.php/FAQ.html#Is_Btrfs_optimized_for_SSD.3F Is Btrfs optimized for SSDs?]
** '''Phoronix mount option benchmarking'''
** [https://blog.erdemagaoglu.com/post/4605524309/lzo-vs-snappy-vs-lzf-vs-zlib-a-comparison-of Lzo vs. zLib]
*** [http://www.phoronix.com/scan.php?page=article&item=btrfs-mount-linux49 Linux 4.9]
*** [http://www.phoronix.com/scan.php?page=article&item=linux_314_btrfs Linux 3.14]
*** [http://www.phoronix.com/scan.php?page=article&item=linux_btrfs_311&num=1 Linux 3.11]
*** [http://www.phoronix.com/scan.php?page=news_item&px=MTM0OTU Linux 3.9]
*** [http://www.phoronix.com/scan.php?page=article&item=btrfs_linux37_mounts&num=1 Linux 3.7]
*** [http://www.phoronix.com/scan.php?page=article&item=linux_btrfs_options&num=1 Linux 3.2]
** [http://blog.erdemagaoglu.com/post/4605524309/lzo-vs-snappy-vs-lzf-vs-zlib-a-comparison-of Lzo vs. zLib]
* '''Miscellaneous'''
* '''Miscellaneous'''
** [http://www.funtoo.org/wiki/BTRFS_Fun Funtoo Wiki Btrfs Fun]
** [[Funtoo:BTRFS Fun]]
** [http://www.phoronix.com/scan.php?page=news_item&px=MTA0ODU Avi Miller presenting Btrfs] at SCALE 10x, January 2012.
** [https://www.phoronix.com/scan.php?page=news_item&px=MTA0ODU Avi Miller presenting Btrfs] at SCALE 10x, January 2012.
** [http://www.phoronix.com/scan.php?page=news_item&px=MTA4Mzc Summary of Chris Mason's talk] from LFCS 2012
** [https://www.phoronix.com/scan.php?page=news_item&px=MTA4Mzc Summary of Chris Mason's talk] from LFCS 2012
** [https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=35054394c4b3cecd52577c2662c84da1f3e73525 Btrfs: stop providing a bmap operation to avoid swapfile corruptions] 2009-01-21
** [https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=35054394c4b3cecd52577c2662c84da1f3e73525 Btrfs: stop providing a bmap operation to avoid swapfile corruptions] 2009-01-21
** [http://marc.merlins.org/perso/btrfs/post_2014-03-22_Btrfs-Tips_-Doing-Fast-Incremental-Backups-With-Btrfs-Send-and-Receive.html Doing Fast Incremental Backups With Btrfs Send and Receive]
** [https://marc.merlins.org/perso/btrfs/post_2014-03-22_Btrfs-Tips_-Doing-Fast-Incremental-Backups-With-Btrfs-Send-and-Receive.html Doing Fast Incremental Backups With Btrfs Send and Receive]

Latest revision as of 13:29, 9 April 2024

From the Btrfs Documentation:

Btrfs is a modern copy on write (COW) file system for Linux aimed at implementing advanced features while also focusing on fault tolerance, repair and easy administration.
Note: Like some other file systems, Btrfs is under continuous development. This means that specific features may not be ready for everyday use yet. To see if your use case might be affected, check the Btrfs Documentation’s Status and the #Known issues section of this article.

Preparation

For user space utilities, install the btrfs-progs package that is required for basic operations.

If you need to boot from a Btrfs file system (i.e., your kernel and initramfs reside on a Btrfs partition), check if your boot loader supports Btrfs.

File system creation

The following shows how to create a new Btrfs file system. To convert an Ext3/4 partition to Btrfs, see #Ext3/4 to Btrfs conversion. To use a partitionless setup, see #Partitionless Btrfs disk.

See mkfs.btrfs(8) for more information.

File system on a single device

To create a Btrfs file system on partition /dev/partition:

# mkfs.btrfs -L mylabel /dev/partition

The Btrfs default nodesize for metadata is 16 KiB, while the default sectorsize for data is equal to page size and autodetected. To use a larger nodesize for metadata (must be a multiple of sectorsize, up to 64 KiB is allowed), specify a value for the nodesize via the -n switch as shown in this example using 32 KiB blocks:

# mkfs.btrfs -L mylabel -n 32k /dev/partition
Note: According to mkfs.btrfs(8) § OPTIONS, "[a] smaller node size increases fragmentation but leads to taller b-trees which in turn leads to lower locking contention. Higher node sizes give better packing and less fragmentation at the cost of more expensive memory operations while updating the metadata blocks".

Multi-device file system

Warning: The RAID 5 and RAID 6 modes of Btrfs are fatally flawed, and should not be used for "anything but testing with throw-away data." List of known problems and partial workarounds. See btrfs(5) § RAID56 STATUS AND RECOMMENDED PRACTICES for status updates.

Multiple devices can be used to create a RAID. Supported RAID levels include RAID 0, RAID 1, RAID 10, RAID 5 and RAID 6. Starting from kernel 5.5 RAID1c3 and RAID1c4 for 3- and 4- copies of RAID 1 level. The RAID levels can be configured separately for data and metadata using the -d and -m options respectively. By default, the data has one copy (single) and the metadata is mirrored (raid1). This is similar to creating a JBOD configuration, where disks are seen as one file system, but files are not duplicated. See Using Btrfs with Multiple Devices for more information about how to create a Btrfs RAID volume.

# mkfs.btrfs -d single -m raid1 /dev/part1 /dev/part2 ...

You must include either the udev hook, systemd hook or the btrfs hook in /etc/mkinitcpio.conf in order to use multiple Btrfs devices in a pool. See the Mkinitcpio#Common hooks article for more information.

Note:
  • It is possible to add devices to a multiple-device file system later on. See the Btrfs wiki article for more information.
  • Devices can be of different sizes. However, if one drive in a RAID configuration is bigger than the others, this extra space will not be used.
  • Some boot loaders such as Syslinux do not support multi-device file systems.
  • Btrfs does not automatically read from the fastest device, so mixing different kinds of disks results in inconsistent performance. See [1] for details.

See #RAID for advice on maintenance specific to multi-device Btrfs file systems.

Configuring the file system

Copy-on-Write (CoW)

By default, Btrfs uses copy-on-write for all files all the time. Writes do not overwrite data in place; instead, a modified copy of the block is written to a new location, and metadata is updated to point at the new location. See the Btrfs Sysadmin Guide section for implementation details, as well as advantages and disadvantages.

Disabling CoW

Warning: Disabling CoW in Btrfs also disables checksums. Btrfs will not be able to detect corrupted nodatacow files. When combined with RAID 1, power outages or other sources of corruption can cause the data to become out of sync.

To disable copy-on-write for newly created files in a mounted subvolume, use the nodatacow mount option. This will only affect newly created files. Copy-on-write will still happen for existing files. The nodatacow option also disables compression. See btrfs(5) for details.

Note: From btrfs(5) § MOUNT OPTIONS:
within a single file system, it is not possible to mount some subvolumes with nodatacow and others with datacow. The mount option of the first mounted subvolume applies to any other subvolumes.

To disable copy-on-write for single files/directories, do:

$ chattr +C /dir/file

This will disable copy-on-write for those operation in which there is only one reference to the file. If there is more than one reference, e.g. due to file clones or lightweight clones or file system snapshots, copy-on-write still occurs. Note that as of coreutils 9.0, cp attempts to perform lightweight copies by default — see cp(1) for more details.

Note: From chattr(1):
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.
Tip: In accordance with the note above, you can use the following trick to disable copy-on-write on existing files in a directory:
$ mv /path/to/dir /path/to/dir_old
$ mkdir /path/to/dir
$ chattr +C /path/to/dir
$ cp -a --reflink=never /path/to/dir_old/. /path/to/dir
$ rm -rf /path/to/dir_old
Make sure that the data are not used during this process. Also note that mv or cp without --reflink=never as described below will not work.
Effect on snapshots

If a file has copy-on-write disabled (NOCOW) and a snapshot is taken, the first write to a file block after the snapshot will be a COW operation because the snapshot locks the old file blocks in place. However, the file will retain the NOCOW attribute and any subsequent writes to the same file block will be in-place until the next snapshot.

Frequent snapshots can reduce the effectiveness of NOCOW, as COW is still required on the first write. To avoid copy-on-write for such files altogether, put them in a separate subvolume and do not take snapshots of that subvolume.

Compression

Btrfs supports transparent and automatic compression. This reduces the size of files as well as significantly increases the lifespan of flash-based media by reducing write amplification. See Fedora:Changes/BtrfsByDefault#Compression, [2], and [3]. It can also improve performance, in some cases (e.g. single thread with heavy file I/O), while obviously harming performance in other cases (e.g. multi-threaded and/or CPU intensive tasks with large file I/O). Better performance is generally achieved with the fastest compress algorithms, zstd and lzo, and some benchmarks provide detailed comparisons.

LZO has a fixed compression level, whereas zlib and zstd have a range of levels from 1 (low compression) to 9 (zlib) or 15 (zstd); see btrfs(5) § COMPRESSION. Changing the levels will affect CPU and I/O throughput differently, so they should be checked / benchmarked before and after changing.

The compress=alg[:level] mount option enables automatically considering every file for compression, where alg is either zlib, lzo, zstd, or no (for no compression). Using this option, Btrfs will check if compressing the first portion of the data shrinks it. If it does, the entire write to that file will be compressed. If it does not, none of it is compressed. With this option, if the first portion of the write does not shrink, no compression will be applied to the write even if the rest of the data would shrink tremendously. [4] This is done to prevent making the disk wait to start writing until all of the data to be written is fully given to Btrfs and compressed.

This article or section needs expansion.

Reason: Missing reference for the "empirical testing" mentioned in the following paragraph. (Discuss in Talk:Btrfs)

The compress-force=alg[:level] mount option can be used instead, which makes Btrfs skip checking if compression shrinks the first portion, and enables automatic compression try for every file. In a worst-case scenario, this can cause (slightly) more CPU usage for no purpose. However, empirical testing on multiple mixed-use systems showed a significant improvement of about 10% disk compression from using compress-force=zstd over just compress=zstd, which also had 10% disk compression. However, keep in mind that forcing compression is against official Btrfs guidelines.

Only files created or modified after the mount option is added will be compressed.

To apply compression to existing files, use the btrfs filesystem defragment -calg command, where alg is either zlib, lzo or zstd. For example, in order to re-compress the whole file system with zstd, run the following command:

# btrfs filesystem defragment -r -v -czstd /
Warning: Defragmenting a file which has a COW copy (either a snapshot copy or one made with cp or bcp) plus using the -c switch with a compression algorithm may result in two unrelated files effectively increasing the disk usage.

To enable compression when installing Arch to an empty Btrfs partition, use the compress option when mounting the file system: mount -o compress=zstd /dev/sdxY /mnt/. During configuration, add compress=zstd to the mount options of the root file system in fstab.

Tip: Compression can also be enabled per-file without using the compress mount option; to do so, apply chattr +c or btrfs property set file compression alg to the file. The first command is using legacy interface of file attributes inherited from ext2 filesystem and is not flexible, so by default the zlib compression is set. The other command sets a property on the file with the given algorithm. (Note: setting level that way is not yet implemented.) When applied to directories, it will cause new files to be automatically compressed as they come.
Warning:
  • Systems using older kernels or btrfs-progs without zstd support may be unable to read or repair your file system if you use this option.
  • GRUB introduced zstd support in 2.04. Make sure you have actually upgraded the bootloader installed in your MBR/ESP since then, by running grub-install with the appropriate options for your BIOS/UEFI setup, since that is not done automatically. See FS#63235.

View compression types and ratios

compsize takes a list of files (or an entire Btrfs file system) and measures compression types used and effective compression ratios. Uncompressed size may not match the number given by other programs such as du(1), because every extent is counted once, even if it is reflinked several times, and even if a part of it is no longer used anywhere but has not been garbage collected. The -x option keeps it on a single file system, which is useful in situations like compsize -x / to avoid it from attempting to look in non-Btrfs subdirectories and fail the entire run.

Subvolumes

"A Btrfs subvolume is not a block device (and cannot be treated as one) instead, a Btrfs subvolume can be thought of as a POSIX file namespace. This namespace can be accessed via the top-level subvolume of the file system, or it can be mounted in its own right." [5]

Each Btrfs file system has a top-level subvolume with ID 5. This subvolume cannot be removed or replaced by another subvolume. The top-level subvolume has path / on the file system and other subvolumes are nested below the top-level subvolume. However, subvolumes can be moved around in the file system and their path may change, whereas their ID cannot.

By default, the top-level subvolume is mounted when mounting the file system. Options allow to mount a specific subvolume instead.

A major use case for subvolumes are snapshots.

See the following links for more details:

Creating a subvolume

To create a subvolume, the Btrfs file system must be mounted. The subvolume's name is set using the last argument.

# btrfs subvolume create /path/to/subvolume
Note: You can use --parents to automatically create parent directories if they do not exist.

Listing subvolumes

To see a list of current subvolumes and their ids under path:

# btrfs subvolume list -p path

Deleting a subvolume

To delete a subvolume:

# btrfs subvolume delete /path/to/subvolume

Alternatively, a subvolume can be deleted like a regular directory (rm -r, rmdir).

Mounting subvolumes

Subvolumes can be mounted like file system partitions using the subvol=/path/to/subvolume or subvolid=objectid mount flags. For example, you could have a subvolume named subvol_root and mount it as /. One can mimic traditional file system partitions by creating various subvolumes under the top level of the file system and then mounting them at the appropriate mount points. It is preferable to mount using subvol=/path/to/subvolume, rather than the subvolid, as the subvolid may change when restoring #Snapshots, requiring a change of mount configuration.

Tip: Changing subvolume layouts is made simpler by not using the top-level subvolume (ID=5) as / (which is done by default). Instead, consider creating a subvolume to house your actual data and mounting it as /.
Note: From btrfs(5) § MOUNT OPTIONS:
Most mount options apply to the whole file system, and only the options for the first subvolume to be mounted will take effect. This is due to lack of implementation and may change in the future.

See the Btrfs Wiki FAQ for which mount options can be used per subvolume.

See Snapper#Suggested filesystem layout, Btrfs SysadminGuide#Managing Snapshots, and SysadminGuide#Layout for example file system layouts using subvolumes.

See btrfs(5) for a full list of Btrfs-specific mount options.

Mounting subvolume as root

To use a subvolume as the root mountpoint, either make it the default subvolume, or specify the subvolume via a kernel parameter using rootflags=subvol=/path/to/subvolume. Edit the root mountpoint in /etc/fstab and specify the mount option subvol=. Alternatively, the subvolume can be specified with its id, rootflags=subvolid=objectid as kernel parameter and subvolid=objectid as mount option in /etc/fstab. It is preferable to mount using subvol=/path/to/subvolume, rather than the subvolid, as the subvolid may change when restoring #Snapshots, requiring a change of mount configuration, or else the system will not boot.

Changing the default sub-volume

The default sub-volume is mounted if no subvol= mount option is provided. To change the default subvolume, do:

# btrfs subvolume set-default subvolume-id /

where subvolume-id can be found by listing.

Note: After changing the default subvolume on a system with GRUB, you should run grub-install again to notify the bootloader of the changes. See this forum thread.

Changing the default subvolume with btrfs subvolume set-default will make the top level of the file system inaccessible, except by use of the subvol=/ or subvolid=5 mount options [6].

Quota

Warning: Qgroup is not stable yet and combining quota with (too many) snapshots of subvolumes can cause performance problems, for example when deleting snapshots. Plus there are several more known issues.

Quota support in Btrfs is implemented at a subvolume level by the use of quota groups or qgroup: Each subvolume is assigned a quota groups in the form of 0/subvolume_id by default. However, it is possible to create a quota group using any number if desired.

To use qgroups, you need to enable quota first using

# btrfs quota enable path

From this point onwards, newly created subvolumes will be controlled by those groups. In order to retrospectively enable them for already existing subvolumes, enable quota normally, then create a qgroup (quota group) for each of those subvolume using their subvolume_id and rescan them:

# btrfs subvolume list path | cut -d' ' -f2 | xargs -I{} -n1 btrfs qgroup create 0/{} path
# btrfs quota rescan path

Quota groups in Btrfs form a tree hierarchy, whereby qgroups are attached to subvolumes. The size limits are set per qgroup and apply when any limit is reached in tree that contains a given subvolume.

Limits on quota groups can be applied either to the total data usage, un-shared data usage, compressed data usage or both. File copy and file deletion may both affect limits since the unshared limit of another qgroup can change if the original volume's files are deleted and only one copy is remaining. For example, a fresh snapshot shares almost all the blocks with the original subvolume, new writes to either subvolume will raise towards the exclusive limit, deletions of common data in one volume raises towards the exclusive limit in the other one.

To apply a limit to a qgroup, use the command btrfs qgroup limit. Depending on your usage, either use a total limit, unshared limit (-e) or compressed limit (-c). To show usage and limits for a given path within a file system, use

# btrfs qgroup show -reF path

Commit interval

The resolution at which data are written to the file system 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 file system. This can be changed by appending the commit mount option in /etc/fstab for the Btrfs partition.

LABEL=arch64 / btrfs defaults,compress=zstd,commit=120 0 0

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 is available at https://docs.kernel.org/admin-guide/sysctl/vm.html.

SSD TRIM

A Btrfs file system is able to free unused blocks from an SSD drive supporting the TRIM command. Asynchronous discard support is available with mount option discard=async, and is enabled by default as of linux 6.2. Freed extents are not discarded immediately, but grouped together and trimmed later by a separate worker thread, improving commit latency.

Asynchronous discard can safely be used alongside periodic trim [7].

More information about enabling and using TRIM can be found in Solid State Drives#TRIM.

Usage

Swap file

Note: Swap files on file systems spanning multiple devices are not supported. See btrfs(5) § SWAPFILE SUPPORT for all limitations.

To properly initialize a swap file, first create a non-snapshotted subvolume to host the file, e.g.

# btrfs subvolume create /swap
Tip: Consider creating the subvolume directly below the top-level subvolume, e.g. @swap. Then, make sure the subvolume is mounted to /swap (or any other accessible location).

Create the swap file:

# btrfs filesystem mkswapfile --size 4g --uuid clear /swap/swapfile

If --size is omitted, the default of 2GiB is used.

Activate the swap file:

# swapon /swap/swapfile

Finally, edit the fstab configuration to add an entry for the swap file:

/etc/fstab
/swap/swapfile none swap defaults 0 0

For additional information, see fstab#Usage.

Note: You can also create the swap file manually, by setting the No_COW attribute on the whole subvolume with chattr, then following the steps in Swap#Swap file creation. See btrfs(5) § SWAPFILE SUPPORT for an alternative method.

To use a swap file for hibernation, you might need to additionally follow the procedures described in Hibernation.

Displaying used/free space

General linux userspace tools such as df(1) will inaccurately report free space on a Btrfs partition. It is recommended to use btrfs filesystem usage to query Btrfs partitions. For example, for a full breakdown of device allocation and usage stats:

# btrfs filesystem usage /
Note: The btrfs filesystem usage command does not currently work correctly with RAID5/RAID6 RAID levels.

Alternatively, btrfs filesystem df allows a quick check on usage of allocated space without the requirement to run as root:

$ btrfs filesystem df /

See [8] for more information.

The same limitations apply to tools which analyze space usage for some subset of the file system, such as du(1) or ncdu(1), as they do not take into account reflinks, snapshots and compression. Instead, see btduAUR and compsize for Btrfs-aware alternatives.

Defragmentation

Btrfs supports online defragmentation through the mount option autodefrag; see btrfs(5) § MOUNT OPTIONS. To manually defragment your root, use:

# btrfs filesystem defragment -r /

Using the above command without the -r switch will result in only the metadata held by the subvolume containing the directory being defragmented. This allows for single file defragmentation by simply specifying the path.

Warning: Defragmenting a file which has a COW copy (either a snapshot copy or one made with cp or bcp) plus using the -c switch with a compression algorithm may result in two unrelated files effectively increasing the disk usage.

RAID

Btrfs offers native "RAID" for #Multi-device file systems. Notable features which set Btrfs RAID apart from mdadm are self-healing redundant arrays and online balancing. See the Btrfs wiki page for more information. The Btrfs sysadmin page also has a section with some more technical background.

Warning: Parity RAID (RAID 5/6) code has multiple serious data-loss bugs in it. See the Btrfs Wiki's RAID5/6 page and a bug report on linux-btrfs mailing list for more detailed information. In June 2020, somebody posted a comprehensive list of current issues and a helpful recovery guide.

Scrub

This article or section needs expansion.

The Btrfs Wiki Glossary says that Btrfs scrub is "[a]n online file system checking tool. Reads all the data and metadata on the file system and uses checksums and the duplicate copies from RAID storage to identify and repair any corrupt data."

Note: A running scrub process will prevent the system from suspending, see this thread for details.
Start manually

To start a (background) scrub on the file system which contains /:

# btrfs scrub start /

To check the status of a running scrub:

# btrfs scrub status /
Start with a service or timer

The btrfs-progs package brings the btrfs-scrub@.timer unit for monthly scrubbing the specified mountpoint. Enable the timer with an escaped path, e.g. btrfs-scrub@-.timer for / and btrfs-scrub@home.timer for /home. You can use systemd-escape -p /path/to/mountpoint to escape the path; see systemd-escape(1) for details.

You can also run the scrub by starting btrfs-scrub@.service (with the same encoded path). The advantage of this over btrfs scrub (as the root user) is that the results of the scrub will be logged in the systemd journal.

On large NVMe drives with insufficient cooling (e.g. in a laptop), scrubbing can read the drive fast enough and long enough to get it very hot. If you are running scrubs with systemd, you can easily limit the rate of scrubbing with the IOReadBandwidthMax option described in systemd.resource-control(5) by using a drop-in file.

Balance

"A balance passes all data in the file system through the allocator again. It is primarily intended to rebalance the data in the file system across the devices when a device is added or removed. A balance will regenerate missing copies for the redundant RAID levels, if a device has failed." [9] See Upstream FAQ page.

On a single-device filesystem, a balance may be also useful for (temporarily) reducing the amount of allocated but unused (meta)data chunks. Sometimes this is needed for fixing "file system full" issues.

# btrfs balance start --bg /
# btrfs balance status /

Snapshots

"A snapshot is simply a subvolume that shares its data (and metadata) with some other subvolume, using Btrfs's COW capabilities." See Btrfs Wiki SysadminGuide#Snapshots for details.

To create a snapshot:

# btrfs subvolume snapshot source [dest/]name

To create a readonly snapshot, add the -r flag. To create a writable version of a readonly snapshot, simply create a snapshot of it.

Note:
  • It is possible for a snapshot to be converted in-place from readonly to writeable with btrfs property set -f -ts '/path/to/snapshot' ro false. However, this is not recommended because it causes issues with any future incremental send/receive. Making a new writeable snapshot prevents such issues.
  • Snapshots are not recursive. Every nested subvolume will be an empty directory inside the snapshot.

Send/receive

A subvolume can be sent to stdout or a file using the send command. This is usually most useful when piped to a Btrfs receive command. For example, to send a snapshot named /root_backup (perhaps of a snapshot you made of / earlier) to /backup, you would do the following:

# btrfs send /root_backup | btrfs receive /backup

The snapshot that is sent must be readonly. The above command is useful for copying a subvolume to an external device (e.g. a USB disk mounted at /backup above).

The subvolume will be created on the receiving end. It does not need to be created manually.

Another example which creates: /mnt/arch-v2/subvolumes/@var:

# btrfs send --proto 2 --compressed-data '/mnt/arch/snapshots/@var' | btrfs receive '/mnt/arch-v2/subvolumes/'

The parameters --proto 2 and --compressed-data used in the example might be useful for more efficient sending (assumes compressed data).

You can also send only the difference between two snapshots. For example, if you have already sent a copy of root_backup above and have made a new readonly snapshot on your system named root_backup_new, then to send only the incremental difference to /backup, do:

# btrfs send -p /root_backup /root_backup_new | btrfs receive /backup

Now, a new subvolume named root_backup_new will be present in /backup.

See Btrfs Wiki's Incremental Backup page and #Incremental backup to external drive on how to use this for incremental backups and for tools that automate the process.

Deduplication

Using copy-on-write, Btrfs is able to copy files or whole subvolumes without actually copying the data. However, whenever a file is altered, a new proper copy is created. Deduplication takes this a step further by actively identifying blocks of data which share common sequences and combining them into an extent with the same copy-on-write semantics.

Tools dedicated to deduplicate a Btrfs formatted partition include duperemove and bees. One may also want to merely deduplicate data on a file based level instead using e.g. rmlint, jdupesAUR or dduper-gitAUR. For an overview of available features of those programs and additional information, have a look at the upstream Wiki entry.

Resizing

Warning: To avoid data loss, ensure that you back up your data before you begin any resizing task.

You can grow a file system to the maximum space available on the device, or specify an exact size. Ensure that you grow the size of the device or logical volume before you attempt to increase the size of the file system. When specifying an exact size for the file system on a device, either increasing or decreasing, ensure that the new size satisfies the following conditions:

  • The new size must be greater than the size of the existing data; otherwise, data loss occurs.
  • The new size must be equal to or less than the current device size because the file system size cannot extend beyond the space available.
Note: If you plan to also decrease the size of the logical volume that holds the file system, ensure that you decrease the size of the file system before you attempt to decrease the size of the device or logical volume.

To extend the file system size to the maximum available size of the device:

# btrfs filesystem resize max /

To extend the file system to a specific size:

# btrfs filesystem resize size /

Replace size with the desired size in bytes. You can also specify units on the value, such as K (kibibytes), M (mebibytes), or G (gibibytes). Alternatively, you can specify an increase or decrease to the current size by prefixing the value with a plus (+) or a minus (-) sign, respectively:

# btrfs filesystem resize +size /
# btrfs filesystem resize -size /

Known issues

A few limitations should be known before trying.

Encryption

Btrfs has no built-in encryption support, but this may come in the future. Users can encrypt the partition before running mkfs.btrfs. See dm-crypt/Encrypting an entire system.

Existing Btrfs file systems can use something like EncFS or TrueCrypt, though perhaps without some of Btrfs features.

btrfs check issues

The tool btrfs check has known issues and should not be run without further reading; see section #btrfs check.

Tips and tricks

Partitionless Btrfs disk

Warning: Most users do not want this type of setup and instead should install Btrfs on a regular partition. Furthermore, GRUB strongly discourages installation to a partitionless disk.

Btrfs can occupy an entire data storage device, replacing the MBR or GPT partitioning schemes, using subvolumes to simulate partitions. However, using a partitionless setup is not required to simply create a Btrfs file system on an existing partition that was created using another method. There are some limitations to partitionless single disk setups:

  • Cannot place other file systems on another partition on the same disk.
  • Due to the previous point, having an ESP on this disk is not possible. Another device is necessary for UEFI boot.

To overwrite the existing partition table with Btrfs, run the following command:

# mkfs.btrfs /dev/sdX

For example, use /dev/sda rather than /dev/sda1. The latter would format an existing partition instead of replacing the entire partitioning scheme. Because the root partition is Btrfs, make sure btrfs is compiled into the kernel, or put btrfs into mkinitcpio.conf#MODULES and regenerate the initramfs.

Install the boot loader like you would for a data storage device with a Master Boot Record. See Syslinux#Manually or GRUB/Tips and tricks#Install to partition or partitionless disk. If your kernel does not boot due to Failed to mount /sysroot., please add GRUB_PRELOAD_MODULES="btrfs" in /etc/default/grub and generate the grub configuration.

Ext3/4 to Btrfs conversion

Warning: There are many reports on the btrfs mailing list about incomplete/corrupt/broken conversions. Make sure you have working backups of any data you cannot afford to lose. See the Convert page on the Btrfs wiki for more information.

Boot from an install CD, then convert by doing:

# btrfs-convert /dev/partition

Mount the partition 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 fstab accordingly when using UUIDs. chroot into the system and rebuild your boot loader's menu list (see Install from existing Linux). If converting a root file system, while still chrooted, run mkinitcpio -p linux to regenerate the initramfs or the system will not successfully boot.

Note: If there is anything wrong, either unable to mount or write files to the newly converted Btrfs; there is always the option to rollback as long as the backup subvolume /ext2_saved is still there. Use the btrfs-convert -r /dev/partition command to rollback; this will discard any modifications to the newly converted Btrfs file system.

After confirming that there are no problems, complete the conversion by deleting the backup ext2_saved sub-volume. Note that you cannot revert back to ext3/4 without it.

# btrfs subvolume delete /ext2_saved

Finally, balance the file system to reclaim the space.

Remember that some applications which were installed prior have to be adapted to Btrfs.

Note: Ext3/4 to Btrfs conversion is a time consuming operation. For a 4 TiB file system and a regular HDD, it can take up to 10 hours.

Corruption recovery

Warning: The tool btrfs check has known issues, see section #btrfs check

btrfs-check cannot be used on a mounted file system. To be able to use btrfs-check without booting from a live USB, add it to the initial ramdisk:

/etc/mkinitcpio.conf
BINARIES=(btrfs)

Regenerate the initramfs.

Then if there is a problem booting, the utility is available for repair.

Note: If the fsck process has to invalidate the space cache (and/or other caches?), it is normal for a subsequent boot to hang up for a while (it may give console messages about btrfs-transaction being hung). The system should recover from this after a while.

See btrfs-check(8) for more information.

Booting into snapshots

In order to boot into a snapshot, the same procedure applies as for mounting a subvolume as your root partition, as given in section mounting a subvolume as your root partition, because snapshots can be mounted like subvolumes.

  • If using GRUB, you can automatically populate your boot menu with Btrfs snapshots when regenerating the configuration file with the help of grub-btrfs or grub-btrfs-gitAUR.
  • If using rEFInd, you can automatically populate your boot menu with Btrfs snapshots with the help of refind-btrfsAUR, after enabling refind-btrfs.service.

Use Btrfs subvolumes with systemd-nspawn

See the Systemd-nspawn#Use Btrfs subvolume as container root and Systemd-nspawn#Use temporary Btrfs snapshot of container articles.

Reducing access time metadata updates

Because of the copy-on-write nature of Btrfs, simply accessing files can trigger the metadata copy and writing. Reducing the frequency of access time updates may eliminate this unexpected disk usage and increase performance. See fstab#atime options for the available options.

Incremental backup to external drive

The following packages use btrfs send and btrfs receive to send backups incrementally to an external drive. Refer to their documentation to see differences in implementation, features, and requirements.

  • btrbk — Tool for creating snapshots and remote backups of Btrfs subvolumes.
https://github.com/digint/btrbk || btrbk
  • snap-sync — Use Snapper snapshots to backup to external drive or remote machine.
https://github.com/wesbarnett/snap-sync || snap-sync
  • snapsync — A synchronization tool for Snapper.
https://github.com/doudou/snapsync || ruby-snapsyncAUR

The following package allows backing up snapper snapshots to non-Btrfs file systems.

  • snapborg — borgmatic-like tool which integrates snapper snapshots with borg backups.
https://github.com/enzingerm/snapborg || snapborgAUR

Automatic snapshots

For the managing and automatic creation of Btrfs snapshot, one may use a snapshot manager such as Snapper, Timeshift or Yabsnap.

Troubleshooting

See the Btrfs Troubleshooting pages and Btrfs Problem FAQ for general troubleshooting.

GRUB

Partition offset

The offset problem may happen when you try to embed core.img into a partitioned disk. It means that it is OK to embed GRUB's core.img into a Btrfs pool on a partitionless disk (e.g. /dev/sdX) directly.

GRUB can boot Btrfs partitions, however the module may be larger than other file systems. And the core.img file made by grub-install may not fit in the first 63 sectors (31.5KiB) of the drive between the MBR and the first partition. Up-to-date partitioning tools such as fdisk and gdisk avoid this issue by offsetting the first partition by roughly 1MiB or 2MiB.

Missing root

The factual accuracy of this article or section is disputed.

Reason: Suggests editing a non-configuration file manually. (Discuss in Talk:Btrfs#Should not suggest to edit files in /usr/share)

Users experiencing 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 the system should boot without an error.

Mounting timed out

Sometimes, especially with large RAID1 arrays, mounting might time out during boot with a journal message such as:

Jan 25 18:05:12 host systemd[1]: storage.mount: Mounting timed out. Terminating.
Jan 25 18:05:46 host systemd[1]: storage.mount: Mount process exited, code=killed, status=15/TERM
Jan 25 18:05:46 host systemd[1]: storage.mount: Failed with result 'timeout'.
Jan 25 18:05:46 host systemd[1]: Failed to mount /storage.
Jan 25 18:05:46 host systemd[1]: Startup finished in 32.943s (firmware) + 3.097s (loader) + 7.247s (kernel)>
Jan 25 18:05:46 host kernel: BTRFS error (device sda): open_ctree failed

This can easily be worked around by providing a longer timeout via the systemd-specific mount option x-systemd.mount-timeout in fstab. For example:

/dev/sda                /storage    btrfs       rw,relatime,x-systemd.mount-timeout=5min  0 0

BTRFS: open_ctree failed

The factual accuracy of this article or section is disputed.

Reason: The status is old. Replacing udev hook with systemd makes no sense since they both use systemd-udevd. (Discuss in Talk:Btrfs)

As of November 2014, there seems to be a bug in systemd or mkinitcpio causing the following error on systems with multi-device Btrfs file system using the btrfs hook in mkinitcpio.conf:

BTRFS: open_ctree failed
mount: wrong fs type, bad option, bad superblock on /dev/sdb2, missing codepage or helper program, or other error

In some cases, useful info is found in syslog - try dmesg|tail or so.

You are now being dropped into an emergency shell.

A workaround is to remove btrfs from the HOOKS array in /etc/mkinitcpio.conf and instead add btrfs to the MODULES array. Then regenerate the initramfs and reboot.

You will get the same error if you try to mount a raid array without one of the devices. In that case, you must add the degraded mount option to /etc/fstab. If your root resides on the array, you must also add rootflags=degraded to your kernel parameters.

As of August 2016, a potential workaround for this bug is to mount the array by a single drive only in /etc/fstab, and allow Btrfs to discover and append the other drives automatically. Group-based identifiers such as UUID and LABEL appear to contribute to the failure. For example, a two-device RAID1 array consisting of 'disk1' and disk2' will have a UUID allocated to it, but instead of using the UUID, use only /dev/mapper/disk1 in /etc/fstab. For a more detailed explanation, see the following blog post.

Another possible workaround is to remove the udev hook in mkinitcpio.conf and replace it with the systemd hook. In this case, btrfs should not be in the HOOKS or MODULES arrays.

See the original forums thread and FS#42884 for further information and discussion.

btrfs check

This article or section is out of date.

Reason: The "heavy development" status is old. (Discuss in Talk:Btrfs)
Warning: Since Btrfs is under heavy development, especially the btrfs check command, it is highly recommended to create a backup and consult btrfs-check(8) before executing btrfs check with the --repair switch.

The btrfs-check(8) command can be used to check or repair an unmounted Btrfs file system. However, this repair tool is still immature and not able to repair certain file system errors even those that do not render the file system unmountable.

Constant drive activity

Since the kernel version 6.2, discard=async mount(8) option is set by default. This has been reported to cause constant drive activity on some drives even while idle, as the discard queue is filled faster than it is processed. This can cause increased power usage, especially on NVMe-based drives.

As of kernel version 6.3, the default discard iops_limit has been changed from 100 to 1000 to address this issue. You can manually set it to a desired value on an old kernel version, e.g.

# echo 1000 > /sys/fs/btrfs/uuid/discard/iops_limit

Where uuid is the UUID of the Btrfs file system. The limit of 1000 will need to be tuned experimentally.

To set the parameter on boot, systemd-tmpfiles may be used, e.g. by creating the following file:

/etc/tmpfiles.d/btrfs-discard.conf
w /sys/fs/btrfs/uuid/discard/iops_limit - - - - 1000

Alternatively, one can disable asynchronous discard altogether by mounting using the nodiscard mount option in fstab, and instead use Periodic TRIM.

See also