def _checkActive(path): if util.pathexists(path): return True util.SMlog("_checkActive: %s does not exist!" % path) symlinkExists = os.path.lexists(path) util.SMlog("_checkActive: symlink exists: %s" % symlinkExists) mapperDeviceExists = False mapperDevice = path[5:].replace("-", "--").replace("/", "-") cmd = [CMD_DMSETUP, "status", mapperDevice] try: ret = util.pread2(cmd) mapperDeviceExists = True util.SMlog("_checkActive: %s: %s" % (mapperDevice, ret)) except util.CommandException: util.SMlog("_checkActive: device %s does not exist" % mapperDevice) mapperPath = "/dev/mapper/" + mapperDevice mapperPathExists = util.pathexists(mapperPath) util.SMlog("_checkActive: path %s exists: %s" % \ (mapperPath, mapperPathExists)) if mapperDeviceExists and mapperPathExists and not symlinkExists: # we can fix this situation manually here try: util.SMlog("_checkActive: attempt to create the symlink manually.") os.symlink(mapperPath, path) except OSError, e: util.SMlog("ERROR: failed to symlink!") if e.errno != errno.EEXIST: raise if util.pathexists(path): util.SMlog("_checkActive: created the symlink manually") return True
def _find_path_with_retries(self, vdi_uuid, maxretry=5, period=2.0): vhd_path = os.path.join(self.sr.path, "%s.%s" % \ (vdi_uuid, self.PARAM_VHD)) raw_path = os.path.join(self.sr.path, "%s.%s" % \ (vdi_uuid, self.PARAM_RAW)) cbt_path = os.path.join(self.sr.path, "%s.%s" % (vdi_uuid, CBTLOG_TAG)) found = False tries = 0 while tries < maxretry and not found: tries += 1 if util.ioretry(lambda: util.pathexists(vhd_path)): self.vdi_type = vhdutil.VDI_TYPE_VHD self.path = vhd_path found = True elif util.ioretry(lambda: util.pathexists(raw_path)): self.vdi_type = vhdutil.VDI_TYPE_RAW self.path = raw_path self.hidden = False found = True elif util.ioretry(lambda: util.pathexists(cbt_path)): self.vdi_type = CBTLOG_TAG self.path = cbt_path self.hidden = False found = True if not found: util.SMlog("VHD %s not found, retry %s of %s" % (vhd_path, tries, maxretry)) time.sleep(period) return found
def _checkmount(self): mount_path = self.path if self.handles("cifs"): mount_path = self.mountpoint return util.ioretry(lambda: util.pathexists(mount_path) and \ (util.ismount(mount_path) or \ util.pathexists(self.remotepath) and self._isbind()))
def createJournalDir(): if util.pathexists(VHD_JOURNAL_LOCATION): return try: os.makedirs(VHD_JOURNAL_LOCATION) except OSError: pass if not util.pathexists(VHD_JOURNAL_LOCATION): raise util.SMException("Failed to create dirs %s" % \ VHD_JOURNAL_LOCATION)
def __init__(self, ns): self.ns = ns self.nsDir = os.path.join(self.BASE_DIR, self.ns) if not util.pathexists(self.nsDir): try: os.makedirs(self.nsDir) except OSError: pass if not util.pathexists(self.nsDir): raise IPCFlagException("failed to create %s" % self.nsDir)
def _reset(ns, obj = None): nsDir = os.path.join(RefCounter.BASE_DIR, ns) if not util.pathexists(nsDir): return if obj: if not util.pathexists(os.path.join(nsDir, obj)): return objList = [obj] else: try: objList = os.listdir(nsDir) except OSError: raise RefCounterException("failed to list '%s'" % ns) for obj in objList: RefCounter._removeObject(ns, obj)
def _get(ns, obj): """Get the ref count values for 'obj' in namespace 'ns'""" objFile = os.path.join(RefCounter.BASE_DIR, ns, obj) (count, binaryCount) = (0, 0) if util.pathexists(objFile): (count, binaryCount) = RefCounter._readCount(objFile) return (count, binaryCount)
def create(self, sr_uuid, size): if util.ioretry(lambda: self._checkmount()): raise xs_errors.XenError('NFSAttached') # Set the target path temporarily to the base dir # so that we can create the target SR directory self.remotepath = self.dconf['serverpath'] try: self.attach(sr_uuid) except: try: os.rmdir(self.path) except: pass raise xs_errors.XenError('NFSMount') newpath = os.path.join(self.path, sr_uuid) if util.ioretry(lambda: util.pathexists(newpath)): if len(util.ioretry(lambda: util.listdir(newpath))) != 0: self.detach(sr_uuid) raise xs_errors.XenError('SRExists') else: try: util.ioretry(lambda: util.makedirs(newpath)) except util.CommandException, inst: if inst.code != errno.EEXIST: self.detach(sr_uuid) raise xs_errors.XenError('NFSCreate', \ opterr='remote directory creation error is %d' \ % inst.code)
def _checkpath(self, path): try: if not util.ioretry(lambda: util.pathexists(path)): return False return True except util.CommandException, inst: raise xs_errors.XenError("EIO", opterr="IO error checking path %s" % path)
def load(self, vdi_uuid): self.vdi_type = SR.DEFAULT_TAP self.path = os.path.join(self.sr.path, "%s.%s" % \ (vdi_uuid,self.vdi_type)) if util.ioretry(lambda: util.pathexists(self.path)): try: st = util.ioretry(lambda: os.stat(self.path)) self.utilisation = long(st.st_size) except util.CommandException, inst: if inst.code == errno.EIO: raise xs_errors.XenError('VDILoad', \ opterr='Failed load VDI information %s' % self.path) else: raise xs_errors.XenError('VDIType', \ opterr='Invalid VDI type %s' % self.vdi_type) try: diskinfo = util.ioretry(lambda: self._query_info(self.path)) if diskinfo.has_key('parent'): self.parent = diskinfo['parent'] else: self.parent = '' self.size = long(diskinfo['size']) * 1024 * 1024 self.hidden = long(diskinfo['hidden']) except util.CommandException, inst: raise xs_errors.XenError('VDILoad', \ opterr='Failed load VDI information %s' % self.path)
def _createNamespace(ns): nsDir = os.path.join(RefCounter.BASE_DIR, ns) if not util.pathexists(nsDir): try: os.makedirs(nsDir) except OSError: raise RefCounterException("failed to makedirs '%s'" % nsDir)
def create(self, sr_uuid, vdi_uuid, size): if util.ioretry(lambda: util.pathexists(self.path)): raise xs_errors.XenError('VDIExists') overhead = 0 if self.vdi_type == vhdutil.VDI_TYPE_VHD: overhead = vhdutil.calcOverheadFull(long(size)) # Test the amount of actual disk space if ENFORCE_VIRT_ALLOC: self.sr._loadvdis() reserved = self.sr.virtual_allocation sr_size = self.sr._getsize() if (sr_size - reserved) < (long(size) + overhead): raise xs_errors.XenError('SRNoSpace') if self.vdi_type == vhdutil.VDI_TYPE_VHD: try: size = vhdutil.validate_and_round_vhd_size(long(size)) mb = 1024L * 1024L size_mb = long(size) / mb util.ioretry(lambda: self._create(str(size_mb), self.path)) self.size = util.ioretry(lambda: self._query_v(self.path)) except util.CommandException, inst: raise xs_errors.XenError('VDICreate', opterr='error %d' % inst.code)
def delete(self, sr_uuid): self.attach(sr_uuid) cleanup.gc_force(self.session, self.uuid) # check to make sure no VDIs are present; then remove old # files that are non VDI's try: if util.ioretry(lambda: util.pathexists(self.path)): #Load the VDI list self._loadvdis() for uuid in self.vdis: if not self.vdis[uuid].deleted: raise xs_errors.XenError('SRNotEmpty', \ opterr='VDIs still exist in SR') # remove everything else, there are no vdi's for name in util.ioretry(lambda: util.listdir(self.path)): fullpath = os.path.join(self.path,name) try: util.ioretry(lambda: os.unlink(fullpath)) except util.CommandException, inst: if inst.code != errno.ENOENT and \ inst.code != errno.EISDIR: raise xs_errors.XenError('FileSRDelete', \ opterr='failed to remove %s error %d' \ % (fullpath, inst.code)) self.detach(sr_uuid)
def delete(self, sr_uuid): if not self._checkpath(self.path): raise xs_errors.XenError("SRUnavailable", opterr="no such directory %s" % self.path) cleanup.gc_force(self.session, self.uuid) # check to make sure no VDIs are present; then remove old # files that are non VDI's try: if util.ioretry(lambda: util.pathexists(self.path)): # Load the VDI list self._loadvdis() for uuid in self.vdis: if not self.vdis[uuid].deleted: raise xs_errors.XenError("SRNotEmpty", opterr="VDIs still exist in SR") # remove everything else, there are no vdi's for name in util.ioretry(lambda: util.listdir(self.path)): fullpath = os.path.join(self.path, name) try: util.ioretry(lambda: os.unlink(fullpath)) except util.CommandException, inst: if inst.code != errno.ENOENT and inst.code != errno.EISDIR: raise xs_errors.XenError( "FileSRDelete", opterr="failed to remove %s error %d" % (fullpath, inst.code) ) except util.CommandException, inst: raise xs_errors.XenError("FileSRDelete", opterr="error %d" % inst.code)
def create(self, sr_uuid, vdi_uuid, size): if util.ioretry(lambda: util.pathexists(self.path)): raise xs_errors.XenError('VDIExists') overhead = 0 if self.vdi_type == vhdutil.VDI_TYPE_VHD: overhead = vhdutil.calcOverheadFull(long(size)) # Test the amount of actual disk space if ENFORCE_VIRT_ALLOC: self.sr._loadvdis() reserved = self.sr.virtual_allocation sr_size = self.sr._getsize() if (sr_size - reserved) < (long(size) + overhead): raise xs_errors.XenError('SRNoSpace') if self.vdi_type == vhdutil.VDI_TYPE_VHD: try: mb = 1024L * 1024L size_mb = util.roundup(VHD_SIZE_INC, long(size)) / mb if size_mb < 1 or (size_mb + (overhead / mb)) >= MAX_DISK_MB: raise xs_errors.XenError('VDISize', opterr='VDI size ' + \ 'must be between 1 MB and %d MB' % \ ((MAX_DISK_MB - MAX_DISK_METADATA) - 1)) util.ioretry(lambda: self._create(str(size_mb), self.path)) self.size = util.ioretry(lambda: self._query_v(self.path)) except util.CommandException, inst: raise xs_errors.XenError('VDICreate', opterr='error %d' % inst.code)
def create(self, sr_uuid, size): if util.pathexists(self.blockdevice): util.zeroOut(self.blockdevice, 0, 1024 * 1024) cmd = ["mkfs", "-t", "ocfs2", "-b", "4K", "-C", "1M", "-N", "16", "-F", self.blockdevice] try: ret = util.pread(cmd) except util.CommandException, inst: raise xs_errors.XenError("OCFSFilesystem", opterr="mkfs failed error %d" % os.strerror(inst.code))
def _checkpath(self, path): try: if util.ioretry(lambda: util.pathexists(path)): if util.ioretry(lambda: util.isdir(path)): return True return False except util.CommandException, inst: raise xs_errors.XenError('EIO', \ opterr='IO error checking path %s' % path)
def create(self, sr_uuid, size): if util.pathexists(self.blockdevice): util.zeroOut(self.blockdevice, 0, 1024*1024) cmd = ['mkfs', '-t', 'ocfs2', '-b', '4K', '-C', '1M', '-N', '16', '-F', self.blockdevice ] try: ret = util.pread(cmd) except util.CommandException, inst: raise xs_errors.XenError('OCFSFilesystem', \ opterr='mkfs failed error %d' % os.strerror(inst.code))
def attach_from_config(self, sr_uuid, vdi_uuid): """Used for HA State-file only. Will not just attach the VDI but also start a tapdisk on the file""" util.SMlog("OCFSFileVDI.attach_from_config") try: if not util.pathexists(self.sr.path): self.sr.attach(sr_uuid) except: util.logException("OCFSFileVDI.attach_from_config") raise xs_errors.XenError("SRUnavailable", opterr="Unable to attach from config")
def attach(self, sr_uuid, vdi_uuid): self.sr._loadvdis() if not self.sr.vdis.has_key(vdi_uuid): raise xs_errors.XenError('VDIUnavailable') if not util.pathexists(self.path): self.sr.refresh() if not util.wait_for_path(self.path, MAX_TIMEOUT): util.SMlog("Unable to detect LUN attached to host [%s]" % self.sr.path) raise xs_errors.XenError('VDIUnavailable') return super(RAWVDI, self).attach(sr_uuid, vdi_uuid)
def _load(self, vdi_uuid): self.vdi_type = SR.DEFAULT_TAP self.path = os.path.join(self.sr.path, "%s.%s" % \ (vdi_uuid,self.vdi_type)) if self.sr.__dict__.get("vhds") and self.sr.vhds.get(vdi_uuid): # VHD info already preloaded: use it instead of querying directly vhdInfo = self.sr.vhds[vdi_uuid] self.utilisation = vhdInfo.sizePhys self.size = vhdInfo.sizeVirt self.hidden = vhdInfo.hidden if self.hidden: self.managed = False self.parent = vhdInfo.parentUuid if self.parent: self.sm_config_override = {'vhd-parent':self.parent} else: self.sm_config_override = {'vhd-parent':None} return try: # Change to the SR directory in case parent # locator field path has changed os.chdir(self.sr.path) except: raise xs_errors.XenError('SRUnavailable') if util.ioretry(lambda: util.pathexists(self.path)): try: st = util.ioretry(lambda: os.stat(self.path)) self.utilisation = long(st.st_size) except util.CommandException, inst: if inst.code == errno.EIO: raise xs_errors.XenError('VDILoad', \ opterr='Failed load VDI information %s' % self.path) else: raise xs_errors.XenError('VDIType', \ opterr='Invalid VDI type %s' % self.vdi_type) try: diskinfo = util.ioretry(lambda: self._query_info(self.path)) if diskinfo.has_key('parent'): self.parent = diskinfo['parent'] self.sm_config_override = {'vhd-parent':self.parent} else: self.sm_config_override = {'vhd-parent':None} self.parent = '' self.size = long(diskinfo['size']) * 1024 * 1024 self.hidden = long(diskinfo['hidden']) if self.hidden: self.managed = False self.exists = True except util.CommandException, inst: raise xs_errors.XenError('VDILoad', \ opterr='Failed load VDI information %s' % self.path)
def delete(self, sr_uuid, vdi_uuid): if not util.ioretry(lambda: util.pathexists(self.path)): return if self.attached: raise xs_errors.XenError('VDIInUse') try: util.ioretry(lambda: self._mark_hidden(self.path)) except util.CommandException, inst: raise xs_errors.XenError('VDIDelete', opterr='error %d' % inst.code)
def _attach_LUN_bySCSIid(self, SCSIid): if not self.attached: raise xs_errors.XenError("SRUnavailable") path = self.mpathmodule.path(SCSIid) if not util.pathexists(path): self.refresh() if not util.wait_for_path(path, MAX_TIMEOUT): util.SMlog("Unable to detect LUN attached to host [%s]" % path) return False return True
def _checkActive(path): if util.pathexists(path): return True util.SMlog("_checkActive: %s does not exist!" % path) util.SMlog("_checkActive: symlink exists: %s" % os.path.lexists(path)) mapperDevice = path[5:].replace("-", "--").replace("/", "-") cmd = [CMD_DMSETUP, "status", mapperDevice] try: ret = util.pread2(cmd) util.SMlog("_checkActive: %s: %s" % (mapperDevice, ret)) except util.CommandException: util.SMlog("_checkActive: device %s does not exist" % mapperDevice) mapperPath = "/dev/mapper/" + mapperDevice util.SMlog("_checkActive: path %s exists: %s" % \ (mapperPath, util.pathexists(mapperPath))) return False
def delete(self, sr_uuid, vdi_uuid): if not util.ioretry(lambda: util.pathexists(self.path)): return if self.attached: raise xs_errors.XenError("VDIInUse") if self.vdi_type == vhdutil.VDI_TYPE_VHD: try: util.ioretry(lambda: self._mark_hidden(self.path)) except util.CommandException, inst: raise xs_errors.XenError("VDIDelete", opterr="error %d" % inst.code)
def attach(self, sr_uuid, vdi_uuid): util.SMlog("RBDVDI.attach for %s" % self.uuid) vdi_ref = self.sr.srcmd.params['vdi_ref'] sm_config = self.session.xenapi.VDI.get_sm_config(vdi_ref) if not hasattr(self,'xenstore_data'): self.xenstore_data = {} self.xenstore_data.update(scsiutil.update_XS_SCSIdata(self.uuid, scsiutil.gen_synthetic_page_data(self.uuid))) self.xenstore_data['storage-type']='rbd' self.xenstore_data['vdi-type']=self.vdi_type self.attached = True self.session.xenapi.VDI.remove_from_sm_config(vdi_ref, 'attached') self.session.xenapi.VDI.add_to_sm_config(vdi_ref, 'attached', 'true') self.size = int(self.session.xenapi.VDI.get_virtual_size(vdi_ref)) ########## vdis = self.session.xenapi.SR.get_VDIs(self.sr.sr_ref) has_a_snapshot = False for tmp_vdi in vdis: tmp_vdi_uuid = self.session.xenapi.VDI.get_uuid(tmp_vdi) tmp_sm_config = self.session.xenapi.VDI.get_sm_config(tmp_vdi) if tmp_sm_config.has_key("snapshot-of"): if tmp_sm_config["snapshot-of"] == vdi_uuid: has_a_snapshot = True if tmp_sm_config.has_key("sxm_mirror"): sxm_mirror_vdi = vdi_uuid ########## SXM VDIs if sm_config.has_key("base_mirror"): if has_a_snapshot: # it's a mirror vdi of storage migrating VM # it's attached first self.session.xenapi.VDI.add_to_sm_config(vdi_ref, 'sxm_mirror', 'true') # creating dm snapshot dev self._setup_mirror(vdi_uuid, self.size) else: # it's a base vdi of storage migrating VM # it's attached after mirror VDI and mirror snapshot VDI has been created self._map_VHD(vdi_uuid) ########## not SXM VDIs else: # it's not SXM VDI, just attach it self._map_VHD(vdi_uuid) if not util.pathexists(self.path): raise xs_errors.XenError('VDIUnavailable', opterr='Could not find: %s' % self.path) return VDI.VDI.attach(self, self.sr.uuid, self.uuid)
def generate_config(self, sr_uuid, vdi_uuid): util.SMlog("OCFSFileVDI.generate_config") if not util.pathexists(self.path): raise xs_errors.XenError('VDIUnavailable') resp = {} resp['device_config'] = self.sr.dconf resp['sr_uuid'] = sr_uuid resp['vdi_uuid'] = vdi_uuid resp['command'] = 'vdi_attach_from_config' # Return the 'config' encoded within a normal XMLRPC response so that # we can use the regular response/error parsing code. config = xmlrpclib.dumps(tuple([resp]), "vdi_attach_from_config") return xmlrpclib.dumps((config,), "", True)
def attach_from_config(self, sr_uuid, vdi_uuid): """ Attach and activate a VDI using config generated by vdi_generate_config above. This is used for cases such as the HA state-file and the redo-log. """ util.SMlog("FileVDI.attach_from_config") try: if not util.pathexists(self.sr.path): self.sr.attach(sr_uuid) except: util.logException("FileVDI.attach_from_config") raise xs_errors.XenError("SRUnavailable", opterr="Unable to attach from config")
def get(self, type, id): """Get the value for the journal entry of type "type" for "id". Return None if no such entry exists""" path = self._getPath(type, id) if not util.pathexists(path): return None try: f = open(path, "r") except IOError, e: if e.errno == errno.ENOENT: # the file can disappear any time, since there is no locking return None raise
def delete(self, sr_uuid, vdi_uuid, data_only = False): if not util.ioretry(lambda: util.pathexists(self.path)): return super(FileVDI, self).delete(sr_uuid, vdi_uuid, data_only) if self.attached: raise xs_errors.XenError('VDIInUse') try: util.force_unlink(self.path) except Exception, e: raise xs_errors.XenError( 'VDIDelete', opterr='Failed to unlink file during deleting VDI: %s' % str(e))
# Set the target path temporarily to the base dir # so that we can create the target SR directory self.remotepath = self.dconf['serverpath'].encode('utf-8') try: self.mount_remotepath(sr_uuid) except Exception, exn: try: os.rmdir(self.path) except: pass raise exn if not self.nosubdir: newpath = os.path.join(self.path, sr_uuid) if util.ioretry(lambda: util.pathexists(newpath)): if len(util.ioretry(lambda: util.listdir(newpath))) != 0: self.detach(sr_uuid) raise xs_errors.XenError('SRExists') else: try: util.ioretry(lambda: util.makedirs(newpath)) except util.CommandException, inst: if inst.code != errno.EEXIST: self.detach(sr_uuid) raise xs_errors.XenError( 'NFSCreate', opterr='remote directory creation error is %d' % inst.code) self.detach(sr_uuid)
def _checkmount(self): return util.pathexists(self.path) \ and util.ismount(self.path)
def checkmount(self): return util.ioretry(lambda: ((util.pathexists(self.mountpoint) and \ util.ismount(self.mountpoint)) and \ util.pathexists(self.linkpath)))
def _lvmBugCleanup(path): # the device should not exist at this point. If it does, this was an LVM # bug, and we manually clean up after LVM here mapperDevice = path[5:].replace("-", "--").replace("/", "-") mapperPath = "/dev/mapper/" + mapperDevice nodeExists = False cmd = [CMD_DMSETUP, "status", mapperDevice] try: util.pread(cmd, expect_rc=1) except util.CommandException, e: if e.code == 0: nodeExists = True if not util.pathexists(mapperPath) and not nodeExists: return util.SMlog("_lvmBugCleanup: seeing dm file %s" % mapperPath) # destroy the dm device if nodeExists: util.SMlog("_lvmBugCleanup: removing dm device %s" % mapperDevice) cmd = [CMD_DMSETUP, "remove", mapperDevice] for i in range(LVM_FAIL_RETRIES): try: util.pread2(cmd) break except util.CommandException, e: if i < LVM_FAIL_RETRIES - 1: util.SMlog("Failed on try %d, retrying" % i)
def create(self, sr_uuid, size): self.__check_license() if self.checkmount(): raise xs_errors.XenError('SMBAttached') try: self.mount() except SMBException, exc: try: os.rmdir(self.mountpoint) except: pass raise xs_errors.XenError('SMBMount', opterr=exc.errstr) if util.ioretry(lambda: util.pathexists(self.linkpath)): if len(util.ioretry(lambda: util.listdir(self.linkpath))) != 0: self.detach(sr_uuid) raise xs_errors.XenError('SRExists') else: try: util.ioretry(lambda: util.makedirs(self.linkpath)) os.symlink(self.linkpath, self.path) except util.CommandException, inst: if inst.code != errno.EEXIST: try: self.unmount(self.mountpoint, True) except SMBException: util.logException('SMBSR.unmount()') raise xs_errors.XenError( 'SMBCreate',
def load(self, vdi_uuid): self.lock = self.sr.lock self.sr.srcmd.params['o_direct'] = self.sr.o_direct if self.sr.srcmd.cmd == "vdi_create": self.vdi_type = vhdutil.VDI_TYPE_VHD if self.sr.srcmd.params.has_key("vdi_sm_config") and \ self.sr.srcmd.params["vdi_sm_config"].has_key("type"): vdi_type = self.sr.srcmd.params["vdi_sm_config"]["type"] if not self.VDI_TYPE.get(vdi_type): raise xs_errors.XenError('VDIType', opterr='Invalid VDI type %s' % vdi_type) self.vdi_type = self.VDI_TYPE[vdi_type] self.path = os.path.join(self.sr.path, "%s%s" % \ (vdi_uuid, vhdutil.FILE_EXTN[self.vdi_type])) else: found = self._find_path_with_retries(vdi_uuid) if not found: if self.sr.srcmd.cmd == "vdi_delete": # Could be delete for CBT log file self.path = os.path.join(self.sr.path, "%s.%s" % (vdi_uuid, self.PARAM_VHD)) return if self.sr.srcmd.cmd == "vdi_attach_from_config": return raise xs_errors.XenError('VDIUnavailable', opterr="VDI %s not found" % vdi_uuid) if self.vdi_type == vhdutil.VDI_TYPE_VHD and \ self.sr.__dict__.get("vhds") and self.sr.vhds.get(vdi_uuid): # VHD info already preloaded: use it instead of querying directly vhdInfo = self.sr.vhds[vdi_uuid] self.utilisation = vhdInfo.sizePhys self.size = vhdInfo.sizeVirt self.hidden = vhdInfo.hidden if self.hidden: self.managed = False self.parent = vhdInfo.parentUuid if self.parent: self.sm_config_override = {'vhd-parent':self.parent} else: self.sm_config_override = {'vhd-parent':None} return try: # Change to the SR directory in case parent # locator field path has changed os.chdir(self.sr.path) except Exception as chdir_exception: util.SMlog("Unable to change to SR directory, SR unavailable, %s", str(chdir_exception)) raise xs_errors.XenError('SRUnavailable', opterr=str(chdir_exception)) if util.ioretry(lambda: util.pathexists(self.path)): try: st = util.ioretry(lambda: os.stat(self.path)) self.utilisation = long(st.st_size) except util.CommandException, inst: if inst.code == errno.EIO: raise xs_errors.XenError('VDILoad', \ opterr='Failed load VDI information %s' % self.path) else: raise xs_errors.XenError('VDIType', \ opterr='Invalid VDI type %s' % self.vdi_type) if self.vdi_type == vhdutil.VDI_TYPE_RAW: self.exists = True self.size = self.utilisation self.sm_config_override = {'type':self.PARAM_RAW} return if self.vdi_type == CBTLOG_TAG: self.exists = True self.size = self.utilisation return try: # The VDI might be activated in R/W mode so the VHD footer # won't be valid, use the back-up one instead. diskinfo = util.ioretry(lambda: self._query_info(self.path, True)) if diskinfo.has_key('parent'): self.parent = diskinfo['parent'] self.sm_config_override = {'vhd-parent':self.parent} else: self.sm_config_override = {'vhd-parent':None} self.parent = '' self.size = long(diskinfo['size']) * 1024 * 1024 self.hidden = long(diskinfo['hidden']) if self.hidden: self.managed = False self.exists = True except util.CommandException, inst: raise xs_errors.XenError('VDILoad', \ opterr='Failed load VDI information %s' % self.path)
def attach(self, sr_uuid, vdi_uuid): util.SMlog("RBDVDI.attach for %s" % self.uuid) vdi_ref = self.session.xenapi.VDI.get_by_uuid(vdi_uuid) sm_config = self.session.xenapi.VDI.get_sm_config(vdi_ref) if sm_config.has_key("snapshot-of"): base_uuid = sm_config["snapshot-of"] # it's a snapshot VDI self.path = self.sr._get_snap_path(base_uuid, vdi_uuid) else: self.path = self.sr._get_path(vdi_uuid) if not hasattr(self, 'xenstore_data'): self.xenstore_data = {} self.xenstore_data.update( scsiutil.update_XS_SCSIdata( self.uuid, scsiutil.gen_synthetic_page_data(self.uuid))) self.xenstore_data['storage-type'] = 'rbd' self.xenstore_data['vdi-type'] = self.vdi_type self.attached = True self.session.xenapi.VDI.remove_from_sm_config(vdi_ref, 'attached') self.session.xenapi.VDI.add_to_sm_config(vdi_ref, 'attached', 'true') self.size = int(self.session.xenapi.VDI.get_virtual_size(vdi_ref)) ########## vdis = self.session.xenapi.SR.get_VDIs(self.sr.sr_ref) has_a_snapshot = False for tmp_vdi in vdis: tmp_vdi_uuid = self.session.xenapi.VDI.get_uuid(tmp_vdi) tmp_sm_config = self.session.xenapi.VDI.get_sm_config(tmp_vdi) if tmp_sm_config.has_key("snapshot-of"): if tmp_sm_config["snapshot-of"] == vdi_uuid: has_a_snapshot = True if tmp_sm_config.has_key("sxm_mirror"): sxm_mirror_vdi = vdi_uuid ########## SXM VDIs if sm_config.has_key("base_mirror"): if has_a_snapshot: # it's a mirror vdi of storage migrating VM # it's attached first self.session.xenapi.VDI.add_to_sm_config( vdi_ref, 'sxm_mirror', 'true') # creating dm snapshot dev self._setup_mirror(vdi_uuid, self.size) else: # it's a base vdi of storage migrating VM # it's attached after mirror VDI and mirror snapshot VDI has been created self._map_VHD(vdi_uuid) ########## not SXM VDIs elif sm_config.has_key("snapshot-of"): base_uuid = sm_config["snapshot-of"] # it's a snapshot VDI, attach it as snapshot self._map_SNAP(base_uuid, vdi_uuid) else: # it's not SXM VDI, just attach it self._map_VHD(vdi_uuid) if not util.pathexists(self.path): raise xs_errors.XenError('VDIUnavailable', opterr='Could not find: %s' % self.path) return VDI.VDI.attach(self, self.sr.uuid, self.uuid)
def _cbt_log_exists(self, logpath): return util.pathexists(logpath)
def _checkmount(self): return util.ioretry(lambda: util.pathexists(self.path)) \ and util.ioretry(lambda: util.ismount(self.path))
def _checkmount(self): return util.ioretry(lambda: util.pathexists(self.path) and \ (util.ismount(self.path) or \ util.pathexists(self.remotepath) and self._isbind()))
def load(self, vdi_uuid): self.lock = self.sr.lock if self.sr.srcmd.cmd == "vdi_create": self.vdi_type = vhdutil.VDI_TYPE_VHD if self.sr.srcmd.params.has_key("vdi_sm_config") and \ self.sr.srcmd.params["vdi_sm_config"].has_key("type"): vdi_type = self.sr.srcmd.params["vdi_sm_config"]["type"] if not self.VDI_TYPE.get(vdi_type): raise xs_errors.XenError('VDIType', opterr='Invalid VDI type %s' % vdi_type) self.vdi_type = self.VDI_TYPE[vdi_type] self.path = os.path.join(self.sr.path, "%s%s" % \ (vdi_uuid, vhdutil.FILE_EXTN[self.vdi_type])) else: vhd_path = os.path.join(self.sr.path, "%s.%s" % \ (vdi_uuid, self.PARAM_VHD)) if util.ioretry(lambda: util.pathexists(vhd_path)): self.vdi_type = vhdutil.VDI_TYPE_VHD self.path = vhd_path else: raw_path = os.path.join(self.sr.path, "%s.%s" % \ (vdi_uuid, self.PARAM_RAW)) self.vdi_type = vhdutil.VDI_TYPE_RAW self.path = raw_path self.hidden = False if not util.ioretry(lambda: util.pathexists(self.path)): if self.sr.srcmd.cmd == "vdi_attach_from_config": return raise xs_errors.XenError('VDIUnavailable', opterr="%s not found" % self.path) if self.vdi_type == vhdutil.VDI_TYPE_VHD and \ self.sr.__dict__.get("vhds") and self.sr.vhds.get(vdi_uuid): # VHD info already preloaded: use it instead of querying directly vhdInfo = self.sr.vhds[vdi_uuid] self.utilisation = vhdInfo.sizePhys self.size = vhdInfo.sizeVirt self.hidden = vhdInfo.hidden if self.hidden: self.managed = False self.parent = vhdInfo.parentUuid if self.parent: self.sm_config_override = {'vhd-parent': self.parent} else: self.sm_config_override = {'vhd-parent': None} return try: # Change to the SR directory in case parent # locator field path has changed os.chdir(self.sr.path) except: raise xs_errors.XenError('SRUnavailable') if util.ioretry(lambda: util.pathexists(self.path)): try: st = util.ioretry(lambda: os.stat(self.path)) self.utilisation = long(st.st_size) except util.CommandException, inst: if inst.code == errno.EIO: raise xs_errors.XenError('VDILoad', \ opterr='Failed load VDI information %s' % self.path) else: raise xs_errors.XenError('VDIType', \ opterr='Invalid VDI type %s' % self.vdi_type) if self.vdi_type == vhdutil.VDI_TYPE_RAW: self.exists = True self.size = self.utilisation self.sm_config_override = {'type': self.PARAM_RAW} return try: diskinfo = util.ioretry(lambda: self._query_info(self.path)) if diskinfo.has_key('parent'): self.parent = diskinfo['parent'] self.sm_config_override = {'vhd-parent': self.parent} else: self.sm_config_override = {'vhd-parent': None} self.parent = '' self.size = long(diskinfo['size']) * 1024 * 1024 self.hidden = long(diskinfo['hidden']) if self.hidden: self.managed = False self.exists = True except util.CommandException, inst: raise xs_errors.XenError('VDILoad', \ opterr='Failed load VDI information %s' % self.path)
def test(self, name): """Test the flag""" flagFile = os.path.join(self.nsDir, name) return util.pathexists(flagFile)
def _getlockstatus(self): if util.ioretry(lambda: util.pathexists(self.sr.path)): if len(filter(self.match_locks, util.ioretry(lambda: \ util.listdir(self.sr.path)))) > 0: return True return False