-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathdevice_connectivity_helper_scsigeneric.go
More file actions
850 lines (731 loc) · 29 KB
/
Copy pathdevice_connectivity_helper_scsigeneric.go
File metadata and controls
850 lines (731 loc) · 29 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
/**
* Copyright 2019 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package device_connectivity
import (
"bufio"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"sync"
"time"
"github.com/ibm/ibm-block-csi-driver/node/logger"
"github.com/ibm/ibm-block-csi-driver/node/pkg/driver/executer"
)
//go:generate mockgen -destination=../../../mocks/mock_OsDeviceConnectivityHelperScsiGenericInterface.go -package=mocks github.com/ibm/ibm-block-csi-driver/node/pkg/driver/device_connectivity OsDeviceConnectivityHelperScsiGenericInterface
type OsDeviceConnectivityHelperScsiGenericInterface interface {
/*
This is helper interface for OsDeviceConnectivityHelperScsiGenericInterface.
Mainly for writing clean unit testing, so we can Mock this interface in order to unit test logic.
*/
RescanDevices(lunId int, arrayIdentifiers []string) error
GetMpathDevice(volumeId string) (string, error)
FlushMultipathDevice(mpathDevice string) error
RemovePhysicalDevice(sysDevices []string) error
RemoveGhostDevice(lun int) error
ValidateLun(lun int, sysDevices []string) error
IsVolumePathMatchesVolumeId(volumeId string, volumePath string) (bool, error)
}
type OsDeviceConnectivityHelperScsiGeneric struct {
Executer executer.ExecuterInterface
Helper OsDeviceConnectivityHelperInterface
MutexMultipathF *sync.Mutex
CleanScsiDevice bool
}
type WaitForMpathResult struct {
devicesPaths []string
err error
}
var (
TimeOutMultipathCmd = 60 * 1000
TimeOutMultipathdCmd = 10 * 1000
TimeOutBlockDevCmd = 10 * 1000
TimeOutSgInqCmd = 3 * 1000
MultipathdWildcardsVolumeIdAndMpath = []string{"%w", "%d"}
MultipathdWildcardsMpathNameAndVolumeId = []string{"%n", "%w"}
multipathdWildcardsMpathAndVolumeId = []string{"%d", "%w"}
)
const (
DevPath = "/dev"
DevMapperPath = "/dev/mapper"
WaitForMpathRetries = 5
WaitForMpathWaitIntervalSec = 1
FcHostSysfsPath = "/sys/class/fc_remote_ports/rport-*/port_name"
IscsiHostRexExPath = "/sys/class/iscsi_host/host*/device/session*/iscsi_session/session*/targetname"
sysDeviceSymLinkFormat = "/sys/block/%s/device"
sysDeviceDeletePathFormat = sysDeviceSymLinkFormat + "/delete"
blockDevCmd = "blockdev"
flushBufsFlag = "--flushbufs"
mpathdSeparator = ","
multipathdCmd = "multipathd"
multipathCmd = "multipath"
WwnOuiEnd = 7
WwnVendorIdentifierEnd = 16
procMountsFilePath = "/proc/mounts"
)
func NewOsDeviceConnectivityHelperScsiGeneric(executer executer.ExecuterInterface, clean_scsi_device bool) OsDeviceConnectivityHelperScsiGenericInterface {
return &OsDeviceConnectivityHelperScsiGeneric{
Executer: executer,
Helper: NewOsDeviceConnectivityHelperGeneric(executer),
MutexMultipathF: &sync.Mutex{},
CleanScsiDevice: clean_scsi_device,
}
}
func (r OsDeviceConnectivityHelperScsiGeneric) IsVolumePathMatchesVolumeId(volumeUuid string, volumePath string) (bool, error) {
logger.Infof("IsVolumePathMatchesVolumeId: Searching matching volume id for volume path: [%s] ", volumePath)
volumeIdVariations := r.Helper.GetVolumeIdVariations(volumeUuid)
mpathDeviceName, err := r.Helper.GetMpathDeviceName(volumePath)
if err != nil {
return false, err
}
dmDirectory := DevPath
multipathdCommandFormatArgs := multipathdWildcardsMpathAndVolumeId
if r.Helper.IsDmName(mpathDeviceName) {
dmDirectory = DevMapperPath
multipathdCommandFormatArgs = MultipathdWildcardsMpathNameAndVolumeId
}
mpathdOutput, err := r.Helper.GetMpathdOutputForVolume(volumeIdVariations, multipathdCommandFormatArgs)
if err != nil {
return false, err
}
mpathVolumeId, err := r.Helper.GetMpathVolumeId(mpathdOutput, mpathDeviceName, dmDirectory)
if err != nil {
return false, err
}
logger.Infof("IsVolumePathMatchesVolumeId: found volume id [%s] for volume path [%s] ", mpathVolumeId, volumePath)
return r.Helper.IsAnyVariationInMpathVolumeId(mpathVolumeId, volumeIdVariations), nil
}
func (r OsDeviceConnectivityHelperScsiGeneric) RescanDevices(lunId int, arrayIdentifiers []string) error {
logger.Debugf("Rescan : Start rescan on specific lun, on lun : {%v}, with array identifiers : {%v}", lunId, arrayIdentifiers)
var hostIDs = make(map[int]bool)
var errStrings []string
if len(arrayIdentifiers) == 0 {
e := &ErrorNotFoundArrayIdentifiers{lunId}
logger.Errorf("%s", e.Error())
return e
}
for _, arrayIdentifier := range arrayIdentifiers {
hostsId, e := r.Helper.GetHostsIdByArrayIdentifier(arrayIdentifier)
if e != nil {
logger.Errorf("%s", e.Error())
errStrings = append(errStrings, e.Error())
}
for _, hostId := range hostsId {
hostIDs[hostId] = true
}
}
if len(hostIDs) == 0 && len(errStrings) != 0 {
err := errors.New(strings.Join(errStrings, ","))
return err
}
for hostNumber := range hostIDs {
filename := fmt.Sprintf("/sys/class/scsi_host/host%d/scan", hostNumber)
f, err := r.Executer.OsOpenFile(filename, os.O_APPEND|os.O_WRONLY, 0200)
if err != nil {
logger.Errorf("Rescan Error: could not open filename : {%v}. err : {%v}", filename, err)
return err
}
defer f.Close()
scanCmd := fmt.Sprintf("- - %d", lunId)
logger.Debugf("Rescan host device : echo %s > %s", scanCmd, filename)
if written, err := r.Executer.FileWriteString(f, scanCmd); err != nil {
logger.Errorf("Rescan Error: could not write to rescan file :{%v}, error : {%v}", filename, err)
return err
} else if written == 0 {
e := &ErrorNothingWasWrittenToScanFileError{filename}
logger.Errorf("%s", e.Error())
return e
}
}
logger.Debugf("Rescan : finish rescan lun on lun id : {%v}, with array identifiers : {%v}", lunId, arrayIdentifiers)
return nil
}
func (r OsDeviceConnectivityHelperScsiGeneric) GetMpathDevice(volumeId string) (string, error) {
logger.Infof("GetMpathDevice: Searching multipath devices for volume : [%s] ", volumeId)
volumeIdVariations := r.Helper.GetVolumeIdVariations(volumeId)
dmPath, _ := r.Helper.GetDmsPath(volumeIdVariations)
if dmPath != "" {
SgInqWwn, _ := r.Helper.GetWwnByScsiInq(dmPath)
if isSameId(SgInqWwn, volumeIdVariations) {
return dmPath, nil
}
logger.Warningf("Expected {%v} but got {%v} from sg_inq", volumeId, SgInqWwn)
}
if err := r.Helper.ReloadMultipath(); err != nil {
return "", err
}
dmPath, err := r.Helper.GetDmsPath(volumeIdVariations)
if err != nil {
return "", err
}
if dmPath == "" {
return "", &MultipathDeviceNotFoundForVolumeError{volumeId}
}
SgInqWwn, err := r.Helper.GetWwnByScsiInq(dmPath)
if err != nil {
return "", err
}
if isSameId(SgInqWwn, volumeIdVariations) {
return dmPath, nil
}
// To make sure we found the right WWN, if not raise error instead of using wrong mpath
return "", &ErrorWrongDeviceFound{dmPath, volumeIdVariations[0], SgInqWwn}
}
func isSameId(wwn string, volumeIdVariations []string) bool {
wwn = strings.ToLower(wwn)
for _, volumeIdVariation := range volumeIdVariations {
if wwn == volumeIdVariation {
return true
}
}
return false
}
func (r OsDeviceConnectivityHelperScsiGeneric) flushDeviceBuffers(deviceName string) error {
devicePath := filepath.Join(DevPath, deviceName)
_, err := r.Executer.ExecuteWithTimeoutSilently(TimeOutBlockDevCmd, blockDevCmd, []string{flushBufsFlag, devicePath})
if err != nil {
logger.Errorf("%v %v {%v} did not succeed to flush the device buffers. err={%v}", blockDevCmd, flushBufsFlag, devicePath,
err.Error())
return err
}
return nil
}
func (r OsDeviceConnectivityHelperScsiGeneric) flushDevicesBuffers(deviceNames []string) error {
logger.Debugf("executing commands : {%v %v} on devices : {%v} and timeout : {%v} mseconds", blockDevCmd, flushBufsFlag, deviceNames, TimeOutBlockDevCmd)
for _, deviceName := range deviceNames {
err := r.flushDeviceBuffers(deviceName)
if err != nil {
return err
}
}
logger.Debugf("Finished executing commands: {%v %v}", blockDevCmd, flushBufsFlag)
return nil
}
func (r OsDeviceConnectivityHelperScsiGeneric) FlushMultipathDevice(mpathDevice string) error {
err := r.flushDeviceBuffers(mpathDevice)
if err != nil {
return err
}
// mpathdevice is dm-4 for example
logger.Debugf("Flushing mpath device : {%v}", mpathDevice)
fullDevice := filepath.Join(DevPath, mpathDevice)
logger.Debugf("Try to acquire lock for running the command multipath -f {%v} (to avoid concurrent multipath commands)", mpathDevice)
r.MutexMultipathF.Lock()
logger.Debugf("Acquired lock for multipath -f command")
_, err = r.Executer.ExecuteWithTimeout(TimeOutMultipathCmd, multipathCmd, []string{"-f", fullDevice})
r.MutexMultipathF.Unlock()
if err != nil {
if _, e := os.Stat(fullDevice); os.IsNotExist(e) {
logger.Debugf("Mpath device {%v} was deleted", fullDevice)
} else {
logger.Errorf("multipath -f {%v} did not succeed to delete the device. err={%v}", fullDevice, err.Error())
return err
}
}
logger.Debugf("Finished flushing mpath device : {%v}", mpathDevice)
return nil
}
func (r OsDeviceConnectivityHelperScsiGeneric) RemovePhysicalDevice(sysDevices []string) error {
flushErr := r.flushDevicesBuffers(sysDevices)
if flushErr != nil {
return flushErr
}
// sysDevices = sdb, sda,...
logger.Debugf(`Removing scsi device : {%v} by writing "1" to the delete file of each device: {%v}`, sysDevices, fmt.Sprintf(sysDeviceDeletePathFormat, "<deviceName>"))
// NOTE: this func could be also relevant for SCSI (not only for iSCSI)
var (
f *os.File
err error
)
for _, deviceName := range sysDevices {
if deviceName == "" {
continue
}
filename := fmt.Sprintf(sysDeviceDeletePathFormat, deviceName)
if f, err = os.OpenFile(filename, os.O_APPEND|os.O_WRONLY, 0200); err != nil {
if os.IsNotExist(err) {
logger.Warningf("Idempotency: Block device {%v} was not found on the system, so skip deleting it", deviceName)
continue
} else {
logger.Errorf("Error while opening file : {%v}. error: {%v}", filename, err.Error())
return err
}
}
defer f.Close()
if _, err := f.WriteString("1"); err != nil {
logger.Errorf("Error while writing to file : {%v}. error: {%v}", filename, err.Error())
return err // TODO: maybe we need to just swallow the error and continnue??
}
}
logger.Debugf("Finished removing SCSI devices : {%v}", sysDevices)
return nil
}
func (o OsDeviceConnectivityHelperScsiGeneric) RemoveGhostDevice(lun int) error {
if !o.CleanScsiDevice {
logger.Debugf("Clean devices disabled, skipping removeGhostDevice") //Can be omitted, debug only.
return nil
}
sgMapCmd := "sg_map"
args := []string{"-x"}
out, err := o.Executer.ExecuteWithTimeoutSilently(TimeOutSgInqCmd, sgMapCmd, args)
if err != nil {
logger.Errorf("Error getting sg devices from sg_map. error: {%v}", err.Error())
return err
}
lines := strings.Split(string(out), "\n")
var (
deleted int = 0
notLun int = 0
notPQ int = 0
)
/*
output pattern that we're looking for is something like this:
/dev/sg2173 34 0 39 <lun> 0
namely: /dev/sg something, then 4 numbers with space between each, the last of which is the lun,
then two spaces and another number
If we want either with or without sd:
pattern := fmt.Sprintf(`^/dev/sg\d+\s+\d+\s\d+\s\d+\s%d\s{2}\d+(?:\s+/dev/\S+)?$`, lun)
If we want only lines with no sd device:
*/
pattern := fmt.Sprintf(`^/dev/sg\d+\s+\d+\s\d+\s\d+\s%d\s{2}\d+$`, lun)
re := regexp.MustCompile(pattern)
for _, line := range lines {
if !re.MatchString(line) {
notLun += 1
continue
}
fields := strings.Fields(line)
sgName := fields[0]
if !o.isGhostIBMDevice(sgName) {
notPQ += 1
continue
}
sgDev := strings.TrimPrefix(sgName, "/dev/")
deletePath := fmt.Sprintf("/sys/class/scsi_generic/%s/device/delete", sgDev)
logger.Debugf("Deleting ghost device: %s", sgDev) // should I print all of them in one line at the end?
if err := os.WriteFile(deletePath, []byte("1"), 0644); err != nil {
logger.Errorf("Error deleting device %s: %v\n", sgDev, err)
} else {
deleted += 1
}
}
if deleted != 0 {
logger.Debugf("Deleted %d devices. Found %d not-our-lun devices, and %d our lun but non ghost", deleted, notLun, notPQ)
}
return nil
}
func (o OsDeviceConnectivityHelperScsiGeneric) isGhostIBMDevice(sgDev string) bool {
sgInqCmd := "sg_inq"
args := []string{sgDev}
outputBytes, err := o.Executer.ExecuteWithTimeoutSilently(TimeOutSgInqCmd, sgInqCmd, args)
if err != nil {
logger.Errorf("Error executing %v. error: {%v}", sgInqCmd, err.Error())
return false
}
outStr := string(outputBytes)
return strings.Contains(outStr, "PQual=1") && strings.Contains(outStr, "IBM")
}
func (r OsDeviceConnectivityHelperScsiGeneric) ValidateLun(lun int, sysDevices []string) error {
logger.Debugf("Validating lun {%v} on devices: {%v}", lun, sysDevices)
for _, sysDevice := range sysDevices {
sysDeviceParts := strings.Split(sysDevice, "/")
device := sysDeviceParts[len(sysDeviceParts)-1]
symLinkPath := fmt.Sprintf(sysDeviceSymLinkFormat, device)
destinationPath, err := filepath.EvalSymlinks(symLinkPath)
if err != nil {
return err
}
if !strings.HasSuffix(destinationPath, strconv.Itoa(lun)) {
return fmt.Errorf("lun not valid, storage lun: %v, linkedPath: %v to device: %v", lun, destinationPath, device)
}
}
logger.Debugf("Finished lun validation")
return nil
}
// ============== OsDeviceConnectivityHelperInterface ==========================
//go:generate mockgen -destination=../../../mocks/mock_OsDeviceConnectivityHelperInterface.go -package=mocks github.com/ibm/ibm-block-csi-driver/node/pkg/driver/device_connectivity OsDeviceConnectivityHelperInterface
type OsDeviceConnectivityHelperInterface interface {
/*
This is helper interface for OsDeviceConnectivityScsiGeneric.
Mainly for writting clean unit testing, so we can Mock this interface in order to unit test OsDeviceConnectivityHelperGeneric logic.
*/
GetHostsIdByArrayIdentifier(arrayIdentifier string) ([]int, error)
GetDmsPath(volumeIdVariations []string) (string, error)
GetWwnByScsiInq(dev string) (string, error)
ReloadMultipath() error
GetMpathdOutputForVolume(volumeIdVariations []string, multipathdCommandFormatArgs []string) (string, error)
GetVolumeIdVariations(volumeUuid string) []string
GetMpathDeviceName(volumePath string) (string, error)
IsAnyVariationInMpathVolumeId(mpathVolumeId string, volumeIdVariations []string) bool
GetMpathVolumeId(mpathdOutput string, mpathDeviceName string, dmDirectory string) (string, error)
IsDmName(mpathDeviceName string) bool
}
type OsDeviceConnectivityHelperGeneric struct {
Executer executer.ExecuterInterface
Helper GetDmsPathHelperInterface
}
func NewOsDeviceConnectivityHelperGeneric(executer executer.ExecuterInterface) OsDeviceConnectivityHelperInterface {
return &OsDeviceConnectivityHelperGeneric{
Executer: executer,
Helper: NewGetDmsPathHelperGeneric(executer),
}
}
func (o OsDeviceConnectivityHelperGeneric) GetHostsIdByArrayIdentifier(arrayIdentifier string) ([]int, error) {
/*
Description:
This function find all the hosts IDs under directory /sys/class/fc_host/ or /sys/class/iscsi_host"
So the function goes over all the above hosts and return back only the host numbers as a list.
*/
//arrayIdentifier is wwn, value is 500507680b25c0aa
var targetFilePath string
var regexpValue string
//IQN format is iqn.yyyy-mm.naming-authority:unique name
//For example: iqn.1986-03.com.ibm:2145.v7k194.node2
iscsiMatchRex := `^iqn\.(\d{4}-\d{2})\.([^:]+)(:)([^,:\s']+)`
isIscsi, err := regexp.MatchString(iscsiMatchRex, arrayIdentifier)
if isIscsi {
targetFilePath = IscsiHostRexExPath
regexpValue = "host([0-9]+)"
} else {
targetFilePath = FcHostSysfsPath
regexpValue = "rport-([0-9]+)"
}
var HostIDs []int
matches, err := o.Executer.FilepathGlob(targetFilePath)
if err != nil {
logger.Errorf("Error while Glob targetFilePath : {%v}. err : {%v}", targetFilePath, err)
return nil, err
}
logger.Debugf("{%v} targetname files matches were found", len(matches))
re := regexp.MustCompile(regexpValue)
logger.Debugf("Check if any match is relevant for storage target (%s)", arrayIdentifier)
for _, targetPath := range matches {
targetName, err := o.Executer.IoutilReadFile(targetPath)
if err != nil {
logger.Warningf("Could not read target name from file : {%v}, error : {%v}", targetPath, err)
continue
}
identifierFromHost := strings.TrimSpace(string(targetName))
//For FC WWNs from the host, the value will like this: 0x500507680b26c0aa, but the arrayIdentifier doesn't have this prefix
if strings.HasPrefix(identifierFromHost, "0x") {
identifierFromHost = strings.TrimLeft(identifierFromHost, "0x")
}
if strings.EqualFold(identifierFromHost, arrayIdentifier) {
regexMatch := re.FindStringSubmatch(targetPath)
logger.Tracef("Found regex matches : {%v}", regexMatch)
hostNumber := -1
if len(regexMatch) < 2 {
logger.Warningf("Could not find host number for targetFilePath : {%v}", targetPath)
continue
} else {
hostNumber, err = strconv.Atoi(regexMatch[1])
if err != nil {
logger.Warningf("Host number in for target file was not valid : {%v}", regexMatch[1])
continue
}
}
HostIDs = append(HostIDs, hostNumber)
logger.Debugf("portState path (%s) was found. Adding host ID {%v} to the id list", targetPath, hostNumber)
}
}
if len(HostIDs) == 0 {
return []int{}, &ConnectivityIdentifierStorageTargetNotFoundError{StorageTargetName: arrayIdentifier, DirectoryPath: targetFilePath}
}
return HostIDs, nil
}
func (o OsDeviceConnectivityHelperGeneric) GetMpathVolumeId(mpathdOutput string, mpathDeviceName string,
dmDirectory string) (string, error) {
mpathVolumeId, err := o.Helper.ExtractVolumeId(mpathDeviceName, mpathdOutput)
if err != nil {
return "", err
}
dmPath := filepath.Join(dmDirectory, filepath.Base(strings.TrimSpace(mpathDeviceName)))
SgInqWwn, err := o.GetWwnByScsiInq(dmPath)
if err != nil {
return "", err
}
if o.IsAnyVariationInMpathVolumeId(mpathVolumeId, []string{SgInqWwn}) {
return mpathVolumeId, nil
}
return "", &ErrorWrongDeviceFound{dmPath, mpathVolumeId, SgInqWwn}
}
func (o OsDeviceConnectivityHelperGeneric) GetWwnByScsiInq(dev string) (string, error) {
/* scsi inq example
$> sg_inq -p 0x83 /dev/mapper/mpathhe
VPD INQUIRY: Device Identification page
Designation descriptor number 1, descriptor length: 20
designator_type: NAA, code_set: Binary
associated with the addressed logical unit
NAA 6, IEEE Company_id: 0x1738
Vendor Specific Identifier: 0xcfc9035eb
Vendor Specific Identifier Extension: 0xcea5f6
[0x6001738cfc9035eb0000000000ceaaaa]
Designation descriptor number 2, descriptor length: 52
designator_type: T10 vendor identification, code_set: ASCII
associated with the addressed logical unit
vendor id: IBM
vendor specific: 2810XIV 60035EB0000000000CEAAAA
Designation descriptor number 3, descriptor length: 43
designator_type: vendor specific [0x0], code_set: ASCII
associated with the addressed logical unit
vendor specific: vol=u_k8s_longevity_ibm-ubiquity-db
Designation descriptor number 4, descriptor length: 37
designator_type: vendor specific [0x0], code_set: ASCII
associated with the addressed logical unit
vendor specific: host=k8s-acceptance-v18-node1
Designation descriptor number 5, descriptor length: 8
designator_type: Target port group, code_set: Binary
associated with the target port
Target port group: 0x0
Designation descriptor number 6, descriptor length: 8
designator_type: Relative target port, code_set: Binary
associated with the target port
Relative target port: 0xd22
*/
sgInqCmd := "sg_inq"
if err := o.Executer.IsExecutable(sgInqCmd); err != nil {
return "", err
}
args := []string{"-p", "0x83", dev}
// add timeout in case the call never comes back.
logger.Debugf("Calling [%s] with timeout", sgInqCmd)
outputBytes, err := o.Executer.ExecuteWithTimeout(TimeOutSgInqCmd, sgInqCmd, args)
if err != nil {
return "", err
}
wwnRegex := "(?i)" + `\[0x(.*?)\]`
wwnRegexCompiled, err := regexp.Compile(wwnRegex)
if err != nil {
return "", err
}
/*
sg_inq on device NAA6 returns "Vendor Specific Identifier Extension"
sg_inq on device EUI-64 returns "Vendor Specific Extension Identifier".
*/
pattern := "(?i)" + "Vendor Specific (Identifier Extension|Extension Identifier):"
scanner := bufio.NewScanner(strings.NewReader(string(outputBytes[:])))
regex, err := regexp.Compile(pattern)
if err != nil {
return "", err
}
wwn := ""
found := false
for scanner.Scan() {
line := scanner.Text()
if found {
matches := wwnRegexCompiled.FindStringSubmatch(line)
if len(matches) != 2 {
logger.Debugf("wrong line, too many matches in sg_inq output : %#v", matches)
return "", &ErrorNoRegexWwnMatchInScsiInq{dev, line}
}
wwn = matches[1]
logger.Debugf("Found wwn [%s] in sg_inq", wwn)
return wwn, nil
}
if regex.MatchString(line) {
found = true
// its one line after "Vendor Specific Identifier Extension:" line which should contain the WWN
continue
}
}
return "", &MultipathDeviceNotFoundForVolumeError{wwn}
}
func (o OsDeviceConnectivityHelperGeneric) ReloadMultipath() error {
logger.Infof("ReloadMultipath: reload start")
if err := o.Executer.IsExecutable(multipathCmd); err != nil {
return err
}
args := []string{}
_, err := o.Executer.ExecuteWithTimeout(TimeOutMultipathCmd, multipathCmd, args)
if err != nil {
return err
}
args = []string{"-r"}
_, err = o.Executer.ExecuteWithTimeout(TimeOutMultipathCmd, multipathCmd, args)
if err != nil {
return err
}
logger.Infof("ReloadMultipath: reload finished successfully")
return nil
}
func (o OsDeviceConnectivityHelperGeneric) GetMpathdOutputForVolume(volumeIdVariations []string,
multipathdCommandFormatArgs []string) (string, error) {
mpathdOutput, err := o.Helper.WaitForDmToExist(volumeIdVariations, WaitForMpathRetries,
WaitForMpathWaitIntervalSec, multipathdCommandFormatArgs)
if err != nil {
return "", err
}
return mpathdOutput, nil
}
func (o OsDeviceConnectivityHelperGeneric) GetDmsPath(volumeIdVariations []string) (string, error) {
mpathdOutput, err := o.Helper.WaitForDmToExist(volumeIdVariations, WaitForMpathRetries,
WaitForMpathWaitIntervalSec, MultipathdWildcardsVolumeIdAndMpath)
if err != nil {
return "", err
}
dms := o.Helper.ExtractDmFieldValues(volumeIdVariations, mpathdOutput)
return o.Helper.GetFullDmPath(dms, volumeIdVariations[0])
}
func (OsDeviceConnectivityHelperGeneric) GetVolumeIdVariations(volumeUuid string) []string {
volumeUuidLower := strings.ToLower(volumeUuid)
volumeNguid := convertScsiIdToNguid(volumeUuidLower)
return []string{volumeUuidLower, volumeNguid}
}
func (o OsDeviceConnectivityHelperGeneric) GetMpathDeviceName(volumePath string) (string, error) {
procMountsFileContent, err := ioutil.ReadFile(procMountsFilePath)
if err != nil {
return "", err
}
procMounts := string(procMountsFileContent)
return o.Helper.GetMpathDeviceNameFromProcMounts(procMounts, volumePath)
}
func (o OsDeviceConnectivityHelperGeneric) IsAnyVariationInMpathVolumeId(mpathVolumeId string, volumeIdVariations []string) bool {
for _, volumeIdVariation := range volumeIdVariations {
if strings.Contains(mpathVolumeId, volumeIdVariation) {
return true
}
}
return false
}
func (o OsDeviceConnectivityHelperGeneric) IsDmName(mpathDeviceName string) bool {
return strings.Contains(mpathDeviceName, "mpath")
}
//go:generate mockgen -destination=../../../mocks/mock_GetDmsPathHelperInterface.go -package=mocks github.com/ibm/ibm-block-csi-driver/node/pkg/driver/device_connectivity GetDmsPathHelperInterface
type GetDmsPathHelperInterface interface {
WaitForDmToExist(volumeIdVariations []string, maxRetries int, intervalSeconds int, multipathdCommandFormatArgs []string) (string, error)
ExtractDmFieldValues(dmFilterValues []string, mpathdOutput string) map[string]bool
GetFullDmPath(dms map[string]bool, volumeId string) (string, error)
IsIndicatorMatchesFilterValues(dmFilterValues []string, dmFieldValue string) bool
GetMpathDeviceNameFromProcMounts(procMounts string, volumePath string) (string, error)
ExtractVolumeId(mpathDeviceName string, mpathdOutput string) (string, error)
}
type GetDmsPathHelperGeneric struct {
executer executer.ExecuterInterface
}
func NewGetDmsPathHelperGeneric(executer executer.ExecuterInterface) GetDmsPathHelperInterface {
return &GetDmsPathHelperGeneric{executer: executer}
}
func (o GetDmsPathHelperGeneric) ExtractVolumeId(mpathDeviceName string, mpathdOutput string) (string, error) {
mpathDeviceNames := []string{mpathDeviceName}
volumeIds := o.ExtractDmFieldValues(mpathDeviceNames, mpathdOutput)
volumeId, err := getUniqueDmFieldValue(volumeIds, mpathDeviceName)
if err != nil {
return "", err
}
if volumeId == "" {
return "", &VolumeIdNotFoundForMultipathDeviceNameError{mpathDeviceName}
}
return volumeId, nil
}
func convertScsiIdToNguid(scsiId string) string {
logger.Infof("Converting scsi uuid : %s to nguid", scsiId)
oui := scsiId[1:WwnOuiEnd]
vendorIdentifier := scsiId[WwnOuiEnd:WwnVendorIdentifierEnd]
vendorIdentifierExtension := scsiId[WwnVendorIdentifierEnd:]
finalNguid := vendorIdentifierExtension + oui + "0" + vendorIdentifier
logger.Infof("Nguid is : %s", finalNguid)
return finalNguid
}
func (o GetDmsPathHelperGeneric) WaitForDmToExist(volumeIdVariations []string, maxRetries int, intervalSeconds int,
multipathdCommandFormatArgs []string) (string, error) {
formatTemplate := strings.Join(multipathdCommandFormatArgs, mpathdSeparator)
args := []string{"show", "maps", "raw", "format", "\"", formatTemplate, "\""}
logger.Debugf("Waiting for dm to exist")
var lastErr error
for i := 0; i < maxRetries; i++ {
out, err := o.executer.ExecuteWithTimeout(TimeOutMultipathdCmd, multipathdCmd, args)
if err != nil {
lastErr = err
logger.Warningf("multipathd show maps failed (attempt %d/%d), retrying: %v", i+1, maxRetries, err)
time.Sleep(time.Second * time.Duration(intervalSeconds))
continue
}
lastErr = nil
dms := string(out)
for _, volumeIdVariation := range volumeIdVariations {
if strings.Contains(dms, volumeIdVariation) {
return dms, nil
}
}
time.Sleep(time.Second * time.Duration(intervalSeconds))
}
if lastErr != nil {
return "", lastErr
}
return "", &MultipathDeviceNotFoundForVolumeError{volumeIdVariations[0]}
}
func (o GetDmsPathHelperGeneric) ExtractDmFieldValues(dmFilterValues []string, mpathdOutput string) map[string]bool {
dmFieldValues := make(map[string]bool)
scanner := bufio.NewScanner(strings.NewReader(mpathdOutput))
for scanner.Scan() {
dmIndicator, dmFieldValue := o.getLineParts(scanner)
if o.IsIndicatorMatchesFilterValues(dmFilterValues, dmIndicator) {
dmFieldValues[dmFieldValue] = true
logger.Infof("ExtractDmFieldValues: found: %s for: %s", dmFieldValue, dmIndicator)
}
}
return dmFieldValues
}
func (GetDmsPathHelperGeneric) getLineParts(scanner *bufio.Scanner) (string, string) {
deviceLine := scanner.Text()
lineParts := strings.Split(deviceLine, mpathdSeparator)
return lineParts[0], lineParts[1]
}
func (o GetDmsPathHelperGeneric) IsIndicatorMatchesFilterValues(dmFilterValues []string, indicatorValue string) bool {
for _, filterValue := range dmFilterValues {
if strings.Contains(indicatorValue, filterValue) {
return true
}
}
return false
}
func (GetDmsPathHelperGeneric) GetFullDmPath(dms map[string]bool, volumeId string) (string, error) {
dm, err := getUniqueDmFieldValue(dms, volumeId)
if err != nil {
return "", err
}
dmPath := filepath.Join(DevPath, filepath.Base(strings.TrimSpace(dm)))
return dmPath, nil
}
func getUniqueDmFieldValue(dmFieldValues map[string]bool, filter string) (string, error) {
if len(dmFieldValues) > 1 {
return "", &MultipleDmFieldValuesError{filter, dmFieldValues}
}
var dmFieldValue string
for dmFieldValue = range dmFieldValues {
break // because its a single value in the map(1 mpath device, if not it should fail above), so just take the first
}
return dmFieldValue, nil
}
func (GetDmsPathHelperGeneric) GetMpathDeviceNameFromProcMounts(procMounts string, volumePath string) (string, error) {
scanner := bufio.NewScanner(strings.NewReader(procMounts))
for scanner.Scan() {
procMountLine := scanner.Text()
if strings.Contains(procMountLine, volumePath) {
return extractMpathDeviceName(procMountLine), nil
}
}
return "", &MultipathDeviceNotFoundForVolumePathError{volumePath}
}
func extractMpathDeviceName(procMountLine string) string {
lineParts := strings.Fields(procMountLine)
mpathDevicePath := lineParts[0]
return filepath.Base(mpathDevicePath)
}