admin 发表于 2023-9-10 15:00:04

virsh 在线添加设备cdrom或者iso镜像

virsh 在线添加设备cdrom或者iso镜像
virsh domblklist Windows2019_x86
Target   Source
----------------------------------------------------------------
vda      /usr/src/Windows2019-x86.qcow2
sda      /usr/src/cn_windows_server_2019_x64_dvd_2d80e042.iso



virsh attach-diskWindows2019_x86 --target sdb   --type cdrom/usr/src/virtio-win-0.1.225.iso--mode readonly
error: Failed to attach disk
error: Operation not supported: cdrom/floppy device hotplug isn't supported



root@compute01:/usr/src# virsh attach-diskWindows2019_x86 /usr/src/virtio-win-0.1.225.iso sdb --cache none --subdriver cdrom --config --persistent
error: Failed to attach disk
error: unsupported configuration: unknown driver format value 'cdrom'

root@compute01:/usr/src# virsh attach-diskWindows2019_x86 /usr/src/virtio-win-0.1.225.iso sdb --cache none --subdriver qcow2 --config --persistent
error: Failed to attach disk
error: internal error: No more available PCI slots

root@compute01:/usr/src# virsh attach-diskWindows2019_x86 /usr/src/virtio-win-0.1.225.iso vdc --cache none --subdriver qcow2 --config --persistent
error: Failed to attach disk
error: internal error: unable to execute QEMU command 'blockdev-add': Image is not in qcow2 format

root@compute01:/usr/src# virsh attach-diskWindows2019_x86 /usr/src/virtio-win-0.1.225.iso vdc --cache none --subdriver raw --config --persistent
Disk attached successfully



root@compute01:/usr/src# virt-install --name=Windows2019_x86--ram 8192 --vcpus=4 --disk path=/usr/src/Windows2019-x86.qcow2,format=qcow2,size=15,bus=virtio --accelerate --cdrom /usr/src/cn_windows_server_2019_x64_dvd_2d80e042.iso --vnc --vncport=5906 --vnclisten=0.0.0.0 --network bridge=br0,model=virtio --noautoconsole --disk path=/usr/src/virtio-win-0.1.225.iso,device=cdrom

Starting install...
ERROR    Cannot get interface MTU on 'br0': No such device
Domain installation does not appear to have been successful.
If it was, you can restart your domain by running:
virsh --connect qemu:///system start Windows2019_x86
otherwise, please restart your installation.
root@compute01:/usr/src#
root@compute01:/usr/src#
root@compute01:/usr/src# netplan apply

** (generate:8799): WARNING **: 13:35:59.651: `gateway4` has been deprecated, use default routes instead.
See the 'Default routes' section of the documentation for more details.

** (process:8797): WARNING **: 13:36:01.040: `gateway4` has been deprecated, use default routes instead.
See the 'Default routes' section of the documentation for more details.

** (process:8797): WARNING **: 13:36:01.052: `gateway4` has been deprecated, use default routes instead.
See the 'Default routes' section of the documentation for more details.

** (process:8797): WARNING **: 13:36:01.589: `gateway4` has been deprecated, use default routes instead.
See the 'Default routes' section of the documentation for more details.

** (process:8797): WARNING **: 13:36:01.589: `gateway4` has been deprecated, use default routes instead.
See the 'Default routes' section of the documentation for more details.
root@compute01:/usr/src#
root@compute01:/usr/src#
root@compute01:/usr/src# virt-install --name=Windows2019_x86--ram 8192 --vcpus=4 --disk path=/usr/src/Windows2019-x86.qcow2,format=qcow2,size=15,bus=virtio --accelerate --cdrom /usr/src/cn_windows_server_2019_x64_dvd_2d80e042.iso --vnc --vncport=5906 --vnclisten=0.0.0.0 --network bridge=br0,model=virtio --noautoconsole --disk path=/usr/src/virtio-win-0.1.225.iso,device=cdrom

Starting install...
Creating domain...                                          |    0 B00:00:00   

Domain is still running. Installation may be in progress.
You can reconnect to the console to complete the installation process.
root@compute01:/usr/src#

在线添加光盘
virsh attach-disk Windows2019_x86 /usr/src/virtio-win-0.1.225.iso/ vdd

其他方法:


再次使用 virsh dumpxml sles12-sp3 可以看到iso文件加载:

<disk type='file' device='cdrom'>
<driver name='qemu' type='raw'/>
<source file='/var/lib/libvirt/images/SLE-12-SP3-Server-DVD-x86_64-GM-DVD1.iso' index='3'/>
<backingStore/>
<target dev='sda' bus='sata'/>
<readonly/>
<alias name='sata0-0-0'/>
<address type='drive' controller='0' bus='0' target='0' unit='0'/>
</disk>
如果要卸载这个iso文件,则创建一个相同结构的xml文件 detach_iso.xml ,但是保持 <source/> 行删除:

<disk type='file' device='cdrom'>
<driver name='qemu' type='raw'/>
<backingStore/>
<target dev='sda' bus='sata'/>
<readonly/>
<alias name='sata0-0-0'/>
<address type='drive' controller='0' bus='0' target='0' unit='0'/>
</disk>
然后执行设备更新:

virsh update-device sles12-sp3 detach_iso.xml
此时提示:

Device updated successfully
再检查虚拟机配置,就看到iso文件已经卸载了。

admin 发表于 2023-9-10 15:00:05

virt-install --name=Windows2019_x86--ram 8192 --vcpus=4 --disk path=/usr/src/Windows2019-x86.qcow2,format=qcow2,size=15,bus=virtio --accelerate --cdrom /usr/src/cn_windows_server_2019_x64_dvd_2d80e042.iso --vnc --vncport=5906 --vnclisten=0.0.0.0 --network bridge=br0,model=virtio --noautoconsole --disk path=/usr/src/virtio-win-0.1.225.iso,device=cdrom创建kvm时,添加对应的多个设备即可。

virsh domblklist Windows2019_x86
Target   Source
----------------------------------------------------------------
vda      /usr/src/Windows2019-x86.qcow2
sda      /usr/src/cn_windows_server_2019_x64_dvd_2d80e042.iso
sdb      /usr/src/virtio-win-0.1.225.iso

admin 发表于 2023-9-10 15:00:06

2.在线调整虚拟机内存(增大或减小)
#调整为4G
virsh setmem cos_v1 4G

#调整为2G
virsh setmem cos_v1 2G
#能够在线调整的最大内存不能超过为虚拟机分配的最大内存(上面xml文件中设置的最大为4G),否则需要关闭虚拟机上调最大内存


3.在线调整虚拟机CPU(只能增大,不能减小)
virsh setvcpus cos_v1 4
virsh setvcpus cos_v1 8
#同样,能够动态调整的最大VCPU个数也不能超过为虚拟机设置的最大VCPU数量


4.在线添加硬盘
#添加qcow2格式硬盘

#创建qcow2格式的新磁盘,大小为40G
qemu-img create -f qcow2 /data/vhosts/test/cos_v1-add1.disk 40G
virsh attach-disk cos_v1 /data/vhosts/test/cos_v1-add1.disk vdb --cache none --subdriver qcow2 --config --persistent
#虚拟机根磁盘为vda,因此这里使用vdb表示新添加磁盘
#--config 参数同时更新虚拟机xml文件,确保重启后依然生效
#添加raw格式硬盘
#创建raw格式的新磁盘,大小为40G
qemu-img create -f raw /data/vhosts/test/cos_v1-add2.disk 40G
virsh attach-disk lnmptest-107 /data/vhosts/test/cos_v1-add2.disk vdc --cache none --subdriver raw --config --persistent

5.在线移除硬盘
#可以查看虚拟机所有磁盘
virsh domblklist cos_v1
virsh detach-disk cos_v1 vdb

6.在线添加网卡
virsh attach-interface --domain cos_v1 --type network --source default --model virtio --config
#可以查看新添加的网卡
virsh domiflist cos_v1

7.在线添加光盘
virsh attach-disk centosbase /data_lij/iso/CentOS-6.4-x86_64-bin-DVD1.iso vdd
页: [1]
查看完整版本: virsh 在线添加设备cdrom或者iso镜像