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"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
2015-10-30 18:55:52 +00:00
|
|
|
"github.com/docker/docker/pkg/mount"
|
2015-08-03 22:05:34 +00:00
|
|
|
)
|
|
|
|
|
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
|
|
|
}
|