github.com/fabiokung/docker@v0.11.2-0.20170222101415-4534dcd49497/pkg/mount/mounter_linux.go (about)

     1  package mount
     2  
     3  import (
     4  	"syscall"
     5  )
     6  
     7  const (
     8  	// ptypes is the set propagation types.
     9  	ptypes = syscall.MS_SHARED | syscall.MS_PRIVATE | syscall.MS_SLAVE | syscall.MS_UNBINDABLE
    10  
    11  	// pflags is the full set valid flags for a change propagation call.
    12  	pflags = ptypes | syscall.MS_REC | syscall.MS_SILENT
    13  
    14  	// broflags is the combination of bind and read only
    15  	broflags = syscall.MS_BIND | syscall.MS_RDONLY
    16  )
    17  
    18  // isremount returns true if either device name or flags identify a remount request, false otherwise.
    19  func isremount(device string, flags uintptr) bool {
    20  	switch {
    21  	// We treat device "" and "none" as a remount request to provide compatibility with
    22  	// requests that don't explicitly set MS_REMOUNT such as those manipulating bind mounts.
    23  	case flags&syscall.MS_REMOUNT != 0, device == "", device == "none":
    24  		return true
    25  	default:
    26  		return false
    27  	}
    28  }
    29  
    30  func mount(device, target, mType string, flags uintptr, data string) error {
    31  	oflags := flags &^ ptypes
    32  	if !isremount(device, flags) {
    33  		// Initial call applying all non-propagation flags.
    34  		if err := syscall.Mount(device, target, mType, oflags, data); err != nil {
    35  			return err
    36  		}
    37  	}
    38  
    39  	if flags&ptypes != 0 {
    40  		// Change the propagation type.
    41  		if err := syscall.Mount("", target, "", flags&pflags, ""); err != nil {
    42  			return err
    43  		}
    44  	}
    45  
    46  	if oflags&broflags == broflags {
    47  		// Remount the bind to apply read only.
    48  		return syscall.Mount("", target, "", oflags|syscall.MS_REMOUNT, "")
    49  	}
    50  
    51  	return nil
    52  }
    53  
    54  func unmount(target string, flag int) error {
    55  	return syscall.Unmount(target, flag)
    56  }