def xpath(self, *args, **kwargs): # Must use this function for ALL XML parsing ret = util.xpath(self.get_xml(), *args, **kwargs) if ret: return ret if not self.is_active(): return ret # The running config did not have the info requested return util.xpath(self.get_xml(inactive=True), *args, **kwargs)
def get_size(self): """ Return size of existing storage """ if self._size is None: ret = 0 if self._vol_object: ret = util.xpath(self._get_vol_xml(), "/volume/capacity") elif self._pool_object: ret = util.xpath(self._get_pool_xml(), "/pool/capacity") elif self._path: ignore, ret = util.stat_disk(self.path) self._size = (float(ret) / 1024.0 / 1024.0 / 1024.0) return self._size
def get_dev_type(self): """ Return disk 'type' value per storage settings """ if self._dev_type is None: if self._vol_object: t = self._vol_object.info()[0] if t == libvirt.VIR_STORAGE_VOL_FILE: self._dev_type = "file" elif t == libvirt.VIR_STORAGE_VOL_BLOCK: self._dev_type = "block" else: self._dev_type = "file" elif self._pool_object: xml = self._get_pool_xml() for source, source_type in [ ("dir", "dir"), ("device", "block"), ("adapter", "block")]: if util.xpath(xml, "/pool/source/%s/@dev" % source): self._dev_type = source_type break elif self._path: if os.path.isdir(self._path): self._dev_type = "dir" elif util.stat_disk(self._path)[0]: self._dev_type = "file" else: self._dev_type = "block" if not self._dev_type: self._dev_type = "block" return self._dev_type
def check_pool(poolname, path): pool = conn.storagePoolLookupByName(poolname) xml = pool.XMLDesc(0) for element in ["dir", "device", "adapter"]: xml_path = util.xpath(xml, "/pool/source/%s/@path" % element) if xml_path == path: return pool
def get_volume_for_pool(pool_object=None, pool_name=None, conn=None): """ Returns volume class associated with passed pool_object/name """ pool_object = StorageVolume.lookup_pool_by_name(pool_object=pool_object, pool_name=pool_name, conn=conn) return StoragePool.get_volume_for_pool(util.xpath( pool_object.XMLDesc(0), "/pool/@type"))
def __init__(self, conn, name, input_vol): if not isinstance(input_vol, libvirt.virStorageVol): raise ValueError(_("input_vol must be a virStorageVol")) pool = input_vol.storagePoolLookupByVolume() # Populate some basic info xml = input_vol.XMLDesc(0) typ = input_vol.info()[0] cap = int(util.xpath(xml, "/volume/capacity")) alc = int(util.xpath(xml, "/volume/allocation")) fmt = util.xpath(xml, "/volume/target/format/@type") StorageVolume.__init__(self, conn, name=name, pool=pool, pool_name=pool.name(), allocation=alc, capacity=cap) self.input_vol = input_vol self._file_type = typ self._format = fmt
def identify_file(input_file): """ Return True if the given file is of this format. """ infile = open(input_file, "r") xml = infile.read() infile.close() res = False try: if xml.count("</Envelope>"): res = bool(util.xpath(xml, "/ovf:Envelope", return_list=True, register_namespace=register_namespace)) except Exception, e: logging.debug("Error parsing OVF XML: %s", str(e))
def host_disk_space(conn): pool = get_default_pool(conn) path = get_default_dir(conn) avail = 0 if pool and pool.is_active(): # FIXME: make sure not inactive? # FIXME: use a conn specific function after we send pool-added pool.refresh() avail = int(util.xpath(pool.get_xml(), "/pool/available")) elif not conn.is_remote() and os.path.exists(path): vfs = os.statvfs(os.path.dirname(path)) avail = vfs[statvfs.F_FRSIZE] * vfs[statvfs.F_BAVAIL] return float(avail / 1024.0 / 1024.0 / 1024.0)
def get_type(self): return util.xpath(self.get_xml(), "/pool/@type")
def _get_path(self): if self._vol_install and not self._path: self._path = (util.xpath(self._vol_install.pool.XMLDesc(0), "/pool/target/path") + "/" + self._vol_install.name) return self._path
def get_target_path(self): return util.xpath(self.get_xml(), "/volume/target/path")
def can_pxe(self): xml = self.get_xml() forward = self.get_ipv4_forward()[0] if forward and forward != "nat": return True return bool(util.xpath(xml, "/network/ip/dhcp/bootp/@file"))
def get_ipv4_forward(self): xml = self.get_xml() fw = util.xpath(xml, "/network/forward/@mode") forwardDev = util.xpath(xml, "/network/forward/@dev") return [fw, forwardDev]
def get_ipv6_routing(self): xml = self.get_xml() ipv6_routing = util.xpath(xml, "/network/@ipv6") return ipv6_routing
def get_name_domain(self): xml = self.get_xml() name_domain = util.xpath(xml, "/network/domain/@name") return name_domain
def get_format(self): return util.xpath(self.get_xml(), "/volume/target/format/@type")
def get_capacity(self): return long(util.xpath(self.get_xml(), "/pool/capacity"))
def get_available(self): return long(util.xpath(self.get_xml(), "/pool/available"))
def get_allocation(self): return long(util.xpath(self.get_xml(), "/pool/allocation"))
def get_target_path(self): return util.xpath(self.get_xml(), "/pool/target/path") or ""
def get_type(self): return util.xpath(self.get_xml(), "/volume/format/@type")
def get_driver_type(self): if self._vol_object: fmt = util.xpath(self._get_vol_xml(), "/volume/target/format/@type") return fmt return None
def _upload_file(conn, meter, destpool, src): # Build stream object stream = conn.newStream(0) def safe_send(data): while True: ret = stream.send(data) if ret == 0 or ret == len(data): break data = data[ret:] if meter is None: meter = urlgrabber.progress.BaseMeter() # Build placeholder volume size = os.path.getsize(src) basename = os.path.basename(src) poolpath = util.xpath(destpool.XMLDesc(0), "/pool/target/path") name = Storage.StorageVolume.find_free_name(basename, pool_object=destpool) if name != basename: logging.debug("Generated non-colliding volume name %s", name) vol_install = VirtualDisk.build_vol_install(conn, name, destpool, (float(size) / 1024.0 / 1024.0 / 1024.0), True) disk = VirtualDisk(conn) disk.path = os.path.join(poolpath, name) disk.set_create_storage(vol_install=vol_install) disk.validate() disk.setup(meter=meter) vol = disk.get_vol_object() if not vol: raise RuntimeError(_("Failed to lookup scratch media volume")) try: # Register upload offset = 0 length = size flags = 0 stream.upload(vol, offset, length, flags) # Open source file fileobj = file(src, "r") # Start transfer total = 0 meter.start(size=size, text=_("Transferring %s") % os.path.basename(src)) while True: # blocksize = (1024 ** 2) blocksize = 1024 data = fileobj.read(blocksize) if not data: break safe_send(data) total += len(data) meter.update(total) # Cleanup stream.finish() meter.end(size) except: if vol: vol.delete(0) raise return vol