Skip to content

Add {{.TempDir}} and {{.GlobalTempDir}} variables to host templates #3318

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions pkg/limayaml/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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}}`
Expand Down
10 changes: 5 additions & 5 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,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{
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion templates/_default/mounts.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
mounts:
- location: "~"
- location: "/tmp/lima"

- location: "{{.GlobalTempDir}}/lima"
mountPoint: /tmp/lima
writable: true
3 changes: 2 additions & 1 deletion templates/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading