Persistency management of memory based filesystem on OpenBSD

Comment on Mastodon

Introduction

For saving my SSD and also speeding up my system, I store some cache files into memory using the mfs filesystem on OpenBSD. But that would be nice to save the content upon shutdown and restore it at start, wouldn't it?

I found that storing the web browser cache in a memory filesystem drastically improve its responsiveness, but it's hard to make measurements of it.

Let's do that with a simple rc.d script.

Configuration

First, I use a mfs filesystem for my Firefox cache, here is the line in /etc/fstab

/dev/sd3b	   /home/solene/.cache/mozilla mfs rw,-s400M,noatime,nosuid,nodev 1 0

This mean I have a 400 MB partition using system memory, it's super fast but limited. tmpfs is disabled in the default kernel because it may have issues and is not well enough maintained, so I stick with mfs which is available out of the box. (tmpfs is faster and only use memory when storing file, while mfs reserves the memory chunk at first).

The script

We will write /etc/rc.d/persistency with the following content, this is a simple script that will store as a tgz file under /var/persistency every mfs mountpoint found in /etc/fstab when it receives the "stop" command. It will also restore the files at the right place when receiving the "start" command.

#!/bin/ksh

STORAGE=/var/persistency/

if [[ "$1" == "start" ]]
then
    install -d -m 700 $STORAGE
    for mountpoint in $(awk '/ mfs / { print $2 }' /etc/fstab)
    do
        tar_name="$(echo ${mountpoint#/} | sed 's,/,_,g').tgz"
        tar_path="${STORAGE}/${tar_name}"
        test -f ${tar_path}
        if [ $? -eq 0 ]
        then
            cd $mountpoint
            if [ $? -eq 0 ]
            then
                tar xzfp ${tar_path} && rm ${tar_path}
            fi
        fi
    done
fi

if [[ "$1" == "stop" ]]
then
    install -d -m 700 $STORAGE
    for mountpoint in $(awk '/ mfs / { print $2 }' /etc/fstab)
    do
        tar_name="$(echo ${mountpoint#/} | sed 's,/,_,g').tgz"
        cd $mountpoint
        if [ $? -eq 0 ]
        then
            tar czf ${STORAGE}/${tar_name} .
        fi
    done
fi

All we need to do now is to use "rcctl enable persistency" so it will be run with start/stop at boot/shutdown times.

Conclusion

Now I'll be able to carry my Firefox cache across reboots while keeping it in mfs.