Published on

如何从 grub2 启动 Ubuntu Live CD iso

Authors
  • avatar
    Name
    ttyS3
    Twitter

上次写了《如何从 grub2 启动 Fedora32 Live CD iso》 , 这次顺便把 Ubuntu 的也补一下吧。

公司开发环境为了大家统一,全部采用的 Ubuntu.

这年头也很少随身带U盘了,因此,只靠硬盘, 自己能求自己,还是能在关键时候有用的。

下载live cd iso并校验

curl -LZO https://mirrors.tuna.tsinghua.edu.cn/ubuntu-releases/groovy/ubuntu-20.10-desktop-amd64.iso
curl -LO https://mirrors.tuna.tsinghua.edu.cn/ubuntu-releases/groovy/SHA256SUMS
curl -LO https://mirrors.tuna.tsinghua.edu.cn/ubuntu-releases/groovy/SHA256SUMS.gpg

校验 可 参考官方文档 https://ubuntu.com/tutorials/how-to-verify-ubuntu#4-retrieve-the-correct-signature-key

gpg --keyid-format long --verify SHA256SUMS.gpg SHA256SUMS
sha256sum -c SHA256SUMS

增加grub2启动项

先准备下文件,由于Ubuntu默认划分的 /boot 分区较小,因此不能放下live cd iso.

直接放到 / 吧。 准备下目录: sudo mkdir /opt/iso, 然后把 iso 文件 copy 到这个目录。

注意,这里的/ 分区我使用的是LVM, 因此,我们要使用 sudo blkid 找出 / 所在的 LV 的 UUID.

sudo blkid
/dev/mapper/vgubuntu-swap_1: UUID="f63131c3-9d02-4813-9db9-d85571f09084" TYPE="swap"
/dev/mapper/vgubuntu-root: UUID="cb94816d-569d-4658-963c-eb675052fc98" BLOCK_SIZE="4096" TYPE="ext4"
/dev/sda1: UUID="ba87f398-f52b-420f-9d19-af30a320c8f5" BLOCK_SIZE="4096" TYPE="xfs" PARTLABEL="Linux filesystem" PARTUUID="e21fd454-dc9a-41cb-8c72-c9fb05cb5d41"
/dev/nvme0n1p1: UUID="97CB-9857" BLOCK_SIZE="512" TYPE="vfat" PARTUUID="ca61970c-01"
/dev/nvme0n1p5: UUID="C7sb3l-laqt-gLDw-ji2s-yC3r-x5bw-Axa9yu" TYPE="LVM2_member" PARTUUID="ca61970c-05"

注意这里 /dev/mapper/vgubuntu-root: UUID="cb94816d-569d-4658-963c-eb675052fc98" BLOCK_SIZE="4096" TYPE="ext4" 就是 / 所在的LV了。 在grub2里,我们只需要取这里的cb94816d-569d-4658-963c-eb675052fc98 即可。

#!/bin/sh
exec tail -n +3 $0
# 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.

menuentry "Ubuntu 20.10 ISO" {
   set isofile="/opt/iso/ubuntu-20.10-desktop-amd64.iso"
   # or set isofile="/<username>/Downloads/ubuntu-20.04-desktop-amd64.iso"
   # if you use a single partition for your $HOME
   rmmod tpm
   search --no-floppy --fs-uuid --set=root cb94816d-569d-4658-963c-eb675052fc98
   loopback loop ($root)$isofile
   linux (loop)/casper/vmlinuz boot=casper iso-scan/filename=$isofile noprompt noeject
   initrd (loop)/casper/initrd
}

grub2 配置调整

Fedora 和 Ubuntu 现在都默认不显示grub menu了 (要开机时按esc才显示), 可以通过修改配置调整成总是显示:

修改 /etc/default/grub

#GRUB_TIMEOUT_STYLE=hidden
#GRUB_TIMEOUT_STYLE=countdown
GRUB_TIMEOUT_STYLE=menu

文档解释: GRUB_TIMEOUT_STYLE

If this option is unset or set to ‘menu’, then GRUB will display the menu and then wait for the timeout set by ‘GRUB_TIMEOUT’ to expire before booting the default entry. Pressing a key interrupts the timeout.

If this option is set to ‘countdown’ or ‘hidden’, then, before displaying the menu, GRUB will wait for the timeout set by ‘GRUB_TIMEOUT’ to expire. If ESC is pressed during that time, it will display the menu and wait for input. If a hotkey associated with a menu entry is pressed, it will boot the associated menu entry immediately. If the timeout expires before either of these happens, it will boot the default entry. In the ‘countdown’ case, it will show a one-line indication of the remaining time.

更新 grub2 配置文件

Ubuntu 下面有一个比较方便的 update-grub2 用来更新grub2配置, 该命令实际上是对 grub2-mkconfig 的便捷封装, 你不用指定配置文件路径,直接跑就行了。

sudo update-grub2
Sourcing file `/etc/default/grub'
Sourcing file `/etc/default/grub.d/init-select.cfg'
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-5.8.0-29-generic
Found initrd image: /boot/initrd.img-5.8.0-29-generic
Found linux image: /boot/vmlinuz-5.8.0-28-generic
Found initrd image: /boot/initrd.img-5.8.0-28-generic
Found linux image: /boot/vmlinuz-5.8.0-26-generic
Found initrd image: /boot/initrd.img-5.8.0-26-generic
Found memtest86+ image: /boot/memtest86+.elf
Found memtest86+ image: /boot/memtest86+.bin
done

参考文档

https://www.gnu.org/software/grub/manual/grub/html_node/Simple-configuration.html

https://unix.stackexchange.com/questions/516429/boot-iso-file-located-on-lvm-from-grub2

https://superuser.com/a/1289853/1162309

https://help.ubuntu.com/community/Grub2/ISOBoot#ISO_File_Location