Bcache

提供: ArchWiki
2017年3月20日 (月) 01:35時点におけるKusakata (トーク | 投稿記録)による版 (翻訳)
(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)
ナビゲーションに移動 検索に移動

Bcache を使うことで SSD を他のブロックデバイス (HDD やアレイ) の読み書きキャッシュ (writeback モード) あるいは読み取りキャッシュ (writethrough または writearound) として活用できます。この記事ではルートパーティションとして Bcache を使って Arch をインストールする方法を説明します。bcache 自体の説明は bcache のホームページ を読んでください。bcache のマニュアル も参照してください。Bcache は 3.10 からメインラインカーネルに含まれています。Arch Linux のインストールディスクのカーネルには 2013.08.01 から含まれています。

Bcache の代わりになるものとして Facebook の Flashcache やその子孫である EnhanceIO が存在します。

Bcache を使うにはバッキングデバイスを bcache ブロックデバイスとしてフォーマットする必要があります。大抵の場合、blocks to-bcache で変換ができます。

警告:
  • 重要なデータは最初にバックアップしてください。
  • bcache-dev ブランチは活発に開発されています。3.18 におけるオンディスクフォーマットの変更は前のフォーマットとの後方互換性がありません [1]。上流の Linux カーネルに組み込まれているバージョンには影響はありません [2]
  • Bcache と btrfs を組み合わせるとファイルシステムが壊れてしまう可能性があります。詳しくは こちらの記事 を参照してください。Btrfs wiki によればカーネル 3.19 以上では修正されているとのことです [3]

既存の環境に bcache デバイスをセットアップ

1. AUR から bcache-toolsAUR パッケージをインストールしてください。

2. バッキングデバイスを作成してください (通常はメカニカルなドライブになります)。バッキングデバイスはデバイス全体でもパーティションでも、あるいは他の標準ブロックデバイスでもかまいません。以下のコマンドで /dev/bcache0 が作成されます:

# make-bcache -B /dev/sdx1

3. キャッシュデバイスを作成してください (通常は SSD になります)。バッキングデバイスもブロックデバイスなら何でもかまいません。

# make-bcache -C /dev/sdy2

上記の例ではデフォルトのブロックサイズ 512B とバケットサイズ 128kB を使います。ブロックサイズはバッキングデバイスのセクタサイズ (通常は 512 あるいは 4k となります) と一致させるべきです。バケットサイズはライトアンプリフィケーションを避けるためにキャッシュデバイスの消去ブロックサイズ (EBS) と合わせるべきです。例えば、4k セクタの HDD と消去ブロックサイズが 2MB の SSD を使用する場合、コマンドは以下のようになります:

# make-bcache --block 4k --bucket 2M -C /dev/sdy2

4. バッキングデバイスに対してキャッシュデバイスを登録してください。# bcache-super-show /dev/sdy2 | grep cset.uuid を実行してキャッシュセット UUID を確認し、最初に bcache デバイスに追加してください。再起動後は Udev ルールによって実行されるため、一回だけ実行すれば十分です:

# echo cset.uuid > /sys/block/bcache0/bcache/attach

5. (キャッシュを読み書き両方で使いたい場合) キャッシュモードを変更してください:

# echo writeback > /sys/block/bcache0/bcache/cache_mode

6. initcpio のときからパーティションを使えるようにする必要がある場合 (ブートプロセスの中で必要とする場合)、/etc/mkinitcpio.conf の MODULES 行に 'bcache' を追加して blockfilesystem の間に 'bcache' フックを追加してください。その後、initramfs イメージを再生成してください。

Bcache の管理

1. 正しくセットアップされていることを確認:

# cat /sys/block/bcache0/bcache/state

出力は以下のどれかになります:

  • no cache: bcache のバッキングデバイスにキャッシュデバイスが登録されていません。
  • clean: 全て問題ありません。キャッシュはクリーンな状態です。
  • dirty: 正しくセットアップされており writeback が有効になっていてキャッシュが汚い状態です。
  • inconsistent: バッキングデバイスがキャッシュデバイスと同期されていない問題が発生しています。

You can have a /dev/bcache0 device associated with a backing device with no caching device attached. This means that all I/O (read/write) are passed directly to the backing device (pass-through mode)

2. 使用しているキャッシュモードを確認するには:

# cat /sys/block/bcache0/bcache/cache_mode 
[writethrough] writeback writearound none

上記の例では writethrough モードが有効になっています。

3. bcached デバイスについての情報を表示:

# bcache-super-show /dev/sdXY

4. バッキングデバイスを停止:

# echo 1 > /sys/block/sdX/sdX[Y]/bcache/stop

5. キャッシュデバイスの登録解除:

# echo 1 > /sys/block/sdX/sdX[Y]/bcache/detach

6. キャッシュデバイスを安全に除去:

# echo <cache-set-uuid> > /sys/block/bcache0/bcache/detach

7. 登録されたデバイスを開放:

# echo 1 > /sys/fs/bcache/<cache-set-uuid>/stop

bcache デバイスにインストール

1. インストールディスク (2013.08.01 以上) を起動。

2. AUR から bcache-toolsAUR パッケージをインストール。

3. HDD をパーティション:

ノート: While it may be true that Grub2 does not offer support for bcache as noted below, it does, however, fully support UEFI. It follows then, that so long as the necessary modules for the linux kernel to properly handle your boot device are either compiled into the kernel or are included in an initramfs, and you can include these files on it, the separate boot partition described below may be omitted in favor of the FAT EFI system partition. See GRUB and/or UEFI for more.

grub cannot handle bcache, so you will need at least 2 partitions (boot and one for the bcache backing device). If you are doing UEFI, you will need an EFI System Partition (ESP) as well. E.g.:

1            2048           22527   10.0 MiB    EF00  EFI System
2           22528          432127   200.0 MiB   8300  arch_boot
3          432128       625142414   297.9 GiB   8300  bcache_backing
ノート: This example has no swapfile/partition. For a swap partition on the cache, use LVM in step 7. For a swap partition outside the cache, be sure to make a swap partition now.

4. HDD を bcache のバッキングデバイスとして設定:

# make-bcache -B /dev/sda3
ノート:
  • When preparing any boot disk it is important to know the ramifications of any decision you may make. Please review and review again the documentation for your chosen boot-loader/-manager and consider seriously how it might relate to bcache.
  • If all associated disks are partitioned at once as below bcache will automatically attach "-B backing stores" to the "-C ssd cache" and step 6 is unnecessary.
# make-bcache -B /dev/sd? /dev/sd? -C /dev/sd?

これで /dev/bcache0 デバイスが作成されます。

5. SSD を設定。SSH をキャッシュデバイスとしてフォーマットしてバッキングデバイスにリンク:

# make-bcache -C /dev/sdb
# echo /dev/sdb > /sys/fs/bcache/register 
# echo UUID__from_previous_command > /sys/block/bcache0/bcache/attach
ノート: If the UUID is forgotten, it can be found with ls /sys/fs/bcache/ after the cache device has been registered.

6. bcache デバイスを作成。/dev/bcache0 デバイスを自由に分割したい場合は LVM あるいは btrfs のサブボリュームを使ってください (/, /home, /var などを分けたい場合など):

# mkfs.btrfs /dev/bcache0
# mount /dev/bcache0 /mnt/
# btrfs subvolume create /mnt/root
# btrfs subvolume create /mnt/home
# umount /mnt

7. インストール用のマウントポイントを作成:

# mkfs.ext4 /dev/sda2
# mkfs.msdos /dev/sda1 (if your ESP is at least 500MB, use mkfs.vfat to make a FAT32 partition instead)
# pacman -S arch-install-scripts
# mount /dev/bcache0 -o subvol=root,compress=lzo /mnt/
# mkdir /mnt/boot /mnt/home
# mount /dev/bcache0 -o subvol=home,compress=lzo /mnt/home
# mount /dev/sda2 /mnt/boot
# mkdir /boot/efi
# mount /dev/sda1 /mnt/boot/efi/

8. インストールガイドに従ってシステムをインストールしてください。ただし以下の設定を行って下さい:

以下を行ってから mkinitcpio -p linux を実行してください:

  • AUR から bcache-toolsAUR パッケージをインストール。
  • /etc/mkinitcpio.conf を編集:
    • "bcache" モジュールを追加。
    • blockfilesystem フックの間に "bcache" フックを追加。

設定

There are many options that can be configured (such as cache mode, cache flush interval, sequential write heuristic, etc.) This is currently done by writing to files in /sys. See the bcache user documentation.

Changing the cache mode is done by echoing one of 'writethrough', 'writeback', 'writearound' or 'none' to /sys/block/bcache[0-9]/bcache/cache_mode.

Note that changes to /sys are temporary, and will revert back after a reboot. To set custom configurations at boot create a .conf file in /etc/tmpfile.d. To set, in a persistent fashion, the sequential cutoff for bcache0 to 1 MB and write back you could create a file /etc/tmpfile.d/my-bcache.conf with the contents

w /sys/block/bcache0/bcache/sequential_cutoff - - - - 1M
w /sys/block/bcache0/bcache/cache_mode        - - - - writeback

Tips and tricks

バッキングデバイスのりサイズ

It is possible to resize the backing device so long as you do not move the partition start. This process is described on the mailing list. Here is an example using btrfs volume directly on bcache0. For LVM containers or for other filesystems, procedure will differ.

拡大

In this example, I grow the filesystem by 4GB.

1. Reboot to a live CD/USB Drive (need not be bcache enabled) and use fdisk, gdisk, parted, or your other favorite tool to delete the backing partition and recreate it with the same start and a total size 4G larger.

警告: Do not use a tool like GParted that might perform filesystem operations! It will not recognize the bcache partition and might overwrite part of it!!

2. Reboot to your normal install. Your filesystem will be currently mounted. That is fine. Issue the command to resize the partition to its maximum. For btrfs, that is

# btrfs filesystem resize max /

For ext3/4, that is:

# resize2fs /dev/bcache0

縮小

In this example, I shrink the filesystem by 4GB.

1. Disable writeback cache (switch to writethrough cache) and wait for the disk to flush.

# echo writethrough > /sys/block/bcache0/bcache/cache_mode
$ watch cat /sys/block/bcache0/bcache/state

wait until state reports "clean". This might take a while.

強制的にキャッシュを消去

I suggest to use

# echo 0 > /sys/block/bcache0/bcache/writeback_percent

This will flush the dirty data of the cache to the backing device in less a minute.

Revert back the value after with

# echo 10 > /sys/block/bcache0/bcache/writeback_percent

2. Shrink the mounted filesystem by something more than the desired amount, to ensure we do not accidentally clip it later. For btrfs, that is:

# btrfs filesystem resize -5G /

For ext3/4 you can use resize2fs, but only if the partition is unmounted

$ df -h /home
/dev/bcache0    290G   20G   270G   1% /home
# umount /home
# resize2fs /dev/bcache0 283G

3. Reboot to a LiveCD/USB drive (does not need to support bcache) and use fdisk, gdisk, parted, or your other favorite tool to delete the backing partition and recreate it with the same start and a total size 4G smaller.

警告: Do not use a tool like GParted that might perform filesystem operations! It will not recognize the bcache partition and might overwrite part of it!!

4. Reboot to your normal install. Your filesystem will be currently mounted. That is fine. Issue the command to resize the partition to its maximum (that is, the size we shrunk the actual partition to in step 3). For btrfs, that is:

# btrfs filesystem resize max /

For ext3/4, that is:

# resize2fs /dev/bcache0

5. Re-enable writeback cache if you want that enabled:

# echo writeback > /sys/block/bcache0/bcache/cache_mode
ノート: If you are very careful you can shrink the filesystem to the exact size in step 2 and avoid step 4. Be careful, though, many partition tools do not do exactly what you want, but instead adjust the requested partition start/end points to end on sector boundaries. This may be difficult to calculate ahead of time

トラブルシューティング

/dev/bcache device does not exist on bootup

以下のようなエラーが busybox シェルに表示される場合:

ERROR: Unable to find root device 'UUID=b6b2d82b-f87e-44d5-bbc5-c51dd7aace15'.
You are being dropped to a recovery shell
    Type 'exit' to try and continue booting

This might happen if the backing device is configured for "writeback" mode (default is writearound). When in "writeback" mode, the /dev/bcache0 device is not started until the cache device is both registered and attached. Registering is something that needs to happen every bootup, but attaching should only have to be done once.

To continue booting, try one of the following:

  • Register both the backing device and the caching device
# echo /dev/sda3 > /sys/fs/bcache/register
# echo /dev/sdb > /sys/fs/bcache/register

If the /dev/bcache0 device now exists, type exit and continue booting. You will need to fix your initcpio to ensure devices are registered before mounting the root device.

ノート:
  • An error of "sh: echo: write error: Invalid argument" means the device was already registered or is not recognized as either a bcache backing device or cache. If using the udev rule on boot it should only attempt to register a device if it finds a bcache superblock
  • This can also happen if using udev's 69-bcache.rules in Installation's step 7 and blkid and bcache-probe "disagree" due to rogue superblocks. See bcache's wiki for a possible explanation/resolution.
  • Re-attach the cache to the backing device:

If the cache device was registered, a folder with the UUID of the cache should exist in /sys/fs/bcache. Use that UUID when following the example below:

# ls /sys/fs/bcache/
b6b2d82b-f87e-44d5-bbc5-c51dd7aace15     register     register_quiet
# echo b6b2d82b-f87e-44d5-bbc5-c51dd7aace15 > /sys/block/sda/sda3/bcache/attach

If the /dev/bcache0 device now exists, type exit and continue booting. You should not have to do this again. If it persists, ask on the bcache mailing list.

ノート: An error of sh: echo: write error: Invalid argument means the device was already attached. An error of sh: echo: write error: No such file or directory means the UUID is not a valid cache (make sure you typed it correctly).
  • Invalidate the cache and force the backing device to run without it. You might want to check some stats, such as "dirty_data" so you have some idea of how much data will be lost.
# cat /sys/block/sda/sda3/bcache/dirty_data
-3.9M

dirty data is data in the cache that has not been written to the backing device. If you force the backing device to run, this data will be lost, even if you later re-attach the cache.

# cat /sys/block/sda/sda3/bcache/running
0
# echo 1 > /sys/block/sda/sda3/bcache/running

The /dev/bcache0 device will now exist. Type exit and continue booting. You might want to unregister the cache device and run make-bcache again. An fsck on /dev/bcache0 would also be wise. See the bcache user documentation.

警告: Only invalidate the cache if one of the two options above did not work.

/sys/fs/bcache/ does not exist

起動したカーネルで bcache が有効になっていません。

参照