libexec/rc: Add var_run rc script

Users with a tmpfs /var/run will lose the directory tree state of
/var/run at reboot. This rc script will optionally (by default)
capture the state of the directory structure in /var/run prior to
shutdown and recreate it at system boot.

Alternatively a user can save the state of the /var/run directories
manually using service var_run save and disable the autosaving of
/var/run state using the var_run_autosave variable, for those
paranoid SSD users.

PR:			259585, 259699
Reported by:		freebsd@walstatt-de.de,
Reviewed by:		philip, gbe (previous version)
MFC after:		1 week
Differential Revision:	https://reviews.freebsd.org/D36386
This commit is contained in:
Cy Schubert 2022-08-28 05:48:25 -07:00
parent 9503043f6e
commit 27b9777c28
5 changed files with 84 additions and 0 deletions

View File

@ -46,6 +46,8 @@
..
ipf mode=0700 tags=package=ipf
..
mtree
..
ntp uname=ntpd gname=ntpd
..
pkg

View File

@ -61,6 +61,12 @@ varmfs_flags="-S" # Extra mount options for the mfs /var
mfs_type="auto" # "md", "tmpfs", "auto" to prefer tmpfs with md as fallback
populate_var="AUTO" # Set to YES to always (re)populate /var, NO to never
cleanvar_enable="YES" # Clean the /var directory
var_run_enable="NO" # Save/restore /var/run structure at shutdown/reboot
var_run_autosave="NO" # Only restore /var/run structure at shutdown/reboot
# The user is expected to issue service var_run save to
# manually save the /var/run mtree
var_run_mtree="/var/db/mtree/BSD.var-run.mtree"
# Where to save /var/run mtree
local_startup="${_localbase}/etc/rc.d" # startup script dirs.
script_name_sep=" " # Change if your startup scripts' names contain spaces
rc_conf_files="/etc/rc.conf /etc/rc.conf.local"

View File

@ -111,6 +111,7 @@ CONFS= DAEMON \
ugidfw \
${_utx} \
var \
var_run \
watchdogd
.if ${MK_NIS} != "no"

47
libexec/rc/rc.d/var_run Executable file
View File

@ -0,0 +1,47 @@
#!/bin/sh
# PROVIDE: var_run
# REQUIRE: mountcritlocal
# BEFORE: cleanvar
. /etc/rc.subr
name=var_run
rcvar=var_run_enable
extra_commands="load save"
start_cmd="_var_run_start"
load_cmd="_var_run_load"
save_cmd="_var_run_save"
stop_cmd="_var_run_stop"
load_rc_config $name
# Set defaults
: ${var_run_enable:="NO"}
: ${var_run_mtree:="/var/db/mtree/BSD.var-run.mtree"}
: ${var_run_autosave:="YES"}
_var_run_load() {
test -f ${var_run_mtree} &&
mtree -U -i -q -f ${var_run_mtree} -p /var/run > /dev/null
}
_var_run_save() {
if [ ! -d $(dirname ${var_run_mtree}) ]; then
mkdir -p ${var_run_mtree}
fi
mtree -dcbj -p /var/run > ${var_run_mtree}
}
_var_run_start() {
df -ttmpfs /var/run > /dev/null 2>&1 &&
_var_run_load
}
_var_run_stop() {
df -ttmpfs /var/run > /dev/null 2>&1 &&
checkyesno var_run_autosave &&
_var_run_save
}
run_rc_command "$1"

View File

@ -463,6 +463,34 @@ is mounted on normal systems.
Clean the
.Pa /var
directory.
.It Va var_run_enable
.Pq Vt bool
Set to "YES" to enable saving of the
.Pa /var/run
directory strcucture into an mtree file at shutdown and the reload of the
.Pa /var/run
directory structure at boot.
.It Va var_run_autosave
.Pq Vt bool
In some cases it may be undesirable to save
.Pa /var/run
at shutdown.
When set to "NO"
.Pa /var/run
is loaded at reboot but not saved at shutdown. Typically in this scenario
a
.Pa service
.Pa var_run
.Pa save
would be performed to save a copy of the
.Pa /var/run
directory structure once, to be reload during all subsequent reboots.
.It Va var_run_mtree
.Pq Vt str
Where to save the
.Pa /var/run
mtree. The default location is
.Pa /var/db/mtree/BSD.var-run.mtree .
.It Va local_startup
.Pq Vt str
List of directories to search for startup script files.