LoginSignup
7
8

More than 1 year has passed since last update.

grubbyコマンドの使い方

Last updated at Posted at 2019-05-19

最新版は以下に移行しました。
https://hana-shin.hatenablog.com/entry/2023/01/26/202108

#1 grubbyコマンドとは?
ブートローダ(GRUB2等)のメニューエントリーの表示や変更を行うコマンドです。
本コマンドを使うことで、デフォルトカーネル(*)の変更や、カーネルパラメータを
変更することができます。
(*) システムを再起動したときに選択されるカーネル、という意味です。

#2 環境
VMware Workstation 14 Player上の仮想マシンを使いました。
仮想マシンは、「最小限のインストール」->「開発ツール」を選択して作成しました。

OS版数
[root@server ~]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)

[root@server ~]# uname -r
3.10.0-693.el7.x86_64
コマンド版数
[root@server ~]# grubby --version
grubby version 8.28

#3 オプション一覧

[root@server ~]# grubby --help
Usage: grubby [OPTION...]
  --add-kernel=kernel-path            add an entry for the specified kernel
  --add-multiboot=STRING              add an entry for the specified multiboot kernel
  --args=args                         default arguments for the new kernel or new arguments for kernel being updated
  --mbargs=STRING                     default arguments for the new multiboot kernel or new arguments for multiboot kernel
                                      being updated
  --bad-image-okay                    don't sanity check images in boot entries (for testing only)
  --boot-filesystem=bootfs            filesystem which contains /boot directory (for testing only)
  --bootloader-probe                  check which bootloader is installed on boot sector
  -c, --config-file=path              path to grub config file to update ("-" for stdin)
  --copy-default                      use the default boot entry as a template for the new entry being added; if the default
                                      is not a linux image, or if the kernel referenced by the default image does not exist,
                                      the first linux entry whose kernel does exist is used as the template
  --debug                             print debugging information for failures
  --default-kernel                    display the path of the default kernel
  --default-index                     display the index of the default kernel
  --default-title                     display the title of the default kernel
  --elilo                             configure elilo bootloader
  --efi                               force grub2 stanzas to use efi
  --env=path                          path for environment data
  --extlinux                          configure extlinux bootloader (from syslinux)
  --grub                              configure grub bootloader
  --grub2                             configure grub2 bootloader
  --info=kernel-path                  display boot information for specified kernel
  --initrd=initrd-path                initrd image for the new kernel
  -i, --extra-initrd=initrd-path      auxiliary initrd image for things other than the new kernel
  --lilo                              configure lilo bootloader
  --make-default                      make the newly added entry the default boot entry
  -o, --output-file=path              path to output updated config file ("-" for stdout)
  --remove-args=STRING                remove kernel arguments
  --remove-mbargs=STRING              remove multiboot kernel arguments
  --remove-kernel=kernel-path         remove all entries for the specified kernel
  --remove-multiboot=STRING           remove all entries for the specified multiboot kernel
  --set-default=kernel-path           make the first entry referencing the specified kernel the default
  --set-default-index=entry-index     make the given entry index the default entry
  --set-index=entry-index             use the given index when creating a new entry
  --silo                              configure silo bootloader
  --title=entry-title                 title to use for the new kernel entry
  --update-kernel=kernel-path         updated information for the specified kernel
  -v, --version                       print the version of this program and exit
  --yaboot                            configure yaboot bootloader
  --zipl                              configure zipl bootloader

Help options:
  -?, --help                          Show this help message
  --usage                             Display brief usage message

#4 事前準備
検証環境としてCentOS7.4を使っています。
現在(2019/5/18)のCentOS最新版は、CentOS7.6です。
事前準備として、CentOS7.6をインストールしておきます。

最新版のインストール
[root@server ~]# yum install kernel

CentOS7.4のカーネル版数は、3.10.0-693
CentOS7.6のカーネル版数は、3.10.0-957.12.2であることがわかります。

版数の確認
[root@server ~]# rpm -qa|grep kernel-3
kernel-3.10.0-693.el7.x86_64
kernel-3.10.0-957.12.2.el7.x86_64

#5 メニューエントリの表示方法

##5.1 すべてのエントリを表示する方法(--info=ALL)

全てのエントリを表示してみます。
メニューエントリには、下記4つのエントリがあることがわかります。
index=0
index=1
index=2
index=3
index=0はCentOS7.6,index=1はCentOS7.4のエントリです。

[root@server ~]# grubby --info=ALL
index=0
kernel=/boot/vmlinuz-3.10.0-957.12.2.el7.x86_64
args="ro rhgb quiet biosdevname=0 net.ifnames=0 LANG=ja_JP.UTF-8"
root=UUID=cbc1bb72-5030-404c-82ce-2ed6c7a878fa
initrd=/boot/initramfs-3.10.0-957.12.2.el7.x86_64.img
title=CentOS Linux (3.10.0-957.12.2.el7.x86_64) 7 (Core)
index=1
kernel=/boot/vmlinuz-3.10.0-693.el7.x86_64
args="ro rhgb quiet biosdevname=0 net.ifnames=0"
root=UUID=cbc1bb72-5030-404c-82ce-2ed6c7a878fa
initrd=/boot/initramfs-3.10.0-693.el7.x86_64.img
title=CentOS Linux (3.10.0-693.el7.x86_64) 7 (Core)
index=2
kernel=/boot/vmlinuz-0-rescue-ac70c8aec0554d09b5cc964fbeb035f2
args="ro rhgb quiet biosdevname=0 net.ifnames=0"
root=UUID=cbc1bb72-5030-404c-82ce-2ed6c7a878fa
initrd=/boot/initramfs-0-rescue-ac70c8aec0554d09b5cc964fbeb035f2.img
title=CentOS Linux (0-rescue-ac70c8aec0554d09b5cc964fbeb035f2) 7 (Core)
index=3
non linux entry

##5.2 インデックス番号を表示する方法(--default-index)
デフォルトカーネルのインデックス番号を表示してみます。
インデックス番号0のカーネルが、デフォルトカーネルであることがわかります。

[root@server ~]# grubby --default-index
0

##5.3 カーネルパスを表示する方法(--default-kernel)
デフォルトカーネルのカーネルパスを表示してみます。
カーネルパスが、以下であることがわかります。
/boot/vmlinuz-3.10.0-957.12.2.el7.x86_64

[root@server ~]# grubby --default-kernel
/boot/vmlinuz-3.10.0-957.12.2.el7.x86_64

##5.4 特定のエントリを表示する方法(--info)

CentOS7.4のエントリ
[root@server ~]# grubby --info=/boot/vmlinuz-3.10.0-693.el7.x86_64
index=1
kernel=/boot/vmlinuz-3.10.0-693.el7.x86_64
args="ro rhgb quiet biosdevname=0 net.ifnames=0"
root=UUID=cbc1bb72-5030-404c-82ce-2ed6c7a878fa
initrd=/boot/initramfs-3.10.0-693.el7.x86_64.img
title=CentOS Linux (3.10.0-693.el7.x86_64) 7 (Core)
CentOS7.6のエントリ
[root@server ~]# grubby --info=/boot/vmlinuz-3.10.0-957.12.2.el7.x86_64
index=0
kernel=/boot/vmlinuz-3.10.0-957.12.2.el7.x86_64
args="ro rhgb quiet biosdevname=0 net.ifnames=0 LANG=ja_JP.UTF-8"
root=UUID=cbc1bb72-5030-404c-82ce-2ed6c7a878fa
initrd=/boot/initramfs-3.10.0-957.12.2.el7.x86_64.img
title=CentOS Linux (3.10.0-957.12.2.el7.x86_64) 7 (Core)

#7 デフォルトカーネルの変更方法
##7.1 インデックスを使う方法(--set-default-index)

現在のカーネル版数を確認します。CentOS7.6であることがわかります。

版数の確認
[root@server ~]# uname -r
3.10.0-957.12.2.el7.x86_64

インデックスを使って、デフォルトカーネルを変更してみます。

デフォルトカーネルの変更
[root@server ~]# grubby --set-default-index=1

システムを再起動します。

再起動
[root@server ~]# shutdown -r now

OSがCentOS7.4に変更されたことがわかります。

版数の確認
[root@server ~]# uname -r
3.10.0-693.el7.x86_64

デフォルトインデックスが0から1に変更されたことがわかります。

grubbyの確認結果
[root@server ~]# grubby --default-index
1

##7.2 カーネルパスを使う方法(--set-default)
現在のカーネル版数を確認します。CentOS7.4であることがわかります。

版数の確認
[root@server ~]# uname -r
3.10.0-693.el7.x86_64

カーネルパスを使って、デフォルトカーネルを変更してみます。

デフォルトカーネルの変更
[root@server ~]# grubby --set-default=/boot/vmlinuz-3.10.0-957.12.2.el7.x86_64

システムを再起動します。

再起動
[root@server ~]# shutdown -r now

OSがCentOS7.6に変更されたことがわかります。

版数の確認
[root@server ~]# uname -r
3.10.0-957.12.2.el7.x86_64

デフォルトカーネルのパスが
/boot/vmlinuz-3.10.0-957.12.2.el7.x86_64に変更されたことがわかります。

grubbyの確認結果
[root@server ~]# grubby --default-kernel
/boot/vmlinuz-3.10.0-957.12.2.el7.x86_64

[root@server ~]# grubby --default-index
0

#8 カーネルパラメータを追加/削除する方法

##8.1 削除する方法(--remove-args)
カーネルパラメータ変更前の状態を確認します。
カーネルパラメータが、以下のようになっています。
args="ro rhgb quiet biosdevname=0 net.ifnames=0 LANG=ja_JP.UTF-8"

カーネルパラメータの確認
[root@server ~]# grubby --info=/boot/vmlinuz-3.10.0-957.12.2.el7.x86_64
index=0
kernel=/boot/vmlinuz-3.10.0-957.12.2.el7.x86_64
args="ro rhgb quiet biosdevname=0 net.ifnames=0 LANG=ja_JP.UTF-8"
root=UUID=cbc1bb72-5030-404c-82ce-2ed6c7a878fa
initrd=/boot/initramfs-3.10.0-957.12.2.el7.x86_64.img
title=CentOS Linux (3.10.0-957.12.2.el7.x86_64) 7 (Core)

カーネルパラメータ(rhgb,quiet)を削除します。

カーネルパラメータの削除
[root@server ~]# grubby --remove-args="rhgb quiet" --update-kernel=/boot/vmlinuz-3.10.0-957.12.2.el7.x86_64

システムを再起動します。

再起動
[root@server ~]# shutdown -r now

カーネルパラメータ(rhgb,quiet)が削除されていることがわかります。

カーネルパラメータの確認
[root@server ~]# grubby --info=/boot/vmlinuz-3.10.0-957.12.2.el7.x86_64
index=0
kernel=/boot/vmlinuz-3.10.0-957.12.2.el7.x86_64
args="ro biosdevname=0 net.ifnames=0 LANG=ja_JP.UTF-8"
root=UUID=cbc1bb72-5030-404c-82ce-2ed6c7a878fa
initrd=/boot/initramfs-3.10.0-957.12.2.el7.x86_64.img
title=CentOS Linux (3.10.0-957.12.2.el7.x86_64) 7 (Core)

##8.2 追加する方法(--args)
カーネルパラメータ変更前の状態を確認します。

カーネルパラメータの確認
[root@server ~]# grubby --info=/boot/vmlinuz-3.10.0-957.12.2.el7.x86_64
index=0
kernel=/boot/vmlinuz-3.10.0-957.12.2.el7.x86_64
args="ro biosdevname=0 net.ifnames=0 LANG=ja_JP.UTF-8"
root=UUID=cbc1bb72-5030-404c-82ce-2ed6c7a878fa
initrd=/boot/initramfs-3.10.0-957.12.2.el7.x86_64.img
title=CentOS Linux (3.10.0-957.12.2.el7.x86_64) 7 (Core)

カーネルパラメータ(rhgb,quiet)を追加します。

カーネルパラメータの追加
[root@server ~]# grubby --args="rhgb quiet" --update-kernel=/boot/vmlinuz-3.10.0-957.12.2.el7.x86_64

システムを再起動します。

再起動
[root@server ~]# shutdown -r now

カーネルパラメータ(rhgb,quiet)が追加されていることがわかります。

カーネルパラメータの確認
[root@server ~]# grubby --info=/boot/vmlinuz-3.10.0-957.12.2.el7.x86_64
index=0
kernel=/boot/vmlinuz-3.10.0-957.12.2.el7.x86_64
args="ro biosdevname=0 net.ifnames=0 LANG=ja_JP.UTF-8 rhgb quiet"
root=UUID=cbc1bb72-5030-404c-82ce-2ed6c7a878fa
initrd=/boot/initramfs-3.10.0-957.12.2.el7.x86_64.img
title=CentOS Linux (3.10.0-957.12.2.el7.x86_64) 7 (Core)

##8.3 全エントリのカーネルパラメータを一括で変更する方法(--update-kernel=ALL)

全エントリのカーネルパラメータにconsole=ttyS0,115200を追加してみます。

カーネルパラメータの変更
[root@server ~]# grubby --update-kernel=ALL --args=console=ttyS0,115200

システムを再起動します。

再起動
[root@server ~]# shutdown -r now

全てのカーネルパラメータにconsole=ttyS0,115200が追加されたことがわかります。

カーネルパラメータの確認
[root@server ~]# grubby --info=ALL
index=0
kernel=/boot/vmlinuz-3.10.0-957.12.2.el7.x86_64
args="ro biosdevname=0 net.ifnames=0 LANG=ja_JP.UTF-8 rhgb quiet console=ttyS0,115200"
root=UUID=cbc1bb72-5030-404c-82ce-2ed6c7a878fa
initrd=/boot/initramfs-3.10.0-957.12.2.el7.x86_64.img
title=CentOS Linux (3.10.0-957.12.2.el7.x86_64) 7 (Core)
index=1
kernel=/boot/vmlinuz-3.10.0-693.el7.x86_64
args="ro rhgb quiet biosdevname=0 net.ifnames=0 console=ttyS0,115200"
root=UUID=cbc1bb72-5030-404c-82ce-2ed6c7a878fa
initrd=/boot/initramfs-3.10.0-693.el7.x86_64.img
title=CentOS Linux (3.10.0-693.el7.x86_64) 7 (Core)
index=2
kernel=/boot/vmlinuz-0-rescue-ac70c8aec0554d09b5cc964fbeb035f2
args="ro rhgb quiet biosdevname=0 net.ifnames=0 console=ttyS0,115200"
root=UUID=cbc1bb72-5030-404c-82ce-2ed6c7a878fa
initrd=/boot/initramfs-0-rescue-ac70c8aec0554d09b5cc964fbeb035f2.img
title=CentOS Linux (0-rescue-ac70c8aec0554d09b5cc964fbeb035f2) 7 (Core)
index=3
non linux entry

#9 エントリを追加/削除する方法
##9.1 エントリを削除する方法(--remove-kernel)
エントリ削除前の全エントリを確認します。

エントリの確認
[root@server ~]# grubby --info=ALL
index=0
kernel=/boot/vmlinuz-3.10.0-957.12.2.el7.x86_64
args="ro biosdevname=0 net.ifnames=0 LANG=ja_JP.UTF-8 rhgb quiet console=ttyS0,115200"
root=UUID=cbc1bb72-5030-404c-82ce-2ed6c7a878fa
initrd=/boot/initramfs-3.10.0-957.12.2.el7.x86_64.img
title=CentOS Linux (3.10.0-957.12.2.el7.x86_64) 7 (Core)
index=1
kernel=/boot/vmlinuz-3.10.0-693.el7.x86_64
args="ro rhgb quiet biosdevname=0 net.ifnames=0 console=ttyS0,115200"
root=UUID=cbc1bb72-5030-404c-82ce-2ed6c7a878fa
initrd=/boot/initramfs-3.10.0-693.el7.x86_64.img
title=CentOS Linux (3.10.0-693.el7.x86_64) 7 (Core)
index=2
kernel=/boot/vmlinuz-0-rescue-ac70c8aec0554d09b5cc964fbeb035f2
args="ro rhgb quiet biosdevname=0 net.ifnames=0 console=ttyS0,115200"
root=UUID=cbc1bb72-5030-404c-82ce-2ed6c7a878fa
initrd=/boot/initramfs-0-rescue-ac70c8aec0554d09b5cc964fbeb035f2.img
title=CentOS Linux (0-rescue-ac70c8aec0554d09b5cc964fbeb035f2) 7 (Core)
index=3
non linux entry

/boot/vmlinuz-3.10.0-957.12.2.el7.x86_64のエントリを削除します。

エントリの削除
[root@server ~]# grubby --remove-kernel=/boot/vmlinuz-3.10.0-957.12.2.el7.x86_64

システムを再起動します。

再起動
[root@server ~]# shutdown -r now

/boot/vmlinuz-3.10.0-957.12.2.el7.x86_64のエントリが削除されたことがわかります。

エントリの確認
[root@server ~]# grubby --info=ALL
index=0
kernel=/boot/vmlinuz-3.10.0-693.el7.x86_64
args="ro rhgb quiet biosdevname=0 net.ifnames=0 console=ttyS0,115200"
root=UUID=cbc1bb72-5030-404c-82ce-2ed6c7a878fa
initrd=/boot/initramfs-3.10.0-693.el7.x86_64.img
title=CentOS Linux (3.10.0-693.el7.x86_64) 7 (Core)
index=1
kernel=/boot/vmlinuz-0-rescue-ac70c8aec0554d09b5cc964fbeb035f2
args="ro rhgb quiet biosdevname=0 net.ifnames=0 console=ttyS0,115200"
root=UUID=cbc1bb72-5030-404c-82ce-2ed6c7a878fa
initrd=/boot/initramfs-0-rescue-ac70c8aec0554d09b5cc964fbeb035f2.img
title=CentOS Linux (0-rescue-ac70c8aec0554d09b5cc964fbeb035f2) 7 (Core)
index=2
non linux entry

##9.2 エントリを追加する方法(--add-kernel)
エントリ追加前の全エントリを確認します。

エントリの確認
[root@server ~]# grubby --info=ALL
index=0
kernel=/boot/vmlinuz-3.10.0-693.el7.x86_64
args="ro rhgb quiet biosdevname=0 net.ifnames=0 console=ttyS0,115200"
root=UUID=cbc1bb72-5030-404c-82ce-2ed6c7a878fa
initrd=/boot/initramfs-3.10.0-693.el7.x86_64.img
title=CentOS Linux (3.10.0-693.el7.x86_64) 7 (Core)
index=1
kernel=/boot/vmlinuz-0-rescue-ac70c8aec0554d09b5cc964fbeb035f2
args="ro rhgb quiet biosdevname=0 net.ifnames=0 console=ttyS0,115200"
root=UUID=cbc1bb72-5030-404c-82ce-2ed6c7a878fa
initrd=/boot/initramfs-0-rescue-ac70c8aec0554d09b5cc964fbeb035f2.img
title=CentOS Linux (0-rescue-ac70c8aec0554d09b5cc964fbeb035f2) 7 (Core)
index=2
non linux entry

/boot/vmlinuz-3.10.0-957.12.2.el7.x86_64をエントリに追加します。
このとき、title,initrd,カーネルパラメータも指定します。
カーネルパラメータは、デフォルトカーネルのパラメータ使用(copy-default)を指定してみます。

[root@server ~]# grubby --add-kernel=/boot/vmlinuz-3.10.0-957.12.2.el7.x86_64 --title="CentOS7.6" --initrd=/boot/initramfs-3.10.0-957.12.2.el7.x86_64.img --copy-default

システムを再起動します。

再起動
[root@server ~]# shutdown -r now

エントリに/boot/vmlinuz-3.10.0-957.12.2.el7.x86_64が追加されたことがわかります。
また、エントリのtitle,initrd,カーネルパラメータは、指定したものが反映されていること
がわかります。

エントリの確認
[root@server ~]# grubby --info=ALL
index=0
kernel=/boot/vmlinuz-3.10.0-957.12.2.el7.x86_64
args="ro rhgb quiet biosdevname=0 net.ifnames=0 console=ttyS0,115200"
root=UUID=cbc1bb72-5030-404c-82ce-2ed6c7a878fa
initrd=/boot/initramfs-3.10.0-957.12.2.el7.x86_64.img
title=CentOS7.6
index=1
kernel=/boot/vmlinuz-3.10.0-693.el7.x86_64
args="ro rhgb quiet biosdevname=0 net.ifnames=0 console=ttyS0,115200"
root=UUID=cbc1bb72-5030-404c-82ce-2ed6c7a878fa
initrd=/boot/initramfs-3.10.0-693.el7.x86_64.img
title=CentOS Linux (3.10.0-693.el7.x86_64) 7 (Core)
index=2
kernel=/boot/vmlinuz-0-rescue-ac70c8aec0554d09b5cc964fbeb035f2
args="ro rhgb quiet biosdevname=0 net.ifnames=0 console=ttyS0,115200"
root=UUID=cbc1bb72-5030-404c-82ce-2ed6c7a878fa
initrd=/boot/initramfs-0-rescue-ac70c8aec0554d09b5cc964fbeb035f2.img
title=CentOS Linux (0-rescue-ac70c8aec0554d09b5cc964fbeb035f2) 7 (Core)
index=3
non linux entry

#Z 参考情報
25.4. GRUBBY ツールを使用した GRUB 2 メニューの永続的な変更
25.9. シリアルコンソールでの GRUB 2
Linux技術トレーニング 基本管理コース II
21.4. Configuring GRUB 2 Using the grubby Tool

CentOS シリアルコンソール

7
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
8