Skip to content

Commit b5f9998

Browse files
committed
vz: add support for nested virtualization
Signed-off-by: Abiola Ibrahim <[email protected]>
1 parent b5b5600 commit b5f9998

File tree

5 files changed

+48
-6
lines changed

5 files changed

+48
-6
lines changed

pkg/limayaml/defaults.go

+10
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,16 @@ func FillDefault(y, d, o *LimaYAML, filePath string) {
735735
y.Rosetta.BinFmt = ptr.Of(false)
736736
}
737737

738+
if y.NestedVirtualization == nil {
739+
y.NestedVirtualization = d.NestedVirtualization
740+
}
741+
if o.NestedVirtualization != nil {
742+
y.NestedVirtualization = o.NestedVirtualization
743+
}
744+
if y.NestedVirtualization == nil {
745+
y.NestedVirtualization = ptr.Of(false)
746+
}
747+
738748
if y.Plain == nil {
739749
y.Plain = d.Plain
740750
}

pkg/limayaml/defaults_test.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ func TestFillDefault(t *testing.T) {
106106
CACertificates: CACertificates{
107107
RemoveDefaults: ptr.Of(false),
108108
},
109-
Plain: ptr.Of(false),
109+
NestedVirtualization: ptr.Of(false),
110+
Plain: ptr.Of(false),
110111
}
111112

112113
defaultPortForward := PortForward{
@@ -293,6 +294,8 @@ func TestFillDefault(t *testing.T) {
293294
BinFmt: ptr.Of(false),
294295
}
295296

297+
expect.NestedVirtualization = ptr.Of(false)
298+
296299
FillDefault(&y, &LimaYAML{}, &LimaYAML{}, filePath)
297300
assert.DeepEqual(t, &y, &expect, opts...)
298301

@@ -420,6 +423,7 @@ func TestFillDefault(t *testing.T) {
420423
Enabled: ptr.Of(true),
421424
BinFmt: ptr.Of(true),
422425
},
426+
NestedVirtualization: ptr.Of(true),
423427
}
424428

425429
expect = d
@@ -634,6 +638,7 @@ func TestFillDefault(t *testing.T) {
634638
Enabled: ptr.Of(false),
635639
BinFmt: ptr.Of(false),
636640
},
641+
NestedVirtualization: ptr.Of(false),
637642
}
638643

639644
y = filledDefaults
@@ -690,6 +695,8 @@ func TestFillDefault(t *testing.T) {
690695
}
691696
expect.Plain = ptr.Of(false)
692697

698+
expect.NestedVirtualization = ptr.Of(false)
699+
693700
FillDefault(&y, &d, &o, filePath)
694701
assert.DeepEqual(t, &y, &expect, opts...)
695702
}

pkg/limayaml/limayaml.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,12 @@ type LimaYAML struct {
3838
DNS []net.IP `yaml:"dns,omitempty" json:"dns,omitempty"`
3939
HostResolver HostResolver `yaml:"hostResolver,omitempty" json:"hostResolver,omitempty"`
4040
// `useHostResolver` was deprecated in Lima v0.8.1, removed in Lima v0.14.0. Use `hostResolver.enabled` instead.
41-
PropagateProxyEnv *bool `yaml:"propagateProxyEnv,omitempty" json:"propagateProxyEnv,omitempty"`
42-
CACertificates CACertificates `yaml:"caCerts,omitempty" json:"caCerts,omitempty"`
43-
Rosetta Rosetta `yaml:"rosetta,omitempty" json:"rosetta,omitempty"`
44-
Plain *bool `yaml:"plain,omitempty" json:"plain,omitempty"`
45-
TimeZone *string `yaml:"timezone,omitempty" json:"timezone,omitempty"`
41+
PropagateProxyEnv *bool `yaml:"propagateProxyEnv,omitempty" json:"propagateProxyEnv,omitempty"`
42+
CACertificates CACertificates `yaml:"caCerts,omitempty" json:"caCerts,omitempty"`
43+
Rosetta Rosetta `yaml:"rosetta,omitempty" json:"rosetta,omitempty"`
44+
Plain *bool `yaml:"plain,omitempty" json:"plain,omitempty"`
45+
TimeZone *string `yaml:"timezone,omitempty" json:"timezone,omitempty"`
46+
NestedVirtualization *bool `yaml:"nestedVirtualization,omitempty" json:"nestedVirtualization,omitempty"`
4647
}
4748

4849
type (

pkg/vz/vm_darwin.go

+23
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"syscall"
1616

1717
"github.com/Code-Hex/vz/v3"
18+
"github.com/coreos/go-semver/semver"
1819
"github.com/docker/go-units"
1920
"github.com/lima-vm/go-qcow2reader"
2021
"github.com/lima-vm/go-qcow2reader/image/raw"
@@ -25,6 +26,7 @@ import (
2526
"github.com/lima-vm/lima/pkg/nativeimgutil"
2627
"github.com/lima-vm/lima/pkg/networks"
2728
"github.com/lima-vm/lima/pkg/networks/usernet"
29+
"github.com/lima-vm/lima/pkg/osutil"
2830
"github.com/lima-vm/lima/pkg/store"
2931
"github.com/lima-vm/lima/pkg/store/filenames"
3032
"github.com/sirupsen/logrus"
@@ -234,6 +236,27 @@ func attachPlatformConfig(driver *driver.BaseDriver, vmConfig *vz.VirtualMachine
234236
if err != nil {
235237
return err
236238
}
239+
240+
// nested virt
241+
if *driver.Yaml.NestedVirtualization {
242+
macOSProductVersion, err := osutil.ProductVersion()
243+
if err != nil {
244+
return fmt.Errorf("failed to get macOS product version: %w", err)
245+
}
246+
247+
if macOSProductVersion.LessThan(*semver.New("15.0.0")) {
248+
return errors.New("nested virtualization requires macOS 15 or newer")
249+
}
250+
251+
if !platformConfig.IsNestedVirtualizationSupported() {
252+
return errors.New("nested virtualization is not supported on this device")
253+
}
254+
255+
if err := platformConfig.SetNestedVirtualizationEnabled(true); err != nil {
256+
return fmt.Errorf("cannot enable nested virtualization: %w", err)
257+
}
258+
}
259+
237260
vmConfig.SetPlatformVirtualMachineConfiguration(platformConfig)
238261
return nil
239262
}

pkg/vz/vz_driver_darwin.go

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ var knownYamlProperties = []string{
4040
"Mounts",
4141
"MountType",
4242
"MountInotify",
43+
"NestedVirtualization",
4344
"Networks",
4445
"OS",
4546
"Param",

0 commit comments

Comments
 (0)