【鼎革‧革鼎】︰ Raspbian Stretch 《六之 F.3下 》

過去想要知道 □ □ linux 的啟動過程,如能深入了解

init

init(為英語:initialization的簡寫)是 Unix類Unix 系統中用來產生其它所有行程的程式。它以守護行程的方式存在,其行程號為1。Linux系統在開機時載入Linux內核後,便由Linux內核載入init程式,由init程式完成餘下的開機過程,比如載入執行級別,載入服務 ,啟動Shell/圖形化介面等等。

Unix 系列中(如 System IIISystem V)init的作用,和研究中的 UnixBSD 衍生版本相比,發生了一些變化。大多數Linux發行版是和 System V 相相容的,但是一些發行版如Slackware 採用的是BSD風格,其它的如 Gentoo 是自己客製的。後來Ubuntu[1][2] 和其他一些發行版採用 Upstart[3] 來代替[4] 傳統的 init 處理程式。至2015年,大部分Linux發行版都已採用新的systemd替代System VUpstart,但systemd向下相容System V

BSD風格

BSD init 執行存放於’/etc/rc’的初始化 shell 指令碼,然後啟動基於文字模式的終端(getty)或者基於圖形介面的終端(窗口系統,如 X)。 這裡沒有執行模式的問題,因為檔案 ‘rc’ 決定了 init 如何執行。

優點: 簡單且易於手動編輯。

缺點: 如果第三方軟體需要在啟動過程執行它自身的初始化指令碼,它必須修改已經存在的啟動指令碼,一旦這種過程中有一個小錯誤 ,都將導致系統無法正常啟動。

值得注意的是,現代的 BSD 衍生系統一直支援使用 ‘rc.local’ 檔案的方式,它將在正常啟動過程接近最後的時間以子指令碼的方式來執行。這樣做減少了整個系統無法啟動的風險。然後,第三方軟體包可以將它們獨立的 start/stop 指令碼安裝到一個原生的 ‘rc.d’ 目錄中(通常這是由 ports collection/pkgsrc 完成的)。 FreeBSDNetBSD 現在預設使用 rc.d ,該目錄中所有的用戶啟動指令碼,都被分成更小的子指令碼,和 SysV 類似。rcorder 通常根據在 rc.d目錄中指令碼之間的依賴關係來決定指令碼的執行順序。

SysV風格

System V init 檢查 ‘/etc/inittab’ 檔案中是否含有 ‘initdefault’ 項。 這告訴 init 系統是否有一個預設執行模式。如果沒有預設的執行模式 ,那麼用戶將進入系統控制台,手動決定進入何種執行模式。

優點:靈活性強

缺陷:比較複雜

 

程式大概思過半矣!今天還得明白

systemd

systemdLinux電腦作業系統之下的一套中央化系統及設定管理程式init軟體),包括有守護行程程式庫跟應用軟體,由Lennart Poettering帶頭開發。其開發目標是提供更優秀的框架以表示系統服務間的依賴關係,並依此實現系統初始化時服務的並列啟動,同時達到降低Shell系統開銷的效果,最終代替現在常用的System VBSD風格init程式。

目前絕大多數的Linux發行版都已採用systemd代替原來的System V

systemd在LGPL 2.1及其後續版本許可證下開源發布[1][2]

起源

systemd這一名字源於Unix中的一個慣例:在Unix中常以「d」作為系統守護行程英語:daemon,亦稱後台行程)的字尾標識。除此以外,systemd亦是借代英文術語D體系,而這一術語即是用於描述一個人具有快速地適應環境並解決困難的能力[3]

設計

 

systemd組件

 

專門由systemd所使用的統一層次結構控制組

System V風格init相比,systemd採用了以下新技術:

  • 將service(服務)、target(執行模式,類似於執行等級)、mount、timer、snapshot、path、socket、 swap等稱為Unit。比如,一個auditd服務(就是auditd.service)就是一個Unit,一個multi-user.target執 行模式也是一個Unit。
  • 採用Socket啟用式與D-Bus啟用式服務,以提高相互依賴的各服務的並列執行效能;
  • cgroups代替行程ID來追蹤行程,以此即使是兩次fork之後生成的守護行程也不會脫離systemd的控制。
  • 用target代替System V的執行級別(Runlevel),比如,SystemD的graphical.target相當於System V的init 5,multi-user.target相當於System V的init 3。
  • 內建新的journald 日誌管理系統。
  • 引入localectltimedatectl等新命令,系統配置更方便。

從設計構思上說,由於systemd使用了cgroup與fanotify等元件以實現其特性,所以只適用於Linux[4]。有鑑於此,考慮到kFreeBSD分支的軟體源無法納入systemd,為與其他分支保持一致,Debian開發者盡力避免納入systemd[5]。但Lennart Poettering本人對此並不在意,並稱「Debian GNU/kFreeBSD不過是玩具系統」[6]。但Debain 8.0 Jessie開始以systemd取代sysvinit。[7]

 

架構。熟悉 Debian 發行版之想法︰

How to LSBize an Init Script

A status page for dependency based boot sequencing is available.

This is a short documentation about how to make an Init Script LSB (Linux Standard Base)-compliant based on the Chapter 20 of the LSB 3.1.

LSB-compliant init scripts need to:

and should also follow Debian Policy, chapter 9.4 Console messages from init.d scripts)

Full information on the actions (and return codes) that LSB scripts have to honor are available at LSB 3.1, Chapter 20.2. Init Script Actions. Maintainers should review that section and review / adjust their init.d scripts accordingly.

……

systemd – system and service manager

………

SYSTEMD-SYSV-GENERATOR(8) systemd-sysv-generator SYSTEMD-SYSV-GENERATOR(8)

NAME

systemd-sysv-generator – Unit generator for SysV init scripts

SYNOPSIS

/lib/systemd/system-generators/systemd-sysv-generator

DESCRIPTION

systemd-sysv-generator is a generator that creates wrapper .service units for SysV init[1] scripts in /etc/init.d/* at boot and when configuration of the system manager is reloaded. This will allow systemd(1) to support them similarly to native units.

 LSB headers[2] in SysV init scripts are interpreted, and the ordering specified in the header is turned into dependencies between the generated unit and other units. The LSB facilities “remote_fs", "network”, “named", "portmap”, “

*** QuickLaTeX cannot compile formula:
time" are supported and will be turned into dependencies on specific native systemd targets. See <b>systemd.special</b>(5) for more details.</span></div>
<div class="Pp"><span style="color: #808080;"> </span></div>
<div class="Pp"><span style="color: #808080;">SysV runlevels have corresponding systemd targets (runlevel <i>X</i>.target). The wrapper unit that is generated will be wanted by those targets which correspond to runlevels for which the script is enabled.</span></div>
<div class="Pp"></div>
<div class="Pp"><span style="color: #808080;"> </span><span style="color: #808080;"><b>systemd</b> does not support SysV scripts as part of early boot, so all wrapper units are ordered after basic.target.</span></div>
<div class="Pp"><span style="color: #808080;"> </span></div>
<div class="Pp"><span style="color: #808080;">systemd-sysv-generator implements <a style="color: #808080;" href="https://manpages.debian.org/stretch/systemd/systemd.generator.7.en.html">systemd.generator(7)</a>.</span></div>
</div>
 

<span style="color: #666699;">方便悠遊 Raspbian Stretch 的天空呦!!</span>

<span style="color: #666699;">假使缺乏經驗與學習︰</span>
<h1><span style="color: #ff9900;"><a style="color: #ff9900;" href="http://linux.vbird.org/linux_basic/0560daemons.php">第十七章、認識系統服務 (daemons)</a></span></h1>
<div class="abstract">

<span style="color: #808080;">在 Unix-Like 的系統中,你會常常聽到 daemon 這個字眼!那麼什麼是傳說中的 daemon 呢?這些 daemon 放在什麼地方?他的功能是什麼?該如何啟動這些 daemon ?又如何有效的將這些 daemon 管理妥當?此外,要如何視察這些 daemon 開了多少個 ports ?又這些 ports 要如何關閉?還有還有,曉得你系統的這些 port 各代表的是什麼服務嗎? 這些都是最基礎需要注意的呢!尤其是在架設網站之前,這裡的觀念就顯的更重要了。</span>

<span style="color: #808080;">從 CentOS 7.x 這一版之後,傳統的 init 已經被捨棄,取而代之的是 systemd 這個傢伙~這傢伙跟之前的 init 有什麼差異? 優缺點為何?如何管理不同種類的服務類型?以及如何取代原本的『執行等級』等等,很重要的改變喔!</span>

</div>
<div class="links">
<ul>
 	<li><span style="color: #808080;"><a style="color: #808080;" href="http://linux.vbird.org/linux_basic/0560daemons.php#daemon">17.1 什麼是 daemon 與服務 (service)</a></span>
<ul>
 	<li><span style="color: #808080;"><a style="color: #808080;" href="http://linux.vbird.org/linux_basic/0560daemons.php#daemon_type_init">17.1.1 早期 Systemp V 的 init 管理行為中 daemon 的主要分類</a></span></li>
 	<li><span style="color: #808080;"><a style="color: #808080;" href="http://linux.vbird.org/linux_basic/0560daemons.php#daemon_type_systemd">17.1.2 systemd 使用的 unit 分類</a></span></li>
</ul>
</li>
 	<li><span style="color: #808080;"><a style="color: #808080;" href="http://linux.vbird.org/linux_basic/0560daemons.php#systemctl_cmd">17.2 透過 systemctl 管理服務</a></span>
<ul>
 	<li><span style="color: #808080;"><a style="color: #808080;" href="http://linux.vbird.org/linux_basic/0560daemons.php#systemctl_start">17.2.1 透過 systemctl 管理單一服務 (service unit) 的啟動/開機啟動與觀察狀態</a></span></li>
 	<li><span style="color: #808080;"><a style="color: #808080;" href="http://linux.vbird.org/linux_basic/0560daemons.php#systemctl_list">17.2.2 透過 systemctl 觀察系統上所有的服務</a></span></li>
 	<li><span style="color: #808080;"><a style="color: #808080;" href="http://linux.vbird.org/linux_basic/0560daemons.php#systemctl_target">17.2.3 透過 systemctl 管理不同的操作環境 (target unit)</a></span></li>
 	<li><span style="color: #808080;"><a style="color: #808080;" href="http://linux.vbird.org/linux_basic/0560daemons.php#systemctl_deps">17.2.4 透過 systemctl 分析各服務之間的相依性</a></span></li>
 	<li><span style="color: #808080;"><a style="color: #808080;" href="http://linux.vbird.org/linux_basic/0560daemons.php#systemctl_files">17.2.5 與 systemd 的 daemon 運作過程相關的目錄簡介</a>: <a style="color: #808080;" href="http://linux.vbird.org/linux_basic/0560daemons.php#etc_services">/etc/services</a></span></li>
 	<li><span style="color: #808080;"><a style="color: #808080;" href="http://linux.vbird.org/linux_basic/0560daemons.php#systemctl_network">17.2.6 關閉網路服務</a></span></li>
</ul>
</li>
 	<li><span style="color: #808080;"><a style="color: #808080;" href="http://linux.vbird.org/linux_basic/0560daemons.php#systemd_cfg">17.3 systemctl 針對 service 類型的設定檔</a></span>
<ul>
 	<li><span style="color: #808080;"><a style="color: #808080;" href="http://linux.vbird.org/linux_basic/0560daemons.php#systemd_cfg_dir">17.3.1 systemctl 設定檔相關目錄簡介</a></span></li>
 	<li><span style="color: #808080;"><a style="color: #808080;" href="http://linux.vbird.org/linux_basic/0560daemons.php#systemd_cfg_options">17.3.2 systemctl 設定檔的設定項目簡介</a></span></li>
 	<li><span style="color: #808080;"><a style="color: #808080;" href="http://linux.vbird.org/linux_basic/0560daemons.php#systemd_cfg_vsftpd">17.3.3 兩個 vsftpd 運作的實例</a></span></li>
 	<li><span style="color: #808080;"><a style="color: #808080;" href="http://linux.vbird.org/linux_basic/0560daemons.php#systemd_cfg_repeat">17.3.4 多重的重複設定方式:以 getty 為例</a></span></li>
 	<li><span style="color: #808080;"><a style="color: #808080;" href="http://linux.vbird.org/linux_basic/0560daemons.php#systemd_cfg_custom">17.3.5 自己的服務自己作</a></span></li>
</ul>
</li>
</ul>
</div>
<span style="color: #808080;">……</span>
<h1 class="title"><span style="color: #ff9900;"><a style="color: #ff9900;" href="https://www.turnkeylinux.org/blog/debugging-systemd-sysv-init-compat">systemd sysv init compatibility mode: how it works and troubleshooting when it breaks</a></span></h1>
<span class="submitted" style="color: #808080;"><a style="color: #808080;" href="https://www.turnkeylinux.org/users/liraz">Liraz Siri</a> - Sat, 2015/11/28 - 11:06 - <a class="comment-count" style="color: #808080;" title="4 comments" href="https://www.turnkeylinux.org/blog/debugging-systemd-sysv-init-compat#comments">4 comments</a> | Latest by <span class="username">Guest</span> </span>
<div class="content">
<div class="field field-name-body field-type-text-with-summary field-label-hidden">
<div class="field-items">
<div class="field-item even">

<span style="color: #808080;">systemd sysv init compatibility mode is magical. That is in the sense that it tries to handle compatibility with sysv init scripts while you are distracted looking somewhere else.</span>

<span style="color: #808080;">When it works it works well, but when things break it makes troubleshooting more difficult. Especially if you don't understand what's going on behind the curtain.</span>

</div>
</div>
</div>
</div>
<span style="color: #808080;">───</span>

 

<span style="color: #666699;">將怎麼賦予字詞意義和判斷結果呢??</span>
<pre class="lang:default decode:true ">pi@raspberrypi:~

*** Error message:
You can't use `macro parameter character #' in math mode.
leading text: <div class="Pp"><span style="color: #
You can't use `macro parameter character #' in math mode.
leading text: <div class="Pp"><span style="color: #
You can't use `macro parameter character #' in math mode.
leading text: <div class="Pp"><span style="color: #
You can't use `macro parameter character #' in math mode.
leading text: ...r: #808080;"> </span><span style="color: #
You can't use `macro parameter character #' in math mode.
leading text: <div class="Pp"><span style="color: #
You can't use `macro parameter character #' in math mode.
leading text: <div class="Pp"><span style="color: #
You can't use `macro parameter character #' in math mode.
leading text: ...sv-generator implements <a style="color: #

service jetty9 status
● jetty9.service – LSB: Start Jetty
Loaded: loaded (/etc/init.d/jetty9; generated; vendor preset: enabled)
Active: active (running) since Thu 2017-11-09 16:53:12 CST; 1min 11s ago
Docs: man:systemd-sysv-generator(8)
Process: 444 ExecStart=/etc/init.d/jetty9 start (code=exited, status=0/SUCCESS)
CGroup: /system.slice/jetty9.service
└─491 /usr/lib/jvm/jdk-8-oracle-arm32-vfp-hflt/bin/java -Xmx256m -Djava.a

11月 09 16:53:06 raspberrypi systemd[1]: Starting LSB: Start Jetty…
11月 09 16:53:12 raspberrypi jetty9[444]: Starting Jetty 9 Servlet Engine: jetty9.
11月 09 16:53:12 raspberrypi systemd[1]: Started LSB: Start Jetty.
pi@raspberrypi:~ pi@raspberrypi:~ systemctl status jetty9
● jetty9.service – LSB: Start Jetty
Loaded: loaded (/etc/init.d/jetty9; generated; vendor preset: enabled)
Active: active (running) since Thu 2017-11-09 16:53:12 CST; 1min 23s ago
Docs: man:systemd-sysv-generator(8)
Process: 444 ExecStart=/etc/init.d/jetty9 start (code=exited, status=0/SUCCESS)
CGroup: /system.slice/jetty9.service
└─491 /usr/lib/jvm/jdk-8-oracle-arm32-vfp-hflt/bin/java -Xmx256m -Djava.a

11月 09 16:53:06 raspberrypi systemd[1]: Starting LSB: Start Jetty…
11月 09 16:53:12 raspberrypi jetty9[444]: Starting Jetty 9 Servlet Engine: jetty9.
11月 09 16:53:12 raspberrypi systemd[1]: Started LSB: Start Jetty.
pi@raspberrypi:~ </pre>   <pre class="lang:default decode:true ">pi@raspberrypi:~ more /etc/init.d/jetty9
#!/bin/sh -e
#
# /etc/init.d/jetty9 — startup script for Jetty 9
#
# Written by Philipp Meier <meier@meisterbohne.de>
# Modified for Jetty 6 by Ludovic Claude <ludovic.claude@laposte.net>
# Modified for Jetty 8 by Jakub Adam <jakub.adam@ktknet.cz>
# Modified for Jetty 9 by Emmanuel Bourg <ebourg@apache.org>
#
### BEGIN INIT INFO
# Provides: jetty9
# Required-Start: local_fsremote_fs network # Required-Stop:local_fs remote_fsnetwork
# Should-Start: named # Should-Stop:named
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start Jetty
# Description: Start Jetty HTTP server and servlet container.
### END INIT INFO

# Configuration files
#
–More–(6%)

 

pi@raspberrypi:/run/systemd/generator.late ls dphys-swapfile.service  guacd.service   multi-user.target.wants graphical.target.wants  jetty9.service  raspi-config.service pi@raspberrypi:/run/systemd/generator.late 
pi@raspberrypi:/run/systemd/generator.late more jetty9.service  # Automatically generated by systemd-sysv-generator  [Unit] Documentation=man:systemd-sysv-generator(8) SourcePath=/etc/init.d/jetty9 Description=LSB: Start Jetty Before=multi-user.target Before=multi-user.target Before=multi-user.target Before=graphical.target After=remote-fs.target After=network-online.target After=nss-lookup.target Wants=network-online.target  [Service] Type=forking Restart=no TimeoutSec=5min IgnoreSIGPIPE=no KillMode=process GuessMainPID=no RemainAfterExit=yes SuccessExitStatus=5 6 ExecStart=/etc/init.d/jetty9 start ExecStop=/etc/init.d/jetty9 stop pi@raspberrypi:/run/systemd/generator.late