0

My Environment

Kubuntu 20.04, Virtual Machine Manager 2.2.1 with WIN10 guest, virsh version 6.0.0, remote-viewer version 7.0

What's Working

Currently I've have 3 monitors and have defined 3 Video nodes with type=QXL.

I start and attach to my VM with

 virsh start win10
 remote-viewer -f spice://localhost:5900 

and everything works just nicely.

My Question

From the cli (virsh start win10), how/can I start the VM and define the number of video nodes in the configuration file?

i.e

virsh start --add-video=type=qxl win10

obviously --add-video does not exist

What I'm Not Asking

How to use virsh to edit the config file from the cli.

1 Answer 1

0

So from my research about a topic I know little about: it is not possible to edit the config file directly using virsh alone.

After a quick dive into python I was able to put together this script which does the job

#!/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}')

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .