diff --git a/pkg/specgen/generate/config_linux.go b/pkg/specgen/generate/config_linux.go index a469661611..965072a0a8 100644 --- a/pkg/specgen/generate/config_linux.go +++ b/pkg/specgen/generate/config_linux.go @@ -8,6 +8,7 @@ import ( "path/filepath" "strings" + "github.com/container-orchestrated-devices/container-device-interface/pkg/cdi" "github.com/containers/podman/v4/libpod/define" "github.com/containers/podman/v4/pkg/rootless" "github.com/containers/podman/v4/pkg/util" @@ -19,6 +20,19 @@ import ( // DevicesFromPath computes a list of devices func DevicesFromPath(g *generate.Generator, devicePath string) error { + if isCDIDevice(devicePath) { + registry := cdi.GetRegistry( + cdi.WithAutoRefresh(false), + ) + if err := registry.Refresh(); err != nil { + logrus.Debugf("The following error was triggered when refreshing the CDI registry: %v", err) + } + _, err := registry.InjectDevices(g.Config, devicePath) + if err != nil { + return fmt.Errorf("setting up CDI devices: %w", err) + } + return nil + } devs := strings.Split(devicePath, ":") resolvedDevicePath := devs[0] // check if it is a symbolic link diff --git a/test/e2e/run_device_test.go b/test/e2e/run_device_test.go index c46afdaca0..b0ae891d7d 100644 --- a/test/e2e/run_device_test.go +++ b/test/e2e/run_device_test.go @@ -1,8 +1,10 @@ package integration import ( + "fmt" "os" "os/exec" + "path/filepath" . "github.com/containers/podman/v4/test/utils" . "github.com/onsi/ginkgo" @@ -10,6 +12,19 @@ import ( . "github.com/onsi/gomega/gexec" ) +func createContainersConfFileWithDevices(pTest *PodmanTestIntegration, devices string) { + configPath := filepath.Join(pTest.TempDir, "containers.conf") + containersConf := []byte(fmt.Sprintf("[containers]\ndevices = [%s]\n", devices)) + err := os.WriteFile(configPath, containersConf, os.ModePerm) + Expect(err).To(BeNil()) + + // Set custom containers.conf file + os.Setenv("CONTAINERS_CONF", configPath) + if IsRemote() { + pTest.RestartRemoteService() + } +} + var _ = Describe("Podman run device", func() { var ( tempdir string @@ -30,7 +45,7 @@ var _ = Describe("Podman run device", func() { podmanTest.Cleanup() f := CurrentGinkgoTestDescription() processTestResult(f) - + os.Unsetenv("CONTAINERS_CONF") }) It("podman run bad device test", func() { @@ -116,6 +131,11 @@ var _ = Describe("Podman run device", func() { session := podmanTest.Podman([]string{"run", "-q", "--security-opt", "label=disable", "--device", "vendor.com/device=myKmsg", ALPINE, "test", "-c", "/dev/kmsg1"}) session.WaitWithDefaultTimeout() Expect(session).Should(Exit(0)) + + createContainersConfFileWithDevices(podmanTest, "\"vendor.com/device=myKmsg\"") + session = podmanTest.Podman([]string{"run", "-q", "--security-opt", "label=disable", ALPINE, "test", "-c", "/dev/kmsg1"}) + session.WaitWithDefaultTimeout() + Expect(session).Should(Exit(0)) }) It("podman run --gpus noop", func() {