LoginSignup
5
3

More than 5 years have passed since last update.

「風が吹けば桶屋が儲かる」的にsystemdのシーケンスをメモる

Last updated at Posted at 2019-02-25

systemdの起動シーケンスのメモ。

前提

  • Amazon Linux 2を使っている。
  • RunLevelは 3 にしている。
  • 動的生成ユニットに関しては割愛。

systemd の起動シーケンス

  1. systemd が起動する。
  2. systemd が起動すると、 /etc/systemd/system/default.target が呼ばれる。
  3. default.target が呼ばれると /etc/systemd/system/default.targetAfter に記述された内容に従い、
    1. ユニット basic.target が起動され
    2. ユニット rescue.service が起動され
    3. ユニット rescue.target が起動される。
  4. After で示されたユニットの起動が終わると、ディレクトリ /etc/systemd/system/default.target.wants 下のユニットが順に呼ばれる。

解説

1. systemd が起動する

 Linuxが立ち上がった時に、initスクリプトが実行されて /sbin/init が実行される。
/sbin/init はすでに systemd のシンボリックリンクになっているので、結果的にはこれが読み込まれる。

$ ls -l /sbin/init 
lrwxrwxrwx 1 root root 22  1月 15 03:30 /sbin/init -> ../lib/systemd/systemd

2. /etc/systemd/system/default.target が呼ばれる

Unit の定義は2ヶ所で行われている。

ディレクトリ 目的
/etc/systemd/system/下 管理者がカスタマイズする内容
/usr/lib/systemd/system/下 システムのデフォルト

同じファイル名のものがあった場合には /etc/systemd/system/ のほうが優先される。
どちらのディレクトリにも defaukt.target ディレクトリは存在するのだが、このルールのために /etc/systemd/system/default.target のほうをsystemdは処理する。
そしてこの /etc/systemd/system/default.target は、RunLevelに応じた実体のシンボリックになっている。RunLevelが3(Multi-Userモード)の場合は、次のようになっている。

$ ls -l /etc/systemd/system/default.target
lrwxrwxrwx 1 root root 41  2月 23 19:39 /etc/systemd/system/default.target -> /usr/lib/systemd/system/multi-user.target

結果的に、 /usr/lib/systemd/system/multi-user.target が処理される。

3. /etc/systemd/system/default.target の After に記述された内容に従い

[Unit] 項目では、いくつか定義を記述できます。
After は、順序関係の定義の1つで、記述された他ユニットの実行の後に実行する、という意味になる。(Beforeはその反対になる)

なお、 default.tareget には Require、 Conflict といった定義もされている。Requireは当ユニットの実行に必須な依存関係を、Conflict は同時に実行してはならない同時実行制限を記述している。

4. basic.target が起動

実体は /usr/lib/systemd/system/basic.target

5. rescue.service が起動

実体は、/usr/lib/systemd/system/rescue.service

6. rescue.target が起動

実体は、/usr/lib/systemd/system/rescue.target

7. /etc/systemd/system/default.target.wants 下のユニットが順に呼ばれる

このディレクトリの下に、default.target が起動させたいユニットのシンボリックリンクが張られている。

参考サイト

『Linux女子部 systemd徹底入門』 - SlideShare
『Systemd メモ書き』 - Qiita

5
3
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3