rc revision 1.148
1#	$NetBSD: rc,v 1.148 2000/03/07 14:33:13 jdolecek Exp $
2#	originally from: @(#)rc	8.2 (Berkeley) 3/17/94
3
4# System startup script run by init on autoboot
5# or after single-user.
6# Output and error are redirected to console by init,
7# and the console is the controlling terminal.
8
9stty status '^T'
10
11# Set shell to ignore SIGINT (2), but not children;
12# shell catches SIGQUIT (3) and returns to single user after fsck.
13trap : 2
14trap : 3	# shouldn't be needed
15
16export HOME=/
17export PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin
18
19# Configure ccd devices.
20if [ -f /etc/ccd.conf ]; then
21	ccdconfig -C
22fi
23
24# Configure raid devices.
25for dev in 0 1 2 3; do
26	if [ -f /etc/raid$dev.conf ]; then
27		raidctl -c /etc/raid$dev.conf raid$dev
28	fi
29done
30
31# Add all block-type swap devices; these might be necessary
32# during disk checks.
33swapctl -A -t blk
34
35if [ -e /fastboot ]; then
36	echo "Fast boot: skipping disk checks."
37elif [ "$1" = autoboot ]; then
38	echo "Automatic boot in progress: starting file system checks."
39	fsck -p
40	case $? in
41	0)
42		;;
43	2)
44		exit 1
45		;;
46	4)
47		echo "Rebooting..."
48		reboot
49		echo "Reboot failed; help!"
50		exit 1
51		;;
52	8)
53		echo "Automatic file system check failed; help!"
54		exit 1
55		;;
56	12)
57		echo "Boot interrupted."
58		exit 1
59		;;
60	130)
61		# interrupt before catcher installed
62		exit 1
63		;;
64	*)
65		echo "Unknown error; help!"
66		exit 1
67		;;
68	esac
69fi
70
71trap "echo 'Boot interrupted.'; exit 1" 3
72
73umount -a >/dev/null 2>&1
74mount /
75rm -f /fastboot		# XXX (root now writeable)
76
77if [ -f /etc/rc.subr ]; then
78	. /etc/rc.subr
79else
80	echo "Can't read /etc/rc.subr; aborting."
81	exit 1;
82fi
83
84if [ -f /etc/rc.conf ]; then
85	. /etc/rc.conf
86fi
87
88if ! checkyesno rc_configured; then
89	echo "/etc/rc.conf is not configured. Multiuser boot aborted."
90	exit 1
91fi
92
93# set flags on ttys.  (do early, in case they use tty for SLIP in netstart)
94echo 'setting tty flags'
95ttyflags -a
96
97# load kernel modules specified in /etc/lkm.conf if the /usr filesystem
98# is already present with "/" or can be mounted now
99if checkyesno lkm && [ -f /etc/rc.lkm ]; then
100	mount /usr >/dev/null 2>&1
101	if [ -x /usr/bin/ld ]; then
102		lkmstage=BEFORENET
103		. /etc/rc.lkm
104	fi
105fi
106
107mount_critical_filesystems local
108
109if [ -e /etc/etc.current ] ; then
110	if [ -h /etc/etc.default ] ; then
111		def=`ls -ld /etc/etc.default 2>&1`
112		default=`expr "$def" : '.*-> etc\.\(.*\)' 2>&1`
113	else
114		default=current
115	fi
116	spc=""
117	conflist=`cd /etc; ls -1d etc.* 2>&1 | egrep -v 'current|default'`
118	for i in $conflist; do
119		name=`expr $i : 'etc\.\(.*\)' 2>&1`
120		if [ $name = $default ] ; then
121			echo -n "${spc}[${name}]"
122		else
123			echo -n "${spc}${name}"
124		fi
125		spc=" "
126	done
127	echo
128	master=$$
129	conf=/etc/passwd
130	while [ ! -d /etc/etc.$conf ] ; do
131		trap 'conf=$default; echo; echo Using default of $conf' 14
132		echo -n "Which configuration [$default] ? "
133		(sleep 30 && kill -ALRM $master) >/dev/null 2>&1 &
134		read conf
135		trap "" 14
136		if [ -z $conf ] ; then
137			conf=$default
138		fi
139		if [ ! -d /etc/etc.$conf -a ! -h /etc/etc.$conf ] ; then
140			conf=/etc/passwd
141		fi
142	done
143	rm -f /etc/etc.current
144	ln -s /etc/etc.$conf /etc/etc.current
145	if [ -f /etc/rc.conf ] ; then
146		. /etc/rc.conf
147	fi
148fi
149
150# set hostname, turn on network
151echo 'starting network'
152sh /etc/netstart
153if [ $? -ne 0 ]; then
154	exit 1
155fi
156
157mount_critical_filesystems network
158
159# Network Address Translation...
160if checkyesno ipnat && [ -f /etc/ipnat.conf ]; then
161	echo 'installing NAT rules ... '
162	if ! checkyesno ipfilter || [ ! -f /etc/ipf.conf ]; then
163		ipf -E -Fa
164	fi
165	ipnat -F -f /etc/ipnat.conf
166fi
167
168# "Critical" file systems are now mounted.  Go ahead and swap
169# to files now, since they will be residing in the critical file
170# systems (or, at least, better).
171swapctl -A -t noblk
172
173# Check for no swap, and warn about it unless that is desired.
174if ! checkyesno no_swap; then
175	swapctl -s | grep 'no swap devices configured' > /dev/null && \
176		echo "WARNING: no swap space configured!"
177fi
178
179# clean up left-over files
180rm -f /etc/nologin
181rm -f /var/spool/lock/LCK.*
182rm -f /var/spool/uucp/STST/*
183(cd /var/run && { rm -rf -- *; install -c -m 664 -g utmp /dev/null utmp; })
184
185# build ps databases
186echo 'building databases...'
187# if this kernel supports machdep.booted_kernel, use that to build the KVM
188# db so dmesg will run work when we boot /netbsd.new, etc.
189sysctl machdep 2>/dev/null | grep -q booted_kernel
190if [ $? -eq 0 ] ; then
191	kvm_mkdb `sysctl -n machdep.booted_kernel`
192	if [ $? -ne 0 ] ; then
193		kvm_mkdb /netbsd
194	fi
195else
196	kvm_mkdb /netbsd
197fi
198dev_mkdb
199
200# get the system dmesg output, hopefully containing the boot messages
201# $dmesg_flags is imported from /etc/rc.conf
202if checkyesno dmesg; then
203	dmesg $dmesg_flags > /var/run/dmesg.boot
204fi
205
206if checkyesno wscons && [ -f /etc/rc.wscons ]; then
207	echo 'configuring wscons'
208	/bin/sh /etc/rc.wscons $wscons_flags
209fi
210
211# start the system logger first, so that all messages from daemons
212# are logged, then start savecore to get a dump on low memory systems
213# and then start the name server.
214
215if checkyesno syslogd; then
216	echo 'starting system logger'
217	# Transitional symlink for old binaries
218	if [ ! -h /dev/log ] ; then
219		ln -sf /var/run/log /dev/log
220	fi
221	rm -f /var/run/log
222	syslogd $syslogd_flags
223fi
224
225# Enable ipmon (only useful if ipfilter is running)
226# NOTE: requires the IPFILTER_LOG kernel option.
227if checkyesno ipmon; then
228	echo 'starting ipmon'
229	ipmon $ipmon_flags &
230fi
231
232# /var/crash should be a directory or a symbolic link
233# to the crash directory if core dumps are to be saved.
234if checkyesno savecore; then
235	if [ -d /var/crash/. ]; then
236		echo checking for core dump...
237		savecore $savecore_flags /var/crash
238	else
239		logger -s "WARNING: no /var/crash directory; savecore not run."
240	fi
241fi
242
243if checkyesno named; then
244	echo 'starting name server';	named $named_flags
245fi
246
247# set time, if requested
248if checkyesno ntpdate; then
249	if [ -z "$ntpdate_hosts" ]; then
250		ntpdate_hosts=`awk '
251		    /^server[ \t]*127.127/	{next}
252		    /^(server|peer)/		{print $2}
253		' </etc/ntp.conf`
254	fi
255	if [ -n "$ntpdate_hosts"  ]; then
256		echo 'Setting date via ntp.'
257		ntpdate -b -s $ntpdate_hosts
258	fi
259fi
260
261# now start the rpc servers, for YP server/client.
262echo -n 'starting rpc daemons:'
263
264# note that portmap is generally required for all other rpc services.
265if checkyesno portmap; then
266	echo -n ' portmap';		portmap $portmap_flags
267fi
268
269if checkyesno ypserv; then
270	if ! checkyesno portmap; then
271		echo
272		logger -s "WARNING: set portmap=YES in /etc/rc.conf" \
273		    "if ypserv=YES."
274	fi
275
276	echo -n ' ypserv';		ypserv $ypserv_flags
277fi
278
279if checkyesno ypbind; then
280	echo -n ' ypbind';		ypbind $ypbind_flags
281fi
282
283if checkyesno yppasswdd; then
284	if ! checkyesno ypserv; then
285		echo
286		logger -s "WARNING: set ypserv=YES in /etc/rc.conf" \
287		    "if yppasswdd=YES."
288	fi
289
290	echo -n ' rpc.yppasswdd';       rpc.yppasswdd $yppasswdd_flags
291fi
292
293if checkyesno bootparamd; then
294	if [ -r /etc/bootparams ]; then
295		echo -n ' rpc.bootparamd'; rpc.bootparamd $bootparamd_flags
296	else
297		echo
298		logger -s "WARNING: /etc/bootparams not found;" \
299		    "bootparamd not started."
300	fi
301fi
302
303echo '.'
304
305# load kernel modules specified in /etc/lkm.conf
306if checkyesno lkm; then
307	if [ -r /etc/rc.lkm ]; then
308		lkmstage=BEFOREMOUNT
309		. /etc/rc.lkm
310	else
311		logger -s "WARNING: /etc/rc.lkm not found; LKMs not loaded."
312	fi
313fi
314
315mount -a
316
317# now start the rpc servers, for NFS server/client.
318if checkyesno nfs_server || checkyesno nfs_client || checkyesno amd; then
319	echo -n 'starting nfs daemons:'
320
321	if (checkyesno nfs_server || checkyesno amd) && \
322	   ! checkyesno portmap; then
323		echo
324		logger -s "WARNING: set portmap=YES in /etc/rc.conf" \
325		    "if nfs_server=YES or amd=YES."
326	fi
327
328	nfs_locking=NO
329
330	if checkyesno nfs_server; then
331		if [ -r /etc/exports ]; then
332			rm -f /var/db/mountdtab
333			echo -n > /var/db/mountdtab
334			echo -n ' mountd';		mountd $mountd_flags
335			echo -n ' nfsd';		nfsd $nfsd_flags
336			nfs_locking=YES
337		else
338			echo
339			logger -s "WARNING: /etc/exports not readable;" \
340			    "NFS server not started."
341		fi
342	fi
343
344	if checkyesno nfs_client; then
345		echo -n ' nfsiod';		nfsiod $nfsiod_flags
346		nfs_locking=YES
347	fi
348
349	if checkyesno nfs_locking; then
350		if checkyesno statd; then
351			echo -n ' rpc.statd';		rpc.statd $statd_flags
352		fi
353		if checkyesno lockd; then
354			echo -n ' rpc.lockd';		rpc.lockd $lockd_flags
355		fi
356	fi
357
358	if checkyesno amd; then
359		if [ -d "$amd_dir" ]; then
360			if [ -r "$amd_master" ]; then
361				echo -n ' amd'
362				amd $amd_flags -p -a $amd_dir \
363				`sed s/#.*$// <$amd_master`  >/var/run/amd.pid
364			else
365				echo
366				logger -s "WARNING: \$amd_master" \
367				    " ($amd_master) not readable;" \
368				    "amd not started."
369			fi
370		else
371			echo
372			logger -s "WARNING: \$amd_dir ($amd_dir) not a" \
373			    "directory; amd not started."
374		fi
375	fi
376	echo '.'
377fi
378
379
380if [ -f /sbin/ldconfig ]; then
381	echo 'creating runtime link editor directory cache.'
382	ldconfig
383fi 
384
385# load kernel modules specified in /etc/lkm.conf
386if checkyesno lkm && [ -f /etc/rc.lkm ]; then
387	lkmstage=AFTERMOUNT
388	. /etc/rc.lkm
389fi
390
391# if $defcorename is set, change it here.
392if [ -n "$defcorename" ]; then
393	echo -n 'setting default core name template: '
394	sysctl -w kern.defcorename=$defcorename
395fi
396
397# if $securelevel is set, change it here, else if it is 0, change
398# it to 1 here, before we start login services.
399if [ -n "$securelevel" ]; then
400	echo -n 'setting securelevel: '
401	sysctl -w kern.securelevel=$securelevel
402else
403	securelevel=`sysctl -n kern.securelevel`
404	if [ x"$securelevel" = x0 ]; then
405		echo -n 'setting securelevel: '
406		sysctl -w kern.securelevel=1
407	fi
408fi
409
410echo -n 'checking quotas:'; quotacheck -a; echo ' done.'
411quotaon -a
412
413chmod 666 /dev/tty[pqrs]*
414
415# check the password temp/lock file
416if [ -f /etc/ptmp ]
417then
418	logger -s -p auth.err \
419	    'password file may be incorrect -- /etc/ptmp exists'
420fi
421
422# XXX replace me with a script that works!
423virecovery=`echo /var/tmp/vi.recover/recover.*`
424if [ "$virecovery" != "/var/tmp/vi.recover/recover.*" ]; then
425	echo preserving editor files
426	for i in $virecovery; do
427		if [ -f $i ]; then
428			sendmail -t < $i
429		fi
430	done
431fi
432
433# sweep /tmp
434if checkyesno clear_tmp; then
435	echo clearing /tmp
436
437	# Prune quickly with one rm, then use find to clean up /tmp/[lq]* (this
438	# is not needed with mfs /tmp, but doesn't hurt anything).
439	(cd /tmp && rm -rf [a-km-pr-zA-Z]* &&
440	    find . ! -name . ! -name lost+found ! -name quota.user \
441		! -name quota.group -exec rm -rf -- {} \; -type d -prune)
442fi
443
444# Update kernel info in /etc/motd
445# Must be done *before* interactive logins are possible to prevent
446# possible race conditions.
447if checkyesno update_motd; then
448	echo 'updating motd.'
449	if [ ! -f /etc/motd ]; then
450		install -c -o root -g wheel -m 664 /dev/null /etc/motd
451	fi
452	T=/etc/_motd
453	rm -f $T
454	sysctl -n kern.version | sed 1q > $T
455	echo "" >> $T
456	sed '1,/^$/d' < /etc/motd >> $T
457	cmp -s $T /etc/motd || cp $T /etc/motd
458	rm -f $T
459fi
460
461if checkyesno accounting; then
462	if [ -f /var/account/acct ]; then
463		echo 'turning on accounting'
464		accton /var/account/acct
465	else
466		logger -s "WARNING: /var/account/acct doesn't exist;" \
467		    "accounting not enabled."
468	fi
469fi
470
471echo -n standard daemons:
472if checkyesno cron; then
473	echo -n ' cron';		cron
474fi
475echo '.'
476
477# now start all the other daemons
478echo -n starting network daemons:
479
480if checkyesno gated && checkyesno routed; then
481	echo
482	logger -s "WARNING: gated and routed both requested to be run:" \
483	    "running only gated."
484	routed=NO
485fi
486
487if checkyesno gated; then
488	if [ -r /etc/gated.conf ]; then
489		echo -n ' gated';		gated $gated_flags
490	else
491		echo
492		logger -s "WARNING: no /etc/gated.conf; gated not started."
493	fi
494fi
495
496if checkyesno routed; then
497	echo -n ' routed';		routed $routed_flags
498fi
499
500if checkyesno mrouted; then
501	echo -n ' mrouted';		mrouted $mrouted_flags
502fi
503
504if checkyesno route6d; then
505	echo -n ' route6d';		route6d $route6d_flags
506fi
507
508if checkyesno rtsold; then
509	if test "x$ip6mode" = "xautohost"; then
510		echo -n ' rtsold';	rtsold $rtsold_flags
511	else
512		echo
513		logger -s "WARNING: ip6mode must be set to autohost" \
514		    "to use rtsold."
515	fi
516fi
517
518if checkyesno rtadvd; then
519	if test "x$ip6mode" = "xrouter"; then
520		echo -n ' rtadvd';	rtadvd $rtadvd_flags
521	else
522		echo
523		logger -s "WARNING: rtadvd can be used on IPv6 router only."
524	fi
525fi
526
527if checkyesno timed; then
528	echo -n ' timed'; 		timed $timed_flags
529fi
530
531if checkyesno xntpd; then
532	echo -n ' xntpd';		xntpd $xntpd_flags
533fi
534
535if checkyesno dhcpd; then
536	if [ -r /etc/dhcpd.conf ]; then
537		echo -n ' dhcpd';		dhcpd $dhcpd_flags
538	else
539		echo
540		logger -s "WARNING: /etc/dhcpd.conf not readable;" \
541		    "dhcpd not started."
542	fi
543elif checkyesno dhcrelay; then
544	echo -n ' dhcrelay';		dhcrelay $dhcrelay_flags
545fi
546
547if checkyesno rwhod; then
548	echo -n ' rwhod';		rwhod
549fi
550
551if checkyesno lpd; then
552	echo -n ' lpd';			lpd $lpd_flags
553fi
554
555# We call sendmail with a full path so that SIGHUP works.
556if checkyesno sendmail; then
557	if [ -r /etc/sendmail.cf ]; then
558		echo -n ' sendmail';	/usr/sbin/sendmail $sendmail_flags
559	else
560		echo
561		logger -s "WARNING: /etc/sendmail.cf not readable;" \
562		    "sendmail not started."
563	fi
564fi
565
566# Start xfs before boot daemons, so its ready before client xterminals.
567if checkyesno xfs; then
568	echo -n ' xfs';			xfs $xfs_flags
569	sleep 2
570fi
571
572if checkyesno rarpd; then
573	if [ -r /etc/ethers ]; then
574		echo -n ' rarpd';	rarpd $rarpd_flags
575	else
576		echo
577		logger -s "WARNING: /etc/ethers not readable;" \
578		    "rarpd not started."
579	fi
580fi
581
582if checkyesno rbootd; then
583	if [ -r /etc/rbootd.conf ]; then
584		echo -n ' rbootd';	rbootd $rbootd_flags
585	else
586		echo
587		logger -s "WARNING: /etc/rbootd.conf not readable;" \
588		    "rbootd not started."
589	fi
590fi
591
592if checkyesno mopd; then
593	echo -n ' mopd';		mopd $mopd_flags
594fi
595
596if checkyesno apmd; then
597	echo -n ' apmd';		apmd $apmd_flags
598fi
599
600if checkyesno screenblank; then
601	echo -n ' screenblank';		screenblank $screenblank_flags
602fi
603
604if checkyesno inetd; then
605	if [ -r /etc/inetd.conf ]; then
606		echo -n ' inetd';	inetd $inetd_flags
607	else
608		echo
609		logger -s "WARNING: /etc/inetd.conf not readable;" \
610		    "inetd not started."
611	fi
612fi
613
614echo '.'
615
616# Kerberos runs ONLY on the Kerberos server machine
617if checkyesno kerberos; then
618	echo -n 'starting kerberos daemons:'
619	echo -n ' kerberos';	kerberos >> /var/log/kerberos.log &
620	echo -n ' kadmind';	kadmind -n >> /var/log/kadmind.log &
621	echo '.'
622fi
623
624# xdm after wscons - need a virtual screen for X
625if checkyesno xdm; then
626	echo 'starting xdm'
627	xdm $xdm_flags
628fi
629
630. /etc/rc.local
631
632date
633exit 0
634