From 67a336140b4bf1076895d300ebee5b6240a6cbda Mon Sep 17 00:00:00 2001 From: Jan Dubois Date: Wed, 30 Apr 2025 17:08:26 -0700 Subject: [PATCH] Add {{.Temp}} variable to host templates Allows writing the temp mount as mounts: - location: "{{.Temp}}/lima" mountPoint: /tmp/lima Note that on macOS this would use $TMPDIR and not /tmp, so would be a change in behaviour. It would not affect existing instances though. Signed-off-by: Jan Dubois --- pkg/limayaml/defaults.go | 18 +++++++++++++----- pkg/limayaml/defaults_test.go | 10 +++++----- templates/_default/mounts.yaml | 4 +++- templates/default.yaml | 3 ++- 4 files changed, 23 insertions(+), 12 deletions(-) diff --git a/pkg/limayaml/defaults.go b/pkg/limayaml/defaults.go index 7b55a1edbed..308471a8fe3 100644 --- a/pkg/limayaml/defaults.go +++ b/pkg/limayaml/defaults.go @@ -738,7 +738,7 @@ func FillDefault(y, d, o *LimaYAML, filePath string, warn bool) { location := make(map[string]int) for _, mount := range slices.Concat(d.Mounts, y.Mounts, o.Mounts) { if out, err := executeHostTemplate(mount.Location, instDir, y.Param); err == nil { - mount.Location = out.String() + mount.Location = filepath.Clean(out.String()) } else { logrus.WithError(err).Warnf("Couldn't process mount location %q as a template", mount.Location) } @@ -955,14 +955,22 @@ func executeHostTemplate(format, instDir string, param map[string]string) (bytes tmpl, err := template.New("").Parse(format) if err == nil { limaHome, _ := dirnames.LimaDir() + globalTempDir := "/tmp" + if runtime.GOOS == "windows" { + // On Windows the global temp directory "%SystemRoot%\Temp" (normally "C:\Windows\Temp") + // is not writable by regular users. + globalTempDir = os.TempDir() + } data := map[string]any{ "Dir": instDir, "Name": filepath.Base(instDir), // TODO: add hostname fields for the host and the guest - "UID": currentUser.Uid, - "User": currentUser.Username, - "Home": userHomeDir, - "Param": param, + "UID": currentUser.Uid, + "User": currentUser.Username, + "Home": userHomeDir, + "Param": param, + "GlobalTempDir": globalTempDir, + "TempDir": os.TempDir(), "Instance": filepath.Base(instDir), // DEPRECATED, use `{{.Name}}` "LimaHome": limaHome, // DEPRECATED, use `{{.Dir}}` instead of `{{.LimaHome}}/{{.Instance}}` diff --git a/pkg/limayaml/defaults_test.go b/pkg/limayaml/defaults_test.go index 9b08ed43b1f..b74a6d7266c 100644 --- a/pkg/limayaml/defaults_test.go +++ b/pkg/limayaml/defaults_test.go @@ -7,6 +7,7 @@ import ( "fmt" "net" "os" + "path" "path/filepath" "runtime" "slices" @@ -146,10 +147,9 @@ func TestFillDefault(t *testing.T) { }, }, Mounts: []Mount{ - // Location will be passed through localpathutil.Expand() which will normalize the name - // (add a drive letter). So we must start with a valid local path to match it again later. - {Location: filepath.Clean(t.TempDir())}, - {Location: "{{.Dir}}/{{.Param.ONE}}", MountPoint: ptr.Of("/mnt/{{.Param.ONE}}")}, + //nolint:usetesting // We need the OS temp directory name here; it is not used to create temp files for testing + {Location: filepath.Clean(os.TempDir())}, + {Location: filepath.Clean("{{.Dir}}/{{.Param.ONE}}"), MountPoint: ptr.Of("/mnt/{{.Param.ONE}}")}, }, MountType: ptr.Of(NINEP), Provision: []Provision{ @@ -240,7 +240,7 @@ func TestFillDefault(t *testing.T) { expect.Mounts[0].Virtiofs.QueueSize = nil // Only missing Mounts field is Writable, and the default value is also the null value: false expect.Mounts[1].Location = filepath.Join(instDir, y.Param["ONE"]) - expect.Mounts[1].MountPoint = ptr.Of(fmt.Sprintf("/mnt/%s", y.Param["ONE"])) + expect.Mounts[1].MountPoint = ptr.Of(path.Join("/mnt", y.Param["ONE"])) expect.Mounts[1].Writable = ptr.Of(false) expect.Mounts[1].SSHFS.Cache = ptr.Of(true) expect.Mounts[1].SSHFS.FollowSymlinks = ptr.Of(false) diff --git a/templates/_default/mounts.yaml b/templates/_default/mounts.yaml index a695c293287..caaf004edda 100644 --- a/templates/_default/mounts.yaml +++ b/templates/_default/mounts.yaml @@ -1,4 +1,6 @@ mounts: - location: "~" -- location: "/tmp/lima" + +- location: "{{.GlobalTempDir}}/lima" + mountPoint: /tmp/lima writable: true diff --git a/templates/default.yaml b/templates/default.yaml index 6dc3a6fd163..ad84a4c719a 100644 --- a/templates/default.yaml +++ b/templates/default.yaml @@ -34,7 +34,8 @@ memory: null disk: null # Expose host directories to the guest, the mount point might be accessible from all UIDs in the guest -# "location" can use these template variables: {{.Home}}, {{.Dir}}, {{.Name}}, {{.UID}}, {{.User}}, and {{.Param.Key}}. +# "location" can use these template variables: {{.Home}}, {{.Dir}}, {{.Name}}, {{.UID}}, {{.User}}, {{.Param.Key}}, +# {{.GlobalTempDir}}, and {{.TempDir}}. The global temp dir is always "/tmp" on Unix. # "mountPoint" can use these template variables: {{.Home}}, {{.Name}}, {{.Hostname}}, {{.UID}}, {{.User}}, and {{.Param.Key}}. # 🟢 Builtin default: [] (Mount nothing) # 🔵 This file: Mount the home as read-only, /tmp/lima as writable (inherited via the `base` mechanism later in this file)