forked from bakins/zfs-flex-volume
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
61 lines (51 loc) · 1.01 KB
/
utils.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
61
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
type (
mount struct {
device string
mountpoint string
filesystem string
}
)
// parse mounts into an array of mounts
// only works on linux
func parseMounts() ([]*mount, error) {
in, err := os.Open("/proc/mounts")
if err != nil {
return nil, err
}
defer in.Close()
var mounts []*mount
s := bufio.NewScanner(in)
for s.Scan() {
parts := strings.Split(s.Text(), " ")
if len(parts) < 3 {
continue
}
mounts = append(mounts, &mount{device: parts[0], mountpoint: parts[1], filesystem: parts[2]})
}
if err := s.Err(); err != nil {
return nil, err
}
return mounts, nil
}
func isMounted(device, mountpoint string) (bool, error) {
mounts, err := parseMounts()
if err != nil {
return false, err
}
for _, m := range mounts {
if m.device == device && m.mountpoint == mountpoint {
if m.filesystem != "zfs" {
return false, fmt.Errorf("unexpected filesystem: %s", m.filesystem)
}
return true, nil
}
}
return false, nil
}