Skip to content

Commit b5a02fd

Browse files
author
Utkarsh Mani Tripathi
committed
add code to discover the target
This commit add the feature to discover the target based on the flag Discovery, which can be set while initializing iscsi.Connector structure. It also adds another flag CHAPDiscovery to proceed to discover the target without secrets. Signed-off-by: Utkarsh Mani Tripathi <[email protected]>
1 parent c545557 commit b5a02fd

File tree

2 files changed

+96
-21
lines changed

2 files changed

+96
-21
lines changed

iscsi/iscsi.go

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ type Connector struct {
4646
Multipath bool `json:"multipath"`
4747
RetryCount int32 `json:"retry_count"`
4848
CheckInterval int32 `json:"check_interval"`
49+
Discovery bool `json:"discovery"`
50+
CHAPDiscovery bool `json:"chap_discovery"`
4951
}
5052

5153
func init() {
@@ -221,7 +223,7 @@ func getMultipathDisk(path string) (string, error) {
221223

222224
// Connect attempts to connect a volume to this node using the provided Connector info
223225
func Connect(c Connector) (string, error) {
224-
226+
var lastErr error
225227
if c.RetryCount == 0 {
226228
c.RetryCount = 10
227229
}
@@ -249,6 +251,11 @@ func Connect(c Connector) (string, error) {
249251
for _, p := range c.TargetPortals {
250252
debug.Printf("process portal: %s\n", p)
251253
baseArgs := []string{"-m", "node", "-T", c.TargetIqn, "-p", p}
254+
// Rescan sessions to discover newly mapped LUNs. Do not specify the interface when rescanning
255+
// to avoid establishing additional sessions to the same target.
256+
if _, err := iscsiCmd(append(baseArgs, []string{"-R"}...)...); err != nil {
257+
debug.Printf("failed to rescan session, err: %v", err)
258+
}
252259

253260
// create our devicePath that we'll be looking for based on the transport being used
254261
if c.Port != "" {
@@ -270,31 +277,45 @@ func Connect(c Connector) (string, error) {
270277
}
271278
}
272279

273-
// create db entry
274-
args := append(baseArgs, []string{"-I", iFace, "-o", "new"}...)
275-
debug.Printf("create the new record: %s\n", args)
280+
if c.Discovery {
281+
// build discoverydb and discover iscsi target
282+
if err := Discovery(p, iFace, c.DiscoverySecrets, c.CHAPDiscovery); err != nil {
283+
debug.Printf("Error in discovery of the target: %s\n", err.Error())
284+
lastErr = err
285+
continue
286+
}
287+
}
288+
276289
// Make sure we don't log the secrets
277-
err := CreateDBEntry(c.TargetIqn, p, iFace, c.DiscoverySecrets, c.SessionSecrets)
290+
err := CreateDBEntry(c.TargetIqn, p, iFace, c.DiscoverySecrets, c.SessionSecrets, c.CHAPDiscovery)
278291
if err != nil {
279292
debug.Printf("Error creating db entry: %s\n", err.Error())
280293
continue
281294
}
295+
282296
// perform the login
283297
err = Login(c.TargetIqn, p)
284298
if err != nil {
285-
return "", err
299+
debug.Printf("failed to login, err: %v", err)
300+
lastErr = err
301+
continue
286302
}
287303
retries := int(c.RetryCount / c.CheckInterval)
288304
if exists, err := waitForPathToExist(&devicePath, retries, int(c.CheckInterval), iscsiTransport); exists {
289305
devicePaths = append(devicePaths, devicePath)
290306
continue
291307
} else if err != nil {
292-
return "", err
293-
}
294-
if len(devicePaths) < 1 {
295-
return "", fmt.Errorf("failed to find device path: %s", devicePath)
308+
lastErr = fmt.Errorf("Couldn't attach disk, err: %v", err)
296309
}
310+
}
311+
312+
if len(devicePaths) < 1 {
313+
iscsiCmd([]string{"-m", "iface", "-I", iFace, "-o", "delete"}...)
314+
return "", fmt.Errorf("failed to find device path: %s, last error seen: %v", devicePaths, lastErr)
315+
}
297316

317+
if lastErr != nil {
318+
debug.Printf("Last error occured during iscsi init: \n%v", lastErr)
298319
}
299320

300321
for i, path := range devicePaths {

iscsi/iscsiadm.go

Lines changed: 65 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,17 @@ func ShowInterface(iface string) (string, error) {
8888
}
8989

9090
// CreateDBEntry sets up a node entry for the specified tgt in the nodes iscsi nodes db
91-
func CreateDBEntry(tgtIQN, portal, iFace string, discoverySecrets, sessionSecrets Secrets) error {
91+
func CreateDBEntry(tgtIQN, portal, iFace string, discoverySecrets, sessionSecrets Secrets, chapDiscovery bool) error {
9292
debug.Println("Begin CreateDBEntry...")
93+
if !chapDiscovery {
94+
return nil
95+
}
9396
baseArgs := []string{"-m", "node", "-T", tgtIQN, "-p", portal}
9497
_, err := iscsiCmd(append(baseArgs, []string{"-I", iFace, "-o", "new"}...)...)
9598
if err != nil {
9699
return err
97100
}
101+
98102
if discoverySecrets.SecretsType == "chap" {
99103
debug.Printf("Setting CHAP Discovery...")
100104
createCHAPEntries(baseArgs, discoverySecrets, true)
@@ -103,25 +107,59 @@ func CreateDBEntry(tgtIQN, portal, iFace string, discoverySecrets, sessionSecret
103107
if sessionSecrets.SecretsType == "chap" {
104108
debug.Printf("Setting CHAP Session...")
105109
createCHAPEntries(baseArgs, sessionSecrets, false)
106-
107110
}
111+
108112
return err
109113

110114
}
111115

116+
func updateISCSIDiscoverydb(tp, iface string, discoverySecrets Secrets) error {
117+
baseArgs := []string{"-m", "discoverydb", "-t", "sendtargets", "-p", tp, "-I", iface}
118+
_, err := iscsiCmd(append(baseArgs, []string{"-o", "update", "-n", "discovery.sendtargets.auth.authmethod", "-v", "CHAP"}...)...)
119+
if err != nil {
120+
return fmt.Errorf("failed to update discoverydb with CHAP, err: %v", err)
121+
}
122+
123+
return createCHAPEntries(baseArgs, discoverySecrets, true)
124+
}
125+
126+
// Discovery discover the iscsi target
127+
func Discovery(tp, iface string, discoverySecrets Secrets, chapDiscovery bool) error {
128+
debug.Println("Begin Discovery...")
129+
baseArgs := []string{"-m", "discoverydb", "-t", "sendtargets", "-p", tp, "-I", iface}
130+
out, err := iscsiCmd(append(baseArgs, []string{"-o", "new"}...)...)
131+
if err != nil {
132+
return fmt.Errorf("failed to create new entry of target in discoverydb, output: %v, err: %v", string(out), err)
133+
}
134+
135+
if chapDiscovery {
136+
if err := updateISCSIDiscoverydb(tp, iface, discoverySecrets); err != nil {
137+
return err
138+
}
139+
}
140+
141+
_, err = iscsiCmd(append(baseArgs, []string{"--discover"}...)...)
142+
if err != nil {
143+
//delete the discoverydb record
144+
iscsiCmd(append(baseArgs, []string{"-o", "delete"}...)...)
145+
return fmt.Errorf("failed to sendtargets to portal %s, err: %v", tp, err)
146+
}
147+
return nil
148+
}
149+
112150
func createCHAPEntries(baseArgs []string, secrets Secrets, discovery bool) error {
113151
args := []string{}
114152
debug.Printf("Begin createCHAPEntries (discovery=%t)...", discovery)
115153
if discovery {
116154
args = append(baseArgs, []string{"-o", "update",
117-
"-n", "node.discovery.auth.authmethod", "-v", "CHAP",
118-
"-n", "node.discovery.auth.username", "-v", secrets.UserName,
119-
"-n", "node.discovery.auth.password", "-v", secrets.Password}...)
155+
"-n", "discovery.sendtargets.auth.authmethod", "-v", "CHAP",
156+
"-n", "discovery.sendtargets.auth.username", "-v", secrets.UserName,
157+
"-n", "discovery.sendtargets.auth.password", "-v", secrets.Password}...)
120158
if secrets.UserNameIn != "" {
121-
args = append(args, []string{"-n", "node.discovery.auth.username_in", "-v", secrets.UserNameIn}...)
159+
args = append(args, []string{"-n", "discovery.sendtargets.auth.username_in", "-v", secrets.UserNameIn}...)
122160
}
123-
if secrets.UserNameIn != "" {
124-
args = append(args, []string{"-n", "node.discovery.auth.password_in", "-v", secrets.PasswordIn}...)
161+
if secrets.PasswordIn != "" {
162+
args = append(args, []string{"-n", "discovery.sendtargets.auth.password_in", "-v", secrets.PasswordIn}...)
125163
}
126164

127165
} else {
@@ -133,13 +171,17 @@ func createCHAPEntries(baseArgs []string, secrets Secrets, discovery bool) error
133171
if secrets.UserNameIn != "" {
134172
args = append(args, []string{"-n", "node.session.auth.username_in", "-v", secrets.UserNameIn}...)
135173
}
136-
if secrets.UserNameIn != "" {
174+
if secrets.PasswordIn != "" {
137175
args = append(args, []string{"-n", "node.session.auth.password_in", "-v", secrets.PasswordIn}...)
138176
}
139177
}
178+
140179
_, err := iscsiCmd(args...)
141-
return err
180+
if err != nil {
181+
return fmt.Errorf("failed to update discoverydb with CHAP, err: %v", err)
182+
}
142183

184+
return nil
143185
}
144186

145187
// GetSessions retrieves a list of current iscsi sessions on the node
@@ -151,7 +193,13 @@ func GetSessions() (string, error) {
151193

152194
// Login performs an iscsi login for the specified target
153195
func Login(tgtIQN, portal string) error {
154-
_, err := iscsiCmd([]string{"-m", "node", "-T", tgtIQN, "-p", portal, "-l"}...)
196+
baseArgs := []string{"-m", "node", "-T", tgtIQN, "-p", portal}
197+
_, err := iscsiCmd(append(baseArgs, []string{"-l"}...)...)
198+
if err != nil {
199+
//delete the node record from database
200+
iscsiCmd(append(baseArgs, []string{"-o", "delete"}...)...)
201+
return fmt.Errorf("failed to sendtargets to portal %s, err: %v", portal, err)
202+
}
155203
return err
156204
}
157205

@@ -173,5 +221,11 @@ func DeleteDBEntry(tgtIQN string) error {
173221
args := []string{"-m", "node", "-T", tgtIQN, "-o", "delete"}
174222
iscsiCmd(args...)
175223
return nil
224+
}
176225

226+
// DeleteIFace delete the iface
227+
func DeleteIFace(iface string) error {
228+
debug.Println("Begin DeleteIFace...")
229+
iscsiCmd([]string{"-m", "iface", "-I", iface, "-o", "delete"}...)
230+
return nil
177231
}

0 commit comments

Comments
 (0)