Mount /usr in RAM (loading from HDD when starting, and saving to when shutting down)

I have the current scenario:

  • 16GB or RAM. For the most part, around 10~12GB unused

  • Almost never install/remove software, this is a mature system

  • Only reboot like once a week, so (re-)boot time is irrelevant

  • /usr is around 8GB

  • HDD is slow, and no budget to change that in the near future

  • Mounting /tmp as tmpfs in fstab did wonders for performance

And I just had this crazy idea:

Is it possible to, somehow, use my RAM to "store" /usr?

In that that, when starting up, it reads current /usr content from HDD and copy it to a tmpfs and mount that as /usr?

If possible, also flushing the (perhaps updated) contents back to HDD when shutting down? Or maybe allowing me to easily "temporarily switch back to HDD" when eventually needed to install/remove new software or updates?

Any approach is welcome, ready-to-use commands will be highly regarded.

Thanks!


This is possible, but pointless. The kernel keeps a cache of data from the disk in RAM. The data that you used most recently is kept in RAM. You will naturally end up with the parts of /usr that you use often in RAM, and the parts you don't use won't be taking up any RAM.

If you want better reaction time when you start an application, you can seed the cache. A file is loaded into memory the first time you use it, and remains there until the memory is reused for something else. You can force a file to be loaded:

cat /path/to/file >/dev/null

For example, to preload all executables and libraries into RAM:

cat /bin/* /lib/* /usr/bin/* /usr/lib/* >/dev/null

This can take a while to complete, so you should do it in the background. You can put the following command in /etc/rc.local:

ionice -c 3 cat /bin/* /lib/* /usr/bin/* /usr/lib/* >/dev/null &

To also load all libraries in subdirectories of /usr/lib* it could be useful to run find:

ionice -c 3 find /bin /usr/bin /usr/lib* -type f -exec ionice -c 3 cat '{}' ';' > /dev/null &

ionice -c 3 cat /bin/* /lib/* /usr/bin/* /usr/lib/* >/dev/null &

Won't run, because there are a lot of directories within the libs. try find:

ionice -c 3 find /bin /usr/bin /usr/lib* -type f -exec cat '{}' ';' > /dev/null &

Everything it produces goes to /dev/null so nothing will annoy you.

I think both ionice are required because find executes the commands in a own process. Please correct me if I'm wrong. (Looks like I were wrong.)

This line in rc.local should do the trick to the already chosen answer.

[edit] removed second "ionice" as suggested in the comment.