Skip to content

Commit

Permalink
Merge pull request #4 from Seagate/feat/debug
Browse files Browse the repository at this point in the history
v0.5.4 storing devicePath, checking slaves, and setting Multipath to …
  • Loading branch information
jskazinski authored Sep 14, 2021
2 parents d7b7d59 + e98a1cc commit 3b2e896
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 19 deletions.
38 changes: 26 additions & 12 deletions iscsi/iscsi.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ type Connector struct {
// DevicePath is dm-x for a multipath device, and sdx for a normal device.
DevicePath string `json:"device_path"`

RetryCount int32 `json:"retry_count"`
CheckInterval int32 `json:"check_interval"`
DoDiscovery bool `json:"do_discovery"`
DoCHAPDiscovery bool `json:"do_chap_discovery"`
TargetIqn string `json:"target_iqn"`
RetryCount int32 `json:"retry_count"`
CheckInterval int32 `json:"check_interval"`
DoDiscovery bool `json:"do_discovery"`
DoCHAPDiscovery bool `json:"do_chap_discovery"`
TargetIqn string `json:"target_iqn"`
TargetPortals []string `json:"target_portals"`
}

Expand Down Expand Up @@ -147,15 +147,16 @@ func waitForPathToExist(devicePath *string, maxRetries, intervalSeconds int, dev
}

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

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

} else {
debug.Printf("filepathGlob: %s", *devicePath)
fpath, _ := filepathGlob(*devicePath)
if fpath == nil {
err = os.ErrNotExist
} else {
// There might be a case that fpath contains multiple device paths if
// multiple PCI devices connect to same iscsi target. We handle this
// case at subsequent logic. Pick up only first path here.
debug.Printf("set - devicePath %s", fpath[0])
*devicePath = fpath[0]
}
}
Expand Down Expand Up @@ -210,6 +213,7 @@ func getMultipathDisk(path string) (string, error) {
}
for _, dmPath := range dmPaths {
sdevices, err := filepath.Glob(filepath.Join(dmPath, "slaves", "*"))
debug.Printf("dmPath=%v, sdevices=%v", dmPath, sdevices)
if err != nil {
debug.Printf("Glob error: %s", err)
}
Expand All @@ -230,7 +234,7 @@ func getMultipathDisk(path string) (string, error) {
}

// Connect attempts to connect a volume to this node using the provided Connector info
func Connect(c Connector) (string, error) {
func Connect(c *Connector) (string, error) {
var lastErr error
if c.RetryCount == 0 {
c.RetryCount = 10
Expand Down Expand Up @@ -279,7 +283,10 @@ func Connect(c Connector) (string, error) {

exists, _ := sessionExists(p, target.Iqn)
if exists {
if exists, err := waitForPathToExist(&devicePath, 1, 1, iscsiTransport); exists {
debug.Printf("Session already exists, checking if device path %q exists", devicePath)
exists, err := waitForPathToExist(&devicePath, int(c.RetryCount), int(c.CheckInterval), iscsiTransport)
debug.Printf("waitForPathToExist: exists=%v err=%v", exists, err)
if exists {
debug.Printf("Appending device path: %s", devicePath)
devicePaths = append(devicePaths, devicePath)
continue
Expand Down Expand Up @@ -334,15 +341,19 @@ func Connect(c Connector) (string, error) {
for i, path := range devicePaths {
if path != "" {
if mappedDevicePath, err := getMultipathDisk(path); mappedDevicePath != "" {
debug.Printf("update devicePaths[%d] before=%v, after=%v", i, devicePaths[i], mappedDevicePath)
devicePaths[i] = mappedDevicePath
c.Multipath = true
if err != nil {
return "", err
}
}
}
}
debug.Printf("After connect we're returning devicePaths: %s", devicePaths)
debug.Printf("After connect we're returning devicePaths: %v", devicePaths)
if len(devicePaths) > 0 {
c.DevicePath = devicePaths[0]
debug.Printf("set -- devicePath %s", c.DevicePath)
return devicePaths[0], err

}
Expand Down Expand Up @@ -373,7 +384,7 @@ func DisconnectVolume(c Connector) error {

debug.Printf("Disconnecting volume in path %s.\n", c.DevicePath)
if c.Multipath {
debug.Printf("Removing multipath device.\n")
debug.Printf("Removing multipath device %s\n", c.DevicePath)
devices, err := GetSysDevicesFromMultipathDevice(c.DevicePath)
if err != nil {
return err
Expand Down Expand Up @@ -453,16 +464,19 @@ func PersistConnector(c *Connector, filePath string) error {
// GetConnectorFromFile attempts to create a Connector using the specified json file (ie /var/lib/pfile/myConnector.json)
func GetConnectorFromFile(filePath string) (*Connector, error) {
f, err := ioutil.ReadFile(filePath)
debug.Printf("GetConnectorFromFile (%s), err=%v\n", filePath, err)
if err != nil {
return &Connector{}, err

}

data := Connector{}
err = json.Unmarshal(f, &data)
if err != nil {
return &Connector{}, err
}

debug.Printf("data: %+v\n", data)

return &data, nil

}
2 changes: 2 additions & 0 deletions iscsi/iscsiadm.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func (e *CmdError) Error() string {

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

// Login performs an iscsi login for the specified target
func Login(tgtIQN, portal string) error {
debug.Println("Begin Login...")
baseArgs := []string{"-m", "node", "-T", tgtIQN, "-p", portal}
_, err := iscsiCmd(append(baseArgs, []string{"-l"}...)...)
if err != nil {
Expand Down
15 changes: 8 additions & 7 deletions iscsi/multipath.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ func ExecWithTimeout(command string, args []string, timeout time.Duration) ([]by
// GetSysDevicesFromMultipathDevice gets all slaves for multipath device dm-x
// in /sys/block/dm-x/slaves/
func GetSysDevicesFromMultipathDevice(device string) ([]string, error) {
debug.Printf("Getting all slaves for multipath device %s.\n", device)
deviceSlavePath := filepath.Join(sysBlockPath, device, "slaves")
debug.Printf("Getting all slaves for multipath device %q\n", device)
deviceSlavePath := filepath.Join(sysBlockPath, filepath.Base(device), "slaves")
slaves, err := ioutil.ReadDir(deviceSlavePath)
debug.Printf("Read device slaves path: %q, slaves=%v, err=%v\n", deviceSlavePath, slaves, err)
if err != nil {
if os.IsNotExist(err) {
return nil, nil
Expand All @@ -62,28 +63,28 @@ func GetSysDevicesFromMultipathDevice(device string) ([]string, error) {
for _, slave := range slaves {
s = append(s, slave.Name())
}
debug.Printf("Found slaves: %v.\n", s)
debug.Printf("Found slaves: %v\n", s)
return s, nil
}

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

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

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

debug.Printf("Finshed flushing multipath device %v.\n", device)
debug.Printf("Finshed flushing multipath device %q\n", device)
return nil
}

Expand Down

0 comments on commit 3b2e896

Please sign in to comment.