devices_linux.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package oci
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "strings"
  7. "github.com/opencontainers/runc/libcontainer/configs"
  8. "github.com/opencontainers/runc/libcontainer/devices"
  9. specs "github.com/opencontainers/runtime-spec/specs-go"
  10. )
  11. // Device transforms a libcontainer configs.Device to a specs.LinuxDevice object.
  12. func Device(d *configs.Device) specs.LinuxDevice {
  13. return specs.LinuxDevice{
  14. Type: string(d.Type),
  15. Path: d.Path,
  16. Major: d.Major,
  17. Minor: d.Minor,
  18. FileMode: fmPtr(int64(d.FileMode)),
  19. UID: u32Ptr(int64(d.Uid)),
  20. GID: u32Ptr(int64(d.Gid)),
  21. }
  22. }
  23. func deviceCgroup(d *configs.Device) specs.LinuxDeviceCgroup {
  24. t := string(d.Type)
  25. return specs.LinuxDeviceCgroup{
  26. Allow: true,
  27. Type: t,
  28. Major: &d.Major,
  29. Minor: &d.Minor,
  30. Access: d.Permissions,
  31. }
  32. }
  33. // DevicesFromPath computes a list of devices and device permissions from paths (pathOnHost and pathInContainer) and cgroup permissions.
  34. func DevicesFromPath(pathOnHost, pathInContainer, cgroupPermissions string) (devs []specs.LinuxDevice, devPermissions []specs.LinuxDeviceCgroup, err error) {
  35. resolvedPathOnHost := pathOnHost
  36. // check if it is a symbolic link
  37. if src, e := os.Lstat(pathOnHost); e == nil && src.Mode()&os.ModeSymlink == os.ModeSymlink {
  38. if linkedPathOnHost, e := filepath.EvalSymlinks(pathOnHost); e == nil {
  39. resolvedPathOnHost = linkedPathOnHost
  40. }
  41. }
  42. device, err := devices.DeviceFromPath(resolvedPathOnHost, cgroupPermissions)
  43. // if there was no error, return the device
  44. if err == nil {
  45. device.Path = pathInContainer
  46. return append(devs, Device(device)), append(devPermissions, deviceCgroup(device)), nil
  47. }
  48. // if the device is not a device node
  49. // try to see if it's a directory holding many devices
  50. if err == devices.ErrNotADevice {
  51. // check if it is a directory
  52. if src, e := os.Stat(resolvedPathOnHost); e == nil && src.IsDir() {
  53. // mount the internal devices recursively
  54. filepath.Walk(resolvedPathOnHost, func(dpath string, f os.FileInfo, e error) error {
  55. childDevice, e := devices.DeviceFromPath(dpath, cgroupPermissions)
  56. if e != nil {
  57. // ignore the device
  58. return nil
  59. }
  60. // add the device to userSpecified devices
  61. childDevice.Path = strings.Replace(dpath, resolvedPathOnHost, pathInContainer, 1)
  62. devs = append(devs, Device(childDevice))
  63. devPermissions = append(devPermissions, deviceCgroup(childDevice))
  64. return nil
  65. })
  66. }
  67. }
  68. if len(devs) > 0 {
  69. return devs, devPermissions, nil
  70. }
  71. return devs, devPermissions, fmt.Errorf("error gathering device information while adding custom device %q: %s", pathOnHost, err)
  72. }