2013-12-18 19:42:49 -05:00
|
|
|
package mount
|
|
|
|
|
|
|
|
import (
|
2017-05-23 10:22:32 -04:00
|
|
|
"golang.org/x/sys/unix"
|
2013-12-18 19:42:49 -05:00
|
|
|
)
|
|
|
|
|
2017-01-24 12:44:00 -05:00
|
|
|
const (
|
|
|
|
// ptypes is the set propagation types.
|
2017-05-23 10:22:32 -04:00
|
|
|
ptypes = unix.MS_SHARED | unix.MS_PRIVATE | unix.MS_SLAVE | unix.MS_UNBINDABLE
|
2017-01-24 12:44:00 -05:00
|
|
|
|
|
|
|
// pflags is the full set valid flags for a change propagation call.
|
2017-05-23 10:22:32 -04:00
|
|
|
pflags = ptypes | unix.MS_REC | unix.MS_SILENT
|
2017-01-24 12:44:00 -05:00
|
|
|
|
|
|
|
// broflags is the combination of bind and read only
|
2017-05-23 10:22:32 -04:00
|
|
|
broflags = unix.MS_BIND | unix.MS_RDONLY
|
2017-01-24 12:44:00 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// isremount returns true if either device name or flags identify a remount request, false otherwise.
|
|
|
|
func isremount(device string, flags uintptr) bool {
|
|
|
|
switch {
|
|
|
|
// We treat device "" and "none" as a remount request to provide compatibility with
|
|
|
|
// requests that don't explicitly set MS_REMOUNT such as those manipulating bind mounts.
|
2017-05-23 10:22:32 -04:00
|
|
|
case flags&unix.MS_REMOUNT != 0, device == "", device == "none":
|
2017-01-24 12:44:00 -05:00
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func mount(device, target, mType string, flags uintptr, data string) error {
|
|
|
|
oflags := flags &^ ptypes
|
2017-07-12 10:17:02 -04:00
|
|
|
if !isremount(device, flags) || data != "" {
|
|
|
|
// Initial call applying all non-propagation flags for mount
|
|
|
|
// or remount with changed data
|
2017-05-23 10:22:32 -04:00
|
|
|
if err := unix.Mount(device, target, mType, oflags, data); err != nil {
|
2017-01-24 12:44:00 -05:00
|
|
|
return err
|
|
|
|
}
|
2014-01-14 17:28:19 -05:00
|
|
|
}
|
|
|
|
|
2017-01-24 12:44:00 -05:00
|
|
|
if flags&ptypes != 0 {
|
|
|
|
// Change the propagation type.
|
2017-05-23 10:22:32 -04:00
|
|
|
if err := unix.Mount("", target, "", flags&pflags, ""); err != nil {
|
2017-01-24 12:44:00 -05:00
|
|
|
return err
|
|
|
|
}
|
2014-01-14 17:28:19 -05:00
|
|
|
}
|
2017-01-24 12:44:00 -05:00
|
|
|
|
|
|
|
if oflags&broflags == broflags {
|
|
|
|
// Remount the bind to apply read only.
|
2017-05-23 10:22:32 -04:00
|
|
|
return unix.Mount("", target, "", oflags|unix.MS_REMOUNT, "")
|
2017-01-24 12:44:00 -05:00
|
|
|
}
|
|
|
|
|
2014-01-14 17:28:19 -05:00
|
|
|
return nil
|
2013-12-18 19:42:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func unmount(target string, flag int) error {
|
2017-05-23 10:22:32 -04:00
|
|
|
return unix.Unmount(target, flag)
|
2013-12-18 19:42:49 -05:00
|
|
|
}
|