upd
This commit is contained in:
@ -0,0 +1,2 @@
|
||||
from .storage_pool import StoragePool
|
||||
from .volume import Volume, VolumeInfo
|
||||
|
@ -1,9 +1,37 @@
|
||||
import libvirt
|
||||
|
||||
from .volume import Volume, VolumeInfo
|
||||
|
||||
|
||||
class StoragePool:
|
||||
def __init__(self, pool: libvirt.virStoragePool):
|
||||
self.pool = pool
|
||||
|
||||
def create_volume(self):
|
||||
@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):
|
||||
pass
|
||||
|
||||
def refresh(self) -> None:
|
||||
self.pool.refresh()
|
||||
|
||||
def create_volume(self, vol_info: VolumeInfo) -> None:
|
||||
# todo: return Volume object?
|
||||
self.pool.createXML(
|
||||
vol_info.to_xml(),
|
||||
flags=libvirt.VIR_STORAGE_VOL_CREATE_PREALLOC_METADATA)
|
||||
|
||||
def get_volume(self, name: str) -> Volume:
|
||||
vol = self.pool.storageVolLookupByName(name)
|
||||
return Volume(self.pool, vol)
|
||||
|
||||
def list_volumes(self) -> list[Volume]:
|
||||
return [Volume(self.pool, vol) for vol in self.pool.listAllVolumes()]
|
||||
|
@ -1,23 +1,64 @@
|
||||
from dataclasses import dataclass
|
||||
from time import time
|
||||
|
||||
import libvirt
|
||||
from lxml.builder import E
|
||||
from lxml.etree import tostring
|
||||
|
||||
|
||||
@dataclass
|
||||
class VolumeInfo:
|
||||
"""
|
||||
Volume info schema
|
||||
{'type': 'local', 'system': True, 'size': 102400, 'mode': 'rw'}
|
||||
"""
|
||||
pass
|
||||
name: str
|
||||
path: str
|
||||
capacity: int
|
||||
|
||||
def to_xml(self) -> str:
|
||||
unixtime = str(int(time()))
|
||||
xml = E.volume(type='file')
|
||||
xml.append(E.name(self.name))
|
||||
xml.append(E.key(self.path))
|
||||
xml.append(E.source())
|
||||
xml.append(E.capacity(str(self.capacity * 1024 * 1024), unit='bytes'))
|
||||
xml.append(E.allocation('0'))
|
||||
xml.append(E.target(
|
||||
E.path(self.path),
|
||||
E.format(type='qcow2'),
|
||||
E.timestamps(
|
||||
E.atime(unixtime),
|
||||
E.mtime(unixtime),
|
||||
E.ctime(unixtime)),
|
||||
E.compat('1.1'),
|
||||
E.features(E.lazy_refcounts())
|
||||
))
|
||||
return tostring(xml, encoding='unicode', pretty_print=True)
|
||||
|
||||
|
||||
class Volume:
|
||||
def __init__(self, pool: libvirt.virStorageVol):
|
||||
def __init__(self, pool: libvirt.virStoragePool,
|
||||
vol: libvirt.virStorageVol):
|
||||
self.pool = pool
|
||||
self.vol = vol
|
||||
|
||||
def lookup_by_path(self):
|
||||
pass
|
||||
@property
|
||||
def name(self) -> str:
|
||||
return self.vol.name()
|
||||
|
||||
def generate_xml(self):
|
||||
pass
|
||||
@property
|
||||
def path(self) -> str:
|
||||
return self.vol.path()
|
||||
|
||||
def create(self):
|
||||
pass
|
||||
def dump_xml(self) -> str:
|
||||
return self.vol.XMLDesc()
|
||||
|
||||
def clone(self, vol_info: VolumeInfo) -> None:
|
||||
self.pool.createXMLFrom(
|
||||
vol_info.to_xml(),
|
||||
self.vol,
|
||||
flags=libvirt.VIR_STORAGE_VOL_CREATE_PREALLOC_METADATA)
|
||||
|
||||
def resize(self, capacity: int):
|
||||
"""Resize volume to `capacity`. Unit is mebibyte."""
|
||||
self.vol.resize(capacity * 1024 * 1024)
|
||||
|
||||
def delete(self) -> None:
|
||||
self.vol.delete()
|
||||
|
Reference in New Issue
Block a user