AlmaLinux9.7のgrub.cfgを読み解く ‐ GRUB
はじめに
以前、レガシーOS(RHEL3)をAmazon EC2へ力技で移行する という記事を書きました。その手順7で grub.conf を手書きしてGRUBをインストールする箇所があります。
あの記事で扱ったのは GRUB Legacy です。一方、現在のAlmaLinux9が使うのは GRUB2 であり、設定ファイルの構造も運用の考え方もかなり変わっています。
この記事では、AlmaLinux9の /boot/grub2/grub.cfg を実際に読み解きながら、GRUB Legacyとの違いも踏まえて整理します。
GRUB LegacyとGRUB2の主な違い
まず大枠を把握しておきます。
| GRUB Legacy | GRUB2(AlmaLinux9) | |
|---|---|---|
| 設定ファイル |
/boot/grub/grub.conf(直接編集) |
/boot/grub2/grub.cfg(自動生成・直接編集禁止) |
| 編集方法 | テキストエディタで直接書く |
/etc/default/grub と /etc/grub.d/ を編集 → grub2-mkconfig で再生成 |
| エントリ定義 |
grub.conf にベタ書き |
BLS形式(/boot/loader/entries/*.conf)に分離 |
| スクリプト構造 | 単一ファイル | セクションごとに /etc/grub.d/ のスクリプトが生成を担当 |
| パスワード保護 |
password コマンド(平文も可) |
PBKDF2ハッシュのみ |
| 起動失敗の自動対処 | なし | boot_counter によるロールバック機能あり |
grub.cfgの全体構造
grub.cfg 全文(クリックで展開)
#
# DO NOT EDIT THIS FILE
#
# It is automatically generated by grub2-mkconfig using templates
# from /etc/grub.d and settings from /etc/default/grub
#
### BEGIN /etc/grub.d/00_header ###
set pager=1
if [ -f ${config_directory}/grubenv ]; then
load_env -f ${config_directory}/grubenv
elif [ -s $prefix/grubenv ]; then
load_env
fi
if [ "${next_entry}" ] ; then
set default="${next_entry}"
set next_entry=
save_env next_entry
set boot_once=true
else
set default="${saved_entry}"
fi
if [ x"${feature_menuentry_id}" = xy ]; then
menuentry_id_option="--id"
else
menuentry_id_option=""
fi
export menuentry_id_option
if [ "${prev_saved_entry}" ]; then
set saved_entry="${prev_saved_entry}"
save_env saved_entry
set prev_saved_entry=
save_env prev_saved_entry
set boot_once=true
fi
function savedefault {
if [ -z "${boot_once}" ]; then
saved_entry="${chosen}"
save_env saved_entry
fi
}
function load_video {
if [ x$feature_all_video_module = xy ]; then
insmod all_video
else
insmod efi_gop
insmod efi_uga
insmod ieee1275_fb
insmod vbe
insmod vga
insmod video_bochs
insmod video_cirrus
fi
}
terminal_output console
if [ x$feature_timeout_style = xy ] ; then
set timeout_style=menu
set timeout=5
# Fallback normal timeout code in case the timeout_style feature is
# unavailable.
else
set timeout=5
fi
### END /etc/grub.d/00_header ###
### BEGIN /etc/grub.d/00_tuned ###
set tuned_params=""
set tuned_initrd=""
### END /etc/grub.d/00_tuned ###
### BEGIN /etc/grub.d/01_users ###
if [ -f ${prefix}/user.cfg ]; then
source ${prefix}/user.cfg
if [ -n "${GRUB2_PASSWORD}" ]; then
set superusers="root"
export superusers
password_pbkdf2 root ${GRUB2_PASSWORD}
fi
fi
### END /etc/grub.d/01_users ###
### BEGIN /etc/grub.d/08_fallback_counting ###
insmod increment
# Check if boot_counter exists and boot_success=0 to activate this behaviour.
if [ -n "${boot_counter}" -a "${boot_success}" = "0" ]; then
# if countdown has ended, choose to boot rollback deployment,
# i.e. default=1 on OSTree-based systems.
if [ "${boot_counter}" = "0" -o "${boot_counter}" = "-1" ]; then
set default=1
set boot_counter=-1
# otherwise decrement boot_counter
else
decrement boot_counter
fi
save_env boot_counter
fi
### END /etc/grub.d/08_fallback_counting ###
### BEGIN /etc/grub.d/10_linux ###
insmod part_msdos
insmod xfs
set root='hd0,msdos1'
if [ x$feature_platform_search_hint = xy ]; then
search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1 --hint='hd0,msdos1' 16de323b-555f-4ac1-be9c-4b0465ea3197
else
search --no-floppy --fs-uuid --set=root 16de323b-555f-4ac1-be9c-4b0465ea3197
fi
insmod part_msdos
insmod xfs
set boot='hd0,msdos1'
if [ x$feature_platform_search_hint = xy ]; then
search --no-floppy --fs-uuid --set=boot --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1 --hint='hd0,msdos1' 16de323b-555f-4ac1-be9c-4b0465ea3197
else
search --no-floppy --fs-uuid --set=boot 16de323b-555f-4ac1-be9c-4b0465ea3197
fi
# This section was generated by a script. Do not modify the generated file - all changes
# will be lost the next time file is regenerated. Instead edit the BootLoaderSpec files.
#
# The blscfg command parses the BootLoaderSpec files stored in /boot/loader/entries and
# populates the boot menu. Please refer to the Boot Loader Specification documentation
# for the files format: https://systemd.io/BOOT_LOADER_SPECIFICATION/.
# The kernelopts variable should be defined in the grubenv file. But to ensure that menu
# entries populated from BootLoaderSpec files that use this variable work correctly even
# without a grubenv file, define a fallback kernelopts variable if this has not been set.
#
# The kernelopts variable in the grubenv file can be modified using the grubby tool or by
# executing the grub2-mkconfig tool. For the latter, the values of the GRUB_CMDLINE_LINUX
# and GRUB_CMDLINE_LINUX_DEFAULT options from /etc/default/grub file are used to set both
# the kernelopts variable in the grubenv file and the fallback kernelopts variable.
if [ -z "${kernelopts}" ]; then
set kernelopts="root=/dev/mapper/almalinux-root ro crashkernel=1G-4G:192M,4G-64G:256M,64G-:512M resume=/dev/mapper/almalinux-swap rd.lvm.lv=almalinux/root rd.lvm.lv=almalinux/swap rhgb quiet "
fi
insmod blscfg
blscfg
### END /etc/grub.d/10_linux ###
### BEGIN /etc/grub.d/10_reset_boot_success ###
# Hiding the menu is ok if last boot was ok or if this is a first boot attempt to boot the entry
if [ "${boot_success}" = "1" -o "${boot_indeterminate}" = "1" ]; then
set menu_hide_ok=1
else
set menu_hide_ok=0
fi
# Reset boot_indeterminate after a successful boot
if [ "${boot_success}" = "1" ] ; then
set boot_indeterminate=0
# Avoid boot_indeterminate causing the menu to be hidden more than once
elif [ "${boot_indeterminate}" = "1" ]; then
set boot_indeterminate=2
fi
# Reset boot_success for current boot
set boot_success=0
save_env boot_success boot_indeterminate
### END /etc/grub.d/10_reset_boot_success ###
### BEGIN /etc/grub.d/12_menu_auto_hide ###
if [ x$feature_timeout_style = xy ] ; then
if [ "${menu_show_once}" ]; then
unset menu_show_once
save_env menu_show_once
set timeout_style=menu
set timeout=60
elif [ "${menu_auto_hide}" -a "${menu_hide_ok}" = "1" ]; then
set orig_timeout_style=${timeout_style}
set orig_timeout=${timeout}
if [ "${fastboot}" = "1" ]; then
# timeout_style=menu + timeout=0 avoids the countdown code keypress check
set timeout_style=menu
set timeout=0
else
set timeout_style=hidden
set timeout=1
fi
fi
fi
### END /etc/grub.d/12_menu_auto_hide ###
### BEGIN /etc/grub.d/14_menu_show_once ###
if [ x$feature_timeout_style = xy ]; then
if [ "${menu_show_once_timeout}" ]; then
set timeout_style=menu
set timeout="${menu_show_once_timeout}"
unset menu_show_once_timeout
save_env menu_show_once_timeout
fi
fi
### END /etc/grub.d/14_menu_show_once ###
### BEGIN /etc/grub.d/20_linux_xen ###
### END /etc/grub.d/20_linux_xen ###
### BEGIN /etc/grub.d/20_ppc_terminfo ###
### END /etc/grub.d/20_ppc_terminfo ###
### BEGIN /etc/grub.d/30_os-prober ###
### END /etc/grub.d/30_os-prober ###
### BEGIN /etc/grub.d/30_uefi-firmware ###
if [ "$grub_platform" = "efi" ]; then
menuentry 'UEFI Firmware Settings' $menuentry_id_option 'uefi-firmware' {
fwsetup
}
fi
### END /etc/grub.d/30_uefi-firmware ###
### BEGIN /etc/grub.d/35_fwupd ###
### END /etc/grub.d/35_fwupd ###
### BEGIN /etc/grub.d/40_custom ###
# This file provides an easy way to add custom menu entries. Simply type the
# menu entries you want to add after this comment. Be careful not to change
# the 'exec tail' line above.
### END /etc/grub.d/40_custom ###
### BEGIN /etc/grub.d/41_custom ###
if [ -f ${config_directory}/custom.cfg ]; then
source ${config_directory}/custom.cfg
elif [ -z "${config_directory}" -a -f $prefix/custom.cfg ]; then
source $prefix/custom.cfg
fi
### END /etc/grub.d/41_custom ###
ファイル冒頭にこう書いてあります。
# DO NOT EDIT THIS FILE
# It is automatically generated by grub2-mkconfig
grub2-mkconfig を実行するたびに上書きされるため、直接編集しても無意味です。設定変更は後述の正しい手順で行います。
ファイルは ### BEGIN /etc/grub.d/XX_xxx ### で区切られており、各スクリプトが生成したセクションが順番に結合された構造です。
各セクションの解説
00_header:基本変数の初期化
set pager=1
GRUBのコマンドライン出力をページングします。長いメッセージが流れなくなります。
if [ -f ${config_directory}/grubenv ]; then
load_env -f ${config_directory}/grubenv
elif [ -s $prefix/grubenv ]; then
load_env
fi
/boot/grub2/grubenv から起動状態の変数(後述の boot_counter や saved_entry 等)を読み込みます。grub2-editenv list で現在の内容を確認できます。
if [ "${next_entry}" ] ; then
set default="${next_entry}"
set next_entry=
save_env next_entry
set boot_once=true
else
set default="${saved_entry}"
fi
next_entry が設定されていれば「次の1回だけ別エントリで起動」します。grub2-reboot コマンドで使われる仕組みです。通常は saved_entry(前回起動したエントリ)がデフォルトになります。
set timeout_style=menu
set timeout=5
タイムアウト5秒。timeout_style=menu はカウントダウン中もメニューを表示します。hidden にすると非表示になり起動が速くなります(後述の 12_menu_auto_hide で自動制御されます)。
01_users:GRUBパスワード保護
if [ -f ${prefix}/user.cfg ]; then
source ${prefix}/user.cfg
if [ -n "${GRUB2_PASSWORD}" ]; then
set superusers="root"
export superusers
password_pbkdf2 root ${GRUB2_PASSWORD}
fi
fi
/boot/grub2/user.cfg にパスワードハッシュが設定されていれば、GRUBメニューの編集や対話的コマンドラインにパスワードが要求されます。
設定方法は grub2-setpassword コマンドを使います。
sudo grub2-setpassword
# プロンプトに従いパスワードを入力するとuser.cfgに書き込まれる
08_fallback_counting:起動失敗時の自動ロールバック
insmod increment
if [ -n "${boot_counter}" -a "${boot_success}" = "0" ]; then
if [ "${boot_counter}" = "0" -o "${boot_counter}" = "-1" ]; then
set default=1
set boot_counter=-1
else
decrement boot_counter
fi
save_env boot_counter
fi
GRUB Legacyにはなかった自動ロールバック機能です。仕組みはこうです。
-
boot_counterに試行回数をセット - 起動成功(
boot_success=1)にならないまま再起動するたびにカウントダウン - カウンタが0になったら
default=1(2番目のエントリ=前のカーネル)に切り替える
通常は boot_counter が設定されていないので動作しませんが、カーネルアップデート後に一定回数失敗したら自動で前のカーネルに戻す運用に使えます。
# boot_counterを手動でセット(例:3回失敗したらロールバック)
sudo grub2-editenv /boot/grub2/grubenv set boot_counter=3
10_linux:カーネルエントリ本体
このセクションが最重要です。
insmod part_msdos
insmod xfs
set root='hd0,msdos1'
search --no-floppy --fs-uuid --set=root ... 16de323b-555f-4ac1-be9c-4b0465ea3197
/boot パーティションをUUIDで特定しています。デバイス名(hd0,msdos1)はあくまでヒントで、最終的にはUUIDで正確なパーティションを見つけます。
if [ -z "${kernelopts}" ]; then
set kernelopts="root=/dev/mapper/almalinux-root ro crashkernel=... rd.lvm.lv=almalinux/root rd.lvm.lv=almalinux/swap rhgb quiet "
fi
カーネルパラメータのフォールバック定義です。通常は grubenv に保存された kernelopts 変数が使われ、このフォールバックは使われません。
主要なオプションの意味は以下の通りです。
| オプション | 意味 |
|---|---|
root=/dev/mapper/almalinux-root |
LVM上のrootボリュームを指定 |
ro |
initramfs段階はread-onlyでマウント |
crashkernel=1G-4G:192M,... |
kdump用メモリ予約(RAM容量に応じて可変) |
resume=/dev/mapper/almalinux-swap |
ハイバネーション復帰元のswap |
rd.lvm.lv=almalinux/root |
initramfs段階でアクティベートするLV |
rhgb quiet |
グラフィカル起動画面、起動ログ抑制 |
insmod blscfg
blscfg
ここが最大の変化点です。BLS(Boot Loader Specification) 形式により、実際のカーネルエントリは /boot/loader/entries/ 配下の .conf ファイルに分離されています。blscfg はそれらを読み込んでメニューを動的生成します。
ls /boot/loader/entries/
# 例:
# xxxxxxxx-0-5.14.0-503.21.1.el9_5.x86_64.conf ← 新カーネル
# xxxxxxxx-0-5.14.0-427.13.1.el9_4.x86_64.conf ← 前のカーネル
カーネルのインストール・削除のたびにここのファイルが増減します。GRUB2の設定を直接触らなくてもカーネル管理ができるのはこの仕組みのおかげです。
10_reset_boot_success / 12_menu_auto_hide:起動成功フラグとメニュー制御
set boot_success=0
save_env boot_success boot_indeterminate
毎起動時に boot_success を0にリセットします。systemdが起動完了後にこれを1にセットします。これが0のまま次回起動を迎えると「前回失敗した」と判定されます。
elif [ "${menu_auto_hide}" -a "${menu_hide_ok}" = "1" ]; then
set timeout_style=hidden
set timeout=1
前回起動が成功していれば(menu_hide_ok=1)GRUBメニューを非表示にして起動を高速化します。問題があるときだけメニューが表示される、という挙動です。
/etc/default/grub に以下を追加すると有効になります。
GRUB_AUTO_HIDE=1
30_uefi-firmware:UEFI設定メニュー
if [ "$grub_platform" = "efi" ]; then
menuentry 'UEFI Firmware Settings' ... {
fwsetup
}
fi
EFI環境でのみ表示されます。選択するとマザーボードのUEFI設定画面に入れます。BIOS環境では表示されません。
設定変更の正しい手順
grub.cfg は直接編集しません。変更内容に応じて以下を使い分けます。
カーネルパラメータの永続変更
# /etc/default/grub を編集
sudo vi /etc/default/grub
# GRUB_CMDLINE_LINUX に追加したいパラメータを書く
# 再生成
sudo grub2-mkconfig -o /boot/grub2/grub.cfg
次回1回だけ別カーネルで起動
# エントリ一覧を確認
sudo grub2-editenv list
grubby --info=ALL | grep title
# 指定して再起動
sudo grub2-reboot "エントリ名"
デフォルト起動エントリの変更
# インデックスで指定
sudo grub2-set-default 1
# または grubby で特定カーネルを指定
sudo grubby --set-default /boot/vmlinuz-5.14.0-xxx.el9.x86_64
カーネルオプションを特定エントリだけ変更
# grubby を使うと BLS エントリを直接編集できる
sudo grubby --update-kernel /boot/vmlinuz-$(uname -r) --args "systemd.unit=rescue.target"
sudo grubby --update-kernel /boot/vmlinuz-$(uname -r) --remove-args "rhgb quiet"
grubenvの確認と操作
起動状態を保持する grubenv はトラブル時にも役立ちます。
# 現在の変数を確認
sudo grub2-editenv list
# 例の出力:
# saved_entry=xxxxxxxx-0-5.14.0-503.21.1.el9_5.x86_64
# boot_success=1
# kernelopts=root=/dev/mapper/almalinux-root ro ...
# 特定の変数を削除(異常な状態をリセットしたいとき)
sudo grub2-editenv /boot/grub2/grubenv unset boot_counter
GRUB Legacyからの移行作業との対比まとめ
前の記事のRHEL3移行手順と、今回のAlmaLinux9の管理方法を対比すると以下のようになります。
| 作業 | RHEL3(GRUB Legacy) | AlmaLinux9(GRUB2) |
|---|---|---|
| 設定ファイルの場所 | /boot/grub/grub.conf |
/boot/grub2/grub.cfg(自動生成) |
| カーネルエントリ追加 |
grub.conf に直書き |
/boot/loader/entries/*.conf に自動追加 |
| カーネルオプション変更 |
grub.conf の kernel 行を編集 |
grubby --update-kernel または /etc/default/grub
|
| GRUBのインストール | grub-install /dev/xvdb |
grub2-install /dev/xvda(同様) |
| デフォルトエントリ変更 |
grub.conf の default= を書き換え |
grub2-set-default |
| パスワード設定 |
password コマンド(平文可) |
grub2-setpassword(PBKDF2のみ) |
GRUBのインストール自体(grub2-install)は変わりませんが、その後の運用がガラリと変わっています。
まとめ
AlmaLinux9のGRUB2は、GRUB Legacyに比べて直接触る機会が大幅に減っています。というか、よほど特殊な事情がなければ基本的に触ることはないかと思います。
逆に言えば、設定の「どこを見れば何がわかるか」を知っておかないとトラブル時に迷います。特に以下の3点を押さえておくと役立つ気がします。
-
grubenvを見れば起動状態の変数がすべてわかる -
/boot/loader/entries/を見ればカーネルエントリの実体がわかる -
grubbyを使えばgrub.cfgを再生成せずにエントリを操作できる
参考
Discussion