Published on

如何从 grub2 启动 Fedora40 Live CD iso

Authors
  • avatar
    Name
    ttyS3
    Twitter

20240325: Fedora 40

/etc/grub.d/40_custom

#!/usr/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 "Fedora Workstation 40 Live" {
	# remove tpm mod to avoid out of memory err
	rmmod tpm
	insmod all_video
	set gfxpayload=keep
	insmod gzio
	insmod part_gpt
	insmod ext2
	insmod chain

	set isofile="/iso/Fedora-Workstation-Live-x86_64-40_Beta-1.10.iso"

	echo "Setting up loopback"
	loopback loop "${isofile}"

	isolabel="Fedora-WS-Live-40_B-1-10"
	# fedora grub2 no probe command
	#probe -l loop --set=isolabel
	echo "ISO Label is ${isolabel}"

	echo "Booting...."
	linux (loop)/images/pxeboot/vmlinuz iso-scan/filename="${isofile}" root=live:CDLABEL=$isolabel rootfstype=auto ro rd.live.image noquiet rd.luks=0 rd.md=0 rd.dm=0
	# disable KMS if getting a blank screen or a "no signal" error from the display, see https://wiki.archlinux.org/index.php/Kernel_mode_setting
	# nomodeset i915.modeset=0 nouveau.modeset=0 
	# nouveau.config=NvBios=PRAMIN video=HDMI-A-4:1920x1080@60
	# modprobe.blacklist=nouveau
	initrd (loop)/images/pxeboot/initrd.img
}

Fedora 39

添加 Live CD 启动项,主要用于系统挂了之后,我们还可以通过它来rescue

下载live cd

从清华源下载比较快

curl -LZO https://mirrors.tuna.tsinghua.edu.cn/fedora/releases/32/Workstation/x86_64/iso/Fedora-Workstation-Live-x86_64-32-1.6.iso
curl -LO https://mirrors.tuna.tsinghua.edu.cn/fedora/releases/32/Workstation/x86_64/iso/Fedora-Workstation-32-1.6-x86_64-CHECKSUM

下载成功后, 参考官方教程校验iso

# 导入 Fedora's GPG key
curl https://getfedora.org/static/fedora.gpg | gpg --import
# 校验CHECKSUM文件是否合法
gpg --verify-files *-CHECKSUM
# 最后校验下载的iso的checksum是否一致
sha256sum -c *-CHECKSUM

然后将文件copy到/boot下。为方便管理,我们在/boot下新建一iso目录, 然后将下载的live cd iso放于此:

sudo mkdir /boot/iso
sudo rsync -avP ./Fedora-Workstation-Live-x86_64-32-1.6.iso /boot/iso/

注意,Fedora 这个 live iso 文件大小大概在 1.8GB, 确保你的 /boot 分区有足够大的空间。

老灯在创建 /boot 分区时就已经考虑到了这一点,给 /boot 分配了至少 4GB 的空间。

增加grub2启动项

mount the iso

/run/media/ttys3/Fedora-WS-Live-39_B-1-1/EFI/BOOT/grub.cfg

set default="1"

function load_video {
  insmod efi_gop
  insmod efi_uga
  insmod video_bochs
  insmod video_cirrus
  insmod all_video
}

load_video
set gfxpayload=keep
insmod gzio
insmod part_gpt
insmod ext2

set timeout=60
### END /etc/grub.d/00_header ###

search --no-floppy --set=root -l 'Fedora-WS-Live-39_B-1-1'

### BEGIN /etc/grub.d/10_linux ###
menuentry 'Start Fedora-Workstation-Live 39_Beta' --class fedora --class gnu-linux --class gnu --class os {
	linuxefi /images/pxeboot/vmlinuz root=live:CDLABEL=Fedora-WS-Live-39_B-1-1  rd.live.image quiet rhgb
	initrdefi /images/pxeboot/initrd.img
}
menuentry 'Test this media & start Fedora-Workstation-Live 39_Beta' --class fedora --class gnu-linux --class gnu --class os {
	linuxefi /images/pxeboot/vmlinuz root=live:CDLABEL=Fedora-WS-Live-39_B-1-1  rd.live.image rd.live.check quiet
	initrdefi /images/pxeboot/initrd.img
}
submenu 'Troubleshooting -->' {
	menuentry 'Start Fedora-Workstation-Live 39_Beta in basic graphics mode' --class fedora --class gnu-linux --class gnu --class os {
		linuxefi /images/pxeboot/vmlinuz root=live:CDLABEL=Fedora-WS-Live-39_B-1-1  rd.live.image nomodeset quiet rhgb
		initrdefi /images/pxeboot/initrd.img
	}
}

grub2-mkconfig 在生成配置文件的时候,默认会读取 /etc/grub.d/40_custom ,这里就是我们添加iso启动项的地方了。

这个文件默认内容如下:

#!/usr/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.

千万注意不要删除exec tail -n +3 $0这一行及以上的内容。

tail -n +3 $0 的意思是取本文件第3行(包括)及之后的内容, 第3行其实就是exec这一行之后的那一行了。

老灯的/etc/grub.d/40_custom文件内容如下:

#!/usr/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 "Fedora Workstation 32 Live" {
	# remove tpm mod to avoid out of memory err
	rmmod tpm
	set isofile="/iso/Fedora-Workstation-Live-x86_64-32-1.6.iso"

	echo "Setting up loopback"
	loopback loop "${isofile}"

	isolabel="Fedora-WS-Live-39_B-1-1"
	# fedora grub2 no probe command
	#probe -l loop --set=isolabel
	echo "ISO Label is ${isolabel}"

	echo "Booting...."
	linuxefi (loop)/images/pxeboot/vmlinuz iso-scan/filename="${isofile}" root=live:CDLABEL=$isolabel rd.live.image  noquiet rd.luks=0 rd.md=0 rd.dm=0
	initrdefi (loop)/images/pxeboot/initrd.img

	# disable KMS if getting a blank screen or a "no signal" error from the display, see https://wiki.archlinux.org/index.php/Kernel_mode_setting
	#nomodeset i915.modeset=0 nouveau.modeset=0
	#nouveau.config=NvBios=PRAMIN video=HDMI-A-4:1920x1080@60
	#modprobe.blacklist=nouveau
}

注意,这里一定要在加载loop设备之前,把tpm模块移除: rmmod tpm, 不然会遇到out of memory错误,无法加载iso

由于Fedora下的grub2没有probe命令,我们只能手动指定isolabel了。

iso 文件的 label 可以用 iso-info (由libcdio包提供)来读取:

❯ iso-info ~/Downloads/Fedora-Workstation-Live-x86_64-32-1.6.iso
iso-info version 2.0.0 x86_64-redhat-linux-gnu
Copyright (c) 2003-2005, 2007-2008, 2011-2015, 2017 R. Bernstein
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
__________________________________
ISO 9660 image: /home/ttys3/Downloads/Fedora-Workstation-Live-x86_64-32-1.6.iso
Preparer    : XORRISO-1.5.2 2019.10.26.180001, LIBISOBURN-1.5.2, LIBISOFS-1.5.2, LIBBURN-1.5.2
Volume      : Fedora-WS-Live-32-1-6
Joliet Level: 3

然后我们就得到了label: Fedora-WS-Live-32-1-6

一般情况下,内核命令行,我们使用下面这个就可以(上面默认的):

linux (loop)/isolinux/vmlinuz iso-scan/filename="${isofile}" root=live:CDLABEL=$isolabel rootfstype=auto ro rd.live.image noquiet rd.luks=0 rd.md=0 rd.dm=0

但是,实际上老灯正在使用的cmdline是:

linux (loop)/isolinux/vmlinuz iso-scan/filename="${isofile}" root=live:CDLABEL=$isolabel rootfstype=auto ro rd.live.image noquiet rd.luks=0 rd.md=0 rd.dm=0 nomodeset i915.modeset=0 nouveau.modeset=0

老灯为什么多加了三个参数 nomodeset i915.modeset=0 nouveau.modeset=0 ?主要是因为老灯的GeForce GTX 1060显卡 在Fedora加载了默认的nouveau开源驱动后,显示器会直接黑屏然后显示没有信号。

❯ lspci -k | grep -i vga
00:02.0 VGA compatible controller: Intel Corporation UHD Graphics 630 (Desktop)
03:00.0 VGA compatible controller: NVIDIA Corporation GP106 [GeForce GTX 1060 3GB] (rev a1)

为了能使nouveau开源驱动能工作(能点亮显示器,能不崩就行了),老灯不得不关闭KMS (Kernel Mode Setting), 增加 nouveau.modeset=0

由于nouveau开源驱动实际上是要依赖KMS才能正常工作的, 关闭KMS的后果是,整个图像和字体都显示得非常大,并且显示分辨率不可调整了。虽然如此,但是至少我们能进图形界面了。

这应该只是nouveau在GTX 1060下的问题吧。因为在其它NVIDIA硬件上,老灯发现nouveau驱动其实是工作良好的。

这里主要是查看了ArchLinux 关于 KMS 的文档,问题才得到解决。

如果驱动能在KMS开启(默认开启的)的情况下正常工作,但是分辨率不太对,可以自行手动指定, 比如 video=HDMI-A-4:1920x1080@60。这里的HDMI-A-4是指的接口。 要查看当前哪些接口是处于连接状态的, 可以使用如下脚本:

for p in /sys/class/drm/*/status; do con=${p%/status}; echo -n "${con#*/card?-}: "; cat $p; done

老灯这里只接了一个显示器, 运行结果如下:

for p in /sys/class/drm/*/status; do con=${p%/status}; echo -n "${con#*/card?-}: "; cat $p; done
DP-1: disconnected
HDMI-A-1: disconnected
HDMI-A-2: disconnected
HDMI-A-3: disconnected
DP-2: disconnected
DVI-D-1: disconnected
HDMI-A-4: connected

如果nouveau驱动怎么折腾都不工作,可以禁用它 modprobe.blacklist=nouveau, 这样至少可以通过按 Ctrl + alt + Fx 进入纯控制台。

另外,Intel的i915驱动兼容性一般很好,很少会有黑屏问题。

re-generate grub2.cfg

sudo grub2-mkconfig -o /etc/grub2-efi.cfg

参考文档

https://wiki.archlinux.org/index.php/Nouveau

https://wiki.archlinux.org/index.php/Kernel_mode_setting#Disabling_modesetting

https://wiki.archlinux.org/index.php/Kernel_mode_setting#Forcing_modes

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

https://forums.freeside.co.uk/t/how-to-boot-a-fedora-30-iso-from-grub/197

https://fedoramagazine.org/start-a-fedora-29-installation-from-the-grub-menu/