Linux 系统启动流程
系统的启动流程可以概括如下:
加载 BIOS 的硬件信息与进行自我测试,并依据设定取得第一个可开机的装置;
2. 读取并执行第一个开机装置内 MBR 的 boot Loader (亦即是 grub, spfdisk 等程序) ; 3. 依据 boot loader 的设定加载 Kernel ,Kernel 会开始侦测硬件与加载驱动程序; 4. 在硬件驱动成功后,Kernel 会主动呼叫 init 程序,而 init 会取得 run-level 信息; 5. init 执行 /etc/rc.d/rc.sysinit 档案来准备软件执行的作业环境 (如网络、时区等) 6. init 执行 run-level 的各个服务之启动 (script 方式) ;
7. init 执行 /etc/rc.d/rc.local 文件。
8. init 执行终端机仿真程序 mingetty 来启动 login 程序,最后就等待用户登入啦;
我们使用流程图来说明linux的启动流程:
开机、加载BIOS-->加载CMOS-->取得硬件配置、BIOS开机自检
在我们按下开机按钮后,你想要启动整部系统首先就得要让系统去加载 BIOS (Basic Input Output System) , 并透过 BIOS 程序去加载 CMOS 的信息,并且藉由 CMOS 内的设定值取得主机的各项硬件配置, 例如 CPU 与接口设备的沟通频率啊、开机装置的搜寻顺序啊、硬盘的大小与类型啊、 系统时间啊、各周边总线 的是否启动 Plug and Play (PnP, 即插即用装置) 啊、 各接口设备的 I/O 地址啊、以及与 CPU 沟通的 IRQ 岔断等等的信息。在取得这些信息后,BIOS 还会进行开机自我测试 (Power-on Self Test, POST) 。 然后开始执行硬件侦测的初始化,并设定 PnP 装置,之后再定义出可开机的装置顺序, 接下来就会开始进 行开机装置的数据读取了 (MBR 相关的任务开始)。
Boot Loader的作用
在进行过开机初始的硬件数据获取与开机自我测试后,接下来启动工作将交友boot loader来继续。
Boot Loader(引导装载程序)主要用来认识操作系统的文件格式并据以加载内核到内存中去执行,它位于启动设备的第第一个扇区内。
boot loader的搜狗百科
加载内核、挂载根目录
在我们使用boot loader将内核加载到内存中后,接下来,Linux 就会将内核(核心文件)解压缩到内存中 并且利用内核的功能,开始测试与驱动各个周边装置,包括储存装置、CPU、网络卡、声卡等等。 此时 Linux 内核会以自己的功能重新侦测一次硬件,而不一定会使用 BIOS 侦测到的硬件信息喔! 也就是说,内核此时才开始接管 BIOS 后的工作了。
linux内核的组成
linux内核可以分为俩部分:
核心文件
模块文件
其中模块文件将在挂载根目录后,才从根目录内获取。
linux内核的核心文件一般位于/boot/下;
[root@tomshen boot]# ls config-2.6.32-431.el6.x86_64 initramfs-2.6.32-431.el6.x86_64.img System.map-2.6.32-431.el6.x86_64 efi lost+found vmlinuz-2.6.32-431.el6.x86_64 grub symvers-2.6.32-431.el6.x86_64.gz [root@tomshen boot]#
vmlinuz-2.6.32-431.el6.x86_64即为Linux内核的核心文件。
而linux内核的模块文件一般位于/lib/modules/:
[root@tomshen modules]# ls -a . .. 2.6.32-431.el6.x86_64 [root@tomshen modules]#
挂载根目录
在我们将内核的核心文件解压缩到内存中后,我们将开始挂载根目录。在此时如果根目录的文件系统或磁盘接 口内核无法支持的化,根目录也将无法挂载。此时我们便需要借助于ramdisk来加载模块。 虚拟文件系统 (Initial RAM Disk) 一般使用的名为 /boot/initrd这个文件的特色是,他能够透过boot loader 来加载到内存中, 然后这个档案会被解压缩并且在内存当中仿真成一个根目录,且此真在内存当中的文件系统 能够提供一支可执行的程序,透过该程序来加载开机过程中所最需要的核心模块,通常这些模块就是 USB, RAID LVM, SCSI 等文件系统与磁盘接口的驱动程序。 在我们使用ramdisk为内核的核心文件加载适当的驱动后,会释放ramdisk,并挂载实际的根目录。
运行用户空间的第一个应用程序:/sbin/init
在将核心加载完成并挂载根目录后,接下来的步骤就是调用我们的第一个进程/sbin/init,/sbin/init最主要的功能
就是准备软件执行的环境,包括系统主机名、网络设置、语系处理、文件系统格式及其他服务的启动等。
init根据系统的不同也分为多种。
init程序的类型: SysV: init, CentOS 5 配置文件:/etc/inittab Upstart: init, CentOS 6 配置文件:/etc/inittab, /etc/init/*.conf Systemd:systemd, CentOS 7 配置文件:/usr/lib/systemd/system, /etc/systemd/system
/sbin/init的配置文件为/etc/inittab
[root@tomshen ~]# cat /etc/inittab # # inittab This file describes how the INIT process should set up # the system in a certain run-level. # # Author: Miquel van Smoorenburg,# 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:5: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:5:initdefault:这行。其意义为[设置选项]:[run level]:[init的操作行为]:[命令选项]
设置选项:一个简单的说明
run level:运行级别
run level :系统运行级别:为了系统的运行或维护等应用目的而设定; 0-6:7个级别 0:关机 1:单用户模式(root, 无须登录), single, 维护模式; 2: 多用户模式,会启动网络功能,但不会启动NFS;维护模式; 3:多用户模式,正常模式;文本界面; 4:预留级别;可同3级别; 5:多用户模式,正常模式;图形界面; 6:重启 默认级别: 3, 5 切换级别: init # 查看级别: runlevel who -r
init的操作行为:指的是我们的第一个进程所要执行的操作。
常用的操作选项:
wait: 切换至此级别运行一次; respawn:此process终止,就重新启动之; initdefault:设定默认运行级别;process省略; sysinit:设定系统初始化方式,此处一般为指定/etc/rc.d/rc.sysinit; ...
命令选项:应该执行的命令,一般为一个脚本。
id:5:initdefault: 这行的意义为获取系统的默认执行等级,这里执行等级为5。
系统初始化
si::sysinit:/etc/rc.d/rc.sysinit这行的意义为系统的初始化,/etc/rc.d/rc.sysinit为指定的初始化脚本,这行的
run level为空代表对所有run level都生效。
我们来查看一下/etc/rc.d/rc.sysinit这个脚本。
[root@tomshen ~]# cat /etc/rc.d/rc.sysinit #!/bin/bash # # /etc/rc.d/rc.sysinit - run once at boot time # # Taken in part from Miquel van Smoorenburg's bcheckrc. # HOSTNAME=$(/bin/hostname) set -m if [ -f /etc/sysconfig/network ]; then . /etc/sysconfig/network fi if [ -z "$HOSTNAME" -o "$HOSTNAME" = "(none)" ]; then HOSTNAME=localhost fi if [ ! -e /proc/mounts ]; then mount -n -t proc /proc /proc mount -n -t sysfs /sys /sys >/dev/null 2>&1 fi if [ ! -d /proc/bus/usb ]; then modprobe usbcore >/dev/null 2>&1 && mount -n -t usbfs /proc/bus/usb /proc/bus/usb else mount -n -t usbfs /proc/bus/usb /proc/bus/usb fi #remount /dev/shm to set attributes from fstab #669700 mount -n -o remount /dev/shm >/dev/null 2>&1 #remount /proc to set attributes from fstab #984003 mount -n -o remount /proc >/dev/null 2>&1 . /etc/init.d/functions PLYMOUTH= [ -x /bin/plymouth ] && PLYMOUTH=yes # Check SELinux status SELINUX_STATE= if [ -e "/selinux/enforce" ] && [ "$(cat /proc/self/attr/current)" != "kernel" ]; then if [ -r "/selinux/enforce" ] ; then SELINUX_STATE=$(cat "/selinux/enforce") else # assume enforcing if you can't read it SELINUX_STATE=1 fi fi if [ -n "$SELINUX_STATE" -a -x /sbin/restorecon ] && __fgrep " /dev " /proc/mounts >/dev/null 2>&1 ; then /sbin/restorecon -R -F /dev 2>/dev/null fi disable_selinux() { echo $"*** Warning -- SELinux is active" echo $"*** Disabling security enforcement for system recovery." echo $"*** Run 'setenforce 1' to reenable." echo "0" > "/selinux/enforce" } relabel_selinux() { # if /sbin/init is not labeled correctly this process is running in the # wrong context, so a reboot will be required after relabel AUTORELABEL= . /etc/selinux/config echo "0" > /selinux/enforce [ -n "$PLYMOUTH" ] && plymouth --hide-splash if [ "$AUTORELABEL" = "0" ]; then echo echo $"*** Warning -- SELinux ${SELINUXTYPE} policy relabel is required. " echo $"*** /etc/selinux/config indicates you want to manually fix labeling" echo $"*** problems. Dropping you to a shell; the system will reboot" echo $"*** when you leave the shell." start rcS-emergency else echo echo $"*** Warning -- SELinux ${SELINUXTYPE} policy relabel is required." echo $"*** Relabeling could take a very long time, depending on file" echo $"*** system size and speed of hard drives." /sbin/fixfiles -F restore > /dev/null 2>&1 fi rm -f /.autorelabel echo $"Unmounting file systems" umount -a mount -n -o remount,ro / echo $"Automatic reboot in progress." reboot -f } # Print a text banner. echo -en $"\t\tWelcome to " read -r system_release < /etc/system-release if [[ "$system_release" == *"Red Hat"* ]]; then [ "$BOOTUP" = "color" ] && echo -en "\\033[0;31m" echo -en "Red Hat" [ "$BOOTUP" = "color" ] && echo -en "\\033[0;39m" PRODUCT=$(sed "s/Red Hat \(.*\) release.*/\1/" /etc/system-release) echo " $PRODUCT" elif [[ "$system_release" == *Fedora* ]]; then [ "$BOOTUP" = "color" ] && echo -en "\\033[0;34m" echo -en "Fedora" [ "$BOOTUP" = "color" ] && echo -en "\\033[0;39m" PRODUCT=$(sed "s/Fedora \(.*\) \?release.*/\1/" /etc/system-release) echo " $PRODUCT" elif [[ "$system_release" =~ "CentOS" ]]; then [ "$BOOTUP" = "color" ] && echo -en "\\033[0;36m" echo -en "CentOS" [ "$BOOTUP" = "color" ] && echo -en "\\033[0;39m" PRODUCT=$(sed "s/CentOS \(.*\) \?release.*/\1/" /etc/system-release) echo " $PRODUCT" else PRODUCT=$(sed "s/ release.*//g" /etc/system-release) echo "$PRODUCT" fi # Only read this once. cmdline=$(cat /proc/cmdline) # Initialize hardware if [ -f /proc/sys/kernel/modprobe ]; then if ! strstr "$cmdline" nomodules && [ -f /proc/modules ] ; then sysctl -w kernel.modprobe="/sbin/modprobe" >/dev/null 2>&1 else # We used to set this to NULL, but that causes 'failed to exec' messages" sysctl -w kernel.modprobe="/bin/true" >/dev/null 2>&1 fi fi touch /dev/.in_sysinit >/dev/null 2>&1 # Set default affinity if [ -x /bin/taskset ]; then if strstr "$cmdline" default_affinity= ; then for arg in $cmdline ; do if [ "${arg##default_affinity=}" != "${arg}" ]; then /bin/taskset -p ${arg##default_affinity=} 1 fi done fi fi nashpid=$(pidof nash 2>/dev/null) [ -n "$nashpid" ] && kill $nashpid >/dev/null 2>&1 unset nashpid /sbin/start_udev # Load other user-defined modules for file in /etc/sysconfig/modules/*.modules ; do [ -x $file ] && $file done # Load modules (for backward compatibility with VARs) if [ -f /etc/rc.modules ]; then /etc/rc.modules fi mount -n /dev/pts >/dev/null 2>&1 [ -n "$SELINUX_STATE" ] && restorecon -F /dev/pts >/dev/null 2>&1 # Configure kernel parameters update_boot_stage RCkernelparam apply_sysctl # Set the hostname. update_boot_stage RChostname action $"Setting hostname ${HOSTNAME}: " hostname ${HOSTNAME} [ -n "${NISDOMAIN}" ] && domainname ${NISDOMAIN} # Sync waiting for storage. { rmmod scsi_wait_scan ; modprobe scsi_wait_scan ; rmmod scsi_wait_scan ; } >/dev/null 2>&1 # Device mapper & related initialization if ! __fgrep "device-mapper" /proc/devices >/dev/null 2>&1 ; then modprobe dm-mod >/dev/null 2>&1 fi if [ -f /etc/crypttab ]; then init_crypto 0 fi if ! strstr "$cmdline" nompath && [ -f /etc/multipath.conf -a \ -x /sbin/multipath ]; then modprobe dm-multipath > /dev/null 2>&1 /sbin/multipath -v 0 if [ -x /sbin/kpartx ]; then /sbin/dmsetup ls --target multipath --exec "/sbin/kpartx -a -p p" >/dev/null fi fi if ! strstr "$cmdline" nodmraid && [ -x /sbin/dmraid ]; then modprobe dm-mirror >/dev/null 2>&1 dmraidsets=$(LC_ALL=C /sbin/dmraid -s -c -i) if [ "$?" = "0" ]; then for dmname in $dmraidsets; do if [[ "$dmname" == isw_* ]] && \ ! strstr "$cmdline" noiswmd; then continue fi /sbin/dmraid -ay -i --rm_partitions -p "$dmname" >/dev/null 2>&1 /sbin/kpartx -a -p p "/dev/mapper/$dmname" done fi fi # Start any MD RAID arrays that haven't been started yet [ -r /proc/mdstat -a -r /dev/md/md-device-map ] && /sbin/mdadm -IRs if [ -x /sbin/lvm ]; then if [ ! -f /.nolvm ] && ! strstr "$cmdline" nolvm ; then action $"Setting up Logical Volume Management:" /sbin/lvm vgchange -a ay --sysinit else echo $"Logical Volume Management disabled at boot." fi fi if [ -f /etc/crypttab ]; then init_crypto 0 fi if [ -f /fastboot ] || strstr "$cmdline" fastboot ; then fastboot=yes fi if [ -f /fsckoptions ]; then fsckoptions=$(cat /fsckoptions) fi if [ -f /forcefsck ] || strstr "$cmdline" forcefsck ; then fsckoptions="-f $fsckoptions" elif [ -f /.autofsck ]; then [ -f /etc/sysconfig/autofsck ] && . /etc/sysconfig/autofsck if [ "$AUTOFSCK_DEF_CHECK" = "yes" ]; then AUTOFSCK_OPT="$AUTOFSCK_OPT -f" fi if [ -n "$AUTOFSCK_SINGLEUSER" ]; then [ -n "$PLYMOUTH" ] && plymouth --hide-splash echo echo $"*** Warning -- the system did not shut down cleanly. " echo $"*** Dropping you to a shell; the system will continue" echo $"*** when you leave the shell." [ -n "$SELINUX_STATE" ] && echo "0" > /selinux/enforce start rcS-emergency [ -n "$SELINUX_STATE" ] && echo "1" > /selinux/enforce [ -n "$PLYMOUTH" ] && plymouth --show-splash fi fsckoptions="$AUTOFSCK_OPT $fsckoptions" fi if [ "$BOOTUP" = "color" ]; then fsckoptions="-C $fsckoptions" else fsckoptions="-V $fsckoptions" fi READONLY= if [ -f /etc/sysconfig/readonly-root ]; then . /etc/sysconfig/readonly-root fi if strstr "$cmdline" readonlyroot ; then READONLY=yes [ -z "$RW_MOUNT" ] && RW_MOUNT=/var/lib/stateless/writable [ -z "$STATE_MOUNT" ] && STATE_MOUNT=/var/lib/stateless/state fi if strstr "$cmdline" noreadonlyroot ; then READONLY=no fi if [ "$READONLY" = "yes" -o "$TEMPORARY_STATE" = "yes" ]; then mount_empty() { if [ -e "$1" ]; then echo "$1" | cpio -p -vd "$RW_MOUNT" &>/dev/null mount -n --bind "$RW_MOUNT$1" "$1" fi } mount_dirs() { if [ -e "$1" ]; then mkdir -p "$RW_MOUNT$1" find "$1" -type d -print0 | cpio -p -0vd "$RW_MOUNT" &>/dev/null mount -n --bind "$RW_MOUNT$1" "$1" fi } mount_files() { if [ -e "$1" ]; then cp -a --parents "$1" "$RW_MOUNT" mount -n --bind "$RW_MOUNT$1" "$1" fi } # Common mount options for scratch space regardless of # type of backing store mountopts= # Scan partitions for local scratch storage rw_mount_dev=$(blkid -t LABEL="$RW_LABEL" -l -o device) # First try to mount scratch storage from /etc/fstab, then any # partition with the proper label. If either succeeds, be sure # to wipe the scratch storage clean. If both fail, then mount # scratch storage via tmpfs. if mount $mountopts "$RW_MOUNT" > /dev/null 2>&1 ; then rm -rf "$RW_MOUNT" > /dev/null 2>&1 elif [ x$rw_mount_dev != x ] && mount $rw_mount_dev $mountopts "$RW_MOUNT" > /dev/null 2>&1; then rm -rf "$RW_MOUNT" > /dev/null 2>&1 else mount -n -t tmpfs $RW_OPTIONS $mountopts none "$RW_MOUNT" fi for file in /etc/rwtab /etc/rwtab.d/* /dev/.initramfs/rwtab ; do is_ignored_file "$file" && continue [ -f $file ] && cat $file | while read type path ; do case "$type" in empty) mount_empty $path ;; files) mount_files $path ;; dirs) mount_dirs $path ;; *) ;; esac [ -n "$SELINUX_STATE" -a -e "$path" ] && restorecon -R "$path" done done # Use any state passed by initramfs [ -d /dev/.initramfs/state ] && cp -a /dev/.initramfs/state/* $RW_MOUNT # In theory there should be no more than one network interface active # this early in the boot process -- the one we're booting from. # Use the network address to set the hostname of the client. This # must be done even if we have local storage. ipaddr= if [ "$HOSTNAME" = "localhost" -o "$HOSTNAME" = "localhost.localdomain" ]; then ipaddr=$(ip addr show to 0.0.0.0/0 scope global | awk '/[[:space:]]inet / { print gensub("/.*","","g",$2) }') for ip in $ipaddr ; do HOSTNAME= eval $(ipcalc -h $ip 2>/dev/null) [ -n "$HOSTNAME" ] && { hostname ${HOSTNAME} ; break; } done fi # Clients with read-only root filesystems may be provided with a # place where they can place minimal amounts of persistent # state. SSH keys or puppet certificates for example. # # Ideally we'll use puppet to manage the state directory and to # create the bind mounts. However, until that's all ready this # is sufficient to build a working system. # First try to mount persistent data from /etc/fstab, then any # partition with the proper label, then fallback to NFS state_mount_dev=$(blkid -t LABEL="$STATE_LABEL" -l -o device) if mount $mountopts $STATE_OPTIONS "$STATE_MOUNT" > /dev/null 2>&1 ; then /bin/true elif [ x$state_mount_dev != x ] && mount $state_mount_dev $mountopts "$STATE_MOUNT" > /dev/null 2>&1; then /bin/true elif [ ! -z "$CLIENTSTATE" ]; then # No local storage was found. Make a final attempt to find # state on an NFS server. mount -t nfs $CLIENTSTATE/$HOSTNAME $STATE_MOUNT -o rw,nolock fi if [ -w "$STATE_MOUNT" ]; then mount_state() { if [ -e "$1" ]; then [ ! -e "$STATE_MOUNT$1" ] && cp -a --parents "$1" "$STATE_MOUNT" mount -n --bind "$STATE_MOUNT$1" "$1" fi } for file in /etc/statetab /etc/statetab.d/* ; do is_ignored_file "$file" && continue [ ! -f "$file" ] && continue if [ -f "$STATE_MOUNT/$file" ] ; then mount -n --bind "$STATE_MOUNT/$file" "$file" fi for path in $(grep -v "^#" "$file" 2>/dev/null); do mount_state "$path" [ -n "$SELINUX_STATE" -a -e "$path" ] && restorecon -R "$path" done done if [ -f "$STATE_MOUNT/files" ] ; then for path in $(grep -v "^#" "$STATE_MOUNT/files" 2>/dev/null); do mount_state "$path" [ -n "$SELINUX_STATE" -a -e "$path" ] && restorecon -R "$path" done fi fi fi if [[ " $fsckoptions" != *" -y"* ]]; then fsckoptions="-a $fsckoptions" fi _RUN_QUOTACHECK=0 if [ -f /forcequotacheck ] || strstr "$cmdline" forcequotacheck ; then _RUN_QUOTACHECK=1 fi if [ -z "$fastboot" -a "$READONLY" != "yes" ]; then STRING=$"Checking filesystems" echo $STRING fsck -T -t noopts=_netdev -A $fsckoptions rc=$? if [ "$rc" -eq "0" ]; then success "$STRING" echo elif [ "$rc" -eq "1" ]; then passed "$STRING" echo elif [ "$rc" -eq "2" -o "$rc" -eq "3" ]; then echo $"Unmounting file systems" umount -a mount -n -o remount,ro / echo $"Automatic reboot in progress." reboot -f fi # A return of 4 or higher means there were serious problems. if [ $rc -gt 1 ]; then [ -n "$PLYMOUTH" ] && plymouth --hide-splash failure "$STRING" echo echo echo $"*** An error occurred during the file system check." echo $"*** Dropping you to a shell; the system will reboot" echo $"*** when you leave the shell." str=$"(Repair filesystem)" PS1="$str \# # "; export PS1 [ "$SELINUX_STATE" = "1" ] && disable_selinux start rcS-emergency echo $"Unmounting file systems" umount -a mount -n -o remount,ro / echo $"Automatic reboot in progress." reboot -f elif [ "$rc" -eq "1" ]; then _RUN_QUOTACHECK=1 fi fi remount_needed() { local state oldifs [ "$READONLY" = "yes" ] && return 1 state=$(LC_ALL=C awk '/ \/ / && ($3 !~ /rootfs/) { print $4 }' /proc/mounts) oldifs=$IFS IFS="," for opt in $state ; do if [ "$opt" = "rw" ]; then IFS=$oldifs return 1 fi done IFS=$oldifs return 0 } # Remount the root filesystem read-write. update_boot_stage RCmountfs if remount_needed ; then action $"Remounting root filesystem in read-write mode: " mount -n -o remount,rw / fi # Clean up SELinux labels if [ -n "$SELINUX_STATE" ]; then restorecon /etc/mtab /etc/ld.so.cache /etc/blkid/blkid.tab /etc/resolv.conf >/dev/null 2>&1 fi # If relabeling, relabel mount points. if [ -n "$SELINUX_STATE" -a "$READONLY" != "yes" ]; then if [ -f /.autorelabel ] || strstr "$cmdline" autorelabel ; then restorecon $(awk '!/^#/ && $4 !~ /noauto/ && $2 ~ /^\// { print $2 }' /etc/fstab) >/dev/null 2>&1 fi fi if [ "$READONLY" != "yes" ] ; then # Clear mtab (> /etc/mtab) &> /dev/null # Remove stale backups rm -f /etc/mtab~ /etc/mtab~~ # Enter mounted filesystems into /etc/mtab mount -f / mount -f /proc >/dev/null 2>&1 mount -f /sys >/dev/null 2>&1 mount -f /dev/pts >/dev/null 2>&1 mount -f /dev/shm >/dev/null 2>&1 mount -f /proc/bus/usb >/dev/null 2>&1 fi # Mount all other filesystems (except for NFS and /proc, which is already # mounted). Contrary to standard usage, # filesystems are NOT unmounted in single user mode. # The 'no' applies to all listed filesystem types. See mount(8). if [ "$READONLY" != "yes" ] ; then action $"Mounting local filesystems: " mount -a -t nonfs,nfs4,smbfs,ncpfs,cifs,gfs,gfs2 -O no_netdev else action $"Mounting local filesystems: " mount -a -n -t nonfs,nfs4,smbfs,ncpfs,cifs,gfs,gfs2 -O no_netdev fi # Update quotas if necessary if [ X"$_RUN_QUOTACHECK" = X1 -a -x /sbin/quotacheck ]; then action $"Checking local filesystem quotas: " /sbin/quotacheck -anug fi if [ -x /sbin/quotaon ]; then action $"Enabling local filesystem quotas: " /sbin/quotaon -aug fi # Check to see if a full relabel is needed if [ -n "$SELINUX_STATE" -a "$READONLY" != "yes" ]; then if [ -f /.autorelabel ] || strstr "$cmdline" autorelabel ; then relabel_selinux fi else if [ -d /etc/selinux -a "$READONLY" != "yes" ]; then [ -f /.autorelabel ] || touch /.autorelabel fi fi # Initialize pseudo-random number generator if [ -f "/var/lib/random-seed" ]; then cat /var/lib/random-seed > /dev/urandom else [ "$READONLY" != "yes" ] && touch /var/lib/random-seed fi if [ "$READONLY" != "yes" ]; then chmod 600 /var/lib/random-seed dd if=/dev/urandom of=/var/lib/random-seed count=1 bs=512 2>/dev/null fi if [ -f /etc/crypttab ]; then init_crypto 1 fi # Configure machine if necessary. if [ -f /.unconfigured ]; then if [ -x /bin/plymouth ]; then /bin/plymouth quit fi if [ -x /usr/bin/system-config-keyboard ]; then /usr/bin/system-config-keyboard fi if [ -x /usr/bin/passwd ]; then /usr/bin/passwd root fi if [ -x /usr/sbin/system-config-network-tui ]; then /usr/sbin/system-config-network-tui fi if [ -x /usr/sbin/timeconfig ]; then /usr/sbin/timeconfig fi if [ -x /usr/sbin/authconfig-tui ]; then /usr/sbin/authconfig-tui --nostart fi if [ -x /usr/sbin/ntsysv ]; then /usr/sbin/ntsysv --level 35 fi # Reread in network configuration data. if [ -f /etc/sysconfig/network ]; then . /etc/sysconfig/network # Reset the hostname. action $"Resetting hostname ${HOSTNAME}: " hostname ${HOSTNAME} fi rm -f /.unconfigured fi # Clean out /. rm -f /fastboot /fsckoptions /forcefsck /.autofsck /forcequotacheck /halt \ /poweroff /.suspended &> /dev/null # Do we need (w|u)tmpx files? We don't set them up, but the sysadmin might... _NEED_XFILES= [ -f /var/run/utmpx -o -f /var/log/wtmpx ] && _NEED_XFILES=1 # Clean up /var. rm -rf /var/lock/cvs/* /var/run/screen/* find /var/lock /var/run ! -type d -exec rm -f {} \; rm -f /var/lib/rpm/__db* &> /dev/null rm -f /var/gdm/.gdmfifo &> /dev/null [ "$PROMPT" != no ] && plymouth watch-keystroke --command "touch /var/run/confirm" --keys=Ii & # Clean up utmp/wtmp > /var/run/utmp touch /var/log/wtmp chgrp utmp /var/run/utmp /var/log/wtmp chmod 0664 /var/run/utmp /var/log/wtmp if [ -n "$_NEED_XFILES" ]; then > /var/run/utmpx touch /var/log/wtmpx chgrp utmp /var/run/utmpx /var/log/wtmpx chmod 0664 /var/run/utmpx /var/log/wtmpx fi [ -n "$SELINUX_STATE" ] && restorecon /var/run/utmp* /var/log/wtmp* >/dev/null 2>&1 # Clean up various /tmp bits [ -n "$SELINUX_STATE" ] && restorecon /tmp rm -f /tmp/.X*-lock /tmp/.lock.* /tmp/.gdm_socket /tmp/.s.PGSQL.* rm -rf /tmp/.X*-unix /tmp/.ICE-unix /tmp/.font-unix /tmp/hsperfdata_* \ /tmp/kde-* /tmp/ksocket-* /tmp/mc-* /tmp/mcop-* /tmp/orbit-* \ /tmp/scrollkeeper-* /tmp/ssh-* \ /dev/.in_sysinit # Make ICE directory mkdir -m 1777 -p /tmp/.ICE-unix >/dev/null 2>&1 chown root:root /tmp/.ICE-unix [ -n "$SELINUX_STATE" ] && restorecon /tmp/.ICE-unix >/dev/null 2>&1 # Start up swapping. update_boot_stage RCswap action $"Enabling /etc/fstab swaps: " swapon -a -e if [ "$AUTOSWAP" = "yes" ]; then curswap=$(awk '/^\/dev/ { print $1 }' /proc/swaps | while read x; do get_numeric_dev dec $x ; echo -n " "; done) swappartitions=$(blkid -t TYPE=swap -o device) if [ x"$swappartitions" != x ]; then for partition in $swappartitions ; do [ ! -e $partition ] && continue majmin=$(get_numeric_dev dec $partition) echo $curswap | grep -qw "$majmin" || action $"Enabling local swap partitions: " swapon $partition done fi fi # Set up binfmt_misc /bin/mount -t binfmt_misc none /proc/sys/fs/binfmt_misc > /dev/null 2>&1 # Boot time profiles. Yes, this should be somewhere else. if [ -x /usr/sbin/system-config-network-cmd ]; then if strstr "$cmdline" netprofile= ; then for arg in $cmdline ; do if [ "${arg##netprofile=}" != "${arg}" ]; then /usr/sbin/system-config-network-cmd --profile ${arg##netprofile=} fi done fi fi # Now that we have all of our basic modules loaded and the kernel going, # let's dump the syslog ring somewhere so we can find it later [ -f /var/log/dmesg ] && mv -f /var/log/dmesg /var/log/dmesg.old dmesg -s 131072 > /var/log/dmesg # create the crash indicator flag to warn on crashes, offer fsck with timeout touch /.autofsck &> /dev/null [ "$PROMPT" != no ] && plymouth --ignore-keystroke=Ii if strstr "$cmdline" confirm ; then touch /var/run/confirm fi # Let rhgb know that we're leaving rc.sysinit if [ -x /bin/plymouth ]; then /bin/plymouth --sysinit fi
在这个脚本中主要设置了一下内容:
(1) 设置主机名; (2) 设置欢迎信息; (3) 激活udev和selinux; (4) 挂载/etc/fstab文件中定义的文件系统; (5) 检测根文件系统,并以读写方式重新挂载根文件系统; (6) 设置系统时钟; (7) 激活swap设备; (8) 根据/etc/sysctl.conf文件设置内核参数; (9) 激活lvm及software raid设备; (10) 加载额外设备的驱动程序; (11) 清理操作; .....
关闭对应运行级别需要关闭的服务,启动需要启动的服务
l0:0:wait:/etc/rc.d/rc 0 l1:1:wait:/etc/rc.d/rc 1 ... l6:6:wait:/etc/rc.d/rc 6
这几行的意义为不同运行级别所需要启动的服务与所需要关闭的服务。
/etc/rc.d/rc是一个脚本,后面跟的数字是传递给它的位置变量。我们
以l5:1:wait:/etc/rc.d/rc 5 为例,传递5这个变量给/etc/rc.d/rc获得
/etc/rc5.d/这个目录,
[root@tomshen ~]# cd /etc/rc5.d/ [root@tomshen rc5.d]# ls K01smartd K75ntpdate S11auditd S26haldaemon K02oddjobd K75quota_nld S11portreserve S26udev-post K05wdaemon K76ypbind S12rsyslog S28autofs K10psacct K80kdump S13cpuspeed S50bluetooth K10saslauthd K84wpa_supplicant S13irqbalance S55sshd K15htcacheclean K87restorecond S13rpcbind S70spice-vdagentd K15httpd K88sssd S15mdmonitor S80postfix K50dnsmasq K89rdisc S22messagebus S82abrt-ccpp K50netconsole K95firstboot S23NetworkManager S82abrtd K50snmpd K99rngd S24nfslock S90crond K50snmptrapd S01sysstat S24rpcgssd S95atd K60nfs S02lvm2-monitor S25blk-availability S99certmonger K69rpcsvcgssd S08ip6tables S25cups S99local K73winbind S08iptables S25netfs K74ntpd S10network S26acpid [root@tomshen rc5.d]#
这个目录内以K开头的文件代表将要关闭的服务,以S开头的文件代表将要开启的服务。
用户自定义开机启动程序
我们以长格式来查看一下/etc/rc5.d/这个目录下的文件。
[root@tomshen rc5.d]# ll 总用量 0 lrwxrwxrwx. 1 root root 16 1月 3 21:36 K01smartd -> ../init.d/smartd lrwxrwxrwx. 1 root root 17 1月 3 21:31 K02oddjobd -> ../init.d/oddjobd lrwxrwxrwx. 1 root root 17 1月 3 21:40 K05wdaemon -> ../init.d/wdaemon lrwxrwxrwx. 1 root root 16 1月 3 21:38 K10psacct -> ../init.d/psacct lrwxrwxrwx. 1 root root 19 1月 3 21:31 K10saslauthd -> ../init.d/saslauthd lrwxrwxrwx. 1 root root 22 1月 3 21:31 K15htcacheclean -> ../init.d/htcacheclean lrwxrwxrwx. 1 root root 15 1月 3 21:31 K15httpd -> ../init.d/httpd lrwxrwxrwx. 1 root root 17 1月 3 21:31 K50dnsmasq -> ../init.d/dnsmasq lrwxrwxrwx. 1 root root 20 1月 3 21:28 K50netconsole -> ../init.d/netconsole lrwxrwxrwx. 1 root root 15 1月 3 21:31 K50snmpd -> ../init.d/snmpd lrwxrwxrwx. 1 root root 19 1月 3 21:31 K50snmptrapd -> ../init.d/snmptrapd lrwxrwxrwx. 1 root root 13 1月 3 21:28 K60nfs -> ../init.d/nfs lrwxrwxrwx. 1 root root 20 1月 3 21:28 K69rpcsvcgssd -> ../init.d/rpcsvcgssd lrwxrwxrwx. 1 root root 17 1月 3 21:43 K73winbind -> ../init.d/winbind lrwxrwxrwx. 1 root root 14 1月 3 13:47 K74ntpd -> ../init.d/ntpd lrwxrwxrwx. 1 root root 17 1月 3 21:31 K75ntpdate -> ../init.d/ntpdate lrwxrwxrwx. 1 root root 19 1月 3 21:36 K75quota_nld -> ../init.d/quota_nld lrwxrwxrwx. 1 root root 16 1月 3 21:43 K76ypbind -> ../init.d/ypbind lrwxrwxrwx. 1 root root 15 1月 3 13:48 K80kdump -> ../init.d/kdump lrwxrwxrwx. 1 root root 24 1月 4 20:55 K84wpa_supplicant -> ../init.d/wpa_supplicant lrwxrwxrwx. 1 root root 21 1月 3 21:28 K87restorecond -> ../init.d/restorecond lrwxrwxrwx. 1 root root 14 1月 4 20:55 K88sssd -> ../init.d/sssd lrwxrwxrwx. 1 root root 15 1月 3 21:28 K89rdisc -> ../init.d/rdisc lrwxrwxrwx. 1 root root 19 1月 3 13:48 K95firstboot -> ../init.d/firstboot lrwxrwxrwx. 1 root root 14 1月 3 21:36 K99rngd -> ../init.d/rngd lrwxrwxrwx. 1 root root 17 1月 3 21:33 S01sysstat -> ../init.d/sysstat lrwxrwxrwx. 1 root root 22 1月 3 21:35 S02lvm2-monitor -> ../init.d/lvm2-monitor lrwxrwxrwx. 1 root root 19 1月 3 21:31 S08ip6tables -> ../init.d/ip6tables lrwxrwxrwx. 1 root root 18 1月 3 21:28 S08iptables -> ../init.d/iptables lrwxrwxrwx. 1 root root 17 1月 3 21:28 S10network -> ../init.d/network lrwxrwxrwx. 1 root root 16 1月 3 21:38 S11auditd -> ../init.d/auditd lrwxrwxrwx. 1 root root 21 1月 3 21:22 S11portreserve -> ../init.d/portreserve lrwxrwxrwx. 1 root root 17 1月 3 21:31 S12rsyslog -> ../init.d/rsyslog lrwxrwxrwx. 1 root root 18 1月 3 21:36 S13cpuspeed -> ../init.d/cpuspeed lrwxrwxrwx. 1 root root 20 1月 3 21:32 S13irqbalance -> ../init.d/irqbalance lrwxrwxrwx. 1 root root 17 1月 3 21:23 S13rpcbind -> ../init.d/rpcbind lrwxrwxrwx. 1 root root 19 1月 3 21:28 S15mdmonitor -> ../init.d/mdmonitor lrwxrwxrwx. 1 root root 20 1月 3 21:21 S22messagebus -> ../init.d/messagebus lrwxrwxrwx. 1 root root 24 1月 3 21:31 S23NetworkManager -> ../init.d/NetworkManager lrwxrwxrwx. 1 root root 17 1月 4 20:55 S24nfslock -> ../init.d/nfslock lrwxrwxrwx. 1 root root 17 1月 4 20:55 S24rpcgssd -> ../init.d/rpcgssd lrwxrwxrwx. 1 root root 26 1月 3 21:35 S25blk-availability -> ../init.d/blk-availability lrwxrwxrwx. 1 root root 14 1月 3 21:28 S25cups -> ../init.d/cups lrwxrwxrwx. 1 root root 15 1月 3 21:28 S25netfs -> ../init.d/netfs lrwxrwxrwx. 1 root root 15 1月 3 21:36 S26acpid -> ../init.d/acpid lrwxrwxrwx. 1 root root 19 1月 3 21:30 S26haldaemon -> ../init.d/haldaemon lrwxrwxrwx. 1 root root 19 1月 3 21:28 S26udev-post -> ../init.d/udev-post lrwxrwxrwx. 1 root root 16 1月 3 21:32 S28autofs -> ../init.d/autofs lrwxrwxrwx. 1 root root 19 1月 3 21:32 S50bluetooth -> ../init.d/bluetooth lrwxrwxrwx. 1 root root 14 1月 3 21:36 S55sshd -> ../init.d/sshd lrwxrwxrwx. 1 root root 24 1月 3 21:36 S70spice-vdagentd -> ../init.d/spice-vdagentd lrwxrwxrwx. 1 root root 17 1月 3 21:31 S80postfix -> ../init.d/postfix lrwxrwxrwx. 1 root root 19 1月 3 21:24 S82abrt-ccpp -> ../init.d/abrt-ccpp lrwxrwxrwx. 1 root root 15 1月 3 21:24 S82abrtd -> ../init.d/abrtd lrwxrwxrwx. 1 root root 15 1月 3 21:31 S90crond -> ../init.d/crond lrwxrwxrwx. 1 root root 13 1月 4 20:55 S95atd -> ../init.d/atd lrwxrwxrwx. 1 root root 20 1月 3 21:31 S99certmonger -> ../init.d/certmonger lrwxrwxrwx. 1 root root 11 1月 3 21:28 S99local -> ../rc.local
这个目录下的文件除了S99local,其他的都是指向/etc/init.d这个目录下某一文件的软连接
[root@tomshen rc5.d]# ll S99local lrwxrwxrwx. 1 root root 11 1月 3 21:28 S99local -> ../rc.local [root@tomshen rc5.d]#
我们看出S99local这个文件是指向/etc/rc.d/local的一个软连接,而这个文件用来存放我们自己想要
执行的系统命令。
我们有任何想要在启动时就进行的工作,直接将它写入/etc/rc.d/local即可,该工作会在开机启动的时候自动挂载。
启动登录终端
# 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 # 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
这是inittab的最后一段代码,这段代码的意义是在运行级别为2/3/4/5时,执行/sbin/mingetty,而且执行6个,因为mingetty就是启动终端的命令。
而x:5:respawn:/etc/X11/prefdm -nodaemon这行的意义是启动执行等级为5的桌面系统。
最后一步,输入用户名、密码就可以登录了。