Michael Shriver Senior Computer Specialist - College of the Environment

Putting /var/log on a ramdisk in NetBSD

Objective

Log files, stored in /var/log are written to frequently. Frequent disk writes contribute to early failure of SD-card based systems (SBCs like the Raspberry Pi). Linux has log2ram which can be used to mount the /var/log directory on a ram disk. The aim is to replicate the core functionality on NetBSD.

Configuration

Archive base /var/log contents

First, we will archive the base filestructing in /var/log, while truncating each log file to 0 bytes. This will be extracted into the tmpfs upon boot.

mkdir -p /usr/share/ramlog
for i in /var/log/*; do cat /dev/null > $i; done
tar -cvzf /usr/share/ramlog/log-image.tar.gz /var/log

Create an rc script

/etc/rc.conf:

no_swap=YES

/etc/fstab:

tmpfs		/var/log	tmpfs	rw,-m1777,-sram%2

/etc/rc.d/ramlog:

#!/bin/sh
#
# mount_mfs_fs: mount memory file system for /var
# by roby, 23 jun 2003 - adapted by Stefano - 01 Sep 2024

# PROVIDE: ramlog
# REQUIRE: root

. /etc/rc.subr

name="ramlog"
start_cmd="ramlog_start"
stop_cmd=":"

ramlog_start()
{
    # Check if the /var entry is present and uncommented in /etc/fstab
    if grep -q '^tmpfs[[:space:]]\+/var/log[[:space:]]\+tmpfs[[:space:]]\+rw,-m1777,-sram%25' /etc/fstab; then
        echo "Mounting memory file system: /var/log"

        # Mount the file system for /var/log
        mount /var/log

        # Extract the contents of the tar file into /var
        tar -xvzpf /usr/share/ramlog/log-image.tar.gz -C /

        echo "Mounting memory file systems: Done."
    else
        echo "The tmpfs entry for /var/log is not present or is commented out in /etc/fstab. Skipping mount and extraction."
    fi
    sleep 5
}

load_rc_config $name
run_rc_command "$1"

/etc/rc.d/mountcritlocal:

Change

# REQUIRE: fsck

To

# REQUIRE: fsck ramlog

Make /etc/rc.d/ramlog executable:

chmod a+x /etc/rc.d/ramlog

Tip: You can verify the startup order using the ‘rcorder’ command:

rcorder /etc/rc.d/* | less

Preventing the tmpfs disk from filling up

On NetBSD newsyslog is responsible for rotating logs. Below is an example for aprx’s log files.

/etc/newsyslog.conf:

# logfilename           [owner:group]   mode ngen size when flags [/pidfile] [sigtype]
...
# Aprx Logs
/var/log/aprx/aprx.log  root:aprx       660  7    100  *    Z
/var/log/aprx/aprx-rf.log root:aprx     660  7    250  *    Z

References