systemd-bootで起動するように構成する(Linux Mint 20)

前提

  • Linux Mint 20 (Ubuntu 20.04LTS ベース)

きっかけ

https://ibulog-iblog.hateblo.jp/entry/2020/06/14/015333

この記事を読んで、起動が 2〜3秒早くなる。という記述に興味を惹かれたので試しにやってみることに。

ポイント

  • ESP からしかカーネルをロードできない

ようするに、initrd.img と vmlinuz が /boot/efi に存在する必要がある。

普通にインストールすると、 /boot/efi(以降、ESP と呼ぶ)は、500MB の fat32 パーティションとして作られる。

initrd.img+vmlinuz で大体容量 100MB なので、余裕を見るとカーネルは3セットが限界。(4セットも行けると思うが)

当方の環境では、https://xanmod.org/ のカーネルが入っているので、これの最新で1セット、標準のカーネルと、標準のカーネルの一つ前のやつ。の3セットとした。

手順 1

できるだけギリギリまで後戻りできるような順番で作業した。

カーネルを ESP にコピーする systemd ユニットを作る

xanmod のカーネルは、標準のカーネルと違って vmlinuz / vmlinuz.old とか initrd.img / initrd.img.old みたいなのを作ってくれないので、/boot 自体に変更があったら全部のカーネルと initrd をコピーする構成とした。

以下の2ファイルを作成する。

/etc/systemd/system/systemd-boot-cp-to-efi.path

1
2
3
4
5
6
7
8
Description=Copy kernel and initrd to /boot/efi for systemd-boot
Documentation=man:systemd.path

[Path]
PathChanged=/boot/

[Install]
WantedBy=multi-user.target

/etc/systemd/system/systemd-boot-cp-to-efi.service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[Unit]
Description=Copy Kernel to ESP
Description=Trigger by path watch (systemd-boot-cp-efi.path)

[Service]
Type=oneshot

# ubuntu general kernel and initrd
ExecStart=/bin/cp -f /boot/vmlinuz /boot/efi/vmlinuz
ExecStart=/bin/cp -f /boot/initrd.img /boot/efi/initrd.img
ExecStart=/bin/cp -f /boot/vmlinuz.old /boot/efi/vmlinuz.old
ExecStart=/bin/cp -f /boot/initrd.img.old /boot/efi/initrd.img.old
ExecStart=/bin/bash -c '/bin/ls /boot/* | /bin/grep -e "\/boot\/vmlinuz.*xanmod" | /usr/bin/sort -Vr | /usr/bin/head -n 1 | /usr/bin/xargs -i /bin/cp -f {} /boot/efi/vmlinuz-xanmod'
ExecStart=/bin/bash -c '/bin/ls /boot/* | /bin/grep -e "\/boot\/initrd.*xanmod" | /usr/bin/sort -Vr | /usr/bin/head -n 1 | /usr/bin/xargs -i /bin/cp -f {} /boot/efi/initrd.img-xanmod'

systemd のユニットを有効化

ユニット有効化

1
2
3
$ sudo su -
# systemctl daemon-reload
# systemctl enable systemd-boot-cp-to-efi.path (serviceはenableにする必要はありません。してもエラーになります)

テスト

1
2
3
4
5
6
7
$ sudo su -
# rm /boot/efi/vmlinuz*
# rm /boot/efi/initrd*
# touch /boot/a (適当なファイルを/bootに作ってsystemdを反応させる)
# ls /boot/efi/ (予定通りコピーされているかチェック)
EFI initrd.img initrd.img.old vmlinuz vmlinuz.old
initrd.img-xanmod loader vmlinuz-xanmod

手順 2

systemd-boot のインストール

1
$ sudo bootctl install

ブートエントリ作成

カーネル3セット分のブートエントリを作成する。

/boot/efi/loader/entries/01xanmod.conf

1
2
3
4
title   Linux Mint (xanmod)
linux /vmlinuz-xanmod
initrd /initrd.img-xanmod
options root=LABEL=root quiet splash rw

/boot/efi/loader/entries/02ubuntu.conf

1
2
3
4
title   Linux Mint (ubuntu)
linux /vmlinuz
initrd /initrd.img
options root=LABEL=root quiet splash rw

/boot/efi/loader/entries/03ubuntu-old.conf

1
2
3
4
title   Linux Mint (ubuntu-old)
linux /vmlinuz.old
initrd /initrd.img.old
options root=LABEL=root quiet splash rw

デフォルト起動エントリの指定

/boot/efi/loader/loader.conf

1
2
3
#timeout 3
#console-mode keep
default 01xanmod # この行だけ変更。標準で起動させたい起動エントリの名前だけを書く(.confは書かない)

最終確認

1
2
3
4
5
6
7
8
9
$ efibootmgr

efibootmgr
BootCurrent: 0007
Timeout: 0 seconds
BootOrder: 0007,0005,0004,0003,0006,0002,0000 ←これが起動順
Boot0000* SanDisk SDSSDH3 500G :
Boot0005* ubuntu ←こっちはGRUB
Boot0007* Linux Boot Manager ←これがsystemd-boot

再起動

起動してくれば OK。おつかれさまでした。

蛇足

起動時に hp のロゴが出たまま →GUI ログイン画面になった。
起動が 2〜3秒早くなった。 なんかいい感じ。