Skip to content

antony@notes:~/suse$ cat "System-Initialization.md"

System Initialization

2023-04-01· suse ·SUSE Linux Enterprise Server

System Initialization

:::warning

:::spoiler 目錄

[TOC]

:::

Understand the GRUB2 Boot Loader

Configuration File Structure

GRUB 2 的設定是基於以下檔案 :

  • /boot/grub2/grub.cfg
    • Configuration of the GRUB 2 menu items
    • 設定 GRUB 2 的功能選項
    • 它取代了 GRUB Legacy 中使用的 menu.lst
    • grub.cfggrub2-mkconfig 命令自動生成,不應進行編輯。
    • [註] GRUB Legacy ,一個舊版本的開機載入程式,也被稱為GRUB 1
  • /boot/grub2/custom.cfg
    • Add custom items to the boot menu
    • 將自訂選項加到 boot menu
    • 這個檔案在電腦開機的時候直接由 grub.cfg 這個檔案 source 進來
  • /etc/default/grub
    • User settings of GRUB 2, additional environmental settings
    • GRUB 2 的使用者設定,和其他環境設置
  • /etc/grub.d/*
    • Integrated into the main configuration file
    • 這個目錄下的腳本在執行 grub2-mkconfig 命令時會被讀取。它們的指令被整合到主要得設定檔 /boot/grub2/grub.cfg 中。
  • /etc/sysconfig/bootloader
    • Boot loader independent settings needed by YaST and new kernels
    • 這個設定檔在用 YaST 設定 boot loader 和每次安裝新的 kernal 時使用。
    • 它被 perl-bootloader 評估,並相應地修改 boot loader 的設定檔(例如 GRUB 2的 /boot/grub2/grub.cfg)。
    • 不是 GRUB 2 特定的配置文件,其值適用於 SUSE Linux Enterprise Server 上安裝的任何引導裝載程序。
  • /boot/grub2/x86_64-efi, /boot/grub2/power-ieee1275, /boot/grub2/s390x
    • Architecture-specific options
    • 這些設定檔包含特定架構的選項。

GRUB 2 可以通過各種方式進行控制。

  • 可以從圖形菜單(閃屏)中選擇現有配置的啟動項。
  • 配置是 /boot/grub2/grub.cfg 文件中加載的,該文件是由其他配置文件編譯而成。
  • 所有 GRUB 2的配置文件都被認為是系統文件,你需要 root 權限來編輯它們。

Define and Describe systemd

Define and Describe systemd

systemd 是 Linux 的 system and session manager,與 System V 和 LSB init scripts 兼容。

systemd 優點

  • 強大的平行化能力
  • 按需求啟動背景程序
  • 使用 Linux Kernal cgroups 限制程序
  • 可以自動重新啟動失敗的背景程序
  • 不僅記錄來自服務的 syslog 消息,還記錄顯示標準錯誤輸出上的消息服務
  • 在手冊頁中有很好的記錄

從 SLES 12 開始,systemd 替換了之前版本中的 System V init 背景程序。它與 SysV init 兼容,仍然可以使用 init scripts,並且可以通過 init runlevel 命令改變運行級別。對於腳本在 /etc/init.d/ 中的服務,chkconfiginsserv 仍然有效,rcservice 命令也可以繼續工作。

systemd 的優點包括:通過平行啟動服務來加快啟動時間,使用服務單元簡化服務配置,以及使用 Kernel Control Groups(cgroups)

systemd 被所有主要的 Linux 發行版所採用。

在 systemd 問世之前,程序 ID 為1,是留給 init 程序的。現在,它被保留給 systemd 程序。

Unit(單元 | 服務)

Systemd 可以管理所有系統資源:

  • 每個系統資源稱為一個 Unit。
  • Unit 是 Systemd 管理系統資源的基本單位
  • 使用一個 Unit File 作為 Unit 的單元文件,Systemd 通過單元文件控制 Unit 的啟動
    • 例如,MySQL 服務被 Systemd 視為一個 Unit,使用一個 mysql.service 作為啟動配置文件

systemd Basics

為了向後兼容,systemd 仍然支持 runlevels,但它使用了一個不同的概念,稱為 target units 來定義系統的具體狀態。

管理 service and target units 是 Linux 系統管理的一個重要部分。

Unit File (單元文件 | 配置文件)

單元文件中包含該單元的描述、屬性、啟動命令等

/etc/systemd/system/目錄下的文件優先於/usr/lib/systemd/system/目錄下的文件。如果你想改變某個服務的配置,請將相應的服務單元文件從/usr/lib/systemd/system/複製到/etc/systemd/system/,並根據需要在那裡編輯。

由於軟件套件只會將其服務文件放在/usr/lib/systemd/system/中,因此你的改動不會被軟件更新所覆蓋。如果你想恢復軟件包附帶的配置,只需刪除你在/etc/systemd/system/中編輯的文件。

“Unit file “是 systemd 的一個通用術語,用於表示以下內容。

:::spoiler 快速記憶 Unit 對應的 Filename 表格

:::

Unit File Structure

Sections:
[Unit]
[Service]|[Target]|...
[Install]

Unit File Sections: [Unit]

如果一個 unit 被啟動,那麼在 Requires 後面列出的 unit 也會被啟動。這個選項對服務啟動順序沒有影響,服務啟動順序需要單獨使用 Before 或 After 設定。如果沒有設定啟動順序,則服務將會平行啟動。

每個 unit 類型都有一個手冊頁面,例如 systemd.service 的手冊頁面就是用來管理服務 unit 的。

Unit File Sections: [Service]

簡易範例 :

~> cat /usr/lib/systemd/system/sshd.service

螢幕輸出 :

[Unit]
Description=OpenSSH Daemon
After=network.target

[Service]
Type=notify
EnvironmentFile=-/etc/sysconfig/ssh
ExecStartPre=/usr/sbin/sshd-gen-keys-start
ExecStartPre=/usr/sbin/sshd -t $SSHD_OPTS
ExecStart=/usr/sbin/sshd -D $SSHD_OPTS
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartPreventExitStatus=255
TasksMax=infinity

[Install]
WantedBy=multi-user.target

Unit File Sections: Secure Services

適用於 [Service], [Socket], [Mount], [Swap] sections / unit types

services, sockets, mount points, and swap devices 的 unit 設定檔共享一個配置選項子集,這些選項定義了生成的程序的執行環境。

Unit File Sections: [Install]

WantedBy=, RequiredBy=

Alias=

also=

systemctl

systemctl 是用來與 systemd 互動的指令

Manage Services

systemd 允許你一次管理多個服務。不像 SysV init 一樣一個接一個執行初始化腳本,你可以執行以下命令:

systemctl start <service1>.service <service2>.service

如果你想列出系統上可用的所有服務,輸入

systemctl list-unit-files --type=service

systemd 最重要的服務管理命令。

在 systemd 中,你可以使用 systemctl start <service>service <service> start 命令(後者在 SLES 11 的 SysV init 中也適用)來啟動一個服務。

Enabling/Disabling Services

systemd 還可以讓你永久性地啟用或禁用服務,使它們在被要求時自動啟動或始終不可用。

可以通過 YaST 或者命令行來實現這一功能。 如果你想知道某個服務是否被啟用,可以使用下面的命令

systemctl is-enabled <service>.service

在 sysv init 中使用的 chkconfiginsserv 命令的功能也被包含在 systemctl 命令中,使用 enabledisable ,如下所示。

Enable service

> sudo systemctl enable sshd

螢幕輸出 :

Created symlink /etc/systemd/system/multi-user.target.wants/sshd.service → /usr/lib/systemd/system/sshd.service.

Disable service

> systemctl disable sshd

螢幕輸出 :

Removed /etc/systemd/system/multi-user.target.wants/sshd.service.

為啟用一個服務而創建的鏈接是基於 service unit file 中[Install]部分的信息 (上例中的 sshd.service,指定 WantedBy=multi-user.target )。

注意

要在啟用服務後立即啟動它,請明確運行

systemctl start <service>.servicerc<service> start

當禁用一個服務時,你可能仍然需要停止當前正在運行的服務實例。

注意: 可以用 service 命令來啟動和停止服務,但不能用這個命令來 enable 或 disable 服務。

參考文章 : Understanding Systemd Units and Unit Files 參考文章 : Linux Systemd 詳細介紹: Unit、Unit File、Systemctl、Target

What is Target

代表 Linux 系統目前或期望的運行狀態,Target (目標)定義了必須存在的一個或多個資源(Unit),讓系統能夠在該狀態下運行並處於活動狀態。

例如,執行 systemd suspend 命令讓系統暫停,會觸發啟動 suspend.target,然後執行裏面的 systemd-suspend.service Unit,使系統達到一個暫停的狀態

Target Units

「Targets」是類似「runlevels」的同步點,但是更細緻。

以下是一個描述 systemd 的 Target Unit 的文字段落,其中包含不同的 Target Unit 名稱、描述以及它們在 System V 中的等級對應。

Identity and SecurityThe Linux Filesystem