Skip to content

Commit 2076ff1

Browse files
committed
decouple vm-type specific default filling and validation for internal drivers
Signed-off-by: Ansuman Sahoo <[email protected]> experiment with wsl2 in ha Signed-off-by: Ansuman Sahoo <[email protected]> boot scripts for wsl2 Signed-off-by: Ansuman Sahoo <[email protected]>
1 parent 22af485 commit 2076ff1

36 files changed

+915
-735
lines changed

cmd/limactl/clone.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/spf13/cobra"
1313

1414
"github.com/lima-vm/lima/v2/cmd/limactl/editflags"
15+
"github.com/lima-vm/lima/v2/pkg/driverutil"
1516
"github.com/lima-vm/lima/v2/pkg/instance"
1617
"github.com/lima-vm/lima/v2/pkg/limatype/filenames"
1718
"github.com/lima-vm/lima/v2/pkg/limayaml"
@@ -79,6 +80,9 @@ func cloneAction(cmd *cobra.Command, args []string) error {
7980
if err != nil {
8081
return err
8182
}
83+
if err := driverutil.ResolveVMType(y, filePath); err != nil {
84+
return fmt.Errorf("failed to accept config for %q: %w", filePath, err)
85+
}
8286
if err := limayaml.Validate(y, true); err != nil {
8387
return saveRejectedYAML(yBytes, err)
8488
}

cmd/limactl/edit.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/spf13/cobra"
1515

1616
"github.com/lima-vm/lima/v2/cmd/limactl/editflags"
17+
"github.com/lima-vm/lima/v2/pkg/driverutil"
1718
"github.com/lima-vm/lima/v2/pkg/editutil"
1819
"github.com/lima-vm/lima/v2/pkg/instance"
1920
"github.com/lima-vm/lima/v2/pkg/limatype"
@@ -120,6 +121,9 @@ func editAction(cmd *cobra.Command, args []string) error {
120121
if err != nil {
121122
return err
122123
}
124+
if err := driverutil.ResolveVMType(y, filePath); err != nil {
125+
return fmt.Errorf("failed to accept config for %q: %w", filePath, err)
126+
}
123127
if err := limayaml.Validate(y, true); err != nil {
124128
return saveRejectedYAML(yBytes, err)
125129
}

cmd/limactl/editflags/editflags.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import (
1515
"github.com/sirupsen/logrus"
1616
"github.com/spf13/cobra"
1717
flag "github.com/spf13/pflag"
18+
19+
"github.com/lima-vm/lima/v2/pkg/registry"
1820
)
1921

2022
// RegisterEdit registers flags related to in-place YAML modification, for `limactl edit`.
@@ -75,6 +77,15 @@ func RegisterEdit(cmd *cobra.Command, commentPrefix string) {
7577
_ = cmd.RegisterFlagCompletionFunc("disk", func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) {
7678
return []string{"10", "30", "50", "100", "200"}, cobra.ShellCompDirectiveNoFileComp
7779
})
80+
81+
flags.String("vm-type", "", commentPrefix+"Virtual machine type")
82+
_ = cmd.RegisterFlagCompletionFunc("vm-type", func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) {
83+
var drivers []string
84+
for k := range registry.List() {
85+
drivers = append(drivers, k)
86+
}
87+
return drivers, cobra.ShellCompDirectiveNoFileComp
88+
})
7889
}
7990

8091
// RegisterCreate registers flags related to in-place YAML modification, for `limactl create`.
@@ -92,11 +103,6 @@ func RegisterCreate(cmd *cobra.Command, commentPrefix string) {
92103
return []string{"user", "system", "user+system", "none"}, cobra.ShellCompDirectiveNoFileComp
93104
})
94105

95-
flags.String("vm-type", "", commentPrefix+"Virtual machine type (qemu, vz)") // colima-compatible
96-
_ = cmd.RegisterFlagCompletionFunc("vm-type", func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) {
97-
return []string{"qemu", "vz"}, cobra.ShellCompDirectiveNoFileComp
98-
})
99-
100106
flags.Bool("plain", false, commentPrefix+"Plain mode. Disables mounts, port forwarding, containerd, etc.")
101107
}
102108

@@ -177,6 +183,7 @@ func YQExpressions(flags *flag.FlagSet, newInstance bool) ([]string, error) {
177183
false,
178184
},
179185
{"mount-type", d(".mountType = %q"), false, false},
186+
{"vm-type", d(".vmType = %q"), false, false},
180187
{"mount-inotify", d(".mountInotify = %s"), false, true},
181188
{"mount-writable", d(".mounts[].writable = %s"), false, false},
182189
{
@@ -262,7 +269,6 @@ func YQExpressions(flags *flag.FlagSet, newInstance bool) ([]string, error) {
262269
false,
263270
},
264271
{"disk", d(".disk= \"%sGiB\""), false, false},
265-
{"vm-type", d(".vmType = %q"), true, false},
266272
{"plain", d(".plain = %s"), true, false},
267273
}
268274
var exprs []string

cmd/limactl/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ func main() {
4444
}
4545
rootCmd := newApp()
4646
if err := executeWithPluginSupport(rootCmd, os.Args[1:]); err != nil {
47+
server.StopAllExternalDrivers()
4748
handleExitCoder(err)
4849
logrus.Fatal(err)
4950
}

cmd/limactl/prune.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ package main
55

66
import (
77
"context"
8+
"fmt"
89
"maps"
910
"os"
1011

1112
"github.com/sirupsen/logrus"
1213
"github.com/spf13/cobra"
1314

1415
"github.com/lima-vm/lima/v2/pkg/downloader"
16+
"github.com/lima-vm/lima/v2/pkg/driverutil"
1517
"github.com/lima-vm/lima/v2/pkg/limatype"
1618
"github.com/lima-vm/lima/v2/pkg/limayaml"
1719
"github.com/lima-vm/lima/v2/pkg/store"
@@ -95,6 +97,9 @@ func knownLocations(ctx context.Context) (map[string]limatype.File, error) {
9597
if err != nil {
9698
return nil, err
9799
}
100+
if err := driverutil.ResolveVMType(y, t.Name); err != nil {
101+
return nil, fmt.Errorf("failed to accept config for %q: %w", t.Name, err)
102+
}
98103
maps.Copy(locations, locationsFromLimaYAML(y))
99104
}
100105
return locations, nil

cmd/limactl/start.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/spf13/pflag"
1818

1919
"github.com/lima-vm/lima/v2/cmd/limactl/editflags"
20+
"github.com/lima-vm/lima/v2/pkg/driverutil"
2021
"github.com/lima-vm/lima/v2/pkg/editutil"
2122
"github.com/lima-vm/lima/v2/pkg/instance"
2223
"github.com/lima-vm/lima/v2/pkg/limatmpl"
@@ -362,6 +363,9 @@ func applyYQExpressionToExistingInstance(ctx context.Context, inst *limatype.Ins
362363
if err != nil {
363364
return nil, err
364365
}
366+
if err := driverutil.ResolveVMType(y, filePath); err != nil {
367+
return nil, fmt.Errorf("failed to accept config for %q: %w", filePath, err)
368+
}
365369
if err := limayaml.Validate(y, true); err != nil {
366370
rejectedYAML := "lima.REJECTED.yaml"
367371
if writeErr := os.WriteFile(rejectedYAML, yBytes, 0o644); writeErr != nil {

cmd/limactl/template.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/sirupsen/logrus"
1414
"github.com/spf13/cobra"
1515

16+
"github.com/lima-vm/lima/v2/pkg/driverutil"
1617
"github.com/lima-vm/lima/v2/pkg/limatmpl"
1718
"github.com/lima-vm/lima/v2/pkg/limatype/dirnames"
1819
"github.com/lima-vm/lima/v2/pkg/limayaml"
@@ -87,6 +88,10 @@ func fillDefaults(ctx context.Context, tmpl *limatmpl.Template) error {
8788
if err == nil {
8889
tmpl.Bytes, err = limayaml.Marshal(tmpl.Config, false)
8990
}
91+
if err := driverutil.ResolveVMType(tmpl.Config, filePath); err != nil {
92+
logrus.Warnf("failed to resolve VM type for %q: %v", filePath, err)
93+
return nil
94+
}
9095
return err
9196
}
9297

@@ -246,6 +251,10 @@ func templateValidateAction(cmd *cobra.Command, args []string) error {
246251
if err != nil {
247252
return err
248253
}
254+
if err := driverutil.ResolveVMType(y, filePath); err != nil {
255+
logrus.Warnf("failed to resolve VM type for %q: %v", filePath, err)
256+
return nil
257+
}
249258
if err := limayaml.Validate(y, false); err != nil {
250259
return fmt.Errorf("failed to validate YAML file %q: %w", arg, err)
251260
}

hack/test-upgrade.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,11 @@ INFO "==========================================================================
9595
INFO "Installing the new Lima ${NEWVER}"
9696
install_lima "${NEWVER}"
9797

98+
INFO "Editing the instance to specify vm-type as qemu explicitly"
99+
limactl edit --vm-type=qemu "${LIMA_INSTANCE}"
100+
98101
INFO "Restarting the instance"
99-
limactl start --tty=false "${LIMA_INSTANCE}" || show_lima_log
102+
limactl start --tty=false --vm-type=qemu "${LIMA_INSTANCE}" || show_lima_log
100103
lima nerdctl info
101104

102105
INFO "Confirming that the host filesystem is still mounted"

pkg/cidata/cidata.go

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/sirupsen/logrus"
2525

2626
"github.com/lima-vm/lima/v2/pkg/debugutil"
27+
"github.com/lima-vm/lima/v2/pkg/driver"
2728
"github.com/lima-vm/lima/v2/pkg/instance/hostname"
2829
"github.com/lima-vm/lima/v2/pkg/iso9660util"
2930
"github.com/lima-vm/lima/v2/pkg/limatype"
@@ -137,14 +138,19 @@ func templateArgs(ctx context.Context, bootScripts bool, instDir, name string, i
137138
Containerd: Containerd{System: *instConfig.Containerd.System, User: *instConfig.Containerd.User, Archive: archive},
138139
SlirpNICName: networks.SlirpNICName,
139140

140-
RosettaEnabled: *instConfig.Rosetta.Enabled,
141-
RosettaBinFmt: *instConfig.Rosetta.BinFmt,
142-
VMType: *instConfig.VMType,
143-
VSockPort: vsockPort,
144-
VirtioPort: virtioPort,
145-
Plain: *instConfig.Plain,
146-
TimeZone: *instConfig.TimeZone,
147-
Param: instConfig.Param,
141+
VMType: *instConfig.VMType,
142+
VSockPort: vsockPort,
143+
VirtioPort: virtioPort,
144+
Plain: *instConfig.Plain,
145+
TimeZone: *instConfig.TimeZone,
146+
Param: instConfig.Param,
147+
}
148+
149+
if instConfig.VMOpts.VZ.Rosetta.Enabled != nil {
150+
args.RosettaEnabled = *instConfig.VMOpts.VZ.Rosetta.Enabled
151+
}
152+
if instConfig.VMOpts.VZ.Rosetta.BinFmt != nil {
153+
args.RosettaEnabled = *instConfig.VMOpts.VZ.Rosetta.BinFmt
148154
}
149155

150156
firstUsernetIndex := limayaml.FirstUsernetIndex(instConfig)
@@ -357,7 +363,7 @@ func GenerateCloudConfig(ctx context.Context, instDir, name string, instConfig *
357363
return os.WriteFile(filepath.Join(instDir, filenames.CloudConfig), config, 0o444)
358364
}
359365

360-
func GenerateISO9660(ctx context.Context, instDir, name string, instConfig *limatype.LimaYAML, udpDNSLocalPort, tcpDNSLocalPort int, guestAgentBinary, nerdctlArchive string, vsockPort int, virtioPort string) error {
366+
func GenerateISO9660(ctx context.Context, drv driver.Driver, instDir, name string, instConfig *limatype.LimaYAML, udpDNSLocalPort, tcpDNSLocalPort int, guestAgentBinary, nerdctlArchive string, vsockPort int, virtioPort string) error {
361367
args, err := templateArgs(ctx, true, instDir, name, instConfig, udpDNSLocalPort, tcpDNSLocalPort, vsockPort, virtioPort)
362368
if err != nil {
363369
return err
@@ -372,6 +378,18 @@ func GenerateISO9660(ctx context.Context, instDir, name string, instConfig *lima
372378
return err
373379
}
374380

381+
driverScripts, err := drv.BootScripts()
382+
if err != nil {
383+
return fmt.Errorf("failed to get boot scripts: %w", err)
384+
}
385+
386+
for filename, content := range driverScripts {
387+
layout = append(layout, iso9660util.Entry{
388+
Path: fmt.Sprintf("boot/%s", filename),
389+
Reader: strings.NewReader(string(content)),
390+
})
391+
}
392+
375393
for i, f := range instConfig.Provision {
376394
switch f.Mode {
377395
case limatype.ProvisionModeSystem, limatype.ProvisionModeUser, limatype.ProvisionModeDependency:

pkg/driver/driver.go

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ type Lifecycle interface {
1515
// Validate returns error if the current driver isn't support for given config
1616
Validate(_ context.Context) error
1717

18-
// Initialize is called on creating the instance for initialization.
18+
// Create is called on creating the instance for the first time.
1919
// (e.g., creating "vz-identifier" file)
2020
//
21-
// Initialize MUST return nil when it is called against an existing instance.
21+
// Create MUST return nil when it is called against an existing instance.
2222
//
23-
// Initialize does not create the disks.
24-
Initialize(_ context.Context) error
23+
// Create does not create the disks.
24+
Create(_ context.Context) error
2525

2626
// CreateDisk returns error if the current driver fails in creating disk
2727
CreateDisk(_ context.Context) error
@@ -34,6 +34,12 @@ type Lifecycle interface {
3434
// Stop will terminate the running vm instance.
3535
// It returns error if there are any errors during Stop
3636
Stop(_ context.Context) error
37+
38+
Delete(_ context.Context) error
39+
40+
InspectStatus(_ context.Context, inst *limatype.Instance) string
41+
42+
BootScripts() (map[string][]byte, error)
3743
}
3844

3945
// GUI defines GUI-related operations.
@@ -54,12 +60,6 @@ type SnapshotManager interface {
5460
ListSnapshots(ctx context.Context) (string, error)
5561
}
5662

57-
// Registration defines operations for registering and unregistering the driver instance.
58-
type Registration interface {
59-
Register(ctx context.Context) error
60-
Unregister(ctx context.Context) error
61-
}
62-
6363
// GuestAgent defines operations for the guest agent.
6464
type GuestAgent interface {
6565
// ForwardGuestAgent returns if the guest agent sock needs forwarding by host agent.
@@ -74,23 +74,33 @@ type Driver interface {
7474
Lifecycle
7575
GUI
7676
SnapshotManager
77-
Registration
7877
GuestAgent
7978

8079
Info() Info
8180

82-
// SetConfig sets the configuration for the instance.
81+
// Configure sets the configuration for the instance.
8382
Configure(inst *limatype.Instance) *ConfiguredDriver
83+
84+
AcceptConfig(cfg *limatype.LimaYAML, filePath string) error
85+
FillConfig(cfg *limatype.LimaYAML, filePath string) error
86+
87+
SSHAddress(ctx context.Context) (string, error)
8488
}
8589

8690
type ConfiguredDriver struct {
8791
Driver
8892
}
8993

9094
type Info struct {
91-
DriverName string `json:"driverName"`
92-
CanRunGUI bool `json:"canRunGui,omitempty"`
93-
VsockPort int `json:"vsockPort"`
94-
VirtioPort string `json:"virtioPort"`
95-
InstanceDir string `json:"instanceDir,omitempty"`
95+
DriverName string `json:"driverName"`
96+
CanRunGUI bool `json:"canRunGui,omitempty"`
97+
VsockPort int `json:"vsockPort"`
98+
VirtioPort string `json:"virtioPort"`
99+
InstanceDir string `json:"instanceDir,omitempty"`
100+
Features DriverFeatures `json:"features"`
101+
}
102+
103+
type DriverFeatures struct {
104+
DynamicSSHAddress bool `json:"dynamicSSHAddress"`
105+
SkipSocketForwarding bool `json:"skipSocketForwarding"`
96106
}

0 commit comments

Comments
 (0)