/
ebs_executor.go
220 lines (184 loc) · 5.5 KB
/
ebs_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
208
209
210
211
212
213
214
215
216
217
218
219
220
package executor
import (
"bufio"
"bytes"
"fmt"
"math/rand"
"os"
"path"
"regexp"
"strings"
"time"
gofig "github.com/akutz/gofig/types"
"github.com/akutz/goof"
log "github.com/sirupsen/logrus"
"github.com/codedellemc/rexray/libstorage/api/registry"
"github.com/codedellemc/rexray/libstorage/api/types"
"github.com/codedellemc/rexray/libstorage/drivers/storage/ebs"
ebsUtils "github.com/codedellemc/rexray/libstorage/drivers/storage/ebs/utils"
)
// driver is the storage executor for the ec2 storage driver.
type driver struct {
name string
config gofig.Config
deviceRange *ebsUtils.DeviceRange
}
func init() {
registry.RegisterStorageExecutor(ebs.Name, newDriver)
// backwards compatibility for ec2 executor
registry.RegisterStorageExecutor(ebs.NameEC2, newEC2Driver)
}
func newDriver() types.StorageExecutor {
return &driver{name: ebs.Name}
}
func newEC2Driver() types.StorageExecutor {
return &driver{name: ebs.NameEC2}
}
func (d *driver) Init(ctx types.Context, config gofig.Config) error {
// ensure backwards compatibility with ebs and ec2 in config
ebs.BackCompat(config)
d.config = config
// initialize device range config
useLargeDeviceRange := d.config.GetBool(ebs.ConfigUseLargeDeviceRange)
log.Debug("executor using large device range: ", useLargeDeviceRange)
d.deviceRange = ebsUtils.GetDeviceRange(useLargeDeviceRange)
return nil
}
func (d *driver) Name() string {
return d.name
}
// Supported returns a flag indicating whether or not the platform
// implementing the executor is valid for the host on which the executor
// resides.
func (d *driver) Supported(
ctx types.Context,
opts types.Store) (bool, error) {
return ebsUtils.IsEC2Instance(ctx)
}
// InstanceID returns the instance ID from the current instance from metadata
func (d *driver) InstanceID(
ctx types.Context,
opts types.Store) (*types.InstanceID, error) {
return ebsUtils.InstanceID(ctx, d.Name())
}
var errNoAvaiDevice = goof.New("no available device")
// NextDevice returns the next available device.
func (d *driver) NextDevice(
ctx types.Context,
opts types.Store) (string, error) {
// Find which letters are used for local devices
localDeviceNames := make(map[string]bool)
// Get device range
ns := d.deviceRange
localDevices, err := d.LocalDevices(
ctx, &types.LocalDevicesOpts{Opts: opts})
if err != nil {
return "", goof.WithError("error getting local devices", err)
}
localDeviceMapping := localDevices.DeviceMap
for localDevice := range localDeviceMapping {
re, _ := regexp.Compile(`^/dev/` +
ns.NextDeviceInfo.Prefix +
`(` + ns.NextDeviceInfo.Pattern + `)`)
res := re.FindStringSubmatch(localDevice)
if len(res) > 0 {
localDeviceNames[res[1]] = true
}
}
// Find which letters are used for ephemeral devices
ephemeralDevices, err := d.getEphemeralDevices(ctx)
if err != nil {
return "", goof.WithError("error getting ephemeral devices", err)
}
for _, ephemeralDevice := range ephemeralDevices {
re, _ := regexp.Compile(`^` +
ns.NextDeviceInfo.Prefix +
`(` + ns.NextDeviceInfo.Pattern + `)`)
res := re.FindStringSubmatch(ephemeralDevice)
if len(res) > 0 {
localDeviceNames[res[1]] = true
}
}
// Find next available letter for device path.
// Device namespace is iterated in random order
// to mitigate ghost device issues.
r := rand.New(rand.NewSource(time.Now().UnixNano()))
parentLength := len(ns.ParentLetters)
for _, pIndex := range r.Perm(parentLength) {
parentSuffix := ""
if parentLength > 1 {
parentSuffix = ns.ParentLetters[pIndex]
}
for _, cIndex := range r.Perm(len(ns.ChildLetters)) {
suffix := parentSuffix + ns.ChildLetters[cIndex]
if localDeviceNames[suffix] {
continue
}
return fmt.Sprintf(
"/dev/%s%s", ns.NextDeviceInfo.Prefix, suffix), nil
}
}
return "", errNoAvaiDevice
}
const procPartitions = "/proc/partitions"
// Retrieve device paths currently attached and/or mounted
func (d *driver) LocalDevices(
ctx types.Context,
opts *types.LocalDevicesOpts) (*types.LocalDevices, error) {
f, err := os.Open(procPartitions)
if err != nil {
return nil, goof.WithError("error reading "+procPartitions, err)
}
defer f.Close()
devMap := map[string]string{}
ns := d.deviceRange
scanner := bufio.NewScanner(f)
for scanner.Scan() {
fields := strings.Fields(scanner.Text())
if len(fields) != 4 {
continue
}
devName := fields[3]
if !ns.DeviceRE.MatchString(devName) {
continue
}
devPath := path.Join("/dev/", devName)
devMap[devPath] = devPath
}
ld := &types.LocalDevices{Driver: d.Name()}
if len(devMap) > 0 {
ld.DeviceMap = devMap
}
return ld, nil
}
var ephemDevRX = regexp.MustCompile(`ephemeral([0-9]|1[0-9]|2[0-3])$`)
// Find ephemeral devices from metadata
func (d *driver) getEphemeralDevices(
ctx types.Context) (deviceNames []string, err error) {
buf, err := ebsUtils.BlockDevices(ctx)
if err != nil {
return nil, err
}
// Filter list of all block devices for ephemeral devices
scanner := bufio.NewScanner(bytes.NewReader(buf))
scanner.Split(bufio.ScanWords)
for scanner.Scan() {
word := scanner.Bytes()
if !ephemDevRX.Match(word) {
continue
}
name, err := ebsUtils.BlockDeviceName(ctx, string(word))
if err != nil {
return nil, goof.WithError(
"ec2 block device mapping lookup failed", err)
}
// compensate for kernel volume mapping i.e. change "/dev/sda" to
// "/dev/xvda"
deviceNameStr := strings.Replace(
string(name),
"sd",
d.deviceRange.NextDeviceInfo.Prefix, 1)
deviceNames = append(deviceNames, deviceNameStr)
}
return deviceNames, nil
}