daemon_linux.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. package daemon // import "github.com/docker/docker/daemon"
  2. import (
  3. "bufio"
  4. "context"
  5. "fmt"
  6. "io"
  7. "net"
  8. "os"
  9. "regexp"
  10. "strings"
  11. "sync"
  12. "github.com/containerd/log"
  13. "github.com/docker/docker/daemon/config"
  14. "github.com/docker/docker/libnetwork/ns"
  15. "github.com/docker/docker/libnetwork/resolvconf"
  16. "github.com/moby/sys/mount"
  17. "github.com/moby/sys/mountinfo"
  18. "github.com/pkg/errors"
  19. "github.com/vishvananda/netlink"
  20. "golang.org/x/sys/unix"
  21. )
  22. // On Linux, plugins use a static path for storing execution state,
  23. // instead of deriving path from daemon's exec-root. This is because
  24. // plugin socket files are created here and they cannot exceed max
  25. // path length of 108 bytes.
  26. func getPluginExecRoot(_ *config.Config) string {
  27. return "/run/docker/plugins"
  28. }
  29. func (daemon *Daemon) cleanupMountsByID(id string) error {
  30. log.G(context.TODO()).Debugf("Cleaning up old mountid %s: start.", id)
  31. f, err := os.Open("/proc/self/mountinfo")
  32. if err != nil {
  33. return err
  34. }
  35. defer f.Close()
  36. return daemon.cleanupMountsFromReaderByID(f, id, mount.Unmount)
  37. }
  38. func (daemon *Daemon) cleanupMountsFromReaderByID(reader io.Reader, id string, unmount func(target string) error) error {
  39. if daemon.root == "" {
  40. return nil
  41. }
  42. var errs []string
  43. regexps := getCleanPatterns(id)
  44. sc := bufio.NewScanner(reader)
  45. for sc.Scan() {
  46. if fields := strings.Fields(sc.Text()); len(fields) > 4 {
  47. if mnt := fields[4]; strings.HasPrefix(mnt, daemon.root) {
  48. for _, p := range regexps {
  49. if p.MatchString(mnt) {
  50. if err := unmount(mnt); err != nil {
  51. log.G(context.TODO()).Error(err)
  52. errs = append(errs, err.Error())
  53. }
  54. }
  55. }
  56. }
  57. }
  58. }
  59. if err := sc.Err(); err != nil {
  60. return err
  61. }
  62. if len(errs) > 0 {
  63. return fmt.Errorf("Error cleaning up mounts:\n%v", strings.Join(errs, "\n"))
  64. }
  65. log.G(context.TODO()).Debugf("Cleaning up old mountid %v: done.", id)
  66. return nil
  67. }
  68. // cleanupMounts umounts used by container resources and the daemon root mount
  69. func (daemon *Daemon) cleanupMounts(cfg *config.Config) error {
  70. if err := daemon.cleanupMountsByID(""); err != nil {
  71. return err
  72. }
  73. info, err := mountinfo.GetMounts(mountinfo.SingleEntryFilter(daemon.root))
  74. if err != nil {
  75. return errors.Wrap(err, "error reading mount table for cleanup")
  76. }
  77. if len(info) < 1 {
  78. // no mount found, we're done here
  79. return nil
  80. }
  81. // `info.Root` here is the root mountpoint of the passed in path (`daemon.root`).
  82. // The ony cases that need to be cleaned up is when the daemon has performed a
  83. // `mount --bind /daemon/root /daemon/root && mount --make-shared /daemon/root`
  84. // This is only done when the daemon is started up and `/daemon/root` is not
  85. // already on a shared mountpoint.
  86. if !shouldUnmountRoot(daemon.root, info[0]) {
  87. return nil
  88. }
  89. unmountFile := getUnmountOnShutdownPath(cfg)
  90. if _, err := os.Stat(unmountFile); err != nil {
  91. return nil
  92. }
  93. log.G(context.TODO()).WithField("mountpoint", daemon.root).Debug("unmounting daemon root")
  94. if err := mount.Unmount(daemon.root); err != nil {
  95. return err
  96. }
  97. return os.Remove(unmountFile)
  98. }
  99. func getCleanPatterns(id string) (regexps []*regexp.Regexp) {
  100. var patterns []string
  101. if id == "" {
  102. id = "[0-9a-f]{64}"
  103. patterns = append(patterns, "containers/"+id+"/mounts/shm", "containers/"+id+"/shm")
  104. }
  105. patterns = append(patterns, "overlay2/"+id+"/merged$", "zfs/graph/"+id+"$")
  106. for _, p := range patterns {
  107. r, err := regexp.Compile(p)
  108. if err == nil {
  109. regexps = append(regexps, r)
  110. }
  111. }
  112. return
  113. }
  114. func shouldUnmountRoot(root string, info *mountinfo.Info) bool {
  115. if !strings.HasSuffix(root, info.Root) {
  116. return false
  117. }
  118. return hasMountInfoOption(info.Optional, sharedPropagationOption)
  119. }
  120. // setupResolvConf sets the appropriate resolv.conf file if not specified
  121. // When systemd-resolved is running the default /etc/resolv.conf points to
  122. // localhost. In this case fetch the alternative config file that is in a
  123. // different path so that containers can use it
  124. // In all the other cases fallback to the default one
  125. func setupResolvConf(config *config.Config) {
  126. if config.ResolvConf != "" {
  127. return
  128. }
  129. config.ResolvConf = resolvconf.Path()
  130. }
  131. // ifaceAddrs returns the IPv4 and IPv6 addresses assigned to the network
  132. // interface with name linkName.
  133. //
  134. // No error is returned if the named interface does not exist.
  135. func ifaceAddrs(linkName string) (v4, v6 []*net.IPNet, err error) {
  136. nl := ns.NlHandle()
  137. link, err := nl.LinkByName(linkName)
  138. if err != nil {
  139. if !errors.As(err, new(netlink.LinkNotFoundError)) {
  140. return nil, nil, err
  141. }
  142. return nil, nil, nil
  143. }
  144. get := func(family int) ([]*net.IPNet, error) {
  145. addrs, err := nl.AddrList(link, family)
  146. if err != nil {
  147. return nil, err
  148. }
  149. ipnets := make([]*net.IPNet, len(addrs))
  150. for i := range addrs {
  151. ipnets[i] = addrs[i].IPNet
  152. }
  153. return ipnets, nil
  154. }
  155. v4, err = get(netlink.FAMILY_V4)
  156. if err != nil {
  157. return nil, nil, err
  158. }
  159. v6, err = get(netlink.FAMILY_V6)
  160. if err != nil {
  161. return nil, nil, err
  162. }
  163. return v4, v6, nil
  164. }
  165. var (
  166. kernelSupportsRROOnce sync.Once
  167. kernelSupportsRROErr error
  168. )
  169. func kernelSupportsRecursivelyReadOnly() error {
  170. fn := func() error {
  171. tmpMnt, err := os.MkdirTemp("", "moby-detect-rro")
  172. if err != nil {
  173. return fmt.Errorf("failed to create a temp directory: %w", err)
  174. }
  175. for {
  176. err = unix.Mount("", tmpMnt, "tmpfs", 0, "")
  177. if !errors.Is(err, unix.EINTR) {
  178. break
  179. }
  180. }
  181. if err != nil {
  182. return fmt.Errorf("failed to mount tmpfs on %q: %w", tmpMnt, err)
  183. }
  184. defer func() {
  185. var umErr error
  186. for {
  187. umErr = unix.Unmount(tmpMnt, 0)
  188. if !errors.Is(umErr, unix.EINTR) {
  189. break
  190. }
  191. }
  192. if umErr != nil {
  193. log.G(context.TODO()).WithError(umErr).Warnf("Failed to unmount %q", tmpMnt)
  194. }
  195. }()
  196. attr := &unix.MountAttr{
  197. Attr_set: unix.MOUNT_ATTR_RDONLY,
  198. }
  199. for {
  200. err = unix.MountSetattr(-1, tmpMnt, unix.AT_RECURSIVE, attr)
  201. if !errors.Is(err, unix.EINTR) {
  202. break
  203. }
  204. }
  205. // ENOSYS on kernel < 5.12
  206. if err != nil {
  207. return fmt.Errorf("failed to call mount_setattr: %w", err)
  208. }
  209. return nil
  210. }
  211. kernelSupportsRROOnce.Do(func() {
  212. kernelSupportsRROErr = fn()
  213. })
  214. return kernelSupportsRROErr
  215. }
  216. func supportsRecursivelyReadOnly(cfg *configStore, runtime string) error {
  217. if err := kernelSupportsRecursivelyReadOnly(); err != nil {
  218. return fmt.Errorf("rro is not supported: %w (kernel is older than 5.12?)", err)
  219. }
  220. if runtime == "" {
  221. runtime = cfg.Runtimes.Default
  222. }
  223. features := cfg.Runtimes.Features(runtime)
  224. if features == nil {
  225. return fmt.Errorf("rro is not supported by runtime %q: OCI features struct is not available", runtime)
  226. }
  227. for _, s := range features.MountOptions {
  228. if s == "rro" {
  229. return nil
  230. }
  231. }
  232. return fmt.Errorf("rro is not supported by runtime %q", runtime)
  233. }