forked from goss-org/goss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mount.go
103 lines (84 loc) · 1.73 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package system
import (
"fmt"
"strings"
"github.com/aelsabbahy/goss/util"
"github.com/docker/docker/pkg/mount"
)
type Mount interface {
MountPoint() string
Exists() (bool, error)
Opts() ([]string, error)
Source() (string, error)
Filesystem() (string, error)
}
type DefMount struct {
mountPoint string
loaded bool
exists bool
mountInfo *mount.Info
err error
}
func NewDefMount(mountPoint string, system *System, config util.Config) Mount {
return &DefMount{
mountPoint: mountPoint,
}
}
func (m *DefMount) setup() error {
if m.loaded {
return m.err
}
m.loaded = true
mountInfo, err := getMount(m.mountPoint)
if err != nil {
m.exists = false
m.err = err
return m.err
}
m.mountInfo = mountInfo
m.exists = true
return nil
}
func (m *DefMount) ID() string {
return m.mountPoint
}
func (m *DefMount) MountPoint() string {
return m.mountPoint
}
func (m *DefMount) Exists() (bool, error) {
if err := m.setup(); err != nil {
return false, nil
}
return m.exists, nil
}
func (m *DefMount) Opts() ([]string, error) {
if err := m.setup(); err != nil {
return nil, err
}
return strings.Split(m.mountInfo.Opts, ","), nil
}
func (m *DefMount) Source() (string, error) {
if err := m.setup(); err != nil {
return "", err
}
return m.mountInfo.Source, nil
}
func (m *DefMount) Filesystem() (string, error) {
if err := m.setup(); err != nil {
return "", err
}
return m.mountInfo.Fstype, nil
}
func getMount(mountpoint string) (*mount.Info, error) {
entries, err := mount.GetMounts()
if err != nil {
return nil, err
}
// Search the table for the mountpoint
for _, e := range entries {
if e.Mountpoint == mountpoint {
return e, nil
}
}
return nil, fmt.Errorf("Mountpoint not found")
}