From 1cc2a0ae8bbcbed79d93613052330b34c6c698f6 Mon Sep 17 00:00:00 2001 From: Cole Robinson Date: Sun, 14 Apr 2019 19:16:10 -0400 Subject: [PATCH] storage: Add ensure_pool_is_running We pretty much require a referenced storage pool to be running if it's intended to be used as a virt-install or virt-manager requested disk. So add a helper to start a pool if needed and optionally refresh it --- virtinst/cli.py | 1 + virtinst/diskbackend.py | 23 +++-------------------- virtinst/kernelupload.py | 2 +- virtinst/storage.py | 20 +++++++++++++++++--- 4 files changed, 22 insertions(+), 24 deletions(-) diff --git a/virtinst/cli.py b/virtinst/cli.py index dbb9cfd5..5ce658f1 100644 --- a/virtinst/cli.py +++ b/virtinst/cli.py @@ -2346,6 +2346,7 @@ class ParserDisk(VirtCLIParser): if poolxml: poolname = poolxml.name poolobj = self.guest.conn.storagePoolLookupByName(poolname) + StoragePool.ensure_pool_is_running(poolobj) if volname: vol_object = poolobj.storageVolLookupByName(volname) diff --git a/virtinst/diskbackend.py b/virtinst/diskbackend.py index 3366e88d..986f0ab6 100644 --- a/virtinst/diskbackend.py +++ b/virtinst/diskbackend.py @@ -17,23 +17,6 @@ import libvirt from .storage import StoragePool, StorageVolume -def _lookup_pool_by_dirname(conn, path): - """ - Try to find the parent pool for the passed path. - If found, and the pool isn't running, attempt to start it up. - - return pool, or None if not found - """ - pool = StoragePool.lookup_pool_by_path(conn, os.path.dirname(path)) - if not pool: - return None - - # Ensure pool is running - if pool.info()[0] != libvirt.VIR_STORAGE_POOL_RUNNING: - pool.create(0) - return pool - - def _lookup_vol_by_path(conn, path): """ Try to find a volume matching the full passed path. Call info() on @@ -96,15 +79,15 @@ def _check_if_path_managed(conn, path): if vol: return vol, vol.storagePoolLookupByVolume() - pool = _lookup_pool_by_dirname(conn, path) + pool = StoragePool.lookup_pool_by_path(conn, os.path.dirname(path)) if not pool: return None, None # We have the parent pool, but didn't find a volume on first lookup # attempt. Refresh the pool and try again, in case we were just out - # of date. + # of date or the pool was inactive. try: - pool.refresh(0) + StoragePool.ensure_pool_is_running(pool, refresh=True) vol, verr = _lookup_vol_by_path(conn, path) if verr: try: diff --git a/virtinst/kernelupload.py b/virtinst/kernelupload.py index b332ed25..92621046 100644 --- a/virtinst/kernelupload.py +++ b/virtinst/kernelupload.py @@ -20,7 +20,7 @@ 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) + StoragePool.ensure_pool_is_running(pool, refresh=True) return pool name = StoragePool.find_free_name(conn, "boot-scratch") diff --git a/virtinst/storage.py b/virtinst/storage.py index b577c9a7..210e8573 100644 --- a/virtinst/storage.py +++ b/virtinst/storage.py @@ -263,6 +263,21 @@ class StoragePool(_StorageObject): kwargs["lib_collision"] = False return util.generate_name(basename, cb, **kwargs) + @staticmethod + def ensure_pool_is_running(pool_object, refresh=False): + """ + If the passed vmmStoragePool isn't running, start it. + + :param pool_object: vmmStoragePool to check/start + :param refresh: If True, run refresh() as well + """ + if pool_object.info()[0] != libvirt.VIR_STORAGE_POOL_RUNNING: + logging.debug("starting pool=%s", pool_object.name()) + pool_object.create(0) + if refresh: + logging.debug("refreshing pool=%s", pool_object.name()) + pool_object.refresh(0) + ###################### # Validation helpers # @@ -535,7 +550,7 @@ class StorageVolume(_StorageObject): Finds a name similar (or equal) to passed 'basename' that is not in use by another volume. Extra params are passed to generate_name """ - pool_object.refresh(0) + StoragePool.ensure_pool_is_running(pool_object, refresh=True) return util.generate_name(basename, pool_object.storageVolLookupByName, **kwargs) @@ -565,8 +580,7 @@ class StorageVolume(_StorageObject): def _get_pool(self): return self._pool def _set_pool(self, newpool): - if newpool.info()[0] != libvirt.VIR_STORAGE_POOL_RUNNING: - raise ValueError(_("pool '%s' must be active.") % newpool.name()) + StoragePool.ensure_pool_is_running(newpool) self._pool = newpool self._pool_xml = StoragePool(self.conn, parsexml=self._pool.XMLDesc(0))