def rollbackLioAddLun(param): """lio中增加lun的回滚函数 @param param 存储了lio中增加lun的多个参数的一个列表 """ osCmd = "lio_node --deliqn=%s" % param[0] try: runExec(osCmd) except Exception, e: VSLogger.error("rollback failed: run %s failed : %s " % (osCmd, repr(e)))
def purgeTarget(lvName, tpgt, suffix): iqn = "test" targetName = "%s:%d:%s" % (iqn, 123456, suffix) handler = VSServiceHandler() try: handler.cleanTarget(lvName, targetName, tpgt) print "purgeTarget success" VSLogger.debug("purgeTarget success") except Exception, e: errMsg = traceback.format_exc() VSLogger.critical(errMsg)
def rollbackTcmCreate(param): """定义创建lio中tcm后端存储的回滚操作 @param param 存储了创建lio中tcm的多个参数的一个列表 """ lioCfgDir = "/sys/kernel/config/target/core/iblock_0/%s_%s" % (param[0], param[1]) if os.path.exists(lioCfgDir): try: osCmd = "tcm_node --freedev iblock_0/%s_%s" % (param[0], param[1]) runExec(osCmd) except Exception, e: VSLogger.error("rollback tcm_node %s_%s failed: %s " % (param[0], param[1], str(e)))
def detachLV(self, lvName): """服务接口,卸载逻辑卷 @param lvName 逻辑卷名称 """ VSLogger.debug("Detach LV(%s) begin..." % (lvName)) try: self.__purgeAttachLV(lvName) except Exception, e: errMsg = traceback.format_exc() VSLogger.error(errMsg) raise VSException(-1, getExceptionErrorMessage(e))
def startTarget(lvName, tpgt, lun, suffix): initiatorName = "iqn.1993-08.org.debian:01:5c487996fda6" iqn = "test" targetName = "%s:%d:%s" % (iqn, 123456, suffix) handler = VSServiceHandler() try: handler.sendTarget(lvName, tpgt, lun, initiatorName, suffix) print "sendTarget success" VSLogger.debug("sendTarget success") # time.sleep(1) # handler.cleanTarget(lvName,targetName) except Exception, e: handler.cleanTarget(lvName, targetName, tpgt) handler.sendTarget(lvName, tpgt, lun, initiatorName, suffix) errMsg = traceback.format_exc() VSLogger.critical(errMsg)
def sendTarget(self, lvName, tpgt, lun, initiatorName, suffix): """服务接口,挂载逻辑卷 @param lvName 输出target @param initiatorName 允许访问此逻辑卷的iscsi initor的名称 @return 返回一个TargetInfo的结构体 """ VSLogger.info("Call interface attachLV(%s, %s)..." % (lvName, initiatorName)) try: rollback = VSActionRollback() actionTcmCreate(rollback, ("test", lvName)) # iqn = dnsName2IQN(VSConfig.getHost()) iqn = "test" # targetName = "%s:%d:%s" % (iqn, time.time() * 1000, lvName) targetName = "%s:%d:%s" % (iqn, 123456, suffix) targetInfo = TargetInfo() targetInfo.targetName = targetName # targetInfo.targetPort = VSConfig.getIscsiPort() targetInfo.chapUser = "******" # targetInfo.chapPass = getRandomPass() targetInfo.chapPass = "******" # "lio_node --addlun %s 1 0 iscsi00 iblock_0/%s_%s" %(targetName, VSConfig.getVgName(), lvName) actionLioAddLun(rollback, (targetName, tpgt, lun, "test", lvName)) # --addnp=TARGET_IQN TPGT IP:PORT osCmd = "lio_node --addnp %s %d %s:%d" % (targetName, tpgt, "0.0.0.0", 3260) runExec(osCmd) # --enableauth=TARGET_IQN TPGT osCmd = "lio_node --enableauth %s %d" % (targetName, tpgt) runExec(osCmd) # lio_node --addlunacl <TargetIQN> <TPG#> <InitiatorIQN> <LUN#> <MappedLUN#> osCmd = "lio_node --addlunacl %s %d %s %d 0" % (targetName, tpgt, initiatorName, lun) runExec(osCmd) # lio_node --setchapauth <TargetIQN> <TPG#><InitiatorIQN> <LoginName> <Password> osCmd = "lio_node --setchapauth %s %d %s %s %s" % ( targetName, tpgt, initiatorName, targetInfo.chapUser, targetInfo.chapPass, ) runExec(osCmd) # --settpgattr=TARGET_IQN TPGT <ATTRIB> <VALUE> osCmd = "lio_node --settpgattr=%s %d cache_dynamic_acls 0" % (targetName, tpgt) runExec(osCmd) # --enabletpg=TARGET_IQN TPGT osCmd = "lio_node --enabletpg %s %d" % (targetName, tpgt) runExec(osCmd) print "sendTarget success" VSLogger.info("sendTarget success") except Exception, e: errMsg = traceback.format_exc() rollback.rollback() VSLogger.error(errMsg) raise VSException(-1, getExceptionErrorMessage(e))
def cleanTarget(self, lvName, targetName, tpgt): # 先后顺序很重要, self.purgeIqn(targetName, tpgt) VSLogger.info("purgeIqn success") print "purgeIqn success" self.purgeTcm(lvName) VSLogger.debug("purgeTcm success") print "purgeTcm success" self.purgeIscsi(targetName) VSLogger.debug("purgeIscsi success") print "purgeIscsi success"
return targetInfo def detachLV(self, lvName): """服务接口,卸载逻辑卷 @param lvName 逻辑卷名称 """ VSLogger.debug("Detach LV(%s) begin..." % (lvName)) try: self.__purgeAttachLV(lvName) except Exception, e: errMsg = traceback.format_exc() VSLogger.error(errMsg) raise VSException(-1, getExceptionErrorMessage(e)) VSLogger.info("Successfully detach LV %s." % (lvName)) def cleanTarget(self, lvName, targetName, tpgt): # 先后顺序很重要, self.purgeIqn(targetName, tpgt) VSLogger.info("purgeIqn success") print "purgeIqn success" self.purgeTcm(lvName) VSLogger.debug("purgeTcm success") print "purgeTcm success" self.purgeIscsi(targetName) VSLogger.debug("purgeIscsi success") print "purgeIscsi success" def startTarget(lvName, tpgt, lun, suffix):