https://wiki.archlinux.org/api.php?action=feedcontributions&user=Knobby&feedformat=atomArchWiki - User contributions [en]2024-03-28T17:03:10ZUser contributionsMediaWiki 1.41.0https://wiki.archlinux.org/index.php?title=Flashcache&diff=138408Flashcache2011-04-26T04:31:25Z<p>Knobby: flashcache is a module for creating caches of a disk on a (hopefully faster) disk such as a solid-state drive.</p>
<hr />
<div>[[Category:Getting and installing Arch (English)]]<br />
[[Category:File systems (English)]]<br />
[[Category:HOWTOs (English)]]<br />
<br />
==Introduction==<br />
<br />
Flashcache is a module originally written and released by Facebook(Mohan Srinivasan, Paul Saab and Vadim Tkachenko) in April of 2010. It is a kernel module that allows caching of a drive on another drive. This is most often used for caching a rotational drive on a smaller solid-state drive for performance reasons. This gives you the speed of an SSD and the size of a standard rotational drive for recently cached files. Facebook originally wrote the module to speed up database I/O, but it is easily extended to any I/O.<br />
<br />
{{Warning|Installation of flashcache at this point is *not* a simple task and should only be attempted by people comfortable with such things. Please read the instructions thoroughly before attempting to install this driver. It is very easy to get a non-booting machine if you are not careful.}}<br />
<br />
It is important to note that this driver is intended to speed up a slower drive with a faster one. It is not required that one of the drives is a solid-state drive. It could be useful in a situation with widely different speed drives such as a fast and small SCSI drive and a larger SATA drive or even a slower "green" drive and a super-fast 10k rpm drive.<br />
<br />
{{Warning|I have read that this module doesn't compile properly on a 32-bit machine, but I have not tested this.}}<br />
<br />
==Installation==<br />
<br />
{{Tip|Throughout the page, /dev/sda will be used to indicate the slow drive and /dev/sdb will used to indicate the fast drive. The two partitions sda3 and sda4 are being cached on sdb1 and sdb2 respectively. Be sure to change these examples to match your setup.}}<br />
<br />
====Getting the kernel module====<br />
<br />
{{Warning|You must have the full kernel source with device-mapper enabled or built as a module.}}<br />
<br />
At this time the kernel module is not included in the arch packages or AUR, so you must download the file directly from github at https://github.com/facebook/flashcache. Extract the image and compile the module/utils:<br />
# tar -xzvf facebook-flashcache-1.0-64-g085b7ba.tar.gz<br />
# cd facebook-flashcache-085b7ba<br />
# make KERNEL_TREE=<root of kernel source tree> (most likely in /usr/src)<br />
<br />
If all goes well you're ready to install the module(you need root access for this):<br />
# sudo make KERNEL_TREE=<root of kernel source tree> install<br />
<br />
From there I copied the executables to /usr/sbin for ease of use:<br />
# sudo cp src/utils/{flashcache_create,flashcache_load,flashcache_destroy} /usr/sbin<br />
<br />
====Preparing the fast drive====<br />
<br />
To prepare the fast drive for the caching required, we need to simply partition it based on our desired cache sizes. I wanted to cache a good deal of the root partition and some of my home partition, so I made my partitions 4 gigs and 116 gigs respectively. Take a look at your partitions and make an informed decision about the partition sizes you require.<br />
<br />
====Setting up the ramdisk====<br />
<br />
If you are setting up a cache that uses your root partition or you do not want to unmount your partitions while running, the best option is to change your ramdisk to support flashcache and do your administration with busybox. If you have unmounted filesystems that you are trying to cache, skip down to [[#Other file changes]].<br />
<br />
I used lvm as a base for my ramdisk changes, but it is very possible that there is a better way to do this. First, I created(be sure to update to match your system!)<br />
{{Filename|/lib/initcpio/hooks/flashcache}}<br />
<pre><br />
# vim:set ft=sh:<br />
run_hook ()<br />
{<br />
/sbin/modprobe -q dm-mod >/dev/null 2>&1<br />
if [ -e "/sys/class/misc/device-mapper" ]; then<br />
if [ ! -e "/dev/mapper/control" ]; then<br />
/bin/mknod "/dev/mapper/control" c $(cat /sys/class/misc/device-mapper/dev | sed 's|:| |')<br />
fi<br />
<br />
[ "${quiet}" = "y" ] && LVMQUIET=">/dev/null"<br />
<br />
msg "Activating cache volumes..."<br />
eval /usr/sbin/flashcache_load cached_part1 /dev/sdb1 /dev/sda3 $LMQUIET<br />
eval /usr/sbin/flashcache_load cached_part2 /dev/sdb2 /dev/sda4 $LMQUIET<br />
fi<br />
}<br />
</pre><br />
<br />
{{Filename|/lib/initcpio/install}}<br />
<pre><br />
# vim: set ft=sh:<br />
<br />
install ()<br />
{<br />
MODULES="dm-mod"<br />
BINARIES=""<br />
FILES=""<br />
SCRIPT="flashcache"<br />
<br />
add_dir "/dev/mapper"<br />
add_binary "/sbin/dmsetup"<br />
add_binary "/usr/sbin/flashcache_create"<br />
add_binary "/usr/sbin/flashcache_load"<br />
add_binary "/usr/sbin/flashcache_destroy"<br />
add_file "/lib/udev/rules.d/10-dm.rules"<br />
add_file "/lib/udev/rules.d/13-dm-disk.rules"<br />
add_file "/lib/udev/rules.d/95-dm-notify.rules"<br />
add_file "/lib/udev/rules.d/11-dm-lvm.rules"<br />
}<br />
<br />
help ()<br />
{<br />
cat<<HELPEOF<br />
This hook loads the necessary modules for a flash drive as a cache device for your root device.<br />
HELPEOF<br />
}<br />
</pre><br />
<br />
Note: I left in the files {{Filename|dm-disk.rules}}, {{Filename|dm-notify.rules}} and {{Filename|dm-lvm.rules}}, but I do not believe these are required.<br />
<br />
Update your {{Filename|/etc/mkinitcpio.conf}}:<br />
<br />
Add flashcache to your modules<br />
# MODULES="flashcache"<br />
<br />
Add flashcache to your hooks(be sure to add it before filesystems)<br />
# HOOKS="... flashcache ..."<br />
<br />
{{Tip|You might need to add usbinput to your hooks as well if you are using a usb keyboard.}}<br />
<br />
Now rebuild your ramdisk image:<br />
# mkinitcpio -g /boot/<your ramdisk filename>.img<br />
<br />
You might want to make this a different ramdisk image than your normal one and create a new entry in your grub config to use this ramdisk.<br />
<br />
====Other file changes====<br />
The new partitions will show up in {{Filename|/dev/mapper}} and you need to mount these partitions instead of the original {{Filename|/dev/sd*}} partitons. You need to edit grub and your fstab to mount these new partitions. In this example, the root partition is {{Filename|/dev/mapper/cached_part1}} and the home directory is {{Filename|/dev/mapper/cached_part2}}.<br />
<br />
{{Filename|/boot/grub/menu.lst}}<br />
<pre><br />
# (0) Arch Linux<br />
title Arch Linux(flashcache)<br />
root (hd0,0)<br />
kernel /vmlinuz26 root=/dev/mapper/cached_part1 ro 5<br />
initrd /kernel-2.6.38.4.img<br />
</pre><br />
<br />
{{Filename|/etc/fstab}}<br />
<pre><br />
...<br />
/dev/mapper/cached_part1 / ext3 defaults 0 1<br />
/dev/mapper/cached_part2 /home ext3 defaults,user_xattr 0 1<br />
...<br />
</pre><br />
<br />
If you're caching your root partition, reboot and press e in grub to edit the kernel command line options. Select the line with the kernel options and press e again. Append 'break=y' without quotes to the end and press enter. Press b to boot. This tells the ramdisk image to stop after loading modules and drop into a shell to allow us to do some work.<br />
<br />
====Creating flashcache====<br />
# flashcache_create cached_part1 /dev/sdb1 /dev/sda3<br />
# flashcache_create cached_part2 /dev/sdb2 /dev/sda4<br />
<br />
Now reboot(make sure you use the flashcache ramdisk) and verify the mounted partitions are the newly created ones.<br />
<br />
=Tweaks=<br />
There are a lot of options for controlling the cache, check the system administrators guide in the documentation for flashcache for all of them. I added the following changes to {{Filename|/etc/sysctl.conf}}:<br />
<br />
<pre><br />
#####################<br />
# flashcache settings<br />
#####################<br />
<br />
# disable writing dirty cache data at shutdown<br />
dev.flashcache.fast_remove = 1<br />
<br />
# change the reclaim policy to LRU from FIFO <br />
dev.flashcache.reclaim_policy = 1<br />
<br />
# don't write "stale" data to disk until evicted due to lack of space<br />
dev.flashcache.fallow_delay = 0<br />
</pre><br />
=Troubleshooting=<br />
* If you get an error trying to create the cache 'device-mapper: reload ioctl failed: Invalid argument', you could be trying to create a cache of a mounted filesystem.<br />
* If boot fails, an easy way to check on the cache is to edit your kernel command line in grub to add break=y and use the ramdisk's shell to poke around.<br />
* /proc/flashcache_stats has interesting information and can tell you if the cache is properly working.<br />
<br />
=More resources=<br />
* Original announcement - http://www.facebook.com/note.php?note_id=388112370932<br />
* Github source - https://github.com/facebook/flashcache</div>Knobby