Skip to content

Commit 3b2e896

Browse files
authored
Merge pull request #4 from Seagate/feat/debug
v0.5.4 storing devicePath, checking slaves, and setting Multipath to …
2 parents d7b7d59 + e98a1cc commit 3b2e896

File tree

3 files changed

+36
-19
lines changed

3 files changed

+36
-19
lines changed

iscsi/iscsi.go

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ type Connector struct {
5555
// DevicePath is dm-x for a multipath device, and sdx for a normal device.
5656
DevicePath string `json:"device_path"`
5757

58-
RetryCount int32 `json:"retry_count"`
59-
CheckInterval int32 `json:"check_interval"`
60-
DoDiscovery bool `json:"do_discovery"`
61-
DoCHAPDiscovery bool `json:"do_chap_discovery"`
62-
TargetIqn string `json:"target_iqn"`
58+
RetryCount int32 `json:"retry_count"`
59+
CheckInterval int32 `json:"check_interval"`
60+
DoDiscovery bool `json:"do_discovery"`
61+
DoCHAPDiscovery bool `json:"do_chap_discovery"`
62+
TargetIqn string `json:"target_iqn"`
6363
TargetPortals []string `json:"target_portals"`
6464
}
6565

@@ -147,15 +147,16 @@ func waitForPathToExist(devicePath *string, maxRetries, intervalSeconds int, dev
147147
}
148148

149149
func waitForPathToExistImpl(devicePath *string, maxRetries, intervalSeconds int, deviceTransport string, osStat statFunc, filepathGlob globFunc) (bool, error) {
150-
if devicePath == nil {
151-
return false, fmt.Errorf("unable to check unspecified devicePath")
150+
if devicePath == nil || *devicePath == "" {
151+
return false, fmt.Errorf("unable to check nil or unspecified devicePath")
152152
}
153153

154154
var err error
155155
for i := 0; i < maxRetries; i++ {
156156
err = nil
157157
if deviceTransport == "tcp" {
158158
_, err = osStat(*devicePath)
159+
debug.Printf("os stat device: exist %v device %v", !os.IsNotExist(err), *devicePath)
159160
if err != nil && !os.IsNotExist(err) {
160161
debug.Printf("Error attempting to stat device: %s", err.Error())
161162
return false, err
@@ -164,13 +165,15 @@ func waitForPathToExistImpl(devicePath *string, maxRetries, intervalSeconds int,
164165
}
165166

166167
} else {
168+
debug.Printf("filepathGlob: %s", *devicePath)
167169
fpath, _ := filepathGlob(*devicePath)
168170
if fpath == nil {
169171
err = os.ErrNotExist
170172
} else {
171173
// There might be a case that fpath contains multiple device paths if
172174
// multiple PCI devices connect to same iscsi target. We handle this
173175
// case at subsequent logic. Pick up only first path here.
176+
debug.Printf("set - devicePath %s", fpath[0])
174177
*devicePath = fpath[0]
175178
}
176179
}
@@ -210,6 +213,7 @@ func getMultipathDisk(path string) (string, error) {
210213
}
211214
for _, dmPath := range dmPaths {
212215
sdevices, err := filepath.Glob(filepath.Join(dmPath, "slaves", "*"))
216+
debug.Printf("dmPath=%v, sdevices=%v", dmPath, sdevices)
213217
if err != nil {
214218
debug.Printf("Glob error: %s", err)
215219
}
@@ -230,7 +234,7 @@ func getMultipathDisk(path string) (string, error) {
230234
}
231235

232236
// Connect attempts to connect a volume to this node using the provided Connector info
233-
func Connect(c Connector) (string, error) {
237+
func Connect(c *Connector) (string, error) {
234238
var lastErr error
235239
if c.RetryCount == 0 {
236240
c.RetryCount = 10
@@ -279,7 +283,10 @@ func Connect(c Connector) (string, error) {
279283

280284
exists, _ := sessionExists(p, target.Iqn)
281285
if exists {
282-
if exists, err := waitForPathToExist(&devicePath, 1, 1, iscsiTransport); exists {
286+
debug.Printf("Session already exists, checking if device path %q exists", devicePath)
287+
exists, err := waitForPathToExist(&devicePath, int(c.RetryCount), int(c.CheckInterval), iscsiTransport)
288+
debug.Printf("waitForPathToExist: exists=%v err=%v", exists, err)
289+
if exists {
283290
debug.Printf("Appending device path: %s", devicePath)
284291
devicePaths = append(devicePaths, devicePath)
285292
continue
@@ -334,15 +341,19 @@ func Connect(c Connector) (string, error) {
334341
for i, path := range devicePaths {
335342
if path != "" {
336343
if mappedDevicePath, err := getMultipathDisk(path); mappedDevicePath != "" {
344+
debug.Printf("update devicePaths[%d] before=%v, after=%v", i, devicePaths[i], mappedDevicePath)
337345
devicePaths[i] = mappedDevicePath
346+
c.Multipath = true
338347
if err != nil {
339348
return "", err
340349
}
341350
}
342351
}
343352
}
344-
debug.Printf("After connect we're returning devicePaths: %s", devicePaths)
353+
debug.Printf("After connect we're returning devicePaths: %v", devicePaths)
345354
if len(devicePaths) > 0 {
355+
c.DevicePath = devicePaths[0]
356+
debug.Printf("set -- devicePath %s", c.DevicePath)
346357
return devicePaths[0], err
347358

348359
}
@@ -373,7 +384,7 @@ func DisconnectVolume(c Connector) error {
373384

374385
debug.Printf("Disconnecting volume in path %s.\n", c.DevicePath)
375386
if c.Multipath {
376-
debug.Printf("Removing multipath device.\n")
387+
debug.Printf("Removing multipath device %s\n", c.DevicePath)
377388
devices, err := GetSysDevicesFromMultipathDevice(c.DevicePath)
378389
if err != nil {
379390
return err
@@ -453,16 +464,19 @@ func PersistConnector(c *Connector, filePath string) error {
453464
// GetConnectorFromFile attempts to create a Connector using the specified json file (ie /var/lib/pfile/myConnector.json)
454465
func GetConnectorFromFile(filePath string) (*Connector, error) {
455466
f, err := ioutil.ReadFile(filePath)
467+
debug.Printf("GetConnectorFromFile (%s), err=%v\n", filePath, err)
456468
if err != nil {
457469
return &Connector{}, err
458-
459470
}
471+
460472
data := Connector{}
461473
err = json.Unmarshal(f, &data)
462474
if err != nil {
463475
return &Connector{}, err
464476
}
465477

478+
debug.Printf("data: %+v\n", data)
479+
466480
return &data, nil
467481

468482
}

iscsi/iscsiadm.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ func (e *CmdError) Error() string {
3636

3737
func iscsiCmd(args ...string) (string, error) {
3838
cmd := execCommand("iscsiadm", args...)
39+
debug.Printf("Run iscsiadm command: %s", strings.Join(append([]string{"iscsiadm"}, args...), " "))
3940
var stdout bytes.Buffer
4041
var iscsiadmError error
4142
cmd.Stdout = &stdout
@@ -180,6 +181,7 @@ func GetSessions() (string, error) {
180181

181182
// Login performs an iscsi login for the specified target
182183
func Login(tgtIQN, portal string) error {
184+
debug.Println("Begin Login...")
183185
baseArgs := []string{"-m", "node", "-T", tgtIQN, "-p", portal}
184186
_, err := iscsiCmd(append(baseArgs, []string{"-l"}...)...)
185187
if err != nil {

iscsi/multipath.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,10 @@ func ExecWithTimeout(command string, args []string, timeout time.Duration) ([]by
4747
// GetSysDevicesFromMultipathDevice gets all slaves for multipath device dm-x
4848
// in /sys/block/dm-x/slaves/
4949
func GetSysDevicesFromMultipathDevice(device string) ([]string, error) {
50-
debug.Printf("Getting all slaves for multipath device %s.\n", device)
51-
deviceSlavePath := filepath.Join(sysBlockPath, device, "slaves")
50+
debug.Printf("Getting all slaves for multipath device %q\n", device)
51+
deviceSlavePath := filepath.Join(sysBlockPath, filepath.Base(device), "slaves")
5252
slaves, err := ioutil.ReadDir(deviceSlavePath)
53+
debug.Printf("Read device slaves path: %q, slaves=%v, err=%v\n", deviceSlavePath, slaves, err)
5354
if err != nil {
5455
if os.IsNotExist(err) {
5556
return nil, nil
@@ -62,28 +63,28 @@ func GetSysDevicesFromMultipathDevice(device string) ([]string, error) {
6263
for _, slave := range slaves {
6364
s = append(s, slave.Name())
6465
}
65-
debug.Printf("Found slaves: %v.\n", s)
66+
debug.Printf("Found slaves: %v\n", s)
6667
return s, nil
6768
}
6869

6970
// FlushMultipathDevice flushes a multipath device dm-x with command multipath -f /dev/dm-x
7071
func FlushMultipathDevice(device string) error {
71-
debug.Printf("Flushing multipath device '%v'.\n", device)
72+
debug.Printf("Flushing multipath device %q\n", device)
7273

73-
fullDevice := filepath.Join(devPath, device)
74+
fullDevice := device
7475
timeout := 5 * time.Second
7576
_, err := execWithTimeout("multipath", []string{"-f", fullDevice}, timeout)
7677

7778
if err != nil {
7879
if _, e := os.Stat(fullDevice); os.IsNotExist(e) {
79-
debug.Printf("Multipath device %v was deleted.\n", device)
80+
debug.Printf("Multipath device %q was deleted\n", device)
8081
} else {
8182
debug.Printf("Command 'multipath -f %v' did not succeed to delete the device: %v\n", fullDevice, err)
8283
return err
8384
}
8485
}
8586

86-
debug.Printf("Finshed flushing multipath device %v.\n", device)
87+
debug.Printf("Finshed flushing multipath device %q\n", device)
8788
return nil
8889
}
8990

0 commit comments

Comments
 (0)