QEMU/KVM: 実行時にビデオデバイスを追加する方法

QEMU/KVM: 実行時にビデオデバイスを追加する方法

私の環境

Kubuntu 20.04、WIN10ゲスト仮想マシンマネージャ2.2.1、virshバージョン6.0.0、リモートビューアバージョン7.0

どうしたの?

現在、タイプ= QXLで定義されている3つのモニターと3つのビデオノードがあります。

起動し、仮想マシンに接続しました。

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

みんな大丈夫です。

私の質問

cli(virsh start win10)で仮想マシンを起動し、設定ファイルのビデオノード数をどのように定義できますか?

つまり

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

明らかに--add-videoは存在しません

何も聞かない

バッシュの使い方編集する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}')

おすすめ記事