Пример #1
0
    def detach(self, sr_uuid):
        keys = []
        pbdref = None
        try:
            pbdref = util.find_my_pbd(self.session, self.host_ref, self.sr_ref)
        except:
            pass
        if self.dconf.has_key("SCSIid"):
            self.mpathmodule.reset(self.dconf["SCSIid"], True)  # explicitly unmap
            keys.append("mpath-" + self.dconf["SCSIid"])

        # Remove iscsi_sessions and multipathed keys
        if pbdref <> None:
            if self.cmd == "sr_detach":
                keys += ["multipathed", "iscsi_sessions", "MPPEnabled"]
            for key in keys:
                try:
                    self.session.xenapi.PBD.remove_from_other_config(pbdref, key)
                except:
                    pass

        if util._decr_iscsiSR_refcount(self.targetIQN, sr_uuid) != 0:
            return

        if self.direct and util._containsVDIinuse(self):
            return

        if iscsilib._checkTGT(self.targetIQN):
            try:
                iscsilib.logout(self.target, self.targetIQN, all=True)
            except util.CommandException, inst:
                raise xs_errors.XenError("ISCSIQueryDaemon", opterr="error is %d" % inst.code)
            if iscsilib._checkTGT(self.targetIQN):
                raise xs_errors.XenError("ISCSIQueryDaemon", opterr="Failed to logout from target")
Пример #2
0
def probe_luns(target, port, iqn):
    portal = get_portal(target, port, iqn)
    iscsilib.login(portal, iqn, "", "")
    try:
        luns = iscsilib.get_luns(iqn, portal)
        return [get_probe_possibility(os.path.join("/dev/iscsi/",iqn,portal,"LUN%s" % lun),lun) for lun in luns]
    finally:
        iscsilib.logout(portal, iqn)
    def create(self, sr_uuid, size):
        try:

            # attach the device
            util.SMlog("Trying to attach iscsi disk")

            self.iscsi.attach(sr_uuid)
            if not self.iscsi.attached:
                raise xs_errors.XenError('SRNotAttached')

            util.SMlog("Attached iscsi disk at %s \n" % self.iscsi.path)

            try:

                # generate new UUIDs for VG and LVs
                old_vg_name = self._getVgName(self.dconf['device'])

                lvm_config_dict = self._getLvmInfo(old_vg_name)
                lvUuidMap = {}  # Maps old lv uuids to new uuids

                for lv_name in lvm_config_dict[old_vg_name]['logical_volumes']:
                    if lv_name == MDVOLUME_NAME:
                        continue
                    oldUuid = lv_name[4:]  # remove the VHD-
                    lvUuidMap[oldUuid] = util.gen_uuid()

                new_vg_name = VG_PREFIX + sr_uuid

                self._resignLvm(sr_uuid, old_vg_name, lvUuidMap,
                                lvm_config_dict)

                # causes creation of nodes and activates the lvm volumes
                LVHDSR.LVHDSR.load(self, sr_uuid)

                new_vdi_info = self._resignSrMetadata(new_vg_name, self.uuid,
                                                      lvUuidMap)
                self._resignVdis(new_vg_name, lvUuidMap)
                self._deleteAllSnapshots(new_vdi_info)

                # Detach LVM
                self.lvmCache.deactivateNoRefcount(MDVOLUME_NAME)
                for newUuid in lvUuidMap.values():
                    new_lv_name = self.LV_VHD_PREFIX + newUuid
                    self.lvmCache.deactivateNoRefcount(new_lv_name)

            except:
                util.logException("RESIGN_CREATE")
                raise

        finally:
            iscsilib.logout(self.iscsi.target, self.iscsi.targetIQN, all=True)

        raise xs_errors.XenError(
            "The SR has been successfully resigned. Use the lvmoiscsi type to attach it"
        )
Пример #4
0
def probe_luns(target, port, iqn):
    portal = get_portal(target, port, iqn)
    iscsilib.login(portal, iqn, "", "")
    try:
        luns = iscsilib.get_luns(iqn, portal)
        return [
            get_probe_possibility(
                os.path.join("/dev/iscsi/", iqn, portal, "LUN%s" % lun), lun)
            for lun in luns
        ]
    finally:
        iscsilib.logout(portal, iqn)
Пример #5
0
    def create(self, sr_uuid, size):
        try:

            # attach the device
            util.SMlog("Trying to attach iscsi disk")

            self.iscsi.attach(sr_uuid)
            if not self.iscsi.attached:
                raise xs_errors.XenError('SRNotAttached')

            util.SMlog("Attached iscsi disk at %s \n" % self.iscsi.path)

            try:

                # generate new UUIDs for VG and LVs
                old_vg_name = self._getVgName(self.dconf['device'])

                lvm_config_dict = self._getLvmInfo(old_vg_name)
                lvUuidMap = {}  # Maps old lv uuids to new uuids

                for lv_name in lvm_config_dict[old_vg_name]['logical_volumes']:
                    if lv_name == MDVOLUME_NAME:
                        continue
                    oldUuid = lv_name[4:]  # remove the VHD-
                    lvUuidMap[oldUuid] = util.gen_uuid()

                new_vg_name = VG_PREFIX + sr_uuid

                self._resignLvm(sr_uuid, old_vg_name, lvUuidMap, lvm_config_dict)

                # causes creation of nodes and activates the lvm volumes
                LVHDSR.LVHDSR.load(self, sr_uuid)

                new_vdi_info = self._resignSrMetadata(new_vg_name, self.uuid, lvUuidMap)
                self._resignVdis(new_vg_name, lvUuidMap)
                self._deleteAllSnapshots(new_vdi_info)


                # Detach LVM
                self.lvmCache.deactivateNoRefcount(MDVOLUME_NAME)
                for newUuid in lvUuidMap.values():
                    new_lv_name = self.LV_VHD_PREFIX + newUuid
                    self.lvmCache.deactivateNoRefcount(new_lv_name)

            except:
                util.logException("RESIGN_CREATE")
                raise

        finally:
            iscsilib.logout(self.iscsi.target, self.iscsi.targetIQN, all=True)

        raise xs_errors.XenError("The SR has been successfully resigned. Use the lvmoiscsi type to attach it")
Пример #6
0
def attach(target, port, iqn, scsiid):
    portal = get_portal(target, port, iqn)
    iscsilib.login(portal, iqn, "", "")
    try:
        luns = iscsilib.get_luns(iqn, portal)
        infos = [get_probe_possibility(os.path.join("/dev/iscsi/",iqn,portal,"LUN%s" % lun),lun) for lun in luns]
        for info in infos:
            if info['info']['SCSIid'] == scsiid:
                return os.path.join("/dev/iscsi",iqn,portal,"LUN%s" % info['info']['LUNid'])
        raise "Unknown SCSIid"
    except:
        iscsilib.logout(portal, iqn)
        raise
Пример #7
0
def attach(target, port, iqn, scsiid):
    portal = get_portal(target, port, iqn)
    iscsilib.login(portal, iqn, "", "")
    try:
        luns = iscsilib.get_luns(iqn, portal)
        infos = [
            get_probe_possibility(
                os.path.join("/dev/iscsi/", iqn, portal, "LUN%s" % lun), lun)
            for lun in luns
        ]
        for info in infos:
            if info['info']['SCSIid'] == scsiid:
                return os.path.join("/dev/iscsi", iqn, portal,
                                    "LUN%s" % info['info']['LUNid'])
        raise "Unknown SCSIid"
    except:
        iscsilib.logout(portal, iqn)
        raise
Пример #8
0
    def detach(self, sr_uuid, delete=False):
        keys = []
        pbdref = None
        try:
            pbdref = util.find_my_pbd(self.session, self.host_ref, self.sr_ref)
        except:
            pass
        if 'SCSIid' in self.dconf:
            self.mpathmodule.reset(self.dconf['SCSIid'], explicit_unmap=True)
            keys.append("mpath-" + self.dconf['SCSIid'])

        # Remove iscsi_sessions and multipathed keys
        if pbdref != None:
            if self.cmd == 'sr_detach':
                keys += ["multipathed", "iscsi_sessions", "MPPEnabled"]
            for key in keys:
                try:
                    self.session.xenapi.PBD.remove_from_other_config(
                        pbdref, key)
                except:
                    pass

        if util._decr_iscsiSR_refcount(self.targetIQN, sr_uuid) != 0:
            return

        if self.direct and util._containsVDIinuse(self):
            return

        if iscsilib._checkTGT(self.targetIQN):
            try:
                iscsilib.logout(self.target, self.targetIQN, all=True)
                if delete:
                    iscsilib.delete(self.targetIQN)
            except util.CommandException as inst:
                raise xs_errors.XenError('ISCSIQueryDaemon', \
                      opterr='error is %d' % inst.code)
            if iscsilib._checkTGT(self.targetIQN):
                raise xs_errors.XenError('ISCSIQueryDaemon', \
                    opterr='Failed to logout from target')

        self.attached = False
Пример #9
0
    def load(self, sr_uuid):
        if not sr_uuid:
            # This is a probe call, generate a temp sr_uuid
            sr_uuid = util.gen_uuid()

        driver = SR.driver('iscsi')
        if self.original_srcmd.dconf.has_key('target'):
            self.original_srcmd.dconf['targetlist'] = self.original_srcmd.dconf['target']
        iscsi = driver(self.original_srcmd, sr_uuid)
        self.iscsiSRs = []
        self.iscsiSRs.append(iscsi)
        
        if self.dconf['target'].find(',') == 0 or self.dconf['targetIQN'] == "*":
            # Instantiate multiple sessions
            self.iscsiSRs = []
            if self.dconf['targetIQN'] == "*":
                IQN = "any"
            else:
                IQN = self.dconf['targetIQN']
            dict = {}
            IQNstring = ""
            IQNs = []
            try:
                if self.dconf.has_key('multiSession'):
                    IQNs = self.dconf['multiSession'].split("|")
                    for IQN in IQNs:
                        if IQN:
                            dict[IQN] = ""
                        else:
                            try:
                                IQNs.remove(IQN)
                            except:
                                # Exceptions are not expected but just in case
                                pass
                    # Order in multiSession must be preserved. It is important for dual-controllers.
                    # IQNstring cannot be built with a dictionary iteration because of this
                    IQNstring = self.dconf['multiSession']
                else:
                    for tgt in self.dconf['target'].split(','):
                        try:
                            tgt_ip = util._convertDNS(tgt)
                        except:
                            raise xs_errors.XenError('DNSError')
                        iscsilib.ensure_daemon_running_ok(iscsi.localIQN)
                        map = iscsilib.discovery(tgt_ip,iscsi.port,iscsi.chapuser,iscsi.chappassword,targetIQN=IQN)
                        util.SMlog("Discovery for IP %s returned %s" % (tgt,map))
                        for i in range(0,len(map)):
                            (portal,tpgt,iqn) = map[i]
                            (ipaddr, port) = iscsilib.parse_IP_port(portal)
                            try:
                                util._testHost(ipaddr, long(port), 'ISCSITarget')
                            except:
                                util.SMlog("Target Not reachable: (%s:%s)" % (ipaddr, port))
                                continue
                            key = "%s,%s,%s" % (ipaddr,port,iqn)
                            dict[key] = ""
                # Again, do not mess up with IQNs order. Dual controllers will benefit from that
                if IQNstring == "":
                    # Compose the IQNstring first
                    for key in dict.iterkeys(): IQNstring += "%s|" % key
                    # Reinitialize and store iterator
                    key_iterator = dict.iterkeys()
                else:
                    key_iterator = IQNs

                # Now load the individual iSCSI base classes
                for key in key_iterator:
                    (ipaddr,port,iqn) = key.split(',')
                    srcmd_copy = copy.deepcopy(self.original_srcmd)
                    srcmd_copy.dconf['target'] = ipaddr
                    srcmd_copy.dconf['targetIQN'] = iqn
                    srcmd_copy.dconf['multiSession'] = IQNstring
                    util.SMlog("Setting targetlist: %s" % srcmd_copy.dconf['targetlist'])
                    self.iscsiSRs.append(driver(srcmd_copy, sr_uuid))
                pbd = util.find_my_pbd(self.session, self.host_ref, self.sr_ref)
                if pbd <> None and not self.dconf.has_key('multiSession'):
                    dconf = self.session.xenapi.PBD.get_device_config(pbd)
                    dconf['multiSession'] = IQNstring
                    self.session.xenapi.PBD.set_device_config(pbd, dconf)
            except:
                util.logException("LVHDoISCSISR.load")
        self.iscsi = self.iscsiSRs[0]

        # Be extremely careful not to throw exceptions here since this function
        # is the main one used by all operations including probing and creating
        pbd = None
        try:
            pbd = util.find_my_pbd(self.session, self.host_ref, self.sr_ref)
        except:
            pass

        # Apart from the upgrade case, user must specify a SCSIid
        if not self.dconf.has_key('SCSIid'):
            # Dual controller issue
            self.LUNs = {}  # Dict for LUNs from all the iscsi objects
            for ii in range(0, len(self.iscsiSRs)):
                self.iscsi = self.iscsiSRs[ii]
                self._LUNprint(sr_uuid)
                for key in self.iscsi.LUNs:
                    self.LUNs[key] = self.iscsi.LUNs[key]
            self.print_LUNs_XML()
            self.iscsi = self.iscsiSRs[0] # back to original value
            raise xs_errors.XenError('ConfigSCSIid')

        self.SCSIid = self.dconf['SCSIid']

        # This block checks if the first iscsi target contains the right SCSIid.
        # If not it scans the other iscsi targets because chances are that more
        # than one controller is present
        dev_match = False
        forced_login = False
        # No need to check if only one iscsi target is present
        if len(self.iscsiSRs) == 1:
            pass
        else:
            target_success = False
            attempt_discovery = False
            for iii in range(0, len(self.iscsiSRs)):
                # Check we didn't leave any iscsi session open
                # If exceptions happened before, the cleanup function has worked on the right target.
                if forced_login == True:
                    try:
                        iscsilib.ensure_daemon_running_ok(self.iscsi.localIQN)
                        iscsilib.logout(self.iscsi.target, self.iscsi.targetIQN)
                        forced_login = False
                    except:
                        raise xs_errors.XenError('ISCSILogout')
                self.iscsi = self.iscsiSRs[iii]
                util.SMlog("path %s" %self.iscsi.path)
                util.SMlog("iscsci data: targetIQN %s, portal %s" % (self.iscsi.targetIQN, self.iscsi.target))
                iscsilib.ensure_daemon_running_ok(self.iscsi.localIQN)
                if not iscsilib._checkTGT(self.iscsi.targetIQN):
                    attempt_discovery = True
                    try:
                        # Ensure iscsi db has been populated
                        map = iscsilib.discovery(
                                  self.iscsi.target,
                                  self.iscsi.port,
                                  self.iscsi.chapuser,
                                  self.iscsi.chappassword,
                                  targetIQN=self.iscsi.targetIQN)
                        if len(map) == 0:
                            util.SMlog("Discovery for iscsi data targetIQN %s,"
                                       " portal %s returned empty list"
                                       " Trying another path if available" %
                                       (self.iscsi.targetIQN,
                                        self.iscsi.target))
                            continue
                    except:
                        util.SMlog("Discovery failed for iscsi data targetIQN"
                                   " %s, portal %s. Trying another path if"
                                   " available" %
                                   (self.iscsi.targetIQN, self.iscsi.target))
                        continue
                    try:
                        iscsilib.login(self.iscsi.target,
                                       self.iscsi.targetIQN,
                                       self.iscsi.chapuser,
                                       self.iscsi.chappassword,
                                       self.iscsi.incoming_chapuser,
                                       self.iscsi.incoming_chappassword,
                                       self.mpath == "true")
                    except:
                        util.SMlog("Login failed for iscsi data targetIQN %s,"
                                   " portal %s. Trying another path"
                                   " if available" %
                                   (self.iscsi.targetIQN, self.iscsi.target))
                        continue
                    target_success = True;
                    forced_login = True
                # A session should be active.
                if not util.wait_for_path(self.iscsi.path, ISCSISR.MAX_TIMEOUT):
                    util.SMlog("%s has no associated LUNs" % self.iscsi.targetIQN)
                    continue
                scsiid_path = "/dev/disk/by-id/scsi-" + self.SCSIid
                if not util.wait_for_path(scsiid_path, ISCSISR.MAX_TIMEOUT):
                    util.SMlog("%s not found" %scsiid_path)
                    continue
                for file in filter(self.iscsi.match_lun, util.listdir(self.iscsi.path)):
                    lun_path = os.path.join(self.iscsi.path,file)
                    lun_dev = scsiutil.getdev(lun_path)
                    try:
                        lun_scsiid = scsiutil.getSCSIid(lun_dev)
                    except:
                        util.SMlog("getSCSIid failed on %s in iscsi %s: LUN"
                                   " offline or iscsi path down" %
                                    (lun_dev, self.iscsi.path))
                        continue
                    util.SMlog("dev from lun %s %s" %(lun_dev, lun_scsiid))
                    if lun_scsiid == self.SCSIid:
                        util.SMlog("lun match in %s" %self.iscsi.path)
                        dev_match = True
                        # No more need to raise ISCSITarget exception.
                        # Resetting attempt_discovery
                        attempt_discovery = False
                        break
                if dev_match:
                    if iii == 0:
                        break
                    util.SMlog("IQN reordering needed")
                    new_iscsiSRs = []
                    IQNs = {}
                    IQNstring = ""
                    # iscsiSRs can be seen as a circular buffer: the head now is the matching one
                    for kkk in range(iii, len(self.iscsiSRs)) + range(0, iii):
                        new_iscsiSRs.append(self.iscsiSRs[kkk])
                        ipaddr = self.iscsiSRs[kkk].target
                        port = self.iscsiSRs[kkk].port
                        iqn = self.iscsiSRs[kkk].targetIQN
                        key = "%s,%s,%s" % (ipaddr,port,iqn)
                        # The final string must preserve the order without repetition
                        if not IQNs.has_key(key):
                            IQNs[key] = ""
                            IQNstring += "%s|" % key
                    util.SMlog("IQNstring is now %s" %IQNstring)
                    self.iscsiSRs = new_iscsiSRs
                    util.SMlog("iqn %s is leading now" %self.iscsiSRs[0].targetIQN)
                    # Updating pbd entry, if any
                    try:
                        pbd = util.find_my_pbd(self.session, self.host_ref, self.sr_ref)
                        if pbd <> None and self.dconf.has_key('multiSession'):
                            util.SMlog("Updating multiSession in PBD")
                            dconf = self.session.xenapi.PBD.get_device_config(pbd)
                            dconf['multiSession'] = IQNstring
                            self.session.xenapi.PBD.set_device_config(pbd, dconf)
                    except:
                        pass
                    break
            if not target_success and attempt_discovery:
                raise xs_errors.XenError('ISCSITarget')

            # Check for any unneeded open iscsi sessions
            if forced_login == True:
                try:
                    iscsilib.ensure_daemon_running_ok(self.iscsi.localIQN)
                    iscsilib.logout(self.iscsi.target, self.iscsi.targetIQN)
                    forced_login = False
                except:
                    raise xs_errors.XenError('ISCSILogout')

        self._pathrefresh(LVHDoISCSISR, load = False)

        LVHDSR.LVHDSR.load(self, sr_uuid)
Пример #10
0
def detach(target, port, iqn):
    portal = get_portal(target, port, iqn)
    iscsilib.logout(portal, iqn)
Пример #11
0
    def load(self, sr_uuid):
        if not sr_uuid:
            # This is a probe call, generate a temp sr_uuid
            sr_uuid = util.gen_uuid()

        driver = SR.driver('iscsi')
        if self.original_srcmd.dconf.has_key('target'):
            self.original_srcmd.dconf[
                'targetlist'] = self.original_srcmd.dconf['target']
        iscsi = driver(self.original_srcmd, sr_uuid)
        self.iscsiSRs = []
        self.iscsiSRs.append(iscsi)

        if self.dconf['target'].find(
                ',') == 0 or self.dconf['targetIQN'] == "*":
            # Instantiate multiple sessions
            self.iscsiSRs = []
            if self.dconf['targetIQN'] == "*":
                IQN = "any"
            else:
                IQN = self.dconf['targetIQN']
            dict = {}
            IQNstring = ""
            IQNs = []
            try:
                if self.dconf.has_key('multiSession'):
                    IQNs = self.dconf['multiSession'].split("|")
                    for IQN in IQNs:
                        if IQN:
                            dict[IQN] = ""
                        else:
                            try:
                                IQNs.remove(IQN)
                            except:
                                # Exceptions are not expected but just in case
                                pass
                    # Order in multiSession must be preserved. It is important for dual-controllers.
                    # IQNstring cannot be built with a dictionary iteration because of this
                    IQNstring = self.dconf['multiSession']
                else:
                    for tgt in self.dconf['target'].split(','):
                        try:
                            tgt_ip = util._convertDNS(tgt)
                        except:
                            raise xs_errors.XenError('DNSError')
                        iscsilib.ensure_daemon_running_ok(iscsi.localIQN)
                        map = iscsilib.discovery(tgt_ip,
                                                 iscsi.port,
                                                 iscsi.chapuser,
                                                 iscsi.chappassword,
                                                 targetIQN=IQN)
                        util.SMlog("Discovery for IP %s returned %s" %
                                   (tgt, map))
                        for i in range(0, len(map)):
                            (portal, tpgt, iqn) = map[i]
                            (ipaddr, port) = iscsilib.parse_IP_port(portal)
                            try:
                                util._testHost(ipaddr, long(port),
                                               'ISCSITarget')
                            except:
                                util.SMlog("Target Not reachable: (%s:%s)" %
                                           (ipaddr, port))
                                continue
                            key = "%s,%s,%s" % (ipaddr, port, iqn)
                            dict[key] = ""
                # Again, do not mess up with IQNs order. Dual controllers will benefit from that
                if IQNstring == "":
                    # Compose the IQNstring first
                    for key in dict.iterkeys():
                        IQNstring += "%s|" % key
                    # Reinitialize and store iterator
                    key_iterator = dict.iterkeys()
                else:
                    key_iterator = IQNs

                # Now load the individual iSCSI base classes
                for key in key_iterator:
                    (ipaddr, port, iqn) = key.split(',')
                    srcmd_copy = copy.deepcopy(self.original_srcmd)
                    srcmd_copy.dconf['target'] = ipaddr
                    srcmd_copy.dconf['targetIQN'] = iqn
                    srcmd_copy.dconf['multiSession'] = IQNstring
                    util.SMlog("Setting targetlist: %s" %
                               srcmd_copy.dconf['targetlist'])
                    self.iscsiSRs.append(driver(srcmd_copy, sr_uuid))
                pbd = util.find_my_pbd(self.session, self.host_ref,
                                       self.sr_ref)
                if pbd <> None and not self.dconf.has_key('multiSession'):
                    dconf = self.session.xenapi.PBD.get_device_config(pbd)
                    dconf['multiSession'] = IQNstring
                    self.session.xenapi.PBD.set_device_config(pbd, dconf)
            except:
                util.logException("OCFSoISCSISR.load")
        self.iscsi = self.iscsiSRs[0]

        # Be extremely careful not to throw exceptions here since this function
        # is the main one used by all operations including probing and creating
        pbd = None
        try:
            pbd = util.find_my_pbd(self.session, self.host_ref, self.sr_ref)
        except:
            pass

        # Apart from the upgrade case, user must specify a SCSIid
        if not self.dconf.has_key('SCSIid'):
            # Dual controller issue
            self.LUNs = {}  # Dict for LUNs from all the iscsi objects
            for ii in range(0, len(self.iscsiSRs)):
                self.iscsi = self.iscsiSRs[ii]
                self._LUNprint(sr_uuid)
                for key in self.iscsi.LUNs:
                    self.LUNs[key] = self.iscsi.LUNs[key]
            self.print_LUNs_XML()
            self.iscsi = self.iscsiSRs[0]  # back to original value
            raise xs_errors.XenError('ConfigSCSIid')

        self.SCSIid = self.dconf['SCSIid']

        # This block checks if the first iscsi target contains the right SCSIid.
        # If not it scans the other iscsi targets because chances are that more
        # than one controller is present
        dev_match = False
        forced_login = False
        # No need to check if only one iscsi target is present
        if len(self.iscsiSRs) == 1:
            pass
        else:
            target_success = False
            attempt_discovery = False
            for iii in range(0, len(self.iscsiSRs)):
                # Check we didn't leave any iscsi session open
                # If exceptions happened before, the cleanup function has worked on the right target.
                if forced_login == True:
                    try:
                        iscsilib.ensure_daemon_running_ok(self.iscsi.localIQN)
                        iscsilib.logout(self.iscsi.target,
                                        self.iscsi.targetIQN)
                        forced_login = False
                    except:
                        raise xs_errors.XenError('ISCSILogout')
                self.iscsi = self.iscsiSRs[iii]
                util.SMlog("path %s" % self.iscsi.path)
                util.SMlog("iscsci data: targetIQN %s, portal %s" %
                           (self.iscsi.targetIQN, self.iscsi.target))
                iscsilib.ensure_daemon_running_ok(self.iscsi.localIQN)
                if not iscsilib._checkTGT(self.iscsi.targetIQN):
                    attempt_discovery = True
                    try:
                        # Ensure iscsi db has been populated
                        map = iscsilib.discovery(
                            self.iscsi.target,
                            self.iscsi.port,
                            self.iscsi.chapuser,
                            self.iscsi.chappassword,
                            targetIQN=self.iscsi.targetIQN)
                        if len(map) == 0:
                            util.SMlog(
                                "Discovery for iscsi data targetIQN %s,"
                                " portal %s returned empty list"
                                " Trying another path if available" %
                                (self.iscsi.targetIQN, self.iscsi.target))
                            continue
                    except:
                        util.SMlog("Discovery failed for iscsi data targetIQN"
                                   " %s, portal %s. Trying another path if"
                                   " available" %
                                   (self.iscsi.targetIQN, self.iscsi.target))
                        continue
                    try:
                        iscsilib.login(self.iscsi.target, self.iscsi.targetIQN,
                                       self.iscsi.chapuser,
                                       self.iscsi.chappassword,
                                       self.iscsi.incoming_chapuser,
                                       self.iscsi.incoming_chappassword,
                                       self.mpath == "true")
                    except:
                        util.SMlog("Login failed for iscsi data targetIQN %s,"
                                   " portal %s. Trying another path"
                                   " if available" %
                                   (self.iscsi.targetIQN, self.iscsi.target))
                        continue
                    target_success = True
                    forced_login = True
                # A session should be active.
                if not util.wait_for_path(self.iscsi.path,
                                          ISCSISR.MAX_TIMEOUT):
                    util.SMlog("%s has no associated LUNs" %
                               self.iscsi.targetIQN)
                    continue
                scsiid_path = "/dev/disk/by-id/scsi-" + self.SCSIid
                if not util.wait_for_path(scsiid_path, ISCSISR.MAX_TIMEOUT):
                    util.SMlog("%s not found" % scsiid_path)
                    continue
                for file in filter(self.iscsi.match_lun,
                                   util.listdir(self.iscsi.path)):
                    lun_path = os.path.join(self.iscsi.path, file)
                    lun_dev = scsiutil.getdev(lun_path)
                    try:
                        lun_scsiid = scsiutil.getSCSIid(lun_dev)
                    except:
                        util.SMlog("getSCSIid failed on %s in iscsi %s: LUN"
                                   " offline or iscsi path down" %
                                   (lun_dev, self.iscsi.path))
                        continue
                    util.SMlog("dev from lun %s %s" % (lun_dev, lun_scsiid))
                    if lun_scsiid == self.SCSIid:
                        util.SMlog("lun match in %s" % self.iscsi.path)
                        dev_match = True
                        # No more need to raise ISCSITarget exception.
                        # Resetting attempt_discovery
                        attempt_discovery = False
                        break
                if dev_match:
                    if iii == 0:
                        break
                    util.SMlog("IQN reordering needed")
                    new_iscsiSRs = []
                    IQNs = {}
                    IQNstring = ""
                    # iscsiSRs can be seen as a circular buffer: the head now is the matching one
                    for kkk in range(iii, len(self.iscsiSRs)) + range(0, iii):
                        new_iscsiSRs.append(self.iscsiSRs[kkk])
                        ipaddr = self.iscsiSRs[kkk].target
                        port = self.iscsiSRs[kkk].port
                        iqn = self.iscsiSRs[kkk].targetIQN
                        key = "%s,%s,%s" % (ipaddr, port, iqn)
                        # The final string must preserve the order without repetition
                        if not IQNs.has_key(key):
                            IQNs[key] = ""
                            IQNstring += "%s|" % key
                    util.SMlog("IQNstring is now %s" % IQNstring)
                    self.iscsiSRs = new_iscsiSRs
                    util.SMlog("iqn %s is leading now" %
                               self.iscsiSRs[0].targetIQN)
                    # Updating pbd entry, if any
                    try:
                        pbd = util.find_my_pbd(self.session, self.host_ref,
                                               self.sr_ref)
                        if pbd <> None and self.dconf.has_key('multiSession'):
                            util.SMlog("Updating multiSession in PBD")
                            dconf = self.session.xenapi.PBD.get_device_config(
                                pbd)
                            dconf['multiSession'] = IQNstring
                            self.session.xenapi.PBD.set_device_config(
                                pbd, dconf)
                    except:
                        pass
                    break
            if not target_success and attempt_discovery:
                raise xs_errors.XenError('ISCSITarget')

            # Check for any unneeded open iscsi sessions
            if forced_login == True:
                try:
                    iscsilib.ensure_daemon_running_ok(self.iscsi.localIQN)
                    iscsilib.logout(self.iscsi.target, self.iscsi.targetIQN)
                    forced_login = False
                except:
                    raise xs_errors.XenError('ISCSILogout')

        self._pathrefresh(OCFSoISCSISR, load=False)
        OCFSSR.OCFSSR.load(self, sr_uuid)
Пример #12
0
 def detach(self, sr_uuid, vdi_uuid):
     # Does iscsi logout
     log("Calling VDI DETACH")
     portal = "%s:%s" % (self.target, self.port)
     iscsilib.logout(portal, self.iqn)
     self.attached = False
Пример #13
0
def detach(target, port, iqn):
    portal = get_portal(target, port, iqn)
    iscsilib.logout(portal,iqn)