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

📄 sysv init 2.6 的开机过程.htm

📁 很好的参考书
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>SysV init 2</title>
<meta name="GENERATOR" content="Microsoft FrontPage 3.0">
<meta name="Microsoft Border" content="none">
</head>

<body background="../images/waybkgnd.gif" text="#C0C0C0" link="#00FF00" vlink="#C0C0C0">
<font SIZE="2">

<p></font><font size="6" color="#0080FF"><u><strong>SysV init 2.6 的开机过程 </strong></u></font></p>

<pre>发信人: Hanky@FruitTea1 (骗谁啊?), 信区: Linux
标  题: SysV init 2.6 的开机过程
发信站: 果茶小站 (Wed May 29 19:38:46 1996)</pre>

<pre>首先我们先讲一下为什麽是 sysv init 2.6:
因为很多 distributions 用了有问题的 2.5x 版......
所以我们以 Linux 为平台, 讲 2.6 的。</pre>

<pre>这样开场白好像怪怪的, 先不管它。</pre>

<pre>一般来说, 系统在跑完 kernel bootstrapping 後, 就去跑 init
这个『万process之父』, 有了它, 才能开始生小孩......
另外 Linux 有两个 kernel 类的 process 也开始跑了起来,
一个是 kflushd, 另一个是 kswapd:</pre>

<pre>        * process ID 1: init
        * process ID 2: kflushd
        * process ID 3: kswapd</pre>

<pre>只有这个 init 是完全属於 user 类的 process, 後两者是 kernel
假藉 process 之名挂在排程上。</pre>

<pre>init 一开始就去读 /etc/inittab, 这个 inittab 中对於各个
runlevel 要跑哪些 rc 或 spawn 出什麽有很清楚的设定。</pre>

<pre>--------[/etc/inittab □例档]---------------------------------</pre>

<pre># /etc/inittab: init(8) configuration.
# $Id: inittab,v 1.4 1996/03/10 11:47:55 miquels Exp $</pre>

<pre># The default runlevel, 我们设为 3; runlevel 的概念我们等会儿提。
id:3:initdefault:</pre>

<pre># 开机的 system configuration/initialization script.
# This is run first except when booting in emergency (-b) mode.
si::sysinit:/etc/init.d/boot</pre>

<pre># What to do in single-user mode. sulogin 即为 Single User LOGIN.
~~:S:wait:/sbin/sulogin</pre>

<pre># /etc/init.d executes the S and K scripts upon change
# of runlevel. 其中 /etc/init.d/rc 是一个 shell script,
# 後面的 0~6 参数表示要跑该 runlevel 所应跑的设定 script.
# Runlevel 0 is halt.
# Runlevel 1 is single-user.
# Runlevels 2-5 are multi-user.
# Runlevel 6 is reboot.</pre>

<pre>l0:0:wait:/etc/init.d/rc 0
l1:1:wait:/etc/init.d/rc 1
l2:2:wait:/etc/init.d/rc 2
l3:3:wait:/etc/init.d/rc 3
l4:4:wait:/etc/init.d/rc 4
l5:5:wait:/etc/init.d/rc 5
l6:6:wait:/etc/init.d/rc 6</pre>

<pre># 当 CTRL-ALT-DEL 按下去了, 该做什麽?一般都是 shutdown -r now
ca:12345:ctrlaltdel:/sbin/shutdown -t1 -r now
#                                 ^^^^^先等个一秒好了</pre>

<pre># Action on special keypress (ALT-UpArrow). 这个我暂时查不到, 抱歉。
kb::kbrequest:/bin/echo &quot;Keyboard Request--edit /etc/inittab to let this work.&quot;</pre>

<pre># What to do when the power fails/returns. 这个我晚一点讲 (有关 UPS 的部份)
pf::powerwait:/etc/init.d/genpowerfail start
#pn::powerfailnow:/etc/init.d/genpowerfail now
po::powerokwait:/etc/init.d/genpowerfail stop
#pg::powerokwait:/etc/init.d/genpowerfail stop</pre>

<pre># /sbin/getty invocations for the runlevels.
# ^^^^^^^^^^^开 console 出来了。
# The &quot;id&quot; field MUST be the same as the last
# characters of the device (after &quot;tty&quot;).
# 例如在 runlevel==3 时, 会有六个 virtual console (tty1~tty6)
# Format:
#  &lt;id&gt;:&lt;runlevels&gt;:&lt;action&gt;:&lt;process&gt;
1:2345:respawn:/sbin/agetty 19200 tty1
2:23:respawn:/sbin/agetty 19200 tty2
3:23:respawn:/sbin/agetty 19200 tty3
4:23:respawn:/sbin/agetty 19200 tty4
5:23:respawn:/sbin/agetty 19200 tty5
6:23:respawn:/sbin/agetty 19200 tty6</pre>

<pre>----------[End of /etc/inittab]---------------------------------</pre>

<pre>上面的设定, 令系统在一跑完 kernel bootstrapping 後, 就去执
行 /etc/init.d/boot 这个 shell script, 如果没什麽问题, 就
进入 default runlevel, runlevel 在 sysv 通常是有如下意义:</pre>

<pre>0: halt (只有 shutdown, 不 reboot 了)
1: Single-user
2: Multi-user
3: Multi-user &amp; 把 network resource export 出来
4: 除了有 Multi-user &amp; export network resource, 一般来讲是留给 xdm
5: 不太用到了.
6: reboot (shutdown 後会 reboot)</pre>

<pre>其中 2-5 都是 multi-user 的 runlevel, 通常 runlevel(2~5)
越高, 所提供的服务也就越多。当系统资源有所变动 (例如电力)
时, 我们可以用 telinit 去告知 init 要变换 runlevel (例如
原来是 runlevel=3, 用 telinit 2 使 runlevel 降为 2), 这
样子可以关掉一些网路资源服务; 或例如 telinit S 与 telinit 1
都是到 Single-user mode (但前者不同的是, telinit S 根本
就直接在 /dev/console 上执行一个 /bin/sh 给你用; 後者会
去执行 /etc/init.d/rc 1 这个指令); telinit 6 就等於 reboot.</pre>

<pre>OK, 有问题请先提出来。</pre>

<pre>下面我们要讲到的是 /etc/init.d/boot 这个 script 应该做些
什麽, 才是我们要的。
</pre>

<pre>再来我们讲一下 /etc/init.d/boot 这个 script.</pre>

<pre>既然 kernel bootstrapping 完, 我们就要开始做一些
很基本的检查、设定, 以及做一些『准备工作』。</pre>

<pre>在 kernel bootstrapping 後, 在 mount root as read-only
前会先做一些工作, 以 Linux 为例:</pre>

<pre>        * 先设定这个 script 的 PATH 及 umask
        * 挂入 kerneld 这个 daemon (这个和 kernel modules 有关, 後面再讲)
        * mdadd -ar, 把 md device 跑起来 (这也是後面再讲)
        * swapon -a, 把所有的 swap partition 打开来用.
        * 挂入 update (bdflush) 这个 daemon</pre>

<pre>kerneld 是 Linux kernel 1.3.xx 有了 modules 化後,
一个会自动插 modules 进 kernel 的 daemon, 也会把
经一段时间後不曾用到的 modules 拔出 kernel。 有关
modules 的概念, 我们等到 kernel 的介绍再谈详细的。</pre>

<pre>md device 是 Linux kernel 1.3.69 後新加入的功能,
它可以把两个以上的 partition 合成一个大的 md device
之後, 直接做出 file system 或 swap space, 而且可以
『交错地』安排 block 位置, 这就像 RAID-0 一样, 所以
不但可以将一堆小的 partition 合成大的来用, 也可以增
进速度。</pre>

<pre>update (bdflush) 这个 daemon 是每隔一段时间 (预设值
是 5 秒) 就把 'dirty blocks' flush 回 disk 中. 这个
一定要在跑 fsck 等主要的 I/O 动作前就先挂入的了。</pre>

<pre>好, 最基本的准备完毕後, 我们就要先来 fsck 了。
首先是把『根』mount 起来, 而且是 read-only:</pre>

<pre>        * mount -n -o remount,ro /</pre>

<pre>其中 -n 的参数是不把 mount 的动作写入 /etc/mtab 中,
因为现在是把 '/' mount 成 read-only, 根本不能写入。
然後我们开始 fsck:</pre>

<pre>        * fsck -A -a</pre>

<pre>参数 -A 是对 /etc/fstab 中的东西全部 check 一次, -a
的参数是指 auto-repair. 在检查後如果有东西实在是不能
修好, 就会执行 sulogin, 然後 reboot.  如果正常, 那就
把 '/' remount 成 read-write:</pre>

<pre>        * mount -n -o remount,rw /</pre>

<pre>因为後面还会 mount -a, 所以这次我们还是用了 -n 参数。</pre>

<pre>接下来是跑一些『当 '/' 可以 write 了』之後, 立刻要做的
事:</pre>

<pre>        * 跑 modules 的设定
        * 把一些 /etc 及 / 下的档案清除
        * 更新 psdatabase</pre>

<pre>□例如下:</pre>

<pre>----------[/etc/init.d/boot 部份内容]-------------------------
# 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>上面的东西, 我想大家大概都看得懂......</pre>

<pre>好, 再来就是把所有的 local partitions 都给它 mount 起来.</pre>

<pre>        * mount -avt nonfs</pre>

<pre>那为何是 -t nonfs 呢? 很简单, 因为我们还没开始设定 network.
然後, 如果我们有一些 swap file 是在 mount -a 後才出现的,
这时就要再跑一次 swapon:</pre>

<pre>        * swapon -a 2&gt;/dev/null</pre>

<pre>才会把 swap file 开来用。
OK, 然後设定网路 (这时是去叫用一个独立的 script,
如果这个 script 不存在, 我们就无法设定 network)
及主机名称, 然後再 mount -a -t nfs 来加挂人家 export 出来的 fs.</pre>

<pre>------[□例如下]------------------------------------------------
if [ -x /etc/init.d/network ]
then
  /etc/init.d/network
fi
# 然後设定 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>好, 这时才把所有的 file system(含 nfs) 都 mount 起来了,
所以现在立刻要做的事, 就是更新 /etc/ld.so.cache 这个档,
设定 system clock, 然後清除 /tmp, /var/run 及 /var/lock
下的大部份垃圾:</pre>

<pre>        * /sbin/ldconfig
        * clock -s
        * 清除 /tmp, /var/run, /var/lock 下的垃圾</pre>

<pre>OK, /tmp, /var/run 及 /var/lock 这些目录下的垃圾都清空了,
这时才去执行 /etc/rc.boot/ 下的所有 script (其中 run-parts
是一个工具程式, 它会把你给的参数[目录]下所有的 scripts 都
给它跑个一次):</pre>

<pre>        * run-parts /etc/rc.boot</pre>

<pre>如果没有 run-parts 这个工具, 不妨自己学著用 shell script
写一个; 或是用笨方法: 还是乖乖地写在这个 /etc/init.d/boot
script 内吧......</pre>

<pre>然後修改 /dev/ttyXX 的属性 (关於 pty256 容後再介绍):</pre>

<pre>        * chmod 666 /dev/tty[pqrstuvwxyzabcde]*
        * chown root.tty /dev/tty[pqrstuvwxyzabcde]*</pre>

<pre>再来就看你还有什麽事打算在这儿就先处理掉的, 也一并在此写入,
或是写个 script 丢到 /etc/rc.boot/ 下也是一样的。 例如把
powerd 跑起来、建立 /etc/motd、建立 /etc/issue.net、建立一
些 links......都是不错的主意。</pre>

<pre>以下是我的 /etc/init.d/boot 这个 script:</pre>

<pre>[附件一]
</pre>

<pre>PATH=&quot;/sbin:/bin:/usr/sbin:/usr/bin&quot;
umask 022</pre>

<pre>echo
echo &quot;Running /etc/init.d/boot...&quot;
echo</pre>

<pre># enable kerneld
if [ -x /sbin/kerneld ]; then
  /sbin/kerneld
fi</pre>

<pre># 把 md 跑起来
if [ -s /etc/mdtab -a -f /sbin/mdadd ]
then
  mdadd -ar
fi</pre>

<pre>echo &quot;Activating swap...&quot;
swapon -a 2&gt;/dev/null</pre>

<pre># Ensure that bdflush (update) is running before any major I/O is
# performed (the following is a good example of such activity :).
update &amp;</pre>

<pre># Check the integrity of all file systems (if not a fastboot).
if [ ! -f /fastboot ]
then
  # Ensure that root is quiescent and read-only before fsck'ing.
  mount -n -o remount,ro /
  if [ $? = 0 ]
  then
    echo &quot;Checking file systems...&quot;
    fsck -A -a

⌨️ 快捷键说明

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