Title: Persistency management of memory based filesystem on OpenBSD | |
Author: Solène | |
Date: 15 December 2021 | |
Tags: openbsd performance nocloud | |
Description: This explains how to automatically backup and restore mfs | |
based filesystem on OpenBSD upon boot and shutdown. | |
# 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 | |
```fstab entry | |
/dev/sd3b /home/solene/.cache/mozilla mfs rw,-s400M,noatime,nosuid,no… | |
``` | |
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. | |
```shell script | |
#!/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. | |
* 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. |