-
Notifications
You must be signed in to change notification settings - Fork 115
/
id_device_path_resolver.go
89 lines (73 loc) · 2.14 KB
/
id_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
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
package devicepathresolver
import (
"fmt"
"path"
"time"
boshudev "github.com/cloudfoundry/bosh-agent/platform/udevdevice"
boshsettings "github.com/cloudfoundry/bosh-agent/settings"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshsys "github.com/cloudfoundry/bosh-utils/system"
)
type idDevicePathResolver struct {
diskWaitTimeout time.Duration
udev boshudev.UdevDevice
fs boshsys.FileSystem
}
func NewIDDevicePathResolver(
diskWaitTimeout time.Duration,
udev boshudev.UdevDevice,
fs boshsys.FileSystem,
) DevicePathResolver {
return idDevicePathResolver{
diskWaitTimeout: diskWaitTimeout,
udev: udev,
fs: fs,
}
}
func (idpr idDevicePathResolver) GetRealDevicePath(diskSettings boshsettings.DiskSettings) (string, bool, error) {
if diskSettings.ID == "" {
return "", false, bosherr.Errorf("Disk ID is not set")
}
if len(diskSettings.ID) < 20 {
return "", false, bosherr.Errorf("Disk ID is not the correct format")
}
err := idpr.udev.Trigger()
if err != nil {
return "", false, bosherr.WrapError(err, "Running udevadm trigger")
}
err = idpr.udev.Settle()
if err != nil {
return "", false, bosherr.WrapError(err, "Running udevadm settle")
}
stopAfter := time.Now().Add(idpr.diskWaitTimeout)
found := false
var realPath string
diskID := diskSettings.ID[0:20]
deviceGlobPattern := fmt.Sprintf("*%s", diskID)
deviceIDPathGlobPattern := path.Join("/", "dev", "disk", "by-id", deviceGlobPattern)
for !found {
if time.Now().After(stopAfter) {
return "", true, bosherr.Errorf("Timed out getting real device path for '%s'", diskID)
}
time.Sleep(100 * time.Millisecond)
pathMatches, err := idpr.fs.Glob(deviceIDPathGlobPattern)
if err != nil {
continue
}
switch len(pathMatches) {
case 0:
continue
case 1:
realPath, err = idpr.fs.ReadAndFollowLink(pathMatches[0])
if err != nil {
continue
}
if idpr.fs.FileExists(realPath) {
found = true
}
default:
return "", true, bosherr.Errorf("More than one disk matched glob %q while getting real device path for %q", deviceIDPathGlobPattern, diskID)
}
}
return realPath, false, nil
}