Advertisement
Illuvitar

tmp - bcache wiki edit

Nov 16th, 2019
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.13 KB | None | 0 0
  1. [[Category:File systems]]
  2. [[ja:Bcache]]
  3. [[zh-hans:Bcache]]
  4. {{Style|Some first-person comments, see [[Help:Style]].}}
  5.  
  6. [https://bcache.evilpiepirate.org/ Bcache] (block cache) allows one to use an SSD as a read/write cache (in writeback mode) or read cache (writethrough or writearound) for another blockdevice (generally a rotating HDD or array). This article will show how to install arch using Bcache as the root partition. For an intro to bcache itself, see [http://bcache.evilpiepirate.org/ the bcache homepage]. Be sure to read and reference [https://evilpiepirate.org/git/linux-bcache.git/tree/Documentation/bcache.txt the bcache manual]. Bcache is in the mainline kernel since 3.10. The kernel on the arch install disk includes the bcache module since 2013.08.01.
  7.  
  8. An alternative to Bcache is Facebook's [[Flashcache]] and its offspring [[EnhanceIO]].
  9.  
  10. {{Accuracy|Backwards incompatible changes to on-disk format for linux 3.18 appears to be false. bcache on-disk format changes have not yet landed upstream yet as of 3.19}}
  11.  
  12. Bcache needs the backing device to be formatted as a bcache block device. Alternatively, see the {{Converting an existing device}} section.
  13.  
  14. {{Warning|
  15. * Be sure you back up any important data first.
  16. * The bcache-dev branch is under heavy development. The on-disk format has undergone changes in 3.18 which are not backwards compatible with previous formats[http://www.spinics.net/lists/linux-bcache/msg02721.html]. Note: This only applies to users who compile-in bcache-dev. The version built-in to the upstream Linux kernel is unaffected[http://www.spinics.net/lists/linux-bcache/msg02724.html].
  17. * Bcache and [[btrfs]] could leave you with a corrupted filesystem. Please visit [https://www.hdevalence.ca/blog/2013-09-21-notes-on-my-archlinux-install this post] for more information. Btrfs wiki reports that it was fixed in kernels 3.19+ [https://btrfs.wiki.kernel.org/index.php/Gotchas#Historical_references].
  18. }}
  19.  
  20. == Setting up a bcache device on an existing system ==
  21.  
  22. {{Warning|make-bcache '''will not''' import an existing drive or partition – it will reformat it. If you want to use an existing data partition as backing device, see [[#Converting an existing device]].}}
  23.  
  24. 1. [[Install]] the {{AUR|bcache-tools}} package from [[AUR]].
  25.  
  26. 2. Create a backing device (This will typically be your mechanical drive). The backing device can be a whole device, a partition or any other standard block device. This will create /dev/bcache0
  27. # make-bcache -B /dev/sdx1
  28.  
  29. 3. Create a cache device (This will typically be your SSD). The cache device can be a whole device, a partition or any other standard block device
  30. # make-bcache -C /dev/sdy2
  31. In this example the default block and bucket sizes of 512B and 128kB are used. The block size should match the backing devices sector size which will usually be either 512 or 4k. The bucket size should match the erase block size of the caching device with the intent of reducing write amplification. For example, using a HDD with 4k sectors and an SSD with an erase block size of 2MB this command would look like
  32. # make-bcache --block 4k --bucket 2M -C /dev/sdy2
  33.  
  34. 4. Register the cache device against your backing device. To find its ''cache set UUID'', run {{ic|# bcache-super-show /dev/sdy2 | grep cset.uuid}} and then add it to the bcache device initially. Udev rules will take care of this on reboot and will only need to be done once.
  35. # echo '''cset.uuid''' > /sys/block/bcache0/bcache/attach
  36.  
  37. 5. Change your cache mode (if you want to cache writes as well as reads):
  38. # echo writeback > /sys/block/bcache0/bcache/cache_mode
  39.  
  40. 6. If you want to have this partition available during the initcpio (i.e. you require it at some point in the boot process) you need to add 'bcache' to your modules array in /etc/mkinitcpio.conf as well as adding the 'bcache' hook in your list between block and filesystems. You must then rebuild the initramfs image. This is typically done with
  41.  
  42. # mkinitcpio -p linux
  43.  
  44. === Bcache management ===
  45. 1. Check that everything has been correctly setup
  46.  
  47. # cat /sys/block/bcache0/bcache/state
  48.  
  49. The output can be:
  50. * '''no cache''': this means you have not attached a caching device to your backing bcache device
  51. * '''clean''': this means everything is ok. The cache is clean.
  52. * '''dirty''': this means everything is setup fine and that you have enabled ''writeback'' and that the cache is dirty.
  53. * '''inconsistent''': you are in trouble because the backing device is not in sync with the caching device
  54.  
  55. You can have a {{ic|/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)
  56.  
  57. 2. See what caching mode is in use
  58. {{bc|# cat /sys/block/bcache0/bcache/cache_mode
  59. [writethrough] writeback writearound none
  60. }}
  61. In the above example, the ''writethrough'' mode is enabled.
  62.  
  63. 3. Show info about a bcached device:
  64.  
  65. # bcache-super-show /dev/sdXY
  66.  
  67. 4. Stop the backing device:
  68.  
  69. # echo 1 > /sys/block/sdX/sdX[Y]/bcache/stop
  70.  
  71. 5. Detach a caching device:
  72.  
  73. # echo 1 > /sys/block/sdX/sdX[Y]/bcache/detach
  74.  
  75. 6. Safely remove the cache device
  76. # echo <cache-set-uuid> > /sys/block/bcache0/bcache/detach
  77.  
  78. 7. Release attached devices
  79. # echo 1 > /sys/fs/bcache/<cache-set-uuid>/stop
  80.  
  81. == Installation to a bcache device ==
  82.  
  83. 1. Boot on the install disk (2013.08.01 minimum).
  84.  
  85. 2. Install the {{AUR|bcache-tools}} package from [[AUR]].
  86.  
  87. 3. Partition your hdd
  88.  
  89. {{Note|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.}}
  90. 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.:
  91. 1 2048 22527 10.0 MiB EF00 EFI System
  92. 2 22528 432127 200.0 MiB 8300 arch_boot
  93. 3 432128 625142414 297.9 GiB 8300 bcache_backing
  94.  
  95. {{Note|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.}}
  96.  
  97. 4. Configure your HDD as a bcache backing device.
  98.  
  99. # make-bcache -B /dev/sda3
  100.  
  101. {{Note|
  102. * 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.
  103. * If all associated disks are partitioned at once as below bcache will automatically attach "-B backing stores" to the "-C ssd cache" and step 5 is unnecessary.
  104. }}
  105.  
  106. # make-bcache -B /dev/sd? /dev/sd? -C /dev/sd?
  107.  
  108. You now have a {{ic|/dev/bcache0}} device.
  109.  
  110. 5. Configure your SSD
  111.  
  112. Format the SSD as a caching device and link it to the backing device
  113.  
  114. # make-bcache -C /dev/sdb
  115. # echo /dev/sdb > /sys/fs/bcache/register
  116. # echo ''UUID__from_previous_command'' > /sys/block/bcache0/bcache/attach
  117.  
  118. {{Note|If the UUID is forgotten, it can be found with {{ic|ls /sys/fs/bcache/}} after the cache device has been registered.}}
  119.  
  120. 6. Format the bcache device. Use LVM or btrfs subvolumes if you want to divide up the {{ic|/dev/bcache0}} device how you like (ex for separate {{ic|/}}, {{ic|/home}}, {{ic|/var}}, etc):
  121.  
  122. # mkfs.btrfs /dev/bcache0
  123. # mount /dev/bcache0 /mnt/
  124. # btrfs subvolume create /mnt/root
  125. # btrfs subvolume create /mnt/home
  126. # umount /mnt
  127.  
  128. You can even setup LUKS on it if you want using e.g. cryptsetup. Referencing the bcache device in the 'cryptdevice' kernel option will work fine, for instance.
  129.  
  130. 7. Prepare the installation mount point:
  131.  
  132. # mkfs.ext4 /dev/sda2
  133. # mkfs.msdos /dev/sda1 (if your ESP is at least 500MB, use mkfs.vfat to make a FAT32 partition instead)
  134. # pacman -S arch-install-scripts
  135. # mount /dev/bcache0 -o subvol=root,compress=lzo /mnt/
  136. # mkdir /mnt/boot /mnt/home
  137. # mount /dev/bcache0 -o subvol=home,compress=lzo /mnt/home
  138. # mount /dev/sda2 /mnt/boot
  139. # mkdir /boot/efi
  140. # mount /dev/sda1 /mnt/boot/efi/
  141.  
  142. 8. Install the system as per the [[Installation guide#Connect to the internet|Installation Guide]] as normal except this:
  143.  
  144. Before you edit {{ic|/etc/mkinitcpio.conf}} and run {{ic|mkinitcpio -p linux}}:
  145.  
  146. * install {{AUR|bcache-tools}} package from the [[AUR]] on the new system.
  147. * Edit {{ic|/etc/mkinitcpio.conf}}:
  148. ** add the "bcache" module
  149. ** add the "bcache" hook between block and filesystem hooks
  150.  
  151. {{Note| Should you want to open the backing device from the installation media for any reason after a reboot you must register it manually. Make sure the bcache module is loaded and then echo the relevant devices to /sys/bcache/register. You should see whether this worked or not by using dmesg.
  152.  
  153. }}
  154.  
  155. == Accessing from the install disk ==
  156.  
  157. This is how to access a bcache partition from the install disk that was present before the install disk was booted. Boot the install disk and install {{AUR|bcache-tools}} from the AUR, just as in the previous section. Then, add the module to the kernel:
  158.  
  159. # modprobe bcache
  160.  
  161. Your device will not appear immediately at {{ic|/dev/bcache*}}. To force the kernel to find it, tell it to reread the partition table:
  162.  
  163. # partprobe
  164.  
  165. Now, {{ic|/dev/bcache*}} should be present, and you can carry on mounting, reformatting, etc. from here.
  166.  
  167. == Configuring ==
  168.  
  169. 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 {{ic|/sys}}. See the [https://evilpiepirate.org/git/linux-bcache.git/tree/Documentation/bcache.txt bcache user documentation].
  170.  
  171. Changing the cache mode is done by echoing one of 'writethrough', 'writeback', 'writearound' or 'none' to /sys/block/bcache[0-9]/bcache/cache_mode.
  172.  
  173. Note that some changes to /sys are temporary, and will revert back after a reboot (It seems that at least cache_mode does not need this workaround). To set custom configurations at boot create a .conf file in {{ic|/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 {{ic|/etc/tmpfile.d/my-bcache.conf}} with the contents
  174.  
  175. w /sys/block/bcache0/bcache/sequential_cutoff - - - - 1M
  176. w /sys/block/bcache0/bcache/cache_mode - - - - writeback
  177.  
  178. == Advanced operations ==
  179.  
  180. === Resize backing device ===
  181.  
  182. It is possible to resize the backing device so long as you do not move the partition start. This process is described on [http://comments.gmane.org/gmane.linux.kernel.bcache.devel/249 the mailing list]. Here is an example using btrfs volume directly on bcache0. For LVM containers or for other filesystems, procedure will differ.
  183.  
  184. ==== Example of growing ====
  185.  
  186. In this example, I grow the filesystem by 4GB.
  187.  
  188. 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.
  189.  
  190. {{Warning|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!!}}
  191.  
  192. 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
  193.  
  194. # btrfs filesystem resize max /
  195.  
  196. For ext3/4, that is:
  197.  
  198. # resize2fs /dev/bcache0
  199.  
  200. ==== Example of shrinking ====
  201.  
  202. In this example, I shrink the filesystem by 4GB.
  203.  
  204. 1. Disable writeback cache (switch to writethrough cache) and wait for the disk to flush.
  205.  
  206. # echo writethrough > /sys/block/bcache0/bcache/cache_mode
  207. $ watch cat /sys/block/bcache0/bcache/state
  208.  
  209. wait until state reports "clean". This might take a while.
  210.  
  211. ====== Force flush of cache to backing device ======
  212. I suggest to use
  213.  
  214. # echo 0 > /sys/block/bcache0/bcache/writeback_percent
  215.  
  216. This will flush the dirty data of the cache to the backing device in less a minute.
  217.  
  218. Revert back the value after with
  219.  
  220. # echo 10 > /sys/block/bcache0/bcache/writeback_percent
  221.  
  222. 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:
  223.  
  224. # btrfs filesystem resize -5G /
  225.  
  226. For ext3/4 you can use ''resize2fs'', but only if the partition is unmounted
  227.  
  228. $ df -h /home
  229. /dev/bcache0 290G 20G 270G 1% /home
  230. # umount /home
  231. # resize2fs /dev/bcache0 283G
  232.  
  233. 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.
  234.  
  235. {{Warning|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!!}}
  236.  
  237. 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:
  238.  
  239. # btrfs filesystem resize max /
  240.  
  241. For ext3/4, that is:
  242.  
  243. # resize2fs /dev/bcache0
  244.  
  245. 5. Re-enable writeback cache if you want that enabled:
  246.  
  247. # echo writeback > /sys/block/bcache0/bcache/cache_mode
  248.  
  249. {{Note|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}}
  250.  
  251.  
  252. == Converting an existing device ==
  253.  
  254. The [https://github.com/g2p/blocks g2p/blocks conversion tool]'s last commit was in December 2014, and various issues prevent it from working.
  255.  
  256. For an in-place conversion of an LVM LV, see https://unix.stackexchange.com/questions/552335/convert-existing-lvm-lv-to-bcache-backing-device this StackExchange answer.
  257.  
  258. [[[ I don't know how to answer the question:
  259.  
  260. What is the output of "pacman -V|base32|head -1"
  261.  
  262. given:
  263.  
  264. head: invalid trailing option -- 1
  265. Try 'head --help' for more information.
  266.  
  267. Also, BIQC4LJNFYQCAIBAEAQCAIBAEAQCAIBAEAQCAICQMFRW2YLOEB3DKLRSFYYCALJANRUWEYLMOBWS is not accepted.
  268.  
  269. Please someone fix my link maunally.
  270. ]]]
  271.  
  272. == Troubleshooting ==
  273.  
  274. === /dev/bcache device does not exist on bootup ===
  275.  
  276. If you are sent to a busy box shell with an error:
  277.  
  278. {{bc|1=
  279. ERROR: Unable to find root device 'UUID=b6b2d82b-f87e-44d5-bbc5-c51dd7aace15'.
  280. You are being dropped to a recovery shell
  281. Type 'exit' to try and continue booting
  282. }}
  283.  
  284. 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.
  285.  
  286. To continue booting, try one of the following:
  287.  
  288. * Register both the backing device and the caching device
  289.  
  290. # echo /dev/sda3 > /sys/fs/bcache/register
  291. # echo /dev/sdb > /sys/fs/bcache/register
  292.  
  293. 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.
  294.  
  295. {{Note|
  296. * 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
  297. * 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 [http://bcache.evilpiepirate.org/#index6h1 bcache's wiki] for a possible explanation/resolution.
  298. }}
  299.  
  300. * Re-attach the cache to the backing device:
  301.  
  302. If the cache device was registered, a folder with the UUID of the cache should exist in {{ic|/sys/fs/bcache}}. Use that UUID when following the example below:
  303.  
  304. # ls /sys/fs/bcache/
  305. b6b2d82b-f87e-44d5-bbc5-c51dd7aace15 register register_quiet
  306. # echo b6b2d82b-f87e-44d5-bbc5-c51dd7aace15 > /sys/block/sda/sda3/bcache/attach
  307.  
  308. If the {{ic|/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.
  309.  
  310. {{Note|An error of {{ic|sh: echo: write error: Invalid argument}} means the device was already attached. An error of {{ic|sh: echo: write error: No such file or directory}} means the UUID is not a valid cache (make sure you typed it correctly).}}
  311.  
  312. * 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.
  313.  
  314. # cat /sys/block/sda/sda3/bcache/dirty_data
  315. -3.9M
  316.  
  317. 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.
  318.  
  319. # cat /sys/block/sda/sda3/bcache/running
  320. 0
  321. # echo 1 > /sys/block/sda/sda3/bcache/running
  322.  
  323. The {{ic|/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 {{ic|/dev/bcache0}} would also be wise. See the [http://atlas.evilpiepirate.org/git/linux-bcache.git/tree/Documentation/bcache.txt?h=bcache bcache user documentation].
  324.  
  325. {{Warning|Only invalidate the cache if one of the two options above did not work.}}
  326.  
  327. === /sys/fs/bcache/ does not exist ===
  328.  
  329. The kernel you booted is not bcache enabled, or you the bcache [[Kernel_module#Manual_module_handling|module is not loaded]]
  330.  
  331. == See also ==
  332.  
  333. * [http://bcache.evilpiepirate.org Bcache Homepage]
  334. * [https://www.kernel.org/doc/Documentation/bcache.txt Bcache Manual]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement