Skip to content

Commit 67a3361

Browse files
committed
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 <jan.dubois@suse.com>
1 parent 3fea800 commit 67a3361

File tree

4 files changed

+23
-12
lines changed

4 files changed

+23
-12
lines changed

pkg/limayaml/defaults.go

+13-5
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,7 @@ func FillDefault(y, d, o *LimaYAML, filePath string, warn bool) {
738738
location := make(map[string]int)
739739
for _, mount := range slices.Concat(d.Mounts, y.Mounts, o.Mounts) {
740740
if out, err := executeHostTemplate(mount.Location, instDir, y.Param); err == nil {
741-
mount.Location = out.String()
741+
mount.Location = filepath.Clean(out.String())
742742
} else {
743743
logrus.WithError(err).Warnf("Couldn't process mount location %q as a template", mount.Location)
744744
}
@@ -955,14 +955,22 @@ func executeHostTemplate(format, instDir string, param map[string]string) (bytes
955955
tmpl, err := template.New("").Parse(format)
956956
if err == nil {
957957
limaHome, _ := dirnames.LimaDir()
958+
globalTempDir := "/tmp"
959+
if runtime.GOOS == "windows" {
960+
// On Windows the global temp directory "%SystemRoot%\Temp" (normally "C:\Windows\Temp")
961+
// is not writable by regular users.
962+
globalTempDir = os.TempDir()
963+
}
958964
data := map[string]any{
959965
"Dir": instDir,
960966
"Name": filepath.Base(instDir),
961967
// TODO: add hostname fields for the host and the guest
962-
"UID": currentUser.Uid,
963-
"User": currentUser.Username,
964-
"Home": userHomeDir,
965-
"Param": param,
968+
"UID": currentUser.Uid,
969+
"User": currentUser.Username,
970+
"Home": userHomeDir,
971+
"Param": param,
972+
"GlobalTempDir": globalTempDir,
973+
"TempDir": os.TempDir(),
966974

967975
"Instance": filepath.Base(instDir), // DEPRECATED, use `{{.Name}}`
968976
"LimaHome": limaHome, // DEPRECATED, use `{{.Dir}}` instead of `{{.LimaHome}}/{{.Instance}}`

pkg/limayaml/defaults_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"net"
99
"os"
10+
"path"
1011
"path/filepath"
1112
"runtime"
1213
"slices"
@@ -146,10 +147,9 @@ func TestFillDefault(t *testing.T) {
146147
},
147148
},
148149
Mounts: []Mount{
149-
// Location will be passed through localpathutil.Expand() which will normalize the name
150-
// (add a drive letter). So we must start with a valid local path to match it again later.
151-
{Location: filepath.Clean(t.TempDir())},
152-
{Location: "{{.Dir}}/{{.Param.ONE}}", MountPoint: ptr.Of("/mnt/{{.Param.ONE}}")},
150+
//nolint:usetesting // We need the OS temp directory name here; it is not used to create temp files for testing
151+
{Location: filepath.Clean(os.TempDir())},
152+
{Location: filepath.Clean("{{.Dir}}/{{.Param.ONE}}"), MountPoint: ptr.Of("/mnt/{{.Param.ONE}}")},
153153
},
154154
MountType: ptr.Of(NINEP),
155155
Provision: []Provision{
@@ -240,7 +240,7 @@ func TestFillDefault(t *testing.T) {
240240
expect.Mounts[0].Virtiofs.QueueSize = nil
241241
// Only missing Mounts field is Writable, and the default value is also the null value: false
242242
expect.Mounts[1].Location = filepath.Join(instDir, y.Param["ONE"])
243-
expect.Mounts[1].MountPoint = ptr.Of(fmt.Sprintf("/mnt/%s", y.Param["ONE"]))
243+
expect.Mounts[1].MountPoint = ptr.Of(path.Join("/mnt", y.Param["ONE"]))
244244
expect.Mounts[1].Writable = ptr.Of(false)
245245
expect.Mounts[1].SSHFS.Cache = ptr.Of(true)
246246
expect.Mounts[1].SSHFS.FollowSymlinks = ptr.Of(false)

templates/_default/mounts.yaml

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
mounts:
22
- location: "~"
3-
- location: "/tmp/lima"
3+
4+
- location: "{{.GlobalTempDir}}/lima"
5+
mountPoint: /tmp/lima
46
writable: true

templates/default.yaml

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ memory: null
3434
disk: null
3535

3636
# Expose host directories to the guest, the mount point might be accessible from all UIDs in the guest
37-
# "location" can use these template variables: {{.Home}}, {{.Dir}}, {{.Name}}, {{.UID}}, {{.User}}, and {{.Param.Key}}.
37+
# "location" can use these template variables: {{.Home}}, {{.Dir}}, {{.Name}}, {{.UID}}, {{.User}}, {{.Param.Key}},
38+
# {{.GlobalTempDir}}, and {{.TempDir}}. The global temp dir is always "/tmp" on Unix.
3839
# "mountPoint" can use these template variables: {{.Home}}, {{.Name}}, {{.Hostname}}, {{.UID}}, {{.User}}, and {{.Param.Key}}.
3940
# 🟢 Builtin default: [] (Mount nothing)
4041
# 🔵 This file: Mount the home as read-only, /tmp/lima as writable (inherited via the `base` mechanism later in this file)

0 commit comments

Comments
 (0)