Btrfs
Btrfs は、耐障害性、修復、管理のしやすさに焦点を当てつつ、先進的な機能を実装することも主眼としている、Linux 向けのコピーオンライト (CoW) ファイルシステムです。Btrfs は Oracle、Red Hat、富士通、Intel、SUSE、STRATOなどによって共同で開発されており、GPL の条件下でライセンスされて広く貢献を受け入れています。
特徴
Ext4 が安全確実で、広い領域をもつ巨大なファイルシステムも扱えるのにもかかわらず、なぜ Btrfs に切り替えるのでしょうか。たしかに Btrfs は未だ試験的なものと思われており、安定性の向上途上です。しかし、Linux システムのデフォルトのファイルシステムとなる時が近づきつつあります。Linux のディストリビューションのなかには、最新のリリースにおいて既に Btrfs に切り替え始めたものもあります。Btrfs は、ZFS にも備わっている一般的な先進機能に対応しています。こうした機能は、 BSD 系ディストリビューションや NAS 機器において ZFS の人気が高くなっている理由でもあります。
- コピーオンライト (CoW) とスナップショット - "ホット" なファイルシステムや仮想マシン (VM) からでさえも、増分バックアップを容易にします。
- ファイルレベルチェックサム - ファイル毎のメタデータがチェックサムを含み、エラーの検出と修復に使用されます。
- 圧縮 - ファイルは動的に圧縮・展開され、読み込み性能を向上させます。
- 自動デフラグメンテーション - ファイルシステムは使用中に、バックグラウンドスレッドによって調整されます。
- サブボリューム - 複数のファイルシステムは、それぞれのパーティションの中に入れて使う代わりに、単一の容量プールを共有できます。
- RAID - Btrfs は固有の RAID 実装を持っており、RAID を行うのに LVM あるいは mdadm は必要ではありません。現時点では RAID 0、1、10 がサポートされています;RAID 5 と 6 は不安定と見られています。
- パーティションは任意です - Btrfs はパーティションに入れて使うことができる一方で、生デバイス (/dev/<device>) を直接使用する能力も持っています。
- データ重複排除 - 限定的なデータ重複排除サポートを持っています;しかしながら、重複排除は将来的に Btrfs の標準的機能となる予定です。これにより、Btrfs はバイナリ差分によってファイル比較を行うことで、容量を節約できます。
機能に関する最新で膨大な一覧は、公式 Wiki のステータスページをみてください。すべての機能が一般の使用に堪えるほど十分に成熟しているわけではありませんが。
Down the road, new clustered filesystems will readily take advantage of Btrfs with its copy on write and other advanced features for their object stores. Ceph is one example of a clustered filesystem that looks very promising, and can take advantage of Btrfs.
インストール
カーネル
Btrfs に対応するには、つぎのカーネルオプションを有効にしてください:
File systems ---> <*> Btrfs filesystem
Emerge
sys-fs/btrfs-progs パッケージには、 Btrfs を扱うために必要なユーティリティが入っています。
root #
emerge --ask sys-fs/btrfs-progs
使い方
Btrfs の長いコマンドをタイプすることはすぐに手間になるでしょう。それぞれのコマンド(最初の btrfs コマンド以外)はとても短い命令に短縮できます。この方法は、コマンドラインから作業するときにタイプする文字数を減らすのに役立ちます。
例えば、/ にあるファイルシステムをデフラグするには、長いコマンドは次のようになります:
root #
btrfs filesystem defragment -v /
Shorten each of the longer commands after the btrfs command by reducing them to their unique, shortest prefix. In this context, unique means that no other btrfs commands will match the command at the command's shortest length. The shortened version of the above command is:
root #
btrfs fi de -v /
fi
で始まる btrfs コマンドは他にありません;filesystem
が唯一です。filesystem
コマンドの下で、de
サブコマンドも同様です。
作成
mkfs.btrfs コマンドは、フォーマットするよう指定されたパーティションのすべての内容を不可逆的に破壊します。mkfs コマンドを実行する前に、正しいドライブとパーティションが選択されていることを必ず確認してください!
/dev/sdXN パーティションを Btrfs で作成するには:
root #
mkfs.btrfs /dev/sdXN
上の例の、N
をフォーマットしたいパーティション番号で、X
をフォーマットしたいディスクレターで置き換えてください。例えば、システムの 1 番目のディスクの 3 番目のパーティションを Btrfs でフォーマッしたい場合には、次を実行してください:
root #
mkfs.btrfs /dev/sda3
すべての Btrfs パーティションについて、/etc/fstab の最後の数値カラムは
0
とすべきです。fsck.btrfs と btrfsck はシステム起動時に毎回実行すべきではありません。マウント
ファイルシステムを作成したら、さまざまな方法でマウント可能です:
- mount - 手動マウント。
- fstab - /etc/fstab にマウントポイントを定義することで、システム起動時に自動でマウントされます。
- Removable media - 必要に応じた自動マウント(USB ドライブに便利です)。
- AutoFS - ファイルシステムアクセス時に自動マウント。
ext* ベースファイルシステムから変換する
btrfs-convert ユーティリティを使用して、ext2、ext3、ext4 ファイルシステムを Btrfs に変換することができます。
次の手順はアンマウントされたファイルシステムの変換のみサポートしています。ルートパーティションを変換するためには、システムレスキューディスク(SystemRescueCD は上手く働きます)で起動してから、ルートパーティションに対して変換コマンドを実行してください。
まず、マウントポイントを確実にアンマウントしてください:
root #
umount <mounted_device>
適切な fsck ツールを使用してファイルシステムの整合性を確認してください。次の例ではファイルシステムは ext4 としています:
root #
fsck.ext4 -f <unmounted_device>
btrfs-convert を使用して、ext* フォーマットされたデバイスを Btrfs フォーマットされたデバイスに変換してください:
root #
btrfs-convert <unmounted_device>
デバイスがフォーマットされたら、忘れずに /etc/fstab を編集して、ファイルシステムのカラムを ext4 から Btrfs に変更してください:
/etc/fstab
ext4 を btrfs に変更する<device> <mountpoint> btrfs defaults 0 0
デフラグ
Btrfs の機能のひとつに、オンラインでのデフラグメンテーションがあります。ルート Btrfs ファイルシステムをデフラグするには、次を実行してください:
root #
btrfs filesystem defragment -r -v /
Defragmenting with kernel versions < 3.9 or ≥ 3.14-rc2 as well as with Linux stable kernel versions ≥ 3.10.31, ≥ 3.12.12 or ≥ 3.13.4 breaks up ref-links between files and their COW copies[1] and thus may increase space usage considerably. Make sure to have enough free space available and not too many snapshots on the drive as full btrfs partitions can get really slow.
圧縮
Btrfs は zlib、lzo、zstd (v5.1.0)[2] 圧縮アルゴリズムを使用した透過的圧縮をサポートしています。
ファイルの属性を設定することで特定のファイルのみ圧縮することも可能です:
user $
chattr +c
compress
マウントオプションは、新しく作成されたファイルをすべて圧縮するように、デフォルトの振る舞いを設定します。ファイルシステム全体を再圧縮するには、次のコマンドを実行してください:
root #
btrfs filesystem defragment -r -v -clzo /
CPU とディスク性能によっては、lzo 圧縮を使うと全体のスループットが向上するかもしれません。
lzo の代わりに zlib または zstd 圧縮アルゴリズムを使用することもできます。zlib は遅いですが高い圧縮率を持ち、zstd は両者の間でいい比率となっています[3]。
ファイルシステム全体で zlib 圧縮を強制するには:
root #
btrfs filesystem defragment -r -v -czlib /
圧縮レベル
カーネルバージョン 4.15.0 以降[4]、zlib 圧縮はレベルを 1-9 の間で設定することができます。カーネルバージョン 5.1.0 以降、zstd はレベル 1-15 の間で設定することができます。例えば、マウント時に zlib を最高圧縮に設定するには:
root #
mount -o compress=zlib:9 /dev/sdXY /path/to/btrfs/mountpoint
または最低圧縮に設定するには:
root #
mount -o compress=zlib:1 /dev/sdXY /path/to/btrfs/mountpoint
また、再マウントすることで圧縮率を調節するには:
root #
mount -o remount,compress=zlib:3 /path/to/btrfs/mountpoint
圧縮率は /proc/mounts を開いて、または次のコマンドを使用して最新の dmesg の出力を確認することで、見ることができるはずです。
root #
dmesg | grep -i btrfs
[ 0.495284] Btrfs loaded, crc32c=crc32c-intel [ 3010.727383] BTRFS: device label My Passport devid 1 transid 31 /dev/sdd1 [ 3111.930960] BTRFS info (device sdd1): disk space caching is enabled [ 3111.930973] BTRFS info (device sdd1): has skinny extents [ 9428.918325] BTRFS info (device sdd1): use zlib compression, level 3
圧縮率とディスク使用量
du や df など、使用中および空き容量を確認するための一般的なユーザ空間ツールは、Btrfs パーティションに対しては不正確な結果を返す場合があります。これは、例えば ext2/3/4 などと比較して、ファイルの書き込みがどのように行われるかに Btrfs 固有の設計の違いがあるためです[5]。
それゆえ、btrfs のユーザ空間ツール btrfs filesystem
が提供する du/df 代替を利用することを推奨します。加えて sys-fs/compsize パッケージに含まれる compsize ツールは、圧縮率と圧縮されたファイルのディスク使用量を考慮した追加情報を提供するので、役に立つでしょう。次の例は、/media/drive の下にマウントされた btrfs パーティションに対して、これらのツールを使用する例です。
user $
btrfs filesystem du -s /media/drive
Total Exclusive Set shared Filename 848.12GiB 848.12GiB 0.00B /media/drive/
user $
btrfs filesystem df /media/drive
Data, single: total=846.00GiB, used=845.61GiB System, DUP: total=8.00MiB, used=112.00KiB Metadata, DUP: total=2.00GiB, used=904.30MiB GlobalReserve, single: total=512.00MiB, used=0.00B
user $
compsize /media/drive
Processed 2262 files, 112115 regular extents (112115 refs), 174 inline. Type Perc Disk Usage Uncompressed Referenced TOTAL 99% 845G 848G 848G none 100% 844G 844G 844G zlib 16% 532M 3.2G 3.2G
複数のデバイス (RAID)
Btrfs は RAID を形成するために、複数のブロックデバイスとともに使用することができます。Btrfs を使用して複数のデバイスにまたがるファイルシステムを作成するのは、mdadm を使用して作成するのと比較して、作成時に初期化にかかる時間が不要なため、とても簡単です。
BTRFS はデータとメタデータを分離して扱います。これはマルチデバイスファイルシステムを使うときに留意すべき重要な要素です。データとメタデータのブロックグループに個別のプロファイルを使用することができます。例えば、メタデータは複数のデバイスをまたいだ RAID1 として構成する一方で、データは RAID5 として構成する、ということが可能です。RAID5 は最低でも 3 個のブロックデバイスを必要とするため、これは 3 個以上のブロックデバイスを使用するときに可能なプロファイルです。
この種のプロファイルは、すべてのデバイスにメタデータを持たせることによる冗長性と、データをデバイスをまたいでストライピングすることで読み込み速度を向上させるメリットを提供します。このプロファイルのデメリットは、メタデータのための必要以上の容量と、RAID5 がパリティビットを使用することによるデータブロックへの書き込み速度の低下です。
作成
最も簡単な方法は、複数のデバイスにまたがるファイルシステムを作成するために、パーティションで分けられていない複数のブロックデバイス全体を使用する方法です。例えば、2 個のデバイスをまたいで RAID1 モードのファイルシステムを作成するには:
root #
mkfs.btrfs -m raid1 <device1> <device2> -d raid1 <device1> <device2>
変換
Converting between RAID profiles is possible with the balance sub-command. For example, say three block devices are presently configured for RAID1 and mounted at /srv. It is possible to convert the data in this profile from RAID1 to RAID5 with using the following command:
root #
btrfs balance start -dconvert=raid5 --force /srv
Conversion can be performed while the filesystem is online and in use. Possible RAID modes in btrfs include RAID0, RAID1, RAID5, RAID6, and RAID10. See the upstream BTRFS wiki for more information.
It is currently not safe to use the RAID 5 or 6 modes[6]. RAID 5 and 6 modes have seen some fixes[7] in Linux 4.12, but overall status is still marked as unstable.[8][9]. Users who want to use RAID5 or RAID6 functionality of btrfs are encouraged to check the btrfs status page for stability status of said modes before utilizing the modes.
削除
デバイスパスによる指定
Block devices (disks) can be removed from multi-device filesystems using the btrfs device remove subcommand:
root #
sudo btrfs device remove /dev/sde /srv
デバイス ID による指定
Use the usage subcommand to determine the device IDs:
root #
btrfs device usage /srv
/dev/sdb, ID: 3 Device size: 1.82TiB Device slack: 0.00B Data,RAID1: 25.00GiB Data,RAID5: 497.00GiB Data,RAID5: 5.00GiB Metadata,RAID5: 17.00GiB Metadata,RAID5: 352.00MiB System,RAID5: 32.00MiB Unallocated: 1.29TiB /dev/sdc, ID: 1 Device size: 1.82TiB Device slack: 0.00B Data,RAID1: 25.00GiB Data,RAID5: 497.00GiB Data,RAID5: 5.00GiB Metadata,RAID5: 17.00GiB Metadata,RAID5: 352.00MiB System,RAID5: 32.00MiB Unallocated: 1.29TiB /dev/sdd, ID: 4 Device size: 1.82TiB Device slack: 0.00B Data,RAID1: 25.00GiB Data,RAID5: 497.00GiB Data,RAID5: 5.00GiB Metadata,RAID5: 17.00GiB Metadata,RAID5: 352.00MiB System,RAID5: 32.00MiB Unallocated: 1.29TiB /dev/sde, ID: 5 Device size: 0.00B Device slack: 0.00B Data,RAID1: 75.00GiB Data,RAID5: 5.00GiB Metadata,RAID5: 352.00MiB Unallocated: 1.74TiB
Next use the device ID to remove the device, in this case /dev/sde will be removed:
root #
btrfs device remove 5 /srv
サブボリューム
As mentioned above in the features list, Btrfs can create subvolumes. Subvolumes can be used to better organize and manage data. They become especially powerful when combined with snapshots. Important distinctions must be made between Btrfs subvolumes and subvolumes created by Logical Volume Management (LVM). Btrfs subvolumes are not block level devices, they are POSIX file namespaces.[10] They can be created at any location in the filesystem and will act like any other directory on the system with one caveat: subvolumes can be mounted and unmounted. Subvolumes are nestable (subvolumes can be created inside other subvolumes), and easily created or removed.
A subvolume cannot be created across different Btrfs filesystems. If /dev/sda and /dev/sdb both contain separate (non-RAID) Btrfs filesystems, there is no way a subvolume can expand across the two filesystems. The snapshot can be moved from one filesystem to another, but it cannot span across the two. It must be on /dev/sda or /dev/sdb.
作成
To create a subvolume, issue the following command inside a Btrfs filesystem's name space:
root #
btrfs subvolume create <dest-name>
Replace <dest-name>
with the desired destination and subvolume name. For example, if a Btrfs filesystem exists at /mnt/btrfs, a subvolume could be created inside it using the following command:
root #
btrfs subvolume create /mnt/btrfs/subvolume1
一覧
To see the subvolume(s) that have been created, use the subvolume list
command followed by a Btrfs filesystem location. If the current directory is somewhere inside a Btrfs filesystem, the following command will display the subvolume(s) that exist on the filesystem:
root #
btrfs subvolume list .
If a Btrfs filesystem with subvolumes exists at the mount point created in the example command above, the output from the list command will look similar to the following:
root #
btrfs subvolume list /mnt/btrfs
ID 309 gen 102913 top level 5 path mnt/btrfs/subvolume1
削除
Subvolumes can be properly removed by using the subvolume delete
command followed by the path to the subvolume. All available subvolume paths in a Btrfs filesystem can be seen using the list command above.
root #
btrfs subvolume delete <subvolume-path>
As above, replace <subvolume-path>
with the actual path to the subvolume to be removed. To delete the subvolume used in the examples above, the following command would be issued:
root #
btrfs subvolume delete /mnt/btrfs/subvolume1
Delete subvolume (no-commit): '/mnt/btrfs/subvolume1'
スナップショット
Snapshots are subvolumes that share data and metadata with other subvolumes. This is made possible by Btrfs' Copy on Write (CoW) ability.[10] Snapshots can be used for several purposes, one of which is to create backups of file system structures at specific points in time.
ルートのファイルシステムが Btrfs であるときには、 subvolume snapshot
コマンドを用いてスナップショットの作成が可能です:
root #
mkdir -p /mnt/backup/rootfs
root #
btrfs subvolume snapshot / /mnt/backup/rootfs/
The following small shell script can be added to a timed cron job to create a timestamped snapshot backup of a Btrfs formatted root filesystem. The timestamps can be adjusted to whatever is preferred by the user.
btrfs_snapshot.sh
Btrfs rootfs snapshot cron job example#!/bin/bash NOW=$(date +"%Y-%m-%d_%H:%M:%S") if [ ! -e /mnt/backup ]; then mkdir -p /mnt/backup fi cd / /sbin/btrfs subvolume snapshot / "/mnt/backup/backup_${NOW}"
Mounting
A subvolume can be mounted in a location different from where it was created, or users can choose to not mount them at all. For example, a user could create a Btrfs filesystem in /mnt/btrfs and create /mnt/btrfs/home and /mnt/btrfs/gentoo-repo subvolumes. The subvolumes could then be mounted at /home and /var/db/repos/gentoo, with the original top level subvolume left unmounted. This results in a configuration where the subvolumes' relative path from the top level subvolume is different from their actual path.
To mount a subvolume, perform the following command, where <rel-path>
is the relative path of the subvolume from the top level subvolume, obtainable through the subvolume list
command:
root #
mount -o subvol=<rel-path> <device> <mountpoint>
Similarly, one can update the filesystem tab to mount their Btrfs subvolumes like so:
/etc/fstab
Mounting Subvolumes<device> <mountpoint> btrfs subvol=<rel-path> 0 2
トラブルシューティング
Using with VM disk images
When using Btrfs with virtual machine disk images, it is best to disable copy-on-write on the disk images in order to speed up IO performance. This can only be performed on files that are newly created. It also possible to disable CoW on all files created within a certain directory. For example, using the chattr command:
root #
chattr +C /var/lib/libvirt/images
Clear the free space cache
clear_cache
マウントオプションを指定してファイルシステムをマウントすることで、Btrfs 内の空きのキャッシュ領域を開放することが可能です。例えば:
root #
mount -o clear_cache /path/to/device /path/to/mountpoint
Btrfs hogging memory (disk cache)
When utilizing some of Btrfs' special abilities (like making many --reflink
copies or creating high amounts of snapshots), a lot of memory can be consumed and not freed fast enough by the kernel's inode cache. This issue can go undiscovered since memory dedicated to the disk cache might not be clearly visible in traditional system monitoring utilities. The slabtop utility (available as part of the sys-process/procps package) was specifically created to determine how much memory kernel objects are consuming:
root #
slabtop
Active / Total Objects (% used) : 5011373 / 5052626 (99.2%) Active / Total Slabs (% used) : 1158843 / 1158843 (100.0%) Active / Total Caches (% used) : 103 / 220 (46.8%) Active / Total Size (% used) : 3874182.66K / 3881148.34K (99.8%) Minimum / Average / Maximum Object : 0.02K / 0.77K / 4096.00K OBJS ACTIVE USE OBJ SIZE SLABS OBJ/SLAB CACHE SIZE NAME 2974761 2974485 99% 1.10K 991587 3 3966348K btrfs_inode 1501479 1496052 99% 0.19K 71499 21 285996K dentry
If the inode cache is consuming too much memory, the kernel can be manually instructed to drop the cache by echoing an integer value to the /proc/sys/vm/drop_caches file[11].
To be safe, and to help the kernel determine the maximum amount of freeable memory, be sure to run a sync before running the echo commands below:
user $
sync
Most of the time Btrfs users will probably want to echo 2 to reclaim just the slab objects (dentries and btrfs_inodes):
root #
echo 2 > /proc/sys/vm/drop_caches
To clear the entire disk cache (slab objects and the page cache) use echo 3 instead:
root #
echo 3 > /proc/sys/vm/drop_caches
While the above commands are non-destructive (as long as a sync was completed before running them), they could seriously but temporarily slow down the system while the kernel loads only the necessary items back into memory. Think twice before running the above commands for systems under heavy load!
More information on kernel slabs can be found in this dedoimedo blog entry.
Mounting Btrfs fails, returning mount: unknown filesystem type 'btrfs'
The original solution by Tim on Stack Exchange inspired the following solution: build the kernel manually instead of using genkernel:
#
cd /usr/src/linux
#
make menuconfig
#
make && make modules_install
#
cp arch/x86_64/boot/bzImage /boot
#
mv /boot/bzImage /boot/whatever_kernel_filename
#
genkernel --install initramfs
Btrfs root doesn't boot
Genkernel's initramfs as created with the command below doesn't load btrfs:
root #
genkernel --btrfs initramfs
Compile support for btrfs in the kernel rather than as a module or Dracut to generate the initramfs.
参考
- Btrfs snapshots - Script that creates snapshots when files have changed
- Btrfs/System Root Guide - Use the Btrfs filesystem as a collection of subvolumes including one as a system root.
- Btrfs native system root guide - An alternative guide on using a subvolume in a Btrfs filesystem as the system's root.
- Ext4 — オープンソースのディスクファイルシステムで、extended filesystem シリーズの最新バージョンです。
- Btrbk - A backup tool for btrfs subvolumes, taking advantage of btrfs specific capabilities to create atomic snapshots and transfer them incrementally to specified backup locations.
- Samba shadow copies - Using Samba to expose Shadow Copies as 'Previous Versions' to Windows clients.
- Snapper — a command-line program capable of managing filesystem snapshots.
- ZFS — a next generation filesystem created by Matthew Ahrens and Jeff Bonwick.
外部の情報
- https://wiki.debian.org/Btrfs - Debian wiki 内の記事
- https://wiki.archlinux.org/index.php/Btrfs - ArchWiki 内の記事
- http://www.funtoo.org/BTRFS_Fun - Funtoo wiki 内の BTRFS Fun 記事
- http://marc.merlins.org/perso/btrfs/post_2014-05-04_Fixing-Btrfs-Filesystem-Full-Problems.html - いくつかの状況における、Btrfs ファイルシステムのニッチな問題を修復するためのヒントと小技。
参照
- ↑ man page for btrfs-filesystem(8), Btrfs wiki. Retrieved on 6th February, 2017.
- ↑ https://btrfs.wiki.kernel.org/index.php/Compression#Can_I_set_the_compression_level.3F
- ↑ https://git.kernel.org/pub/scm/linux/kernel/git/mason/linux-btrfs.git/commit/?h=next&id=5c1aab1dd5445ed8bdcdbb575abc1b0d7ee5b2e7
- ↑ https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=f51d2b59120ff364a5e612a594ed358767e1cd09
- ↑ https://btrfs.wiki.kernel.org/index.php/Compression#How_can_I_determine_compressed_size_of_a_file.3F
- ↑ Article mentioning that parity RAID code has multiple serious data-loss bugs, Btrfs wiki. Retrieved on January 1st, 2017.
- ↑ Michael Larabel, Btrfs RAID56 "Mostly OK", Phoronix. July 8, 2017.
- ↑ btrfs: scrub: Fix RAID56 recovery race condition, source commit, April 18th 2017.
- ↑ GIT PULL Btrfs from Chris Mason, Linux kernel mailinglist, May 9th 2017.
- ↑ 10.0 10.1 Section explaining the differences between subvolumes and logical volumes in LVM, Btrfs wiki. Retrieved on January 1st, 2017.
- ↑ Documentation for /proc/sys/vm/*, Kernel.org. Retrieved on January 1st, 2017.