Linux system startup process

1. Start the overall flow chart

  kernel and initrd: detect hardware -> load driver (initrd, mini linux) -> mount root file system -> rootfs(/sbin/init). initrd is not required.

Two, three main init

  Describes the three main Init systems: sysvinit, UpStart and systemd

1、sysvinit

  SysVinit runs sequentially (serial), the concept is simple and clear, and the startup is slow. The startup process is as follows:
init--->inittab(runlevel)--->rc.sysinit(system initialization)--->rc--->rcX.d(start and stop services)--->rc.local-- ->tty and login

  Disadvantages: Frequent use of hardware hot-plug technology, sysvinit needs to start all the services corresponding to the hardware that may be used by default; the mounting of network shared disks (nfs, iSCSI) requires the netfs service to mount all these network disks; slow startup .

2、upstart

  Upstart uses an event-driven model. Boot systems faster (parallel); dynamically start services when new hardware is discovered; dynamically stop services when hardware is unplugged.

  The main concepts of UpStart are job and event. A job is a unit of work that is used to complete a piece of work, such as starting a background service or running a configuration command. Each job waits for one or more events, and once the event occurs, upstart triggers the job to complete the corresponding work.

  Boot upstart first generates a startup event.

、 、 Systemd

  There are two biggest features of systemd:
     (1) It provides a more radical parallel startup capability than UpStart, and uses technologies such as socket / D-Bus activation to start services. The result: faster startup.
     (2), use CGroup statistics to track child processes, clean and reliable.

3.1. Parallelized Socket
    For traditional Unix daemons, the real dependence on each other is the socket provided by the service.
    If we can establish the sockets necessary for the client program as soon as possible and make the client program wait instead of starting the client program after the server program is fully started, we can speed up the startup process and further parallelize the process startup.
3.2. Parallelized Bus service
    Modern daemons on Linux all interact through DBus instead of sockets.
    DBus already provides all the necessary hooks: using DBus will load the service on first access and give a minimal, one-per-request synchronization mechanism that both consumers and producers start at the same time.
    For example, Avahi and CUPS (CUPS needs Avahi to scan printers on mDNS/DNS-SD) are started at the same time, just simply start both at the same time. If CUPS starts faster than Avahi, DBus will cache the request into the queue until the Avahi service is executed. processed.

 To summarize: Socket-based and DBus-based services can start all processes in parallel at once without any additional synchronization. Activation-based strategies also allow us to lazy load services. If the service is rarely used, we can start it when it is accessed for the first time, rather than during the boot process.

3.3、并行化文件系统任务
    系统启动过程中,文件系统相关的活动是最耗时的,比如挂载文件系统,fsck,磁盘配额检查等。
    Systemd 集成了 autofs 的实现,对于系统中的挂载点,比如/home,当系统启动的时候,systemd 为其创建一个临时的自动挂载点。在这个时刻/home 真正的挂载设备尚未启动好,真正的挂载操作还没有执行,文件系统检测也还没有完成。可是那些依赖该目录的进程已经可以并发启动,他们的 open()操作被内建在 systemd 中的 autofs 捕获,将该 open()调用挂起(可中断睡眠状态)。然后等待真正的挂载操作完成,文件系统检测也完成后,systemd 将该自动挂载点替换为真正的挂载点,并让 open()调用返回。由此,实现了那些依赖于文件系统的服务和文件系统本身同时并发启动。

3.3.1、三个init启动时间示意图

 

3.3.2、systemd设置运行级别:
  systemd执行的第一个目标是default.target
  获取当前的运行级别:systemctl get-default
  设置:systemctl set-default multi-user.target
  /lib/systemd/system/default.target -> graphical.target

3.3.3、systemd与sysvinit运行级别对应表

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325733517&siteId=291194637