Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

How to change the QEMU invocation command flags for a guest

Custom device flags for QEMU go to guest_device_args list in the source code.

Save Using the device_model_override option inside a guest .cfg file we can change the QEMU binary that runs for a guest. The following Python3 script can be used as a device_model_override binary and launch the QEMU build of your choice with the command flags you want.

  • Custom command flags for QEMU go to the hard-coded global variable guest_device_args list in the script’s source code.

  • QEMU Binary path is hard-coded in the QEMU_BINARY_PATH global variable.

  • Environment flags can be set by adding os.environ["ENV_VAR"] = "VALUE" lines.

  • stdout/stderr are redirected to the path in the hard-coded LOG_FILE global variable.

Save the following source code as qemu_device_model.py.Set , set executable permissions with chmod +x qemu_device_model.py.Override and override the xen device model parameter in the guest .cfg file:

...

This will call the script with the same arguments it’d otherwise pass to the QEMU binary.

The source code:

#!/usr/bin/env python3
Expand
titleqemu_device_model.py script
Code Block
languagepy
py script
Code Block
languagepy
#!/usr/bin/env python3

# SPDX-License-Identifier: GPL-3.0-or-later
# <manos.pitsidianakis@linaro.org>

import sys, signal, os
from subprocess import Popen

""" Usage

Save as `qemu_device_model.py`, set as executable
(`chmod +x qemu_device_model.py`) and add in xen guest .cfg:

device_model_version="qemu-xen"
device_model_override="/home/root/qemu_device_model.py"

"""


def handler_exit_signals(signum, frame):
    raise SystemExit(0)


signal.signal(signal.SIGINT, handler_exit_signals)
signal.signal(signal.SIGTERM, handler_exit_signals)

#os.environ["LD_LIBRARY_PATH"] = "/usr/local/lib/aarch64-linux-gnu"
QEMU_BINARY_PATH = "/usr/bin/qemu-system-aarch64"
LOG_FILE = "/home/root/qemu_log.txt"

args = sys.argv[1:]
dom_id = None
name = None

# print("args are:",args)

i = 0
try:
    while i < len(args):
        if args[i].startswith("-xen-domid"):
            dom_id = args[i + 1]
            i += 2
            continue

        if args[i].startswith("-name"):
            name = args[i + 1]
            i += 2
            continue

        i += 1
except IndexError as exc:
    print(exc, "for i =", i)
    print("Invalid command line arguments: ", args)
    sys.exit(1)

if name is None:
    name = "trs-vm"

if dom_id is None:
    print("No -xen-domid found in arguments:\nArguments were:", args)
    sys.exit(1)

libxl_cmd_socket = "/var/run/xen/qmp-libxl-{}".format(dom_id)
libxenstat_cmd_socket = "/var/run/xen/qmp-libxenstat-{}".format(dom_id)

# Create new argv with flags that include the domid and guest name first, then
# add the device flags

xen_guest_specific_args = [
    "-name",
    name,
    "-xen-domid",
    dom_id,
    "-chardev",
    "socket,id=libxl-cmd,path={},server=on,wait=off".format(libxl_cmd_socket),
    "-chardev",
    "socket,id=libxenstat-cmd,path={},server=on,wait=off".format(libxenstat_cmd_socket),
    "-mon",
    "chardev=libxl-cmd,mode=control",
    "-mon",
    "chardev=libxenstat-cmd,mode=control",
    "-nodefaults",
    "-no-user-config",
    "-xen-attach",
    "-no-shutdown",
    "-smp",
    "4,maxcpus=4",
    "-machine",
    "xenpvh",
    "-m",
    "6144",
]

guest_device_args = [
    "-vga",
    "std",
    "-vnc",
    "none",
    "-display",
    "sdl,gl=on",
    "-device",
    "virtio-gpu-pci,disable-legacy=on,iommu_platform=on,xres=1080,yres=600",
    "-global",
    "virtio-mmio.force-legacy=false",
    "-device",
    "virtio-mouse-pci,disable-legacy=on,iommu_platform=on",
    "-device",
    "virtio-keyboard-pci,disable-legacy=on,iommu_platform=on",
    "-device",
    "virtio-snd-pci,audiodev=snd0,disable-legacy=on,iommu_platform=on",
    "-audiodev",
    "alsa,id=snd0,out.dev=default",
]

new_args = [QEMU_BINARY_PATH] + xen_guest_specific_args + guest_device_args

# print("new args are:",new_args)

with open(LOG_FILE, "a") as log, Popen(new_args, stdout=log, stderr=log) as proc:
    try:
        proc.wait()
    except SystemExit:
        proc.kill()
    sys.exit(proc.returncode)

...