2023-12-10 18:24:05 +00:00
|
|
|
import os
|
|
|
|
|
|
|
|
from cutekit import shell
|
|
|
|
|
|
|
|
from .image import Image
|
|
|
|
|
|
|
|
class Qemu:
|
2023-12-17 15:32:53 +00:00
|
|
|
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):
|
2023-12-10 18:24:05 +00:00
|
|
|
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")
|
|
|
|
|
2023-12-17 15:32:53 +00:00
|
|
|
if no_display:
|
|
|
|
self.__args += ["-display", "none"]
|
2023-12-10 18:24:05 +00:00
|
|
|
|
|
|
|
if self.iskvmAvailable():
|
|
|
|
self.__args += ["-enable-kvm", "-cpu", "host"]
|
|
|
|
|
2023-12-17 15:32:53 +00:00
|
|
|
if soft_dbg:
|
|
|
|
self.__args += ["-d", "int"]
|
|
|
|
|
2023-12-10 18:24:05 +00:00
|
|
|
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)
|