diff --git a/driver.go b/driver.go index 7a1ce69..6417c12 100644 --- a/driver.go +++ b/driver.go @@ -7,6 +7,7 @@ import ( "io/ioutil" "os" "path/filepath" + "strings" "sync" "time" @@ -287,74 +288,37 @@ func (d *iscsiDriver) mountVolume(v *iscsiVolume) error { out := "" errMsg := "" - diskPathFolder := "/dev/disk/by-path" - fi, err := os.Lstat(diskPathFolder) - if os.IsNotExist(err) { - // Disk By Path doesn't exist + // TODO: Move this feature to iscsi driver - iscsiPathFolder := "/host/tmp/iscsi" - fi, err = os.Lstat(iscsiPathFolder) - if os.IsNotExist(err) { - // iSCSI Administration folder doesn't exist + // Get target device + cmd := "iscsiadm -m session | grep \"" + v.TargetIQN + "\" | head -n 1 | sed -e 's/.*\\[\\(.*\\)\\].*/\\1/g'" + out, errMsg = util.ExecuteCommandString(cmd) - return err - } else { - // iSCSI Administration folder exists, seen on Synology systems - - // Get target device - cmd := "ls /host/tmp/iscsi | grep \"" + v.TargetIQN + "\" | head -n 1" - out, errMsg = util.ExecuteCommandString(cmd) - - if len(out) > 0 { - logrus.Debug(out) - } - if len(errMsg) > 0 { - err := fmt.Errorf("Unable to find device: %s", errMsg) - return err - } - - cmd = "readlink -nf /host/tmp/iscsi/" + out - out, errMsg = util.ExecuteCommandString(cmd) - - if len(out) > 0 { - logrus.Debug(out) - } - if len(errMsg) > 0 { - err := fmt.Errorf("Unable to locate device: %s", errMsg) - return err - } - } - } else { - // Disk By Path exists - - // Get target device - cmd := "ls /dev/disk/by-path | grep \"" + v.TargetIQN + "\" | head -n 1" - out, errMsg = util.ExecuteCommandString(cmd) - - if len(out) > 0 { - logrus.Debug(out) - } - if len(errMsg) > 0 { - err := fmt.Errorf("Unable to find device: %s", errMsg) - return err - } - - cmd = "readlink -nf /dev/disk/by-path/" + out - out, errMsg = util.ExecuteCommandString(cmd) - - if len(out) > 0 { - logrus.Debug(out) - } - if len(errMsg) > 0 { - err := fmt.Errorf("Unable to locate device: %s", errMsg) - return err - } + if len(out) > 0 { + logrus.Debug(out) + } + if len(errMsg) > 0 { + err := fmt.Errorf("Unable to find device: %s", errMsg) + return err } - _ = fi + out = strings.TrimSuffix(out, "\n") + + cmd = "iscsiadm -m session -r " + out + " -P 3 | grep \"Attached scsi disk\" | sed -e 's/.*Attached scsi disk \\([^\\t]*\\).*/\\/dev\\/\\1/g'" + out, errMsg = util.ExecuteCommandString(cmd) + + if len(out) > 0 { + logrus.Debug(out) + } + if len(errMsg) > 0 { + err := fmt.Errorf("Unable to locate device: %s", errMsg) + return err + } + + out = strings.TrimSuffix(out, "\n") // Mount - cmd := "mount " + out + " " + v.Mountpoint + cmd = "mount " + out + " " + v.Mountpoint out, errMsg = util.ExecuteCommandString(cmd) if len(out) > 0 { @@ -381,10 +345,12 @@ func (d *iscsiDriver) unmountVolume(v *iscsiVolume) error { logrus.Info(err) } - // Logout from iSCSI Target - err := iSCSI.LogoutTarget(v.TargetIQN, v.Host) - if err != nil { - return err + if iSCSI.TargetLoggedIn(v.TargetIQN) { + // Logout from iSCSI Target + err := iSCSI.LogoutTarget(v.TargetIQN, v.Host) + if err != nil { + return err + } } return nil diff --git a/initializeVolume b/initializeVolume index 3cee866..370a0b4 100755 --- a/initializeVolume +++ b/initializeVolume @@ -15,8 +15,8 @@ iscsiadm -m node -T ${IQN} -p ${HOST} -l sleep 0.5 -DEVICEPATH=$(ls /dev/disk/by-path | grep "${IQN}" | head -n 1) -DEVICE=$(readlink -nf /dev/disk/by-path/${DEVICEPATH}) +SESSIONID=$(iscsiadm -m session | grep "${IQN}" | head -n 1 | sed -e 's/.*\[\(.*\)\].*/\1/g') +DEVICE=$(iscsiadm -m session -r ${SESSIONID} -P 3 | grep "Attached scsi disk" | sed -e 's/.*Attached scsi disk \([^\t]*\).*/\/dev\/\1/g') FILESYSTEM=$(blkid ${DEVICE}) if [ -z "${FILESYSTEM}" ]; then