Skip to content

Commit

Permalink
Merge pull request #64 from kuba-moo/main
Browse files Browse the repository at this point in the history
virtme: let user disable KVM accel easily
  • Loading branch information
Andrea Righi authored Feb 2, 2024
2 parents 28e29e2 + 28f0710 commit b7cb323
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 25 deletions.
43 changes: 22 additions & 21 deletions virtme/architectures.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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"])
Expand Down Expand Up @@ -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"])
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"])

Expand All @@ -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"
Expand All @@ -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"])
Expand Down
16 changes: 12 additions & 4 deletions virtme/commands/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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:
Expand All @@ -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):
Expand Down Expand Up @@ -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"])
Expand Down
14 changes: 14 additions & 0 deletions virtme_ng/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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"]} '
Expand Down

0 comments on commit b7cb323

Please sign in to comment.