navy/meta/plugins/qemu.py

67 lines
1.7 KiB
Python

import os
from cutekit import shell
from .image import Image
class Qemu:
def __init__(self, img: Image, no_reboot: bool = True, no_display: bool = False,
no_shutdown: bool = True, efi: bool = True, memory: str = "4G", debug: bool = False, soft_dbg: bool = False):
self.__img = img
self.__binary = None
self.__efi = efi
self.__args = [
"-m", str(memory),
]
if debug:
self.__args += [
"-s", "-S"
]
if no_reboot:
self.__args.append("-no-reboot")
if no_shutdown:
self.__args.append("-no-shutdown")
if no_display:
self.__args += ["-display", "none"]
if self.iskvmAvailable():
self.__args += ["-enable-kvm", "-cpu", "host"]
if soft_dbg:
self.__args += ["-d", "int"]
try:
getattr(self, f"arch_{img.builder.arch}")()
except AttributeError:
raise NotImplementedError()
def iskvmAvailable(self) -> bool:
return os.path.exists("/dev/kvm") and os.access("/dev/kvm", os.R_OK)
def arch_x86_64(self):
self.__binary = "qemu-system-x86_64"
self.__args += [
"-smp", "4",
"-debugcon", "mon:stdio"
]
if self.__efi:
ovmf = shell.wget(
"https://retrage.github.io/edk2-nightly/bin/RELEASEX64_OVMF.fd"
)
self.__args += [
"-drive", f"format=raw,file=fat:rw:{self.__img},media=disk",
"-bios", ovmf
]
def run(self):
if self.__binary is None:
raise RuntimeError("Architecture is not set")
shell.exec(self.__binary, *self.__args)