1- // +build !windows
1+ // +build !darwin,! windows
22
33package mount
44
@@ -10,7 +10,37 @@ import (
1010 "golang.org/x/sys/unix"
1111)
1212
13- func recursiveUnmount (target string ) error {
13+ // Mount will mount filesystem according to the specified configuration.
14+ // Options must be specified like the mount or fstab unix commands:
15+ // "opt1=val1,opt2=val2". See flags.go for supported option flags.
16+ func Mount (device , target , mType , options string ) error {
17+ flag , data := parseOptions (options )
18+ return mount (device , target , mType , uintptr (flag ), data )
19+ }
20+
21+ // Unmount lazily unmounts a filesystem on supported platforms, otherwise does
22+ // a normal unmount. If target is not a mount point, no error is returned.
23+ func Unmount (target string ) error {
24+ err := unix .Unmount (target , mntDetach )
25+ if err == nil || err == unix .EINVAL {
26+ // Ignore "not mounted" error here. Note the same error
27+ // can be returned if flags are invalid, so this code
28+ // assumes that the flags value is always correct.
29+ return nil
30+ }
31+
32+ return & mountError {
33+ op : "umount" ,
34+ target : target ,
35+ flags : uintptr (mntDetach ),
36+ err : err ,
37+ }
38+ }
39+
40+ // RecursiveUnmount unmounts the target and all mounts underneath, starting
41+ // with the deepest mount first. The argument does not have to be a mount
42+ // point itself.
43+ func RecursiveUnmount (target string ) error {
1444 // Fast path, works if target is a mount point that can be unmounted.
1545 // On Linux, mntDetach flag ensures a recursive unmount. For other
1646 // platforms, if there are submounts, we'll get EBUSY (and fall back
@@ -33,7 +63,7 @@ func recursiveUnmount(target string) error {
3363
3464 var suberr error
3565 for i , m := range mounts {
36- err = unmount (m .Mountpoint , mntDetach )
66+ err = Unmount (m .Mountpoint )
3767 if err != nil {
3868 if i == len (mounts )- 1 { // last mount
3969 return fmt .Errorf ("%w (possible cause: %s)" , err , suberr )
@@ -49,20 +79,3 @@ func recursiveUnmount(target string) error {
4979 }
5080 return nil
5181}
52-
53- func unmount (target string , flags int ) error {
54- err := unix .Unmount (target , flags )
55- if err == nil || err == unix .EINVAL {
56- // Ignore "not mounted" error here. Note the same error
57- // can be returned if flags are invalid, so this code
58- // assumes that the flags value is always correct.
59- return nil
60- }
61-
62- return & mountError {
63- op : "umount" ,
64- target : target ,
65- flags : uintptr (flags ),
66- err : err ,
67- }
68- }
0 commit comments