Merge pull request #51624 from AkihiroSuda/fix-51601

cdi: read XDG_CONFIG_HOME/cdi and XDG_RUNTIME_DIR/cdi for rootless
This commit is contained in:
Sebastiaan van Stijn
2025-12-17 16:27:13 +01:00
committed by GitHub
4 changed files with 101 additions and 19 deletions

View File

@@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"os"
"path/filepath"
"github.com/containerd/log"
"github.com/moby/moby/api/types/system"
@@ -22,6 +23,14 @@ type cdiHandler struct {
// The driver injects CDI devices into an incoming OCI spec and is called for DeviceRequests associated with CDI devices.
// If the list of CDI spec directories is empty, the driver is not registered.
func RegisterCDIDriver(cdiSpecDirs ...string) *cdi.Cache {
for i, dir := range cdiSpecDirs {
if _, err := os.Stat(dir); !errors.Is(err, os.ErrNotExist) {
cdiSpecDirs[i], err = filepath.EvalSymlinks(dir)
if err != nil {
log.L.WithField("dir", dir).WithError(err).Warn("Failed to evaluate symlinks for CDI spec directory")
}
}
}
driver, cache := newCDIDeviceDriver(cdiSpecDirs...)
registerDeviceDriver("cdi", driver)
return cache

View File

@@ -57,6 +57,7 @@ import (
"github.com/moby/moby/v2/pkg/homedir"
"github.com/moby/moby/v2/pkg/pidfile"
"github.com/moby/moby/v2/pkg/plugingetter"
"github.com/moby/sys/userns"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/pflag"
@@ -666,7 +667,35 @@ func loadDaemonCliConfig(opts *daemonOptions) (*config.Config, error) {
if conf.CDISpecDirs == nil {
// If the CDISpecDirs is not set at this stage, we set it to the default.
conf.CDISpecDirs = append([]string(nil), cdi.DefaultSpecDirs...)
} else if len(conf.CDISpecDirs) == 1 && conf.CDISpecDirs[0] == "" {
if rootless.RunningWithRootlessKit() {
// In rootless mode, we add the user-specific CDI spec directory.
xch, err := homedir.GetConfigHome()
if err != nil {
return nil, err
}
xrd, err := homedir.GetRuntimeDir()
if err != nil {
return nil, err
}
conf.CDISpecDirs = append(conf.CDISpecDirs, filepath.Join(xch, "cdi"), filepath.Join(xrd, "cdi"))
}
}
// Filter out CDI spec directories that are not readable, and log appropriately
var cdiSpecDirs []string
for _, dir := range conf.CDISpecDirs {
// Non-existing directories are not filtered out here, as CDI spec directories are allowed to not exist.
if _, err := os.ReadDir(dir); err == nil || errors.Is(err, os.ErrNotExist) {
cdiSpecDirs = append(cdiSpecDirs, dir)
} else {
logLevel := log.ErrorLevel
if userns.RunningInUserNS() && errors.Is(err, os.ErrPermission) {
logLevel = log.DebugLevel
}
log.L.WithField("dir", dir).WithError(err).Log(logLevel, "CDI spec directory cannot be accessed, skipping")
}
}
conf.CDISpecDirs = cdiSpecDirs
if len(conf.CDISpecDirs) == 1 && conf.CDISpecDirs[0] == "" {
// If CDISpecDirs is set to an empty string, we clear it to ensure that CDI is disabled.
conf.CDISpecDirs = nil
}