box.matto.nl
Enjoying Open Source Software

$HOME in ramdisk on FreeBSD

$HOME in ramdisk

By putting $HOME in ramdisk you get a clutter free home directory other people can only dream of

I have been using $HOME in ramdisk for many years. A very long time ago I started building my own diskless systems.

Most of the time these systems ran with $HOME in ramdisk.

Benefits

Your home directory typically will have a lot a small files, like the cache from vi, emacs, your browser et cetera. This will wear out your flash memory, so if you are running from USB memory sticks or SSD disks, mounting your home directory in memory will significant reduce the wear.

When you run $HOME in ramdisk, you are very much aware that everything that is created there will be gone after the next reboot. This helps to be aware that changes should either be committed to the versioning system (like CVS or Git) or be done on different machines. This helps to keep your data safe, as it will hone the habit of not having important files on your laptop :)

Because your home directory is back to its virgin state after each reboot, you don't have to worry about cookies and such stuff.

Clutter free home directory

Putting $HOME in ramdisk is great for keeping your home directory clutter-free! After every reboot you start with a nice clean home directory. This doesnot have to be an empty directory, see below.

mfs and tmpfs on FreeBSD

In FreeBSD, ramdisks are called mfs, memory file systems. Also there is tmpfs. I choose for tmpfs, because that is what I also use on my Debian boxes.

It is very straightforward to set up a tmpfs mounted directory on FreeBSD.

/etc/fstab

Here follow the relevant line in my /etc/fstab

tmpfs  /home/matto  tmpfs  rw,size=128m,mode=1700,failok  0  1

After the next boot you will have /home/matto in tmpfs.

I needed to add the switch "failok" because my FreeBSD system is running with rootfs on ZFS. If this is omitted, the bootprocess will stop prematurely and drop into a shell.

Populating the ramdisk

After rebooting the home directory is mounted in MFS, but it will be totally empty. This is usually not something you would want. Probably you want some configuration files like .vimrc, .ratpoisonrc, .zshrc in your home directory. Maybe also some symlinks to writable files or directories. And perhaps you would like to have your ssh-keys and gpg-keys here too.

There must be about a zillion different ways to populate your empty home directory. So, for fun, let just do this with the help of cpio.

First, we have to create a directory somewhere that we can use to build our setup. This can be an extra directory on the same system that will hold your ramdisk, for example /home/matto-template, but it can also be on a different machine. Here you create the files and directories you would like to have in your home directory.

When this is done, you can simply create a cpio archive from this.

  • cd into the directory
  • run the command: find . | cpio -ov > /tmp/homedir.cpio

This will create a archive in /tmp, so we can do some testing :)

You can omit the v in the switch (-ov), then it will not show which files are being archived.

Now go to a different place and unpack this tree:

cpio -idv < /tmp/homedir.cpio

You will see that everything is there, including directories and subdirectories.

Again you can omit the v in the switch (-idv), then it will not show which files are being restored.

When all is satisfying transport the cpio-archive to a place on your system from where you can unpack it, like /etc/homedir/homedir.cpio.

The final step is to put the unpack command in a rc.d file.

I created /usr/local/etc/rc.d/homedir for this, with the following contents.

#!/bin/sh

cd /home/matto
cpio -id < /etc/homedir/homedir.cpio -R matto:matto

This will unpack the cpio archive /etc/homedir/homedir.cpio in the MFS. Now we have a populated home directory. The switch -R matto:matto will set the ownership of the files to user matto and group matto.

If you want to make changes to your home directory you have to update the cpio archive. This can be done to unpack it somewhere, make the changes and build the cpio archive again.

mkdir -p /tmp/homedir
cd /tmp/homedir 
cpio -id < /etc/homemdir/homedir.cpio -R matto:matto

... make changes ...

cd /tmp/homedir 
find . | cpio -ov > /etc/homedir/homedir.cpio

Enjoy your clutter-free home directory in memory filesystem!

Tags:

⇽ FreeBSD 11 on Raspberry Pi Awkiawki: a light weight wiki as your personal wiki ⇾