Skip to content

Commit 1efb74c

Browse files
authored
Merge pull request #13 from 27149chen/support_device_with_multiple_targets
support device with multiple targets
2 parents 1430b53 + 0b6de4b commit 1efb74c

File tree

1 file changed

+33
-24
lines changed

1 file changed

+33
-24
lines changed

iscsi/iscsi.go

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import (
1616
"time"
1717
)
1818

19+
const defaultPort = "3260"
20+
1921
var (
2022
debug *log.Logger
2123
execCommand = exec.Command
@@ -32,22 +34,26 @@ type iscsiSession struct {
3234
Name string
3335
}
3436

37+
type TargetInfo struct {
38+
Iqn string `json:"iqn"`
39+
Portal string `json:"portal"`
40+
Port string `json:"port"`
41+
}
42+
3543
//Connector provides a struct to hold all of the needed parameters to make our iscsi connection
3644
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"`
45+
VolumeName string `json:"volume_name"`
46+
Targets []TargetInfo `json:"targets"`
47+
Lun int32 `json:"lun"`
48+
AuthType string `json:"auth_type"`
49+
DiscoverySecrets Secrets `json:"discovery_secrets"`
50+
SessionSecrets Secrets `json:"session_secrets"`
51+
Interface string `json:"interface"`
52+
Multipath bool `json:"multipath"`
53+
RetryCount int32 `json:"retry_count"`
54+
CheckInterval int32 `json:"check_interval"`
55+
DoDiscovery bool `json:"do_discovery"`
56+
DoCHAPDiscovery bool `json:"do_chap_discovery"`
5157
}
5258

5359
func init() {
@@ -248,25 +254,28 @@ func Connect(c Connector) (string, error) {
248254
}
249255
iscsiTransport := extractTransportName(out)
250256

251-
for _, p := range c.TargetPortals {
252-
debug.Printf("process portal: %s\n", p)
253-
baseArgs := []string{"-m", "node", "-T", c.TargetIqn, "-p", p}
257+
for _, target := range c.Targets {
258+
debug.Printf("process targetIqn: %s, portal: %s\n", target.Iqn, target.Portal)
259+
baseArgs := []string{"-m", "node", "-T", target.Iqn, "-p", target.Portal}
254260
// Rescan sessions to discover newly mapped LUNs. Do not specify the interface when rescanning
255261
// to avoid establishing additional sessions to the same target.
256262
if _, err := iscsiCmd(append(baseArgs, []string{"-R"}...)...); err != nil {
257263
debug.Printf("failed to rescan session, err: %v", err)
258264
}
259265

260266
// create our devicePath that we'll be looking for based on the transport being used
261-
if c.Port != "" {
262-
p = strings.Join([]string{p, c.Port}, ":")
267+
port := defaultPort
268+
if target.Port != "" {
269+
port = target.Port
263270
}
264-
devicePath := strings.Join([]string{"/dev/disk/by-path/ip", p, "iscsi", c.TargetIqn, "lun", fmt.Sprint(c.Lun)}, "-")
271+
// portal with port
272+
p := strings.Join([]string{target.Portal, port}, ":")
273+
devicePath := strings.Join([]string{"/dev/disk/by-path/ip", p, "iscsi", target.Iqn, "lun", fmt.Sprint(c.Lun)}, "-")
265274
if iscsiTransport != "tcp" {
266-
devicePath = strings.Join([]string{"/dev/disk/by-path/pci", "*", "ip", p, "iscsi", c.TargetIqn, "lun", fmt.Sprint(c.Lun)}, "-")
275+
devicePath = strings.Join([]string{"/dev/disk/by-path/pci", "*", "ip", p, "iscsi", target.Iqn, "lun", fmt.Sprint(c.Lun)}, "-")
267276
}
268277

269-
exists, _ := sessionExists(p, c.TargetIqn)
278+
exists, _ := sessionExists(p, target.Iqn)
270279
if exists {
271280
if exists, err := waitForPathToExist(&devicePath, 1, 1, iscsiTransport); exists {
272281
debug.Printf("Appending device path: %s", devicePath)
@@ -287,14 +296,14 @@ func Connect(c Connector) (string, error) {
287296
}
288297

289298
// Make sure we don't log the secrets
290-
err := CreateDBEntry(c.TargetIqn, p, iFace, c.DiscoverySecrets, c.SessionSecrets, c.DoCHAPDiscovery)
299+
err := CreateDBEntry(target.Iqn, p, iFace, c.DiscoverySecrets, c.SessionSecrets, c.DoCHAPDiscovery)
291300
if err != nil {
292301
debug.Printf("Error creating db entry: %s\n", err.Error())
293302
continue
294303
}
295304

296305
// perform the login
297-
err = Login(c.TargetIqn, p)
306+
err = Login(target.Iqn, p)
298307
if err != nil {
299308
debug.Printf("failed to login, err: %v", err)
300309
lastErr = err

0 commit comments

Comments
 (0)