storage.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. #
  2. # The Qubes OS Project, http://www.qubes-os.org
  3. #
  4. # Copyright (C) 2016 Marek Marczykowski-Górecki
  5. # <marmarek@invisiblethingslab.com>
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License along
  18. # with this program; if not, write to the Free Software Foundation, Inc.,
  19. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. #
  21. import asyncio
  22. import os
  23. import shutil
  24. import qubes.storage.lvm
  25. import qubes.tests
  26. import qubes.tests.storage_lvm
  27. import qubes.vm.appvm
  28. class StorageTestMixin(object):
  29. def setUp(self):
  30. super(StorageTestMixin, self).setUp()
  31. self.init_default_template()
  32. self.vm1 = self.app.add_new_vm(qubes.vm.appvm.AppVM,
  33. name=self.make_vm_name('vm1'),
  34. label='red')
  35. self.vm1.create_on_disk()
  36. self.vm2 = self.app.add_new_vm(qubes.vm.appvm.AppVM,
  37. name=self.make_vm_name('vm2'),
  38. label='red')
  39. self.vm2.create_on_disk()
  40. self.pool = None
  41. self.init_pool()
  42. self.app.save()
  43. def init_pool(self):
  44. ''' Initialize storage pool to be tested, store it in self.pool'''
  45. raise NotImplementedError
  46. def test_000_volatile(self):
  47. '''Test if volatile volume is really volatile'''
  48. return self.loop.run_until_complete(self._test_000_volatile())
  49. @asyncio.coroutine
  50. def _test_000_volatile(self):
  51. size = 32*1024*1024
  52. volume_config = {
  53. 'pool': self.pool.name,
  54. 'size': size,
  55. 'save_on_stop': False,
  56. 'rw': True,
  57. }
  58. testvol = self.vm1.storage.init_volume('testvol', volume_config)
  59. yield from self.vm1.storage.get_pool(testvol).create(testvol)
  60. self.app.save()
  61. yield from (self.vm1.start())
  62. # volatile image not clean
  63. yield from (self.vm1.run_for_stdio(
  64. 'head -c {} /dev/zero 2>&1 | diff -q /dev/xvde - 2>&1'.format(size),
  65. user='root'))
  66. # volatile image not volatile
  67. yield from (
  68. self.vm1.run_for_stdio('echo test123 > /dev/xvde', user='root'))
  69. yield from (self.vm1.shutdown(wait=True))
  70. yield from (self.vm1.start())
  71. yield from (self.vm1.run_for_stdio(
  72. 'head -c {} /dev/zero 2>&1 | diff -q /dev/xvde - 2>&1'.format(size),
  73. user='root'))
  74. def test_001_non_volatile(self):
  75. '''Test if non-volatile volume is really non-volatile'''
  76. return self.loop.run_until_complete(self._test_001_non_volatile())
  77. @asyncio.coroutine
  78. def _test_001_non_volatile(self):
  79. size = 32*1024*1024
  80. volume_config = {
  81. 'pool': self.pool.name,
  82. 'size': size,
  83. 'save_on_stop': True,
  84. 'rw': True,
  85. }
  86. testvol = yield from self.vm1.storage.init_volume(
  87. 'testvol', volume_config)
  88. yield from self.vm1.storage.get_pool(testvol).create(testvol)
  89. self.app.save()
  90. yield from self.vm1.start()
  91. # non-volatile image not clean
  92. yield from self.vm1.run_for_stdio(
  93. 'head -c {} /dev/zero 2>&1 | diff -q /dev/xvde - 2>&1'.format(size),
  94. user='root')
  95. yield from self.vm1.run_for_stdio('echo test123 > /dev/xvde',
  96. user='root')
  97. yield from self.vm1.shutdown(wait=True)
  98. yield from self.vm1.start()
  99. # non-volatile image volatile
  100. with self.assertRaises(subprocess.CalledProcessError):
  101. yield from self.vm1.run_for_stdio(
  102. 'head -c {} /dev/zero 2>&1 | diff -q /dev/xvde - 2>&1'.format(
  103. size),
  104. user='root')
  105. def test_002_read_only(self):
  106. '''Test read-only volume'''
  107. self.loop.run_until_complete(self._test_002_read_only())
  108. @asyncio.coroutine
  109. def _test_002_read_only(self):
  110. size = 32 * 1024 * 1024
  111. volume_config = {
  112. 'pool': self.pool.name,
  113. 'size': size,
  114. 'save_on_stop': False,
  115. 'rw': False,
  116. }
  117. testvol = self.vm1.storage.init_volume('testvol', volume_config)
  118. yield from self.vm1.storage.get_pool(testvol).create(testvol)
  119. self.app.save()
  120. yield from self.vm1.start()
  121. # non-volatile image not clean
  122. yield from self.vm1.run_for_stdio(
  123. 'head -c {} /dev/zero 2>&1 | diff -q /dev/xvde - 2>&1'.format(size),
  124. user='root')
  125. # Write to read-only volume unexpectedly succeeded
  126. with self.assertRaises(subprocess.CalledProcessError):
  127. yield from self.vm1.run_for_stdio('echo test123 > /dev/xvde',
  128. user='root')
  129. # read-only volume modified
  130. yield from self.vm1.run_for_stdio(
  131. 'head -c {} /dev/zero 2>&1 | diff -q /dev/xvde - 2>&1'.format(size),
  132. user='root')
  133. def test_003_snapshot(self):
  134. '''Test snapshot volume data propagation'''
  135. self.loop.run_until_complete(self._test_003_snapshot())
  136. @asyncio.coroutine
  137. def _test_003_snapshot(self):
  138. size = 128 * 1024 * 1024
  139. volume_config = {
  140. 'pool': self.pool.name,
  141. 'size': size,
  142. 'save_on_stop': True,
  143. 'rw': True,
  144. }
  145. testvol = self.vm1.storage.init_volume('testvol', volume_config)
  146. yield from self.vm1.storage.get_pool(testvol).create(testvol)
  147. volume_config = {
  148. 'pool': self.pool.name,
  149. 'size': size,
  150. 'snap_on_start': True,
  151. 'source': testvol.vid,
  152. 'rw': True,
  153. }
  154. testvol_snap = self.vm2.storage.init_volume('testvol', volume_config)
  155. yield from self.vm2.storage.get_pool(testvol_snap).create(testvol_snap)
  156. self.app.save()
  157. yield from self.vm1.start()
  158. yield from self.vm2.start()
  159. # origin image not clean
  160. yield from self.vm1.run_for_stdio(
  161. 'head -c {} /dev/zero 2>&1 | diff -q /dev/xvde - 2>&1'.format(size),
  162. user='root')
  163. # snapshot image not clean
  164. yield from self.vm2.run_for_stdio(
  165. 'head -c {} /dev/zero | diff -q /dev/xvde -'.format(size),
  166. user='root')
  167. # Write to read-write volume failed
  168. yield from self.vm1.run_for_stdio('echo test123 > /dev/xvde && sync',
  169. user='root')
  170. # origin changes propagated to snapshot too early
  171. yield from self.vm2.run_for_stdio(
  172. 'head -c {} /dev/zero 2>&1 | diff -q /dev/xvde - 2>&1'.format(size),
  173. user='root')
  174. yield from self.vm1.shutdown(wait=True)
  175. # after origin shutdown there should be still no change
  176. # origin changes propagated to snapshot too early2
  177. yield from self.vm2.run_for_stdio(
  178. 'head -c {} /dev/zero 2>&1 | diff -q /dev/xvde - 2>&1'.format(size),
  179. user='root')
  180. yield from self.vm2.shutdown(wait=True)
  181. yield from self.vm2.start()
  182. # only after target VM restart changes should be visible
  183. # origin changes not visible in snapshot
  184. with self.assertRaises(subprocess.CalledProcessError):
  185. yield from self.vm2.run(
  186. 'head -c {} /dev/zero 2>&1 | diff -q /dev/xvde - 2>&1'.format(
  187. size),
  188. user='root')
  189. def test_004_snapshot_non_persistent(self):
  190. '''Test snapshot volume non-persistence'''
  191. return self.loop.run_until_complete(
  192. self._test_004_snapshot_non_persistent())
  193. @asyncio.coroutine
  194. def _test_004_snapshot_non_persistent(self):
  195. size = 128 * 1024 * 1024
  196. volume_config = {
  197. 'pool': self.pool.name,
  198. 'size': size,
  199. 'save_on_stop': True,
  200. 'rw': True,
  201. }
  202. testvol = self.vm1.storage.init_volume('testvol', volume_config)
  203. yield from self.vm1.storage.get_pool(testvol).create(testvol)
  204. volume_config = {
  205. 'pool': self.pool.name,
  206. 'size': size,
  207. 'snap_on_start': True,
  208. 'source': testvol.vid,
  209. 'rw': True,
  210. }
  211. testvol_snap = self.vm2.storage.init_volume('testvol', volume_config)
  212. yield from self.vm2.storage.get_pool(testvol_snap).create(testvol_snap)
  213. self.app.save()
  214. yield from self.vm2.start()
  215. # snapshot image not clean
  216. yield from self.vm2.run_for_stdio(
  217. 'head -c {} /dev/zero | diff -q /dev/xvde -'.format(size),
  218. user='root')
  219. # Write to read-write snapshot volume failed
  220. yield from self.vm2.run_for_stdio('echo test123 > /dev/xvde && sync',
  221. user='root')
  222. yield from self.vm2.shutdown(wait=True)
  223. yield from self.vm2.start()
  224. # changes on snapshot survived VM restart
  225. yield from self.vm2.run_for_stdio(
  226. 'head -c {} /dev/zero 2>&1 | diff -q /dev/xvde - 2>&1'.format(size),
  227. user='root')
  228. class StorageFile(StorageTestMixin, qubes.tests.SystemTestCase):
  229. def init_pool(self):
  230. self.dir_path = '/var/tmp/test-pool'
  231. self.pool = self.app.add_pool(dir_path=self.dir_path,
  232. name='test-pool', driver='file')
  233. os.mkdir(os.path.join(self.dir_path, 'appvms', self.vm1.name))
  234. os.mkdir(os.path.join(self.dir_path, 'appvms', self.vm2.name))
  235. def tearDown(self):
  236. self.app.remove_pool('test-pool')
  237. shutil.rmtree(self.dir_path)
  238. super(StorageFile, self).tearDown()
  239. @qubes.tests.storage_lvm.skipUnlessLvmPoolExists
  240. class StorageLVM(StorageTestMixin, qubes.tests.SystemTestCase):
  241. def init_pool(self):
  242. # check if the default LVM Thin pool qubes_dom0/pool00 exists
  243. volume_group, thin_pool = \
  244. qubes.tests.storage_lvm.DEFAULT_LVM_POOL.split('/', 1)
  245. self.pool = self._find_pool(volume_group, thin_pool)
  246. if not self.pool:
  247. self.pool = self.app.add_pool(**qubes.tests.storage_lvm.POOL_CONF)
  248. self.created_pool = True
  249. def tearDown(self):
  250. ''' Remove the default lvm pool if it was created only for this test '''
  251. if self.created_pool:
  252. self.app.remove_pool(self.pool.name)
  253. super(StorageLVM, self).tearDown()
  254. def _find_pool(self, volume_group, thin_pool):
  255. ''' Returns the pool matching the specified ``volume_group`` &
  256. ``thin_pool``, or None.
  257. '''
  258. pools = [p for p in self.app.pools
  259. if issubclass(p.__class__, qubes.storage.lvm.ThinPool)]
  260. for pool in pools:
  261. if pool.volume_group == volume_group \
  262. and pool.thin_pool == thin_pool:
  263. return pool
  264. return None