-
Notifications
You must be signed in to change notification settings - Fork 19
/
filesystems_linux.go
71 lines (58 loc) · 1.51 KB
/
filesystems_linux.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
62
63
64
65
66
67
68
69
70
71
package filesystems
import (
"bufio"
"fmt"
"os"
"strings"
"syscall"
errorspkg "github.com/pkg/errors"
)
const (
XfsType = int64(0x58465342)
BtrfsType = int64(0x9123683E)
)
func CheckFSPath(path string, filesystem string, mountOptions ...string) error {
statfs := syscall.Statfs_t{}
err := syscall.Statfs(path, &statfs)
if err != nil {
return errorspkg.Wrapf(err, "Failed to detect type of filesystem")
}
fsType, err := filesystemCode(filesystem)
if err != nil {
return err
}
if statfs.Type != fsType {
return errorspkg.Errorf("Store path filesystem (%s) is incompatible with requested driver", path)
}
return checkMountOptions(path, filesystem, mountOptions...)
}
func checkMountOptions(path, filesystem string, options ...string) error {
mounts, err := os.Open("/proc/mounts")
if err != nil {
return errorspkg.Errorf("Failed to open /proc/mounts: %s", err.Error())
}
scanner := bufio.NewScanner(mounts)
for scanner.Scan() {
mountPoint := scanner.Text()
if !strings.Contains(mountPoint, fmt.Sprintf("%s %s", filesystem, path)) {
continue
}
for _, option := range options {
if !strings.Contains(mountPoint, option) {
return errorspkg.Errorf("'%s' option missing at the mount point '%s'", option, mountPoint)
}
}
return nil
}
return nil
}
func filesystemCode(filesystem string) (int64, error) {
switch filesystem {
case "xfs":
return XfsType, nil
case "btrfs":
return BtrfsType, nil
default:
return 0, errorspkg.Errorf("filesystem %s is not supported", filesystem)
}
}