Skip to content

Commit

Permalink
Allow using default for the default mounts
Browse files Browse the repository at this point in the history
Signed-off-by: Anders F Björklund <[email protected]>
  • Loading branch information
afbjorklund committed Oct 7, 2024
1 parent ff25fd6 commit 3fda61d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
23 changes: 23 additions & 0 deletions pkg/limayaml/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,19 @@ func defaultContainerdArchives() []File {
return containerd.Archives
}

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) })
Expand Down Expand Up @@ -647,6 +660,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 {
Expand Down
1 change: 1 addition & 0 deletions pkg/limayaml/limayaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,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"`
Expand Down
10 changes: 10 additions & 0 deletions pkg/limayaml/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ import (
"github.com/sirupsen/logrus"
)

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 {
Expand All @@ -32,6 +41,7 @@ func unmarshalImage(dst *Image, b []byte) error {
}

var customMarshalers = []yaml.DecodeOption{
yaml.CustomUnmarshaler[Mount](unmarshalMount),
yaml.CustomUnmarshaler[Disk](unmarshalDisk),
yaml.CustomUnmarshaler[Image](unmarshalImage),
}
Expand Down
7 changes: 7 additions & 0 deletions pkg/limayaml/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,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)
Expand Down

0 comments on commit 3fda61d

Please sign in to comment.