2015-08-03 18:05:34 -04:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2015-08-26 08:00:01 -04:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2015-08-03 18:05:34 -04:00
|
|
|
"os"
|
2016-03-29 18:27:04 -04:00
|
|
|
"regexp"
|
2015-08-03 18:05:34 -04:00
|
|
|
"strings"
|
|
|
|
|
2017-05-31 20:11:42 -04:00
|
|
|
"github.com/docker/docker/pkg/fileutils"
|
2015-10-30 14:55:52 -04:00
|
|
|
"github.com/docker/docker/pkg/mount"
|
2017-07-26 17:42:13 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2015-08-03 18:05:34 -04:00
|
|
|
)
|
|
|
|
|
2017-02-02 14:22:12 -05: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 14:50:19 -04: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 18:27:04 -04:00
|
|
|
|
|
|
|
regexps := getCleanPatterns(id)
|
2016-03-18 14:50:19 -04:00
|
|
|
sc := bufio.NewScanner(reader)
|
|
|
|
for sc.Scan() {
|
2016-03-29 18:27:04 -04: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 14:50:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := sc.Err(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(errors) > 0 {
|
2016-03-29 18:27:04 -04:00
|
|
|
return fmt.Errorf("Error cleaning up mounts:\n%v", strings.Join(errors, "\n"))
|
2016-03-18 14:50:19 -04:00
|
|
|
}
|
|
|
|
|
2016-03-29 18:27:04 -04:00
|
|
|
logrus.Debugf("Cleaning up old mountid %v: done.", id)
|
2016-03-18 14:50:19 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-03 18:05:34 -04:00
|
|
|
// cleanupMounts umounts shm/mqueue mounts for old containers
|
|
|
|
func (daemon *Daemon) cleanupMounts() error {
|
2016-03-29 18:27:04 -04:00
|
|
|
return daemon.cleanupMountsByID("")
|
2015-08-26 08:00:01 -04:00
|
|
|
}
|
|
|
|
|
2016-03-29 18:27:04 -04: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 03:58:33 -04:00
|
|
|
}
|
2016-03-29 18:27:04 -04: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 18:05:34 -04:00
|
|
|
}
|
|
|
|
}
|
2016-03-29 18:27:04 -04:00
|
|
|
return
|
2015-08-03 18:05:34 -04:00
|
|
|
}
|
2017-05-31 20:11:42 -04:00
|
|
|
|
|
|
|
func getRealPath(path string) (string, error) {
|
|
|
|
return fileutils.ReadSymlinkedDirectory(path)
|
|
|
|
}
|