Skip to content

Commit

Permalink
vz: add support for nested virtualization
Browse files Browse the repository at this point in the history
Signed-off-by: Abiola Ibrahim <git@abiosoft.com>
  • Loading branch information
abiosoft committed Oct 8, 2024
1 parent a26c031 commit 10a60fe
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 8 deletions.
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,7 @@ require (
// We can't just `require` github.com/inetaf/tcpproxy, as gvisor-tap-vsock
// still imports inet.af/tcpproxy: https://github.com/containers/gvisor-tap-vsock/pull/399
replace inet.af/tcpproxy => github.com/inetaf/tcpproxy v0.0.0-20240214030015-3ce58045626c

// Nested virtualization support is yet to be merged into VZ https://github.com/Code-Hex/vz/pull/159.
// We use our (temporary) fork to add the feature.
replace github.com/Code-Hex/vz/v3 => github.com/lima-vm/vz/v3 v3.0.0-20241008080607-2a22b5e278ee
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ github.com/AlecAivazis/survey/v2 v2.3.7 h1:6I/u8FvytdGsgonrYsVn2t8t4QiRnh6QSTqkk
github.com/AlecAivazis/survey/v2 v2.3.7/go.mod h1:xUTIdE4KCOIjsBAE1JYsUPoCqYdZ1reCfTwbto0Fduo=
github.com/Code-Hex/go-infinity-channel v1.0.0 h1:M8BWlfDOxq9or9yvF9+YkceoTkDI1pFAqvnP87Zh0Nw=
github.com/Code-Hex/go-infinity-channel v1.0.0/go.mod h1:5yUVg/Fqao9dAjcpzoQ33WwfdMWmISOrQloDRn3bsvY=
github.com/Code-Hex/vz/v3 v3.1.0 h1:rcMIbZwPYwf78yXOhK68DZgYMdzxlrdmpDuM+NnGf1I=
github.com/Code-Hex/vz/v3 v3.1.0/go.mod h1:xUfvg1VJ5A6ZQNuzQERwXJ7l2ZdTnY6eCy9CIS6/DYQ=
github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY=
github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=
github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2 h1:+vx7roKuyA63nhn5WAunQHLTznkw5W8b1Xc0dNjp83s=
Expand Down Expand Up @@ -171,6 +169,8 @@ github.com/lima-vm/go-qcow2reader v0.1.2 h1:A9+h9Yg2oWDxmMlE2oJsdd+djaKk+Ge4hawv
github.com/lima-vm/go-qcow2reader v0.1.2/go.mod h1:e3p29BzLT8hNh4jbLezdFAHU4eBijf0bm5GilStCRKE=
github.com/lima-vm/sshocker v0.3.4 h1:5rn6vMkfqwZSZiBW+Udo505OIRhPB4xbLUDdEnFgWwI=
github.com/lima-vm/sshocker v0.3.4/go.mod h1:QT4c7XNmeQTv79h5/8EgiS7U51B9BLenlXV7idCY0tE=
github.com/lima-vm/vz/v3 v3.0.0-20241008080607-2a22b5e278ee h1:USiLYd9WbmtU1mPM0egUMrz9QVpMkblMyKioC2EsWCA=
github.com/lima-vm/vz/v3 v3.0.0-20241008080607-2a22b5e278ee/go.mod h1:WqWQuBbT4SbjO4C4GHG9m9HO8j5jecAmMh4eyVSEbEg=
github.com/linuxkit/virtsock v0.0.0-20220523201153-1a23e78aa7a2 h1:DZMFueDbfz6PNc1GwDRA8+6lBx1TB9UnxDQliCqR73Y=
github.com/linuxkit/virtsock v0.0.0-20220523201153-1a23e78aa7a2/go.mod h1:SWzULI85WerrFt3u+nIm5F9l7EvxZTKQvd0InF3nmgM=
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
Expand Down
10 changes: 10 additions & 0 deletions pkg/limayaml/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,16 @@ func FillDefault(y, d, o *LimaYAML, filePath string) {
y.Rosetta.BinFmt = ptr.Of(false)
}

if y.NestedVirtualization == nil {
y.NestedVirtualization = d.NestedVirtualization
}
if o.NestedVirtualization != nil {
y.NestedVirtualization = o.NestedVirtualization
}
if y.NestedVirtualization == nil {
y.NestedVirtualization = ptr.Of(false)
}

if y.Plain == nil {
y.Plain = d.Plain
}
Expand Down
9 changes: 8 additions & 1 deletion pkg/limayaml/defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ func TestFillDefault(t *testing.T) {
CACertificates: CACertificates{
RemoveDefaults: ptr.Of(false),
},
Plain: ptr.Of(false),
NestedVirtualization: ptr.Of(false),
Plain: ptr.Of(false),
}

defaultPortForward := PortForward{
Expand Down Expand Up @@ -293,6 +294,8 @@ func TestFillDefault(t *testing.T) {
BinFmt: ptr.Of(false),
}

expect.NestedVirtualization = ptr.Of(false)

FillDefault(&y, &LimaYAML{}, &LimaYAML{}, filePath)
assert.DeepEqual(t, &y, &expect, opts...)

Expand Down Expand Up @@ -420,6 +423,7 @@ func TestFillDefault(t *testing.T) {
Enabled: ptr.Of(true),
BinFmt: ptr.Of(true),
},
NestedVirtualization: ptr.Of(true),
}

expect = d
Expand Down Expand Up @@ -634,6 +638,7 @@ func TestFillDefault(t *testing.T) {
Enabled: ptr.Of(false),
BinFmt: ptr.Of(false),
},
NestedVirtualization: ptr.Of(false),
}

y = filledDefaults
Expand Down Expand Up @@ -690,6 +695,8 @@ func TestFillDefault(t *testing.T) {
}
expect.Plain = ptr.Of(false)

expect.NestedVirtualization = ptr.Of(false)

FillDefault(&y, &d, &o, filePath)
assert.DeepEqual(t, &y, &expect, opts...)
}
Expand Down
11 changes: 6 additions & 5 deletions pkg/limayaml/limayaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ type LimaYAML struct {
DNS []net.IP `yaml:"dns,omitempty" json:"dns,omitempty"`
HostResolver HostResolver `yaml:"hostResolver,omitempty" json:"hostResolver,omitempty"`
// `useHostResolver` was deprecated in Lima v0.8.1, removed in Lima v0.14.0. Use `hostResolver.enabled` instead.
PropagateProxyEnv *bool `yaml:"propagateProxyEnv,omitempty" json:"propagateProxyEnv,omitempty"`
CACertificates CACertificates `yaml:"caCerts,omitempty" json:"caCerts,omitempty"`
Rosetta Rosetta `yaml:"rosetta,omitempty" json:"rosetta,omitempty"`
Plain *bool `yaml:"plain,omitempty" json:"plain,omitempty"`
TimeZone *string `yaml:"timezone,omitempty" json:"timezone,omitempty"`
PropagateProxyEnv *bool `yaml:"propagateProxyEnv,omitempty" json:"propagateProxyEnv,omitempty"`
CACertificates CACertificates `yaml:"caCerts,omitempty" json:"caCerts,omitempty"`
Rosetta Rosetta `yaml:"rosetta,omitempty" json:"rosetta,omitempty"`
Plain *bool `yaml:"plain,omitempty" json:"plain,omitempty"`
TimeZone *string `yaml:"timezone,omitempty" json:"timezone,omitempty"`
NestedVirtualization *bool `yaml:"nestedVirtualization,omitempty" json:"nestedVirtualization,omitempty"`
}

type (
Expand Down
23 changes: 23 additions & 0 deletions pkg/vz/vm_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"syscall"

"github.com/Code-Hex/vz/v3"
"github.com/coreos/go-semver/semver"
"github.com/docker/go-units"
"github.com/lima-vm/go-qcow2reader"
"github.com/lima-vm/go-qcow2reader/image/raw"
Expand All @@ -25,6 +26,7 @@ import (
"github.com/lima-vm/lima/pkg/nativeimgutil"
"github.com/lima-vm/lima/pkg/networks"
"github.com/lima-vm/lima/pkg/networks/usernet"
"github.com/lima-vm/lima/pkg/osutil"
"github.com/lima-vm/lima/pkg/store"
"github.com/lima-vm/lima/pkg/store/filenames"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -234,6 +236,27 @@ func attachPlatformConfig(driver *driver.BaseDriver, vmConfig *vz.VirtualMachine
if err != nil {
return err
}

// nested virt
if *driver.Instance.Config.NestedVirtualization {
macOSProductVersion, err := osutil.ProductVersion()
if err != nil {
return fmt.Errorf("failed to get macOS product version: %w", err)
}

if macOSProductVersion.LessThan(*semver.New("15.0.0")) {
return errors.New("nested virtualization requires macOS 15 or newer")
}

if !vz.IsNestedVirtualizationSupported() {
return errors.New("nested virtualization is not supported on this device")
}

if err := platformConfig.SetNestedVirtualizationEnabled(true); err != nil {
return fmt.Errorf("cannot enable nested virtualization: %w", err)
}
}

vmConfig.SetPlatformVirtualMachineConfiguration(platformConfig)
return nil
}
Expand Down
1 change: 1 addition & 0 deletions pkg/vz/vz_driver_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ var knownYamlProperties = []string{
"Mounts",
"MountType",
"MountInotify",
"NestedVirtualization",
"Networks",
"OS",
"Param",
Expand Down

0 comments on commit 10a60fe

Please sign in to comment.