def _make_stub_pool(self): pool = self._get_pool_from_sourcelist() if not pool: pool = StoragePool(self.conn.get_backend()) pool.type = self._get_config_pool_type() pool.name = self.widget("pool-name").get_text() return pool
def populate_pool_type(self): model = self.widget("pool-type").get_model() model.clear() types = StoragePool.get_pool_types() types.sort() for typ in types: model.append([typ, "%s: %s" % (typ, StoragePool.get_pool_type_desc(typ))])
def _build_pool_type_list(self): # [pool type, label] model = Gtk.ListStore(str, str) type_list = self.widget("pool-type") type_list.set_model(model) uiutil.init_combo_text_column(type_list, 1) for typ in sorted(StoragePool.get_pool_types()): desc = StoragePool.get_pool_type_desc(typ) model.append([typ, "%s: %s" % (typ, desc)])
def _populate_pools(self): pool_list = self.widget("pool-list") curpool = self._current_pool() model = pool_list.get_model() # Prevent events while the model is modified pool_list.set_model(None) try: pool_list.get_selection().unselect_all() model.clear() for pool in self.conn.list_pools(): pool.disconnect_by_obj(self) pool.connect("state-changed", self._pool_changed_cb) pool.connect("refreshed", self._pool_changed_cb) name = pool.get_name() typ = StoragePool.get_pool_type_desc(pool.get_type()) label = "%s\n<span size='small'>%s</span>" % (name, typ) row = [None] * POOL_NUM_COLUMNS row[POOL_COLUMN_CONNKEY] = pool.get_connkey() row[POOL_COLUMN_LABEL] = label row[POOL_COLUMN_ISACTIVE] = pool.is_active() row[POOL_COLUMN_PERCENT] = _get_pool_size_percent(pool) model.append(row) finally: pool_list.set_model(model) uiutil.set_list_selection(pool_list, curpool and curpool.get_connkey() or None)
def testEnumerateNetFS(self): name = "pool-netfs-list" host = "example.com" lst = StoragePool.pool_list_from_sources(self.conn, StoragePool.TYPE_NETFS, host=host) self._enumerateCompare(name, lst)
def browse_target_path(self, ignore1=None): startfolder = StoragePool.get_default_dir(self.conn.get_backend()) target = self._browse_file(_("Choose target directory"), startfolder=startfolder, foldermode=True) if target: self.widget("pool-target-path").get_child().set_text(target)
def convert_disks(self, disk_format, destdir=None, dry=False): """ Convert a disk into the requested format if possible, in the given output directory. Raises RuntimeError or other failures. """ if disk_format == "none": disk_format = None if destdir is None: destdir = StoragePool.get_default_dir(self.conn, build=not dry) guest = self.get_guest() for disk in guest.get_devices("disk"): if disk.device != "disk": continue if disk_format and disk.driver_type == disk_format: logging.debug("path=%s is already in requested format=%s", disk.path, disk_format) disk_format = None basepath = os.path.splitext(os.path.basename(disk.path))[0] newpath = re.sub(r"\s", "_", basepath) if disk_format: newpath += "." + disk_format newpath = os.path.join(destdir, newpath) if os.path.exists(newpath): raise RuntimeError(_("New path name '%s' already exists") % newpath) if not disk_format or disk_format == "none": self._copy_file(disk.path, newpath, dry) else: self._qemu_convert(disk.path, newpath, disk_format, dry) disk.driver_type = disk_format disk.path = newpath self._err_clean.append(newpath)
def manage_path(conn, path): """ If path is not managed, try to create a storage pool to probe the path """ vol, pool, path_is_pool = check_if_path_managed(conn, path) if vol or pool or not _can_auto_manage(path): return vol, pool, path_is_pool dirname = os.path.dirname(path) poolname = StoragePool.find_free_name( conn, os.path.basename(dirname) or "pool") logging.debug("Attempting to build pool=%s target=%s", poolname, dirname) poolxml = StoragePool(conn) poolxml.name = poolxml.find_free_name( conn, os.path.basename(dirname) or "dirpool") poolxml.type = poolxml.TYPE_DIR poolxml.target_path = dirname pool = poolxml.install(build=False, create=True, autostart=True) conn.clear_cache(pools=True) vol = None for checkvol in pool.listVolumes(): if checkvol == os.path.basename(path): vol = pool.storageVolLookupByName(checkvol) break return vol, pool, False
def _browse_target_cb(self, src): startfolder = StoragePool.get_default_dir(self.conn.get_backend()) target = self.err.browse_local( self.conn, _("Choose target directory"), dialog_type=Gtk.FileChooserAction.SELECT_FOLDER, start_folder=startfolder) if target: self.widget("pool-target-path").get_child().set_text(target)
def _list_pool_sources(self, pool_type): plist = [] try: plist = StoragePool.pool_list_from_sources(self.conn.get_backend(), pool_type) except Exception: # pragma: no cover log.exception("Pool enumeration failed") return plist
def _list_pool_sources(self, pool_type, host=None): plist = [] try: plist = StoragePool.pool_list_from_sources(self.conn.get_backend(), pool_type, host=host) except Exception: logging.exception("Pool enumeration failed") return plist
def list_pool_sources(self, host=None): pool_type = self._pool.type plist = [] try: plist = StoragePool.pool_list_from_sources( self.conn.get_backend(), pool_type, host=host) except Exception: logging.exception("Pool enumeration failed") return plist
def testMisc(self): # Misc coverage testing vol = StorageVolume(self.conn) self.assertTrue(vol.is_size_conflict()[0] is False) fullconn = utils.URIs.open_testdriver_cached() glusterpool = fullconn.storagePoolLookupByName("gluster-pool") diskpool = fullconn.storagePoolLookupByName("disk-pool") glustervol = StorageVolume(fullconn) glustervol.pool = glusterpool self.assertTrue(glustervol.supports_format() is True) diskvol = StorageVolume(fullconn) diskvol.pool = diskpool self.assertTrue(diskvol.supports_format() is False) glusterpool.destroy() StoragePool.ensure_pool_is_running(glusterpool) # Check pool collision detection self.assertEqual(StoragePool.find_free_name(fullconn, "gluster-pool"), "gluster-pool-1")
def testMisc(): conn = utils.URIs.open_testdefault_cached() # Misc coverage testing vol = StorageVolume(conn) assert vol.is_size_conflict()[0] is False fullconn = utils.URIs.open_testdriver_cached() glusterpool = fullconn.storagePoolLookupByName("gluster-pool") diskpool = fullconn.storagePoolLookupByName("disk-pool") glustervol = StorageVolume(fullconn) glustervol.pool = glusterpool assert glustervol.supports_format() is False diskvol = StorageVolume(fullconn) diskvol.pool = diskpool assert diskvol.supports_format() is False glusterpool.destroy() StoragePool.ensure_pool_is_running(glusterpool) # Check pool collision detection name = StoragePool.find_free_name(fullconn, "gluster-pool") assert name == "gluster-pool-1"
def _reset_state(self): defaultname = StoragePool.find_free_name(self.conn.get_backend(), "pool") self.widget("pool-name").set_text(defaultname) self.widget("pool-name").grab_focus() self.widget("pool-target-path").get_child().set_text("") self.widget("pool-source-path").get_child().set_text("") self.widget("pool-hostname").set_text("") self.widget("pool-iqn-chk").set_active(False) self.widget("pool-iqn-chk").toggled() self.widget("pool-iqn").set_text("") self.widget("pool-format").set_active(0) self.widget("pool-build").set_sensitive(True) self.widget("pool-build").set_active(False) uiutil.set_list_selection(self.widget("pool-type"), 0) self._show_options_by_pool()
def populate_storage_pools(pool_list, conn, curpool): model = pool_list.get_model() # Prevent events while the model is modified pool_list.set_model(None) model.clear() for uuid in conn.list_pool_uuids(): per = get_pool_size_percent(conn, uuid) pool = conn.get_pool(uuid) name = pool.get_name() typ = StoragePool.get_pool_type_desc(pool.get_type()) label = "%s\n<span size='small'>%s</span>" % (name, typ) model.append([uuid, label, pool.is_active(), per]) pool_list.set_model(model) uiutil.set_row_selection(pool_list, curpool and curpool.get_uuid() or None)
def populate_pool_state(self, uuid): pool = self.conn.get_pool(uuid) pool.tick() auto = pool.get_autostart() active = pool.is_active() # Set pool details state self.widget("pool-details").set_sensitive(True) self.widget("pool-name").set_markup("<b>%s:</b>" % pool.get_name()) self.widget("pool-name-entry").set_text(pool.get_name()) self.widget("pool-name-entry").set_editable(not active) self.widget("pool-sizes").set_markup( """<span size="large">%s Free</span> / <i>%s In Use</i>""" % (pool.get_pretty_available(), pool.get_pretty_allocation())) self.widget("pool-type").set_text( StoragePool.get_pool_type_desc(pool.get_type())) self.widget("pool-location").set_text( pool.get_target_path()) self.widget("pool-state-icon").set_from_icon_name( ((active and self.ICON_RUNNING) or self.ICON_SHUTOFF), Gtk.IconSize.BUTTON) self.widget("pool-state").set_text( (active and _("Active")) or _("Inactive")) self.widget("pool-autostart").set_label( (auto and _("On Boot")) or _("Never")) self.widget("pool-autostart").set_active(auto) self.widget("vol-list").set_sensitive(active) self.repopulate_storage_volumes() self.widget("pool-delete").set_sensitive(not active) self.widget("pool-stop").set_sensitive(active) self.widget("pool-start").set_sensitive(not active) self.widget("vol-add").set_sensitive(active) self.widget("vol-add").set_tooltip_text(_("Create new volume")) self.widget("vol-delete").set_sensitive(False) if active and not pool.supports_volume_creation(): self.widget("vol-add").set_sensitive(False) self.widget("vol-add").set_tooltip_text( _("Pool does not support volume creation"))
def convert_disks(self, disk_format, destdir=None, dry=False): """ Convert a disk into the requested format if possible, in the given output directory. Raises RuntimeError or other failures. """ if disk_format == "none": disk_format = None if destdir is None: poolxml = StoragePool.build_default_pool(self.conn) destdir = poolxml.target_path guest = self.get_guest() for disk in guest.devices.disk: if disk.device != "disk": continue if disk_format and disk.driver_type == disk_format: log.debug("path=%s is already in requested format=%s", disk.path, disk_format) disk_format = None basepath = os.path.splitext(os.path.basename(disk.path))[0] newpath = re.sub(r'\s', '_', basepath) if disk_format: newpath += ("." + disk_format) newpath = os.path.join(destdir, newpath) if os.path.exists(newpath) and not _is_test(): raise RuntimeError( _("New path name '%s' already exists") % newpath) if not disk_format or disk_format == "none": self._copy_file(disk.path, newpath, dry) else: self._qemu_convert(disk.path, newpath, disk_format, dry) disk.driver_type = disk_format disk.path = newpath self._err_clean.append(newpath)
def _build_pool(conn, meter, path): pool = StoragePool.lookup_pool_by_path(conn, path) if pool: logging.debug("Existing pool '%s' found for %s", pool.name(), path) pool.refresh(0) return pool name = util.generate_name("boot-scratch", conn.storagePoolLookupByName) logging.debug("Building storage pool: path=%s name=%s", path, name) poolbuild = StoragePool(conn) poolbuild.type = poolbuild.TYPE_DIR poolbuild.name = name poolbuild.target_path = path # Explicitly don't build? since if we are creating this directory # we probably don't have correct perms ret = poolbuild.install(meter=meter, create=True, build=False, autostart=True) conn.clear_cache(pools=True) return ret
def testEnumerateLogical(self): name = "pool-logical-list" lst = StoragePool.pool_list_from_sources(self.conn, StoragePool.TYPE_LOGICAL) self._enumerateCompare(name, lst)
def testEnumerateLogical(): conn = utils.URIs.open_testdefault_cached() lst = StoragePool.pool_list_from_sources(conn, StoragePool.TYPE_LOGICAL) assert lst == ["testvg1", "testvg2"]
def makepool(name, create): poolxml = StoragePool(conn) poolxml.type = "dir" poolxml.name = name poolxml.target_path = "/tmp/foo/bar/baz/%s" % name return poolxml.install(create=create)
def createPool(conn, ptype, poolname=None, fmt=None, target_path=None, source_path=None, source_name=None, iqn=None): if poolname is None: poolname = StoragePool.find_free_name(conn, "%s-pool" % ptype) pool_inst = StoragePool(conn) pool_inst.name = poolname pool_inst.type = ptype if pool_inst.supports_property("hosts"): hostobj = pool_inst.hosts.add_new() hostobj.name = "some.random.hostname" if pool_inst.supports_property("source_path"): pool_inst.source_path = source_path or "/some/source/path" if pool_inst.supports_property("target_path"): pool_inst.target_path = target_path or "/some/target/path" if fmt and pool_inst.supports_property("format"): pool_inst.format = fmt if source_name and pool_inst.supports_property("source_name"): pool_inst.source_name = source_name if iqn and pool_inst.supports_property("iqn"): pool_inst.iqn = iqn return poolCompare(pool_inst)
def createPool(conn, ptype, poolname=None, fmt=None, target_path=None, source_path=None, source_name=None, uuid=None, iqn=None): if poolname is None: poolname = _findFreePoolName(conn, str(ptype) + "-pool") if uuid is None: uuid = generate_uuid_from_string(poolname) pool_inst = StoragePool(conn) pool_inst.name = poolname pool_inst.type = ptype pool_inst.uuid = uuid if pool_inst.supports_property("host"): pool_inst.host = "some.random.hostname" if pool_inst.supports_property("source_path"): pool_inst.source_path = source_path or "/some/source/path" if pool_inst.supports_property("target_path"): pool_inst.target_path = target_path or "/some/target/path" if fmt and pool_inst.supports_property("format"): pool_inst.format = fmt if source_name and pool_inst.supports_property("source_name"): pool_inst.source_name = source_name if iqn and pool_inst.supports_property("iqn"): pool_inst.iqn = iqn pool_inst.validate() return poolCompare(pool_inst)
def testEnumerateLogical(self): lst = StoragePool.pool_list_from_sources(self.conn, StoragePool.TYPE_LOGICAL) self.assertEqual(lst, ["testvg1", "testvg2"])
def createPool(conn, ptype, poolname=None, fmt=None, target_path=None, source_path=None, source_name=None, uuid=None, iqn=None): if poolname is None: poolname = _findFreePoolName(conn, str(ptype) + "-pool") if uuid is None: uuid = generate_uuid_from_string(poolname) pool_inst = StoragePool(conn) pool_inst.name = poolname pool_inst.type = ptype pool_inst.uuid = uuid if pool_inst.supports_property("hosts"): pool_inst.add_host("some.random.hostname") if pool_inst.supports_property("source_path"): pool_inst.source_path = source_path or "/some/source/path" if pool_inst.supports_property("target_path"): pool_inst.target_path = target_path or "/some/target/path" if fmt and pool_inst.supports_property("format"): pool_inst.format = fmt if source_name and pool_inst.supports_property("source_name"): pool_inst.source_name = source_name if iqn and pool_inst.supports_property("iqn"): pool_inst.iqn = iqn pool_inst.validate() return poolCompare(pool_inst)
def _make_stub_pool(self): pool = StoragePool(self.conn.get_backend()) pool.type = self.get_config_type() return pool
def testEnumerateiSCSI(self): host = "example.com" lst = StoragePool.pool_list_from_sources(self.conn, StoragePool.TYPE_ISCSI, host=host) self.assertTrue(len(lst) == 0)
def createPool(conn, ptype, poolname=None, fmt=None, target_path=None, source_path=None, source_name=None, iqn=None): if poolname is None: poolname = StoragePool.find_free_name(conn, "%s-pool" % ptype) pool_inst = StoragePool(conn) pool_inst.name = poolname pool_inst.type = ptype if pool_inst.supports_property("hosts"): hostobj = pool_inst.hosts.add_new() hostobj.name = "some.random.hostname" if pool_inst.supports_property("source_path"): pool_inst.source_path = source_path or "/some/source/path" if pool_inst.supports_property("target_path"): pool_inst.target_path = target_path or "/some/target/path" if fmt and pool_inst.supports_property("format"): pool_inst.format = fmt if source_name and pool_inst.supports_property("source_name"): pool_inst.source_name = source_name if iqn and pool_inst.supports_property("iqn"): pool_inst.iqn = iqn pool_inst.validate() return poolCompare(pool_inst)
and e.get_error_code() != libvirt.VIR_ERR_NO_STORAGE_VOL): raise return None, e def lookup_vol_name(name): try: name = os.path.basename(path) if pool and name in pool.listVolumes(): return pool.lookupByName(name) except: pass return None vol = lookup_vol_by_path()[0] if not vol: pool = StoragePool.lookup_pool_by_path(conn, os.path.dirname(path)) # Is pool running? if pool and pool.info()[0] != libvirt.VIR_STORAGE_POOL_RUNNING: pool = None # Attempt to lookup path as a storage volume if pool and not vol: try: # Pool may need to be refreshed, but if it errors, # invalidate it pool.refresh(0) vol, verr = lookup_vol_by_path() if verr: vol = lookup_vol_name(os.path.basename(path)) except Exception, e:
def createPool(conn, ptype, poolname=None, fmt=None, target_path=None, source_path=None, source_name=None, iqn=None): if poolname is None: poolname = StoragePool.find_free_name(conn, "%s-pool" % ptype) pool_inst = StoragePool(conn) pool_inst.name = poolname pool_inst.type = ptype if pool_inst.supports_hosts(): hostobj = pool_inst.hosts.add_new() hostobj.name = "some.random.hostname" if pool_inst.supports_source_path(): pool_inst.source_path = source_path or "/some/source/path" if pool_inst.supports_target_path(): pool_inst.target_path = (target_path or pool_inst.default_target_path()) if fmt and pool_inst.supports_format(): pool_inst.format = fmt if pool_inst.supports_source_name(): pool_inst.source_name = (source_name or pool_inst.default_source_name()) if iqn and pool_inst.supports_iqn(): pool_inst.iqn = iqn return poolCompare(pool_inst)
def _build_xmlobj_from_xmleditor(self): xml = self._xmleditor.get_xml() log.debug("Using XML from xmleditor:\n%s", xml) return StoragePool(self.conn.get_backend(), parsexml=xml)