KVM 虚拟机添加 CDROM 设备并挂载 ISO 引导文件

WHAT

根据用户提供的虚拟机导出的 base 镜像:

qemu-img convert -f qcow2 -O qcow2 -c vm.qcow2 imported-vm.qcow2

使用 virt-install ... --import 导入方式安装后,虚拟机无法开机,提示下面报错:

[    2.772200] XFS (sda4): Internal error XFS_WANT_CORRUPTED_GOTO at line 1627 of file fs/xfs/libxfs/xfs_alloc.c. Caller xfs_free_extent+0xf9/0x130 [xfs]
[    2.774653] XFS (sda4): Failed to recover EFIs
[  OK  ] Started Show Plymouth Boot Screen.
[  OK  ] Reached target Paths.
[  OK  ] Reached target Basic System.
         Mounting Configuration File System...
[  OK  ] Mounted Configuration File System.
[  OK  ] Found device QEMU_HARDDISK 4.
         Starting File System Check on /dev/...7-5271-4030-8808-366e1bc46c0e...
[  OK  ] Started dracut initqueue hook.
[  OK  ] Reached target Remote File Systems (Pre).
[  OK  ] Reached target Remote File Systems.
         Starting dracut pre-mount hook...
[  OK  ] Started File System Check on /dev/d...627-5271-4030-8808-366e1bc46c0e.
[  OK  ] Started dracut pre-mount hook.
         Mounting /sysroot...
[   ***] A start job is running for /sysroot (3min 59s / 4min 31s)[  241.955354] INFO: task mount:533 blocked for more than 120 seconds.
[FAILED] Failed to mount /sysroot.
See 'systemctl status sysroot.mount' for details.
[DEPEND] Dependency failed for Initrd Root File System.
[DEPEND] Depen[  OK  ] Reached target Initrd File Systems.

Generating "/run/initramfs/rdsosreport.txt"

Entering emergency mode. Exit the shell to continue.
Type "journalctl" to view system logs.
You might want to save "/run/initramfs/rdsosreport.txt" to a USB stick or /boot
after mounting them and attach it to a bug report.


:/#

VM 开机引导在 Mounting /sysroot... 卡了很久,而且 最上面 也有 XFS 文件系统报错。

应该是 根分区自检 失败了,只能通过挂载 ISO 在系统之外的 LiveOS 里面 xfs_repair 了。

HOW

杯具的是发现 VM 没有 CDROM 设备 - -.

# virsh domblklist new-vm-imported
 Target   Source
-------------------------------------------------------------
 sda      /home/kvm/new-vm-imported.qcow2

尝试使用 attach-device 提示 CDROM 不支持热插拔

<disk type='file' device='cdrom'>
  <driver name='qemu' type='raw'/>
  <source index='4'/>
  <target dev='sdb' bus='sata'/>
  <readonly/>
  <alias name='sata0-0-1'/>
  <address type='drive' controller='0' bus='0' target='0' unit='1'/>
</disk>
# virsh attach-device new-vm-imported sata-cdrom.xml
error: Failed to attach device from sata-cdrom.xml
error: Operation not supported: cdrom/floppy device hotplug isn't supported

只能使用 virsh edit VM_DOMAIN_NAME 编辑 VM 配置文件了

冷重启 virsh destroy VM_DOMAIN_NAME 后,挂载 ISO 提示 读写权限 有问题:

# virsh attach-disk new-vm-imported /root/CentOS-8.5.2111-x86_64-boot.iso sdb --type cdrom --mode readonly
error: Failed to attach disk
error: Cannot access storage file '/root/CentOS-8.5.2111-x86_64-boot.iso': No such file or directory

将 ISO 移动到 qemu 用户 可读 的目录,即可挂载 ISO 文件:

# namei -l /home/kvm/
f: /home/kvm/
dr-xr-xr-x root root /
drwxr-xr-x root root home
drwxr-xr-x root root kvm

# mv /root/CentOS-8.5.2111-x86_64-boot.iso /home/kvm/

# virsh attach-disk new-vm-imported /home/kvm/CentOS-8.5.2111-x86_64-boot.iso sdb --type cdrom --mode readonly
Disk attached successfully

# virsh domblklist new-vm-imported
 Target   Source
-------------------------------------------------------------
 sda      /home/kvm/new-vm-imported.qcow2
 sdb      /home/kvm/CentOS-8.5.2111-x86_64-boot.iso

也可以 使用 virt-xml 命令添加 CDROM 设备

# virt-xml new-vm-imported --add-device --disk /home/kvm/CentOS-8.5.2111-x86_64-boot.iso,device=cdrom
Domain 'new-vm-imported' defined successfully.
Changes will take effect after the domain is fully powered off.

# virsh domblklist new-vm-imported
 Target   Source
-------------------------------------------------------------
 sda      /home/kvm/new-vm-imported

强制关机 后可以看到新添加的 CDROM 设备:

# virsh destroy new-vm-imported
Domain new-vm-imported destroyed

# virsh domblklist new-vm-imported
 Target   Source
-------------------------------------------------------------
 sda      /home/kvm/new-vm-imported
 hda      /home/kvm/CentOS-8.5.2111-x86_64-boot.iso

然后通过 virsh reset VM_DOMAIN_NAME 命令重启,即可选择 ISO 引导:

Machine UUID new-vm-imported

iPXE (http://ipxe.org) 00:03.0 CB00 PCI2.10 PnP PMM+BFF8F710+BFECF710 CB00



Press ESC for boot menu.

Select boot device:

1. virtio-scsi Drive QEMU QEMU HARDDISK 2.5+
2. Legacy option rom
3. DVD/CD [AHCI/1: QEMU DVD-ROM ATAPI-4 DVD/CD] (CentOS-8-5-2111-x86_64-dvd)
4. iPXE (PCI 00:03.0)

VNC 连接图形界面,使用 Tab 按键修改引导参数,添加 console=ttyS0 inst.text 引导参数:

img

引导进入 LiveOS 后切换至 shell 页面,使用 xfs_repair -L /dev/sda4 修复一下文件系统:

img

img

重启之后,顺利进入系统:

# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0    2T  0 disk
├─sda1   8:1    0    1M  0 part
├─sda2   8:2    0    1G  0 part /boot
├─sda3   8:3    0    8G  0 part [SWAP]
└─sda4   8:4    0    2T  0 part /

最后卸载 ISO 即可:

# virsh change-media new-vm-imported sdb --eject
Successfully ejected media.

# virsh domblklist new-vm-imported
 Target   Source
-------------------------------------------------------------
 sda      /home/kvm/new-vm-imported.qcow2
 sdb      -

reference

RHEL8 / Configuring and managing virtualization / Chapter 10. Managing virtual devices

RHEL7 / Virtualization Deployment and Administration Guide / 20.27. Host Machine Management / 20.27.6.3. Changing the media of a CDROM