[Kimchi-devel] [PATCH 3/4] Add volume ref_cnt: Add model and mockmodel implementation

Sheldon shaohef at linux.vnet.ibm.com
Mon Feb 17 12:13:20 UTC 2014


On 02/17/2014 04:38 PM, lvroyce at linux.vnet.ibm.com wrote:
> From: Royce Lv <lvroyce at linux.vnet.ibm.com>
>
> Set ref_cnt to 0 when new volume created,
> report it when lookup storage volume, for volume outside kimchi scope,
For me, I care when ref_cnt decrease and increase?
It is useful for disable network/storagpool used by guest/template.

> set ref_cnt to be -1.
>
> Signed-off-by: Royce Lv <lvroyce at linux.vnet.ibm.com>
> ---
>   src/kimchi/mockmodel.py            |  3 +++
>   src/kimchi/model/storagevolumes.py | 21 +++++++++++++++++++++
>   2 files changed, 24 insertions(+)
>
> diff --git a/src/kimchi/mockmodel.py b/src/kimchi/mockmodel.py
> index bde6a5c..1faf2a5 100644
> --- a/src/kimchi/mockmodel.py
> +++ b/src/kimchi/mockmodel.py
> @@ -386,6 +386,7 @@ class MockModel(object):
>               name = params['name']
>               volume = MockStorageVolume(pool, name, params)
>               volume.info['type'] = params['type']
> +            volume.info['ref_cnt'] = params.get('ref_cnt', 0)
>               volume.info['format'] = params['format']
>               volume.info['path'] = os.path.join(
>                   pool.info['path'], name)
> @@ -747,6 +748,7 @@ class MockVMTemplate(VMTemplate):
>           disk_paths = []
>           for vol_info in volumes:
>               vol_info['capacity'] = vol_info['capacity'] << 10
> +            vol_info['ref_cnt'] = -1
>               self.model.storagevolumes_create(pool.name, vol_info)
>               disk_paths.append({'pool': pool.name, 'volume': vol_info['name']})
>           return disk_paths
> @@ -850,6 +852,7 @@ class MockStorageVolume(object):
>                        'capacity': capacity << 20,
>                        'allocation': params.get('allocation','512'),
>                        'path': params.get('path'),
> +                     'ref_cnt': params.get('ref_cnt'),
>                        'format': fmt}
>           if fmt == 'iso':
>               self.info['allocation'] = self.info['capacity']
> diff --git a/src/kimchi/model/storagevolumes.py b/src/kimchi/model/storagevolumes.py
> index e3f00ca..3dc89a9 100644
> --- a/src/kimchi/model/storagevolumes.py
> +++ b/src/kimchi/model/storagevolumes.py
> @@ -40,6 +40,7 @@ VOLUME_TYPE_MAP = {0: 'file',
>   class StorageVolumesModel(object):
>       def __init__(self, **kargs):
>           self.conn = kargs['conn']
> +        self.objstore = kargs['objstore']
>
>       def create(self, pool, params):
>           vol_xml = """
> @@ -58,6 +59,7 @@ class StorageVolumesModel(object):
>           params.setdefault('format', 'qcow2')
>
>           name = params['name']
> +        vol_id = '%s:%s' % (pool, name)
>           try:
>               pool = StoragePoolModel.get_storagepool(pool, self.conn)
>               xml = vol_xml % params
> @@ -71,6 +73,10 @@ class StorageVolumesModel(object):
>               raise OperationFailed("KCHVOL0007E",
>                                     {'name': name, 'pool': pool,
>                                      'err': e.get_error_message()})
> +
> +        with self.objstore as session:
> +            session.store('storagevolume', vol_id, {'ref_cnt': 0})
> +
>           return name
>
>       def get_list(self, pool_name):
> @@ -89,6 +95,7 @@ class StorageVolumesModel(object):
>   class StorageVolumeModel(object):
>       def __init__(self, **kargs):
>           self.conn = kargs['conn']
> +        self.objstore = kargs['objstore']
>
>       def _get_storagevolume(self, pool, name):
>           pool = StoragePoolModel.get_storagepool(pool, self.conn)
> @@ -103,16 +110,30 @@ class StorageVolumeModel(object):
>               else:
>                   raise
>
> +    def _get_ref_cnt(self, pool, name):
> +        vol_id = '%s:%s' % (pool, name)
> +        with self.objstore as session:
> +            try:
> +                extra_info = session.get('storagevolume', vol_id)
> +            except NotFoundError:
> +                extra_info = {}
> +
> +        # -1 for storage volume created beyond kimchi scope
> +        ref_cnt = extra_info.get('ref_cnt', -1)
> +        return ref_cnt
> +
>       def lookup(self, pool, name):
>           vol = self._get_storagevolume(pool, name)
>           path = vol.path()
>           info = vol.info()
>           xml = vol.XMLDesc(0)
>           fmt = xmlutils.xpath_get_text(xml, "/volume/target/format/@type")[0]
> +
>           res = dict(type=VOLUME_TYPE_MAP[info[0]],
>                      capacity=info[1],
>                      allocation=info[2],
>                      path=path,
> +                   ref_cnt=self._get_ref_cnt(pool, name),
>                      format=fmt)
>           if fmt == 'iso':
>               if os.path.islink(path):


-- 
Thanks and best regards!

Sheldon Feng(冯少合)<shaohef at linux.vnet.ibm.com>
IBM Linux Technology Center




More information about the Kimchi-devel mailing list