Add VNC support for virtual mode VMs

This patch makes possible to interact with guest OS from a remote machine using
VNC.
more_events
Roberto Hueso Gómez 2020-05-13 14:13:47 +02:00
parent 38b6d77561
commit ff988b80b5
1 changed files with 33 additions and 7 deletions

View File

@ -20,10 +20,12 @@ import re
import math import math
import sys import sys
import enum import enum
import time
class OgVM: class OgVM:
DEFAULT_CPU = 'host' DEFAULT_CPU = 'host'
DEFAULT_VGA = 'std' DEFAULT_VGA = 'VGA'
DEFAULT_QMP_IP = 'localhost'
DEFAULT_QMP_PORT = 4444 DEFAULT_QMP_PORT = 4444
class State(enum.Enum): class State(enum.Enum):
@ -35,12 +37,16 @@ class OgVM:
memory=None, memory=None,
cpu=DEFAULT_CPU, cpu=DEFAULT_CPU,
vga=DEFAULT_VGA, vga=DEFAULT_VGA,
qmp_port=DEFAULT_QMP_PORT): qmp_ip=DEFAULT_QMP_IP,
qmp_port=DEFAULT_QMP_PORT,
vnc_params=None):
self.partition_path = partition_path self.partition_path = partition_path
self.cpu = cpu self.cpu = cpu
self.vga = vga self.vga = vga
self.qmp_ip = qmp_ip
self.qmp_port = qmp_port self.qmp_port = qmp_port
self.proc = None self.proc = None
self.vnc_params = vnc_params
if memory: if memory:
self.mem = memory self.mem = memory
@ -52,12 +58,29 @@ class OgVM:
def run_vm(self): def run_vm(self):
cmd = (f'qemu-system-x86_64 -hda {self.partition_path} ' if self.vnc_params:
f'-qmp tcp:localhost:4444,server,nowait --enable-kvm ' vnc_str = f'-vnc 0.0.0.0:0,password'
f'-vga {self.vga} -display gtk -cpu {self.cpu} -m {self.mem}M ' else:
f'-boot c -full-screen') vnc_str = ''
cmd = (f'qemu-system-x86_64 -accel kvm -cpu {self.cpu} -smp 4 '
f'-drive file={self.partition_path},if=virtio '
f'-qmp tcp:localhost:4444,server,nowait '
f'-device {self.vga},vgamem_mb=128 -display gtk '
f'-m {self.mem}M -boot c -full-screen {vnc_str}')
self.proc = subprocess.Popen([cmd], shell=True) self.proc = subprocess.Popen([cmd], shell=True)
if self.vnc_params:
# Wait for QMP to be available.
time.sleep(10)
qmp = OgQMP(self.qmp_ip, self.qmp_port)
cmd = { "execute": "change",
"arguments": { "device": "vnc",
"target": "password",
"arg": str(self.vnc_params['pass']) } }
qmp.talk(str(cmd))
qmp.disconnect()
class OgQMP: class OgQMP:
QMP_TIMEOUT = 5 QMP_TIMEOUT = 5
QMP_POWEROFF_TIMEOUT = 300 QMP_POWEROFF_TIMEOUT = 300
@ -203,6 +226,9 @@ class OgVirtualOperations:
partition = request.getPartition() partition = request.getPartition()
part_path = f'{self.OG_PARTITIONS_PATH}/disk{disk}_part{partition}.qcow2' part_path = f'{self.OG_PARTITIONS_PATH}/disk{disk}_part{partition}.qcow2'
if ogRest.CONFIG['vnc']['activate']:
qemu = OgVM(part_path, vnc_params=ogRest.CONFIG['vnc'])
else:
qemu = OgVM(part_path) qemu = OgVM(part_path)
qemu.run_vm() qemu.run_vm()