diff --git a/virtme/architectures.py b/virtme/architectures.py index b30dc3d..0466693 100644 --- a/virtme/architectures.py +++ b/virtme/architectures.py @@ -23,8 +23,9 @@ def virtiofs_support() -> bool: return False @staticmethod - def qemuargs(is_native) -> List[str]: + def qemuargs(is_native, use_kvm) -> List[str]: _ = is_native + _ = use_kvm return [] @staticmethod @@ -77,8 +78,8 @@ def dtb_path() -> Optional[str]: class Arch_unknown(Arch): @staticmethod - def qemuargs(is_native): - return Arch.qemuargs(is_native) + def qemuargs(is_native, use_kvm): + return Arch.qemuargs(is_native, use_kvm) class Arch_x86(Arch): @@ -93,13 +94,13 @@ def virtiofs_support() -> bool: return True @staticmethod - def qemuargs(is_native): - ret = Arch.qemuargs(is_native) + def qemuargs(is_native, use_kvm): + ret = Arch.qemuargs(is_native, use_kvm) # Add a watchdog. This is useful for testing. ret.extend(["-device", "i6300esb,id=watchdog0"]) - if is_native and os.access("/dev/kvm", os.R_OK): + if is_native and use_kvm: # If we're likely to use KVM, request a full-featured CPU. # (NB: if KVM fails, this will cause problems. We should probe.) ret.extend(["-cpu", "host"]) # We can't migrate regardless. @@ -176,13 +177,13 @@ def qemu_display_args() -> List[str]: ] @staticmethod - def qemuargs(is_native): - ret = Arch.qemuargs(is_native) + def qemuargs(is_native, use_kvm): + ret = Arch.qemuargs(is_native, use_kvm) # Use microvm architecture for faster boot ret.extend(["-M", "microvm,accel=kvm,pcie=on"]) - if is_native and os.access("/dev/kvm", os.R_OK): + if is_native and use_kvm: # If we're likely to use KVM, request a full-featured CPU. # (NB: if KVM fails, this will cause problems. We should probe.) ret.extend(["-cpu", "host"]) # We can't migrate regardless. @@ -197,8 +198,8 @@ def __init__(self): self.defconfig_target = "vexpress_defconfig" @staticmethod - def qemuargs(is_native): - ret = Arch.qemuargs(is_native) + def qemuargs(is_native, use_kvm): + ret = Arch.qemuargs(is_native, use_kvm) # Emulate a vexpress-a15. ret.extend(["-M", "vexpress-a15"]) @@ -247,8 +248,8 @@ def __init__(self): self.gccname = "aarch64" @staticmethod - def qemuargs(is_native): - ret = Arch.qemuargs(is_native) + def qemuargs(is_native, use_kvm): + ret = Arch.qemuargs(is_native, use_kvm) if is_native: ret.extend(["-M", "virt,gic-version=host"]) @@ -289,8 +290,8 @@ def __init__(self, name): self.gccname = "powerpc64le" @staticmethod - def qemuargs(is_native): - ret = Arch.qemuargs(is_native) + def qemuargs(is_native, use_kvm): + ret = Arch.qemuargs(is_native, use_kvm) ret.extend(["-M", "pseries"]) return ret @@ -324,8 +325,8 @@ def virtiofs_support() -> bool: return True @staticmethod - def qemuargs(is_native): - ret = Arch.qemuargs(is_native) + def qemuargs(is_native, use_kvm): + ret = Arch.qemuargs(is_native, use_kvm) ret.extend(["-machine", "virt"]) ret.extend(["-bios", "default"]) @@ -349,8 +350,8 @@ def __init__(self): self.gccname = "sparc64" @staticmethod - def qemuargs(is_native): - return Arch.qemuargs(is_native) + def qemuargs(is_native, use_kvm): + return Arch.qemuargs(is_native, use_kvm) def kimg_path(self): return "arch/sparc/boot/image" @@ -374,8 +375,8 @@ def virtio_dev_type(virtiotype): return "virtio-%s-ccw" % virtiotype @staticmethod - def qemuargs(is_native): - ret = Arch.qemuargs(is_native) + def qemuargs(is_native, use_kvm): + ret = Arch.qemuargs(is_native, use_kvm) # Ask for the latest version of s390-ccw ret.extend(["-M", "s390-ccw-virtio"]) diff --git a/virtme/commands/run.py b/virtme/commands/run.py index 9ed64b6..65c7184 100644 --- a/virtme/commands/run.py +++ b/virtme/commands/run.py @@ -233,6 +233,11 @@ def make_parser() -> argparse.ArgumentParser: action="store_true", help='Avoid using the "microvm" QEMU architecture (only on x86_64)', ) + g.add_argument( + "--disable-kvm", + action="store_true", + help='Avoid using hardware virtualization / KVM', + ) g.add_argument( "--force-initramfs", action="store_true", @@ -720,7 +725,9 @@ def sanitize_disk_args(func: str, arg: str) -> Tuple[str, str]: return name, fn -def can_use_kvm(): +def can_use_kvm(args): + if args.disable_kvm: + return False if not os.path.exists("/dev/kvm"): return False try: @@ -733,7 +740,7 @@ def can_use_kvm(): def can_use_microvm(args): - return not args.disable_microvm and not args.numa and args.arch == "x86_64" and can_use_kvm() + return not args.disable_microvm and not args.numa and args.arch == "x86_64" and can_use_kvm(args) def has_read_acl(username, file_path): @@ -963,11 +970,12 @@ def do_it() -> int: kernelargs.append("virtme_rw_overlay%d=%s" % (i, d)) # Turn on KVM if available - if is_native and can_use_kvm(): + kvm_ok = can_use_kvm(args) + if is_native and kvm_ok: qemuargs.extend(["-machine", "accel=kvm:tcg"]) # Add architecture-specific options - qemuargs.extend(arch.qemuargs(is_native)) + qemuargs.extend(arch.qemuargs(is_native, kvm_ok)) # Set up / override baseline devices qemuargs.extend(["-parallel", "none"]) diff --git a/virtme_ng/run.py b/virtme_ng/run.py index fd45688..4d18ffa 100644 --- a/virtme_ng/run.py +++ b/virtme_ng/run.py @@ -235,6 +235,12 @@ def make_parser(): help='Avoid using the "microvm" QEMU architecture (only on x86_64)', ) + parser.add_argument( + "--disable-kvm", + action="store_true", + help='Avoid using hardware virtualization / KVM', + ) + parser.add_argument( "--cwd", action="store", @@ -855,6 +861,12 @@ def _get_virtme_disable_microvm(self, args): else: self.virtme_param["disable_microvm"] = "" + def _get_virtme_disable_kvm(self, args): + if args.disable_kvm: + self.virtme_param["disable_kvm"] = "--disable-kvm" + else: + self.virtme_param["disable_kvm"] = "" + def _get_virtme_9p(self, args): if args.force_9p: self.virtme_param["force_9p"] = "--force-9p" @@ -965,6 +977,7 @@ def run(self, args): self._get_virtme_disk(args) self._get_virtme_sound(args) self._get_virtme_disable_microvm(args) + self._get_virtme_disable_kvm(args) self._get_virtme_9p(args) self._get_virtme_initramfs(args) self._get_virtme_graphics(args) @@ -1000,6 +1013,7 @@ def run(self, args): + f'{self.virtme_param["disk"]} ' + f'{self.virtme_param["sound"]} ' + f'{self.virtme_param["disable_microvm"]} ' + + f'{self.virtme_param["disable_kvm"]} ' + f'{self.virtme_param["force_9p"]} ' + f'{self.virtme_param["force_initramfs"]} ' + f'{self.virtme_param["graphics"]} '