About me: My name is Solène Rapenne, pronouns she/her. I like learning and sharing knowledge. Hobbies: '(BSD OpenBSD Qubes OS Lisp cmdline gaming security QubesOS internet-stuff). I love percent and lambda characters. Qubes OS core team member, former OpenBSD developer solene@. No AI is involved in this blog.

Contact me: solene at dataswamp dot org or @solene@bsd.network (mastodon).

I'm a freelance OpenBSD, FreeBSD, Linux and Qubes OS consultant, this includes DevOps, DevSecOps, technical writing or documentation work. If you enjoy this blog, you can sponsor my open source work financially so I can write this blog and contribute to Free Software as my daily job.

Persistency management of memory based filesystem on OpenBSD

Written by Solène, on 15 December 2021.
Tags: #openbsd #performance #nocloud

Comments on Fediverse/Mastodon

1. 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.

2. 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).

3. 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.

4. Conclusion §

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

  • Beware! A situation like using a mfs for a cache can lead to getting a full filesystem because it's never emptied, I think I'll run into the mfs filesystem full after a week or two.
  • Beware 2! If the system has a crash, mfs data will be lost. The script remove the archives at boot after using it, you could change the script to remove them before creating the newer archive upon stop, so at least you could recover "latest known version", but it's absolutely not a backup. mfs data are volatile and I just want to save it softly for performance purpose.