forked from cloudfoundry/bosh-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scsi_id_device_path_resolver.go
91 lines (74 loc) · 2.38 KB
/
scsi_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
90
91
package devicepathresolver
import (
"strings"
"time"
boshsettings "github.com/cloudfoundry/bosh-agent/settings"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
boshsys "github.com/cloudfoundry/bosh-utils/system"
)
// SCSIIDDevicePathResolver resolves device path by performing a
// SCSI rescan then looking under "/dev/disk/by-id/*uuid"
// where "uuid" is the cloud ID of the disk
type SCSIIDDevicePathResolver struct {
diskWaitTimeout time.Duration
fs boshsys.FileSystem
logTag string
logger boshlog.Logger
}
func NewSCSIIDDevicePathResolver(
diskWaitTimeout time.Duration,
fs boshsys.FileSystem,
logger boshlog.Logger,
) SCSIIDDevicePathResolver {
return SCSIIDDevicePathResolver{
diskWaitTimeout: diskWaitTimeout,
fs: fs,
logTag: "scsiIDresolver",
logger: logger,
}
}
func (idpr SCSIIDDevicePathResolver) GetRealDevicePath(diskSettings boshsettings.DiskSettings) (string, bool, error) {
if diskSettings.DeviceID == "" {
return "", false, bosherr.Errorf("Disk device ID is not set")
}
hostPaths, err := idpr.fs.Glob("/sys/class/scsi_host/host*/scan")
if err != nil {
return "", false, bosherr.WrapError(err, "Could not list SCSI hosts")
}
for _, hostPath := range hostPaths {
idpr.logger.Info(idpr.logTag, "Performing SCSI rescan of "+hostPath)
err = idpr.fs.WriteFileString(hostPath, "- - -")
if err != nil {
return "", false, bosherr.WrapError(err, "Starting SCSI rescan")
}
}
stopAfter := time.Now().Add(idpr.diskWaitTimeout)
found := false
var realPath string
for !found {
idpr.logger.Debug(idpr.logTag, "Waiting for device to appear")
if time.Now().After(stopAfter) {
return "", true, bosherr.Errorf("Timed out getting real device path for '%s'", diskSettings.DeviceID)
}
time.Sleep(100 * time.Millisecond)
uuid := strings.Replace(diskSettings.DeviceID, "-", "", -1)
disks, err := idpr.fs.Glob("/dev/disk/by-id/*" + uuid)
if err != nil {
return "", false, bosherr.WrapError(err, "Could not list disks by id")
}
for _, path := range disks {
idpr.logger.Debug(idpr.logTag, "Reading link "+path)
realPath, err = idpr.fs.ReadAndFollowLink(path)
if err != nil {
continue
}
if idpr.fs.FileExists(realPath) {
idpr.logger.Debug(idpr.logTag, "Found real path "+realPath)
found = true
break
}
}
}
return realPath, false, nil
}