ubuntu启动脚本一览分析

#rc--run command的意思【rc解释
harvey@ubuntu:/etc$ cat ./init/rc-sysinit.conf # rc-sysinit - System V initialisation compatibility # # This task runs the old System V-style system initialisation scripts, # and enters the default runlevel when finished. #选定系统的启动级别,还没有真正进入系统启动阶段 description "System V initialisation compatibility" author "Scott James Remnant <[email protected]>" start on (filesystem and static-network-up) or failsafe-boot stop on runlevel # Default runlevel, this may be overriden on the kernel command-line # or by faking an old /etc/inittab entry
#设置默认的启动级别为2,但是当有inittab文件和内核命令行的时候默认值会被改变
env DEFAULT_RUNLEVEL=2 emits runlevel # There can be no previous runlevel here, but there might be old # information in /var/run/utmp that we pick up, and we don't want # that. # # These override that
#防止/var/run/utmp下面旧的启动信息干扰,把信息置空
env RUNLEVEL= env PREVLEVEL= console output env INIT_VERBOSE task #如果存在inittab文件就按照inittab的要求计算默认DEFAULTLEVEL script # Check for default runlevel in /etc/inittab if [ -r /etc/inittab ] then eval "$(sed -nre 's/^[^#][^:]*:([0-6sS]):initdefault:.*/DEFAULT_RUNLEVEL="\1";/p' /etc/inittab || true)" fi # Check kernel command-line for typical arguments for ARG in $(cat /proc/cmdline) do case "${ARG}" in -b|emergency) # Emergency shell [ -n "${FROM_SINGLE_USER_MODE}" ] || sulogin ;; [0123456sS]) # Override runlevel DEFAULT_RUNLEVEL="${ARG}" ;; -s|single) # Single user mode [ -n "${FROM_SINGLE_USER_MODE}" ] || DEFAULT_RUNLEVEL=S ;; esac done # Run the system initialisation scripts
#执行系统内核脚本改变DEFAULTLEVEL的值 [
-n "${FROM_SINGLE_USER_MODE}" ] || /etc/init.d/rcS # Switch into the default runlevel
#开放根据选定的level启动系统 telinit
"${DEFAULT_RUNLEVEL}" #执行telinit命令 end script

 在ubuntu下init 和telinit都是二进制命令:

harvey@ubuntu:/etc$ ll /sbin/init /sbin/telinit

-rwxr-xr-x 1 root root 167192  1月 19  2013 /sbin/init*

-rwxr-xr-x 1 root root  51416  1月 19  2013 /sbin/telinit*

 

ubuntu启动流程:

Ubuntu的upstart init文件→→进入/etc/init文件夹下选择进入的level值→→进入/etc/rc{level}.d文件夹,根据文件夹内的软连接的名字判断执行方式实际进入/etc/init.d文件夹执行命令

 1 harvey@ubuntu:/etc/rc2.d$ ll #rc2.d 文件夹下都是链接到init.d文件夹中的软连接,软连接以S开头表示启动服务K开头的表示停止,rc{level}.d文件夹类似于windows的启动文件夹,windows中放入启动文件夹中的程序会被启动  2 总用量 20

 3 drwxr-xr-x   2 root root  4096  3月 13 14:11 ./

 4 drwxr-xr-x 132 root root 12288  3月 14  2014 ../

 5 -rw-r--r--   1 root root   677  7月 27  2012 README

 6 lrwxrwxrwx   1 root root    20  3月 13 01:48 S20kerneloops -> ../init.d/kerneloops* 
#exec="/usr/sbin/kerneloops"执行该程序,并把pid保存在/var/run/$prog.pid使用的conf是/etc/kerneloops.conf文件【证明usr放的是程序代码,var防止运行时信息】 7 lrwxrwxrwx 1 root root 27 3月 13 01:48 S20speech-dispatcher -> ../init.d/speech-dispatcher*
#服务程序位置daemon="/usr/bin/speech-dispatcher" 进程文件信息PIDFILE=/var/run/speech-dispatcher/speech-dispatcher.pid 进程所属用户user='speech-dispatcher' 8 lrwxrwxrwx 1 root root 18 3月 13 14:11 S21sendmail -> ../init.d/sendmail*
#服务程序位置/usr/sbin/sendmail 脚本文件位置 /etc/init.d/sendmail 9 lrwxrwxrwx 1 root root 18 3月 13 10:38 S35whereami -> ../init.d/whereami*
#strat whereami 10 lrwxrwxrwx 1 root root 20 3月 13 01:48 S50pulseaudio -> ../init.d/pulseaudio*
#服务程序位置/usr/bin/pulseaudio PID文件位置/var/run/pulse 11 lrwxrwxrwx 1 root root 15 3月 13 01:48 S50rsync -> ../init.d/rsync*
#服务位置/usr/bin/rsync 12 lrwxrwxrwx 1 root root 15 3月 13 01:48 S50saned -> ../init.d/saned*
#daemon服务的位置 /usr/sbin/saned 13 lrwxrwxrwx 1 root root 19 3月 13 01:48 S70dns-clean -> ../init.d/dns-clean* 14 lrwxrwxrwx 1 root root 18 3月 13 01:48 S70pppd-dns -> ../init.d/pppd-dns* 15 lrwxrwxrwx 1 root root 14 3月 13 01:48 S75sudo -> ../init.d/sudo* 16 lrwxrwxrwx 1 root root 22 3月 13 01:48 S99acpi-support -> ../init.d/acpi-support* 17 lrwxrwxrwx 1 root root 21 3月 13 01:48 S99grub-common -> ../init.d/grub-common* 18 lrwxrwxrwx 1 root root 18 3月 13 01:48 S99ondemand -> ../init.d/ondemand* 19 lrwxrwxrwx 1 root root 18 3月 13 01:48 S99rc.local -> ../init.d/rc.local* #

 ubuntu用runlevel查看当前运行的级别,用sudo init {num}来切换工作的level。level0 表示关键 level6表示重启动 level2是默认级别

harvey@ubuntu:~$ runlevel  #查看当前工作level

N 2

harvey@ubuntu:~$ sudo init 0 #shutdown和reboot就是切换的runleve

 

 

 

 

参考资料:ubuntu为什么没有/etc/inittab文件? 深究ubuntu的启动流程分析

 

你可能感兴趣的:(ubuntu)