Skip to content

Commit

Permalink
pkg/runner: enable HVF for macOS and rename KVM flag
Browse files Browse the repository at this point in the history
Rename KVM flag for more generic hardware acceleration that works both
for Linux and macOS.

Signed-off-by: Mahe Tardy <[email protected]>
  • Loading branch information
mtardy committed Aug 9, 2024
1 parent 3e5c5b8 commit 4ee7098
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
4 changes: 3 additions & 1 deletion cmd/lvh/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ func RunCommand() *cobra.Command {
cmd.Flags().BoolVar(&pullImage, "pull-image", true, "Pull image from an OCI repository if it is not found locally")
cmd.Flags().StringVar(&rcnf.KernelFname, "kernel", "", "kernel filename to boot with. (if empty no -kernel option will be passed to qemu)")
cmd.Flags().BoolVar(&rcnf.QemuPrint, "qemu-cmd-print", false, "Do not run the qemu command, just print it")
cmd.Flags().BoolVar(&rcnf.DisableKVM, "qemu-disable-kvm", false, "Do not use KVM acceleration, even if /dev/kvm exists")
cmd.Flags().BoolVar(&rcnf.DisableHardwareAccel, "qemu-disable-kvm", false, "Do not use KVM acceleration, even if /dev/kvm exists")
cmd.Flags().MarkDeprecated("qemu-disable-kvm", "use --no-hw-accel")
cmd.Flags().BoolVar(&rcnf.DisableHardwareAccel, "no-hw-accel", false, "Do not use hardware acceleration, KVM for Linux or HVF for macOS")
cmd.Flags().BoolVar(&rcnf.Daemonize, "daemonize", false, "daemonize QEMU after initializing")
cmd.Flags().StringVar(&rcnf.ConsoleLogFile, "console-log-file", "", "Save VM console output to given file")
cmd.Flags().StringVar(&rcnf.HostMount, "host-mount", "", "Mount the specified host directory in the VM using a 'host_mount' tag")
Expand Down
4 changes: 2 additions & 2 deletions pkg/runner/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ type RunConf struct {
KernelAppendArgs []string
// Do not run the qemu command, just print it
QemuPrint bool
// Do not use KVM acceleration, even if /dev/kvm exists
DisableKVM bool
// Do not use hardware acceleration, KVM for Linux or HVF for macOS
DisableHardwareAccel bool
// Daemonize QEMU after initializing
Daemonize bool
// Log file for virtual console output
Expand Down
20 changes: 13 additions & 7 deletions pkg/runner/qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"os"
"os/exec"
"runtime"
"strings"

"github.com/cilium/little-vm-helper/pkg/arch"
Expand All @@ -30,13 +31,18 @@ func BuildQemuArgs(log *logrus.Logger, rcnf *RunConf) ([]string, error) {

// quick-and-dirty kvm detection
kvmEnabled := false
if !rcnf.DisableKVM {
if f, err := os.OpenFile("/dev/kvm", os.O_RDWR, 0755); err == nil {
qemuArgs = append(qemuArgs, "-enable-kvm")
f.Close()
kvmEnabled = true
} else {
log.Info("KVM disabled")
if !rcnf.DisableHardwareAccel {
switch runtime.GOOS {
case "linux":
if f, err := os.OpenFile("/dev/kvm", os.O_RDWR, 0755); err == nil {
qemuArgs = append(qemuArgs, "-enable-kvm")
f.Close()
kvmEnabled = true
} else {
log.Info("KVM disabled")
}
case "darwin":
qemuArgs = append(qemuArgs, "-accel", "hvf")
}
}

Expand Down

0 comments on commit 4ee7098

Please sign in to comment.