| 1 | //go:build !darwin && !windows |
| 2 | |
| 3 | package mount |
| 4 | |
| 5 | import ( |
| 6 | "fmt" |
| 7 | "sort" |
| 8 | |
| 9 | "github.com/moby/sys/mountinfo" |
| 10 | "golang.org/x/sys/unix" |
| 11 | ) |
| 12 | |
| 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 { |
| 44 | // Fast path, works if target is a mount point that can be unmounted. |
| 45 | // On Linux, mntDetach flag ensures a recursive unmount. For other |
| 46 | // platforms, if there are submounts, we'll get EBUSY (and fall back |
| 47 | // to the slow path). NOTE we do not ignore EINVAL here as target might |
| 48 | // not be a mount point itself (but there can be mounts underneath). |
| 49 | if err := unix.Unmount(target, mntDetach); err == nil { |
| 50 | return nil |
| 51 | } |
| 52 | |
| 53 | // Slow path: get all submounts, sort, unmount one by one. |
| 54 | mounts, err := mountinfo.GetMounts(mountinfo.PrefixFilter(target)) |
| 55 | if err != nil { |
| 56 | return err |
| 57 | } |
| 58 | |
| 59 | // Make the deepest mount be first |
| 60 | sort.Slice(mounts, func(i, j int) bool { |
| 61 | return len(mounts[i].Mountpoint) > len(mounts[j].Mountpoint) |
| 62 | }) |
| 63 | |
| 64 | var ( |
| 65 | suberr error |
| 66 | lastMount = len(mounts) - 1 |
| 67 | ) |
| 68 | for i, m := range mounts { |
| 69 | err = Unmount(m.Mountpoint) |
| 70 | if err != nil { |
| 71 | if i == lastMount { |
| 72 | if suberr != nil { |
| 73 | // TODO: switch to %w for suberr once we stop supporting go < 1.20. |
| 74 | return fmt.Errorf("%w (possible cause: %s)", err, suberr.Error()) |
| 75 | } |
| 76 | return err |
| 77 | } |
| 78 | // This is a submount, we can ignore the error for now, |
| 79 | // the final unmount will fail if this is a real problem. |
| 80 | // With that in mind, the _first_ failed unmount error |
| 81 | // might be the real error cause, so let's keep it. |
| 82 | if suberr == nil { |
| 83 | suberr = err |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | return nil |
| 88 | } |