forked from branden/rexray
-
Notifications
You must be signed in to change notification settings - Fork 1
/
drivers_os.go
160 lines (137 loc) · 3.54 KB
/
drivers_os.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package core
import (
"bytes"
"strings"
log "github.com/Sirupsen/logrus"
"github.com/docker/docker/pkg/mount"
"github.com/emccode/rexray/core/errors"
)
// MountInfoArray is an alias of []*mount.Info
type MountInfoArray []*mount.Info
// OSDriver is the interface implemented by types that provide OS introspection
// and management.
type OSDriver interface {
Driver
// Shows the existing mount points
GetMounts(string, string) (MountInfoArray, error)
// Check whether path is mounted or not
Mounted(string) (bool, error)
// Unmount based on a path
Unmount(string) error
// Mount based on a device, target, options, label
Mount(string, string, string, string) error
// Format a device with a FS type
Format(string, string, bool) error
}
// OSDriverManager acts as both a OSDriverManager and as an aggregate of OS
// drivers, providing batch methods.
type OSDriverManager interface {
OSDriver
// Drivers gets a channel which receives a list of all of the configured
// OS drivers.
Drivers() <-chan OSDriver
}
type odm struct {
rexray *RexRay
drivers map[string]OSDriver
}
func (r *odm) Init(rexray *RexRay) error {
if len(r.drivers) == 0 {
return errors.ErrNoOSDrivers
}
return nil
}
func (r *odm) Name() string {
var b bytes.Buffer
for d := range r.Drivers() {
if b.Len() > 0 {
b.WriteString(" ")
}
b.WriteString(d.Name())
}
return b.String()
}
func (r *odm) Drivers() <-chan OSDriver {
c := make(chan OSDriver)
go func() {
if len(r.drivers) == 0 {
close(c)
return
}
for _, v := range r.drivers {
c <- v
}
close(c)
}()
return c
}
func (r *odm) GetMounts(
deviceName, mountPoint string) (MountInfoArray, error) {
for _, d := range r.drivers {
log.WithFields(log.Fields{
"moduleName": r.rexray.Context,
"deviceName": deviceName,
"mountPoint": mountPoint,
"driverName": d.Name()}).Info("odm.GetMounts")
mounts, err := d.GetMounts(deviceName, mountPoint)
if err != nil {
return nil, err
}
return mounts, nil
}
return nil, errors.ErrNoOSDetected
}
func (r *odm) Mounted(mountPoint string) (bool, error) {
for _, d := range r.drivers {
log.WithFields(log.Fields{
"moduleName": r.rexray.Context,
"mountPoint": mountPoint,
"driverName": d.Name()}).Info("odm.Mounted")
return d.Mounted(mountPoint)
}
return false, errors.ErrNoOSDetected
}
func (r *odm) Unmount(mountPoint string) error {
for _, d := range r.drivers {
log.WithFields(log.Fields{
"moduleName": r.rexray.Context,
"mountPoint": mountPoint,
"driverName": d.Name()}).Info("odm.Unmount")
return d.Unmount(mountPoint)
}
return errors.ErrNoOSDetected
}
func (r *odm) Mount(
device, target, mountOptions, mountLabel string) error {
for _, d := range r.drivers {
log.WithFields(log.Fields{
"moduleName": r.rexray.Context,
"device": device,
"target": target,
"mountOptions": mountOptions,
"mountLabel": mountLabel,
"driverName": d.Name()}).Info("odm.Mount")
return d.Mount(device, target, mountOptions, mountLabel)
}
return errors.ErrNoOSDetected
}
func (r *odm) isNfsDevice(device string) bool {
return strings.Contains(device, ":")
}
func (r *odm) Format(
deviceName, fsType string, overwriteFs bool) error {
for _, d := range r.drivers {
log.WithFields(log.Fields{
"moduleName": r.rexray.Context,
"deviceName": deviceName,
"fsType": fsType,
"overwriteFs": overwriteFs,
"driverName": d.Name()}).Info(
"odm.Format")
if r.isNfsDevice(deviceName) {
return nil
}
return d.Format(deviceName, fsType, overwriteFs)
}
return errors.ErrNoOSDetected
}