github.com/nguyentm83/docker@v1.5.0/pkg/mount/mount.go (about)

     1  package mount
     2  
     3  import (
     4  	"time"
     5  )
     6  
     7  func GetMounts() ([]*MountInfo, error) {
     8  	return parseMountTable()
     9  }
    10  
    11  // Looks at /proc/self/mountinfo to determine of the specified
    12  // mountpoint has been mounted
    13  func Mounted(mountpoint string) (bool, error) {
    14  	entries, err := parseMountTable()
    15  	if err != nil {
    16  		return false, err
    17  	}
    18  
    19  	// Search the table for the mountpoint
    20  	for _, e := range entries {
    21  		if e.Mountpoint == mountpoint {
    22  			return true, nil
    23  		}
    24  	}
    25  	return false, nil
    26  }
    27  
    28  // Mount the specified options at the target path only if
    29  // the target is not mounted
    30  // Options must be specified as fstab style
    31  func Mount(device, target, mType, options string) error {
    32  	flag, _ := parseOptions(options)
    33  	if flag&REMOUNT != REMOUNT {
    34  		if mounted, err := Mounted(target); err != nil || mounted {
    35  			return err
    36  		}
    37  	}
    38  	return ForceMount(device, target, mType, options)
    39  }
    40  
    41  // Mount the specified options at the target path
    42  // reguardless if the target is mounted or not
    43  // Options must be specified as fstab style
    44  func ForceMount(device, target, mType, options string) error {
    45  	flag, data := parseOptions(options)
    46  	if err := mount(device, target, mType, uintptr(flag), data); err != nil {
    47  		return err
    48  	}
    49  	return nil
    50  }
    51  
    52  // Unmount the target only if it is mounted
    53  func Unmount(target string) error {
    54  	if mounted, err := Mounted(target); err != nil || !mounted {
    55  		return err
    56  	}
    57  	return ForceUnmount(target)
    58  }
    59  
    60  // Unmount the target reguardless if it is mounted or not
    61  func ForceUnmount(target string) (err error) {
    62  	// Simple retry logic for unmount
    63  	for i := 0; i < 10; i++ {
    64  		if err = unmount(target, 0); err == nil {
    65  			return nil
    66  		}
    67  		time.Sleep(100 * time.Millisecond)
    68  	}
    69  	return
    70  }