Skip to content

Commit

Permalink
Add {{.Temp}} variable to host templates
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
jandubois committed Mar 8, 2025
1 parent 88f350b commit 1f6fd20
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
3 changes: 2 additions & 1 deletion pkg/limayaml/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,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)
}
Expand Down Expand Up @@ -927,6 +927,7 @@ func executeHostTemplate(format, instDir string, param map[string]string) (bytes
"User": currentUser.Username,
"Home": userHomeDir,
"Param": param,
"Temp": os.TempDir(),

"Instance": filepath.Base(instDir), // DEPRECATED, use `{{.Name}}`
"LimaHome": limaHome, // DEPRECATED, use `{{.Dir}}` instead of `{{.LimaHome}}/{{.Instance}}`
Expand Down
13 changes: 7 additions & 6 deletions pkg/limayaml/defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"net"
"os"
"path"
"path/filepath"
"runtime"
"slices"
Expand Down Expand Up @@ -146,8 +147,8 @@ func TestFillDefault(t *testing.T) {
},
},
Mounts: []Mount{
{Location: "/tmp"},
{Location: "{{.Dir}}/{{.Param.ONE}}", MountPoint: ptr.Of("/mnt/{{.Param.ONE}}")},
{Location: filepath.Clean(os.TempDir())},
{Location: filepath.Clean("{{.Dir}}/{{.Param.ONE}}"), MountPoint: ptr.Of("/mnt/{{.Param.ONE}}")},
},
MountType: ptr.Of(NINEP),
Provision: []Provision{
Expand Down Expand Up @@ -231,8 +232,8 @@ func TestFillDefault(t *testing.T) {
expect.Mounts[0].NineP.Cache = ptr.Of(Default9pCacheForRO)
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 = fmt.Sprintf("%s/%s", instDir, y.Param["ONE"])
expect.Mounts[1].MountPoint = ptr.Of(fmt.Sprintf("/mnt/%s", y.Param["ONE"]))
expect.Mounts[1].Location = filepath.Join(instDir, 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)
Expand Down Expand Up @@ -385,7 +386,7 @@ func TestFillDefault(t *testing.T) {

Mounts: []Mount{
{
Location: "/var/log",
Location: filepath.Clean("/var/log"),
Writable: ptr.Of(false),
},
},
Expand Down Expand Up @@ -593,7 +594,7 @@ func TestFillDefault(t *testing.T) {

Mounts: []Mount{
{
Location: "/var/log",
Location: filepath.Clean("/var/log"),
Writable: ptr.Of(true),
SSHFS: SSHFS{
Cache: ptr.Of(false),
Expand Down
3 changes: 2 additions & 1 deletion templates/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ mounts:
# See https://www.kernel.org/doc/Documentation/filesystems/9p.txt
# 🟢 Builtin default: "fscache" for non-writable mounts, "mmap" for writable mounts
cache: null
- location: "/tmp/lima"
- location: "{{.Temp}}/lima"
mountPoint: /tmp/lima
# 🟢 Builtin default: false
# 🔵 This file: true (only for "/tmp/lima")
writable: true
Expand Down

0 comments on commit 1f6fd20

Please sign in to comment.