📄 common-sys-v-init.html
字号:
<br># Surprise! Re-directing from a HERE document (as in
<br># "cat << EOF") won't work, because the root is read-only.
<br>echo
<br>echo "fsck failed. Please repair manually and reboot. Please note"
<br>echo "that the root file system is currently mounted read-only. To"
<br>echo "remount it read-write:"
<br>echo
<br>echo " bash# mount -n -o remount,rw /"
<br>echo
<br>echo "CONTROL-D will reboot the system."
<br>echo
<br># Start a single user shell on the console
<br>/sbin/sulogin /dev/console
<br>sync
<br>reboot
<br>fi
<br>echo
<br>else
<br>echo "*** ERROR! Cannot fsck because root is not read-only!"
<br>echo
<br>fi
<br>else
<br>echo "*** Fast boot ... skipping disk checks"
<br>echo
<br>fi
<p># Remount the root file system in read-write mode.
<br>mount -n -o remount,rw /
<p># Load the appropriate modules.
<br>if [ -x /etc/init.d/modules ]
<br>then
<br>/etc/init.d/modules
<br>fi
<p># Remove /etc/mtab*, /etc/rmtab, /etc/nologin and /fastboot.
<br>rm -f /etc/mtab* /etc/nologin /fastboot /etc/rmtab
<p># update /etc/psdatabase
<br>psupdate 2> /dev/null
<br># or
<br>#ps -U 2> /dev/null
<p># Mount local file systems in /etc/fstab.
<br>echo "Mounting local file systems..."
<br>mount -avt nonfs
<p># Execute swapon command again, in case we want to swap to
<br># a file on a now mounted filesystem.
<br>swapon -a 2>/dev/null
<p># Setup the network interfaces. Note that /var/run and /var/lock
<br># are cleaned up after this, so don't put anything in the "network"
<br># script that leave a pidfile or a lockfile.
<br>if [ -x /etc/init.d/network ]
<br>then
<br>/etc/init.d/network
<br>fi
<p># Set hostname.
<br># If there's no /etc/HOSTNAME, fall back on this default:
<br>if [ ! -r /etc/HOSTNAME ]; then
<br>echo "Henry.Dorm10.NCTU.edu.tw" > /etc/HOSTNAME
<br>fi
<br>cat /etc/HOSTNAME | cut -f1 -d . > /etc/hostname
<br>hostname --file /etc/hostname
<p># Now that TCP/IP is configured, mount the NFS file systems in /etc/fstab.
<br>echo "Mounting remote file systems..."
<br>mount -a -t nfs
<p># Update all the shared library links automatically
<br>echo "Update /etc/ld.so.cache and all the shared library links."
<br>/sbin/ldconfig
<p># Set GMT="-u" if your system clock is set to GMT, and GMT=""
<br># if not.
<br>GMT=""
<br># Set and adjust the CMOS clock.
<br>clock -s $GMT
<br>if [ ! -f /etc/adjtime ]
<br>then
<br>echo "0.0 0 0.0" > /etc/adjtime
<br>fi
<br>clock -a $GMT
<p># Now that /usr/lib/zoneinfo should be available, announce the local
time.
<br>echo
<br>echo "Local time: `date`"
<br>echo
<p># Wipe /tmp (and don't erase `lost+found', `quota.user' or `quota.group')!
<br># Note that files _in_ lost+found _are_ deleted.
<br>echo -n "Cleaning up /tmp, /var/run and /var/lock... "
<br>( cd /tmp && \
<br>find . \
<br>! -name .\
<br>! \( -name lost+found -uid 0 \) \
<br>! \( -name quota.user -uid 0 \) \
<br>! \( -name quota.group -uid 0 \) \
<br>-depth -exec rm -rf -- {} \; )
<br># Clean up any stale locks.
<br>( cd /var/lock && find . -type f -exec rm -f -- {} \; )
<br># Clean up /var/run and create /var/run/utmp so that we can login.
<br>( cd /var/run && find . ! -type d -exec rm -f -- {} \; )
<br>: > /var/run/utmp
<br>echo "done."
<p># Run the package-specific boot scripts in /etc/rc.boot.
<br>run-parts /etc/rc.boot
<br># Set pseudo-terminal access permissions.
<br>chmod 666 /dev/tty[pqrstuvwxyzabcde]*
<br>chown root.tty /dev/tty[pqrstuvwxyzabcde]*
<p># Setup the /etc/issue.net to reflect the current kernel level:
<br>cat /etc/issue > /etc/issue.net
<br>uname -a >> /etc/issue.net
<br>echo >> /etc/issue.net
<p>touch /etc/motd
<p># and startup powerd
<br>echo "Start up the genpowerd" ; /sbin/genpowerd /dev/UPS henry
<p>--------------------- --------------------- ---------------------
<br>这下我们的 /etc/init.d/boot 跑完了, 依照 /etc/inittab
<br>的设定, 是跑 runlevel=3, 执行 /etc/init.d/rc 3 这个指令。
<p>/etc/init.d/rc 是一个不错 (聪明?) 的 shell script,
<br>我们来介绍一下它的运作方式。
<p>在 SysV init 2.6 所用的 /etc/ 下, 除了 init.d/ 及
<br>boot/ 这两个子目录外, 尚有:
<p>drwxr-xr-x 2 root 1024 May 28 09:08 rc0.d/
<br>drwxr-xr-x 2 root 1024 May 28 09:08 rc1.d/
<br>drwxr-xr-x 2 root 1024 May 29 09:54 rc2.d/
<br>drwxr-xr-x 2 root 1024 May 28 09:08 rc3.d/
<br>drwxr-xr-x 2 root 1024 May 28 09:08 rc4.d/
<br>drwxr-xr-x 2 root 1024 May 28 09:08 rc5.d/
<br>drwxr-xr-x 2 root 1024 May 28 09:08 rc6.d/
<p>这七个子目录, 我们以 rc3.d 来作示范:
<p># dir rc3.d/
<br>S20cron -> ../init.d/cron*
<br>S20gpm -> ../init.d/gpm*
<br>S20httpd -> ../init.d/httpd*
<br>S20innbbsd -> ../init.d/innbbsd*
<br>S20ip_acct -> ../init.d/ip_acct*
<br>S20lpd -> ../init.d/lpd*
<br>S20netbase -> ../init.d/netbase*
<br>S20netstd_misc -> ../init.d/netstd_misc*
<br>S20nfs -> ../init.d/nfs*
<br>S20quota -> ../init.d/quota*
<br>S30sendmail -> ../init.d/sendmail*
<br>S30syslogd -> ../init.d/syslogd*
<p>这些都是一个一个的 symbolic link 到 /etc/init.d/ 下的 script,
<br>其中 S20 是一种控制说明, S 是表示 start (K 是表示 kill), 20
<br>这个数字表示它们的执行顺序 (20 比 30 先执行, 同数字则照字母
<br>顺序即可)。
<p>/etc/init.d/rc 就是照给定的 runlevel 数字, 去对应的目录看这些
<br>东西来决定要跑那些 script.
<p>那么, init.d/ 下的那些 script 又是怎么回事呢?
<br>我们来看一下好了:
<p>root@Henry:/etc# dir init.d/
<br>total 45
<br>-rwxr-xr-x 1 root 5231 May 28 17:47 boot*
<br>-rwxr-xr-x 1 root 290 May 28 09:08 cron*
<br>-rwxr-xr-- 1 root 2250 May 29 01:29 genpowerfail*
<br>-rwxr-xr-x 1 root 283 May 28 06:41 gpm*
<br>-rwxr-xr-x 1 root 707 Feb 29 20:17 halt*
<br>-rwxr-xr-x 1 root 718 May 28 06:27 httpd*
<br>-rwxr-xr-x 1 root 494 May 28 07:05 innbbsd*
<br>-rwxr-x--- 1 root 333 May 28 07:12 ip_acct*
<br>-rwxr-xr-x 1 root 343 May 28 06:35 lpd*
<br>-rwxr-xr-x 1 root 500 May 28 05:25 modules*
<br>-rwxr-xr-x 1 root 699 May 28 06:12 netbase*
<br>-rwxr-xr-x 1 root 391 Mar 19 10:32 netstd_init*
<br>-rwxr-xr-x 1 root 598 May 29 09:54 netstd_misc*
<br>-rwxr-xr-x 1 root 1372 May 29 10:05 network*
<br>-rwxr-xr-x 1 root 1208 May 28 05:36 nfs*
<br>-rwxr-xr-x 1 root 1258 Dec 28 08:02 powerfail*
<br>-rwxr-x--- 1 root 891 May 28 06:45 quota*
<br>-rwxr-xr-x 1 root 2928 Jan 4 19:59 rc*
<br>-rwxr-xr-x 1 root 653 Feb 29 20:17 reboot*
<br>-rwxr-xr-x 1 root 696 May 28 07:34 sendmail*
<br>-rwxr-xr-x 1 root 527 Mar 20 00:44 single*
<br>-rwxr-xr-x 1 root 1078 Dec 28 08:21 skeleton*
<br>-rwxr-xr-x 1 root 640 May 29 08:21 syslogd*
<p>呵呵, 没想到有这么多吧?一个提供比较多服务的工作站, 大概
<br>就需要这些。 除了 boot, rc, network, modules 是我们介绍
<br>过的之外, 其他都是给 /etc/rc[0-6].d/ 做 symbolic link 用
<br>去了。
<p>OK, 我叫一个 script 出来给大家看:
<p>-------[/etc/init.d/netbase]----------------------------
<br>#!/bin/sh
<br>#
<br># Start networking daemons.
<p>test -f /usr/sbin/rpc.portmap || exit 0
<p>case "$1" in
<br>start)
<br>echo -n "Starting base networking daemons: "
<br>echo -n "rpc.portmap "
<br>start-stop-daemon --start --quiet --exec /usr/sbin/rpc.portmap
<br>echo -n "xinetd "
<br>start-stop-daemon --start --quiet --exec /usr/sbin/xinetd
<br>echo
<br>;;
<br>stop)
<br>start-stop-daemon --stop --quiet --oknodo --exec /usr/sbin/xinetd
<br>start-stop-daemon --stop --quiet --oknodo --exec /usr/sbin/rpc.portmap
<br>killall -9 slattach 2>/dev/null || exit 0
<br>;;
<br>reload)
<br>start-stop-daemon --stop --oknodo --signal 10 --exec /usr/sbin/xinetd
<br>;;
<br>*)
<br>echo "Usage: /etc/init.d/netbase {start|stop|reload}"
<br>exit 1
<br>;;
<br>esac
<p>exit 0
<p>-------[end]-----------------------------------------------------------------
<p>其中 start-stop-daemon 是一个 perl script, 参数 --start 是跑它去启动
<br>那个 daemon (例如 xinetd), 参数 --stop 是叫它杀掉那个 daemon。 一般
<br>而言, 如果给的 signal 对的话 (预设值是 9, SIGKILL), 尚可使该 daemon
<br>reload 一次它的配置文件 (以 xinetd 为例是 10, SIGUSR1), 而通常都是给
<br>1, SIGHUP。
<p>S20netbase 这个 symbolic link 会使得 /etc/init.d/rc 去启动它, 也就是
<br>跑 '/etc/init.d/netbase start', 使得 rpc.portmap 及 xinetd 执行起来,
<br>就达到我们所要求的。
<p>其他如 lpd、gpm、httpd、nfs、也是如此。 补充一点的是, nfs 类的 daemon
<br>一定得在 rpc.portmap 跑起来之后才能正确动作 (因为 nfs 也是一种 rpc),
<br>但在此 S20netbase 会比 S20nfs 先跑 (考虑字母顺序), 所以看起来没有什么
<br>问题。 注意一下执行顺序是很重要的事。
<p>在此再补充一点, debian 有一个工具程序 update-rc.d 很有用:
<p>-----------[man update-rc.d]-------------------------------------
<br>NAME
<br>update-rc.d - install and remove System-V style init
<br>script links
<p>SYNOPSIS
<br>update-rc.d remove
<p>update-rc.d defaults [ | denumber> ]
<p>update-rc.d start | stop level> [ [ [...]]] .
<p>----------[end of part]------------------------------------------
<p>它会帮助你把新造好的 script 加入 /etc/rc[0-6].d/ 中的 symbolic
<br>link. 如果是 defaults, 则在 rc[2-5].d/ 下造 S20xxxxx, rc[016].d/
<br>下造 K20xxxxx 的 symbolic link, 不错吧?
<p>好, /etc/init.d/rc 3 也跑完了, 再来就是开 console
<br>了。 依照 inittab 的设定, 是在 tty1 ~ tty6 开六个
<br>virtual console, 所以我们可以用 atl-f1 到 f6 来切
<br>换 virtual console 的操作。 login 之后, 我们会看
<br>到下面的 process:
<p>root@Henry:/etc# ps ax |grep getty
<br>165 v02 S 0:00 /sbin/agetty 19200 tty2
<br>166 v03 S 0:00 /sbin/agetty 19200 tty3
<br>167 v04 S 0:00 /sbin/agetty 19200 tty4
<br>168 v05 S 0:00 /sbin/agetty 19200 tty5
<br>169 v06 S 0:00 /sbin/agetty 19200 tty6
<p>因为我们已经 login 了, 用掉 tty1 了, 所以看不到 tty1
<br>在跑 agetty.
<p>有关 SysV init 2.6 版就先讲到这里, 大家有没有什么问题?
<p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -