specgen: support CDI devices from containers.conf
Closes: https://github.com/containers/podman/issues/16232 Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
This commit is contained in:
		
							parent
							
								
									7eb11e7bb3
								
							
						
					
					
						commit
						5d26628df6
					
				|  | @ -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
 | ||||
|  |  | |||
|  | @ -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() { | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue