Skip to content

Refactor: Decouple Qemu Internal from pkg/instance #3377

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 0 additions & 23 deletions pkg/instance/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,14 @@ import (
"os"
"os/exec"
"path/filepath"
"runtime"
"syscall"
"text/template"
"time"

"github.com/coreos/go-semver/semver"
"github.com/lima-vm/lima/pkg/driver"
"github.com/lima-vm/lima/pkg/driverutil"
"github.com/lima-vm/lima/pkg/executil"
"github.com/lima-vm/lima/pkg/osutil"
"github.com/lima-vm/lima/pkg/qemu"
"github.com/lima-vm/lima/pkg/qemu/entitlementutil"
"github.com/mattn/go-isatty"

"github.com/lima-vm/lima/pkg/downloader"
Expand Down Expand Up @@ -134,25 +130,6 @@ func Start(ctx context.Context, inst *store.Instance, limactl string, launchHost

haSockPath := filepath.Join(inst.Dir, filenames.HostAgentSock)

// Ask the user to sign the qemu binary with the "com.apple.security.hypervisor" if needed.
// Workaround for https://github.com/lima-vm/lima/issues/1742
if runtime.GOOS == "darwin" && inst.VMType == limayaml.QEMU {
macOSProductVersion, err := osutil.ProductVersion()
if err != nil {
return err
}
// The codesign --xml option is only available on macOS Monterey and later
if !macOSProductVersion.LessThan(*semver.New("12.0.0")) {
qExe, _, err := qemu.Exe(inst.Arch)
if err != nil {
return fmt.Errorf("failed to find the QEMU binary for the architecture %q: %w", inst.Arch, err)
}
if accel := qemu.Accel(inst.Arch); accel == "hvf" {
entitlementutil.AskToSignIfNotSignedProperly(qExe)
}
}
}

prepared, err := Prepare(ctx, inst)
if err != nil {
return err
Expand Down
30 changes: 30 additions & 0 deletions pkg/qemu/qemu_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ import (
"text/template"
"time"

"github.com/coreos/go-semver/semver"
"github.com/digitalocean/go-qemu/qmp"
"github.com/digitalocean/go-qemu/qmp/raw"
"github.com/lima-vm/lima/pkg/driver"
"github.com/lima-vm/lima/pkg/executil"
"github.com/lima-vm/lima/pkg/limayaml"
"github.com/lima-vm/lima/pkg/networks/usernet"
"github.com/lima-vm/lima/pkg/osutil"
"github.com/lima-vm/lima/pkg/qemu/entitlementutil"
"github.com/lima-vm/lima/pkg/store"
"github.com/lima-vm/lima/pkg/store/filenames"
"github.com/sirupsen/logrus"
Expand All @@ -47,6 +50,12 @@ func New(driver *driver.BaseDriver) *LimaQemuDriver {
}

func (l *LimaQemuDriver) Validate() error {
if runtime.GOOS == "darwin" {
if err := l.checkBinarySignature(); err != nil {
return err
}
}

if *l.Instance.Config.MountType == limayaml.VIRTIOFS && runtime.GOOS != "linux" {
return fmt.Errorf("field `mountType` must be %q or %q for QEMU driver on non-Linux, got %q",
limayaml.REVSSHFS, limayaml.NINEP, *l.Instance.Config.MountType)
Expand Down Expand Up @@ -233,6 +242,27 @@ func waitFileExists(path string, timeout time.Duration) error {
return nil
}

// Ask the user to sign the qemu binary with the "com.apple.security.hypervisor" if needed.
// Workaround for https://github.com/lima-vm/lima/issues/1742
func (l *LimaQemuDriver) checkBinarySignature() error {
macOSProductVersion, err := osutil.ProductVersion()
if err != nil {
return err
}
// The codesign --xml option is only available on macOS Monterey and later
if !macOSProductVersion.LessThan(*semver.New("12.0.0")) {
qExe, _, err := Exe(l.BaseDriver.Instance.Arch)
if err != nil {
return fmt.Errorf("failed to find the QEMU binary for the architecture %q: %w", l.BaseDriver.Instance.Arch, err)
}
if accel := Accel(l.BaseDriver.Instance.Arch); accel == "hvf" {
entitlementutil.AskToSignIfNotSignedProperly(qExe)
}
}

return nil
}

func (l *LimaQemuDriver) changeVNCPassword(password string) error {
qmpSockPath := filepath.Join(l.Instance.Dir, filenames.QMPSock)
err := waitFileExists(qmpSockPath, 30*time.Second)
Expand Down
Loading