Linux启动过程以简单的步骤解释
本教程描述了详细的Linux引导过程,例如所涉及的阶段,运行哪些脚本,读取哪些配置文件及其订单,因为打开系统的时间直到获取登录提示。
1)BIOS初始化
BIOS或者基本输入输出系统是一个固件程序,它执行与硬件的非常基本的交互级别。
这是第一个在计算机接通时控制的程序。
BIOS对称为POST的所有硬件组件和外围设备或者"自检电源"执行测试。
它初始化用于启动所需的硬件。
在POST成功执行后,BIOS从设备列表查找引导设备。
现代化BIOS允许我们配置BIOS检查的这些设备(有时称为Boot Preference)的顺序。
这些引导设备可以是软盘驱动器,CDROM,硬盘驱动器,网络接口或者其他可移动介质(例如USB闪存驱动器)。
BIOS检查可引导设备上的引导扇区。
引导扇区是存储设备上的第一个物理扇区,并包含引导计算机所需的代码。
2)主启动记录
在硬盘和许多其他大容量存储介质的情况下,引导扇区是MBR。
MBR由硬盘的第一部门的512字节组成。
值得注意的是,MBR不在任何分区内都位于任何分区内。
MBR先于第一分区。
MBR的布局如下:
•前446个字节包含可引导代码。
•接下来64字节包含4个分区的分区信息(16x4)。
这就是硬盘只有4个主要分区的原因,因为MBR可以仅存储4个分区的信息。
因此,如果在硬盘上需要超过4个分区,则必须扩展一个主分区,并且从这些扩展分区中创建逻辑分区。
•最后2个字节适用于MBR签名,也称为Magic Number。
(因此总共446 + 64 + 2 = 512字节)。
| ================= | ====== | ====== | ====== | ====== | === | | 446 | 16 | 16 | 16 | 16 | 2 | | ================= | ====== | ====== | ====== | ====== | === |
第一个446字节的MBR包含从中找到分区的代码。
其余的引导过程从该分区发生。
此分区包含一个名为"bootloader"的软件程序,用于引导系统。
3)关于grub
根据GNU。
ORG,"Bootloader是计算机启动时运行的第一个软件程序"。
GRUB或者Grand Unified Bootloader是Linux等操作系统的引导程序。
有两种主要版本的GRUB可用(GRUB版本1和2)。
目前,大多数Linux Distrs已经开始使用GRUB版本2. GRUB的一个主要特征是它可以使用Linux镜像安装,无需运行操作系统。
GRUB是一个多级引导加载程序(第1级,阶段1. 5和第2阶段)。
下面解释GRUB版本1和版本2的3个阶段。
GRUB版本1阶段
第1阶段可以直接加载阶段2,但通常设置为装载阶段1. 5. GRUB级1.
5位于MBR和第一分区之前的第一千克硬盘中的前30千字节。
如果此空间不可用(异常分区表,特殊磁盘驱动程序,GPT或者LVM磁盘),则安装阶段1.5将失败。
阶段1. 5图像包含文件系统驱动程序。
这使得第1阶段1. 5能够从文件系统中的任何已知位置直接加载阶段2,例如来自/引导/GRUB。
然后,第2阶段将加载默认配置文件和所需的任何其他模块。
GRUB版本2阶段
第1阶段 - >启动。
IMG存储在MBR中(或者可选地在任何卷引导记录中),并通过LBA48地址(GRUB遗留的1024个气缸边界省略),并且在安装时,它被配置为加载第一部核心。
IMG.
阶段1. 5 - >核心。
默认情况下,IMG将在MBR和第一分区之间写入扇区,当这些扇区是免费且可用的。
出于遗留原因,硬盘的第一个分区不会在扇区1中开始(计数以0开始),但在扇区63处留下空间的63个扇区的差距,这不是文件系统的任何分区的一部分因此,不容易出现与之相关的任何问题。
一旦执行,核心。
IMG将加载其配置文件和所需的任何其他模块,特别是文件系统驱动程序。
在安装时,它是从DiskBoot生成的。
IMG并配置为加载阶段2.
有关使用GRUB版本2,请参阅此页面的底部以获取启动过程
属于阶段2的阶段2 - >文件在/boot/grub-directory中保持,它是/boot /目录的子目录。
GRUB启动阶段错误
Grub Stage 1 Errors: Hard Disk Error Floppy Error Read Error Geom Error
Grub Stage 1.5 Errors: The Stage 1.5 handles errors by printing an error number in the form of "Error":
Grub Stage 2 Errors: Selected item won't fit into memory Selected disk doesn't exist Disk read error Disk write error Disk geometry error Attempt to access block outside pa No such partition Bad filename (must be absolute path name or block list) Bad file or directory type File not found Inconsistent filesystem structure Filesystem compatibility error, can\'t read whole file Error while parsing number Device string unrecognizable Invalid device requested Loading below 1MB is not supported Unsupported Multiboot features requested Unknown boot failure Must load Multiboot kernel before modules Must load Linux kernel before initrd Cannot boot without kernel loaded Unrecognized command Bad or incompatible header on compressed file Bad or corrupt data while decompressing file Bad or corrupt version of stage1/stage2
这是GRUB的示例 。
CONF:
# grub.conf generated by anaconda ## Note that you do not have to rerun grub after making changes to this file # NOTICE: You have a /boot partition. This means that # all kernel and initrd paths are relative to /boot/, eg. # root (hd0,0) # kernel /vmlinuz-version ro root=/dev/VolGroup00/LogVol00 # initrd /initrd-version.img #boot=/dev/sda default=0 timeout=5 splashimage=(hd0,0)/grub/splash.xpm.gz hiddenmenu title Red Hat Enterprise Linux Server (2.6.18-238.el5) root (hd0,0) kernel /vmlinuz-2.6.18-238.el5 ro root=/dev/VolGroup00/LogVol00 initrd /initrd-2.6.18-238.el5.img title Windows XP Pro rootnoverify (hd0,1) chainload +1
GRUB允许我们从可用的操作系统列表中进行选择。
不同的操作系统在grub中有不同的标题。
Conf。
对应于此配置文件,将要求我们在GRUB菜单处选择两个选项:Red Hat Enterprise Linux服务器(2. 6. 18-238.
el5)和Windows XP Pro。
标题后的命令是从GRUB菜单中选择条目时在后台运行的命令。
例如,当用户选择Red Hat Enterprise Linux的第一个选项时,以下三个命令执行:
root (hd0,0) kernel /vmlinuz-2.6.18-238.el5 ro root=/dev/VolGroup00/LogVol00 initrd /initrd-2.6.18-238.el5.img
对应于此菜单条目,例如:"root(hd0,0)"指定压缩内核图像和initrd文件所在的分区。
第二个命令例如:"kernel/vmlinuz-2.
6. 18-238.
el5 ro root =/dev/dev/volgroup00/logvol00"告诉哪个内核镜像使用(在这种情况下,在这种情况下Vmlinuz-2.
6. 18-238.
el5)。
此命令的参数是'ro'和'root'。
'root'指定文件系统(I. /目录)的根目录所在的设备; 'RO'表示该分区将以只读模式安装(即,内核以只读模式安装根分区)。
请注意,根文件系统的分区和此内核图像所在的分区(即引导分区)是不同的。
第三个命令是Initrd的位置。
在进入Initrd的细节之前,让我们来看看在启动时造成的问题。
鸡/蛋模块问题和initrd
内核需要装入根文件系统(如上第二命令中的指定)。
此文件系统可能在某些分区上,其中包含以下功能之一:
Logical Volume Management Software RAID NFS (Network File System) Some other encrypted partition
Linux内核没有编译它的这些功能。
但它们作为模块存在。
因此,内核需要加载这些模块才能安装根文件系统。
这些模块存在于/lib/modules /目录下,它存在于根文件系统本身上。
但是这个根文件系统未安装(这是我们到目前为止的尝试)。
那么,核心如何访问用于安装文件系统本身(无需安装的文件系统的模块?
内核和GRUB通过INITRD提供初始内存磁盘来提供解决方案。
它包含安装文件系统所需的模块,并且仅包含该文件所需的模块。
GRUB还支持Chainloading,该方法用于将控制传递给其他引导加载程序。
GRUB使用Chainloadinging以引导像窗口的操作系统。
这可以在标题Windows XP Pro下面在上面的配置文件中进行检查。
4)内核初始化
在GRUB阶段2之后,通过INITRD已知核和必要模块的位置。
现在内核加载到内存中并初始化。
编译INITRD镜像并将其安装到内存中。
它用作临时根文件系统,可帮助内核正确启动,而无需安装任何根文件系统。
既然所有驱动程序都加载到内存中,并且内核已启动,内核将根文件系统以读取模式安装,并启动第一个进程。
5)初步过程
'init'是内核启动的第一个进程(初始化过程)。
这是所有流程的父级。
Init Process的PID(进程ID)始终为1.
此过程仍然存在,直到计算机停止。
它负责整个系统状态。
此过程的设置存储在其配置文件,/etc/inittab(系统初始化表)中。
在潜水到更深入进入此文件的详细信息之前并将其进一步与启动过程一起进行,让我们讨论Runlevels。
6)Runlevel
Runlevel是系统boot子的状态。
它可以在单个用户模式下启动多用户模式,带网络和图形等。
以下是Linux定义的默认运行尺寸:
0: Halt or shutdown the system 1: Single user mode 2: Multi-user mode, without networking 3: Full multi user mode, with NFS (typical for servers) 4: Officially not defined; Unused 5: Full multi user with NFS and graphics (typical for desktops) 6: Reboot
有一些另外的Runlevel可以很少使用,例如:
s,S or single: Alternate single user mode emergency: Bypass rc.sysinit (discussed later in this article)
说明:Runlevel 1用于维护目的,因为这是一个非常有限的运行级别。
仅在此运行级别中运行的最小脚本。
只有root用户可以登录。
没有其他用户可以登录此运行级别。
runlevel 2比runlevel更轻松1.其中所有用户都可以登录,但网络服务未运行。
Runlevel 3提供了一个完整的工作环境。
所有用户都可以登录,已启用网络。
Runlevel 4仅用于实验目的。
在RunleVel 5中,图形控制台可用。
Runlevel'0'是系统的停止状态,并切换到Runlevel 6将重新启动系统。
既然我们很清楚runlevels,让我们继续讨论init过程及其配置文件'inittab'。
从Rhel的'inittab'文件是:
## inittab This file describes how the INIT process should set up # the system in a certain run-level. ## Author: Miquel van Smoorenburg, <[email protected]> # Modified for RHS Linux by Marc Ewing and Donnie Barnes ## Default runlevel. The runlevels used by RHS are: # 0 - halt (Do NOT set initdefault to this) # 1 - Single user mode # 2 - Multiuser, without NFS (The same as 3, if you do not have networking) # 3 - Full multiuser mode # 4 - unused # 5 - X11 # 6 - reboot (Do NOT set initdefault to this) #id:3:initdefault: # System initialization. si::sysinit:/etc/rc.d/rc.sysinit 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 # Trap CTRL-ALT-DELETE ca::ctrlaltdel:/sbin/shutdown -t3 -r now # When our UPS tells us power has failed, assume we have a few minutes # of power left. Schedule a shutdown for 2 minutes from now. # This does, of course, assume you have powerd installed and your # UPS connected and working correctly. pf::powerfail:/sbin/shutdown -f -h +2 "Power Failure; System Shutting Down" # If power was restored before the shutdown kicked in, cancel it. pr:12345:powerokwait:/sbin/shutdown -c "Power Restored; Shutdown Cancelled" # 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
此文件中的条目的一般格式是:
ID:Runlevel:Action:Process
"id" is the unique sequence of characters for identifying an entry in this file. "runlevels" are the runlevels for which that entry is applicable. "action" is the action that is to be taken for the runlevel. "process" specifies the process that is to be executed.
现在,让我们来看看这个文件中的一些条目。
例如:"ID:3:initDefault:"的第一个取消注释行指定系统将引导的默认运行级别。
根据此文件,系统将默认引导到RunleVel 3.
一个重要的线是系统初始化行(具有ID的ID:SI)
si::sysinit:/etc/rc.d/rc.sysinit
这条行告诉init进程来执行"/etc/rc.d/rc。
sysinit"脚本。
这是在引导过程中由init执行的第一个脚本。
7)脚本rc.sysinit
当此脚本执行时,它会询问用户进行交互式设置(按"I"进入交互式启动)。
此脚本对包含的系统执行大量功能:
•设置主机名•它检查并挂载文件系统(/inc/fstab中的文件系统)(/proc,/sys等)。
•启用交换•设置SELInux•从SYSCTL配置内核参数。
CONF•设置系统时钟•加载所需的模块•设置最小路径•初始化所需的串行和其他端口•启用RAID和LVM•以及已挂载的根文件系统(如GRUB配置文件中指定),已被选中并重新安装读写。
执行此脚本后,根据"/etc/inittab",运行Runlevel特定文件。
在我们的文件中,默认的runlevel是3.所以对应于此runlevel的行是:
l3:3:wait:/etc/rc.d/rc 3
在"/etc/rc.d"目录下,存在对应于每个运行级别的目录。
根据这一行,/etc/rc中的脚本。
D/RC3.
d目录已运行。
让我们列出此目录中的文件。
(其他目录,如RC1.D,RC2.D.。。。它们中有类似的文件)。
[root@redhat-server ~]# ls -l /etc/rc.d/rc3.d/ total 304 lrwxrwxrwx 1 root root 17 May 3 03:51 K01dnsmasq -> ../init.d/dnsmasq lrwxrwxrwx 1 root root 24 May 3 03:51 K02avahi-dnsconfd -> ../init.d/avahi-dnsconfd lrwxrwxrwx 1 root root 24 May 3 03:54 K02NetworkManager -> ../init.d/NetworkManager lrwxrwxrwx 1 root root 16 May 3 03:50 K05conman -> ../init.d/conman lrwxrwxrwx 1 root root 19 May 3 03:51 K05saslauthd -> ../init.d/saslauthd lrwxrwxrwx 1 root root 17 May 3 03:52 K05wdaemon -> ../init.d/wdaemon lrwxrwxrwx 1 root root 16 May 3 03:50 K10psacct -> ../init.d/psacct lrwxrwxrwx 1 root root 13 May 3 03:53 K20nfs -> ../init.d/nfs lrwxrwxrwx 1 root root 14 May 3 03:53 K24irda -> ../init.d/irda lrwxrwxrwx 1 root root 19 May 3 03:53 K35vncserver -> ../init.d/vncserver lrwxrwxrwx 1 root root 17 May 3 03:56 K35winbind -> ../init.d/winbind lrwxrwxrwx 1 root root 20 May 3 03:51 K50netconsole -> ../init.d/netconsole lrwxrwxrwx 1 root root 16 May 5 10:10 K50vsftpd -> ../init.d/vsftpd lrwxrwxrwx 1 root root 20 May 3 03:53 K69rpcsvcgssd -> ../init.d/rpcsvcgssd lrwxrwxrwx 1 root root 16 May 3 03:56 K73ypbind -> ../init.d/ypbind lrwxrwxrwx 1 root root 14 May 3 03:52 K74ipmi -> ../init.d/ipmi lrwxrwxrwx 1 root root 14 May 3 03:49 K74nscd -> ../init.d/nscd lrwxrwxrwx 1 root root 14 May 3 04:00 K74ntpd -> ../init.d/ntpd lrwxrwxrwx 1 root root 15 May 3 03:49 K80kdump -> ../init.d/kdump lrwxrwxrwx 1 root root 15 May 3 03:52 K85mdmpd -> ../init.d/mdmpd lrwxrwxrwx 1 root root 20 May 3 03:49 K87multipathd -> ../init.d/multipathd lrwxrwxrwx 1 root root 24 May 3 03:51 K88wpa_supplicant -> ../init.d/wpa_supplicant lrwxrwxrwx 1 root root 14 May 3 03:52 K89dund -> ../init.d/dund lrwxrwxrwx 1 root root 18 May 3 03:49 K89netplugd -> ../init.d/netplugd lrwxrwxrwx 1 root root 14 May 3 03:52 K89pand -> ../init.d/pand lrwxrwxrwx 1 root root 15 May 3 03:49 K89rdisc -> ../init.d/rdisc lrwxrwxrwx 1 root root 14 May 3 03:53 K91capi -> ../init.d/capi lrwxrwxrwx 1 root root 25 May 3 03:52 K99readahead_later -> ../init.d/readahead_later lrwxrwxrwx 1 root root 23 May 3 03:52 S00microcode_ctl -> ../init.d/microcode_ctl lrwxrwxrwx 1 root root 25 May 3 03:52 S04readahead_early -> ../init.d/readahead_early lrwxrwxrwx 1 root root 15 May 3 03:54 S05kudzu -> ../init.d/kudzu lrwxrwxrwx 1 root root 16 May 3 03:51 S07iscsid -> ../init.d/iscsid lrwxrwxrwx 1 root root 19 May 3 03:49 S08ip6tables -> ../init.d/ip6tables lrwxrwxrwx 1 root root 18 May 3 03:49 S08iptables -> ../init.d/iptables lrwxrwxrwx 1 root root 18 May 3 03:51 S08mcstrans -> ../init.d/mcstrans lrwxrwxrwx 1 root root 14 May 3 03:53 S09isdn -> ../init.d/isdn lrwxrwxrwx 1 root root 17 May 3 03:51 S10network -> ../init.d/network lrwxrwxrwx 1 root root 16 May 3 03:49 S11auditd -> ../init.d/auditd lrwxrwxrwx 1 root root 21 May 3 03:51 S12restorecond -> ../init.d/restorecond lrwxrwxrwx 1 root root 16 May 3 03:51 S12syslog -> ../init.d/syslog lrwxrwxrwx 1 root root 18 May 3 03:49 S13cpuspeed -> ../init.d/cpuspeed
此目录中的某些文件以"k"开头。
以's'开头的文件对应于必须在该特定运行级别"启动"的脚本,而具有"K"的脚本对应于"杀死"的脚本。
这些文件只是在"/etc/rc.d/init/d"目录下的脚本软链接(一个软链接点"/etc/rc。
本地"本身是一个软链接"/etc/rc。
/rc/local")。
"/etc/rc.d/init.d/re/rc。
的脚本是守护进程。
守护进程是在后台运行的过程,并提供某种服务。
例如,HTTP守护程序(httpd)提供Web服务。
在执行所有这些脚本之后,运行"/etc/rc.本地"脚本运行。
如果有想要在系统启动时执行的命令或者脚本,则可以将其放在此脚本中。
[root@redhat-server ~]# cat /etc/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
既然所有这些脚本都已成功执行,将显示登录提示符。
Red Hat Enterprise Linux Server release 5.6 (Tikanga) Kernel 2.6.18-238.el5 on an i686 redhat-server login:
Bootloader Grub版本2分解Linux启动过程步骤
1.Hardware Power Up , CPU gets into real mode and jumps to fixed location 0xFFFF0 i.e., hardwired in the CPU circuit. 2.BIOS program (ROM) stored in that location will be executed 3.BIOS code will select the boot device (hard disk , CD ROM , Floppy , USB etc ) that we have configured. 4.Once BIOS undestands the boot device , BIOS code will load the MBR to 内存. grub-install is the program that write data into MBR. 5.BIOS code jumps to the start address of the loaded MBR 6.The core.img from /boot/grub/core.img is loaded in memory. Once core.img has been loaded it can use search.mod to find all further /boot/grub files 7.The executed core.img code now initialises all the modules that are built into it (linked into core.img); one of these modules will be a filesystem driver capable of reading the filesystem on which directory /boot/grub lives. 8.You can link config file into core.img if you need booting through pxe , remote nfs etc. In normal cases, it is not required. 9.Core.img now loads file /boot/grub/normal.mod dynamically from disk , this module provides an interactive command-line. 10.Grub will load a file /boot/grub/grub.cfg which is a shell script and it will then configure the standard Grub boot menu. 11.In grub.cfg "linux /vmlinuz-linux root=/dev/sda3 ro" , "linux" module loads a kernel into memory