--- title: Lab Meeting Minutes 2023/04/21 tags: lab_meeting --- > Outline > [TOC] --- # PERAL Lab Meeting - 時間:112 年 04 月 21 日 9:00 - 地點:線上 - 線上會議連結 : [Online](https://meet.google.com/icy-mnxu-ick) - 出席者:吳坤熹老師、謝萬霖、吳騰然、劉怡君、田蕙瑜、莊才賢、紀見如、劉冠伶、林大智、繆亭霄 - 會議主題:[Systemd](https://docs.google.com/presentation/d/1CfJEax-rcbr6HvjyT5hDJgJ_aurbwNNbJ5g7B90NfSM/edit?usp=sharing) - 主講者: 莊才賢 - 主記: 劉怡君 ## 會議內容 - How Linux Machine Boot * booting * hardware(BIOS) * self-testing and accesses nonvolatile memory(NVRAM) * 檢查周邊設備 * find the boot device(Disk, USB…) * transfer the control to this program * OS loader(store at MBR sector) * locate the OS loader, load it, and run it * kernel * run swapper for paging, start an init process * root user-space process(INIT) * handling the `runlevel` as an `init` * handle main process's livecycle * run boot script * for every service(mail, nfs, cron) they all have to start up scripts at `/etc/rc[0-6S].d` * use the alphabet order as the president * INIT - manage daemons/processes * MUST do (start all the necessary daemons, background processes, services) * starting up a boot screen * various parts of the networking stack * hardware in your machine. * MAY do * logging information * watching for devices you insert or remove * managing user login - `runlevels` vs `systemd` * runlevel(SysVInit) * eg. halt, single-user, multi-user, undefined(user-defined), graphical login, reboot. * `/etc/rc0.d` shows the init scripts at runlevel N 0 , it means it runs at halt status * Just type `runlevel` at command line to check the current runlevel. * Target(Systemd) * Use the target to replace runlevel. eg. `multi-user.target`, `graphical.target`. * features * only systemd reached `multi-user.target`, the services specified at `graphical.target` would execute * `basic.target` is the implicit dependencies, which means the basic system service would all start - service ordering - SysVInit - hard to manage the dependency(script format) - Systemd - 分散式管理 - **Unit** 是 Systemd 內的最小單位, 負責掌管許多服務相關的工作。 - 傳統的 INIT System 由一整個 Service 檔案進行服務管理(包含服務相依性) - 透過 Systemd 可以把許多操控系統的動作分解成許多 Unit - Common Unit * .service * Daemon or background process * .target * time as a representation * .timer * run cron jobs but as systemd * .socket * .device * .mount * .automount * .swap * .path * .snapshot - Format of unitfile(Section Directives) - General parts * `Description=` * 可以透過 systemd cli 工具看到的 Unit file 描述 * `Documentation=` * Doc 位置,可以是 URl 或是 man page 位置(透過 `systemctl status` 查看) * `Documentation=man:sshd(8) man:sshd_config(5)` * `OnFailure=`, `OnSuccess=` * 可以指定此 Unit 成功 active 後啟動的行為,可以指定一個 Systemd Service * Ex. 可以設定當一個 Unit 失敗後,寄信給某個 User * `Requires=` * 這會列出這個 unit file 依賴的 unit file 的列表 * 所有相依的 unit file 和此 unit file 會同時平行的啟用,但若有一個 Unit file 無法啟用(systemd 層級),這個 Unit file 就會被標示為啟用失敗 * `Wants=` * 類似 Requires 但是較不嚴格 * 同樣會平行啟用這些 unit * 差別是若有任意一或多個 Unit file 啟用失敗或是不存在,這個 Unit 也不會被標示為啟用失敗 * 較為推薦的方式來管理 dependency * `BindsTo=` * 類似 Requires= 但更為嚴格 * 若是相依的 unit 中途 Crash,會導致此 Unit 停止 * `Before=` * 這個列表內的 unit 會在此 unit 執行完畢(被標記為 strated)後執行,此 unit 要先於這些 unit files * `After=` * 在這列表內的 unit 執行完畢後執行此 Service * 這個列表內的 unit 會在此 unit 執行完畢(被標記為 strated)前執行,此 unit 要後於這些 unit files - Optional parts * `WantedBy=` * 最常被用來做是否此 unit 應該被排入 systemd 排程的 directives * 類似於剛剛說到的 `Want=` * 主 Unit file 需要撰寫 Want 以啟用相依的 Unit,但 wantedby 可以寫在次要的 unit,以保持主 unit 乾淨 * 若是此 unit 有 `WantedBy=multi-user.target driective`,啟用的時候就會被 link 到這個目錄 `/etc/systemd/system/multi-user.target.wants` ,取消的時候 link 就會被刪除 * `RequiredBy=` * 會產生一個 requires 結尾的資料夾,ex. `/etc/systemd/system/multi-user.target.requires` ![](https://i.imgur.com/v0acyMl.png) - Unit-Specific * Target Section * a synchronization point of system’s state * make Services can hook on it * easy to goto deterministic state * `man bootup` * utilities * systemctl get-default * get current bootup target * systemctl set-default * set default bootup target * systemctl isolate * goto some target * systemctl list-dependencies * show the Unit’s dependencies * Service (Section Directives) * `Type=` * eg. oneshot, simple, forking, notify. * `ExecStart=` * 定義包含 “絕對路徑” 和參數的 command,通常應該只定義一次(除非是 oneshot service) * `ExecStartPre=` * 告訴 systemd 在執行主程式之前,要執行什麼程式,可以定義多次 * `ExecReload=` * reload 的指令 * `ExecStop=` * stop 的指令,若是沒有提供, process 將直接被 kill 掉 * `TimeoutSec=` * systemd 會跟據這個秒數決定 graceful shutdown 的時間,超過這個時間將會強制被 kill :::info - How Systemd Works without Using RunLevel * `multi-user.target` Requires and After `basic.target` * block until the `basic.target` is done * `graphical.target` Requires and After `multi-user.target` * block until the `multi-user.target` is done ::: --- ### 建議&問題 1. [name=Lawrence] p.26 關於路徑的設定,script 一定要放在 /usr/local/bin 嗎? Ans: [name=] 不一定,想放在 home direfctory 也行。 2. [name=Jennifer] p.12 為什麼分成多個 Unit 就可以做到更多事情? Ans: 全部寫在同一個 file 會比較不好 debug。 3. [name=August] p.21 target 跟 service 的差別? Ans: target 不是 section directive 4. [name=Ashely] p.23 這些參數一定要寫嗎? 還是 optional? Ans: 有些不寫的話會出問題,通常自定義的 service 會用 simple。 5. [name=Angela] P.19 What's the difference between "Forward" and "Reverse"? Ans: For example, sshd -> multi-user.target. Reverse 的話, 你 multi-user.target 中就不用再 specify dependency. 6. [name=Angela] p.16 the seqence of listed points should be the same with table items 7. [name=Phoebe] P.21 isolate 可以往上也可以往下切換? Ans: 可以。 8. [name=Toby] p.21 可以把一個開起來,其他全部關掉? Ans: 比較精確的講法是,把一個 target 底下的 service 全部開起來. 9. [name=Toby] p.28 可以修改 log msg 還是說可以自定義 log msg? Ans: 可以自定義。 10. [name=Edgar] 用 systemctl isolate 指令切換 runlevel 後可以直接透過 `runlevel` 直接查看嗎? Ans: 可以。 11. [name=Edgar] p.19 相依性必須自己去注意對嗎? Ans: 12. [name=Edgar] p.12 還原用start or enable? Ans: 13. [name=Edgar] p.24 `ExecStartPre=` command 後面接了 `-` 任何 return 值為非零的狀況都會被容忍,這之間是否需要空白隔開? Ans: 這我只在文件上看到,細節要再試試。 14. [name=Edgar] P.29 What is the minimum value of AccuracySec? Ans: 1ms. (Edgar: "I have seen 1us.") 15. [name=Solomon] p.26 Why use cp instead of ln when ln can yield a similar result? Ans: The tutorials recommend using the cp command, but based on your suggestion, using the hardlink option would be a better choice. 16. [name=Solomon] p.22 If I specified "After", do I still need to specify "Requires"? Ans: It depends. If some other services have "required" the service which you depend, the your service can successfully start. However, if no other service "requires" the service, then no one starts the service, and your service will indefinitely wait for that. ## 待追蹤事項 1. [name=] ## 臨時動議 --- 散會結束時間: