2018-02-05 21:05:59 +00:00
|
|
|
package oci // import "github.com/docker/docker/oci"
|
2016-11-17 00:18:43 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2021-12-01 15:08:03 +00:00
|
|
|
coci "github.com/containerd/containerd/oci"
|
2019-08-05 14:37:47 +00:00
|
|
|
specs "github.com/opencontainers/runtime-spec/specs-go"
|
2016-11-17 00:18:43 +00:00
|
|
|
)
|
|
|
|
|
2021-12-01 15:08:03 +00:00
|
|
|
func deviceCgroup(d *specs.LinuxDevice, permissions string) specs.LinuxDeviceCgroup {
|
2017-04-27 21:52:47 +00:00
|
|
|
return specs.LinuxDeviceCgroup{
|
2016-11-17 00:18:43 +00:00
|
|
|
Allow: true,
|
2021-12-01 15:08:03 +00:00
|
|
|
Type: d.Type,
|
2016-11-17 00:18:43 +00:00
|
|
|
Major: &d.Major,
|
|
|
|
Minor: &d.Minor,
|
2021-12-01 15:08:03 +00:00
|
|
|
Access: permissions,
|
2016-11-17 00:18:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DevicesFromPath computes a list of devices and device permissions from paths (pathOnHost and pathInContainer) and cgroup permissions.
|
2017-04-27 21:52:47 +00:00
|
|
|
func DevicesFromPath(pathOnHost, pathInContainer, cgroupPermissions string) (devs []specs.LinuxDevice, devPermissions []specs.LinuxDeviceCgroup, err error) {
|
2016-11-17 00:18:43 +00:00
|
|
|
resolvedPathOnHost := pathOnHost
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
resolvedPathOnHost = linkedPathOnHost
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-01 15:08:03 +00:00
|
|
|
device, err := coci.DeviceFromPath(resolvedPathOnHost)
|
2016-11-17 00:18:43 +00:00
|
|
|
// if there was no error, return the device
|
|
|
|
if err == nil {
|
|
|
|
device.Path = pathInContainer
|
2021-12-01 15:08:03 +00:00
|
|
|
return append(devs, *device), append(devPermissions, deviceCgroup(device, cgroupPermissions)), nil
|
2016-11-17 00:18:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// if the device is not a device node
|
|
|
|
// try to see if it's a directory holding many devices
|
2021-12-01 15:08:03 +00:00
|
|
|
if err == coci.ErrNotADevice {
|
2016-11-17 00:18:43 +00:00
|
|
|
// check if it is a directory
|
|
|
|
if src, e := os.Stat(resolvedPathOnHost); e == nil && src.IsDir() {
|
|
|
|
// mount the internal devices recursively
|
2019-08-28 17:29:19 +00:00
|
|
|
// TODO check if additional errors should be handled or logged
|
2022-10-09 15:21:04 +00:00
|
|
|
_ = filepath.WalkDir(resolvedPathOnHost, func(dpath string, f os.DirEntry, _ error) error {
|
2021-12-01 15:08:03 +00:00
|
|
|
childDevice, e := coci.DeviceFromPath(dpath)
|
2016-11-17 00:18:43 +00:00
|
|
|
if e != nil {
|
|
|
|
// ignore the device
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// add the device to userSpecified devices
|
|
|
|
childDevice.Path = strings.Replace(dpath, resolvedPathOnHost, pathInContainer, 1)
|
2021-12-01 15:08:03 +00:00
|
|
|
devs = append(devs, *childDevice)
|
|
|
|
devPermissions = append(devPermissions, deviceCgroup(childDevice, cgroupPermissions))
|
2016-11-17 00:18:43 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(devs) > 0 {
|
|
|
|
return devs, devPermissions, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return devs, devPermissions, fmt.Errorf("error gathering device information while adding custom device %q: %s", pathOnHost, err)
|
|
|
|
}
|