xref: /dragonfly/etc/rc.d/diskless (revision aa8d5dcb)
1#!/bin/sh
2#
3# Copyright (c) 1999  Matt Dillon
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12#    notice, this list of conditions and the following disclaimer in the
13#    documentation and/or other materials provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25# SUCH DAMAGE.
26#
27# $FreeBSD: src/etc/rc.d/diskless,v 1.25 2003/02/15 16:34:14 jhay Exp $
28# $DragonFly: src/etc/rc.d/diskless,v 1.4 2004/01/26 17:21:15 rob Exp $
29#
30
31# PROVIDE: diskless
32# REQUIRE: initdiskless rcconf mountcritlocal
33# BEFORE: addswap random
34# KEYWORD: DragonFly
35
36if [ -r /etc/rc.subr ]; then
37    . /etc/rc.subr
38    dummy_rc_command "$1"
39fi
40
41dlv=`/sbin/sysctl -n vfs.nfs.diskless_valid 2> /dev/null`
42[ ${dlv:=0} -eq 0 ] && exit 0
43
44name="diskless2"
45
46# Provide a function for normalizing the mounting of memory
47# filesystems.  This should allow the rest of the code here to remain
48# as close as possible between 5-current and 4-stable.
49#   $1 = size
50#   $2 = mount point
51#   $3 = (optional) bytes-per-inode
52mount_md() {
53	if [ -n "$3" ]; then
54		bpi="-i $3"
55	fi
56	/sbin/mdmfs $bpi -s $1 -M md $2
57}
58
59# If there is a global system configuration file, suck it in.
60#
61if [ -r /etc/rc.subr ]; then
62	. /etc/rc.subr
63	load_rc_config $name
64elif [ -r /etc/defaults/rc.conf ]; then
65	. /etc/defaults/rc.conf
66	source_rc_confs
67elif [ -r /etc/rc.conf ]; then
68	. /etc/rc.conf
69fi
70
71# If we do not have a writable /var, create a memory
72# filesystem for /var.  We don't have /usr yet so
73# use mkdir instead of touch to test.  We want mount
74# to record its mounts so we have to make sure /var/db
75# exists before doing the mount -a.
76#
77if (/bin/mkdir /var/.diskless 2> /dev/null); then
78        rmdir /var/.diskless
79else
80	echo "+++ mount_md of /var"
81	mount_md ${varsize:=32m} /var
82fi
83
84if [ ! -d /var/db ]; then
85	mkdir /var/db
86fi
87
88# Now we need the rest of our mounts, particularly /usr
89#
90mount -a       # chown and chgrp are in /usr
91
92# Populate /var
93#
94echo "+++ populate /var using /etc/mtree/BSD.var.dist"
95/usr/sbin/mtree -deU -f /etc/mtree/BSD.var.dist -p /var > /dev/null
96case ${sendmail_enable} in
97[Nn][Oo][Nn][Ee])
98	;;
99*)
100	/usr/sbin/mtree -deU -f /etc/mtree/BSD.sendmail.dist -p / > /dev/null
101	;;
102esac
103
104echo "+++ create log files based on the contents of /etc/newsyslog.conf"
105LOGFILES=`/usr/bin/awk '$1 != "#" { printf "%s ", $1 } ' /etc/newsyslog.conf`
106if [ -n "$LOGFILES" ]; then
107	/usr/bin/touch $LOGFILES
108fi
109
110echo "+++ create lastlog"
111/usr/bin/touch /var/log/lastlog
112
113# Make sure our aliases database is uptodate, the aliases may have
114# been overriden in /conf.
115#
116/usr/bin/newaliases
117
118# XXX make sure to create one dir for each printer as requested by lpd
119#
120# If we do not have a writable /tmp, create a memory
121# filesystem for /tmp.  If /tmp is a symlink (e.g. to /var/tmp,
122# then it should already be writable).
123#
124if (/bin/mkdir /tmp/.diskless 2> /dev/null); then
125	rmdir /tmp/.diskless
126else
127	if [ -h /tmp ]; then
128		echo "*** /tmp is a symlink to a non-writable area!"
129		echo "dropping into shell, ^D to continue anyway."
130		/bin/sh
131	else
132		mount_md ${tmpsize:=20480} /tmp
133		chmod 01777 /tmp
134	fi
135fi
136
137if sysctl vfs.devfs.generation > /dev/null 2>&1 ; then
138	# we have DEVFS, no worries...
139	true
140elif (/bin/mkdir /dev/.diskless 2> /dev/null); then
141	# if /dev is writable assume it has already been populated
142	# via rc.diskless1
143	#
144	rmdir /dev/.diskless
145else
146	(cd /; find -x dev | cpio -o -H newc) > /tmp/dev.tmp
147	mount_md 4096 /dev 512
148	(cd /; cpio -i -H newc -d < /tmp/dev.tmp)
149	rm -f /tmp/dev.tmp
150fi
151
152# generate our hostname
153#
154if [ -z "`hostname -s`" ]; then
155	hostname=`/bin/kenv dhcp.host-name`
156	hostname $hostname
157	echo "Hostname is $hostname"
158fi
159
160# if the info is available via dhcp/kenv
161# build the resolv.conf
162#
163if [ ! -e /etc/resolv.conf ]; then
164	echo domain `/bin/kenv dhcp.domain-name` > /etc/resolv.conf
165
166	set `/bin/kenv dhcp.domain-name-servers`
167	for ns in `IFS=','; echo $*`; do
168		echo nameserver $ns >> /etc/resolv.conf;
169	done
170fi
171
172