From 930fe417a8885e42a17a1d0f2c05807793abc265 Mon Sep 17 00:00:00 2001 From: Bahtiar `kalkin-` Gadimov Date: Thu, 16 Jun 2016 11:15:38 +0200 Subject: [PATCH] Remove storage size and usage methods from QubesVM --- qubes/storage/__init__.py | 10 ------ qubes/tests/vm/adminvm.py | 17 +++++---- qubes/tools/qvm_ls.py | 52 +++++++++++++++++---------- qubes/vm/adminvm.py | 22 ++---------- qubes/vm/qubesvm.py | 76 --------------------------------------- 5 files changed, 44 insertions(+), 133 deletions(-) diff --git a/qubes/storage/__init__.py b/qubes/storage/__init__.py index 873bd21f..28db653c 100644 --- a/qubes/storage/__init__.py +++ b/qubes/storage/__init__.py @@ -129,16 +129,6 @@ class Storage(object): result += volume.usage return result - # TODO Remove this wrapper - def get_disk_utilization_private_img(self): - # pylint: disable=invalid-name,missing-docstring - return self.vm.volume['private'].usage - - # TODO Remove this wrapper - def get_private_img_sz(self): - # :pylint: disable=missing-docstring - return self.vm.volume['private'].size - def resize(self, volume, size): ''' Resize volume ''' self.get_pool(volume).resize(volume, size) diff --git a/qubes/tests/vm/adminvm.py b/qubes/tests/vm/adminvm.py index 9e09979c..f1ee1ced 100644 --- a/qubes/tests/vm/adminvm.py +++ b/qubes/tests/vm/adminvm.py @@ -38,7 +38,7 @@ class TC_00_AdminVM(qubes.tests.QubesTestCase): self.app = qubes.tests.vm.TestApp() self.vm = qubes.vm.adminvm.AdminVM(self.app, xml=None, qid=0, name='dom0') - except: # pylint: disable=bare-except + except: # pylint: disable=bare-except if self.id().endswith('.test_000_init'): raise self.skipTest('setup failed') @@ -69,17 +69,16 @@ class TC_00_AdminVM(qubes.tests.QubesTestCase): self.assertGreater(self.vm.get_mem_static_max(), 0) def test_304_get_disk_utilization(self): - self.assertEqual(self.vm.get_disk_utilization(), 0) + self.assertRaises(self.vm.storage.get_disk_utilization(), 0) - def test_305_get_disk_utilization_private_img(self): - # pylint: disable=invalid-name - self.assertEqual(self.vm.get_disk_utilization_private_img(), 0) + def test_305_has_no_private_volume(self): + self.assertEqual(KeyError, self.vm.volumes['private']) - def test_306_get_private_img_sz(self): - self.assertEqual(self.vm.get_private_img_sz(), 0) + def test_306_has_no_root_volume(self): + self.assertEqual(KeyError, self.vm.volumes['root']) - def test_307_verify_files(self): - self.assertEqual(self.vm.get_private_img_sz(), 0) + def test_307_has_no_volatile_volume(self): + self.assertEqual(KeyError, self.vm.volumes['volatile']) def test_310_start(self): with self.assertRaises(qubes.exc.QubesException): diff --git a/qubes/tools/qvm_ls.py b/qubes/tools/qvm_ls.py index f98cd7f1..24ec5d5d 100644 --- a/qubes/tools/qvm_ls.py +++ b/qubes/tools/qvm_ls.py @@ -31,7 +31,6 @@ from __future__ import print_function import __builtin__ import argparse import collections -import math import sys import textwrap @@ -122,8 +121,10 @@ class Column(object): if isinstance(ret, (qubes.vm.BaseVM, qubes.Label)): return ret.name - return ret + if isinstance(ret, int): + return str(ret) + return ret def __repr__(self): return '{}(head={!r}, width={!r})'.format(self.__class__.__name__, @@ -389,6 +390,29 @@ class StatusColumn(Column): return ''.join((flag(self, vm) or '-') for flag in self.get_flags()) +def calc_size(vm, volume_name): + ''' Calculates the volume size in MB ''' + try: + return vm.volumes[volume_name].size // 1024 // 1024 + except KeyError: + return 0 + +def calc_usage(vm, volume_name): + ''' Calculates the volume usage in MB ''' + try: + return vm.volumes[volume_name].usage // 1024 // 1024 + except KeyError: + return 0 + +def calc_used(vm, volume_name): + ''' Calculates the volume usage in percent ''' + size = calc_size(vm, volume_name) + if size == 0: + return 0 + usage = calc_usage(vm, volume_name) + return usage * 100 // size + + # todo maxmem Column('GATEWAY', width=15, @@ -396,45 +420,37 @@ Column('GATEWAY', width=15, doc='Network gateway.') Column('MEMORY', width=5, - attr=(lambda vm: vm.get_mem()/1024 if vm.is_running() else None), + attr=(lambda vm: vm.get_mem() / 1024 if vm.is_running() else None), doc='Memory currently used by VM') Column('DISK', width=5, - attr=(lambda vm: vm.get_disk_utilization()/1024/1024), + attr=(lambda vm: vm.storage.get_disk_utilization() / 1024 / 1024), doc='Total disk utilisation.') Column('PRIV-CURR', width=5, - attr=(lambda vm: - int(math.floor(vm.get_disk_utilization_private_img()/1024/1024))), + attr=(lambda vm: calc_usage(vm, 'private')), doc='Disk utilisation by private image (/home, /usr/local).') Column('PRIV-MAX', width=5, - attr=(lambda vm: - int(math.floor(vm.get_private_img_sz()/1024/1024))), + attr=(lambda vm: calc_size(vm, 'private')), doc='Maximum available space for private image.') Column('PRIV-USED', width=5, - attr=(lambda vm: - int(math.floor(vm.get_disk_utilization_private_img() * 100 - / vm.get_private_img_sz()))), + attr=(lambda vm: calc_used(vm, 'private')), doc='Disk utilisation by private image as a percentage of available space.') Column('ROOT-CURR', width=5, - attr=(lambda vm: - int(math.floor(vm.get_disk_utilization_private_img()/1024/1024))), + attr=(lambda vm: calc_usage(vm, 'root')), doc='Disk utilisation by root image (/usr, /lib, /etc, ...).') Column('ROOT-MAX', width=5, - attr=(lambda vm: - int(math.floor(vm.get_private_img_sz()/1024/1024))), + attr=(lambda vm: calc_size(vm, 'root')), doc='Maximum available space for root image.') Column('ROOT-USED', width=5, - attr=(lambda vm: - int(math.floor(vm.get_disk_utilization_private_img() * 100 - / vm.get_private_img_sz()))), + attr=(lambda vm: calc_used(vm, 'root')), doc='Disk utilisation by root image as a percentage of available space.') diff --git a/qubes/vm/adminvm.py b/qubes/vm/adminvm.py index a429df2c..632c1dbd 100644 --- a/qubes/vm/adminvm.py +++ b/qubes/vm/adminvm.py @@ -95,7 +95,7 @@ class AdminVM(qubes.vm.qubesvm.QubesVM): :py:meth:`qubes.vm.qubesvm.QubesVM.get_mem` ''' - #return psutil.virtual_memory().total/1024 + # return psutil.virtual_memory().total/1024 for line in open('/proc/meminfo'): if line.startswith('MemTotal:'): return int(line.split(':')[1].strip().split()[0]) @@ -110,23 +110,6 @@ class AdminVM(qubes.vm.qubesvm.QubesVM): ''' return self.app.vmm.libvirt_conn.getInfo()[1] - - def get_disk_utilization(self): - '''Always ``0``. - - .. seealso: - :py:meth:`qubes.vm.qubesvm.QubesVM.get_disk_utilization` - ''' - return 0 - - def get_disk_utilization_private_img(self): - '''Always ``0``. - - .. seealso: - :py:meth:`qubes.vm.qubesvm.QubesVM.get_disk_utilization_private_img` - ''' - return 0 - def get_private_img_sz(self): '''Always ``0``. @@ -135,7 +118,6 @@ class AdminVM(qubes.vm.qubesvm.QubesVM): ''' return 0 - def verify_files(self): '''Always :py:obj:`True` @@ -150,7 +132,7 @@ class AdminVM(qubes.vm.qubesvm.QubesVM): .. seealso: :py:meth:`qubes.vm.qubesvm.QubesVM.start` - ''' # pylint: disable=unused-argument + ''' # pylint: disable=unused-argument raise qubes.exc.QubesVMError(self, 'Cannot start Dom0 fake domain!') diff --git a/qubes/vm/qubesvm.py b/qubes/vm/qubesvm.py index b6db2356..48fe3158 100644 --- a/qubes/vm/qubesvm.py +++ b/qubes/vm/qubesvm.py @@ -1547,82 +1547,6 @@ class QubesVM(qubes.vm.mix.net.NetVMMixin, qubes.vm.BaseVM): 'libvirt error code: {!r}'.format(e.get_error_code())) raise - - - # XXX shouldn't this go only to vms that have root image? - def get_disk_utilization_root_img(self): - '''Get space that is actually ocuppied by :py:attr:`volumes['root']`. - - This is a temporary wrapper for backwards compatibility. You should - call directly :py:attr:`volumes[name].utilization` - :returns: domain's real disk image size [FIXME unit] - :rtype: FIXME - - .. seealso:: :py:meth:`get_root_img_sz` - ''' - - warnings.warn("get_disk_utilization_root_img is deprecated," - " use volumes['root'].utilization", DeprecationWarning) - return qubes.storage.file.get_disk_usage( - self.volumes['root'].utilization) - - - # XXX shouldn't this go only to vms that have root image? - def get_root_img_sz(self): - '''Get the size of the :py:attr:`volumes['root']`. - - This is a temporary wrapper for backwards compatibility. You should - call directly :py:attr:`volumes[name].size` - :returns: domain's virtual disk size [FIXME unit] - :rtype: FIXME - - .. seealso:: :py:meth:`get_disk_utilization_root_img` - ''' - - warnings.warn( - "get_disk_root_img_sz is deprecated, use volumes['root'].size", - DeprecationWarning) - return qubes.storage.file.get_disk_usage(self.volumes['root'].size) - - def get_disk_utilization_private_img(self): - '''Get space that is actually ocuppied by :py:attr:`volumes['private']`. - - This is a temporary wrapper for backwards compatibility. You should - call directly :py:attr:`volumes[name].utilization` - :returns: domain's real disk image size [FIXME unit] - :rtype: FIXME - ''' # pylint: disable=invalid-name - - warnings.warn("get_disk_utilization_private_img is deprecated," - " use volumes['private'].utilization", DeprecationWarning) - return qubes.storage.file.get_disk_usage(self.volumes[ - 'private'].utilization) - - def get_private_img_sz(self): - '''Get the size of the :py:attr:`volumes['private']`. - - This is a temporary wrapper for backwards compatibility. You should - call directly :py:attr:`volumes[name].size` - :returns: domain's virtual disk size [FIXME unit] - :rtype: FIXME - - .. seealso:: :py:meth:`get_disk_utilization_private_img` - ''' - - warnings.warn("get_disk_private_img_sz is deprecated," - " use volumes['private'].size", DeprecationWarning) - return qubes.storage.file.get_disk_usage(self.volumes['private'].size) - - def get_disk_utilization(self): - '''Return total space actually occuppied by all files belonging to \ - this domain. - - :returns: domain's total disk usage [FIXME unit] - :rtype: FIXME - ''' - - return qubes.storage.file.get_disk_usage(self.dir_path) - # TODO move to storage def verify_files(self): '''Verify that files accessed by this machine are sane.