spec_opts_linux.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. Copyright The containerd Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package oci
  14. import (
  15. "context"
  16. "github.com/containerd/containerd/containers"
  17. "github.com/containerd/containerd/pkg/cap"
  18. specs "github.com/opencontainers/runtime-spec/specs-go"
  19. )
  20. // WithHostDevices adds all the hosts device nodes to the container's spec
  21. func WithHostDevices(_ context.Context, _ Client, _ *containers.Container, s *Spec) error {
  22. setLinux(s)
  23. devs, err := HostDevices()
  24. if err != nil {
  25. return err
  26. }
  27. s.Linux.Devices = append(s.Linux.Devices, devs...)
  28. return nil
  29. }
  30. // WithDevices recursively adds devices from the passed in path and associated cgroup rules for that device.
  31. // If devicePath is a dir it traverses the dir to add all devices in that dir.
  32. // If devicePath is not a dir, it attempts to add the single device.
  33. // If containerPath is not set then the device path is used for the container path.
  34. func WithDevices(devicePath, containerPath, permissions string) SpecOpts {
  35. return func(_ context.Context, _ Client, _ *containers.Container, s *Spec) error {
  36. devs, err := getDevices(devicePath, containerPath)
  37. if err != nil {
  38. return err
  39. }
  40. for i := range devs {
  41. s.Linux.Devices = append(s.Linux.Devices, devs[i])
  42. s.Linux.Resources.Devices = append(s.Linux.Resources.Devices, specs.LinuxDeviceCgroup{
  43. Allow: true,
  44. Type: devs[i].Type,
  45. Major: &devs[i].Major,
  46. Minor: &devs[i].Minor,
  47. Access: permissions,
  48. })
  49. }
  50. return nil
  51. }
  52. }
  53. // WithAllCurrentCapabilities propagates the effective capabilities of the caller process to the container process.
  54. // The capability set may differ from WithAllKnownCapabilities when running in a container.
  55. var WithAllCurrentCapabilities = func(ctx context.Context, client Client, c *containers.Container, s *Spec) error {
  56. caps, err := cap.Current()
  57. if err != nil {
  58. return err
  59. }
  60. return WithCapabilities(caps)(ctx, client, c, s)
  61. }
  62. // WithAllKnownCapabilities sets all the known linux capabilities for the container process
  63. var WithAllKnownCapabilities = func(ctx context.Context, client Client, c *containers.Container, s *Spec) error {
  64. caps := cap.Known()
  65. return WithCapabilities(caps)(ctx, client, c, s)
  66. }
  67. func escapeAndCombineArgs(args []string) string {
  68. panic("not supported")
  69. }