forked from cloudfoundry-attic/bosh-init
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapped_device_path_resolver.go
61 lines (46 loc) · 1.54 KB
/
mapped_device_path_resolver.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
package devicepathresolver
import (
"strings"
"time"
boshsettings "github.com/cloudfoundry/bosh-init/internal/github.com/cloudfoundry/bosh-agent/settings"
bosherr "github.com/cloudfoundry/bosh-init/internal/github.com/cloudfoundry/bosh-utils/errors"
boshsys "github.com/cloudfoundry/bosh-init/internal/github.com/cloudfoundry/bosh-utils/system"
)
type mappedDevicePathResolver struct {
diskWaitTimeout time.Duration
fs boshsys.FileSystem
}
func NewMappedDevicePathResolver(
diskWaitTimeout time.Duration,
fs boshsys.FileSystem,
) DevicePathResolver {
return mappedDevicePathResolver{fs: fs, diskWaitTimeout: diskWaitTimeout}
}
func (dpr mappedDevicePathResolver) GetRealDevicePath(diskSettings boshsettings.DiskSettings) (string, bool, error) {
stopAfter := time.Now().Add(dpr.diskWaitTimeout)
devicePath := diskSettings.Path
realPath, found := dpr.findPossibleDevice(devicePath)
for !found {
if time.Now().After(stopAfter) {
return "", true, bosherr.Errorf("Timed out getting real device path for %s", devicePath)
}
time.Sleep(100 * time.Millisecond)
realPath, found = dpr.findPossibleDevice(devicePath)
}
return realPath, false, nil
}
func (dpr mappedDevicePathResolver) findPossibleDevice(devicePath string) (string, bool) {
pathSuffix := strings.Split(devicePath, "/dev/sd")[1]
possiblePrefixes := []string{
"/dev/xvd", // Xen
"/dev/vd", // KVM
"/dev/sd",
}
for _, prefix := range possiblePrefixes {
path := prefix + pathSuffix
if dpr.fs.FileExists(path) {
return path, true
}
}
return "", false
}