xref: /freebsd/libexec/rc/rc.d/var (revision 0696600c41600d80bcd993bfd8e675d0ae6951fe)
1*0696600cSBjoern A. Zeeb#!/bin/sh
2*0696600cSBjoern A. Zeeb#
3*0696600cSBjoern A. Zeeb# Copyright (c) 1999  Matt Dillon
4*0696600cSBjoern A. Zeeb# All rights reserved.
5*0696600cSBjoern A. Zeeb#
6*0696600cSBjoern A. Zeeb# Redistribution and use in source and binary forms, with or without
7*0696600cSBjoern A. Zeeb# modification, are permitted provided that the following conditions
8*0696600cSBjoern A. Zeeb# are met:
9*0696600cSBjoern A. Zeeb# 1. Redistributions of source code must retain the above copyright
10*0696600cSBjoern A. Zeeb#    notice, this list of conditions and the following disclaimer.
11*0696600cSBjoern A. Zeeb# 2. Redistributions in binary form must reproduce the above copyright
12*0696600cSBjoern A. Zeeb#    notice, this list of conditions and the following disclaimer in the
13*0696600cSBjoern A. Zeeb#    documentation and/or other materials provided with the distribution.
14*0696600cSBjoern A. Zeeb#
15*0696600cSBjoern A. Zeeb# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16*0696600cSBjoern A. Zeeb# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17*0696600cSBjoern A. Zeeb# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18*0696600cSBjoern A. Zeeb# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19*0696600cSBjoern A. Zeeb# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20*0696600cSBjoern A. Zeeb# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21*0696600cSBjoern A. Zeeb# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22*0696600cSBjoern A. Zeeb# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23*0696600cSBjoern A. Zeeb# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24*0696600cSBjoern A. Zeeb# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25*0696600cSBjoern A. Zeeb# SUCH DAMAGE.
26*0696600cSBjoern A. Zeeb#
27*0696600cSBjoern A. Zeeb# $FreeBSD$
28*0696600cSBjoern A. Zeeb#
29*0696600cSBjoern A. Zeeb
30*0696600cSBjoern A. Zeeb# PROVIDE: var
31*0696600cSBjoern A. Zeeb# REQUIRE: mountcritlocal
32*0696600cSBjoern A. Zeeb
33*0696600cSBjoern A. Zeeb# NFS /var is not supported, unless NFS /var is part of diskless NFS /
34*0696600cSBjoern A. Zeeb
35*0696600cSBjoern A. Zeeb. /etc/rc.subr
36*0696600cSBjoern A. Zeeb
37*0696600cSBjoern A. Zeebname="var"
38*0696600cSBjoern A. Zeebdesc="Populate /var directory"
39*0696600cSBjoern A. Zeebstop_cmd=':'
40*0696600cSBjoern A. Zeeb
41*0696600cSBjoern A. Zeebload_rc_config $name
42*0696600cSBjoern A. Zeeb
43*0696600cSBjoern A. Zeebpopulate_var()
44*0696600cSBjoern A. Zeeb{
45*0696600cSBjoern A. Zeeb	/usr/sbin/mtree -deiU -f /etc/mtree/BSD.var.dist -p /var > /dev/null
46*0696600cSBjoern A. Zeeb	case ${sendmail_enable} in
47*0696600cSBjoern A. Zeeb	[Nn][Oo][Nn][Ee])
48*0696600cSBjoern A. Zeeb		;;
49*0696600cSBjoern A. Zeeb	*)
50*0696600cSBjoern A. Zeeb		/usr/sbin/mtree -deiU -f /etc/mtree/BSD.sendmail.dist -p / > /dev/null
51*0696600cSBjoern A. Zeeb		;;
52*0696600cSBjoern A. Zeeb	esac
53*0696600cSBjoern A. Zeeb}
54*0696600cSBjoern A. Zeeb
55*0696600cSBjoern A. Zeeb# If we do not have a writable /var, create a memory filesystem for /var
56*0696600cSBjoern A. Zeeb# unless told otherwise by rc.conf.  We don't have /usr yet so use mkdir
57*0696600cSBjoern A. Zeeb# instead of touch to test.  We want mount to record its mounts so we
58*0696600cSBjoern A. Zeeb# have to make sure /var/db exists before doing the mount -a.
59*0696600cSBjoern A. Zeeb#
60*0696600cSBjoern A. Zeebcase "${varmfs}" in
61*0696600cSBjoern A. Zeeb[Yy][Ee][Ss])
62*0696600cSBjoern A. Zeeb	mount_md ${varsize} /var "${varmfs_flags}"
63*0696600cSBjoern A. Zeeb	;;
64*0696600cSBjoern A. Zeeb[Nn][Oo])
65*0696600cSBjoern A. Zeeb	;;
66*0696600cSBjoern A. Zeeb*)
67*0696600cSBjoern A. Zeeb	if /bin/mkdir -p /var/.diskless 2> /dev/null; then
68*0696600cSBjoern A. Zeeb		rmdir /var/.diskless
69*0696600cSBjoern A. Zeeb	else
70*0696600cSBjoern A. Zeeb		mount_md ${varsize} /var "${varmfs_flags}"
71*0696600cSBjoern A. Zeeb	fi
72*0696600cSBjoern A. Zeebesac
73*0696600cSBjoern A. Zeeb
74*0696600cSBjoern A. Zeeb
75*0696600cSBjoern A. Zeeb# If we have an empty looking /var, populate it, but only if we have
76*0696600cSBjoern A. Zeeb# /usr available.  Hopefully, we'll eventually find a workaround, but
77*0696600cSBjoern A. Zeeb# in realistic diskless setups, we're probably ok.
78*0696600cSBjoern A. Zeebcase "${populate_var}" in
79*0696600cSBjoern A. Zeeb[Yy][Ee][Ss])
80*0696600cSBjoern A. Zeeb	populate_var
81*0696600cSBjoern A. Zeeb	;;
82*0696600cSBjoern A. Zeeb[Nn][Oo])
83*0696600cSBjoern A. Zeeb	exit 0
84*0696600cSBjoern A. Zeeb	;;
85*0696600cSBjoern A. Zeeb*)
86*0696600cSBjoern A. Zeeb	if [ -d /var/run -a -d /var/db -a -d /var/empty ] ; then
87*0696600cSBjoern A. Zeeb		true
88*0696600cSBjoern A. Zeeb	elif [ -x /usr/sbin/mtree ] ; then
89*0696600cSBjoern A. Zeeb		populate_var
90*0696600cSBjoern A. Zeeb	else
91*0696600cSBjoern A. Zeeb		# We need mtree to populate /var so try mounting /usr.
92*0696600cSBjoern A. Zeeb		# If this does not work, we can not boot so it is OK to
93*0696600cSBjoern A. Zeeb		# try to mount out of order.
94*0696600cSBjoern A. Zeeb		mount /usr
95*0696600cSBjoern A. Zeeb		if [ ! -x /usr/sbin/mtree ] ; then
96*0696600cSBjoern A. Zeeb			exit 1
97*0696600cSBjoern A. Zeeb		else
98*0696600cSBjoern A. Zeeb			populate_var
99*0696600cSBjoern A. Zeeb		fi
100*0696600cSBjoern A. Zeeb	fi
101*0696600cSBjoern A. Zeeb	;;
102*0696600cSBjoern A. Zeebesac
103*0696600cSBjoern A. Zeeb
104*0696600cSBjoern A. Zeeb# Make sure we have /var/log/utx.lastlogin and /var/log/utx.log files
105*0696600cSBjoern A. Zeebif [ ! -f /var/log/utx.lastlogin ]; then
106*0696600cSBjoern A. Zeeb	cp /dev/null /var/log/utx.lastlogin
107*0696600cSBjoern A. Zeeb	chmod 644 /var/log/utx.lastlogin
108*0696600cSBjoern A. Zeebfi
109*0696600cSBjoern A. Zeebif [ ! -f /var/log/utx.log ]; then
110*0696600cSBjoern A. Zeeb	cp /dev/null /var/log/utx.log
111*0696600cSBjoern A. Zeeb	chmod 644 /var/log/utx.log
112*0696600cSBjoern A. Zeebfi
113