daemon_linux.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package daemon
  2. import (
  3. "bufio"
  4. "fmt"
  5. "io"
  6. "os"
  7. "regexp"
  8. "strings"
  9. "github.com/Sirupsen/logrus"
  10. "github.com/docker/docker/pkg/mount"
  11. )
  12. // On Linux, plugins use a static path for storing execution state,
  13. // instead of deriving path from daemon's exec-root. This is because
  14. // plugin socket files are created here and they cannot exceed max
  15. // path length of 108 bytes.
  16. func getPluginExecRoot(root string) string {
  17. return "/run/docker/plugins"
  18. }
  19. func (daemon *Daemon) cleanupMountsByID(id string) error {
  20. logrus.Debugf("Cleaning up old mountid %s: start.", id)
  21. f, err := os.Open("/proc/self/mountinfo")
  22. if err != nil {
  23. return err
  24. }
  25. defer f.Close()
  26. return daemon.cleanupMountsFromReaderByID(f, id, mount.Unmount)
  27. }
  28. func (daemon *Daemon) cleanupMountsFromReaderByID(reader io.Reader, id string, unmount func(target string) error) error {
  29. if daemon.root == "" {
  30. return nil
  31. }
  32. var errors []string
  33. regexps := getCleanPatterns(id)
  34. sc := bufio.NewScanner(reader)
  35. for sc.Scan() {
  36. if fields := strings.Fields(sc.Text()); len(fields) >= 4 {
  37. if mnt := fields[4]; strings.HasPrefix(mnt, daemon.root) {
  38. for _, p := range regexps {
  39. if p.MatchString(mnt) {
  40. if err := unmount(mnt); err != nil {
  41. logrus.Error(err)
  42. errors = append(errors, err.Error())
  43. }
  44. }
  45. }
  46. }
  47. }
  48. }
  49. if err := sc.Err(); err != nil {
  50. return err
  51. }
  52. if len(errors) > 0 {
  53. return fmt.Errorf("Error cleaning up mounts:\n%v", strings.Join(errors, "\n"))
  54. }
  55. logrus.Debugf("Cleaning up old mountid %v: done.", id)
  56. return nil
  57. }
  58. // cleanupMounts umounts shm/mqueue mounts for old containers
  59. func (daemon *Daemon) cleanupMounts() error {
  60. return daemon.cleanupMountsByID("")
  61. }
  62. func getCleanPatterns(id string) (regexps []*regexp.Regexp) {
  63. var patterns []string
  64. if id == "" {
  65. id = "[0-9a-f]{64}"
  66. patterns = append(patterns, "containers/"+id+"/shm")
  67. }
  68. patterns = append(patterns, "aufs/mnt/"+id+"$", "overlay/"+id+"/merged$", "zfs/graph/"+id+"$")
  69. for _, p := range patterns {
  70. r, err := regexp.Compile(p)
  71. if err == nil {
  72. regexps = append(regexps, r)
  73. }
  74. }
  75. return
  76. }