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