github.com/stevegt/moby@v1.13.1/pkg/mount/mount.go (about)

     1  package mount
     2  
     3  import (
     4  	"time"
     5  )
     6  
     7  // GetMounts retrieves a list of mounts for the current running process.
     8  func GetMounts() ([]*Info, error) {
     9  	return parseMountTable()
    10  }
    11  
    12  // Mounted determines if a specified mountpoint has been mounted.
    13  // On Linux it looks at /proc/self/mountinfo and on Solaris at mnttab.
    14  func Mounted(mountpoint string) (bool, error) {
    15  	entries, err := parseMountTable()
    16  	if err != nil {
    17  		return false, err
    18  	}
    19  
    20  	// Search the table for the mountpoint
    21  	for _, e := range entries {
    22  		if e.Mountpoint == mountpoint {
    23  			return true, nil
    24  		}
    25  	}
    26  	return false, nil
    27  }
    28  
    29  // Mount will mount filesystem according to the specified configuration, on the
    30  // condition that the target path is *not* already mounted. Options must be
    31  // specified like the mount or fstab unix commands: "opt1=val1,opt2=val2". See
    32  // flags.go for supported option flags.
    33  func Mount(device, target, mType, options string) error {
    34  	flag, _ := parseOptions(options)
    35  	if flag&REMOUNT != REMOUNT {
    36  		if mounted, err := Mounted(target); err != nil || mounted {
    37  			return err
    38  		}
    39  	}
    40  	return ForceMount(device, target, mType, options)
    41  }
    42  
    43  // ForceMount will mount a filesystem according to the specified configuration,
    44  // *regardless* if the target path is not already mounted. Options must be
    45  // specified like the mount or fstab unix commands: "opt1=val1,opt2=val2". See
    46  // flags.go for supported option flags.
    47  func ForceMount(device, target, mType, options string) error {
    48  	flag, data := parseOptions(options)
    49  	if err := mount(device, target, mType, uintptr(flag), data); err != nil {
    50  		return err
    51  	}
    52  	return nil
    53  }
    54  
    55  // Unmount will unmount the target filesystem, so long as it is mounted.
    56  func Unmount(target string) error {
    57  	if mounted, err := Mounted(target); err != nil || !mounted {
    58  		return err
    59  	}
    60  	return ForceUnmount(target)
    61  }
    62  
    63  // ForceUnmount will force an unmount of the target filesystem, regardless if
    64  // it is mounted or not.
    65  func ForceUnmount(target string) (err error) {
    66  	// Simple retry logic for unmount
    67  	for i := 0; i < 10; i++ {
    68  		if err = unmount(target, 0); err == nil {
    69  			return nil
    70  		}
    71  		time.Sleep(100 * time.Millisecond)
    72  	}
    73  	return
    74  }