rc revision 71340
1#!/bin/sh 2# 3# Copyright (c) 2000 The FreeBSD Project 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: head/etc/rc 71340 2001-01-21 20:57:38Z dougb $ 28# From: @(#)rc 5.27 (Berkeley) 6/5/91 29# 30 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. 35 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. 40 41stty status '^T' 42 43# Set shell to ignore SIGINT (2), but not children; 44# shell catches SIGQUIT (3) and returns to single user after fsck. 45# 46trap : 2 47trap : 3 # shouldn't be needed 48 49bootmode=$1 50 51HOME=/ 52PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin 53export HOME PATH 54 55# BOOTP diskless boot. We have to run the rc file early in order to 56# retarget various config files. 57# 58if [ -r /etc/rc.diskless1 ]; then 59 dlv=`/sbin/sysctl -n vfs.nfs.diskless_valid 2> /dev/null` 60 if [ ${dlv:=0} != 0 ]; then 61 . /etc/rc.diskless1 62 fi 63fi 64 65# If there is a global system configuration file, suck it in. 66# 67if [ -r /etc/defaults/rc.conf ]; then 68 . /etc/defaults/rc.conf 69 source_rc_confs 70elif [ -r /etc/rc.conf ]; then 71 . /etc/rc.conf 72fi 73 74feed_dev_random() { 75 if [ -f "${1}" -a -r "${1}" -a -s "${1}" ]; then 76 echo "Using ${1} as an entropy file" 77 cat "${1}" | dd of=/dev/random bs=8k 2>/dev/null 78 fi 79} 80 81chkdepend() { 82 svc=$1 83 svc_var=$2 84 dep=$3 85 dep_var=$4 86 87 eval svc_val=\${$svc_var} 88 eval dep_val=\${$dep_var} 89 90 case ${svc_val} in 91 [Yy][Ee][Ss]) 92 case ${dep_val} in 93 [Yy][Ee][Ss]) 94 ;; 95 *) 96 eval ${dep_var}="YES" 97 echo "DEPENDENCY NOTE: ${dep} will be enabled" \ 98 "to support ${svc}" 99 ;; 100 esac 101 ;; 102 esac 103} 104 105chkdepend amd amd_enable portmap portmap_enable 106chkdepend NFS nfs_server_enable portmap portmap_enable 107chkdepend NIS nis_server_enable portmap portmap_enable 108chkdepend NIS nis_client_enable portmap portmap_enable 109 110# First pass at reseeding /dev/random. 111# 112# XXX temporary until we can get the entropy 113# harvesting rate up 114# Entropy below is not great, 115# but better than nothing. 116( ps -efauxww; sysctl -a; date; df -ib; dmesg; ps -efauxww; ) \ 117 | dd of=/dev/random bs=8k 2>/dev/null 118cat /bin/ls | dd of=/dev/random bs=8k 2>/dev/null 119 120case ${entropy_file} in 121[Nn][Oo] | '') 122 ;; 123*) 124 if [ -w /dev/random ]; then 125 feed_dev_random "${entropy_file}" 126 fi 127 ;; 128esac 129 130# Configure ccd devices. 131# 132if [ -r /etc/ccd.conf ]; then 133 ccdconfig -C 134fi 135 136case ${start_vinum} in 137[Yy][Ee][Ss]) 138 vinum start 139 ;; 140esac 141 142swapon -a 143 144case ${bootmode} in 145autoboot) 146 echo 'Automatic boot in progress...' 147 fsck -p 148 case $? in 149 0) 150 ;; 151 2) 152 exit 1 153 ;; 154 4) 155 reboot 156 echo 'Reboot failed... help!' 157 exit 1 158 ;; 159 8) 160 echo 'Automatic file system check failed... help!' 161 exit 1 162 ;; 163 12) 164 echo 'Reboot interrupted' 165 exit 1 166 ;; 167 130) 168 # interrupt before catcher installed 169 exit 1 170 ;; 171 *) 172 echo 'Unknown error in reboot' 173 exit 1 174 ;; 175 esac 176 ;; 177*) 178 echo 'Skipping disk checks ...' 179 ;; 180esac 181 182set -T 183trap "echo 'Reboot interrupted'; exit 1" 3 184 185# root normally must be read/write, but if this is a BOOTP NFS 186# diskless boot it does not have to be. 187# 188case ${root_rw_mount} in 189[Nn][Oo] | '') 190 ;; 191*) 192 if ! mount -u -o rw / ; then 193 echo 'Mounting root filesystem rw failed, startup aborted' 194 exit 1 195 fi 196 ;; 197esac 198 199umount -a >/dev/null 2>&1 200 201# Mount everything except nfs filesystems. 202mount -a -t nonfs 203 204case $? in 2050) 206 ;; 207*) 208 echo 'Mounting /etc/fstab filesystems failed, startup aborted' 209 exit 1 210 ;; 211esac 212 213# Run custom disk mounting function here 214# 215if [ -n "${diskless_mount}" -a -r "${diskless_mount}" ]; then 216 sh ${diskless_mount} 217fi 218 219# Reseed /dev/random with previously stored entropy. 220case ${entropy_dir} in 221[Nn][Oo]) 222 ;; 223*) 224 entropy_dir=${entropy_dir:-/var/db/entropy} 225 if [ -d "${entropy_dir}" ]; then 226 if [ -w /dev/random ]; then 227 for seedfile in ${entropy_dir}/*; do 228 feed_dev_random "${seedfile}" 229 done 230 fi 231 fi 232 ;; 233esac 234 235case ${entropy_file} in 236[Nn][Oo] | '') 237 ;; 238*) 239 if [ -w /dev/random ]; then 240 feed_dev_random "${entropy_file}" 241 fi 242 ;; 243esac 244 245adjkerntz -i 246 247purgedir() { 248 local dir file 249 250 if [ $# -eq 0 ]; then 251 purgedir . 252 else 253 for dir 254 do 255 ( 256 cd "$dir" && for file in .* * 257 do 258 [ ."$file" = .. -o ."$file" = ... ] && continue 259 [ -d "$file" -a ! -L "$file" ] && 260 purgedir "$file" 261 [ -f "$file" ] && rm -f -- "$file" 262 done 263 ) 264 done 265 fi 266} 267 268clean_var() { 269 if [ ! -f /var/run/clean_var ]; then 270 rm -rf /var/run/* 271 purgedir /var/spool/lock 272 rm -rf /var/spool/uucp/.Temp/* 273 # Keep a copy of the boot messages around 274 dmesg >/var/run/dmesg.boot 275 # And an initial utmp file 276 (cd /var/run && cp /dev/null utmp && chmod 644 utmp;) 277 >/var/run/clean_var 278 fi 279} 280 281if [ -d /var/run -a -d /var/spool/lock -a -d /var/spool/uucp/.Temp ]; then 282 # network_pass1() *may* end up writing stuff to /var - we don't want to 283 # remove it immediately afterwards - *nor* to we want to fail to clean 284 # an nfs-mounted /var. 285 clean_var 286fi 287 288# Add additional swapfile, if configured. 289# 290case ${swapfile} in 291[Nn][Oo] | '') 292 ;; 293*) 294 if [ -w "${swapfile}" -a -c /dev/vn0b ]; then 295 echo "Adding ${swapfile} as additional swap" 296 vnconfig /dev/vn0b ${swapfile} && swapon /dev/vn0b 297 fi 298 ;; 299esac 300 301# Set sysctl variables as early as we can 302# 303if [ -r /etc/rc.sysctl ]; then 304 . /etc/rc.sysctl 305fi 306 307# Configure serial devices 308# 309if [ -r /etc/rc.serial ]; then 310 . /etc/rc.serial 311fi 312 313# Start up PC-card configuration 314# 315if [ -r /etc/rc.pccard ]; then 316 . /etc/rc.pccard 317fi 318 319# Start up the initial network configuration. 320# 321if [ -r /etc/rc.network ]; then 322 . /etc/rc.network # We only need to do this once. 323 network_pass1 324fi 325 326case ${ipv6_enable} in 327[Yy][Ee][Ss]) 328 if [ -r /etc/rc.network6 ]; then 329 . /etc/rc.network6 # We only need to do this once also. 330 network6_pass1 331 fi 332 ;; 333esac 334 335# Mount NFS filesystems if present in /etc/fstab 336case "`mount -d -a -t nfs`" in 337*mount_nfs*) 338 echo -n 'Mounting NFS file systems:' 339 mount -a -t nfs 340 echo '.' 341 ;; 342esac 343 344# Whack the pty perms back into shape. 345# 346if ls /dev/tty[pqrsPQRS]* > /dev/null 2>&1; then 347 chflags 0 /dev/tty[pqrsPQRS]* 348 chmod 666 /dev/tty[pqrsPQRS]* 349 chown root:wheel /dev/tty[pqrsPQRS]* 350fi 351 352# Clean up left-over files 353# 354clean_var # If it hasn't already been done 355rm /var/run/clean_var 356 357# Clearing /tmp at boot-time seems to have a long tradition. It doesn't 358# help in any way for long-living systems, and it might accidentally 359# clobber files you would rather like to have preserved after a crash 360# (if not using mfs /tmp anyway). 361# 362# See also the example of another cleanup policy in /etc/periodic/daily. 363# 364case ${clear_tmp_enable} in 365[Yy][Ee][Ss]) 366 echo -n 'Clearing /tmp:' 367 # prune quickly with one rm, then use find to clean up /tmp/[lq]* 368 # (not needed with mfs /tmp, but doesn't hurt there...) 369 (cd /tmp && rm -rf [a-km-pr-zA-Z]* && 370 find -d . ! -name . ! -name lost+found ! -name quota.user \ 371 ! -name quota.group -exec rm -rf -- {} \;) 372 echo '.' 373 ;; 374esac 375 376# Remove X lock files, since they will prevent you from restarting X11 377# after a system crash. 378# 379rm -f /tmp/.X*-lock /tmp/.X11-unix/* 380 381# Snapshot any kernel -c changes back to disk here <someday>. 382# This has changed with ELF and /kernel.config. 383 384echo -n 'Additional daemons:' 385 386# Start system logging and name service. Named needs to start before syslogd 387# if you don't have a /etc/resolv.conf. 388# 389case ${syslogd_enable} in 390[Yy][Ee][Ss]) 391 # Transitional symlink (for the next couple of years :) until all 392 # binaries have had a chance to move towards /var/run/log. 393 if [ ! -h /dev/log ]; then 394 # might complain for r/o root f/s 395 ln -sf /var/run/log /dev/log 396 fi 397 398 rm -f /var/run/log 399 echo -n ' syslogd'; syslogd ${syslogd_flags} 400 ;; 401esac 402 403echo '.' 404 405# Build device name databases if we are not using DEVFS 406# 407if sysctl vfs.devfs.generation > /dev/null 2>&1 ; then 408 rm -f /var/run/dev.db 409else 410 dev_mkdb 411fi 412 413# Enable dumpdev so that savecore can see it. 414# /var/crash should be a directory or a symbolic link 415# to the crash directory if core dumps are to be saved. 416# 417case ${dumpdev} in 418[Nn][Oo] | '') 419 ;; 420*) 421 if [ -e "${dumpdev}" -a -d /var/crash ]; then 422 dumpon -v ${dumpdev} 423 echo -n 'Checking for core dump: ' 424 savecore /var/crash 425 fi 426 ;; 427esac 428 429if [ -n "${network_pass1_done}" ]; then 430 network_pass2 431fi 432 433# Enable/Check the quotas (must be after ypbind if using NIS) 434# 435case ${enable_quotas} in 436[Yy][Ee][Ss]) 437 case ${check_quotas} in 438 [Yy][Ee][Ss]) 439 echo -n 'Checking quotas:' 440 quotacheck -a 441 echo ' done.' 442 ;; 443 esac 444 445 echo -n 'Enabling quotas:' 446 quotaon -a 447 echo ' done.' 448 ;; 449esac 450 451if [ -n "${network_pass2_done}" ]; then 452 network_pass3 453fi 454 455# Check the password temp/lock file 456# 457if [ -e /etc/ptmp ]; then 458 logger -s -p auth.err \ 459 "password file may be incorrect -- /etc/ptmp exists" 460fi 461 462case ${accounting_enable} in 463[Yy][Ee][Ss]) 464 if [ -d /var/account ]; then 465 echo 'Turning on accounting:' 466 if [ ! -e /var/account/acct ]; then 467 touch /var/account/acct 468 fi 469 accton /var/account/acct 470 fi 471 ;; 472esac 473 474# Make shared lib searching a little faster. Leave /usr/lib first if you 475# add your own entries or you may come to grief. 476# 477ldconfig="/sbin/ldconfig" 478case ${ldconfig_insecure} in 479[Yy][Ee][Ss]) 480 ldconfig="${ldconfig} -i" 481 ;; 482esac 483if [ -x /sbin/ldconfig ]; then 484 case `/usr/bin/objformat` in 485 elf) 486 _LDC=/usr/lib 487 for i in ${ldconfig_paths}; do 488 if [ -d "${i}" ]; then 489 _LDC="${_LDC} ${i}" 490 fi 491 done 492 echo 'ELF ldconfig path:' ${_LDC} 493 ${ldconfig} -elf ${_LDC} 494 ;; 495 esac 496 497 # Legacy aout support for i386 only 498 case `sysctl -n hw.machine` in 499 i386) 500 # Default the a.out ldconfig path. 501 : ${ldconfig_paths_aout=${ldconfig_paths}} 502 _LDC=/usr/lib/aout 503 for i in ${ldconfig_paths_aout}; do 504 if [ -d "${i}" ]; then 505 _LDC="${_LDC} ${i}" 506 fi 507 done 508 echo 'a.out ldconfig path:' ${_LDC} 509 ${ldconfig} -aout ${_LDC} 510 ;; 511 esac 512fi 513 514# Now start up miscellaneous daemons that don't belong anywhere else 515# 516echo -n 'Starting standard daemons:' 517case ${inetd_enable} in 518[Nn][Oo]) 519 ;; 520*) 521 echo -n ' inetd'; inetd ${inetd_flags} 522 ;; 523esac 524 525case ${cron_enable} in 526[Nn][Oo]) 527 ;; 528*) 529 echo -n ' cron'; ${cron_program:-/usr/sbin/cron} ${cron_flags} 530 ;; 531esac 532 533case ${lpd_enable} in 534[Yy][Ee][Ss]) 535 echo -n ' printer'; ${lpd_program:-/usr/sbin/lpd} ${lpd_flags} 536 ;; 537esac 538 539case ${sendmail_enable} in 540[Yy][Ee][Ss]) 541 if [ -r /etc/mail/sendmail.cf ]; then 542 echo -n ' sendmail'; /usr/sbin/sendmail ${sendmail_flags} 543 fi 544 ;; 545esac 546 547case ${sshd_enable} in 548[Yy][Ee][Ss]) 549 if [ -x ${sshd_program:-/usr/sbin/sshd} ]; then 550 echo -n ' sshd'; 551 ${sshd_program:-/usr/sbin/sshd} ${sshd_flags} 552 fi 553 ;; 554esac 555 556case ${usbd_enable} in 557[Yy][Ee][Ss]) 558 echo -n ' usbd'; /usr/sbin/usbd ${usbd_flags} 559 ;; 560esac 561 562echo '.' 563 564# Recover vi editor files. 565find /var/tmp/vi.recover ! -type f -a ! -type d -delete 566vibackup=`echo /var/tmp/vi.recover/vi.*` 567if [ "${vibackup}" != '/var/tmp/vi.recover/vi.*' ]; then 568 echo -n 'Recovering vi editor sessions:' 569 for i in /var/tmp/vi.recover/vi.*; do 570 # Only test files that are readable. 571 if [ ! -r "${i}" ]; then 572 continue 573 fi 574 575 # Unmodified nvi editor backup files either have the 576 # execute bit set or are zero length. Delete them. 577 if [ -x "${i}" -o ! -s "${i}" ]; then 578 rm -f "${i}" 579 fi 580 done 581 582 # It is possible to get incomplete recovery files, if the editor 583 # crashes at the right time. 584 virecovery=`echo /var/tmp/vi.recover/recover.*` 585 if [ "${virecovery}" != "/var/tmp/vi.recover/recover.*" ]; then 586 for i in /var/tmp/vi.recover/recover.*; do 587 # Only test files that are readable. 588 if [ ! -r "${i}" ]; then 589 continue 590 fi 591 592 # Delete any recovery files that are zero length, 593 # corrupted, or that have no corresponding backup file. 594 # Else send mail to the user. 595 recfile=`awk '/^X-vi-recover-path:/{print $2}' < "${i}"` 596 if [ -n "${recfile}" -a -s "${recfile}" ]; then 597 sendmail -t < "${i}" 598 else 599 rm -f "${i}" 600 fi 601 done 602 fi 603 echo '.' 604fi 605 606# Make a bounds file for msgs(1) if there isn't one already 607# 608if [ -d /var/msgs -a ! -f /var/msgs/bounds -a ! -L /var/msgs/bounds ]; then 609 echo 0 > /var/msgs/bounds 610fi 611 612case ${update_motd} in 613[Nn][Oo] | '') 614 ;; 615*) 616 if T=`mktemp /tmp/_motd.XXXXXX`; then 617 uname -v | sed -e 's,^\([^#]*\) #\(.* [1-2][0-9][0-9][0-9]\).*/\([^\]*\) $,\1 (\3) #\2,' > ${T} 618 awk '{if (NR == 1) {if ($1 == "FreeBSD") {next} else {print "\n"$0}} else {print}}' < /etc/motd >> ${T} 619 cmp -s ${T} /etc/motd || { 620 cp ${T} /etc/motd 621 chmod 644 /etc/motd 622 } 623 rm -f ${T} 624 fi 625 ;; 626esac 627 628# Configure implementation specific stuff 629# 630arch=`uname -m` 631if [ -r /etc/rc.${arch} ]; then 632 . /etc/rc.${arch} 633fi 634 635# Configure the system console 636# 637if [ -r /etc/rc.syscons ]; then 638 . /etc/rc.syscons 639fi 640 641# Run rc.devfs if readable to customize devfs 642# 643if [ -r /etc/rc.devfs ]; then 644 sh /etc/rc.devfs 645fi 646 647echo -n 'Additional ABI support:' 648 649# Load the SysV IPC API if requested. 650case ${sysvipc_enable} in 651[Yy][Ee][Ss]) 652 echo -n ' sysvipc' 653 kldload sysvmsg >/dev/null 2>&1 654 kldload sysvsem >/dev/null 2>&1 655 kldload sysvshm >/dev/null 2>&1 656 ;; 657esac 658 659# Start the Linux binary compatibility if requested. 660# 661case ${linux_enable} in 662[Yy][Ee][Ss]) 663 echo -n ' linux' 664 if ! kldstat -v | grep -E 'linux(aout|elf)' > /dev/null; then 665 kldload linux > /dev/null 2>&1 666 fi 667 if [ -x /compat/linux/sbin/ldconfig ]; then 668 /compat/linux/sbin/ldconfig 669 fi 670 ;; 671esac 672 673# Start the SysVR4 binary emulation if requested. 674# 675case ${svr4_enable} in 676[Yy][Ee][Ss]) 677 echo -n ' svr4'; kldload svr4 > /dev/null 2>&1 678 ;; 679esac 680 681echo '.' 682 683# Do traditional (but rather obsolete) rc.local file if it exists. If you 684# use this file and want to make it programmatic, source /etc/defaults/rc.conf 685# in /etc/rc.local and add your custom variables to /etc/rc.conf, as 686# shown below. Please do not put local extensions into /etc/rc itself. 687# Use /etc/rc.local 688# 689# ---- rc.local ---- 690# if [ -r /etc/defaults/rc.conf ]; then 691# . /etc/defaults/rc.conf 692# source_rc_confs 693# elif [ -r /etc/rc.conf ]; then 694# . /etc/rc.conf 695# fi 696# 697# ... additional startup conditionals ... 698# ---- rc.local ---- 699# 700if [ -r /etc/rc.local ]; then 701 echo -n 'Starting local daemons:' 702 sh /etc/rc.local 703 echo '.' 704fi 705 706# For each valid dir in $local_startup, search for init scripts matching *.sh 707# 708case ${local_startup} in 709[Nn][Oo] | '') 710 ;; 711*) 712 echo -n 'Local package initialization:' 713 for dir in ${local_startup}; do 714 if [ -d "${dir}" ]; then 715 for script in ${dir}/*.sh; do 716 if [ -x "${script}" ]; then 717 (set -T 718 trap 'exit 1' 2 719 ${script} start) 720 fi 721 done 722 fi 723 done 724 echo '.' 725 ;; 726esac 727 728if [ -n "${network_pass3_done}" ]; then 729 network_pass4 730fi 731 732# Raise kernel security level. This should be done only after `fsck' has 733# repaired local file systems if you want the securelevel to be greater than 1. 734# 735case ${kern_securelevel_enable} in 736[Yy][Ee][Ss]) 737 if [ "${kern_securelevel}" -ge 0 ]; then 738 echo 'Raising kernel security level: ' 739 sysctl -w kern.securelevel=${kern_securelevel} 740 fi 741 ;; 742esac 743 744echo '' 745 746date 747 748exit 0 749 750