2015-08-03 22:05:34 +00:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2015-08-26 12:00:01 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2015-08-03 22:05:34 +00:00
|
|
|
"os"
|
2016-03-29 22:27:04 +00:00
|
|
|
"regexp"
|
2015-08-03 22:05:34 +00:00
|
|
|
"strings"
|
|
|
|
|
2017-06-01 00:11:42 +00:00
|
|
|
"github.com/docker/docker/pkg/fileutils"
|
2015-10-30 18:55:52 +00:00
|
|
|
"github.com/docker/docker/pkg/mount"
|
2017-07-26 21:42:13 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2015-08-03 22:05:34 +00:00
|
|
|
)
|
|
|
|
|
2017-02-02 19:22:12 +00:00
|
|
|
// On Linux, plugins use a static path for storing execution state,
|
|
|
|
// instead of deriving path from daemon's exec-root. This is because
|
|
|
|
// plugin socket files are created here and they cannot exceed max
|
|
|
|
// path length of 108 bytes.
|
|
|
|
func getPluginExecRoot(root string) string {
|
|
|
|
return "/run/docker/plugins"
|
|
|
|
}
|
|
|
|
|
2016-03-18 18:50:19 +00:00
|
|
|
func (daemon *Daemon) cleanupMountsByID(id string) error {
|
|
|
|
logrus.Debugf("Cleaning up old mountid %s: start.", id)
|
|
|
|
f, err := os.Open("/proc/self/mountinfo")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
return daemon.cleanupMountsFromReaderByID(f, id, mount.Unmount)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (daemon *Daemon) cleanupMountsFromReaderByID(reader io.Reader, id string, unmount func(target string) error) error {
|
|
|
|
if daemon.root == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var errors []string
|
2016-03-29 22:27:04 +00:00
|
|
|
|
|
|
|
regexps := getCleanPatterns(id)
|
2016-03-18 18:50:19 +00:00
|
|
|
sc := bufio.NewScanner(reader)
|
|
|
|
for sc.Scan() {
|
2016-03-29 22:27:04 +00:00
|
|
|
if fields := strings.Fields(sc.Text()); len(fields) >= 4 {
|
|
|
|
if mnt := fields[4]; strings.HasPrefix(mnt, daemon.root) {
|
|
|
|
for _, p := range regexps {
|
|
|
|
if p.MatchString(mnt) {
|
|
|
|
if err := unmount(mnt); err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
errors = append(errors, err.Error())
|
|
|
|
}
|
|
|
|
}
|
2016-03-18 18:50:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := sc.Err(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(errors) > 0 {
|
2016-03-29 22:27:04 +00:00
|
|
|
return fmt.Errorf("Error cleaning up mounts:\n%v", strings.Join(errors, "\n"))
|
2016-03-18 18:50:19 +00:00
|
|
|
}
|
|
|
|
|
2016-03-29 22:27:04 +00:00
|
|
|
logrus.Debugf("Cleaning up old mountid %v: done.", id)
|
2016-03-18 18:50:19 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-03 22:05:34 +00:00
|
|
|
// cleanupMounts umounts shm/mqueue mounts for old containers
|
|
|
|
func (daemon *Daemon) cleanupMounts() error {
|
2016-03-29 22:27:04 +00:00
|
|
|
return daemon.cleanupMountsByID("")
|
2015-08-26 12:00:01 +00:00
|
|
|
}
|
|
|
|
|
2016-03-29 22:27:04 +00:00
|
|
|
func getCleanPatterns(id string) (regexps []*regexp.Regexp) {
|
|
|
|
var patterns []string
|
|
|
|
if id == "" {
|
|
|
|
id = "[0-9a-f]{64}"
|
|
|
|
patterns = append(patterns, "containers/"+id+"/shm")
|
2015-08-25 07:58:33 +00:00
|
|
|
}
|
2016-03-29 22:27:04 +00:00
|
|
|
patterns = append(patterns, "aufs/mnt/"+id+"$", "overlay/"+id+"/merged$", "zfs/graph/"+id+"$")
|
|
|
|
for _, p := range patterns {
|
|
|
|
r, err := regexp.Compile(p)
|
|
|
|
if err == nil {
|
|
|
|
regexps = append(regexps, r)
|
2015-08-03 22:05:34 +00:00
|
|
|
}
|
|
|
|
}
|
2016-03-29 22:27:04 +00:00
|
|
|
return
|
2015-08-03 22:05:34 +00:00
|
|
|
}
|
2017-06-01 00:11:42 +00:00
|
|
|
|
|
|
|
func getRealPath(path string) (string, error) {
|
|
|
|
return fileutils.ReadSymlinkedDirectory(path)
|
|
|
|
}
|