daemon_linux.go 3.8 KB

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