task_opts_unix.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //go:build !windows
  2. /*
  3. Copyright The containerd Authors.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package containerd
  15. import (
  16. "context"
  17. "errors"
  18. "github.com/containerd/containerd/runtime/linux/runctypes"
  19. "github.com/containerd/containerd/runtime/v2/runc/options"
  20. )
  21. // WithNoNewKeyring causes tasks not to be created with a new keyring for secret storage.
  22. // There is an upper limit on the number of keyrings in a linux system
  23. func WithNoNewKeyring(ctx context.Context, c *Client, ti *TaskInfo) error {
  24. if CheckRuntime(ti.Runtime(), "io.containerd.runc") {
  25. if ti.Options == nil {
  26. ti.Options = &options.Options{}
  27. }
  28. opts, ok := ti.Options.(*options.Options)
  29. if !ok {
  30. return errors.New("invalid v2 shim create options format")
  31. }
  32. opts.NoNewKeyring = true
  33. } else {
  34. if ti.Options == nil {
  35. ti.Options = &runctypes.CreateOptions{}
  36. }
  37. opts, ok := ti.Options.(*runctypes.CreateOptions)
  38. if !ok {
  39. return errors.New("could not cast TaskInfo Options to CreateOptions")
  40. }
  41. opts.NoNewKeyring = true
  42. }
  43. return nil
  44. }
  45. // WithNoPivotRoot instructs the runtime not to you pivot_root
  46. func WithNoPivotRoot(_ context.Context, _ *Client, ti *TaskInfo) error {
  47. if CheckRuntime(ti.Runtime(), "io.containerd.runc") {
  48. if ti.Options == nil {
  49. ti.Options = &options.Options{}
  50. }
  51. opts, ok := ti.Options.(*options.Options)
  52. if !ok {
  53. return errors.New("invalid v2 shim create options format")
  54. }
  55. opts.NoPivotRoot = true
  56. } else {
  57. if ti.Options == nil {
  58. ti.Options = &runctypes.CreateOptions{
  59. NoPivotRoot: true,
  60. }
  61. return nil
  62. }
  63. opts, ok := ti.Options.(*runctypes.CreateOptions)
  64. if !ok {
  65. return errors.New("invalid options type, expected runctypes.CreateOptions")
  66. }
  67. opts.NoPivotRoot = true
  68. }
  69. return nil
  70. }
  71. // WithShimCgroup sets the existing cgroup for the shim
  72. func WithShimCgroup(path string) NewTaskOpts {
  73. return func(ctx context.Context, c *Client, ti *TaskInfo) error {
  74. if CheckRuntime(ti.Runtime(), "io.containerd.runc") {
  75. if ti.Options == nil {
  76. ti.Options = &options.Options{}
  77. }
  78. opts, ok := ti.Options.(*options.Options)
  79. if !ok {
  80. return errors.New("invalid v2 shim create options format")
  81. }
  82. opts.ShimCgroup = path
  83. } else {
  84. if ti.Options == nil {
  85. ti.Options = &runctypes.CreateOptions{}
  86. }
  87. opts, ok := ti.Options.(*runctypes.CreateOptions)
  88. if !ok {
  89. return errors.New("could not cast TaskInfo Options to CreateOptions")
  90. }
  91. opts.ShimCgroup = path
  92. }
  93. return nil
  94. }
  95. }
  96. // WithUIDOwner allows console I/O to work with the remapped UID in user namespace
  97. func WithUIDOwner(uid uint32) NewTaskOpts {
  98. return func(ctx context.Context, c *Client, ti *TaskInfo) error {
  99. if CheckRuntime(ti.Runtime(), "io.containerd.runc") {
  100. if ti.Options == nil {
  101. ti.Options = &options.Options{}
  102. }
  103. opts, ok := ti.Options.(*options.Options)
  104. if !ok {
  105. return errors.New("invalid v2 shim create options format")
  106. }
  107. opts.IoUid = uid
  108. } else {
  109. if ti.Options == nil {
  110. ti.Options = &runctypes.CreateOptions{}
  111. }
  112. opts, ok := ti.Options.(*runctypes.CreateOptions)
  113. if !ok {
  114. return errors.New("could not cast TaskInfo Options to CreateOptions")
  115. }
  116. opts.IoUid = uid
  117. }
  118. return nil
  119. }
  120. }
  121. // WithGIDOwner allows console I/O to work with the remapped GID in user namespace
  122. func WithGIDOwner(gid uint32) NewTaskOpts {
  123. return func(ctx context.Context, c *Client, ti *TaskInfo) error {
  124. if CheckRuntime(ti.Runtime(), "io.containerd.runc") {
  125. if ti.Options == nil {
  126. ti.Options = &options.Options{}
  127. }
  128. opts, ok := ti.Options.(*options.Options)
  129. if !ok {
  130. return errors.New("invalid v2 shim create options format")
  131. }
  132. opts.IoGid = gid
  133. } else {
  134. if ti.Options == nil {
  135. ti.Options = &runctypes.CreateOptions{}
  136. }
  137. opts, ok := ti.Options.(*runctypes.CreateOptions)
  138. if !ok {
  139. return errors.New("could not cast TaskInfo Options to CreateOptions")
  140. }
  141. opts.IoGid = gid
  142. }
  143. return nil
  144. }
  145. }