Move Chromium's Cache to MFS Memory Disk on FreeBSD

It is possible to use FreeBSD’s MFS Memory Disk to speed up access times of cached web objects.

From md(4):

The md driver provides support for four kinds of memory backed virtual disks:

swap:
Backing store is allocated from buffer memory. Pages get pushed out to the swap when the system is under memory pressure, otherwise they stay in the operating memory. Using swap backing is generally preferable over malloc backing.

Chromium’s cache is at $HOME/.cache/chromium, and so, my /etc/fstab contains:

1
2
3
4

# 256M mfs for google chrome
/dev/md0 /home/ootput/.cache/chromium mfs rw,-s256m,-wootput:ootput,noauto 2 0

From mount_mfs(8):

-s256m specifies a memory reservation of 256 M;

-wootput:ootput hands ownership of the mountpoint from root to the user; and

noauto prevents the system from failing to boot properly if the mountpoint does not (yet) exist.

The following additions to /etc/rc.conf were also made:

1
2
3
4
5

mdconfig_md0="-t swap -s 256m"
mdconfig_md0_owner="ootput:ootput"
mdconfig_md0_perms="1777"

which lists arguments to mdconfig(8) for device md0.

At mininum the -t type must be specified and either a -s size for malloc or swap backed md(4) devices or a -f file for vnode backed md(4) devices.

The redundancies found in fstab and rc.conf allows automatic mounting at boot, or manual mounting after boot.

To test the changes made, as root:

# /etc/rc.d/mdconfig restart

On to the $USER/bin/pack_chrome executable script that will both mount the md device, and/or sync contents from the cache at RAM to a backup cache on the HDD:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

#!/usr/local/bin/bash

CACHE="$HOME/.cache/chromium"
BACKUP="$CACHE.bak"

for DIR in $CACHE $BACKUP; do
if [ ! -d "${DIR}" ]; then
mkdir -p ${DIR}
fi
done

if test -z "$(/sbin/mount | grep -F "$CACHE" )"; then
sudo /sbin/mount "$CACHE"
fi

if test -f "$CACHE/.synced"; then
rsync --exclude '.synced' --exclude '.snap' --delete -avrPx
"$CACHE/" "$BACKUP"
else
rsync --delete -avrPx "$BACKUP/" "$CACHE"
touch "$CACHE/.synced"
fi

The script requires that the $USER is in sudoers(5), and is able to use the /sbin/mount command.

Finally, after $ crontab -e:

1
2
3

*/30 * * * * $HOME/bin/pack_chrome >/dev/null 2>&1

.. and the script gets called every half-hour by cron to automate the syncing process. This also requires that $USER is specifically allowed in /var/cron/allow.