Bcache

提供: ArchWiki
2018年2月7日 (水) 00:17時点におけるKusakata.bot (トーク | 投稿記録)による版 (文字列「http://www.spinics.net/」を「https://www.spinics.net/」に置換)
ナビゲーションに移動 検索に移動

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: バッキングデバイスがキャッシュデバイスと同期されていない問題が発生しています。

/dev/bcache0 デバイスをキャッシュデバイスがアタッチされていないバッキングデバイスに関連付けることもできます。全ての I/O (読み書き) が直接バッキングデバイスに渡されるようになります (パススルーモード)。

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 がキャッシュを処理することができないため、最低でも2つのパーティションが必要です (起動用のパーティションと bcache のバッキングデバイス用のパーティション)。UEFI を使っている場合、EFI System Partition (ESP) も必要です。例:

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" フックを追加。

設定

設定できるオプションは多数存在します (キャッシュモードやキャッシュの消去間隔、シーケンシャル書き込みのヒューリスティックなど)。/sys のファイルに書き込むことで設定します。詳しくは bcache のユーザードキュメント を見てください。

キャッシュモードを切り替えるには /sys/block/bcache[0-9]/bcache/cache_mode に 'writethrough', 'writeback', 'writearound', 'none' のどれかを書き込みます。

/sys の変更は一時的なものであり、再起動すると元に戻ってしまうので注意してください。起動時にカスタム設定を行うには /etc/tmpfile.d.conf ファイルを作成してください。例えば bcache0 のシーケンシャルカットオフを 1 MB にしてキャッシュモードを writeback に永続的に設定するには、/etc/tmpfile.d/my-bcache.conf ファイルを以下の内容で作成します:

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

ヒントとテクニック

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

パーティションの開始位置を移動しない限り、バッキングデバイスのサイズを変更することができます。詳しくは メーリングリスト で説明されています。以下の例では bcache0 上で直接 btrfs ボリュームを使用しています。LVM コンテナや他のファイルシステムの場合、手順は変わります。

拡大

以下の例では、ファイルシステムを 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

縮小

以下の例ではファイルシステムを 4GB まで縮小します。

1. writeback キャッシュを無効化 (writethrough キャッシュに切り替え) してディスクが消去されるまで待機する:

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

状態が "clean" になるまで待ってください。しばらく時間がかかります。

ノート: 以下のコマンドを使って強制的にキャッシュを消去することを推奨します:
# echo 0 > /sys/block/bcache0/bcache/writeback_percent

バッキングデバイスに存在するキャッシュデータが1分以内で消去されます。

消去できたら以下のコマンドで値を元に戻してください:

# 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 /

ext3/4 の場合は resize2fs を使用します (ただしパーティションがアンマウントされている必要があります):

$ 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 デバイスが存在しない

以下のようなエラーが 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

バッキングデバイスが "writeback" モードに設定されていると上記のエラーが発生することがあります (デフォルトは writearound です)。"writeback" モードでは、キャッシュデバイスが登録・アタッチされるまでは /dev/bcache0 デバイスが起動しません。登録は起動するたびに行う必要がありますが、アタッチは一度だけやれば十分です。

ブートを続行するには、以下の方法を試してみてください:

  • バッキングデバイスとキャッシュデバイスの両方を登録する:
# echo /dev/sda3 > /sys/fs/bcache/register
# echo /dev/sdb > /sys/fs/bcache/register

/dev/bcache0 デバイスが現れたら、exit と入力して起動を続行してください。ルートデバイスをマウントする前にデバイスが登録されるように initcpio を修正する必要があります。

ノート: "sh: echo: write error: Invalid argument" というエラーが表示される場合、デバイスが既に登録されているか bcache のバッキングデバイスあるいはキャッシュデバイスとして認識されていません。起動時に udev ルールを使っているのであれば、bcache のスーパーブロックが見つけられないかデバイスの登録が試行されます。
  • キャッシュデバイスをバッキングデバイスに再アタッチ:

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/ が存在しない

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

参照