storage_lvm.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #
  2. # The Qubes OS Project, http://www.qubes-os.org
  3. #
  4. # Copyright (C) 2016 Bahtiar `kalkin-` Gadimov <bahtiar@gadimov.de>
  5. #
  6. # This library is free software; you can redistribute it and/or
  7. # modify it under the terms of the GNU Lesser General Public
  8. # License as published by the Free Software Foundation; either
  9. # version 2.1 of the License, or (at your option) any later version.
  10. #
  11. # This library is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. # Lesser General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Lesser General Public
  17. # License along with this library; if not, see <https://www.gnu.org/licenses/>.
  18. #
  19. ''' Tests for lvm storage driver. By default tests are going to use the
  20. 'qubes_dom0/pool00'. An alternative LVM thin pool may be provided via
  21. :envvar:`DEFAULT_LVM_POOL` shell variable.
  22. Any pool variables prefixed with 'LVM_' or 'lvm_' represent a LVM
  23. 'volume_group/thin_pool' combination. Pool variables without a prefix
  24. represent a :py:class:`qubes.storage.lvm.ThinPool`.
  25. '''
  26. import os
  27. import unittest
  28. import qubes.tests
  29. from qubes.storage.lvm import ThinPool, ThinVolume
  30. if 'DEFAULT_LVM_POOL' in os.environ.keys():
  31. DEFAULT_LVM_POOL = os.environ['DEFAULT_LVM_POOL']
  32. else:
  33. DEFAULT_LVM_POOL = 'qubes_dom0/pool00'
  34. def lvm_pool_exists(volume_group, thin_pool):
  35. ''' Returns ``True`` if thin pool exists in the volume group. '''
  36. path = "/dev/mapper/{!s}-{!s}".format(volume_group, thin_pool)
  37. return os.path.exists(path)
  38. def skipUnlessLvmPoolExists(test_item): # pylint: disable=invalid-name
  39. ''' Decorator that skips LVM tests if the default pool is missing. '''
  40. volume_group, thin_pool = DEFAULT_LVM_POOL.split('/', 1)
  41. result = lvm_pool_exists(volume_group, thin_pool)
  42. msg = 'LVM thin pool {!r} does not exist'.format(DEFAULT_LVM_POOL)
  43. return unittest.skipUnless(result, msg)(test_item)
  44. POOL_CONF = {'name': 'test-lvm',
  45. 'driver': 'lvm_thin',
  46. 'volume_group': DEFAULT_LVM_POOL.split('/')[0],
  47. 'thin_pool': DEFAULT_LVM_POOL.split('/')[1]}
  48. class ThinPoolBase(qubes.tests.QubesTestCase):
  49. ''' Sanity tests for :py:class:`qubes.storage.lvm.ThinPool` '''
  50. created_pool = False
  51. def setUp(self):
  52. super(ThinPoolBase, self).setUp()
  53. volume_group, thin_pool = DEFAULT_LVM_POOL.split('/', 1)
  54. self.pool = self._find_pool(volume_group, thin_pool)
  55. if not self.pool:
  56. self.pool = self.app.add_pool(**POOL_CONF)
  57. self.created_pool = True
  58. def tearDown(self):
  59. ''' Remove the default lvm pool if it was created only for this test '''
  60. if self.created_pool:
  61. self.app.remove_pool(self.pool.name)
  62. super(ThinPoolBase, self).tearDown()
  63. def _find_pool(self, volume_group, thin_pool):
  64. ''' Returns the pool matching the specified ``volume_group`` &
  65. ``thin_pool``, or None.
  66. '''
  67. pools = [p for p in self.app.pools
  68. if issubclass(p.__class__, ThinPool)]
  69. for pool in pools:
  70. if pool.volume_group == volume_group \
  71. and pool.thin_pool == thin_pool:
  72. return pool
  73. return None
  74. @skipUnlessLvmPoolExists
  75. class TC_00_ThinPool(ThinPoolBase):
  76. ''' Sanity tests for :py:class:`qubes.storage.lvm.ThinPool` '''
  77. def setUp(self):
  78. xml_path = '/tmp/qubes-test.xml'
  79. self.app = qubes.Qubes.create_empty_store(store=xml_path,
  80. clockvm=None,
  81. updatevm=None,
  82. offline_mode=True,
  83. )
  84. os.environ['QUBES_XML_PATH'] = xml_path
  85. super(TC_00_ThinPool, self).setUp()
  86. def tearDown(self):
  87. super(TC_00_ThinPool, self).tearDown()
  88. os.unlink(self.app.store)
  89. del self.app
  90. for attr in dir(self):
  91. if isinstance(getattr(self, attr), qubes.vm.BaseVM):
  92. delattr(self, attr)
  93. def test_000_default_thin_pool(self):
  94. ''' Check whether :py:data`DEFAULT_LVM_POOL` exists. This pool is
  95. created by default, if at installation time LVM + Thin was chosen.
  96. '''
  97. msg = 'Thin pool {!r} does not exist'.format(DEFAULT_LVM_POOL)
  98. self.assertTrue(self.pool, msg)
  99. def test_001_origin_volume(self):
  100. ''' Test origin volume creation '''
  101. config = {
  102. 'name': 'root',
  103. 'pool': self.pool.name,
  104. 'save_on_stop': True,
  105. 'rw': True,
  106. 'size': qubes.config.defaults['root_img_size'],
  107. }
  108. vm = qubes.tests.storage.TestVM(self)
  109. volume = self.app.get_pool(self.pool.name).init_volume(vm, config)
  110. self.assertIsInstance(volume, ThinVolume)
  111. self.assertEqual(volume.name, 'root')
  112. self.assertEqual(volume.pool, self.pool.name)
  113. self.assertEqual(volume.size, qubes.config.defaults['root_img_size'])
  114. volume.create()
  115. path = "/dev/%s" % volume.vid
  116. self.assertTrue(os.path.exists(path))
  117. volume.remove()
  118. def test_003_read_write_volume(self):
  119. ''' Test read-write volume creation '''
  120. config = {
  121. 'name': 'root',
  122. 'pool': self.pool.name,
  123. 'rw': True,
  124. 'save_on_stop': True,
  125. 'size': qubes.config.defaults['root_img_size'],
  126. }
  127. vm = qubes.tests.storage.TestVM(self)
  128. volume = self.app.get_pool(self.pool.name).init_volume(vm, config)
  129. self.assertIsInstance(volume, ThinVolume)
  130. self.assertEqual(volume.name, 'root')
  131. self.assertEqual(volume.pool, self.pool.name)
  132. self.assertEqual(volume.size, qubes.config.defaults['root_img_size'])
  133. volume.create()
  134. path = "/dev/%s" % volume.vid
  135. self.assertTrue(os.path.exists(path))
  136. volume.remove()
  137. @skipUnlessLvmPoolExists
  138. class TC_01_ThinPool(ThinPoolBase, qubes.tests.SystemTestCase):
  139. ''' Sanity tests for :py:class:`qubes.storage.lvm.ThinPool` '''
  140. def setUp(self):
  141. super(TC_01_ThinPool, self).setUp()
  142. self.init_default_template()
  143. def test_004_import(self):
  144. template_vm = self.app.default_template
  145. name = self.make_vm_name('import')
  146. vm = self.app.add_new_vm(qubes.vm.templatevm.TemplateVM, name=name,
  147. label='red')
  148. vm.clone_properties(template_vm)
  149. vm.clone_disk_files(template_vm, pool='test-lvm')
  150. for v_name, volume in vm.volumes.items():
  151. if volume.save_on_stop:
  152. expected = "/dev/{!s}/vm-{!s}-{!s}".format(
  153. DEFAULT_LVM_POOL.split('/')[0], vm.name, v_name)
  154. self.assertEqual(volume.path, expected)
  155. with self.assertNotRaises(qubes.exc.QubesException):
  156. vm.start()
  157. def test_005_create_appvm(self):
  158. vm = self.app.add_new_vm(cls=qubes.vm.appvm.AppVM,
  159. name=self.make_vm_name('appvm'), label='red')
  160. vm.create_on_disk(pool='test-lvm')
  161. for v_name, volume in vm.volumes.items():
  162. if volume.save_on_stop:
  163. expected = "/dev/{!s}/vm-{!s}-{!s}".format(
  164. DEFAULT_LVM_POOL.split('/')[0], vm.name, v_name)
  165. self.assertEqual(volume.path, expected)
  166. with self.assertNotRaises(qubes.exc.QubesException):
  167. vm.start()