def _getVMs(self): master = self.getMasterUUID() cmd = "xe vm-list dom-id=0 resident-on=%s params=uuid --minimal" % \ master stdout = tutil.execCmd(cmd, 0, self.logger, LOG_LEVEL_CMD + 1) masterVM = stdout.strip() if not tutil.validateUUID(masterVM): raise SMException("Got invalid UUID: %s" % masterVM) slaveVM = None host = self.getThisHost() if host == master: cmd = "xe host-list params=uuid --minimal" stdout = tutil.execCmd(cmd, 0, self.logger, LOG_LEVEL_CMD + 1) hosts = stdout.strip().split(",") for h in hosts: if h != master: host = h break if host == master: return (masterVM, None) cmd = "xe vm-list dom-id=0 resident-on=%s params=uuid --minimal" % host stdout = tutil.execCmd(cmd, 0, self.logger, LOG_LEVEL_CMD + 1) slaveVM = stdout.strip() if not tutil.validateUUID(slaveVM): raise SMException("Got invalid UUID: %s" % slaveVM) return (masterVM, slaveVM)
def _unplugVBD(self, vbd): self.logger.log("Unplugging VBD %s" % vbd, 2) cmd = "xe vbd-unplug uuid=%s" % vbd try: tutil.execCmd(cmd, 0, self.logger, LOG_LEVEL_CMD) except tutil.CommandException, inst: if str(inst).find("device is not currently attached") == -1: raise
def mount(self, dev, mountDir): if not tutil.pathExists(mountDir): try: os.makedirs(mountDir) except OSError: raise SMException("makedirs failed (for %s)" % mountDir) if not tutil.pathExists(mountDir): raise SMException("mount dir '%s' not created" % mountDir) cmd = "mount %s %s" % (dev, mountDir) tutil.execCmd(cmd, 0, self.logger, LOG_LEVEL_CMD)
def _getInfoVDI(self, uuid): """Retrieves the parameters of the spcified VDI in the form of a dictionary.""" cmd = "xe vdi-list uuid=%s params=all" % uuid stdout = tutil.execCmd(cmd, 0, self.logger, LOG_LEVEL_CMD + 1) return self._toDict(stdout)
def _getInfoSR(self, uuid): cmd = "xe sr-list uuid=%s params=all" % uuid stdout = tutil.execCmd(cmd, 0, self.logger, LOG_LEVEL_CMD + 1) info = self._toDict(stdout) if not info["sm-config"]: info["sm-config"] = dict() return info
def _createVBD(self, vdi, vm, ro = False, vbdLetter = None, unpluggable = True): """Creates a VBD for the specified VDI on the specified VM. If a device is not supplied (vbdLetter), the first available one is used. Returns the UUID of the VBD.""" mode = "rw" if ro: mode="ro" if None == vbdLetter: devices = self._vm_get_allowed_vbd_devices(vm) assert len(devices) > 0 # FIXME raise exception instead vbdLetter = devices[0] cmd = "xe vbd-create vm-uuid=%s vdi-uuid=%s type=disk mode=%s "\ "device=%s unpluggable=%s" % (vm, vdi, mode, vbdLetter, unpluggable) stdout = tutil.execCmd(cmd, 0, self.logger, LOG_LEVEL_CMD) # XXX xe vbd-create returns 0 if the device name is invalid if not stdout: raise SMException("No output from vbd-create") vbdUuid = stdout.strip() if not tutil.validateUUID(vbdUuid): raise SMException("Got invalid UUID: %s" % vbdUuid) return vbdUuid
def _getPoolUUID(self): cmd = "xe pool-list params=uuid --minimal" stdout = tutil.execCmd(cmd, 0, self.logger, LOG_LEVEL_CMD) poolUuid = stdout.strip() if not tutil.validateUUID(poolUuid): raise SMException("Got invalid UUID: %s" % poolUuid) return poolUuid
def _onMaster(self, plugin, fn, args): argsStr = "" for key, val in args.iteritems(): argsStr += " args:%s=%s" % (key, val) cmd = "xe host-call-plugin host-uuid=%s plugin=%s fn=%s %s" % \ (self.getMasterUUID(), plugin, fn, argsStr) stdout = tutil.execCmd(cmd, 0, self.logger, LOG_LEVEL_CMD) return stdout.strip()
def _vdi_get_vbd(self, vdi_uuid): """Retrieves the UUID of the specified VDI, else None.""" out = tutil.execCmd('xe vbd-list vdi-uuid=' + vdi_uuid + ' --minimal', 0, self.logger, LOG_LEVEL_CMD).strip() if '' != out: return out else: return None
def _getVDIs(self, sr): cmd = "xe vdi-list sr-uuid=%s params=uuid --minimal" % sr stdout = tutil.execCmd(cmd, 0, self.logger, LOG_LEVEL_SUB) stdout = stdout.strip() if not stdout: return [] vdiList = stdout.split(",") return vdiList
def _findSR(self, type): cmd = "xe sr-list type=%s --minimal" % type stdout = tutil.execCmd(cmd, 0, self.logger, LOG_LEVEL_CMD) stdout = stdout.strip() if not stdout: return [] srList = stdout.split(",") return srList
def _getDefaultSR(self): cmd = "xe pool-param-get param-name=default-SR uuid=%s --minimal" % \ self._getPoolUUID() stdout = tutil.execCmd(cmd, 0, self.logger, LOG_LEVEL_CMD) uuid = stdout.strip() if tutil.validateUUID(uuid): return uuid return None
def _getDom0UUID(self): cmd = "xe vm-list dom-id=0 params=uuid --minimal" stdout = tutil.execCmd(cmd, 0, self.logger, LOG_LEVEL_CMD) if not stdout: raise SMException("No output from vm-list") dom0uuid = stdout.strip() if not tutil.validateUUID(dom0uuid): raise SMException("Got invalid UUID: %s" % dom0uuid) return dom0uuid
def _findSR(self, type): "Retrieves all SRs of the specified type in the form of a list." cmd = "xe sr-list type=%s --minimal" % type stdout = tutil.execCmd(cmd, 0, self.logger, LOG_LEVEL_CMD) stdout = stdout.strip() if not stdout: return [] srList = stdout.split(",") return srList
def _getInfoSR(self, uuid): """Retrieves the parameters of the specified SR in the form of a dictionary.""" cmd = "xe sr-list uuid=%s params=all" % uuid stdout = tutil.execCmd(cmd, 0, self.logger, LOG_LEVEL_CMD + 1) info = self._toDict(stdout) if not info["sm-config"]: info["sm-config"] = dict() return info
def _getVDIs(self, sr): """Retrieves the UUIDs of all the VDIs on the specified SR, in the form of a list.""" cmd = "xe vdi-list sr-uuid=%s params=uuid --minimal" % sr stdout = tutil.execCmd(cmd, 0, self.logger, LOG_LEVEL_SUB) stdout = stdout.strip() if not stdout: return [] vdiList = stdout.split(",") return vdiList
def _createSR(self, type, size): cmd = "xe sr-create name-label='%s' physical-size=%d \ content-type=user device-config:device=%s type=%s" % \ (self.SR_LABEL, size, self.targetDevice, type) stdout = tutil.execCmd(cmd, 0, self.logger, LOG_LEVEL_CMD) if not stdout: raise SMException("No output from sr-create") srUuid = stdout.strip() if not tutil.validateUUID(srUuid): raise SMException("Got invalid UUID: %s" % srUuid) return srUuid
def _getVHDParentNoCheck(self, sr, path): cmd = "vhd-util read -p -n %s" % path text = tutil.execCmd(cmd, 0, self.logger, LOG_LEVEL_SUB) for line in text.split('\n'): if line.find("decoded name :") != -1: val = line.split(':')[1].strip() vdi = val.replace("--", "-")[-40:] if vdi[1:].startswith("LV-"): vdi = vdi[1:] return os.path.join(self.getPathSR(sr), vdi) return None
def _vdi_get_parent(self, vdi_uuid): """Retrieves the parent UUID of the VDI.""" # XXX It is expected to be a VHD VDI. out = tutil.execCmd('xe vdi-param-get uuid=' + vdi_uuid \ + ' param-name=sm-config', 0, self.logger, LOG_LEVEL_CMD) assert re.match('vhd-parent: ' + tutil.uuidre + '; vdi_type: vhd\n', out) return re.search(tutil.uuidre, out).group(0)
def _cloneVDI(self, vdi): cmd = "xe vdi-clone uuid=%s" % vdi for i in range(CMD_NUM_RETRIES): try: stdout = tutil.execCmd(cmd, 0, self.logger, LOG_LEVEL_CMD) break except tutil.CommandException, inst: if str(inst).find("VDI_IN_USE") != -1: self.logger.log("Command failed, retrying", LOG_LEVEL_CMD) time.sleep(CMD_RETRY_PERIOD) else: raise
def _createVBD(self, vdi, vbdLetter, ro, vm): mode = "rw" if ro: mode="ro" cmd = "xe vbd-create vm-uuid=%s vdi-uuid=%s type=disk mode=%s "\ "device=xvd%s unpluggable=true" % (vm, vdi, mode, vbdLetter) stdout = tutil.execCmd(cmd, 0, self.logger, LOG_LEVEL_CMD) if not stdout: raise SMException("No output from vbd-create") vbdUuid = stdout.strip() if not tutil.validateUUID(vbdUuid): raise SMException("Got invalid UUID: %s" % vbdUuid) return vbdUuid
def _getLeafVDIs(self, sr): vdiList = self.getVDIs(sr) cmd = "xe vdi-list sr-uuid=%s name-label='base copy' params=uuid --minimal" % sr stdout = tutil.execCmd(cmd, 0, self.logger, LOG_LEVEL_SUB) stdout = stdout.strip() if not stdout: baseList = [] baseList = stdout.split(",") leafList = [] for vdi in vdiList: if vdi not in baseList: leafList.append(vdi) return leafList
def _createVDI(self, sr, size, name, raw): cmd = "xe vdi-create sr-uuid=%s name-label='%s' \ type=user virtual-size=%d" % (sr, name, size) if raw: cmd += " sm-config:type=raw" else: cmd += " sm-config:type=vhd" stdout = tutil.execCmd(cmd, 0, self.logger, LOG_LEVEL_CMD) if not stdout: raise SMException("No output from vdi-create") vdiUuid = stdout.strip() if not tutil.validateUUID(vdiUuid): raise SMException("Got invalid UUID: %s" % vdiUuid) return vdiUuid
def _snapshotVDI(self, vdi, single = None): cmd = "xe vdi-snapshot uuid=%s" % vdi if single: cmd += " driver-params:type=single" for i in range(CMD_NUM_RETRIES): try: stdout = tutil.execCmd(cmd, 0, self.logger, LOG_LEVEL_CMD) break except tutil.CommandException, inst: if str(inst).find("VDI_IN_USE") != -1: self.logger.log("Command failed, retrying", LOG_LEVEL_CMD) time.sleep(CMD_RETRY_PERIOD) else: raise
def _getParam(self, obj, uuid, param): """Retrieves the specified parameter(s) of the specified object. Arguments: object: An object can be anything that would render the "<obj>-list" string valid for passing it as an argument to a "xe" command, e.g. sr-list, vdi-list etc. The uuid is the UUID of the desired object. uuid: The UUID of the specified object. param: The desired parameter, e.g. all, allow-caching, etc.""" cmd = "xe %s-list uuid=%s params=%s" % (obj, uuid, param) return tutil.execCmd(cmd, 0, self.logger, LOG_LEVEL_SUB)
def _getLeafVDIs(self, sr): """Retrieves the UUIDs of the leaf VDIs on the specified SR in the form of a list.""" vdiList = self.getVDIs(sr) cmd = "xe vdi-list sr-uuid=%s name-label='base copy' params=uuid --minimal" % sr stdout = tutil.execCmd(cmd, 0, self.logger, LOG_LEVEL_SUB) stdout = stdout.strip() if not stdout: baseList = [] baseList = stdout.split(",") # TODO Which VDIs does the name-label='base copy' return? leafList = [] for vdi in vdiList: if vdi not in baseList: leafList.append(vdi) return leafList
def _cloneVDI(self, vdi): cmd = "xe vdi-clone uuid=%s" % vdi for i in range(CMD_NUM_RETRIES): try: stdout = tutil.execCmd(cmd, 0, self.logger, LOG_LEVEL_CMD) break except tutil.CommandException as inst: if str(inst).find("VDI_IN_USE") != -1: self.logger.log("Command failed, retrying", LOG_LEVEL_CMD) time.sleep(CMD_RETRY_PERIOD) else: raise if not stdout: raise SMException("No output from vdi-snapshot") cloneUuid = stdout.strip() if not tutil.validateUUID(cloneUuid): raise SMException("Got invalid UUID: %s" % cloneUuid) return cloneUuid
def _snapshotVDI(self, vdi, single=None): cmd = "xe vdi-snapshot uuid=%s" % vdi if single: cmd += " driver-params:type=single" for i in range(CMD_NUM_RETRIES): try: stdout = tutil.execCmd(cmd, 0, self.logger, LOG_LEVEL_CMD) break except tutil.CommandException as inst: if str(inst).find("VDI_IN_USE") != -1: self.logger.log("Command failed, retrying", LOG_LEVEL_CMD) time.sleep(CMD_RETRY_PERIOD) else: raise if not stdout: raise SMException("No output from vdi-snapshot") snapUuid = stdout.strip() if not tutil.validateUUID(snapUuid): raise SMException("Got invalid UUID: %s" % snapUuid) return snapUuid
def _createVDI(self, sr, size = 2**30, name = None, raw = False): """Creates a VDI on the specified SR using the specified size and name. The raw argument controls whether the VDI to be created must be of raw or VHD format. Returns the UUID of the created VDI.""" if None == name: name = ''.join(random.choice(string.hexdigits) for x in range(3)) cmd = "xe vdi-create sr-uuid=%s name-label='%s' \ type=user virtual-size=%d name-description=test-vdi" \ % (sr, name, size) if raw: cmd += " sm-config:type=raw" else: cmd += " sm-config:type=vhd" stdout = tutil.execCmd(cmd, 0, self.logger, LOG_LEVEL_CMD) if not stdout: raise SMException("No output from vdi-create") vdiUuid = stdout.strip() if not tutil.validateUUID(vdiUuid): raise SMException("Got invalid UUID: %s" % vdiUuid) return vdiUuid
def _destroySR(self, sr): self.logger.log("Destroying SR %s" % sr, 2) cmd = "xe pbd-list sr-uuid=%s params=uuid" % sr stdout = tutil.execCmd(cmd, 0, self.logger, LOG_LEVEL_CMD) if not stdout: raise SMException("PBD not found for SR %s" % sr) pbdUuid = stdout.strip().split(":")[1].strip() cmd = "xe pbd-unplug uuid=%s" % pbdUuid tutil.execCmd(cmd, 0, self.logger, LOG_LEVEL_CMD) cmd = "xe sr-destroy uuid=%s" % sr tutil.execCmd(cmd, 0, self.logger, LOG_LEVEL_CMD) cmd = "xe sr-list uuid=%s" % sr stdout = tutil.execCmd(cmd, 0, self.logger, LOG_LEVEL_CMD) if stdout: raise SMException("SR %s still present after sr-destroy" % sr)
def _host_getParam(self, uuid, param): return tutil.execCmd('xe host-param-get uuid=' + uuid \ + ' param-name=' + param + ' --minimal', 0, self.logger, LOG_LEVEL_CMD)
def _vm_start(self, vm_uuid): tutil.execCmd('xe vm-start uuid=' + vm_uuid, 0, self.logger, LOG_LEVEL_CMD)