一、问题
之前写过 KVM虚拟机动态增加网卡和硬盘的内容,这还是命令行玩KVM时候写的东西(openstack和cloudstack平分秋色的时代),这一晃眼七八年过去了。又到了手撕KVM的时候了,最近遇到的一个虚拟化嵌套环境下要新增虚拟机下面虚拟机的网卡。结果发现之前的方法不成功。报错:
[root@foundation20 qemu]# virsh attach-interface serverb --type bridge --source privbr0 error: Failed to attach interface error: internal error: No more available PCI slots
最初真的以为是PCI配置不够用了,因为这些虚拟机,每个上面都挂载了N块硬盘。先是按之前的方法移掉了几块不用的硬盘,再增加发现还是这个报错。然后通过修改virsh edit servera这样的方式修改xml配置文件的方式,可以增加,不过也有增加不成功的时候。这就有点蛋疼了,不能保证100%的成功率,而且还需要进行批量操作。只能通过–help查看帮助信息了。通过查看帮助信息,发现其增加了–config、–current、–persistent几个参数。
二、解决方法
先使用–config方法增加并重启虚拟机后,发现网卡增加成功了。不过查看网卡类型是rtl8139 。
[root@foundation20 qemu]# virsh attach-interface servera --type bridge --source privbr0 --config Interface attached successfully
把参数改用其他的参数再测试,直接指定网卡的类型是virtio模式,发现直接成功了。
[root@foundation20 qemu]# virsh attach-interface serverb --type bridge --source privbr0 --current --model virtio Interface attached successfully
而发现之前的删除方法还是有效的:
[root@foundation20 qemu]# virsh attach-interface servera --type bridge --source privbr0 --model virtio --target eth5 --current [root@foundation20 qemu]# virsh detach-interface servera --type bridge --mac 52:54:00:28:71:fe --persistent [root@foundation20 qemu]# virsh domiflist servera Interface Type Source Model MAC ------------------------------------------------------- vnet4 bridge privbr0 virtio 52:54:00:00:fa:0a vnet6 bridge privbr0 virtio 52:54:00:cc:0a:85