Skip to content

Commit 0fd8753

Browse files
author
Lou
committed
support device with multiple targets
Signed-off-by: Lou <[email protected]>
1 parent 1430b53 commit 0fd8753

File tree

1 file changed

+27
-24
lines changed

1 file changed

+27
-24
lines changed

iscsi/iscsi.go

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,25 @@ type iscsiSession struct {
3232
Name string
3333
}
3434

35+
type TargetInfo struct {
36+
Iqn, Portal string
37+
}
38+
3539
//Connector provides a struct to hold all of the needed parameters to make our iscsi connection
3640
type Connector struct {
37-
VolumeName string `json:"volume_name"`
38-
TargetIqn string `json:"target_iqn"`
39-
TargetPortals []string `json:"target_portals"`
40-
Port string `json:"port"`
41-
Lun int32 `json:"lun"`
42-
AuthType string `json:"auth_type"`
43-
DiscoverySecrets Secrets `json:"discovery_secrets"`
44-
SessionSecrets Secrets `json:"session_secrets"`
45-
Interface string `json:"interface"`
46-
Multipath bool `json:"multipath"`
47-
RetryCount int32 `json:"retry_count"`
48-
CheckInterval int32 `json:"check_interval"`
49-
DoDiscovery bool `json:"do_discovery"`
50-
DoCHAPDiscovery bool `json:"do_chap_discovery"`
41+
VolumeName string `json:"volume_name"`
42+
Targets []TargetInfo `json:"targets"`
43+
Port string `json:"port"`
44+
Lun int32 `json:"lun"`
45+
AuthType string `json:"auth_type"`
46+
DiscoverySecrets Secrets `json:"discovery_secrets"`
47+
SessionSecrets Secrets `json:"session_secrets"`
48+
Interface string `json:"interface"`
49+
Multipath bool `json:"multipath"`
50+
RetryCount int32 `json:"retry_count"`
51+
CheckInterval int32 `json:"check_interval"`
52+
DoDiscovery bool `json:"do_discovery"`
53+
DoCHAPDiscovery bool `json:"do_chap_discovery"`
5154
}
5255

5356
func init() {
@@ -248,9 +251,9 @@ func Connect(c Connector) (string, error) {
248251
}
249252
iscsiTransport := extractTransportName(out)
250253

251-
for _, p := range c.TargetPortals {
252-
debug.Printf("process portal: %s\n", p)
253-
baseArgs := []string{"-m", "node", "-T", c.TargetIqn, "-p", p}
254+
for _, target := range c.Targets {
255+
debug.Printf("process targetIqn: %s, portal: %s\n", target.Iqn, target.Portal)
256+
baseArgs := []string{"-m", "node", "-T", target.Iqn, "-target.Portal", target.Portal}
254257
// Rescan sessions to discover newly mapped LUNs. Do not specify the interface when rescanning
255258
// to avoid establishing additional sessions to the same target.
256259
if _, err := iscsiCmd(append(baseArgs, []string{"-R"}...)...); err != nil {
@@ -259,14 +262,14 @@ func Connect(c Connector) (string, error) {
259262

260263
// create our devicePath that we'll be looking for based on the transport being used
261264
if c.Port != "" {
262-
p = strings.Join([]string{p, c.Port}, ":")
265+
target.Portal = strings.Join([]string{target.Portal, c.Port}, ":")
263266
}
264-
devicePath := strings.Join([]string{"/dev/disk/by-path/ip", p, "iscsi", c.TargetIqn, "lun", fmt.Sprint(c.Lun)}, "-")
267+
devicePath := strings.Join([]string{"/dev/disk/by-path/ip", target.Portal, "iscsi", target.Iqn, "lun", fmt.Sprint(c.Lun)}, "-")
265268
if iscsiTransport != "tcp" {
266-
devicePath = strings.Join([]string{"/dev/disk/by-path/pci", "*", "ip", p, "iscsi", c.TargetIqn, "lun", fmt.Sprint(c.Lun)}, "-")
269+
devicePath = strings.Join([]string{"/dev/disk/by-path/pci", "*", "ip", target.Portal, "iscsi", target.Iqn, "lun", fmt.Sprint(c.Lun)}, "-")
267270
}
268271

269-
exists, _ := sessionExists(p, c.TargetIqn)
272+
exists, _ := sessionExists(target.Portal, target.Iqn)
270273
if exists {
271274
if exists, err := waitForPathToExist(&devicePath, 1, 1, iscsiTransport); exists {
272275
debug.Printf("Appending device path: %s", devicePath)
@@ -279,22 +282,22 @@ func Connect(c Connector) (string, error) {
279282

280283
if c.DoDiscovery {
281284
// build discoverydb and discover iscsi target
282-
if err := Discovery(p, iFace, c.DiscoverySecrets, c.DoCHAPDiscovery); err != nil {
285+
if err := Discovery(target.Portal, iFace, c.DiscoverySecrets, c.DoCHAPDiscovery); err != nil {
283286
debug.Printf("Error in discovery of the target: %s\n", err.Error())
284287
lastErr = err
285288
continue
286289
}
287290
}
288291

289292
// Make sure we don't log the secrets
290-
err := CreateDBEntry(c.TargetIqn, p, iFace, c.DiscoverySecrets, c.SessionSecrets, c.DoCHAPDiscovery)
293+
err := CreateDBEntry(target.Iqn, target.Portal, iFace, c.DiscoverySecrets, c.SessionSecrets, c.DoCHAPDiscovery)
291294
if err != nil {
292295
debug.Printf("Error creating db entry: %s\n", err.Error())
293296
continue
294297
}
295298

296299
// perform the login
297-
err = Login(c.TargetIqn, p)
300+
err = Login(target.Iqn, target.Portal)
298301
if err != nil {
299302
debug.Printf("failed to login, err: %v", err)
300303
lastErr = err

0 commit comments

Comments
 (0)