Btrfs (Русский)
Из Wikipedia:Btrfs:
- Btrfs (файловая система основанная на структурах B-деревьев, произносится как "butter F S", "better F S", "b-tree F S", или просто как аббревиатура) эта файловая система работает по принципу «копирование при записи» (COW), первоначально разработанная Oracle Corporation для использования в Linux. Разработка Btrfs началась в 2007, а в августе 2014 файловая система была помечена как стабильная.
Из Btrfs Wiki:
- Btrfs это новая файловая система для Linux с принципом копирования при записи (CoW), направленная на реализацию дополнительных функций с особым упором на отказоустойчивость, восстановление и простоту администрирования. Совместная разработка Oracle, Red Hat, Fujitsu, Intel, SUSE, STRATO и многих других, Btrfs распространяется под лицензией GPL, что позволяет внести свой вклад любому желающему.
Contents
Подготовка
Официальные ядра linux и linux-lts содержат поддержку Btrfs. Если вы хотите разместить загрузчик на разделе с файловой системой Btrfs, проверьте, поддерживает ли ваш boot loader Btrfs.
Установите пакет пользовательских утилит btrfs-progs.
Создание раздела диска Btrfs
Btrfs может обладать всем устройством хранения данных, заменяя схемы разбиения MBR или GPT, используя подтома для имитации разделов. Не нужно разбивать разделы, чтобы просто создать файловую систему Btrfs[broken link: invalid section] на существующем разделе который был создан с использованием другого метода. There are some limitations to partitionless single disk setups:
- Cannot use different file systems for different mount points.
- Cannot use swap area as Btrfs does not support swap files and there is no place to create swap partition. This also limits the use of hibernation/resume, which needs a swap area to store the hibernation image.
- Cannot use UEFI to 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.
Install the boot loader like you would for a data storage device with a Master Boot Record. See Syslinux#Manual install or GRUB#Install to partition or partitionless disk[broken link: invalid section].
Создание файловой системы
Файловая система Btrfs может быть создана с нуля или конвертирована из имеющейся ext3/ext4.
Создание новой файловой системы
Файловая система на одном устройстве
Для форматирования раздела:
# mkfs.btrfs -L mylabel /dev/partition
В Btrfs размер блока по умолчанию 16KB. Для того чтобы использовать больший размер блока для данных/метаданных, укажите значение nodesize
с помощью -n
как показано в примере с 16KB блоками:
# mkfs.btrfs -L МояМетка -n 16k /dev/partition
Файловая система на нескольких устройствах
Multiple devices can be entered to create a RAID. Supported RAID levels include RAID 0, RAID 1, RAID 10, RAID 5 and RAID 6. The RAID levels can be configured separately for data and metadata using the -d
and -m
options respectively. By default the data is striped (raid0
) and the metadata is mirrored (raid1
). See Using Btrfs with Multiple Devices for more information about how to create a Btrfs RAID volume as well as the manpage for mkfs.btrfs
.
# mkfs.btrfs -d raid0 -m raid1 /dev/part1 /dev/part2 ...
You must include either the udev
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.
See #RAID for advice on maintenance specific to multi-device Btrfs file systems.
Конвертация Ext3/4 в Btrfs
Boot from an install CD, then convert by doing:
# btrfs-convert /dev/partition
Mount the partion and test the conversion by checking the files. Be sure to change the /etc/fstab
to reflect the change (type to btrfs
and fs_passno [the last field] to 0
as Btrfs does not do a file system check on boot). Also note that the UUID of the partition will have changed, so update fstab accordingly when using UUIDs. 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 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 grub-install /dev/partition
and regenerate the config as well grub-mkconfig -o /boot/grub/grub.cfg
.
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.
Настройка файловой системы
Копирование при записи (CoW)
По умолчанию, Btrfs использует Копирование при записи для всех файлов постоянно. Чтобы узнать как это реализовано и какие есть преимущества и недостатки, смотрите the Btrfs Sysadmin Guide section.
Отключение CoW
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.
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. through cp --reflink=always
or because of a filesystem snapshot), copy-on-write still occurs.
Принудительное CoW
To force copy-on-write when copying files use:
$ cp --reflink source dest
This would only be required if CoW was disabled for the file to be copied (as implemented above). See the man page on cp
for more details on the --reflink
flag.
Сжатие
Btrfs supports transparent compression, meaning every file on the partition is automatically compressed. This not only reduces the size of files, but also improves performance, in particular if using the lzo algorithm, in some specific use cases (e.g. single thread with heavy file IO), while obviously harming performance on other cases (e.g. multithreaded and/or cpu intensive tasks with large file IO).
Compression is enabled using the compress=zlib
or compress=lzo
mount options. Only files created or modified after the mount option is added will be compressed. However, it can be applied quite easily to existing files (e.g. after a conversion from ext3/4) using the btrfs filesystem defragment -calg
command, where alg
is either zlib
or lzo
. In order to re-compress the whole file system with lzo, run the following command:
# btrfs filesystem defragment -r -v -clzo /
When installing Arch to an empty Btrfs partition, use the compress
option when mounting the file system: mount -o compress=lzo /dev/sdxY /mnt/
. During configuration, add compress=lzo
to the mount options of the root file system in fstab.
Подтома
"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." [2]
Each Btrfs file system has a top-level subvolume with ID 5. It can be mounted as /
(by default), or another subvolume can be mounted[broken link: invalid section] instead.
See the following links for more details:
- Btrfs Wiki SysadminGuide#Subvolumes
- Btrfs Wiki Getting started#Basic Filesystem Commands
- Btrfs Wiki Trees
Создание подтома
Чтобы создать подтом:
# btrfs subvolume create /path/to/subvolume
Просмотр подтомов
Чтобы просмотреть список текущих подтомов по пути
:
# btrfs subvolume list -p путь
Удаление подтома
Для удаления подтома:
# btrfs subvolume delete /path/to/subvolume
Попытка удалить каталог /путь/к/подтому
без использования указанной выше команды не удалит подтом.
Монтирование подтомов
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. Thus one can restore a file system (or part of it) to a previous state easily using #Snapshots[broken link: invalid section].
See Snapper#Suggested filesystem layout, Btrfs SysadminGuide#Managing Snapshots, and Btrfs SysadminGuide#Layout for example file system layouts using subvolumes.
Изменение подтома по умолчанию
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[broken link: invalid section].
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 commit
mount option in /etc/fstab
for the btrfs partition.
LABEL=arch64 / btrfs defaults,noatime,ssd,compress=lzo,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 resides in Documentation/sysctl/vm.txt
.
SSD TRIM
Файловая система Btrfs способна освобождать неиспользуемые блоки из SSD-диска, поддерживающего команду TRIM. Больше информации о задействовании и использовании TRIM можно найти в разделе Solid State Drives#TRIM.
Использование
Показать использованное/свободное место
General linux userspace tools such as /usr/bin/df
will inaccurately report free space on a Btrfs partition. It is recommended to use /usr/bin/btrfs
to query a Btrfs partition. Below is an illustration of this effect, first querying using df -h
, and then using btrfs filesystem df
:
$ df -h /
Filesystem Size Used Avail Use% Mounted on /dev/sda3 119G 3.0G 116G 3% /
$ btrfs filesystem df /
Data: total=3.01GB, used=2.73GB System: total=4.00MB, used=16.00KB Metadata: total=1.01GB, used=181.83MB
Notice that df -h
reports 3.0GB used but btrfs filesystem df
reports 2.73GB for the data. This is due to the way Btrfs allocates space into the pool. The true disk usage is the sum of all three 'used' values which is inferior to 3.0GB as reported by df -h
.
Another useful command to show a less verbose readout of used space is btrfs filesystem show
:
# btrfs filesystem show /dev/sda3
The newest command to get information on free/used space of a is btrfs filesystem usage
:
# btrfs filesystem usage
Дефрагментация
Btrfs supports online defragmentation. To defragment the metadata of the root folder:
# btrfs filesystem defragment /
This will not defragment the entire file system. For more information read this page on the Btrfs wiki.
To defragment the entire file system verbosely:
# btrfs filesystem defragment -r -v /
RAID
Btrfs offers native "RAID" for #Multi-device file system[broken link: invalid section]s. 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.
Scrub
The 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."
Start manually
To start a (background) scrub on the filesystem which contains /
:
# btrfs scrub start /
To check the status of a running scrub:
# btrfs scrub status /
Запуск службы или таймера
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 the systemd-escape tool to escape a given string, see systemd-escape(1)
for examples.
You can also run the scrub by starting btrfs-scrub@.service
(with the same encoded path). The advantage of this over # btrfs scrub
is that the results of the scrub will be logged in the systemd journal.
Balance
"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." [5] 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 "filesystem full" issues.
# btrfs balance start / # btrfs balance status /
Снимки
"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 writable version of a readonly snapshot, simply create a snapshot of it.
Отправить / получить
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).
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 on how to use this for an incremental backups and for tools that automate the process.
Известные проблемы
Несколько ограничений должны быть известны перед использованием.
Шифрование
Btrfs has no built-in encryption support, but this may come in future. Users can encrypt the partition before running mkfs.btrfs
. See dm-crypt/Encrypting an entire system#Btrfs subvolumes with swap.
Existing Btrfs file systems can use something like EncFS or TrueCrypt, though perhaps without some of Btrfs' features.
Файл подкачки (Swap)
Btrfs does not yet support swap files. This is due to swap files requiring a function that Btrfs does not have for possibility of file system corruption [6]. Patches for swapfile support are already available [7] and may be included in an upcoming kernel release. As an alternative a swap file can be mounted on a loop device with poorer performance but will not be able to hibernate. Install the package systemd-swap to automate this.
Ядро Linux-rt
As of version 3.14.12_rt9, the linux-rt[broken link: invalid section] kernel does not boot with the Btrfs file system. This is due to the slow development of the rt patchset.
Tips and tricks
Checksum hardware acceleration
Verify if Btrfs checksum is hardware accelerated:
$ dmesg | grep crc32c
Btrfs loaded, crc32c=crc32c-intel
If you see crc32c=crc32c-generic
, it is probably because your root partition is Btrfs, and you will have to compile crc32c-intel into kernel to make it work. Note: put crc32c-intel
into mkinitcpio.conf does NOT work.
Corruption recovery
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="/usr/bin/btrfs"
Regenerate the initial ramdisk using mkinitcpio.
Then if there is a problem booting, the utility is available for repair.
See the Btrfs Wiki page for more information.
Загрузка в снимки с помощью GRUB
You can manually create a GRUB#GNU/Linux menu entry[broken link: invalid section] with the rootflags=subvol=
argument. The subvol=
mount options in /etc/fstab
of the snapshot to boot into also have to be specified correctly.
Alternatively, you can automatically populate your GRUB menu with btrfs snapshots when regenerating the GRUB configuration file by using grub-btrfs or grub-btrfs-gitAUR.
Использование подтомов Btrfs с systemd-nspawn
See the Systemd-nspawn#Use Btrfs subvolume as container root and Systemd-nspawn#Use temporary Btrfs snapshot of container articles.
Решение проблем
See the Btrfs Problem FAQ for general troubleshooting.
GRUB
Смещение разделов
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.
Отсутствует root
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.
Ошибка 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 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 with mkinitcpio -p linux
(adjust the preset if needed) and reboot.
See the original forums thread and FS#42884 for further information and discussion.
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.
Проверка btrfs
The 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.
See Btrfsck for more information.
Смотрите также
- Официальный сайт
- Performance related
-
Miscellaneous
- Funtoo Wiki Btrfs Fun
- Avi Miller presenting Btrfs at SCALE 10x, January 2012.
- Summary of Chris Mason's talk from LFCS 2012
- Btrfs: stop providing a bmap operation to avoid swapfile corruptions 2009-01-21
- Doing Fast Incremental Backups With Btrfs Send and Receive