bundle.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. // +build linux
  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 linux
  15. import (
  16. "context"
  17. "crypto/sha256"
  18. "encoding/json"
  19. "fmt"
  20. "io/ioutil"
  21. "os"
  22. "path/filepath"
  23. "github.com/containerd/containerd/events/exchange"
  24. "github.com/containerd/containerd/runtime/linux/runctypes"
  25. "github.com/containerd/containerd/runtime/v1/shim"
  26. "github.com/containerd/containerd/runtime/v1/shim/client"
  27. "github.com/opencontainers/runtime-spec/specs-go"
  28. "github.com/pkg/errors"
  29. )
  30. // loadBundle loads an existing bundle from disk
  31. func loadBundle(id, path, workdir string) *bundle {
  32. return &bundle{
  33. id: id,
  34. path: path,
  35. workDir: workdir,
  36. }
  37. }
  38. // newBundle creates a new bundle on disk at the provided path for the given id
  39. func newBundle(id, path, workDir string, spec []byte) (b *bundle, err error) {
  40. if err := os.MkdirAll(path, 0711); err != nil {
  41. return nil, err
  42. }
  43. path = filepath.Join(path, id)
  44. if err := os.Mkdir(path, 0700); err != nil {
  45. return nil, err
  46. }
  47. defer func() {
  48. if err != nil {
  49. os.RemoveAll(path)
  50. }
  51. }()
  52. if err := prepareBundleDirectoryPermissions(path, spec); err != nil {
  53. return nil, err
  54. }
  55. workDir = filepath.Join(workDir, id)
  56. if err := os.MkdirAll(workDir, 0711); err != nil {
  57. return nil, err
  58. }
  59. defer func() {
  60. if err != nil {
  61. os.RemoveAll(workDir)
  62. }
  63. }()
  64. rootfs := filepath.Join(path, "rootfs")
  65. if err := os.MkdirAll(rootfs, 0711); err != nil {
  66. return nil, err
  67. }
  68. err = ioutil.WriteFile(filepath.Join(path, configFilename), spec, 0666)
  69. return &bundle{
  70. id: id,
  71. path: path,
  72. workDir: workDir,
  73. }, err
  74. }
  75. // prepareBundleDirectoryPermissions prepares the permissions of the bundle
  76. // directory. When user namespaces are enabled, the permissions are modified
  77. // to allow the remapped root GID to access the bundle.
  78. func prepareBundleDirectoryPermissions(path string, spec []byte) error {
  79. gid, err := remappedGID(spec)
  80. if err != nil {
  81. return err
  82. }
  83. if gid == 0 {
  84. return nil
  85. }
  86. if err := os.Chown(path, -1, int(gid)); err != nil {
  87. return err
  88. }
  89. return os.Chmod(path, 0710)
  90. }
  91. // ociSpecUserNS is a subset of specs.Spec used to reduce garbage during
  92. // unmarshal.
  93. type ociSpecUserNS struct {
  94. Linux *linuxSpecUserNS
  95. }
  96. // linuxSpecUserNS is a subset of specs.Linux used to reduce garbage during
  97. // unmarshal.
  98. type linuxSpecUserNS struct {
  99. GIDMappings []specs.LinuxIDMapping
  100. }
  101. // remappedGID reads the remapped GID 0 from the OCI spec, if it exists. If
  102. // there is no remapping, remappedGID returns 0. If the spec cannot be parsed,
  103. // remappedGID returns an error.
  104. func remappedGID(spec []byte) (uint32, error) {
  105. var ociSpec ociSpecUserNS
  106. err := json.Unmarshal(spec, &ociSpec)
  107. if err != nil {
  108. return 0, err
  109. }
  110. if ociSpec.Linux == nil || len(ociSpec.Linux.GIDMappings) == 0 {
  111. return 0, nil
  112. }
  113. for _, mapping := range ociSpec.Linux.GIDMappings {
  114. if mapping.ContainerID == 0 {
  115. return mapping.HostID, nil
  116. }
  117. }
  118. return 0, nil
  119. }
  120. type bundle struct {
  121. id string
  122. path string
  123. workDir string
  124. }
  125. // ShimOpt specifies shim options for initialization and connection
  126. type ShimOpt func(*bundle, string, *runctypes.RuncOptions) (shim.Config, client.Opt)
  127. // ShimRemote is a ShimOpt for connecting and starting a remote shim
  128. func ShimRemote(c *Config, daemonAddress, cgroup string, exitHandler func()) ShimOpt {
  129. return func(b *bundle, ns string, ropts *runctypes.RuncOptions) (shim.Config, client.Opt) {
  130. config := b.shimConfig(ns, c, ropts)
  131. return config,
  132. client.WithStart(c.Shim, b.shimAddress(ns, daemonAddress), daemonAddress, cgroup, c.ShimDebug, exitHandler)
  133. }
  134. }
  135. // ShimLocal is a ShimOpt for using an in process shim implementation
  136. func ShimLocal(c *Config, exchange *exchange.Exchange) ShimOpt {
  137. return func(b *bundle, ns string, ropts *runctypes.RuncOptions) (shim.Config, client.Opt) {
  138. return b.shimConfig(ns, c, ropts), client.WithLocal(exchange)
  139. }
  140. }
  141. // ShimConnect is a ShimOpt for connecting to an existing remote shim
  142. func ShimConnect(c *Config, onClose func()) ShimOpt {
  143. return func(b *bundle, ns string, ropts *runctypes.RuncOptions) (shim.Config, client.Opt) {
  144. return b.shimConfig(ns, c, ropts), client.WithConnect(b.decideShimAddress(ns), onClose)
  145. }
  146. }
  147. // NewShimClient connects to the shim managing the bundle and tasks creating it if needed
  148. func (b *bundle) NewShimClient(ctx context.Context, namespace string, getClientOpts ShimOpt, runcOpts *runctypes.RuncOptions) (*client.Client, error) {
  149. cfg, opt := getClientOpts(b, namespace, runcOpts)
  150. return client.New(ctx, cfg, opt)
  151. }
  152. // Delete deletes the bundle from disk
  153. func (b *bundle) Delete() error {
  154. address, _ := b.loadAddress()
  155. if address != "" {
  156. // we don't care about errors here
  157. client.RemoveSocket(address)
  158. }
  159. err := atomicDelete(b.path)
  160. if err == nil {
  161. return atomicDelete(b.workDir)
  162. }
  163. // error removing the bundle path; still attempt removing work dir
  164. err2 := atomicDelete(b.workDir)
  165. if err2 == nil {
  166. return err
  167. }
  168. return errors.Wrapf(err, "Failed to remove both bundle and workdir locations: %v", err2)
  169. }
  170. func (b *bundle) legacyShimAddress(namespace string) string {
  171. return filepath.Join(string(filepath.Separator), "containerd-shim", namespace, b.id, "shim.sock")
  172. }
  173. const socketRoot = "/run/containerd"
  174. func (b *bundle) shimAddress(namespace, socketPath string) string {
  175. d := sha256.Sum256([]byte(filepath.Join(socketPath, namespace, b.id)))
  176. return fmt.Sprintf("unix://%s/%x", filepath.Join(socketRoot, "s"), d)
  177. }
  178. func (b *bundle) loadAddress() (string, error) {
  179. addressPath := filepath.Join(b.path, "address")
  180. data, err := ioutil.ReadFile(addressPath)
  181. if err != nil {
  182. return "", err
  183. }
  184. return string(data), nil
  185. }
  186. func (b *bundle) decideShimAddress(namespace string) string {
  187. address, err := b.loadAddress()
  188. if err != nil {
  189. return b.legacyShimAddress(namespace)
  190. }
  191. return address
  192. }
  193. func (b *bundle) shimConfig(namespace string, c *Config, runcOptions *runctypes.RuncOptions) shim.Config {
  194. var (
  195. criuPath string
  196. runtimeRoot = c.RuntimeRoot
  197. systemdCgroup bool
  198. )
  199. if runcOptions != nil {
  200. criuPath = runcOptions.CriuPath
  201. systemdCgroup = runcOptions.SystemdCgroup
  202. if runcOptions.RuntimeRoot != "" {
  203. runtimeRoot = runcOptions.RuntimeRoot
  204. }
  205. }
  206. return shim.Config{
  207. Path: b.path,
  208. WorkDir: b.workDir,
  209. Namespace: namespace,
  210. Criu: criuPath,
  211. RuntimeRoot: runtimeRoot,
  212. SystemdCgroup: systemdCgroup,
  213. }
  214. }
  215. // atomicDelete renames the path to a hidden file before removal
  216. func atomicDelete(path string) error {
  217. // create a hidden dir for an atomic removal
  218. atomicPath := filepath.Join(filepath.Dir(path), fmt.Sprintf(".%s", filepath.Base(path)))
  219. if err := os.Rename(path, atomicPath); err != nil {
  220. if os.IsNotExist(err) {
  221. return nil
  222. }
  223. return err
  224. }
  225. return os.RemoveAll(atomicPath)
  226. }