/
2021-07-03.post
69 lines (52 loc) · 3.85 KB
/
2021-07-03.post
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
;;;;;
title: インストール済みのGentooをセキュアブートで起動できるように設定した時のメモ
tags: Gentoo, Linux
date: 2021-07-03 09:42:56
format: md
;;;;;
前提条件としてセキュアブートをオフにしてインストールしたGentooかつ,ブートローダーにGRUB2をinitにsystemdを採用し尚且つsys-boot/shim,app-crypt/sbsigntools,sys-boot/efibootmgrをインストール済みの状態であることを想定している.
GRUBインストール時のオプションは
```
sudo grub-install --target=x86_64-efi --efi-directory=/boot/efi --modules="tpm" --disable-shim-lock
```
を想定.これについては[こちら](https://forum.manjaro.org/t/grub-fails-to-load-with-shim-and-secure-boot-enabled/62522/2)を参照.しておかないとブート時にエラーが出る.UEFIでTPMを有効しないといけないかは検証していない(多分要る気はする).
セキュアブートとTPMの有効方法はマザーボードごとに違ったりするのでここでは触れない.
またここに書かれていることは2021-07-03時点の物であり,環境差もあるので動作を保証しない.
# setup shim
```
cp /usr/share/shim/BOOTX64.EFI /boot/efi/EFI/gentoo/ && cp /usr/share/shim/mmx64.efi /boot/efi/EFIT/gentoo/
```
## EFIに登録する
```
efibootmgr -c -d /dev/nvme0n1 -p 1 -L "shim" -l "\EFI\gentoo\BOOTX64.EFI"
```
dオプションはEFIパーティションのあるデバイスをpオプションにはパーティションナンバーを入れる.
# Sign to kernel and grub
opensslを使う.
[参考URL:https://wiki.archlinux.org/title/Unified_Extensible_Firmware_Interface/Secure_Boot#shim](https://wiki.archlinux.org/title/Unified_Extensible_Firmware_Interface/Secure_Boot#shim)
```
openssl req -newkey rsa:4096 -nodes -keyout MOK.key -new -x509 -sha256 -subj "/CN= My Key /" -out MOK.crt
```
daysオプションで有効期限を設定した方が良いかと思ったが煩わしいのでやめた.
```
openssl x509 -outform DER -in MOK.crt -out MOK.cer
```
このcerファイルは/boot/efi/EFI/gentoo/に放り込んでおく.
セキュアブートで起動した時に証明書が無いとmmx64.efiが起動し,keyを指定してenrollしろと言われるのでMOK.cerを置いた場所を指定する必要があるのでよしなにして.
```
sbsign --key MOK.key --cert MOK.crt --output vmlinuz-path vmlinuz-path
```
vmlinuz-pathのところを/boot/下にある署名したいvmlinuzファイルのパスに置き換えて実行.
## Example:
```
sbsign --key MOK.key --cert MOK.crt --output /boot/efi/EFI/gentoo/grubx64.efi /boot/efi/EFI/gentoo/grubx64.efi
```
以上.
UEFIに戻ってセキュアブートを有効にして,shimラベルのやつをブートに指定すればmmx64.efiが起動して証明書を指定するように言われるのでそれをして再起動すればブートできるだろう.
# 追記
カーネルやGRUBをアップデートする度に署名する必要がある.
またgrub-mkconfigで設定をアップデートする際にEFIが書き換えられて手動定義したラベルが消えるっぽいのでefibootmgrで登録し直す必要があるだろう(回避策があるかもしれないが調べるの面倒なので).
# 参考文献
- [https://wiki.archlinux.org/title/Unified_Extensible_Firmware_Interface/Secure_Boot#shim](https://wiki.archlinux.org/title/Unified_Extensible_Firmware_Interface/Secure_Boot#shim)
- [https://forums.gentoo.org/viewtopic-t-1132536-start-0.html](https://forums.gentoo.org/viewtopic-t-1132536-start-0.html)
- [https://forum.manjaro.org/t/grub-fails-to-load-with-shim-and-secure-boot-enabled/62522/2](https://forum.manjaro.org/t/grub-fails-to-load-with-shim-and-secure-boot-enabled/62522/2)