2023-09-02 00:52:28 +03:00
|
|
|
import logging
|
|
|
|
|
2023-08-27 23:42:56 +03:00
|
|
|
import libvirt
|
|
|
|
|
2023-09-02 00:52:28 +03:00
|
|
|
from ..exceptions import StoragePoolError
|
2023-08-31 20:37:41 +03:00
|
|
|
from .volume import Volume, VolumeInfo
|
|
|
|
|
2023-08-27 23:42:56 +03:00
|
|
|
|
2023-09-02 00:52:28 +03:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2023-08-27 23:42:56 +03:00
|
|
|
class StoragePool:
|
|
|
|
def __init__(self, pool: libvirt.virStoragePool):
|
|
|
|
self.pool = pool
|
|
|
|
|
2023-08-31 20:37:41 +03:00
|
|
|
@property
|
|
|
|
def name(self) -> str:
|
|
|
|
return self.pool.name()
|
|
|
|
|
|
|
|
def dump_xml(self) -> str:
|
|
|
|
return self.pool.XMLDesc()
|
|
|
|
|
|
|
|
def create(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def delete(self):
|
2023-08-27 23:42:56 +03:00
|
|
|
pass
|
2023-08-31 20:37:41 +03:00
|
|
|
|
|
|
|
def refresh(self) -> None:
|
|
|
|
self.pool.refresh()
|
|
|
|
|
2023-09-02 00:52:28 +03:00
|
|
|
def create_volume(self, vol_info: VolumeInfo) -> Volume:
|
|
|
|
"""
|
|
|
|
Create storage volume and return Volume instance.
|
|
|
|
"""
|
|
|
|
logger.info(f'Create storage volume vol={vol_info.name} '
|
|
|
|
f'in pool={self.pool}')
|
|
|
|
vol = self.pool.createXML(
|
2023-08-31 20:37:41 +03:00
|
|
|
vol_info.to_xml(),
|
|
|
|
flags=libvirt.VIR_STORAGE_VOL_CREATE_PREALLOC_METADATA)
|
|
|
|
return Volume(self.pool, vol)
|
|
|
|
|
2023-09-02 00:52:28 +03:00
|
|
|
def get_volume(self, name: str) -> Volume | None:
|
|
|
|
"""
|
|
|
|
Lookup and return Volume instance or None.
|
|
|
|
"""
|
|
|
|
logger.info(f'Lookup for storage volume vol={name} '
|
|
|
|
f'in pool={self.pool.name}')
|
|
|
|
try:
|
|
|
|
vol = self.pool.storageVolLookupByName(name)
|
|
|
|
return Volume(self.pool, vol)
|
|
|
|
except libvirt.libvirtError as err:
|
|
|
|
if (err.get_error_domain() == libvirt.VIR_FROM_STORAGE
|
|
|
|
err.get_error_code() == libvirt.VIR_ERR_NO_STORAGE_VOL):
|
|
|
|
logger.error(err.get_error_message())
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
logger.error(f'libvirt error: {err}')
|
|
|
|
raise StoragePoolError(f'libvirt error: {err}') from err
|
|
|
|
|
2023-08-31 20:37:41 +03:00
|
|
|
def list_volumes(self) -> list[Volume]:
|
|
|
|
return [Volume(self.pool, vol) for vol in self.pool.listAllVolumes()]
|