3 # Copyright (c) 2000 The FreeBSD Project
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
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.
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
27 # @(#)rc 5.27 (Berkeley) 6/5/91
31 # System startup script run by init on autoboot
32 # or after single-user.
33 # Output and error are redirected to console by init,
34 # and the console is the controlling terminal.
36 # Note that almost all of the user-configurable behavior is no longer in
37 # this file, but rather in /etc/defaults/rc.conf. Please check that file
38 # first before contemplating any changes here. If you do need to change
39 # this file for some reason, we would like to know about it.
43 # Set shell to ignore SIGINT (2), but not children;
44 # shell catches SIGQUIT (3) and returns to single user after fsck.
47 trap : 3 # shouldn't be needed
50 PATH=/sbin:/bin:/usr/sbin:/usr/bin
53 # check_rcng() is run in a subshell solely to determine the
54 # RCNG mode. We do not want to pollute our variable space
55 # too soon so the procedure must be run in a subshell. An
56 # exit code of 3 indicates RCNG is enabled.
60 if [ -r /etc/defaults/rc.conf ]; then
61 . /etc/defaults/rc.conf
63 elif [ -r /etc/rc.conf ]; then
67 # Diskless setups have to depend on a different mechanism since
68 # their config files haven't been retargeted yet.
70 [ -e /.rcng_yes ] && rc_ng="YES"
92 # Note: the system configuration files are loaded as part of
93 # the RCNG system (rc.d/rccond). Do not load them here as it may
94 # interfere with diskless booting.
96 if [ "$1" = autoboot ]; then
99 rc_fast=yes # run_rc_command(): do fast booting
105 os=`eval ${CMD_OSTYPE}`
106 files=`rcorder -k ${os} -s nostart /etc/rc.d/* 2>/dev/null`
108 for _rc_elem in ${files}; do
109 run_rc_script ${_rc_elem} ${_boot}
117 # fall-through to the old rc scripts
123 # BOOTP diskless boot. We have to run the rc file early in order to
124 # retarget various config files.
126 if [ -r /etc/rc.diskless1 ]; then
127 dlv=`/sbin/sysctl -n vfs.nfs.diskless_valid 2> /dev/null`
128 if [ ${dlv:=0} != 0 ]; then
133 # If there is a global system configuration file, suck it in.
135 if [ -r /etc/defaults/rc.conf ]; then
136 . /etc/defaults/rc.conf
138 elif [ -r /etc/rc.conf ]; then
142 # XXX - Deprecated variable name support
143 # for rpcbind and ntpd
145 [ -n "$portmap_enable" ] && rpcbind_enable="$portmap_enable"
146 [ -n "$portmap_program" ] && rpcbind_program="$portmap_program"
147 [ -n "$portmap_flags" ] && rpcbind_flags="$portmap_flags"
148 [ -n "$single_mountd_enable" ] && mountd_enable="$single_mountd_enable"
149 [ -n "$xntpd_enable" ] && ntpd_enable="$xntpd_enable"
150 [ -n "$xntpd_program" ] && ntpd_program="$xntpd_program"
151 [ -n "$xntpd_flags" ] && ntpd_flags="$xntpd_flags"
154 if [ -f "${1}" -a -r "${1}" -a -s "${1}" ]; then
155 # echo "Using ${1} as an entropy file"
156 cat "${1}" | dd of=/dev/random bs=8k 2>/dev/null
166 eval svc_val=\${$svc_var}
167 eval dep_val=\${$dep_var}
175 eval ${dep_var}="YES"
176 echo "DEPENDENCY NOTE: ${dep} will be enabled" \
184 chkdepend amd amd_enable rpcbind rpcbind_enable
185 chkdepend amd amd_enable NFS nfs_client_enable
186 chkdepend NFS nfs_server_enable rpcbind rpcbind_enable
187 chkdepend NIS nis_server_enable rpcbind rpcbind_enable
188 chkdepend NIS nis_client_enable rpcbind rpcbind_enable
190 # Enable dumpdev early so that a crash during the boot process can be caught.
197 /sbin/dumpon -v ${dumpdev}
201 # Enable harvesting of entropy via devices. The sooner this happens the
202 # better so that we can take advantage of the boot process.
204 echo -n 'Entropy harvesting:'
206 case ${harvest_interrupt} in
210 if [ -w /dev/random ]; then
211 /sbin/sysctl kern.random.sys.harvest.interrupt=1 >/dev/null
212 echo -n ' interrupts'
217 case ${harvest_ethernet} in
221 if [ -w /dev/random ]; then
222 /sbin/sysctl kern.random.sys.harvest.ethernet=1 >/dev/null
228 case ${harvest_p_to_p} in
232 if [ -w /dev/random ]; then
233 /sbin/sysctl kern.random.sys.harvest.point_to_point=1 >/dev/null
234 echo -n ' point_to_point'
241 # First pass at reseeding /dev/random.
243 case ${entropy_file} in
247 if [ -w /dev/random ]; then
248 feed_dev_random "${entropy_file}"
253 # XXX temporary until we can get the entropy
255 # Entropy below is not great,
256 # but better than nothing.
257 ( ps -fauxww; sysctl -a; date; df -ib; dmesg; ps -fauxww; ) \
258 | dd of=/dev/random bs=8k 2>/dev/null
259 cat /bin/ls | dd of=/dev/random bs=8k 2>/dev/null
261 # Configure ccd devices.
263 if [ -r /etc/ccd.conf ]; then
267 case ${start_vinum} in
275 # Last chance to do things before potentially waiting for
276 # operator to do fsck related tasks
277 if [ -r /etc/rc.early ]; then
283 echo 'Automatic boot in progress...'
284 case ${background_fsck} in
300 echo 'Reboot failed... help!'
304 case ${fsck_y_enable} in
306 echo 'File system preen failed, trying fsck -y . . .'
312 echo 'Automatic filesystem check failed . . . help!'
318 echo 'Automatic filesystem check failed . . . help!'
324 echo 'Reboot interrupted'
328 # interrupt before catcher installed
332 echo 'Unknown error in reboot'
338 echo 'Skipping disk checks ...'
343 trap "echo 'Reboot interrupted'; exit 1" 3
345 # root normally must be read/write, but if this is a BOOTP NFS
346 # diskless boot it does not have to be.
348 case ${root_rw_mount} in
352 if ! mount -u -o rw / ; then
353 echo 'Mounting root filesystem rw failed, startup aborted'
359 umount -a >/dev/null 2>&1
361 # Set up the list of network filesystem types for which mounting should be
362 # delayed until after network initialization.
363 networkfs_types='nfs:NFS smbfs:SMB portalfs:PORTAL'
364 case ${extra_netfs_types} in
368 networkfs_types="${networkfs_types} ${extra_netfs_types}"
372 # Mount everything except nfs filesystems.
374 for i in ${networkfs_types}; do
376 mount_excludes="${mount_excludes}${fstype},"
378 mount_excludes=${mount_excludes%,}
379 mount -a -t ${mount_excludes}
385 echo 'Mounting /etc/fstab filesystems failed, startup aborted'
390 # Run custom disk mounting function here
392 if [ -n "${diskless_mount}" -a -r "${diskless_mount}" ]; then
396 # If we booted a special kernel remove the record so we will boot
397 # the default kernel next time
398 rm -f /boot/nextboot.conf
400 # Reseed /dev/random with previously stored entropy.
401 case ${entropy_dir} in
405 entropy_dir=${entropy_dir:-/var/db/entropy}
406 if [ -d "${entropy_dir}" ]; then
407 if [ -w /dev/random ]; then
408 for seedfile in ${entropy_dir}/*; do
409 feed_dev_random "${seedfile}"
416 case ${entropy_file} in
420 if [ -w /dev/random ]; then
421 feed_dev_random "${entropy_file}"
431 if [ $# -eq 0 ]; then
437 cd "$dir" && for file in .* *
439 [ ."$file" = .. -o ."$file" = ... ] && continue
440 if [ -d "$file" -a ! -L "$file" ]
453 if [ -d /var/run -a ! -f /var/run/clean_var ]; then
455 # Keep a copy of the boot messages around
456 dmesg >/var/run/dmesg.boot
457 # And an initial utmp file
458 (cd /var/run && cp /dev/null utmp && chmod 644 utmp;)
461 if [ -d /var/spool/lock -a ! -f /var/spool/lock/clean_var ]; then
462 purgedir /var/spool/lock
463 >/var/spool/lock/clean_var
465 rm -rf /var/spool/uucp/.Temp/*
468 # network_pass1() *may* end up writing stuff to /var - we don't want to
469 # remove it immediately afterwards - *nor* do we want to fail to clean
470 # an NFS-mounted /var.
471 rm -f /var/run/clean_var /var/spool/lock/clean_var
474 # Add additional swapfile, if configured.
480 if [ -w "${swapfile}" -a -c /dev/mdctl ]; then
481 echo "Adding ${swapfile} as additional swap"
482 mdev=`mdconfig -a -t vnode -f ${swapfile}` && swapon /dev/${mdev}
487 # Early pass to set the variables we can
489 if [ -r /etc/rc.sysctl ]; then
490 sh /etc/rc.sysctl first
493 # Configure serial devices
495 if [ -r /etc/rc.serial ]; then
499 # Start up PC-card configuration
501 if [ -r /etc/rc.pccard ]; then
505 # Start up the initial network configuration.
507 if [ -r /etc/rc.network ]; then
508 . /etc/rc.network # We only need to do this once.
512 case ${ipv6_enable} in
514 if [ -r /etc/rc.network6 ]; then
515 . /etc/rc.network6 # We only need to do this once also.
521 # Mount NFS filesystems if present in /etc/fstab
523 # XXX When the vfsload() issues with nfsclient support and related sysctls
524 # have been resolved, this block can be removed, and the condition that
525 # skips nfs in the following block (for "other network filesystems") can
527 case "`mount -d -a -t nfs 2> /dev/null`" in
529 # Handle absent nfs client support
530 nfsclient_in_kernel=0
531 if sysctl vfs.nfs >/dev/null 2>&1; then
532 nfsclient_in_kernel=1
534 kldload nfsclient && nfsclient_in_kernel=1
537 case ${nfsclient_in_kernel} in
539 echo -n 'Mounting NFS filesystem:'
544 echo 'Warning: nfs mount requested, but no nfs client in kernel'
550 # Mount other network filesystems if present in /etc/fstab
551 for i in ${networkfs_types}; do
555 if [ "${fstype}" = "nfs" ]; then
558 case "`mount -d -a -t ${fstype}`" in
560 echo -n "Mounting ${fsdecr} filesystems:"
561 mount -a -t ${fstype}
567 # Whack the pty perms back into shape.
569 if ls /dev/tty[pqrsPQRS]* > /dev/null 2>&1; then
570 chflags 0 /dev/tty[pqrsPQRS]*
571 chmod 666 /dev/tty[pqrsPQRS]*
572 chown root:wheel /dev/tty[pqrsPQRS]*
575 # Clean up left-over files
577 clean_var # If it hasn't already been done
578 rm /var/run/clean_var /var/spool/lock/clean_var
580 # Clearing /tmp at boot-time seems to have a long tradition. It doesn't
581 # help in any way for long-living systems, and it might accidentally
582 # clobber files you would rather like to have preserved after a crash
583 # (if not using mfs /tmp anyway).
585 # See also the example of another cleanup policy in /etc/periodic/daily.
587 case ${clear_tmp_enable} in
589 echo -n 'Clearing /tmp:'
590 # prune quickly with one rm, then use find to clean up /tmp/[lq]*
591 # (not needed with mfs /tmp, but doesn't hurt there...)
592 (cd /tmp && rm -rf [a-km-pr-zA-Z]* &&
593 find -d . ! -name . ! -name lost+found ! -name quota.user \
594 ! -name quota.group -exec rm -rf -- {} \;)
599 # Remove X lock files, since they will prevent you from restarting X11
600 # after a system crash.
603 rm -fr /tmp/.X11-unix
604 mkdir -m 1777 /tmp/.X11-unix
606 # Snapshot any kernel -c changes back to disk here <someday>.
607 # This has changed with ELF and /kernel.config.
609 # Load LOMAC(4) security if wanted.
610 case ${lomac_enable} in
612 kldload mac_lomac >/dev/null 2>&1
616 echo -n 'Additional daemons:'
618 # Start system logging and name service. Named needs to start before syslogd
619 # if you don't have a /etc/resolv.conf.
621 case ${syslogd_enable} in
623 # Transitional symlink (for the next couple of years :) until all
624 # binaries have had a chance to move towards /var/run/log.
625 if [ ! -L /dev/log ]; then
626 # might complain for r/o root f/s
627 ln -sf /var/run/log /dev/log
632 ${syslogd_program:-/usr/sbin/syslogd} ${syslogd_flags}
638 # Build device name databases if we are not using DEVFS
640 if sysctl vfs.devfs.generation > /dev/null 2>&1 ; then
641 rm -f /var/run/dev.db
646 # $dumpdir should be a directory or a symbolic link
647 # to the crash directory if core dumps are to be saved.
649 if [ "${dumpdev}" != 'NO' ]; then
659 if [ "${dumpdir}" != 'NO' ]; then
660 echo -n 'Checking for core dump: '
661 /sbin/savecore ${savecore_flags} "${dumpdir}"
665 if [ -n "${network_pass1_done}" ]; then
669 # Enable/Check the quotas (must be after ypbind if using NIS)
671 case ${enable_quotas} in
673 case ${check_quotas} in
675 echo -n 'Checking quotas:'
681 echo -n 'Enabling quotas:'
687 if [ -n "${network_pass2_done}" ]; then
691 # Check the password temp/lock file
693 if [ -e /etc/ptmp ]; then
694 logger -s -p auth.err \
695 "password file may be incorrect -- /etc/ptmp exists"
698 case ${accounting_enable} in
700 if [ -d /var/account ]; then
701 echo 'Turning on accounting:'
702 if [ ! -e /var/account/acct ]; then
703 touch /var/account/acct
705 accton /var/account/acct
710 # Make shared lib searching a little faster. Leave /usr/lib first if you
711 # add your own entries or you may come to grief.
713 ldconfig="/sbin/ldconfig"
714 case ${ldconfig_insecure} in
716 ldconfig="${ldconfig} -i"
719 if [ -x /sbin/ldconfig ]; then
721 for i in ${ldconfig_paths}; do
722 if [ -d "${i}" ]; then
726 echo 'ELF ldconfig path:' ${_LDC}
729 # Legacy aout support for i386 only
730 case `sysctl -n hw.machine_arch` in
732 # Default the a.out ldconfig path.
733 : ${ldconfig_paths_aout=${ldconfig_paths}}
735 for i in ${ldconfig_paths_aout}; do
736 if [ -d "${i}" ]; then
740 echo 'a.out ldconfig path:' ${_LDC}
741 ${ldconfig} -aout ${_LDC}
746 # Now start up miscellaneous daemons that don't belong anywhere else
748 echo -n 'Starting standard daemons:'
749 case ${inetd_enable} in
753 echo -n ' inetd'; ${inetd_program:-/usr/sbin/inetd} ${inetd_flags}
757 case ${cron_enable} in
761 echo -n ' cron'; ${cron_program:-/usr/sbin/cron} ${cron_flags}
765 case ${lpd_enable} in
767 echo -n ' printer'; ${lpd_program:-/usr/sbin/lpd} ${lpd_flags}
771 case ${sshd_enable} in
773 if [ -x ${sshd_program:-/usr/sbin/sshd} ]; then
775 ${sshd_program:-/usr/sbin/sshd} ${sshd_flags}
780 case ${usbd_enable} in
782 echo -n ' usbd'; /usr/sbin/usbd ${usbd_flags}
786 case ${mta_start_script} in
788 if [ -r ${mta_start_script} ]; then
789 sh ${mta_start_script}
796 # Recover vi editor files.
797 find /var/tmp/vi.recover ! -type f -a ! -type d -delete
798 vibackup=`echo /var/tmp/vi.recover/vi.*`
799 if [ "${vibackup}" != '/var/tmp/vi.recover/vi.*' ]; then
800 echo -n 'Recovering vi editor sessions:'
801 for i in /var/tmp/vi.recover/vi.*; do
802 # Only test files that are readable.
803 if [ ! -r "${i}" ]; then
807 # Unmodified nvi editor backup files either have the
808 # execute bit set or are zero length. Delete them.
809 if [ -x "${i}" -o ! -s "${i}" ]; then
814 # It is possible to get incomplete recovery files, if the editor
815 # crashes at the right time.
816 virecovery=`echo /var/tmp/vi.recover/recover.*`
817 if [ "${virecovery}" != "/var/tmp/vi.recover/recover.*" ]; then
818 for i in /var/tmp/vi.recover/recover.*; do
819 # Only test files that are readable.
820 if [ ! -r "${i}" ]; then
824 # Delete any recovery files that are zero length,
825 # corrupted, or that have no corresponding backup file.
826 # Else send mail to the user.
827 recfile=`awk '/^X-vi-recover-path:/{print $2}' < "${i}"`
828 if [ -n "${recfile}" -a -s "${recfile}" ]; then
838 # Make a bounds file for msgs(1) if there isn't one already
840 if [ -d /var/msgs -a ! -f /var/msgs/bounds -a ! -L /var/msgs/bounds ]; then
841 echo 0 > /var/msgs/bounds
844 case ${update_motd} in
848 if T=`mktemp /tmp/_motd.XXXXXX`; then
849 uname -v | sed -e 's,^\([^#]*\) #\(.* [1-2][0-9][0-9][0-9]\).*/\([^\]*\) $,\1 (\3) #\2,' > ${T}
850 awk '{if (NR == 1) {if ($1 == "FreeBSD") {next} else {print "\n"$0}} else {print}}' < /etc/motd >> ${T}
851 cmp -s ${T} /etc/motd || {
860 # Run rc.devfs if readable to customize devfs
862 if [ -r /etc/rc.devfs ]; then
866 # Configure implementation specific stuff
869 if [ -r /etc/rc.${arch} ]; then
873 # Configure the system console
875 if [ -r /etc/rc.syscons ]; then
879 echo -n 'Additional ABI support:'
881 # Load the SysV IPC API if requested.
882 case ${sysvipc_enable} in
885 kldload sysvmsg >/dev/null 2>&1
886 kldload sysvsem >/dev/null 2>&1
887 kldload sysvshm >/dev/null 2>&1
891 # Start the Linux binary compatibility if requested.
893 case ${linux_enable} in
896 if ! kldstat -v | grep -E 'linux(aout|elf)' > /dev/null; then
897 kldload linux > /dev/null 2>&1
899 if [ -x /compat/linux/sbin/ldconfig ]; then
900 /compat/linux/sbin/ldconfig
905 # Start the SysVR4 binary emulation if requested.
907 case ${svr4_enable} in
909 echo -n ' svr4'; kldload svr4 > /dev/null 2>&1
915 # Do traditional (but rather obsolete) rc.local file if it exists. If you
916 # use this file and want to make it programmatic, source /etc/defaults/rc.conf
917 # in /etc/rc.local and add your custom variables to /etc/rc.conf, as
918 # shown below. Please do not put local extensions into /etc/rc itself.
922 # if [ -r /etc/defaults/rc.conf ]; then
923 # . /etc/defaults/rc.conf
925 # elif [ -r /etc/rc.conf ]; then
929 # ... additional startup conditionals ...
932 if [ -r /etc/rc.local ]; then
933 echo -n 'Starting local daemons:'
938 # For each valid dir in $local_startup, search for init scripts matching *.sh
940 case ${local_startup} in
944 echo -n 'Local package initialization:'
946 if [ -z "${script_name_sep}" ]; then
949 for dir in ${local_startup}; do
950 if [ -d "${dir}" ]; then
951 for script in ${dir}/*.sh; do
952 slist="${slist}${script_name_sep}${script}"
956 script_save_sep="$IFS"
957 IFS="${script_name_sep}"
958 for script in ${slist}; do
959 if [ -x "${script}" ]; then
963 elif [ -f "${script}" -o -L "${script}" ]; then
964 echo -n " (skipping ${script##*/}, not executable)"
967 IFS="${script_save_sep}"
972 if [ -n "${network_pass3_done}" ]; then
976 # Late pass to set variables we missed the first time
978 if [ -r /etc/rc.sysctl ]; then
979 sh /etc/rc.sysctl last
982 # Raise kernel security level. This should be done only after `fsck' has
983 # repaired local filesystems if you want the securelevel to be greater than 1.
985 case ${kern_securelevel_enable} in
987 if [ "${kern_securelevel}" -ge 0 ]; then
988 echo 'Raising kernel security level: '
989 sysctl kern.securelevel=${kern_securelevel}
994 # Start background fsck checks if necessary
995 case ${background_fsck} in
997 bgfsck_msg='Starting background file system checks'
998 if [ ${background_fsck_delay:=0} -gt 0 ]; then
999 bgfsck_msg="${bgfsck_msg} in ${background_fsck_delay} seconds"
1001 echo "${bgfsck_msg}."
1003 (sleep ${background_fsck_delay}; nice -4 fsck -B -p) 2>&1 | \
1004 logger -p daemon.notice &