Linux启动过程,大致可分为3步
第1步加载BIOS并读取MBR以引导启动kernel;第2步执行init启动各项服务,包括/etc/rc.d/rc.sysinit以及/etc/modprobe.conf等;第3步执行/bin/login等候用户登录
加载kernel
1加载BIOS硬件信息
2读取第一个启动设备的MBR的引导加载程序(lilo/grub/spfdisk)
3加载kernel并解压缩,尝试驱动所有硬件设备
主机启动后第一个读取的地方就是BIOS,其记录了各种硬件信息,系统根据这些信息进行加电自检,成功后开始读取MBR;
MBR包含引导加载程序以便将OS kernel读入内存,对于linux常见的有lilo和grub,而kernel位于/boot目录,
$ strings /boot/vmlinuz-2.6.9-67.EL | head -5
Direct booting from floppy is no longer supported.
Please use a boot loader program instead.
Remove disk and press any key to reboot . . .
.HdrS
ZZuD
执行init
4执行init程序,并由init执行/etc/rc.d/rc.sysinit
5启动外挂模块/etc/modprobe.conf
6执行各个批处理文件
7执行/etc/rc.d/rc.local
加载完kernel后,第一个执行的程序为/sbin/init(其pid为1),而其需要读取/etc/inittab获取具体配置信息
$ ps aux | head -10
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 4756 516 ? S 2012 0:19 init [3]
root 2 0.0 0.0 0 0 ? S 2012 0:22 [migration/0]
root 3 0.0 0.0 0 0 ? SN 2012 0:00 [ksoftirqd/0]
root 4 0.0 0.0 0 0 ? S 2012 0:18 [migration/1]
root 5 0.0 0.0 0 0 ? SN 2012 0:00 [ksoftirqd/1]
root 6 0.0 0.0 0 0 ? S 2012 0:18 [migration/2]
root 7 0.0 0.0 0 0 ? SN 2012 0:00 [ksoftirqd/2]
root 8 0.0 0.0 0 0 ? S 2012 0:15 [migration/3]
root 9 0.0 0.0 0 0 ? SN 2012 0:00 [ksoftirqd/3]
首先获取默认运行等级,然后执行sysinit初始化各种配置,接着依次启动系统服务和用户自定义服务,最后开启终端等待用户登录
$ more /etc/inittab
…
id:3:initdefault:
…
# System initialization.
si::sysinit:/etc/rc.d/rc.sysinit
…
而sysinit做的工作比较多,按照顺序主要如下:
获取网络环境和主机类型,读取/etc/sysconfig/network;
载入/proc以及USB设备/sys;
检测接口设备和PnP,根据kernel启动时检测的结果(/proc/sys/kernel/modprobe)进行IDE/SCSI/网络等设备的检测;
加载自定义模块,/etc/sysconfig/modules/*.modules;
加载kernel相关设置,/etc/sysctl.conf;
设置系统时间clock;
设置RAID/LVM;
调用fsck检测磁盘文件系统;
以可读取模式重载系统磁盘;
清除启动时产生的临时文件;
将相关启动信息写入/var/log/dmesg;
执行完sysinit后接下来要启动各项系统服务,根据如下配置,决定了/etc/rc.d/rcN.d包含程序的运行等级,而这些程序全是指向/etc/rc.d/init.d的软链接
$ more /etc/inittab
…
l0:0:wait:/etc/rc.d/rc 0
l1:1:wait:/etc/rc.d/rc 1
l2:2:wait:/etc/rc.d/rc 2
l3:3:wait:/etc/rc.d/rc 3
l4:4:wait:/etc/rc.d/rc 4
l5:5:wait:/etc/rc.d/rc 5
l6:6:wait:/etc/rc.d/rc 6
…
接下来启动用户自定义服务/etc/rc.d/rc.local,也就是说如果用户想添加随OS自动启动的服务,需要配置该文件
$ more /etc/rc.d/rc.local
#!/bin/sh
#
# This script. will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style. init stuff.
touch /var/lock/subsys/local
启动终端
8 执行/bin/login,等待用户登录
9 登录后以shell控制主机
接下来启动终端或X-window界面等待用户登录
$ more /etc/inittab
…
# Run gettys in standard runlevels
1:2345:respawn:/sbin/mingetty tty1
2:2345:respawn:/sbin/mingetty tty2
3:2345:respawn:/sbin/mingetty tty3
4:2345:respawn:/sbin/mingetty tty4
5:2345:respawn:/sbin/mingetty tty5
6:2345:respawn:/sbin/mingetty tty6
# Run xdm in runlevel 5
x:5:respawn:/etc/X11/prefdm -nodaemon
…
注:/etc/inittab中的wait表示需执行完该命令才能进行下一步,而respawn表示关闭后会自动重其;
/etc/init.d/sysinit总共约1000行,通过sh -x调试
首先读取network信息,然后加载/proc和/sys
18 if [ -f /etc/sysconfig/network ]; then
19 . /etc/sysconfig/network
20 fi
…
26 mount -n -t proc /proc /proc
27 [ -d /proc/bus/usb ] && mount -n -t usbfs /proc/bus/usb /proc/bus/usb
28 mount -n -t sysfs /sys /sys >/dev/null 2>&1
然后设置字体/clock/lang,以下是sh –x调试输出结果
Setting default font (latarcyrheb-sun16): /etc/rc.d/rc.sysinit: line 91: /dev/tty1: Permission denied
[FAILED]
Welcome to Red Hat Enterprise Linux AS
Press 'I' to enter interactive startup.
Sorry, only the superuser can change the Hardware Clock.
Setting clock (localtime): Wed Jan 30 14:06:42 CST 2013 [ OK ]
klogctl: Operation not permitted
接下来执行
169 [ -x /sbin/start_udev ] && /sbin/start_udev
对应调试信息
Starting udev: rm: cannot remove `/dev//.udev.tdb': Permission denied
ln: cannot remove `/dev//fd': Permission denied
ln: cannot remove `/dev//stdin': Permission denied
ln: cannot remove `/dev//stdout': Permission denied
ln: cannot remove `/dev//stderr': Permission denied
ln: cannot remove `/dev//core': Permission denied
MAKEDEV: error making /dev/tty1: Permission denied
接下来配置硬件及kernel参数
Initializing hardware... storage network audio done[ OK ]
然后是文件系统完整性检验
[FAILED]
Your system appears to have shut down uncleanly
Press Y within 1 seconds to force file system integrity check...
Checking root filesystem
fsck.ext3: You must have r/w access to the filesystem or be root
重载root文件系统
481 # Remount the root filesystem read-write.
482 update_boot_stage RCmountfs
…
加载多路径
496 if [ -c /dev/mapper/control ]; then
497 if [ -f /etc/multipath.conf -a -x /sbin/multipath.static ]; then
498 modprobe dm-multipath >/dev/null 2>&1
499 /sbin/multipath.static -v 0
…
初始化随机数生成器
770 # Initialize pseudo-random number generator
771 if [ -f "/var/lib/random-seed" ]; then
772 cat /var/lib/random-seed > /dev/urandom
773 else
774 touch /var/lib/random-seed
775 fi
776 chmod 600 /var/lib/random-seed
777 dd if=/dev/urandom f=/var/lib/random-seed count=1 bs=512 2>/dev/null
…
最后删除临时文件并写入/var/log/dmesg
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/15480802/viewspace-753566/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/15480802/viewspace-753566/