daemon_linux.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package daemon // import "github.com/docker/docker/daemon"
  2. import (
  3. "bufio"
  4. "fmt"
  5. "io"
  6. "net"
  7. "os"
  8. "regexp"
  9. "strings"
  10. "github.com/docker/docker/daemon/config"
  11. "github.com/docker/docker/libnetwork/ns"
  12. "github.com/docker/docker/libnetwork/resolvconf"
  13. "github.com/moby/sys/mount"
  14. "github.com/moby/sys/mountinfo"
  15. "github.com/pkg/errors"
  16. "github.com/sirupsen/logrus"
  17. "github.com/vishvananda/netlink"
  18. )
  19. // On Linux, plugins use a static path for storing execution state,
  20. // instead of deriving path from daemon's exec-root. This is because
  21. // plugin socket files are created here and they cannot exceed max
  22. // path length of 108 bytes.
  23. func getPluginExecRoot(_ *config.Config) string {
  24. return "/run/docker/plugins"
  25. }
  26. func (daemon *Daemon) cleanupMountsByID(id string) error {
  27. logrus.Debugf("Cleaning up old mountid %s: start.", id)
  28. f, err := os.Open("/proc/self/mountinfo")
  29. if err != nil {
  30. return err
  31. }
  32. defer f.Close()
  33. return daemon.cleanupMountsFromReaderByID(f, id, mount.Unmount)
  34. }
  35. func (daemon *Daemon) cleanupMountsFromReaderByID(reader io.Reader, id string, unmount func(target string) error) error {
  36. if daemon.root == "" {
  37. return nil
  38. }
  39. var errs []string
  40. regexps := getCleanPatterns(id)
  41. sc := bufio.NewScanner(reader)
  42. for sc.Scan() {
  43. if fields := strings.Fields(sc.Text()); len(fields) > 4 {
  44. if mnt := fields[4]; strings.HasPrefix(mnt, daemon.root) {
  45. for _, p := range regexps {
  46. if p.MatchString(mnt) {
  47. if err := unmount(mnt); err != nil {
  48. logrus.Error(err)
  49. errs = append(errs, err.Error())
  50. }
  51. }
  52. }
  53. }
  54. }
  55. }
  56. if err := sc.Err(); err != nil {
  57. return err
  58. }
  59. if len(errs) > 0 {
  60. return fmt.Errorf("Error cleaning up mounts:\n%v", strings.Join(errs, "\n"))
  61. }
  62. logrus.Debugf("Cleaning up old mountid %v: done.", id)
  63. return nil
  64. }
  65. // cleanupMounts umounts used by container resources and the daemon root mount
  66. func (daemon *Daemon) cleanupMounts() error {
  67. if err := daemon.cleanupMountsByID(""); err != nil {
  68. return err
  69. }
  70. info, err := mountinfo.GetMounts(mountinfo.SingleEntryFilter(daemon.root))
  71. if err != nil {
  72. return errors.Wrap(err, "error reading mount table for cleanup")
  73. }
  74. if len(info) < 1 {
  75. // no mount found, we're done here
  76. return nil
  77. }
  78. // `info.Root` here is the root mountpoint of the passed in path (`daemon.root`).
  79. // The ony cases that need to be cleaned up is when the daemon has performed a
  80. // `mount --bind /daemon/root /daemon/root && mount --make-shared /daemon/root`
  81. // This is only done when the daemon is started up and `/daemon/root` is not
  82. // already on a shared mountpoint.
  83. if !shouldUnmountRoot(daemon.root, info[0]) {
  84. return nil
  85. }
  86. unmountFile := getUnmountOnShutdownPath(daemon.configStore)
  87. if _, err := os.Stat(unmountFile); err != nil {
  88. return nil
  89. }
  90. logrus.WithField("mountpoint", daemon.root).Debug("unmounting daemon root")
  91. if err := mount.Unmount(daemon.root); err != nil {
  92. return err
  93. }
  94. return os.Remove(unmountFile)
  95. }
  96. func getCleanPatterns(id string) (regexps []*regexp.Regexp) {
  97. var patterns []string
  98. if id == "" {
  99. id = "[0-9a-f]{64}"
  100. patterns = append(patterns, "containers/"+id+"/mounts/shm", "containers/"+id+"/shm")
  101. }
  102. patterns = append(patterns, "overlay2/"+id+"/merged$", "aufs/mnt/"+id+"$", "overlay/"+id+"/merged$", "zfs/graph/"+id+"$")
  103. for _, p := range patterns {
  104. r, err := regexp.Compile(p)
  105. if err == nil {
  106. regexps = append(regexps, r)
  107. }
  108. }
  109. return
  110. }
  111. func shouldUnmountRoot(root string, info *mountinfo.Info) bool {
  112. if !strings.HasSuffix(root, info.Root) {
  113. return false
  114. }
  115. return hasMountInfoOption(info.Optional, sharedPropagationOption)
  116. }
  117. // setupResolvConf sets the appropriate resolv.conf file if not specified
  118. // When systemd-resolved is running the default /etc/resolv.conf points to
  119. // localhost. In this case fetch the alternative config file that is in a
  120. // different path so that containers can use it
  121. // In all the other cases fallback to the default one
  122. func setupResolvConf(config *config.Config) {
  123. if config.ResolvConf != "" {
  124. return
  125. }
  126. config.ResolvConf = resolvconf.Path()
  127. }
  128. // ifaceAddrs returns the IPv4 and IPv6 addresses assigned to the network
  129. // interface with name linkName.
  130. //
  131. // No error is returned if the named interface does not exist.
  132. func ifaceAddrs(linkName string) (v4, v6 []*net.IPNet, err error) {
  133. nl := ns.NlHandle()
  134. link, err := nl.LinkByName(linkName)
  135. if err != nil {
  136. if !errors.As(err, new(netlink.LinkNotFoundError)) {
  137. return nil, nil, err
  138. }
  139. return nil, nil, nil
  140. }
  141. get := func(family int) ([]*net.IPNet, error) {
  142. addrs, err := nl.AddrList(link, family)
  143. if err != nil {
  144. return nil, err
  145. }
  146. ipnets := make([]*net.IPNet, len(addrs))
  147. for i := range addrs {
  148. ipnets[i] = addrs[i].IPNet
  149. }
  150. return ipnets, nil
  151. }
  152. v4, err = get(netlink.FAMILY_V4)
  153. if err != nil {
  154. return nil, nil, err
  155. }
  156. v6, err = get(netlink.FAMILY_V6)
  157. if err != nil {
  158. return nil, nil, err
  159. }
  160. return v4, v6, nil
  161. }