From 3b0be33a469c2fb44e0da5e6ea839c10a809336c Mon Sep 17 00:00:00 2001 From: Venky Natham Date: Thu, 9 Dec 2021 15:01:24 -0800 Subject: [PATCH] Add support for * for device name in --device This takes care of the [issue](https://github.com/docker/cli/issues/2968) from the server side Signed-off-by: Venky Natham --- oci/devices_linux.go | 20 +++++++++++++++++++ .../containerd/containerd/oci/utils_unix.go | 20 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/oci/devices_linux.go b/oci/devices_linux.go index ca1c4886b9678..ae8bd6023d380 100644 --- a/oci/devices_linux.go +++ b/oci/devices_linux.go @@ -38,6 +38,26 @@ func deviceCgroup(d *devices.Device) specs.LinuxDeviceCgroup { func DevicesFromPath(pathOnHost, pathInContainer, cgroupPermissions string) (devs []specs.LinuxDevice, devPermissions []specs.LinuxDeviceCgroup, err error) { resolvedPathOnHost := pathOnHost + // Only wildcard * is supported + if strings.HasSuffix(resolvedPathOnHost, "*") { + devicePaths, _ := filepath.Glob(resolvedPathOnHost) + var err error + var dev *devices.Device + for _, devicePath := range devicePaths { + dev, err = devices.DeviceFromPath(devicePath, cgroupPermissions) + if err != nil { + return nil, nil, fmt.Errorf("no device %q %s", devicePath, err) + } + devs = append(devs, Device(dev)) + devPermissions = append(devPermissions, deviceCgroup(dev)) + } + if len(devs) > 0 { + return devs, devPermissions, nil + } else { + return devs, devPermissions, fmt.Errorf("error gathering device information while adding custom device %q: %s", pathOnHost, err) + } + } + // check if it is a symbolic link if src, e := os.Lstat(pathOnHost); e == nil && src.Mode()&os.ModeSymlink == os.ModeSymlink { if linkedPathOnHost, e := filepath.EvalSymlinks(pathOnHost); e == nil { diff --git a/vendor/github.com/containerd/containerd/oci/utils_unix.go b/vendor/github.com/containerd/containerd/oci/utils_unix.go index 108cacf5b8bfd..b44a79fd2f37c 100644 --- a/vendor/github.com/containerd/containerd/oci/utils_unix.go +++ b/vendor/github.com/containerd/containerd/oci/utils_unix.go @@ -1,3 +1,4 @@ +//go:build !windows // +build !windows /* @@ -22,6 +23,7 @@ import ( "io/ioutil" "os" "path/filepath" + "strings" specs "github.com/opencontainers/runtime-spec/specs-go" "github.com/pkg/errors" @@ -36,6 +38,24 @@ func HostDevices() ([]specs.LinuxDevice, error) { } func getDevices(path, containerPath string) ([]specs.LinuxDevice, error) { + // Only wildcard * is supported + if strings.HasSuffix(path, "*") { + if containerPath != "" { + return nil, errors.New("Wildcard device should not have container path") + } + var out []specs.LinuxDevice + devicePaths, _ := filepath.Glob(path) + var err error + var dev *specs.LinuxDevice + for _, devicePath := range devicePaths { + dev, err = deviceFromPath(devicePath) + if err != nil { + return nil, errors.Wrap(err, "error getting device from path") + } + out = append(out, *dev) + } + return out, err + } stat, err := os.Stat(path) if err != nil { return nil, errors.Wrap(err, "error stating device path")