github.com/rhatdan/docker@v0.7.7-0.20180119204836-47a0dcbcd20a/daemon/daemon_linux.go (about)

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