github.com/goern/docker@v1.9.0-rc1/daemon/daemon_linux.go (about)

     1  package daemon
     2  
     3  import (
     4  	"bufio"
     5  	"fmt"
     6  	"io"
     7  	"os"
     8  	"path/filepath"
     9  	"strings"
    10  
    11  	"github.com/Sirupsen/logrus"
    12  )
    13  
    14  // cleanupMounts umounts shm/mqueue mounts for old containers
    15  func (daemon *Daemon) cleanupMounts() error {
    16  	logrus.Debugf("Cleaning up old shm/mqueue mounts: start.")
    17  	f, err := os.Open("/proc/self/mountinfo")
    18  	if err != nil {
    19  		return err
    20  	}
    21  	defer f.Close()
    22  
    23  	return daemon.cleanupMountsFromReader(f, detachMounted)
    24  }
    25  
    26  func (daemon *Daemon) cleanupMountsFromReader(reader io.Reader, unmount func(target string) error) error {
    27  	if daemon.repository == "" {
    28  		return nil
    29  	}
    30  	sc := bufio.NewScanner(reader)
    31  	var errors []string
    32  	for sc.Scan() {
    33  		line := sc.Text()
    34  		fields := strings.Fields(line)
    35  		if strings.HasPrefix(fields[4], daemon.repository) {
    36  			logrus.Debugf("Mount base: %v, repository %s", fields[4], daemon.repository)
    37  			mnt := fields[4]
    38  			mountBase := filepath.Base(mnt)
    39  			if mountBase == "mqueue" || mountBase == "shm" {
    40  				logrus.Debugf("Unmounting %v", mnt)
    41  				if err := unmount(mnt); err != nil {
    42  					logrus.Error(err)
    43  					errors = append(errors, err.Error())
    44  				}
    45  			}
    46  		}
    47  	}
    48  
    49  	if err := sc.Err(); err != nil {
    50  		return err
    51  	}
    52  
    53  	if len(errors) > 0 {
    54  		return fmt.Errorf("Error cleaningup mounts:\n%v", strings.Join(errors, "\n"))
    55  	}
    56  
    57  	logrus.Debugf("Cleaning up old shm/mqueue mounts: done.")
    58  	return nil
    59  }