From 93c40a5e93f0d71bdf89da81655142c76975ea3d Mon Sep 17 00:00:00 2001 From: Franco Fichtner Date: Tue, 19 Sep 2017 08:20:32 +0200 Subject: [PATCH] rc: refactor /var handling, get MFS dirs from rc vars This works regardless of enabled state, as long as the plugin is installed the directories are transitioned (makes sense for temporary disabling of services). PR: https://forum.opnsense.org/index.php?topic=5987.0 --- plist | 1 + src/etc/rc | 147 +++++++++++------------------------------ src/etc/rc.subr.d/var | 150 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 190 insertions(+), 108 deletions(-) create mode 100755 src/etc/rc.subr.d/var diff --git a/plist b/plist index bb1faf927..91822098a 100644 --- a/plist +++ b/plist @@ -126,6 +126,7 @@ /usr/local/etc/rc.restart_webgui /usr/local/etc/rc.shutdown /usr/local/etc/rc.sshd +/usr/local/etc/rc.subr.d/var /usr/local/etc/rc.swapon /usr/local/etc/rc.syshook /usr/local/etc/rc.syshook.d/05-beep.stop diff --git a/src/etc/rc b/src/etc/rc index 1f138ea62..970faf141 100755 --- a/src/etc/rc +++ b/src/etc/rc @@ -4,6 +4,29 @@ # Copyright (c) 2004-2010 Scott Ullrich # Copyright (c) 2003-2004 Manuel Kasper # All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +# SUCH DAMAGE. stty status '^T' 2> /dev/null @@ -102,120 +125,28 @@ fi # run the config importer during early startup /usr/local/etc/rc.importer -b -# Enable console output if its muted. +# enable console output if it was muted /sbin/conscontrol mute off > /dev/null -setup_mfs_link() -{ - ROOT=${1} - MFS=${2} - NAME=${3} - - # Create dummy directory to for MFS-bound - # directories that require a persistent - # storage underneath to run. - - if [ ! -d "${ROOT}${MFS}/${NAME}" ]; then - mkdir -p "${ROOT}${MFS}" "${MFS}/${NAME}" - mv "${MFS}/${NAME}" "${ROOT}${MFS}" - # create a symlink underneath as well - ln -s "${ROOT}${MFS}/${NAME}" "${MFS}/${NAME}" - fi -} - -install_mfs_link() -{ - ROOT=${1} - MFS=${2} - NAME=${3} - - # Redirect persistent, but MFS-bound - # directory after tmpfs mount. - - mkdir -p "${MFS}" - ln -s "${ROOT}${MFS}/${NAME}" "${MFS}/${NAME}" -} - -remove_mfs_link() -{ - ROOT=${1} - MFS=${2} - NAME=${3} - - # Persistent copies of MFS-bound directories - # still there must be moved back into place. - - if [ -d "${ROOT}${MFS}/${NAME}" ]; then - mkdir -p "${MFS}" - # reverse the recovery symlink before - # moving back the original database - rm -f "${MFS}/${NAME}" - mv "${ROOT}${MFS}/${NAME}" "${MFS}/" - fi - - # ensure directory always exist - mkdir -p "${MFS}/${NAME}" -} - -# pre-17.1 compat: use_mfs_tmpvar matches both patterns -USE_MFS_VAR=`/usr/bin/grep -c 'use_mfs_.*var[^_]' /conf/config.xml` -USE_MFS_TMP=`/usr/bin/grep -c 'use_mfs_tmp[^_]' /conf/config.xml` - -if [ ${USE_MFS_TMP} -ne 0 ]; then - mount -t tmpfs -o mode=01777 tmpfs /tmp -fi - -if [ ${USE_MFS_VAR} -ne 0 ]; then - echo -n "Setting up memory disks..." - - setup_mfs_link /root /var/cache opnsense-update - setup_mfs_link /root /var/log bsdinstaller - setup_mfs_link /root /var/cache pkg - setup_mfs_link /root /var/db pkg - setup_mfs_link /root /var crash - - mount -t tmpfs tmpfs /var - - install_mfs_link /root /var/cache opnsense-update - install_mfs_link /root /var/log bsdinstaller - install_mfs_link /root /var/cache pkg - install_mfs_link /root /var/db pkg - install_mfs_link /root /var crash - - echo "done." -else - remove_mfs_link /root /var/cache opnsense-update - remove_mfs_link /root /var/log bsdinstaller - remove_mfs_link /root /var/cache pkg - remove_mfs_link /root /var/db pkg - remove_mfs_link /root /var crash -fi - -# ensure default directories in /var -mtree -deiU -f /etc/mtree/BSD.var.dist -p /var > /dev/null - -# old config files are stored in this place -mkdir -p /var/etc - -# clear nameserver, searchdomain and IP cache files -rm -f /var/db/*_ip /var/db/*_ipv6 /var/db/*_cacheip /var/db/*_cacheipv6 -rm -f /var/etc/nameserver_* /var/etc/searchdomain_* - -# Clear all files in this directory to prevent stale state of -# services. At one point this also helped to prevent shutdown(8) -# from dropping "nologin" into the directory, preventing login on -# the next boot. -rm -rf /var/run/* - -# set up and recover a crash dump before activating swap -/usr/local/etc/rc.crashdump -/usr/local/etc/rc.swapon +# all sorts of maintenance tasks for /var +/usr/local/etc/rc.subr.d/var # write /var/run/dmesg.boot /etc/rc.d/dmesg onestart -rm -rf /tmp/* -chmod 1777 /tmp +# XXX pre-17.1 compat: avoid matching use_mfs_tmpvar +USE_MFS_TMP=`/usr/bin/grep -c 'use_mfs_tmp[^_]' /conf/config.xml` + +if [ ${USE_MFS_TMP} -ne 0 ]; then + mount -t tmpfs -o mode=01777 tmpfs /tmp +else + rm -rf /tmp/* + chmod 1777 /tmp +fi + +# set up and recover a crash dump before activating swap +/usr/local/etc/rc.crashdump +/usr/local/etc/rc.swapon echo -n "." /sbin/ldconfig -elf /usr/lib /usr/local/lib /lib diff --git a/src/etc/rc.subr.d/var b/src/etc/rc.subr.d/var new file mode 100755 index 000000000..1dd0b7203 --- /dev/null +++ b/src/etc/rc.subr.d/var @@ -0,0 +1,150 @@ +#!/bin/sh + +# Copyright (c) 2014-2017 Franco Fichtner +# Copyright (c) 2004-2010 Scott Ullrich +# Copyright (c) 2003-2004 Manuel Kasper +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +# SUCH DAMAGE. + +ROOTDIR="/root" + +setup_mfs_link() +{ + ROOT=${ROOTDIR} + MFS=$(dirname ${1}) + NAME=$(basename ${1}) + + # Create dummy directory to for MFS-bound + # directories that require a persistent + # storage underneath to run. + + if [ ! -d "${ROOT}${MFS}/${NAME}" ]; then + mkdir -p "${ROOT}${MFS}" "${MFS}/${NAME}" + mv "${MFS}/${NAME}" "${ROOT}${MFS}" + # create a symlink underneath as well + ln -s "${ROOT}${MFS}/${NAME}" "${MFS}/${NAME}" + fi +} + +install_mfs_link() +{ + ROOT=${ROOTDIR} + MFS=$(dirname ${1}) + NAME=$(basename ${1}) + + # Redirect persistent, but MFS-bound + # directory after tmpfs mount. + + mkdir -p "${MFS}" + ln -s "${ROOT}${MFS}/${NAME}" "${MFS}/${NAME}" +} + +remove_mfs_link() +{ + ROOT=${ROOTDIR} + MFS=$(dirname ${1}) + NAME=$(basename ${1}) + + # Persistent copies of MFS-bound directories + # still there must be moved back into place. + + if [ -d "${ROOT}${MFS}/${NAME}" ]; then + mkdir -p "${MFS}" + # reverse the recovery symlink before + # moving back the original database + rm -f "${MFS}/${NAME}" + mv "${ROOT}${MFS}/${NAME}" "${MFS}/" + fi + + # ensure directory always exist + mkdir -p "${MFS}/${NAME}" +} + +# check which directories we need +if [ -f /etc/rc.conf ]; then + . /etc/rc.conf +fi +if [ -f /etc/rc.conf.local ]; then + . /etc/rc.conf.local +fi +for RC_CONF in $(find /etc/rc.conf.d -type f); do + . ${RC_CONF} +done + +RC_FILES="$(ls /etc/rc.d/[a-z]* /usr/local/etc/rc.d/[a-z]* 2> /dev/null || true)" +MFS_DIRS=" +/var/cache/opnsense-update +/var/cache/pkg +/var/crash +/var/db/pkg +/var/log/bsdinstaller +" + +for RC_FILE in ${RC_FILES}; do + eval "$(grep "^name[[:blank:]]*=" ${RC_FILE})" + VAR_MFS="${name}_var_mfs" + eval "VAR_DIRS=\$${VAR_MFS}" + for VAR_DIR in ${VAR_DIRS}; do + MFS_DIRS="${MFS_DIRS} ${VAR_DIR}" + done +done + +# XXX pre-17.1 compat: use_mfs_tmpvar matches both patterns +USE_MFS_VAR=`/usr/bin/grep -c 'use_mfs_.*var[^_]' /conf/config.xml` + +if [ ${USE_MFS_VAR} -ne 0 ]; then + echo -n "Setting up memory disks..." + + for DIR in ${MFS_DIRS}; do + setup_mfs_link ${DIR} + done + + mount -t tmpfs tmpfs /var + + for DIR in ${MFS_DIRS}; do + install_mfs_link ${DIR} + done + + echo "done." +else + for DIR in ${MFS_DIRS}; do + remove_mfs_link ${DIR} + done +fi + +# ensure default directories in /var +mtree -deiU -f /etc/mtree/BSD.var.dist -p /var > /dev/null + +# old config files are stored in this place +mkdir -p /var/etc + +# clear nameserver, searchdomain and IP cache files +rm -f /var/db/*_ip /var/db/*_ipv6 /var/db/*_cacheip /var/db/*_cacheipv6 +rm -f /var/etc/nameserver_* /var/etc/searchdomain_* + +# Clear all files in this directory to prevent stale state of +# services. At one point this also helped to prevent shutdown(8) +# from dropping "nologin" into the directory, preventing login on +# the next boot. +rm -rf /var/run/*