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 <sunzitai_1832@bupt.edu.cn>
This commit is contained in:
stevenfryto 2025-07-07 05:43:01 +00:00 committed by Xuewei Niu
parent 294b2c1c10
commit 3c7a670129

View File

@ -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
}