From 3c7a67012938d8a17af7d9aef5f4548f314a9da5 Mon Sep 17 00:00:00 2001 From: stevenfryto Date: Mon, 7 Jul 2025 05:43:01 +0000 Subject: [PATCH] runtime: Fix rootlessDir not correctly set in rootless VMM mode Previously, the rootlessDir variable in `src/runtime/virtcontainers/pkg/rootless.go` was initialized at package load time using `os.Getenv("XDG_RUNTIME_DIR")`. However, in rootless VMM mode, the correct value of $XDG_RUNTIME_DIR is set later during runtime using os.Setenv(), so rootlessDir remained empty. This patch defers the initialization of rootlessDir until the first call to `GetRootlessDir()`, ensuring it always reflects the current environment value of $XDG_RUNTIME_DIR. Fixes: #11526 Signed-off-by: stevenfryto --- src/runtime/virtcontainers/pkg/rootless/rootless.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/runtime/virtcontainers/pkg/rootless/rootless.go b/src/runtime/virtcontainers/pkg/rootless/rootless.go index f3152c02a9..ef2f6d0cca 100644 --- a/src/runtime/virtcontainers/pkg/rootless/rootless.go +++ b/src/runtime/virtcontainers/pkg/rootless/rootless.go @@ -38,7 +38,10 @@ var ( // XDG_RUNTIME_DIR defines the base directory relative to // which user-specific non-essential runtime files are stored. - rootlessDir = os.Getenv("XDG_RUNTIME_DIR") + rootlessDir string + + // Used for lazy initialization of rootlessDir + rootlessDirOnce sync.Once rootlessLog = logrus.WithFields(logrus.Fields{ "source": "rootless", @@ -82,5 +85,9 @@ func isRootlessFunc() bool { // GetRootlessDir returns the path to the location for rootless // container and sandbox storage func GetRootlessDir() string { + rootlessDirOnce.Do(func() { + rootlessDir = os.Getenv("XDG_RUNTIME_DIR") + rootlessLog.WithField("rootlessDir", rootlessDir).Debug("initialized rootlessDir") + }) return rootlessDir }