⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sysv init 2.6 的开机过程.htm

📁 很好的参考书
💻 HTM
📖 第 1 页 / 共 2 页
字号:
    # If there was a failure, drop into single-user mode.
    #
    # NOTE: "failure" is defined as exiting with a return code of
    # 2 or larger.  A return code of 1 indicates that file system
    # errors were corrected but that the boot may proceed.
    if [ $? -gt 1 ]
    then
      # Surprise! Re-directing from a HERE document (as in
      # "cat << EOF") won't work, because the root is read-only.
      echo
      echo "fsck failed.  Please repair manually and reboot.  Please note"
      echo "that the root file system is currently mounted read-only.  To"
      echo "remount it read-write:"
      echo
      echo "   bash# mount -n -o remount,rw /"
      echo
      echo "CONTROL-D will reboot the system."
      echo
      # Start a single user shell on the console
      /sbin/sulogin /dev/console
      sync
      reboot
    fi
    echo
  else
    echo "*** ERROR!  Cannot fsck because root is not read-only!"
    echo
  fi
else
  echo "*** Fast boot ... skipping disk checks"
  echo
fi</pre>

<pre># Remount the root file system in read-write mode.
mount -n -o remount,rw /</pre>

<pre># Load the appropriate modules.
if [ -x /etc/init.d/modules ]
then
  /etc/init.d/modules
fi</pre>

<pre># Remove /etc/mtab*, /etc/rmtab, /etc/nologin and /fastboot.
rm -f /etc/mtab* /etc/nologin /fastboot /etc/rmtab</pre>

<pre># update /etc/psdatabase
psupdate 2&gt; /dev/null
# or
#ps -U 2&gt; /dev/null</pre>

<pre># Mount local file systems in /etc/fstab.
echo &quot;Mounting local file systems...&quot;
mount -avt nonfs</pre>

<pre># Execute swapon command again, in case we want to swap to
# a file on a now mounted filesystem.
swapon -a 2&gt;/dev/null</pre>

<pre># Setup the network interfaces. Note that /var/run and /var/lock
# are cleaned up after this, so don't put anything in the &quot;network&quot;
# script that leave a pidfile or a lockfile.
if [ -x /etc/init.d/network ]
then
  /etc/init.d/network
fi</pre>

<pre># Set hostname.
# If there's no /etc/HOSTNAME, fall back on this default:
if [ ! -r /etc/HOSTNAME ]; then
   echo &quot;Henry.Dorm10.NCTU.edu.tw&quot; &gt; /etc/HOSTNAME
fi
cat /etc/HOSTNAME | cut -f1 -d . &gt; /etc/hostname
hostname --file /etc/hostname</pre>

<pre># Now that TCP/IP is configured, mount the NFS file systems in /etc/fstab.
echo &quot;Mounting remote file systems...&quot;
mount -a -t nfs</pre>

<pre># Update all the shared library links automatically
echo &quot;Update /etc/ld.so.cache and all the shared library links.&quot;
/sbin/ldconfig</pre>

<pre># Set GMT=&quot;-u&quot; if your system clock is set to GMT, and GMT=&quot;&quot;
# if not.
GMT=&quot;&quot;
# Set and adjust the CMOS clock.
clock -s $GMT
if [ ! -f /etc/adjtime ]
then
  echo &quot;0.0 0 0.0&quot; &gt; /etc/adjtime
fi
clock -a $GMT</pre>

<pre># Now that /usr/lib/zoneinfo should be available, announce the local time.
echo
echo &quot;Local time: `date`&quot;
echo</pre>

<pre># Wipe /tmp (and don't erase `lost+found', `quota.user' or `quota.group')!
# Note that files _in_ lost+found _are_ deleted.
echo -n &quot;Cleaning up /tmp, /var/run and /var/lock... &quot;
( cd /tmp &amp;&amp; \
  find . \
  ! -name .\
  ! \( -name lost+found -uid 0 \) \
  ! \( -name quota.user -uid 0 \) \
  ! \( -name quota.group -uid 0 \) \
    -depth -exec rm -rf -- {} \; )
# Clean up any stale locks.
( cd /var/lock &amp;&amp; find . -type f -exec rm -f -- {} \; )
# Clean up /var/run and create /var/run/utmp so that we can login.
( cd /var/run &amp;&amp; find . ! -type d -exec rm -f -- {} \; )
: &gt; /var/run/utmp
echo &quot;done.&quot;</pre>

<pre># Run the package-specific boot scripts in /etc/rc.boot.
run-parts /etc/rc.boot
# Set pseudo-terminal access permissions.
chmod 666 /dev/tty[pqrstuvwxyzabcde]*
chown root.tty /dev/tty[pqrstuvwxyzabcde]*</pre>

<pre># Setup the /etc/issue.net to reflect the current kernel level:
cat /etc/issue &gt; /etc/issue.net
uname -a &gt;&gt; /etc/issue.net
echo &gt;&gt; /etc/issue.net</pre>

<pre>touch /etc/motd</pre>

<pre># and startup powerd
echo &quot;Start up the genpowerd&quot; ; /sbin/genpowerd /dev/UPS henry</pre>

<pre>--------------------- --------------------- ---------------------
这下我们的 /etc/init.d/boot 跑完了, 依照 /etc/inittab
的设定, 是跑 runlevel=3, 执行 /etc/init.d/rc 3 这个指令。</pre>

<pre>/etc/init.d/rc 是一个不错 (聪明?) 的 shell script,
我们来介绍一下它的运作方式。</pre>

<pre>在 SysV init 2.6 所用的 /etc/ 下, 除了 init.d/ 及
boot/ 这两个子目录外, 尚有:</pre>

<pre>drwxr-xr-x   2 root         1024 May 28 09:08 rc0.d/
drwxr-xr-x   2 root         1024 May 28 09:08 rc1.d/
drwxr-xr-x   2 root         1024 May 29 09:54 rc2.d/
drwxr-xr-x   2 root         1024 May 28 09:08 rc3.d/
drwxr-xr-x   2 root         1024 May 28 09:08 rc4.d/
drwxr-xr-x   2 root         1024 May 28 09:08 rc5.d/
drwxr-xr-x   2 root         1024 May 28 09:08 rc6.d/</pre>

<pre>这七个子目录, 我们以 rc3.d 来作□例:</pre>

<pre># dir rc3.d/
S20cron -&gt; ../init.d/cron*
S20gpm -&gt; ../init.d/gpm*
S20httpd -&gt; ../init.d/httpd*
S20innbbsd -&gt; ../init.d/innbbsd*
S20ip_acct -&gt; ../init.d/ip_acct*
S20lpd -&gt; ../init.d/lpd*
S20netbase -&gt; ../init.d/netbase*
S20netstd_misc -&gt; ../init.d/netstd_misc*
S20nfs -&gt; ../init.d/nfs*
S20quota -&gt; ../init.d/quota*
S30sendmail -&gt; ../init.d/sendmail*
S30syslogd -&gt; ../init.d/syslogd*</pre>

<pre>这些都是一个一个的 symbolic link 到 /etc/init.d/ 下的 script,
其中 S20 是一种控制说明, S 是表示 start (K 是表示 kill), 20
这个数字表示它们的执行顺序 (20 比 30 先执行, 同数字则照字母
顺序即可)。</pre>

<pre>/etc/init.d/rc 就是照给定的 runlevel 数字, 去对应的目录看这些
东西来决定要跑那些 script.</pre>

<pre>那麽, init.d/ 下的那些 script 又是怎麽回事呢?
我们来看一下好了:</pre>

<pre>root@Henry:/etc# dir init.d/
total 45
-rwxr-xr-x   1 root         5231 May 28 17:47 boot*
-rwxr-xr-x   1 root          290 May 28 09:08 cron*
-rwxr-xr--   1 root         2250 May 29 01:29 genpowerfail*
-rwxr-xr-x   1 root          283 May 28 06:41 gpm*
-rwxr-xr-x   1 root          707 Feb 29 20:17 halt*
-rwxr-xr-x   1 root          718 May 28 06:27 httpd*
-rwxr-xr-x   1 root          494 May 28 07:05 innbbsd*
-rwxr-x---   1 root          333 May 28 07:12 ip_acct*
-rwxr-xr-x   1 root          343 May 28 06:35 lpd*
-rwxr-xr-x   1 root          500 May 28 05:25 modules*
-rwxr-xr-x   1 root          699 May 28 06:12 netbase*
-rwxr-xr-x   1 root          391 Mar 19 10:32 netstd_init*
-rwxr-xr-x   1 root          598 May 29 09:54 netstd_misc*
-rwxr-xr-x   1 root         1372 May 29 10:05 network*
-rwxr-xr-x   1 root         1208 May 28 05:36 nfs*
-rwxr-xr-x   1 root         1258 Dec 28 08:02 powerfail*
-rwxr-x---   1 root          891 May 28 06:45 quota*
-rwxr-xr-x   1 root         2928 Jan  4 19:59 rc*
-rwxr-xr-x   1 root          653 Feb 29 20:17 reboot*
-rwxr-xr-x   1 root          696 May 28 07:34 sendmail*
-rwxr-xr-x   1 root          527 Mar 20 00:44 single*
-rwxr-xr-x   1 root         1078 Dec 28 08:21 skeleton*
-rwxr-xr-x   1 root          640 May 29 08:21 syslogd*</pre>

<pre>呵呵, 没想到有这麽多吧?一个提供比较多服务的工作站, 大概
就需要这些。  除了 boot, rc, network, modules 是我们介绍
过的之外, 其他都是给 /etc/rc[0-6].d/ 做 symbolic link 用
去了。</pre>

<pre>OK, 我叫一个 script 出来给大家看:</pre>

<pre>-------[/etc/init.d/netbase]----------------------------
#!/bin/sh
#
# Start networking daemons.</pre>

<pre>test -f /usr/sbin/rpc.portmap || exit 0</pre>

<pre>case &quot;$1&quot; in
  start)
        echo -n &quot;Starting base networking daemons: &quot;
        echo -n &quot;rpc.portmap &quot;
        start-stop-daemon --start --quiet --exec /usr/sbin/rpc.portmap
        echo -n &quot;xinetd &quot;
        start-stop-daemon --start --quiet --exec /usr/sbin/xinetd
        echo
        ;;
  stop)
        start-stop-daemon --stop --quiet --oknodo --exec /usr/sbin/xinetd
        start-stop-daemon --stop --quiet --oknodo --exec /usr/sbin/rpc.portmap
        killall -9 slattach 2&gt;/dev/null || exit 0
        ;;
  reload)
        start-stop-daemon --stop --oknodo --signal 10 --exec /usr/sbin/xinetd
        ;;
  *)
        echo &quot;Usage: /etc/init.d/netbase {start|stop|reload}&quot;
        exit 1
        ;;
esac</pre>

<pre>exit 0</pre>

<pre>-------[end]-----------------------------------------------------------------</pre>

<pre>其中 start-stop-daemon 是一个 perl script, 参数 --start 是跑它去启动
那个 daemon (例如 xinetd), 参数 --stop 是叫它杀掉那个 daemon。  一般
而言, 如果给的 signal 对的话 (预设值是 9, SIGKILL), 尚可使该 daemon
reload 一次它的设定档 (以 xinetd 为例是 10, SIGUSR1), 而通常都是给
1, SIGHUP。</pre>

<pre>S20netbase 这个 symbolic link 会使得 /etc/init.d/rc 去启动它, 也就是
跑 '/etc/init.d/netbase start', 使得 rpc.portmap 及 xinetd 执行起来,
就达到我们所要求的。</pre>

<pre>其他如 lpd、gpm、httpd、nfs、也是如此。  补充一点的是, nfs 类的 daemon
一定得在 rpc.portmap 跑起来之後才能正确动作 (因为 nfs 也是一种 rpc),
但在此 S20netbase 会比 S20nfs 先跑 (考虑字母顺序), 所以看起来没有什麽
问题。  注意一下执行顺序是很重要的事。</pre>

<pre>在此再补充一点, debian 有一个工具程式 update-rc.d 很有用:</pre>

<pre>-----------[man update-rc.d]-------------------------------------
NAME
       update-rc.d  -  install  and  remove  System-V  style init
       script links</pre>

<pre>SYNOPSIS
       update-rc.d &lt;basename&gt; remove</pre>

<pre>       update-rc.d &lt;basename&gt; defaults [ &lt;codenumber&gt; | &lt;startco-
       denumber&gt; &lt;stopcodenumber&gt; ]</pre>

<pre>       update-rc.d  &lt;basename&gt;  start  |  stop &lt;codenumber&gt; &lt;run-
       level&gt; [ &lt;runlevel&gt; [ &lt;runlevel&gt; [...]]]  .</pre>

<pre>----------[end of part]------------------------------------------</pre>

<pre>它会帮助你把新造好的 script 加入 /etc/rc[0-6].d/ 中的 symbolic
link. 如果是 defaults, 则在 rc[2-5].d/ 下造 S20xxxxx, rc[016].d/
下造 K20xxxxx 的 symbolic link, 不错吧?</pre>

<pre>好, /etc/init.d/rc 3 也跑完了, 再来就是开 console
了。 依照 inittab 的设定, 是在 tty1 ~ tty6 开六个
virtual console, 所以我们可以用 atl-f1 到 f6 来切
换 virtual console 的操作。  login 之後, 我们会看
到下面的 process:</pre>

<pre>root@Henry:/etc# ps ax |grep getty
  165 v02 S     0:00 /sbin/agetty 19200 tty2
  166 v03 S     0:00 /sbin/agetty 19200 tty3
  167 v04 S     0:00 /sbin/agetty 19200 tty4
  168 v05 S     0:00 /sbin/agetty 19200 tty5
  169 v06 S     0:00 /sbin/agetty 19200 tty6</pre>

<pre>因为我们已经 login 了, 用掉 tty1 了, 所以看不到 tty1
在跑 agetty.</pre>

<pre>有关 SysV init 2.6 版就先讲到这□, 大家有没有什麽问题?</pre>

<pre>接下来要谈到的是 UPS 和 powerd 的连接方式, 我们先休息
十分钟。</pre>

<p><a href="../hkbx!.htm"><strong><big>返回“新客补习”</big></strong></a></p>
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -