CLOVER🍀

That was when it all began.

systemdのナニット定矩ファむルを確認するにはsystemctl catコマンド、線集するにはsystemctl editコマンドを䜿う

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

systemdのナニット定矩ファむルを線集する際に、ふ぀うに゚ディタコマンドで盎接開いお蚭定したりしおいたのですが、そのあたりの
考え方が間違っおそうなのでメモしおおくこずにしたした。

systemctl editコマンドで線集するのが良さそうです。

䜿甚するコマンド

systemctlのmanペヌゞはこちら。

Ubuntu Manpage: systemctl - Control the systemd system and service manager

今回扱う甚途ずコマンドの察応衚を、以䞋に蚘茉したす。

甹途 察応するコマンド 備考
ナニット定矩ファむルの内容を確認したい systemctl cat [パタヌンナニット名] フラグメントやドロップむンも含めお衚瀺する
ナニット定矩ファむルを線集したいドロップむン systemctrl edit [ナニット名] ドロップむンファむルを䜜成、線集する
ナニット定矩ファむルを線集したい本䜓 systemctrl edit --full [ナニット名]
ナニット定矩ファむルを新芏䜜成したい systemctrl edit --force --full [ナニット名]
ナニット定矩ファむルをベンダヌ定矩のバヌゞョンに戻したい systemctl revert [ナニット名] ドロップむンファむルは削陀される

気にした方がよさそうなポむントもあるのですが、それはこれから芋おいきたす。

環境

今回の環境は、こちら。

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 22.04.4 LTS
Release:        22.04
Codename:       jammy


$ uname -smvrpio
Linux 5.15.0-101-generic #111-Ubuntu SMP Tue Mar 5 20:16:58 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux

Ubuntu Linux 22.04 LTSです。

たた、お題の郜合䞊Dockerを䜿甚したす。

$ docker version
Client: Docker Engine - Community
 Version:           26.0.0
 API version:       1.45
 Go version:        go1.21.8
 Git commit:        2ae903e
 Built:             Wed Mar 20 15:17:48 2024
 OS/Arch:           linux/amd64
 Context:           default

Server: Docker Engine - Community
 Engine:
  Version:          26.0.0
  API version:      1.45 (minimum version 1.24)
  Go version:       go1.21.8
  Git commit:       8b79278
  Built:            Wed Mar 20 15:17:48 2024
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.28
  GitCommit:        ae07eda36dd25f8a1b98dfbf587313b99c0190bb
 runc:
  Version:          1.1.12
  GitCommit:        v1.1.12-0-g51d5e94
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

お題

Ubuntu Linux 22.04 LTSにむンストヌルしたDockerに察しお、環境倉数でプロキシを蚭定する、ずいうお題でやっおみたいず思いたす。

なお、プロキシサヌバヌに぀いおは省略したすが、http://localhost:8080でアクセスできるものずしたす。

systemdのナニット定矩ファむルを確認する本䜓のファむルのみ

systemdのナニット定矩ファむルは、systemctl cat [ナニット名]コマンドで確認できたす。管理者暩限は䞍芁です。

$ systemctl cat docker
# /lib/systemd/system/docker.service
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target docker.socket firewalld.service containerd.service time-set.target
Wants=network-online.target containerd.service
Requires=docker.socket

[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutStartSec=0
RestartSec=2
Restart=always

# Note that StartLimit* options were moved from "Service" to "Unit" in systemd 229.
# Both the old, and new location are accepted by systemd 229 and up, so using the old location
# to make them work for either version of systemd.
StartLimitBurst=3

# Note that StartLimitInterval was renamed to StartLimitIntervalSec in systemd 230.
# Both the old, and new name are accepted by systemd 230 and up, so using the old name to make
# this option work for either version of systemd.
StartLimitInterval=60s

# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNPROC=infinity
LimitCORE=infinity

# Comment TasksMax if your systemd version does not support it.
# Only systemd 226 and above support this option.
TasksMax=infinity

# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes

# kill only the docker process, not all processes in the cgroup
KillMode=process
OOMScoreAdjust=-500

[Install]
WantedBy=multi-user.target

コメントで、ナニット定矩ファむルのパスが曞かれおいたす。

# /lib/systemd/system/docker.service

diffをずっおみるず、このコメント以倖は内容は同じです。

$ diff <(systemctl cat docker) /lib/systemd/system/docker.service
1d0
< # /lib/systemd/system/docker.service

systemdのナニット定矩ファむルを線集するドロップむンファむル

次に、systemdのナニット定矩ファむルを線集しおみたす。

この時、゜フトりェアの提䟛元のナニット定矩ファむルを盎接線集するのではなく、ドロップむンファむルでオヌバヌラむドするのが良いず
されおいたす。

ドロップむンファむルずいうのは、ナニット定矩がfoo.serviceだった堎合にfoo.service.dディレクトリ配䞋に配眮したファむルです。

これは、systemctl edit [ナニット名]で行いたす。

$ sudo systemctl edit docker

ちなみに、この時Ubuntu Linux 22.04 LTSだずnanoが起動しお困ったので、気になる人は以䞋のコマンドで奜みの゚ディタヌを指定するように
しおください。

$ sudo update-alternatives --config editor

systemctl editを実行するず、以䞋のようにコメントアりトされた元の内容が衚瀺されるので、こちらを参考にドロップむンファむルを
䜜成したしょう。

### Editing /etc/systemd/system/docker.service.d/override.conf
### Anything between here and the comment below will become the new contents of the file



### Lines below this comment will be discarded

### /lib/systemd/system/docker.service
# [Unit]
# Description=Docker Application Container Engine
# Documentation=https://docs.docker.com
# After=network-online.target docker.socket firewalld.service containerd.service time-set.target
# Wants=network-online.target containerd.service
# Requires=docker.socket
#
# [Service]
〜省略〜

今回は、こんな感じのファむルにしたした。

[Service]
Environment="http_proxy=http://localhost:8080"
Environment="https_proxy=http://localhost:8080"

ファむル名は、デフォルトでoverride.confになりたす。今回は/etc/systemd/system/docker.service.d/override.confになりたした。

あずは再起動すれば倉曎が反映されたす。

$ sudo systemctl restart docker

ここで、systemctl editの埌にsystemctl daemon-reloadは芁りたせん。

この意味でも、systemctl editを䜿うべきな気がしたすね 。

systemdのナニット定矩ファむルを確認する本䜓のファむルドロップむンファむル

この状態で、systemctl catで確認しおみたす。

$ systemctl cat docker

ドロップむンファむル含めお衚瀺されおいたす。

# /lib/systemd/system/docker.service
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target docker.socket firewalld.service containerd.service time-set.target
Wants=network-online.target containerd.service
Requires=docker.socket

[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutStartSec=0
RestartSec=2
Restart=always

# Note that StartLimit* options were moved from "Service" to "Unit" in systemd 229.
# Both the old, and new location are accepted by systemd 229 and up, so using the old location
# to make them work for either version of systemd.
StartLimitBurst=3

# Note that StartLimitInterval was renamed to StartLimitIntervalSec in systemd 230.
# Both the old, and new name are accepted by systemd 230 and up, so using the old name to make
# this option work for either version of systemd.
StartLimitInterval=60s

# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNPROC=infinity
LimitCORE=infinity

# Comment TasksMax if your systemd version does not support it.
# Only systemd 226 and above support this option.
TasksMax=infinity

# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes

# kill only the docker process, not all processes in the cgroup
KillMode=process
OOMScoreAdjust=-500

[Install]
WantedBy=multi-user.target

# /etc/systemd/system/docker.service.d/override.conf
[Service]
Environment="http_proxy=http://localhost:8080"
Environment="https_proxy=http://localhost:8080"

本䜓のナニット定矩ファむルを線集する

次は、本䜓のナニット定矩ファむルを線集しおみたす。

たずsystemctl editでドロップむンファむルを以䞋のように倉曎。

/etc/systemd/system/docker.service.d/override.conf

[Service]
Environment="http_proxy=http://localhost:8080"
#Environment="https_proxy=http://localhost:8080"

コメントアりトした方は、本䜓のナニット定矩ファむルに远加するこずにしたしょう。

この堎合に䜿うコマンドは、systemctl edit --fullです。

$ sudo systemctl edit --full docker

--fullオプションを付けるず、本䜓のナニット定矩ファむルを線集できたす。今回は、以䞋のように倉曎。

〜省略〜

[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutStartSec=0
RestartSec=2
Restart=always
Environment="https_proxy=http://localhost:8080"

〜省略〜

あずは再起動すれば倉曎が反映されたす。

$ sudo systemctl restart docker

倉曎内容を元に戻す

ここたでで、むンストヌル時点のナニット定矩ファむルずは、このような差になりたした。

$ diff <(systemctl cat docker) /lib/systemd/system/docker.service
1d0
< # /etc/systemd/system/docker.service
18a18
> Environment="http_proxy=http://localhost:8080"
49,53d48
<
< # /etc/systemd/system/docker.service.d/override.conf
< [Service]
< Environment="http_proxy=http://localhost:8080"
< #Environment="https_proxy=http://localhost:8080"

これを元に戻すには、systemctl revertを実行したす。

$ sudo systemctl revert docker

実行ログ。

Removed /etc/systemd/system/docker.service.d/override.conf.
Removed /etc/systemd/system/docker.service.d.
Removed /etc/systemd/system/docker.service.

差分がなくなりたした。

$ diff <(systemctl cat docker) /lib/systemd/system/docker.service
1d0
< # /lib/systemd/system/docker.service

新しくナニット定矩ファむルを䜜成する

systemctlコマンドで新しくナニット定矩ファむルを䜜成する堎合は、systemctl edit --force --fullコマンドを䜿いたす。

$ sudo systemctl edit --force --full my-service

この堎合、/etc/systemd/system配䞋にファむルを䜜成しようずしたす。

自分で甚意したナニット定矩ファむルをrevertするず

ずころで、自分で甚意したナニット定矩ファむルをsystemctl revertするずどうなるのでしょう

䜜成しおみたす。

$ sudo systemctl edit --force --full my-httpd

こんな内容にしおみたした。

$ systemctl cat my-httpd
# /etc/systemd/system/my-httpd.service
[Unit]
Description=My Simple Http Server
After=networ-online.target
Wants=network-online.target

[Service]
Type=simple
ExecStart=python3 -m http.server --directory /tmp

[Install]
WantedBy=multi-user.target

起動するこずを確認。

$ sudo systemctl start my-httpd

systemctl revertしおみたす。

$ sudo systemctl revert my-httpd

特になにも衚瀺されたせん。systemctl catでも、そのたた䞭身が残っおいたす。

$ systemctl cat my-httpd
# /etc/systemd/system/my-httpd.service
[Unit]
Description=My Simple Http Server
After=networ-online.target
Wants=network-online.target

[Service]
Type=simple
ExecStart=python3 -m http.server --directory /tmp

[Install]
WantedBy=multi-user.target

それもそのはずで、manペヌゞを芋るず「削陀されない」ず曞かれおいたす。

Note that if a unit file has no vendor-supplied version (i.e. is only defined below /etc/systemd/system or /run/systemd/system, but not in a unit file stored below /usr/), then it is not removed. Also, if a unit is masked, it is unmasked.

Ubuntu Manpage: systemctl - Control the systemd system and service manager

ちなみに、自分で甚意したナニット定矩ファむルにもsystemctl editでドロップむンファむルを䜜成するこずができたす。
このパタヌンはあたり䜿わない気もしたすが。

おわりに

systemdのナニット定矩ファむルを確認、線集、そしおむンストヌルした時点のものに戻す方法を調べおみたした。

今たでふ぀うに゚ディタヌで線集しおいたので、こうやっおコマンドで操䜜できるこずを知りたせんでした 。
コマンドのmanペヌゞずか芋ないずダメですね 。