166830Sobrien#!/bin/sh
266830Sobrien#
366830Sobrien# Copyright (c) 1999  Matt Dillon
466830Sobrien# All rights reserved.
566830Sobrien#
666830Sobrien# Redistribution and use in source and binary forms, with or without
766830Sobrien# modification, are permitted provided that the following conditions
866830Sobrien# are met:
966830Sobrien# 1. Redistributions of source code must retain the above copyright
1066830Sobrien#    notice, this list of conditions and the following disclaimer.
1166830Sobrien# 2. Redistributions in binary form must reproduce the above copyright
1266830Sobrien#    notice, this list of conditions and the following disclaimer in the
1366830Sobrien#    documentation and/or other materials provided with the distribution.
1466830Sobrien#
1566830Sobrien# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1666830Sobrien# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1766830Sobrien# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1866830Sobrien# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1966830Sobrien# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2066830Sobrien# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2166830Sobrien# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2266830Sobrien# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2366830Sobrien# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2466830Sobrien# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25100280Sgordon# SUCH DAMAGE.
2651231Ssheldonh#
2766830Sobrien# $FreeBSD$
2866830Sobrien#
2943803Sdillon
30127345Sbrooks# PROVIDE: var
31301612Sngie# REQUIRE: mountcritlocal
3275931Simp
33240336Sobrien# NFS /var is not supported, unless NFS /var is part of diskless NFS /
34240336Sobrien
35127345Sbrooks. /etc/rc.subr
3676409Sbsd
37127345Sbrooksname="var"
38174464Sdougbstop_cmd=':'
3976409Sbsd
40127345Sbrooksload_rc_config $name
4143803Sdillon
42165664Syarpopulate_var()
43127345Sbrooks{
44127345Sbrooks	/usr/sbin/mtree -deU -f /etc/mtree/BSD.var.dist -p /var > /dev/null
45127345Sbrooks	case ${sendmail_enable} in
46127345Sbrooks	[Nn][Oo][Nn][Ee])
47127345Sbrooks		;;
48127345Sbrooks	*)
49127345Sbrooks		/usr/sbin/mtree -deU -f /etc/mtree/BSD.sendmail.dist -p / > /dev/null
50127345Sbrooks		;;
51127345Sbrooks	esac
52127345Sbrooks}
53108191Sdillon
54127345Sbrooks# If we do not have a writable /var, create a memory filesystem for /var
55127345Sbrooks# unless told otherwise by rc.conf.  We don't have /usr yet so use mkdir
56127345Sbrooks# instead of touch to test.  We want mount to record its mounts so we
57127345Sbrooks# have to make sure /var/db exists before doing the mount -a.
58108191Sdillon#
59127345Sbrookscase "${varmfs}" in
60127345Sbrooks[Yy][Ee][Ss])
61137451Skeramida	mount_md ${varsize} /var "${varmfs_flags}"
62100285Sbsd	;;
63127345Sbrooks[Nn][Oo])
64127345Sbrooks	;;
65100285Sbsd*)
66169923Srse	if /bin/mkdir -p /var/.diskless 2> /dev/null; then
67127345Sbrooks		rmdir /var/.diskless
68127345Sbrooks	else
69137451Skeramida		mount_md ${varsize} /var "${varmfs_flags}"
70127345Sbrooks	fi
71100285Sbsdesac
72100285Sbsd
7375898Simp
74127345Sbrooks# If we have an empty looking /var, populate it, but only if we have
75127345Sbrooks# /usr available.  Hopefully, we'll eventually find a workaround, but
76127345Sbrooks# in realistic diskless setups, we're probably ok.
77127345Sbrookscase "${populate_var}" in
78127345Sbrooks[Yy][Ee][Ss])
79165664Syar	populate_var
80124149Sbrooks	;;
81127345Sbrooks[Nn][Oo])
82142957Sbrooks	exit 0
83127345Sbrooks	;;
84124149Sbrooks*)
85127345Sbrooks	if [ -d /var/run -a -d /var/db -a -d /var/empty ] ; then
86127345Sbrooks		true
87142965Sbrooks	elif [ -x /usr/sbin/mtree ] ; then
88165664Syar		populate_var
89127345Sbrooks	else
90142965Sbrooks		# We need mtree to populate /var so try mounting /usr.
91142965Sbrooks		# If this does not work, we can not boot so it is OK to
92142965Sbrooks		# try to mount out of order.
93142965Sbrooks		mount /usr
94142965Sbrooks		if [ ! -x /usr/sbin/mtree ] ; then
95142965Sbrooks			exit 1
96142965Sbrooks		else
97165664Syar			populate_var
98142965Sbrooks		fi
99127345Sbrooks	fi
100124149Sbrooks	;;
101124149Sbrooksesac
102142957Sbrooks
103202218Sed# Make sure we have /var/log/utx.lastlogin and /var/log/utx.log files
104202218Sedif [ ! -f /var/log/utx.lastlogin ]; then
105202218Sed	cp /dev/null /var/log/utx.lastlogin
106202218Sed	chmod 644 /var/log/utx.lastlogin
107142957Sbrooksfi
108202218Sedif [ ! -f /var/log/utx.log ]; then
109202218Sed	cp /dev/null /var/log/utx.log
110202218Sed	chmod 644 /var/log/utx.log
111142957Sbrooksfi
112