-
Notifications
You must be signed in to change notification settings - Fork 327
/
devices.go
152 lines (124 loc) · 3.49 KB
/
devices.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
package utils
import (
"bufio"
"bytes"
"fmt"
"os"
"path"
"regexp"
"strings"
"github.com/akutz/goof"
"github.com/rexray/rexray/libstorage/api/types"
"github.com/rexray/rexray/libstorage/drivers/storage/fittedcloud"
)
var errNoAvaiDevice = goof.New("no available device")
// NextDevice returns the next available device.
func NextDevice(
ctx types.Context,
opts types.Store) (string, error) {
// All possible device paths on Linux EC2 instances are /dev/xvd[f-p]
letters := []string{
"f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p"}
// Find which letters are used for local devices
localDeviceNames := make(map[string]bool)
localDevices, err := 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/` +
NextDeviceInfo.Prefix +
`(` + NextDeviceInfo.Pattern + `)`)
res := re.FindStringSubmatch(localDevice)
if len(res) > 0 {
localDeviceNames[res[1]] = true
}
}
// Find which letters are used for ephemeral devices
ephemeralDevices, err := getEphemeralDevices(ctx)
if err != nil {
return "", goof.WithError("error getting ephemeral devices", err)
}
for _, ephemeralDevice := range ephemeralDevices {
re, _ := regexp.Compile(`^` +
NextDeviceInfo.Prefix +
`(` + NextDeviceInfo.Pattern + `)`)
res := re.FindStringSubmatch(ephemeralDevice)
if len(res) > 0 {
localDeviceNames[res[1]] = true
}
}
// Find next available letter for device path
for _, letter := range letters {
if localDeviceNames[letter] {
continue
}
return fmt.Sprintf(
"/dev/%s%s", NextDeviceInfo.Prefix, letter), nil
}
return "", errNoAvaiDevice
}
const procPartitions = "/proc/partitions"
var xvdRX = regexp.MustCompile(`^xvd[a-z]$`)
// LocalDevices retrieves device paths currently attached and/or mounted
func 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{}
scanner := bufio.NewScanner(f)
for scanner.Scan() {
fields := strings.Fields(scanner.Text())
if len(fields) != 4 {
continue
}
devName := fields[3]
if !xvdRX.MatchString(devName) {
continue
}
devPath := path.Join("/dev/", devName)
devMap[devPath] = devPath
}
ld := &types.LocalDevices{Driver: fittedcloud.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 getEphemeralDevices(
ctx types.Context) (deviceNames []string, err error) {
buf, err := 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 := 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",
NextDeviceInfo.Prefix, 1)
deviceNames = append(deviceNames, deviceNameStr)
}
return deviceNames, nil
}