配置 GRUB 以在 QEMU 中加载

配置 GRUB 以在 QEMU 中加载

我有一个 debootstrapped Debian 映像,它是使用以下命令创建的:

qemu-img create -f qcow2 disk.qcow2 16G

su

export PATH=$PATH:/sbin:/usr/sbin
export TERM=xterm

modprobe nbd
qemu-nbd --connect=/dev/nbd0 disk.qcow2 --cache=unsafe --discard=unmap
parted --align optimal --script --machine --fix /dev/nbd0 -- \
       mklabel gpt \
       mkpart efi fat32 '0%' 500MiB \
       set 1 boot on \
       mkpart swap linux-swap 500MiB 1GiB \
       mkpart root ext4 1GiB '100%'
mkfs.vfat -F 32 /dev/nbd0p1
mkswap /dev/nbd0p2
mkfs.ext4 /dev/nbd0p3

mkdir -p disk
mount /dev/nbd0p3 disk

debootstrap --arch amd64 bullseye ./disk
mkdir -p ./disk/boot/efi
mount /dev/nbd0p1 ./disk/boot/efi
mount --bind /dev ./disk/dev
mount -t devpts /dev/pts ./disk/dev/pts
mount -t proc proc ./disk/proc
mount -t sysfs sysfs ./disk/sys
mount -t tmpfs tmpfs ./disk/tmp

chroot ./disk

echo -e 'password\npassword' | passwd

echo debian > /etc/hostname

UUID1=$(blkid /dev/nbd0p1 | grep -Eo ' UUID="[^ ]*"' | sed 's/[" ]//g')
UUID2=$(blkid /dev/nbd0p2 | grep -Eo ' UUID="[^ ]*"' | sed 's/[" ]//g')
UUID3=$(blkid /dev/nbd0p3 | grep -Eo ' UUID="[^ ]*"' | sed 's/[" ]//g')
echo "none /tmp tmpfs defaults 0 0" > /etc/fstab
echo "$UUID1 /boot/efi vfat defaults 0 2" >> /etc/fstab
echo "$UUID2 none swap sw 0 0" >> /etc/fstab
echo "$UUID3 / ext4 errors=remount-ro 0 1" >> /etc/fstab

cat > /etc/apt/sources.list << __EOF__
deb http://deb.debian.org/debian/ bullseye main
deb-src http://deb.debian.org/debian/ bullseye main
deb http://deb.debian.org/debian/ bullseye-updates main
deb-src http://mirror.mephi.ru/debian/ bullseye-updates main
deb http://deb.debian.org/debian-security/ bullseye-security main
deb-src http://deb.debian.org/debian-security/ bullseye-security main
__EOF__

export DEBIAN_FRONTEND=noninteractive
export LANG="C.UTF-8"
apt-get update
apt-get upgrade

systemctl enable [email protected]
systemctl enable systemd-networkd.service

cat > /etc/systemd/network/80-dhcp.network << __EOF__
[Match]
Name=en*
[Network]
DHCP=yes
__EOF__

apt-get install -y linux-image-5.10.0-8-amd64 grub-efi
apt-get clean
apt-get autoclean
rm -rf /var/lib/apt/lists/*

grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=debian --recheck --no-nvram /dev/nbd0

echo 'GRUB_DISABLE_OS_PROBER=true' >> /etc/default/grub
update-grub2

exit

for dev in tmp sys proc dev/pts dev boot/efi; do
    umount ./disk/$dev
done

umount disk
rmdir disk
qemu-nbd --disconnect /dev/nbd0

exit

当我从驱动器复制内核和 initrd 映像并使用命令直接启动它们时,我可以成功运行此映像

qemu-system-x86_64 \
    -drive format=qcow2,file=disk.qcow2 \
    -kernel vmlinuz \
    -initrd initrd.img \
    -cpu host -M q35  \
    -enable-kvm \
    -m 2G,maxmem=4G \
    -device rtl8139,netdev=network0 \
    -netdev user,id=network0 \
    -append "root=/dev/sda3"

但是当我尝试使用 GRUB 启动该磁盘时海BIOS(默认 QEMU BIOS)使用命令

qemu-system-x86_64 \
    -drive format=qcow2,file=disk.qcow2 \
    -boot c  \
    -cpu host -M q35  \
    -enable-kvm \
    -m 2G,maxmem=4G \
    -device rtl8139,netdev=network0 \
    -netdev user,id=network0

它卡在线Booting from Hard Disk...路上。

使用时OVMF电喷用命令

qemu-system-x86_64 \
    -drive format=qcow2,file=disk.qcow2 \
    -boot c  \
    -bios /usr/share/ovmf/OVMF.fd \
    -cpu host -M q35  \
    -enable-kvm \
    -m 2G,maxmem=4G \
    -device rtl8139,netdev=network0 \
    -netdev user,id=network0

我收到错误行

BdsDxe: failed to load Boot0001 "UEFI QEMU DVD-ROM QM00005 " from PciRoot(0x0)/Pci(0x1F,0x2)/Sata(0x2,0xFFFF,0x0): Not Found
BdsDxe: failed to load Boot0002 "UEFI QEMU HARDDISK QM00001 " from PciRoot(0x0)/Pci(0x1F,0x2)/Sata(0x0,0xFFFF,0x0): Not Found

如何让我的磁盘加载 GRUB?

答案1

我无法让 UEFI 版本工作,但 BIOS 安装工作正常。

以下是获取可从 QEMU 启动的 debootstrapped Debian 的完整代码:

qemu-img create -f qcow2 disk.qcow2 16G

su

export PATH=$PATH:/sbin:/usr/sbin
export TERM=xterm

modprobe nbd
qemu-nbd --connect=/dev/nbd0 disk.qcow2 --cache=unsafe --discard=unmap
parted --align optimal --script --machine --fix /dev/nbd0 -- \
       mklabel gpt \
       mkpart fat32 '0%' 2MiB \
       set 1 bios_grub on \
       mkpart swap linux-swap 2MiB 1GiB \
       mkpart root ext4 1GiB '100%'
mkswap /dev/nbd0p2
mkfs.ext4 /dev/nbd0p3

mkdir -p disk
mount /dev/nbd0p3 disk

debootstrap --include=systemd-sysv --variant=minbase --arch amd64 bullseye ./disk

mount --bind /dev ./disk/dev
mount -t devpts /dev/pts ./disk/dev/pts
mount -t proc proc ./disk/proc
mount -t sysfs sysfs ./disk/sys
mount -t tmpfs tmpfs ./disk/tmp

chroot ./disk

echo -e 'password\npassword' | passwd
echo debian > /etc/hostname

UUID1=$(blkid /dev/nbd0p2 | grep -Eo ' UUID="[^ ]*"' | sed 's/[" ]//g')
UUID2=$(blkid /dev/nbd0p3 | grep -Eo ' UUID="[^ ]*"' | sed 's/[" ]//g')
echo "none /tmp tmpfs defaults 0 0" > /etc/fstab
echo "$UUID1 none swap sw 0 0" >> /etc/fstab
echo "$UUID2 / ext4 errors=remount-ro 0 1" >> /etc/fstab

cat > /etc/apt/sources.list << __EOF__
deb http://deb.debian.org/debian/ bullseye main
deb-src http://deb.debian.org/debian/ bullseye main
deb http://deb.debian.org/debian/ bullseye-updates main
deb-src http://mirror.mephi.ru/debian/ bullseye-updates main
deb http://deb.debian.org/debian-security/ bullseye-security main
deb-src http://deb.debian.org/debian-security/ bullseye-security main
__EOF__

export DEBIAN_FRONTEND=noninteractive
export LANG="C.UTF-8"
apt-get update
apt-get upgrade -y

systemctl enable [email protected]
systemctl enable systemd-networkd.service

cat > /etc/systemd/network/80-dhcp.network << __EOF__
[Match]
Name=en*
[Network]
DHCP=yes
__EOF__

apt-get install -y linux-image-5.10.0-8-amd64 grub2
apt-get clean
apt-get autoclean
rm -rf /var/lib/apt/lists/*

echo 'GRUB_DISABLE_OS_PROBER=true' >> /etc/default/grub
grub-install --target=i386-pc --recheck /dev/nbd0
update-grub2

exit

for dev in tmp sys proc dev/pts dev; do
    umount ./disk/$dev
done

umount disk

rmdir disk
qemu-nbd --disconnect /dev/nbd0

exit

qemu-system-x86_64 \
     -drive format=qcow2,file=disk.qcow2 \
     -boot c  \
     -cpu host -M q35  \
     -enable-kvm \
     -m 2G,maxmem=4G \
     -device rtl8139,netdev=network0 \
     -netdev user,id=network0

相关内容