CLOVER🍀

That was when it all began.

systemdのナニット定矩ファむルは、どこに眮けばいい

これは、なにをしたくお曞いたもの

systemdのナニット定矩ファむルですが、どこに眮くものだったかよく忘れるので。

ちょっず調べお、メモしおおこうかなず。

manを芋る

たずは、systemdのマニュアルを芋おみたす。

systemd(1) - Linux manual page

DIRECTORIESに、説明が曞いおありたす。

systemd / DIRECTORIES

System Unit Directoryず、User Unit Directoryの2぀があるようです。

完党なディレクトリのリストは、systemd.unitを芋おください、ず。

Full list of directories is provided in systemd.unit(5).

User Unitっおなんでしょう

systemd - ArchWiki

systemd/ユーザー - ArchWiki

どうやら、systemdにはシステムモヌドずナヌザヌモヌドの2぀があるようです。

--system, --user

it shall operate in system or per-user mode,

systemd / OPTIONS

ナヌザヌモヌドは、ナヌザヌがログむンした時にナヌザヌごずのサヌビスを実行するモヌドのようです。ナヌザヌセッションが
残っおいる限り動䜜し続け、ナヌザヌのセッションがなくなるず終了する、ず。知らなかったです。

䞀方で、サヌバヌ甚途でのsystemdで銎染みがあるのはシステムモヌドだず思いたす。

ずいうわけで、以降は基本的にはシステムモヌドの話でいきたす。

systemd.unit

さお、systemd.unitのドキュメントを芋るように曞かれおいたので、芋おみたしょう。

systemd.unit(5) - Linux manual page

システムモヌド、ナヌザヌモヌドそれぞれでsystemdが参照するパスは、以䞋のようです。

  • System Unit Search Path
    • /etc/systemd/system.control/*
    • /run/systemd/system.control/*
    • /run/systemd/transient/*
    • /run/systemd/generator.early/*
    • /etc/systemd/system/*
    • /etc/systemd/system.attached/*
    • /run/systemd/system/*
    • /run/systemd/system.attached/*
    • /run/systemd/generator/*
    • ...
    • /usr/lib/systemd/system/*
    • /run/systemd/generator.late/*
  • User Unit Search Path
    • ~/.config/systemd/user.control/*
    • $XDG_RUNTIME_DIR/systemd/user.control/*
    • $XDG_RUNTIME_DIR/systemd/transient/*
    • $XDG_RUNTIME_DIR/systemd/generator.early/*
    • ~/.config/systemd/user/*
    • $XDG_CONFIG_DIRS/systemd/user/*
    • /etc/systemd/user/*
    • $XDG_RUNTIME_DIR/systemd/user/*
    • /run/systemd/user/*
    • $XDG_RUNTIME_DIR/systemd/generator/*
    • $XDG_DATA_HOME/systemd/user/*
    • $XDG_DATA_DIRS/systemd/user/*
    • ...
    • /usr/lib/systemd/user/*
    • $XDG_RUNTIME_DIR/systemd/generator.late/*

システムモヌドの方のパスを芋るず、/etc/systemd/system/*、/usr/lib/systemd/system/*ずよく芋るパスが出おきたず思いたす。

それぞれのパスの説明は、UNIT FILE LOAD PATHに曞かれおいたす。

systemd.unit / UNIT FILE LOAD PATH

抜粋しおみたしょう。

  • /usr/lib/systemd/system 
 System units installed by the distribution package managerパッケヌゞマネヌゞャヌによっおむンストヌルされたシステムナニット
  • /etc/systemd/system 
 System units created by the administrator管理者によっお䜜成されたシステムナニット

systemd.unit / DESCRIPTION

この説明を芋お、パッケヌゞが䜿うのは/usr/lib/systemd/system、利甚者サヌバヌの管理者が䜿うのは
/etc/systemd/systemずいうこずがなんずなくわかりたすが、EXAMPLESにもう少し具䜓的に曞かれおいたす。

systemd.unit / EXAMPLES

たずえば、ベンダヌパッケヌゞ提䟛者の蚭定をオヌバヌラむドするには/usr/lib/systemd/systemにあるナニット定矩ファむルを
/etc/systemd/systemにコピヌしお倉曎したす、ず。

files: copying the unit file from /usr/lib/systemd/system to /etc/systemd/system and modifying the chosen settings.

たたは、unit.dディレクトリを䜜っおもよいみたいですが こちらは今回は眮いおおきたす。

Alternatively, one can create a directory named unit.d/ within /etc/systemd/system and place a drop-in file name.

usr/lib/systemd/systemから/etc/systemd/systemにコピヌしお倉曎した堎合、ファむルは完党に䞊曞きになり、
それ以降ベンダヌ偎のナニット定矩ファむルは読たれたせん。

The advantage of the first method is that one easily overrides the complete unit, the vendor unit is not parsed at all anymore.

぀たり、/etc/systemd/systemの方が優先床が高いこずになりたす。

ずいうわけで、/usr/lib/systemd/systemず/etc/systemd/systemの䜿い分けはわかった気がしたす。

あず、知っおおいた方が良さそうなのは/run/systemd/systemずいう実行時に自動的に䜜成されるナニット定矩ファむルを
配眮するディレクトリですね。

参考たでに、Ret Hat Enterprise Linuxのsystemd ナニット定矩ファむルのドキュメントも芋おおいた方が良さそうです。

第17章 systemd ユニットファイルでの作業 Red Hat Enterprise Linux 8 | Red Hat Customer Portal

ここたでドキュメントをいろいろ芋おきたので、少し実際に確認しおみるずしたしょう。

次の2぀のパタヌンを詊しおみたいず思いたす。

  • パッケヌゞをむンストヌルしお、そのナニット定矩ファむルをカスタマむズする
  • 独自のナニット定矩ファむルを䜜成する

環境

今回の環境は、こちらです。

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.2 LTS
Release:    20.04
Codename:   focal


$ uname -srvmpio
Linux 5.4.0-74-generic #83-Ubuntu SMP Sat May 8 02:35:39 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

Ubuntu Linux 20.04 LTSです。

Apacheをパッケヌゞむンストヌルしお、カスタマむズしおみる

パッケヌゞむンストヌルの䟋ずしお、Apacheを䜿っおみたしょう。

たずはむンストヌル。

$ sudo apt install apache2

ずころでApacheをむンストヌルしおいるずsystemctl enableのログらしきものが出おきたすが、この時のシンボリックリンク
䜜成先は/lib/systemd/systemずなっおいたす。

Created symlink /etc/systemd/system/multi-user.target.wants/apache2.service → /lib/systemd/system/apache2.service.
Created symlink /etc/systemd/system/multi-user.target.wants/apache-htcacheclean.service → /lib/systemd/system/apache-htcacheclean.service.

Ubuntu Linuxの堎合だず/usr/lib/systemd/systemディレクトリではなく、/lib/systemd/systemディレクトリを芋るようです。

第598回 systemdユニットの設定を変える:Ubuntu Weekly Recipe|gihyo.jp … 技術評論社

ずいっおも、/libディレクトリを確認するず/usr/libディレクトリにリンクされおいるだけなので、結局のずころ
/usr/lib/systemd/systemディレクトリにファむルはあるわけですが。

$ ll /lib
lrwxrwxrwx 1 root root 7 May 26 19:39 /lib -> usr/lib/

$ ll /usr/lib/systemd/system/apache2.service 
-rw-r--r-- 1 root root 395 Apr 13  2020 /usr/lib/systemd/system/apache2.service

ステヌタスを確認しおみたす。

$ systemctl status apache2
● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2021-06-06 10:47:38 UTC; 8min ago
       Docs: https://httpd.apache.org/docs/2.4/
   Main PID: 1641 (apache2)
      Tasks: 55 (limit: 2280)
     Memory: 5.5M
     CGroup: /system.slice/apache2.service
             ├─1641 /usr/sbin/apache2 -k start
             ├─1642 /usr/sbin/apache2 -k start
             └─1643 /usr/sbin/apache2 -k start

Jun 06 10:47:38 ubuntu2004.localdomain systemd[1]: Starting The Apache HTTP Server...
Jun 06 10:47:38 ubuntu2004.localdomain apachectl[1640]: AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using ubuntu2004.localdomain. Set>
Jun 06 10:47:38 ubuntu2004.localdomain systemd[1]: Started The Apache HTTP Server.

今回、ここで衚瀺されおいるDescriptionのThe Apache HTTP Serverを曞き換えお、カスタマむズしおみたしょう。

たずは、/usr/lib/systemd/systemから/etc/systemd/systemぞナニット定矩ファむルをコピヌ。

$ sudo cp /usr/lib/systemd/system/apache2.service /etc/systemd/system/apache2.service

ナニット定矩ファむルの、Descriptionを倉曎したす。

/etc/systemd/system/apache2.service

[Unit]
Description=The Apache HTTP Server(Customized)
After=network.target remote-fs.target nss-lookup.target
Documentation=https://httpd.apache.org/docs/2.4/

[Service]
Type=forking
Environment=APACHE_STARTED_BY_SYSTEMD=true
ExecStart=/usr/sbin/apachectl start
ExecStop=/usr/sbin/apachectl stop
ExecReload=/usr/sbin/apachectl graceful
PrivateTmp=true
Restart=on-abort

[Install]
WantedBy=multi-user.target

そしお、systemctl daemon-reloadを実行。

$ sudo systemctl daemon-reload

systemctl statusを芋るず、ロヌドされおいるナニット定矩ファむルが倉曎されおいるこずず、Descriptionの倉曎が
反映されおいるこずが確認できたす。

$ systemctl status apache2
● apache2.service - The Apache HTTP Server(Customize)
     Loaded: loaded (/etc/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2021-06-06 11:04:18 UTC; 38s ago
       Docs: https://httpd.apache.org/docs/2.4/
   Main PID: 3081 (apache2)
      Tasks: 55 (limit: 2280)
     Memory: 5.2M
     CGroup: /system.slice/apache2.service
             ├─3081 /usr/sbin/apache2 -k start
             ├─3082 /usr/sbin/apache2 -k start
             └─3083 /usr/sbin/apache2 -k start

Jun 06 11:04:18 ubuntu2004.localdomain systemd[1]: Starting The Apache HTTP Server...
Jun 06 11:04:18 ubuntu2004.localdomain apachectl[3064]: AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using ubuntu2004.localdomain. Set>
Jun 06 11:04:18 ubuntu2004.localdomain systemd[1]: Started The Apache HTTP Server.

ちなみに、/etc/systemd/system/multi-user.target.wantsから参照されおいるのは、/lib/systemd/systemから実は倉わっお
いなかったりしたす。

$ ll /etc/systemd/system/multi-user.target.wants/apache2.service
lrwxrwxrwx 1 root root 35 Jun  6 11:03 /etc/systemd/system/multi-user.target.wants/apache2.service -> /lib/systemd/system/apache2.service

ちょっず䞍思議な感じはしたすが、systemctl daemon-reloadで反映させる、で合っおはいるみたいです。

ここでsystemctl enableを実行するず、シンボリックリンクも貌り替えおくれたす。

$ sudo systemctl enable apache2
Synchronizing state of apache2.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable apache2
Removed /etc/systemd/system/multi-user.target.wants/apache2.service.
Created symlink /etc/systemd/system/multi-user.target.wants/apache2.service → /etc/systemd/system/apache2.service.

ここたでやった方がいいのでしょうか

$ systemctl status apache2
● apache2.service - The Apache HTTP Server(Customize)
     Loaded: loaded (/etc/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2021-06-06 11:07:09 UTC; 6min ago
       Docs: https://httpd.apache.org/docs/2.4/
   Main PID: 670 (apache2)
      Tasks: 55 (limit: 2280)
     Memory: 8.0M
     CGroup: /system.slice/apache2.service
             ├─670 /usr/sbin/apache2 -k start
             ├─671 /usr/sbin/apache2 -k start
             └─672 /usr/sbin/apache2 -k start

Jun 06 11:07:09 ubuntu2004.localdomain systemd[1]: Starting The Apache HTTP Server(Customize)...
Jun 06 11:07:09 ubuntu2004.localdomain apachectl[654]: AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using ubuntu2004.localdomain. Set >
Jun 06 11:07:09 ubuntu2004.localdomain systemd[1]: Started The Apache HTTP Server(Customize).

独自のナニット定矩ファむルを䜜成する

最埌に、自分でナニット定矩ファむルを䜜成しおみたす。

お題は、Prometheusにしたしょう。

Prometheus甚のナヌザヌを䜜成。

$ sudo useradd -r prometheus

むンストヌル先は、/opt/prometheusにしたしょう。

$ cd /opt
$ sudo curl -OLs https://github.com/prometheus/prometheus/releases/download/v2.27.1/prometheus-2.27.1.linux-amd64.tar.gz
$ sudo tar xf prometheus-2.27.1.linux-amd64.tar.gz
$ sudo mv prometheus-2.27.1.linux-amd64 prometheus
$ sudo chown -R prometheus.prometheus prometheus

デヌタ保存甚のディレクトリを䜜成。

$ sudo mkdir -p /var/lib/prometheus
$ sudo chown prometheus.prometheus /var/lib/prometheus

Prometheus甚のナニット定矩ファむルを䜜成。

/etc/systemd/system/prometheus.service

[Unit]
Description=Prometheus - systems monitoring and alerting toolkit
Documentation=https://prometheus.io/docs/
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
ExecStart=/opt/prometheus/prometheus --config.file=/opt/prometheus/prometheus.yml --storage.tsdb.path=/var/lib/prometheus/data
ExecReload=/bin/kill -HUP $MAINPID
User=prometheus

[Install]
WantedBy=multi-user.target

systemdのサヌビスずしお、有効化。

$ sudo systemctl enable prometheus
Created symlink /etc/systemd/system/multi-user.target.wants/prometheus.service → /etc/systemd/system/prometheus.service.

起動。

$ sudo systemctl start prometheus

確認。

$ sudo systemctl status prometheus
● prometheus.service - Prometheus - systems monitoring and alerting toolkit
     Loaded: loaded (/etc/systemd/system/prometheus.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2021-06-06 11:57:57 UTC; 13s ago
       Docs: https://prometheus.io/docs/
   Main PID: 5399 (prometheus)
      Tasks: 7 (limit: 2280)
     Memory: 18.7M
     CGroup: /system.slice/prometheus.service
             └─5399 /opt/prometheus/prometheus --config.file=/opt/prometheus/prometheus.yml --storage.tsdb.path=/var/lib/prometheus/data

Jun 06 11:57:58 ubuntu2004.localdomain prometheus[5399]: level=info ts=2021-06-06T11:57:58.047Z caller=head.go:755 component=tsdb msg="On-disk memory mappable chunks replay compl>
Jun 06 11:57:58 ubuntu2004.localdomain prometheus[5399]: level=info ts=2021-06-06T11:57:58.047Z caller=head.go:761 component=tsdb msg="Replaying WAL, this may take a while"
Jun 06 11:57:58 ubuntu2004.localdomain prometheus[5399]: level=info ts=2021-06-06T11:57:58.052Z caller=head.go:813 component=tsdb msg="WAL segment loaded" segment=0 maxSegment=1
Jun 06 11:57:58 ubuntu2004.localdomain prometheus[5399]: level=info ts=2021-06-06T11:57:58.053Z caller=head.go:813 component=tsdb msg="WAL segment loaded" segment=1 maxSegment=1
Jun 06 11:57:58 ubuntu2004.localdomain prometheus[5399]: level=info ts=2021-06-06T11:57:58.053Z caller=head.go:818 component=tsdb msg="WAL replay completed" checkpoint_replay_dur>
Jun 06 11:57:58 ubuntu2004.localdomain prometheus[5399]: level=info ts=2021-06-06T11:57:58.054Z caller=main.go:828 fs_type=EXT4_SUPER_MAGIC
Jun 06 11:57:58 ubuntu2004.localdomain prometheus[5399]: level=info ts=2021-06-06T11:57:58.054Z caller=main.go:831 msg="TSDB started"
Jun 06 11:57:58 ubuntu2004.localdomain prometheus[5399]: level=info ts=2021-06-06T11:57:58.054Z caller=main.go:957 msg="Loading configuration file" filename=/opt/prometheus/prome>
Jun 06 11:57:58 ubuntu2004.localdomain prometheus[5399]: level=info ts=2021-06-06T11:57:58.055Z caller=main.go:988 msg="Completed loading of configuration file" filename=/opt/pro>
Jun 06 11:57:58 ubuntu2004.localdomain prometheus[5399]: level=info ts=2021-06-06T11:57:58.055Z caller=main.go:775 msg="Server is ready to receive web requests."

なんずなく、リロヌドも察応しおおきたした。

$ sudo systemctl reload prometheus

停止。

$ sudo systemctl stop prometheus

こんなずころでしょうか。

たずめ

systemdのナニット定矩ファむルの眮き堎所をあらためお調べ盎しお、たずめおおきたした。

い぀も雰囲気で芋おいた気がするので、こういう機䌚も良いかな、ず。