Use XDG_DATA_DIRS instead of hardcoding /usr/share

When running nvidia-ctk on a system that uses a custom XDG_DATA_DIRS
environment variable value, the configuration files for `glvnd`,
`vulkan`, and `egl` fail to get passed through from the host to the
container. Reading from XDG_DATA_DIRS instead of hardcoding the default
value allows for finding said files so they can be mounted in the
container.

Signed-off-by: Jared Baur <jaredbaur@fastmail.com>
Signed-off-by: Evan Lezar <elezar@nvidia.com>
This commit is contained in:
Jared Baur 2024-03-23 14:41:21 -07:00 committed by Evan Lezar
parent 29c0f82ed2
commit 5788e622f4

View File

@ -17,6 +17,7 @@
package root
import (
"os"
"path/filepath"
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
@ -59,18 +60,24 @@ func (r *Driver) Libraries() lookup.Locator {
// If configSearchPaths is specified, these paths are used as absolute paths,
// otherwise, /etc and /usr/share are searched.
func (r *Driver) Configs() lookup.Locator {
searchRoot := r.Root
searchPaths := []string{"/etc", "/usr/share"}
if len(r.configSearchPaths) > 0 {
searchRoot = "/"
searchPaths = normalizeSearchPaths(r.configSearchPaths...)
return lookup.NewFileLocator(r.configSearchOptions()...)
}
return lookup.NewFileLocator(
func (r *Driver) configSearchOptions() []lookup.Option {
if len(r.configSearchPaths) > 0 {
return []lookup.Option{
lookup.WithLogger(r.logger),
lookup.WithRoot(searchRoot),
lookup.WithRoot("/"),
lookup.WithSearchPaths(normalizeSearchPaths(r.configSearchPaths...)...),
}
}
searchPaths := []string{"/etc"}
searchPaths = append(searchPaths, xdgDataDirs()...)
return []lookup.Option{
lookup.WithLogger(r.logger),
lookup.WithRoot(r.Root),
lookup.WithSearchPaths(searchPaths...),
)
}
}
// normalizeSearchPaths takes a list of paths and normalized these.
@ -85,3 +92,13 @@ func normalizeSearchPaths(paths ...string) []string {
}
return normalized
}
// xdgDataDirs finds the paths as specified in the environment variable XDG_DATA_DIRS.
// See https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html.
func xdgDataDirs() []string {
if dirs, exists := os.LookupEnv("XDG_DATA_DIRS"); exists && dirs != "" {
return normalizeSearchPaths(dirs)
}
return []string{"/usr/local/share", "/usr/share"}
}