container_opts_unix.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. "fmt"
  18. "os"
  19. "path/filepath"
  20. "syscall"
  21. "github.com/containerd/containerd/containers"
  22. "github.com/containerd/containerd/errdefs"
  23. "github.com/containerd/containerd/mount"
  24. "github.com/opencontainers/image-spec/identity"
  25. )
  26. // WithRemappedSnapshot creates a new snapshot and remaps the uid/gid for the
  27. // filesystem to be used by a container with user namespaces
  28. func WithRemappedSnapshot(id string, i Image, uid, gid uint32) NewContainerOpts {
  29. return withRemappedSnapshotBase(id, i, uid, gid, false)
  30. }
  31. // WithRemappedSnapshotView is similar to WithRemappedSnapshot but rootfs is mounted as read-only.
  32. func WithRemappedSnapshotView(id string, i Image, uid, gid uint32) NewContainerOpts {
  33. return withRemappedSnapshotBase(id, i, uid, gid, true)
  34. }
  35. func withRemappedSnapshotBase(id string, i Image, uid, gid uint32, readonly bool) NewContainerOpts {
  36. return func(ctx context.Context, client *Client, c *containers.Container) error {
  37. diffIDs, err := i.(*image).i.RootFS(ctx, client.ContentStore(), client.platform)
  38. if err != nil {
  39. return err
  40. }
  41. var (
  42. parent = identity.ChainID(diffIDs).String()
  43. usernsID = fmt.Sprintf("%s-%d-%d", parent, uid, gid)
  44. )
  45. c.Snapshotter, err = client.resolveSnapshotterName(ctx, c.Snapshotter)
  46. if err != nil {
  47. return err
  48. }
  49. snapshotter, err := client.getSnapshotter(ctx, c.Snapshotter)
  50. if err != nil {
  51. return err
  52. }
  53. if _, err := snapshotter.Stat(ctx, usernsID); err == nil {
  54. if _, err := snapshotter.Prepare(ctx, id, usernsID); err == nil {
  55. c.SnapshotKey = id
  56. c.Image = i.Name()
  57. return nil
  58. } else if !errdefs.IsNotFound(err) {
  59. return err
  60. }
  61. }
  62. mounts, err := snapshotter.Prepare(ctx, usernsID+"-remap", parent)
  63. if err != nil {
  64. return err
  65. }
  66. if err := remapRootFS(ctx, mounts, uid, gid); err != nil {
  67. snapshotter.Remove(ctx, usernsID)
  68. return err
  69. }
  70. if err := snapshotter.Commit(ctx, usernsID, usernsID+"-remap"); err != nil {
  71. return err
  72. }
  73. if readonly {
  74. _, err = snapshotter.View(ctx, id, usernsID)
  75. } else {
  76. _, err = snapshotter.Prepare(ctx, id, usernsID)
  77. }
  78. if err != nil {
  79. return err
  80. }
  81. c.SnapshotKey = id
  82. c.Image = i.Name()
  83. return nil
  84. }
  85. }
  86. func remapRootFS(ctx context.Context, mounts []mount.Mount, uid, gid uint32) error {
  87. return mount.WithTempMount(ctx, mounts, func(root string) error {
  88. return filepath.Walk(root, incrementFS(root, uid, gid))
  89. })
  90. }
  91. func incrementFS(root string, uidInc, gidInc uint32) filepath.WalkFunc {
  92. return func(path string, info os.FileInfo, err error) error {
  93. if err != nil {
  94. return err
  95. }
  96. var (
  97. stat = info.Sys().(*syscall.Stat_t)
  98. u, g = int(stat.Uid + uidInc), int(stat.Gid + gidInc)
  99. )
  100. // be sure the lchown the path as to not de-reference the symlink to a host file
  101. return os.Lchown(path, u, g)
  102. }
  103. }