Skip to content

Commit

Permalink
specgen: support CDI devices from containers.conf
Browse files Browse the repository at this point in the history
Closes: #16232

Signed-off-by: Giuseppe Scrivano <[email protected]>
  • Loading branch information
giuseppe committed Nov 25, 2022
1 parent 7eb11e7 commit 5d26628
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
14 changes: 14 additions & 0 deletions pkg/specgen/generate/config_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
22 changes: 21 additions & 1 deletion test/e2e/run_device_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
package integration

import (
"fmt"
"os"
"os/exec"
"path/filepath"

. "github.com/containers/podman/v4/test/utils"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "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
Expand All @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 5d26628

Please sign in to comment.