-
Notifications
You must be signed in to change notification settings - Fork 115
/
scsi_lun_device_path_resolver.go
123 lines (96 loc) · 3.39 KB
/
scsi_lun_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
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
package devicepathresolver
import (
"fmt"
"path"
"path/filepath"
"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"
)
type SCSILunDevicePathResolver struct {
diskWaitTimeout time.Duration
fs boshsys.FileSystem
logTag string
logger boshlog.Logger
}
func NewSCSILunDevicePathResolver(
diskWaitTimeout time.Duration,
fs boshsys.FileSystem,
logger boshlog.Logger,
) SCSILunDevicePathResolver {
return SCSILunDevicePathResolver{
fs: fs,
diskWaitTimeout: diskWaitTimeout,
logTag: "scsiLunResolver",
logger: logger,
}
}
func (ldpr SCSILunDevicePathResolver) GetRealDevicePath(diskSettings boshsettings.DiskSettings) (string, bool, error) {
if diskSettings.Lun == "" {
return "", false, bosherr.Error("Disk lun is not set")
}
if diskSettings.HostDeviceID == "" {
return "", false, bosherr.Error("Disk host_device_id is not set")
}
hostPaths, err := ldpr.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 {
ldpr.logger.Info(ldpr.logTag, "Performing SCSI rescan of %s", hostPath)
err = ldpr.fs.WriteFileString(hostPath, "- - -")
if err != nil {
return "", false, bosherr.WrapError(err, "Starting SCSI rescan")
}
}
stopAfter := time.Now().Add(ldpr.diskWaitTimeout)
var vmBusDeviceForDataDisks string
vmBusDevices, err := ldpr.fs.Glob("/sys/bus/vmbus/devices/*/device_id")
if err != nil {
return "", false, bosherr.WrapError(err, "Could not list vmbus devices")
}
for _, vmBusDevice := range vmBusDevices {
deviceID, err := ldpr.fs.ReadFileString(vmBusDevice)
if err != nil {
continue
}
if strings.TrimSpace(deviceID) == diskSettings.HostDeviceID {
vmBusDeviceSplits := strings.Split(vmBusDevice, "/")
vmBusDeviceForDataDisks = vmBusDeviceSplits[5]
break
}
}
if vmBusDeviceForDataDisks == "" {
return "", false, bosherr.WrapErrorf(err, "Cannot find the vmbus device by host_device_id '%s'", diskSettings.HostDeviceID)
}
ldpr.logger.Debug(ldpr.logTag, "Find the vmbus device '%s' by host_device_id '%s'", vmBusDeviceForDataDisks, diskSettings.HostDeviceID)
deviceGlobPath := fmt.Sprintf("/sys/bus/scsi/devices/*:*:*:%s/block/*", diskSettings.Lun)
for {
ldpr.logger.Debug(ldpr.logTag, "Waiting for device to appear")
if time.Now().After(stopAfter) {
return "", true, bosherr.Errorf("Timed out getting real device path by lun '%s' and host_device_id '%s'", diskSettings.Lun, diskSettings.HostDeviceID)
}
time.Sleep(100 * time.Millisecond)
devicePaths, err := ldpr.fs.Glob(deviceGlobPath)
if err != nil {
return "", false, bosherr.WrapErrorf(err, "Could not list disks by lun '%s'", diskSettings.Lun)
}
for _, devicePath := range devicePaths {
baseName := path.Base(devicePath)
tempPath, err := ldpr.fs.ReadAndFollowLink(path.Join("/sys/class/block/", baseName))
if err != nil {
continue
}
if strings.Contains(tempPath, fmt.Sprintf("%[1]c%s%[1]c", filepath.Separator, vmBusDeviceForDataDisks)) {
realPath := path.Join("/dev/", baseName)
if ldpr.fs.FileExists(realPath) {
ldpr.logger.Debug(ldpr.logTag, "Found real path '%s'", realPath)
return realPath, false, nil
}
}
}
}
}