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
This commit is contained in:
Cole Robinson 2019-04-14 19:16:10 -04:00
parent 202ac53831
commit 1cc2a0ae8b
4 changed files with 22 additions and 24 deletions

View File

@ -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)

View File

@ -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:

View File

@ -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")

View File

@ -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))