-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathmount.go
60 lines (46 loc) · 1.18 KB
/
mount.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package mount
import (
"errors"
"os/exec"
)
func Mount(source string, mountpoint string, fstype *string, options *string) error {
args := []string{"--verbose"}
if fstype != nil && *fstype != "" {
args = append(args, "-t", *fstype)
}
if options != nil && *options != "" {
args = append(args, "-o", *options)
}
args = append(args, source, mountpoint)
if _, err := executeCommand("mount", args...); err != nil {
return err
}
return nil
}
func UmountByMountPoint(mountpoint string) error {
if _, err := executeCommand("umount", "--force", "--verbose", "--quiet", mountpoint); err != nil {
return err
}
return nil
}
func UmountByDevice(device string) error {
if _, err := executeCommand("umount", "--force", "--verbose", "--quiet", "--recursive", device); err != nil {
return err
}
return nil
}
func executeCommand(name string, arg ...string) ([]byte, error) {
cmd := exec.Command(name, arg...)
println(cmd.String())
out, err := cmd.Output()
println(string(out))
if err != nil {
if exitError, ok := err.(*exec.ExitError); ok {
message := string(exitError.Stderr)
println(message)
return nil, errors.New(message)
}
return nil, err
}
return out, nil
}