🌏

Ubuntuにおけるカーネルの変更方法

2024/04/23に公開

カーネルのアップデート方法に関する情報は見つかるが元のバージョンに戻したり、それ以前のバージョンに戻す方法に関する情報はまとまったものは見つからなかったのでまとめる。


カーネルの確認

まずは現在のカーネルを確認しておく。

uname -r

現在インストールされているカーネルについては以下のコマンドで確認できる。

ls /lib/modules

カーネルの変更

/boot/grub/grub.cfgから現在のカーネルの情報を確認

egrep '^\s*(menuentry|submenu)\s' /boot/grub/grub.cfg
menuentry 'Ubuntu' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-simple-be4c6937-f550-4a49-8ff3-264f2ca28c5f' {
submenu 'Advanced options for Ubuntu' $menuentry_id_option 'gnulinux-advanced-be4c6937-f550-4a49-8ff3-264f2ca28c5f' {
        menuentry 'Ubuntu, with Linux 5.15.0-102-generic' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-5.15.0-102-generic-advanced-be4c6937-f550-4a49-8ff3-264f2ca28c5f' {
        menuentry 'Ubuntu, with Linux 5.15.0-102-generic (recovery mode)' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-5.15.0-102-generic-recovery-be4c6937-f550-4a49-8ff3-264f2ca28c5f' {
        menuentry 'Ubuntu, with Linux 5.15.0-101-generic' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-5.15.0-101-generic-advanced-be4c6937-f550-4a49-8ff3-264f2ca28c5f' {
        menuentry 'Ubuntu, with Linux 5.15.0-101-generic (recovery mode)' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-5.15.0-101-generic-recovery-be4c6937-f550-4a49-8ff3-264f2ca28c5f' {
menuentry 'UEFI Firmware Settings' $menuentry_id_option 'uefi-firmware' {

変更するカーネルの記述方法が2パターンあるので以下で説明する。

a. カーネルバージョンを文字列で指定して設定

以下のコマンドで整形して出力

 grep 'menuentry ' /boot/grub/grub.cfg | cut -f 2 -d "'" | nl -v 0
     0  Ubuntu
     1  Ubuntu, with Linux 5.15.0-102-generic
     2  Ubuntu, with Linux 5.15.0-102-generic (recovery mode)
     3  Ubuntu, with Linux 5.15.0-101-generic
     4  Ubuntu, with Linux 5.15.0-101-generic (recovery mode)
     5  UEFI Firmware Settings

例えば、5.15.0-101-genericに戻したい場合は上記のコマンドで確認後に以下のコマンドでカーネルを変更

# 次回起動時のみ
sudo grub-reboot "Advanced options for Ubuntu>Ubuntu, with Linux 5.15.0-101-generic"
# デフォルトに設定
sudo grub-set-default "Advanced options for Ubuntu>Ubuntu, with Linux 5.15.0-101-generic"

Advanced options for Ubuntu>を含めて記述するのがポイントとなる。

b. カーネルバージョンをインデックスで指定して設定

以下のコマンドで整形して出力

awk -F\' -v l=-1 -v ll=-1 \
    '/menuentry |submenu / {
    if ($1=="menuentry ") {l+=1; print l "\t" $2}
    else if ($1=="submenu ") {l+=1; ll=-1; print l "\t" $2}
    else {ll+=1; print "\t" l ">" ll "\t" $2}
    }' \
/boot/grub/grub.cfg
0       Ubuntu
1       Advanced options for Ubuntu
        1>0     Ubuntu, with Linux 5.15.0-102-generic
        1>1     Ubuntu, with Linux 5.15.0-102-generic (recovery mode)
        1>2     Ubuntu, with Linux 5.15.0-101-generic
        1>3     Ubuntu, with Linux 5.15.0-101-generic (recovery mode)
2       UEFI Firmware Settings

例えば、5.15.0-101-genericに戻したい場合は上記のコマンドで確認後に以下のコマンドでカーネルを変更

# 次回起動時のみ
grub-reboot '1>2'
# デフォルトに設定
sudo grub-set-default '1>2'

一度に両パターンの設定方法を出力

awk -v var="$(egrep '^\s*submenu\s' /boot/grub/grub.cfg | cut -f 2 -d "'")" \
    -F \' -v l=-1 -v ll=-1 \
    '/menuentry |submenu / {
    if ($1=="menuentry ") {l+=1}
    else if ($1=="submenu ") {l+=1; ll=-1;}
    else {ll+=1; print l ">" ll "\t" var ">" $2}
    }' \
/boot/grub/grub.cfg
1>0     Advanced options for Ubuntu>Ubuntu, with Linux 5.15.0-102-generic
1>1     Advanced options for Ubuntu>Ubuntu, with Linux 5.15.0-102-generic (recovery mode)
1>2     Advanced options for Ubuntu>Ubuntu, with Linux 5.15.0-101-generic
1>3     Advanced options for Ubuntu>Ubuntu, with Linux 5.15.0-101-generic (recovery mode)

GRUB_DEFAULTの設定

前述の設定だけでは上手く適用されないので/etc/default/grub.d/99-use-saved-entry.cfgファイルを作成し、GRUB_DEFAULT=savedを設定する。

sudo vim /etc/default/grub.d/99-use-saved-entry.cfg
/etc/default/grub.d/99-use-saved-entry.cfg
GRUB_DEFAULT=saved

エディタでの編集が面倒な場合は以下を実行

sudo echo 'GRUB_DEFAULT=saved' >> /etc/default/grub.d/99-use-saved-entry.cfg

grubのアップデート

上記まで設定後、grubをアップデート

sudo update-grub

以下のようなlogが出力される。

Sourcing file `/etc/default/grub'
Sourcing file `/etc/default/grub.d/99-use-saved-entry.cfg'
Sourcing file `/etc/default/grub.d/init-select.cfg'
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-5.15.0-102-generic
Found initrd image: /boot/initrd.img-5.15.0-102-generic
Found linux image: /boot/vmlinuz-5.15.0-101-generic
Found initrd image: /boot/initrd.img-5.15.0-101-generic
Memtest86+ needs a 16-bit boot, that is not available on EFI, exiting
Warning: os-prober will not be executed to detect other bootable partitions.
Systems on them will not be added to the GRUB boot configuration.
Check GRUB_DISABLE_OS_PROBER documentation entry.
Adding boot menu entry for UEFI Firmware Settings ...
done
  • etc/default/grub.d/99-use-saved-entry.cfgが 読み込まれていることを確認
  • Warning: Please don't use old title ...のlogが出力されていないことを確認
    ※出力されている場合はカーネルの指定方法が誤っているので確認すること。

問題なければ、マシンの再起動(sudo reboot)
再起動後、uname -rで指定したカーネルバージョンに変更されていれば完了。


reference

Discussion