/
rbd_executor.go
207 lines (168 loc) · 4.51 KB
/
rbd_executor.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package executor
import (
"bufio"
"bytes"
"net"
"os/exec"
"strings"
gofig "github.com/akutz/gofig/types"
"github.com/akutz/goof"
"github.com/akutz/gotil"
"github.com/codedellemc/rexray/libstorage/api/context"
"github.com/codedellemc/rexray/libstorage/api/registry"
"github.com/codedellemc/rexray/libstorage/api/types"
"github.com/codedellemc/rexray/libstorage/drivers/storage/rbd"
"github.com/codedellemc/rexray/libstorage/drivers/storage/rbd/utils"
)
type driver struct {
config gofig.Config
doModprobe bool
}
func init() {
registry.RegisterStorageExecutor(rbd.Name, newdriver)
}
func newdriver() types.StorageExecutor {
return &driver{}
}
func (d *driver) Init(context types.Context, config gofig.Config) error {
d.config = config
d.doModprobe = config.GetBool(rbd.ConfigTestModule)
return nil
}
func (d *driver) Name() string {
return rbd.Name
}
func (d *driver) Supported(
ctx types.Context,
opts types.Store) (bool, error) {
if !gotil.FileExistsInPath("ceph") {
return false, nil
}
if !gotil.FileExistsInPath("rbd") {
return false, nil
}
if !gotil.FileExistsInPath("ip") {
return false, nil
}
if d.doModprobe {
cmd := exec.Command("modprobe", "rbd")
if _, _, err := utils.RunCommand(ctx, cmd); err != nil {
return false, nil
}
}
return true, nil
}
// NextDevice returns the next available device.
func (d *driver) NextDevice(
ctx types.Context,
opts types.Store) (string, error) {
return "", types.ErrNotImplemented
}
// LocalDevices returns a map of the system's local devices.
func (d *driver) LocalDevices(
ctx types.Context,
opts *types.LocalDevicesOpts) (*types.LocalDevices, error) {
devMap, err := utils.GetMappedRBDs(ctx)
if err != nil {
return nil, err
}
ld := &types.LocalDevices{Driver: d.Name()}
if len(devMap) > 0 {
ld.DeviceMap = devMap
}
return ld, nil
}
// InstanceID returns the local system's InstanceID.
func (d *driver) InstanceID(
ctx types.Context,
opts types.Store) (*types.InstanceID, error) {
return GetInstanceID(ctx, nil, nil)
}
// GetInstanceID returns the instance ID object
func GetInstanceID(
ctx types.Context,
monIPs []net.IP,
localIntfs []net.Addr) (*types.InstanceID, error) {
/* Ceph doesn't have only one unique identifier per client, it can have
several. With the way the RBD driver is used, we will see multiple
identifiers used, and therefore returning any of those identifiers
is actually confusing rather than helpful. Instead, we use the client
IP address that is on the interface that can reach the monitors.
We loop through all the monitor IPs, looking for a local interface
that is on the same L2 segment. If these all fail, We are on an L3
segment so we grab the IP from the default route.
*/
if ctx == nil {
ctx = context.Background()
}
var err error
if nil == monIPs {
monIPs, err = getCephMonIPs(ctx)
if err != nil {
return nil, err
}
}
if len(monIPs) == 0 {
return nil, goof.New("no ceph monitors found")
}
if nil == localIntfs {
localIntfs, err = net.InterfaceAddrs()
if err != nil {
return nil, err
}
}
iid := &types.InstanceID{Driver: rbd.Name}
for _, intf := range localIntfs {
localIP, localNet, _ := net.ParseCIDR(intf.String())
for _, monIP := range monIPs {
if localNet.Contains(monIP) {
// Monitor reachable over L2
iid.ID = localIP.String()
return iid, nil
}
}
}
// No luck finding L2 match, check for default/static route to monitor
localIP, err := getSrcIP(ctx, monIPs[0].String())
if err != nil {
return nil, err
}
iid.ID = localIP
return iid, nil
}
func getCephMonIPs(ctx types.Context) ([]net.IP, error) {
cmd := exec.Command("ceph-conf", "--lookup", "mon_host")
out, _, err := utils.RunCommand(ctx, cmd)
if err != nil {
return nil, goof.WithError("unable to get ceph monitors", err)
}
monStrings := strings.Split(strings.TrimSpace(string(out)), ",")
monIps, err := utils.ParseMonitorAddresses(monStrings)
if err != nil {
return nil, err
}
return monIps, nil
}
func getSrcIP(
ctx types.Context,
destIP string) (string, error) {
cmd := exec.Command("ip", "-oneline", "route", "get", destIP)
out, _, err := utils.RunCommand(ctx, cmd)
if err != nil {
return "", goof.WithError("unable get ip routes", err)
}
byteReader := bytes.NewReader(out)
scanner := bufio.NewScanner(byteReader)
scanner.Split(bufio.ScanWords)
found := false
for scanner.Scan() {
if !found {
if scanner.Text() == "src" {
found = true
continue
}
}
return scanner.Text(), nil
}
return "", goof.New("unable to parse ip output")
}