我的环境
Kubuntu 20.04、带有 WIN10 guest 虚拟机管理器 2.2.1、virsh 版本 6.0.0、远程查看器版本 7.0
什么在起作用
目前我有 3 个显示器,并定义了 3 个 type=QXL 的视频节点。
我启动并附加到我的虚拟机
virsh start win10
remote-viewer -f spice://localhost:5900
一切都很好。
我的问题
从 cli (virsh start win10) 中,我如何/可以启动虚拟机并在配置文件中定义视频节点的数量?
IE
virsh start --add-video=type=qxl win10
显然 --add-video 不存在
我不问什么
如何使用virsh编辑来自 cli 的配置文件。
答案1
因此,根据我对一个我知之甚少的主题的研究:单独使用 virsh 直接编辑配置文件是不可能的。
快速深入了解 python 之后,我能够将这个脚本放在一起来完成这项工作
#!/usr/bin/python3
import xml.etree.ElementTree as ET
import libvirt
import sys
import os
desired_number_of_monitors = 3
domain_name = 'win10'
remote_viewer_config_path = os.path.join( os.path.dirname(os.path.realpath(__file__)), 'remote-viewer-win10-connection.ini')
if not os.path.exists(remote_viewer_config_path):
print(f'could not find {remote_viewer_config_path}: try again')
sys.exit(1)
if len(sys.argv) >= 2:
desired_number_of_monitors = int(sys.argv[1])
try:
libvert_con = libvirt.open(None)
except libvirt.libvirt.libvirtError:
print('Failed to open connection to the hypervisor')
sys.exit(1)
try:
win10_domain = libvert_con.lookupByName(domain_name)
except libvirt.libvirtError:
print(f'Failed to find domain {domain_name}')
sys.exit(1)
try:
raw_xml = win10_domain.XMLDesc(0)
except libvirt.libvirtError:
print(f'Failed to get xml from domain {domain_name}')
sys.exit(1)
try:
tree = ET.ElementTree(ET.fromstring(raw_xml))
devices = tree.find('.//devices')
#remove all video nodes
for v in devices.findall('video'):
devices.remove(v)
#add the desired number of video nodes
while desired_number_of_monitors > len(devices.findall('video')):
video = ET.Element('video')
video.append(ET.fromstring("<model type='qxl'></model>"))
video.append(ET.fromstring("<address type='pci'></address>"))
devices.append(video)
#the updated xml configuration
output_xml = ET.tostring(tree.getroot(),encoding='unicode')
except Exception as e:
print(f'failed to edit the xml for {domain_name} : {e}')
try:
win10_domain = libvert_con.defineXML(output_xml)
#if the domain is running stop exit
if win10_domain.isActive():
print(f'{win10_domain.name()} is running: try again')
sys.exit(1)
else:
#start the vm? am I actually re-creating the vm or just starting it and does it matter
if win10_domain.create() < 0:
print(f'Failed to start the {win10_domain.name()} domain')
else:
print(f'{win10_domain.name()} vm started')
os.system(f'remote-viewer -f {remote_viewer_config_path}')
except Exception as e2:
print(f'failed to start update or start {domain_name} : {e2}')