From f517ff6d8006e19525adae1105f91012d022332d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20F=20Bj=C3=B6rklund?= Date: Sun, 16 Jun 2024 13:56:37 +0200 Subject: [PATCH] Allow using default for the default mounts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Anders F Björklund --- pkg/limayaml/defaults.go | 23 +++++++++++++++++++++++ pkg/limayaml/limayaml.go | 1 + pkg/limayaml/load.go | 21 +++++++++++++++++++++ pkg/limayaml/validate.go | 7 +++++++ 4 files changed, 52 insertions(+) diff --git a/pkg/limayaml/defaults.go b/pkg/limayaml/defaults.go index 858c84bd9ef..c0143c8ac4d 100644 --- a/pkg/limayaml/defaults.go +++ b/pkg/limayaml/defaults.go @@ -91,6 +91,19 @@ func defaultContainerdArchives() []File { } } +func defaultMounts() []Mount { + return []Mount{ + { + Location: "~", + Writable: ptr.Of(false), + }, + { + Location: "/tmp/lima", + Writable: ptr.Of(true), + }, + } +} + // FirstUsernetIndex gets the index of first usernet network under l.Network[]. Returns -1 if no usernet network found. func FirstUsernetIndex(l *LimaYAML) int { return slices.IndexFunc(l.Networks, func(network Network) bool { return networks.IsUsernet(network.Lima) }) @@ -640,6 +653,16 @@ func FillDefault(y, d, o *LimaYAML, filePath string) { } y.Mounts = mounts + mounts = []Mount{} + for _, mount := range y.Mounts { + if mount.Name == "default" { + mounts = append(mounts, defaultMounts()...) + continue + } + mounts = append(mounts, mount) + } + y.Mounts = mounts + for i := range y.Mounts { mount := &y.Mounts[i] if mount.SSHFS.Cache == nil { diff --git a/pkg/limayaml/limayaml.go b/pkg/limayaml/limayaml.go index 7956e53bb56..8b6ed16b53b 100644 --- a/pkg/limayaml/limayaml.go +++ b/pkg/limayaml/limayaml.go @@ -112,6 +112,7 @@ type Disk struct { } type Mount struct { + Name string `yaml:"name,omitempty" json:"name,omitempty"` Location string `yaml:"location" json:"location"` // REQUIRED MountPoint string `yaml:"mountPoint,omitempty" json:"mountPoint,omitempty"` Writable *bool `yaml:"writable,omitempty" json:"writable,omitempty"` diff --git a/pkg/limayaml/load.go b/pkg/limayaml/load.go index 43ad9427d7f..36c4bbd07f3 100644 --- a/pkg/limayaml/load.go +++ b/pkg/limayaml/load.go @@ -13,6 +13,15 @@ import ( yamlv3 "gopkg.in/yaml.v3" ) +func unmarshalMount(dst *Mount, b []byte) error { + var s string + if err := yaml.Unmarshal(b, &s); err == nil { + *dst = Mount{Name: s} + return nil + } + return yaml.Unmarshal(b, dst) +} + func unmarshalDisk(dst *Disk, b []byte) error { var s string if err := yaml.Unmarshal(b, &s); err == nil { @@ -32,10 +41,22 @@ func unmarshalImage(dst *Image, b []byte) error { } var customMarshalers = []yaml.DecodeOption{ + yaml.CustomUnmarshaler[Mount](unmarshalMount), yaml.CustomUnmarshaler[Disk](unmarshalDisk), yaml.CustomUnmarshaler[Image](unmarshalImage), } +func (d *Mount) UnmarshalYAML(value *yamlv3.Node) error { + var v interface{} + if err := value.Decode(&v); err != nil { + return err + } + if s, ok := v.(string); ok { + *d = Mount{Name: s} + } + return nil +} + func (d *Disk) UnmarshalYAML(value *yamlv3.Node) error { var v interface{} if err := value.Decode(&v); err != nil { diff --git a/pkg/limayaml/validate.go b/pkg/limayaml/validate.go index 0ee4559f6d5..84de0a154eb 100644 --- a/pkg/limayaml/validate.go +++ b/pkg/limayaml/validate.go @@ -134,6 +134,13 @@ func Validate(y *LimaYAML, warn bool) error { reservedHome := fmt.Sprintf("/home/%s.linux", u.Username) for i, f := range y.Mounts { + if f.Name != "" { + if f.Name != "default" { + return fmt.Errorf("field `mounts[%d].name` refers to an unknown name: %q", + i, f.Name) + } + continue + } if !filepath.IsAbs(f.Location) && !strings.HasPrefix(f.Location, "~") { return fmt.Errorf("field `mounts[%d].location` must be an absolute path, got %q", i, f.Location)