Skip to content

Commit 57553f8

Browse files
committed
feat: Add limactl vz-vmnet-shared
It shares `VmnetNetwork` serialization between VMs using SharedMode. - `limactl vz-vmnet-shared --enable-mach-service`: register Mach service and launch - `limactl vz-vmnet-shared --enable-mach-service=false`: unregister Mach service When the `limactl` executable file is updated due to rebuilds, etc., the VM using the serialization data held by the Mach server before the update cannot be booted. It is necessary to add a version check and restart the service as appropriate. Also, it seems that it cannot be used with an external vz driver. Signed-off-by: Norio Nomura <[email protected]>
1 parent 8125323 commit 57553f8

File tree

9 files changed

+355
-8
lines changed

9 files changed

+355
-8
lines changed

cmd/limactl/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ func newApp() *cobra.Command {
208208
newNetworkCommand(),
209209
newCloneCommand(),
210210
newRenameCommand(),
211+
newVzVmnetSharedCommand(),
211212
)
212213
addPluginCommands(rootCmd)
213214

cmd/limactl/vz-vmnet-shared.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// SPDX-FileCopyrightText: Copyright The Lima Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package main
5+
6+
import (
7+
"github.com/spf13/cobra"
8+
)
9+
10+
func newVzVmnetSharedCommand() *cobra.Command {
11+
newCommand := &cobra.Command{
12+
Use: "vz-vmnet-shared",
13+
Short: "Run vz-vmnet-shared",
14+
Args: cobra.ExactArgs(0),
15+
RunE: newVzVmnetSharedAction,
16+
ValidArgsFunction: newVzVmnetSharedComplete,
17+
Hidden: true,
18+
}
19+
newCommand.Flags().Bool("enable-mach-service", false, "Enable Mach service")
20+
newCommand.Flags().String("mach-service", "", "Run as Mach service")
21+
_ = newCommand.Flags().MarkHidden("mach-service")
22+
return newCommand
23+
}
24+
25+
func newVzVmnetSharedComplete(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
26+
return bashCompleteInstanceNames(cmd)
27+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// SPDX-FileCopyrightText: Copyright The Lima Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package main
5+
6+
import (
7+
"errors"
8+
"os"
9+
"os/signal"
10+
"syscall"
11+
12+
"github.com/coreos/go-semver/semver"
13+
"github.com/spf13/cobra"
14+
15+
"github.com/lima-vm/lima/v2/pkg/osutil"
16+
"github.com/lima-vm/lima/v2/pkg/vzvmnetshared"
17+
)
18+
19+
func newVzVmnetSharedAction(cmd *cobra.Command, _ []string) error {
20+
macOSProductVersion, err := osutil.ProductVersion()
21+
if err != nil {
22+
return err
23+
}
24+
if macOSProductVersion.LessThan(*semver.New("26.0.0")) {
25+
return errors.New("vz-vmnet-shared requires macOS 26 or higher to run")
26+
}
27+
28+
if !cmd.HasLocalFlags() {
29+
return cmd.Help()
30+
}
31+
32+
ctx, cancel := signal.NotifyContext(cmd.Context(), os.Interrupt, syscall.SIGTERM)
33+
defer cancel()
34+
35+
if machServiceName, _ := cmd.Flags().GetString("mach-service"); machServiceName != "" {
36+
return vzvmnetshared.RunMachService(ctx, machServiceName)
37+
} else if enableMachService, _ := cmd.Flags().GetBool("enable-mach-service"); enableMachService {
38+
return vzvmnetshared.RegisterMachService(ctx)
39+
}
40+
return vzvmnetshared.UnregisterMachService(ctx)
41+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//go:build !darwin
2+
3+
// SPDX-FileCopyrightText: Copyright The Lima Authors
4+
// SPDX-License-Identifier: Apache-2.0
5+
6+
package main
7+
8+
import (
9+
"errors"
10+
11+
"github.com/spf13/cobra"
12+
)
13+
14+
func newVzVmnetSharedAction(_ *cobra.Command, _ []string) error {
15+
return errors.New("vz-vmnet-shared command is only supported on macOS")
16+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,4 @@ require (
148148
sigs.k8s.io/structured-merge-diff/v6 v6.3.0 // indirect
149149
)
150150

151-
replace github.com/Code-Hex/vz/v3 => github.com/norio-nomura/vz/v3 v3.7.2-0.20251122122159-6617c8faa123
151+
replace github.com/Code-Hex/vz/v3 => github.com/norio-nomura/vz/v3 v3.7.2-0.20251203072611-007c2a5b352c

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,8 @@ github.com/modern-go/reflect2 v1.0.3-0.20250322232337-35a7c28c31ee h1:W5t00kpgFd
207207
github.com/modern-go/reflect2 v1.0.3-0.20250322232337-35a7c28c31ee/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
208208
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
209209
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
210-
github.com/norio-nomura/vz/v3 v3.7.2-0.20251122122159-6617c8faa123 h1:3Xzg1W5gel17So2d2NSA+flx6yoyknx5nG9Pb6eZU6s=
211-
github.com/norio-nomura/vz/v3 v3.7.2-0.20251122122159-6617c8faa123/go.mod h1:+0IVfZY7N/7Vv5KpZWbEgTRK6jMg4s7DVM+op2hdyrs=
210+
github.com/norio-nomura/vz/v3 v3.7.2-0.20251203072611-007c2a5b352c h1:0QGVXjk6/KA2G5yLQZGKZf9GB8caGCMS5H3sy0zPoH0=
211+
github.com/norio-nomura/vz/v3 v3.7.2-0.20251203072611-007c2a5b352c/go.mod h1:+0IVfZY7N/7Vv5KpZWbEgTRK6jMg4s7DVM+op2hdyrs=
212212
github.com/nxadm/tail v1.4.11 h1:8feyoE3OzPrcshW5/MJ4sGESc5cqmGkGCWlco4l0bqY=
213213
github.com/nxadm/tail v1.4.11/go.mod h1:OTaG3NK980DZzxbRq6lEuzgU+mug70nY11sMd4JXXHc=
214214
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=

pkg/driver/vz/vm_darwin.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"fmt"
1212
"io/fs"
1313
"net"
14+
"net/netip"
1415
"os"
1516
"path/filepath"
1617
"runtime"
@@ -37,6 +38,7 @@ import (
3738
"github.com/lima-vm/lima/v2/pkg/networks/usernet"
3839
"github.com/lima-vm/lima/v2/pkg/osutil"
3940
"github.com/lima-vm/lima/v2/pkg/store"
41+
"github.com/lima-vm/lima/v2/pkg/vzvmnetshared"
4042
)
4143

4244
// diskImageCachingMode is set to DiskImageCachingModeCached so as to avoid disk corruption on ARM:
@@ -374,11 +376,8 @@ func attachNetwork(ctx context.Context, inst *limatype.Instance, vmConfig *vz.Vi
374376
}
375377
configurations = append(configurations, networkConfig)
376378
} else if nw.VZShared != nil && *nw.VZShared {
377-
config, err := vz.NewVmnetNetworkConfiguration(vz.SharedMode)
378-
if err != nil {
379-
return err
380-
}
381-
network, err := vz.NewVmnetNetwork(config)
379+
subnet := netip.MustParsePrefix("192.168.107.0/24")
380+
network, err := vzvmnetshared.RequestSharedVmnetNetwork(ctx, subnet)
382381
if err != nil {
383382
return err
384383
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>Label</key>
6+
<string>{{.Label}}</string>
7+
<key>ProgramArguments</key>
8+
<array>
9+
{{- range $arg := .ProgramArguments}}
10+
<string>{{$arg}}</string>
11+
{{- end}}
12+
</array>
13+
<key>RunAtLoad</key>
14+
<true/>
15+
<key>WorkingDirectory</key>
16+
<string>{{ .WorkingDirectory }}</string>
17+
<key>StandardErrorPath</key>
18+
<string>{{ .WorkingDirectory }}/stderr.log</string>
19+
<key>StandardOutPath</key>
20+
<string>{{ .WorkingDirectory }}/stdout.log</string>
21+
<key>MachServices</key>
22+
<dict>
23+
{{- range $service := .MachServices}}
24+
<key>{{$service}}</key>
25+
<true/>
26+
{{- end}}
27+
</dict>
28+
</dict>
29+
</plist>

0 commit comments

Comments
 (0)