kernel module(nvidia driver)に署名してsecure bootで困らないようにする

目下windows10とubuntu20.04のデュアルブートのマシンを主に使用している。 windows 11にupgradeするのにsecure boot関係でコケたら嫌だなぁと思って、キチンとsecure bootを有効にした。 以下はメモ書き。

仕組み

ubuntuのsecure bootの対応・仕組みはこちらに詳しい。

ubuntuがbootするときには

  • shim(Microsoftにより署名済み)
  • grub
  • if 鍵の管理するように言われている
    • MokManager
  • else
    • OS起動

という感じで起動するらしい。MOK: Machine-Owner Keyとのこと。 普通はMicrosoftに署名されたものしかブート時に読みこめないが、MokManagerを起動してそこに自分で作ったMOKを登録してあげると、 kernel moduleを事前にそのMOKで署名しておくことで読み込めるようになる、という風に理解した。

手順

secure bootを有効にして起動したubuntu20.04でnvidia driverを入れると、 ncursesなUIでMOKをshimに登録するためのパスワードを要求してきて、そこで勝手にMOKを生成してくれる。 dkmsによりビルドされたnvidia driverもその生成されたMOKにより署名つきになる。 もしそのMOK生成を手動で行うには

Use the following command to enroll an existing key into shim:
$ sudo update-secureboot-policy --enroll-key
If no MOK exists, the script will exit with a message to that effect. If the key is already enrolled, the script will exit, doing nothing. If the key exists but it not shown to be enrolled, the user will be prompted for a password to use after reboot, so that the key can be enrolled.
One can generate a new MOK using the following command:
$ sudo update-secureboot-policy --new-key

のようなコマンドで鍵が生成される。 出力先は /var/lib/shim-signed/mok/MOK.priv (秘密鍵) と /var/lib/shim-signed/mok/MOK.der (証明書、公開鍵)。

この公開鍵をshimに登録するようOSからお願いする。 先程のパスワードを入力する。

$ sudo mokutil --import /var/lib/shim-signed/mok/MOK.der
input password: xxxxx
input password again: xxxxx

これで再起動すると

Perform MOK management

 Continue boot
  <Enroll MOK>
Enroll key from disk
Enroll hash from disk

という画面が現れるので、 を選択し

[Enroll MOK]
View Key 0
<Continue>

でView Key 0で証明書の内容を確認したら。 あとは先程のpasswordをいれて再起動する。 nvidia-smiがちゃんとdriverを認識して色々出力してくれればOK.

他のdkmsなモジュールを自動でsignしてくれるスクリプトも見つけたものの、なぜかすでに勝手に登録されている。要確認。 先のURL(Ubuntu 20.04 その74 - UbuntuとUEFIセキュアブート - kledgeb)によるとdkmsが勝手に署名してくれるらしい。便利だなぁ。

ちなみにsecurebootではhibernationやsuspendはサポートされてないらしい。この点は不便だな。 unix.stackexchange.com