27
27
osStat = os .Stat
28
28
filepathGlob = filepath .Glob
29
29
osOpenFile = os .OpenFile
30
+ sleep = time .Sleep
30
31
)
31
32
32
33
// iscsiSession contains information avout an iSCSI session
@@ -162,14 +163,14 @@ func getCurrentSessions() ([]iscsiSession, error) {
162
163
163
164
// waitForPathToExist wait for a file at a path to exists on disk
164
165
func waitForPathToExist (devicePath * string , maxRetries , intervalSeconds uint , deviceTransport string ) error {
165
- if devicePath == nil {
166
+ if devicePath == nil || * devicePath == "" {
166
167
return fmt .Errorf ("unable to check unspecified devicePath" )
167
168
}
168
169
169
170
for i := uint (0 ); i <= maxRetries ; i ++ {
170
171
if i != 0 {
171
172
debug .Printf ("Device path %q doesn't exists yet, retrying in %d seconds (%d/%d)" , * devicePath , intervalSeconds , i , maxRetries )
172
- time . Sleep (time .Second * time .Duration (intervalSeconds ))
173
+ sleep (time .Second * time .Duration (intervalSeconds ))
173
174
}
174
175
175
176
if err := pathExists (devicePath , deviceTransport ); err == nil {
@@ -195,7 +196,11 @@ func pathExists(devicePath *string, deviceTransport string) error {
195
196
return err
196
197
}
197
198
} else {
198
- fpath , _ := filepathGlob (* devicePath )
199
+ fpath , err := filepathGlob (* devicePath )
200
+
201
+ if err != nil {
202
+ return err
203
+ }
199
204
if fpath == nil {
200
205
return os .ErrNotExist
201
206
}
@@ -210,22 +215,17 @@ func pathExists(devicePath *string, deviceTransport string) error {
210
215
211
216
// getMultipathDevice returns a multipath device for the configured targets if it exists
212
217
func getMultipathDevice (devices []Device ) (* Device , error ) {
213
- var deviceInfo deviceInfo
214
218
var multipathDevice * Device
215
219
var devicePaths []string
216
220
217
221
for _ , device := range devices {
218
222
devicePaths = append (devicePaths , device .GetPath ())
219
223
}
220
- out , err := lsblk ("-J " , devicePaths )
224
+ deviceInfo , err := lsblk ("" , devicePaths )
221
225
if err != nil {
222
226
return nil , err
223
227
}
224
228
225
- if err = json .Unmarshal (out , & deviceInfo ); err != nil {
226
- return nil , err
227
- }
228
-
229
229
for _ , device := range deviceInfo .BlockDevices {
230
230
if len (device .Children ) != 1 {
231
231
return nil , fmt .Errorf ("device is not mapped to exactly one multipath device: %v" , device .Children )
@@ -454,18 +454,12 @@ func (c *Connector) IsMultipathEnabled() bool {
454
454
func GetSCSIDevices (devicePaths []string ) ([]Device , error ) {
455
455
debug .Printf ("Getting info about SCSI devices %s.\n " , devicePaths )
456
456
457
- out , err := lsblk ("-JS " , devicePaths )
457
+ deviceInfo , err := lsblk ("-S " , devicePaths )
458
458
if err != nil {
459
459
debug .Printf ("An error occured while looking info about SCSI devices: %v" , err )
460
460
return nil , err
461
461
}
462
462
463
- var deviceInfo deviceInfo
464
- err = json .Unmarshal (out , & deviceInfo )
465
- if err != nil {
466
- return nil , err
467
- }
468
-
469
463
return deviceInfo .BlockDevices , nil
470
464
}
471
465
@@ -488,7 +482,7 @@ func GetISCSIDevices(devicePaths []string) (devices []Device, err error) {
488
482
}
489
483
490
484
// lsblk execute the lsblk commands
491
- func lsblk (flags string , devicePaths []string ) ([]byte , error ) {
485
+ func lsblkRaw (flags string , devicePaths []string ) ([]byte , error ) {
492
486
out , err := execCommand ("lsblk" , append ([]string {flags }, devicePaths ... )... ).CombinedOutput ()
493
487
debug .Printf ("lsblk %s %s" , flags , strings .Join (devicePaths , " " ))
494
488
if err != nil {
@@ -498,6 +492,21 @@ func lsblk(flags string, devicePaths []string) ([]byte, error) {
498
492
return out , nil
499
493
}
500
494
495
+ func lsblk (flags string , devicePaths []string ) (* deviceInfo , error ) {
496
+ var deviceInfo deviceInfo
497
+
498
+ out , err := lsblkRaw ("-J " + flags , devicePaths )
499
+ if err != nil {
500
+ return nil , err
501
+ }
502
+
503
+ if err = json .Unmarshal (out , & deviceInfo ); err != nil {
504
+ return nil , err
505
+ }
506
+
507
+ return & deviceInfo , nil
508
+ }
509
+
501
510
// writeInSCSIDeviceFile write into special devices files to change devices state
502
511
func writeInSCSIDeviceFile (hctl string , file string , content string ) error {
503
512
filename := filepath .Join ("/sys/class/scsi_device" , hctl , "device" , file )
@@ -614,15 +623,15 @@ func (d *Device) WriteDeviceFile(name string, content string) error {
614
623
615
624
// Shutdown turn off an SCSI device by writing offline\n in /sys/class/scsi_device/h:c:t:l/device/state
616
625
func (d * Device ) Shutdown () error {
617
- return writeInSCSIDeviceFile ( d . Hctl , "state" , "offline\n " )
626
+ return d . WriteDeviceFile ( "state" , "offline\n " )
618
627
}
619
628
620
629
// Delete detach an SCSI device by writing 1 in /sys/class/scsi_device/h:c:t:l/device/delete
621
630
func (d * Device ) Delete () error {
622
- return writeInSCSIDeviceFile ( d . Hctl , "delete" , "1" )
631
+ return d . WriteDeviceFile ( "delete" , "1" )
623
632
}
624
633
625
634
// Rescan rescan an SCSI device by writing 1 in /sys/class/scsi_device/h:c:t:l/device/rescan
626
635
func (d * Device ) Rescan () error {
627
- return writeInSCSIDeviceFile ( d . Hctl , "rescan" , "1" )
636
+ return d . WriteDeviceFile ( "rescan" , "1" )
628
637
}
0 commit comments