storage.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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(qubes.tests.SystemTestsMixin):
  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. 'internal': False,
  56. 'save_on_stop': False,
  57. 'rw': True,
  58. }
  59. testvol = self.vm1.storage.init_volume('testvol', volume_config)
  60. yield from self.vm1.storage.get_pool(testvol).create(testvol)
  61. self.app.save()
  62. yield from (self.vm1.start())
  63. # volatile image not clean
  64. yield from (self.vm1.run_for_stdio(
  65. 'head -c {} /dev/zero 2>&1 | diff -q /dev/xvde - 2>&1'.format(size),
  66. user='root'))
  67. # volatile image not volatile
  68. yield from (
  69. self.vm1.run_for_stdio('echo test123 > /dev/xvde', user='root'))
  70. yield from (self.vm1.shutdown(wait=True))
  71. yield from (self.vm1.start())
  72. yield from (self.vm1.run_for_stdio(
  73. 'head -c {} /dev/zero 2>&1 | diff -q /dev/xvde - 2>&1'.format(size),
  74. user='root'))
  75. def test_001_non_volatile(self):
  76. '''Test if non-volatile volume is really non-volatile'''
  77. return self.loop.run_until_complete(self._test_001_non_volatile())
  78. @asyncio.coroutine
  79. def _test_001_non_volatile(self):
  80. size = 32*1024*1024
  81. volume_config = {
  82. 'pool': self.pool.name,
  83. 'size': size,
  84. 'internal': False,
  85. 'save_on_stop': True,
  86. 'rw': True,
  87. }
  88. testvol = yield from self.vm1.storage.init_volume(
  89. 'testvol', volume_config)
  90. yield from self.vm1.storage.get_pool(testvol).create(testvol)
  91. self.app.save()
  92. yield from self.vm1.start()
  93. # non-volatile image not clean
  94. yield from self.vm1.run_for_stdio(
  95. 'head -c {} /dev/zero 2>&1 | diff -q /dev/xvde - 2>&1'.format(size),
  96. user='root')
  97. yield from self.vm1.run_for_stdio('echo test123 > /dev/xvde',
  98. user='root')
  99. yield from self.vm1.shutdown(wait=True)
  100. yield from self.vm1.start()
  101. # non-volatile image volatile
  102. with self.assertRaises(subprocess.CalledProcessError):
  103. yield from self.vm1.run_for_stdio(
  104. 'head -c {} /dev/zero 2>&1 | diff -q /dev/xvde - 2>&1'.format(
  105. size),
  106. user='root')
  107. def test_002_read_only(self):
  108. '''Test read-only volume'''
  109. self.loop.run_until_complete(self._test_002_read_only())
  110. @asyncio.coroutine
  111. def _test_002_read_only(self):
  112. size = 32 * 1024 * 1024
  113. volume_config = {
  114. 'pool': self.pool.name,
  115. 'size': size,
  116. 'internal': False,
  117. 'save_on_stop': False,
  118. 'rw': False,
  119. }
  120. testvol = self.vm1.storage.init_volume('testvol', volume_config)
  121. yield from self.vm1.storage.get_pool(testvol).create(testvol)
  122. self.app.save()
  123. yield from self.vm1.start()
  124. # non-volatile image not clean
  125. yield from self.vm1.run_for_stdio(
  126. 'head -c {} /dev/zero 2>&1 | diff -q /dev/xvde - 2>&1'.format(size),
  127. user='root')
  128. # Write to read-only volume unexpectedly succeeded
  129. with self.assertRaises(subprocess.CalledProcessError):
  130. yield from self.vm1.run_for_stdio('echo test123 > /dev/xvde',
  131. user='root')
  132. # read-only volume modified
  133. yield from self.vm1.run_for_stdio(
  134. 'head -c {} /dev/zero 2>&1 | diff -q /dev/xvde - 2>&1'.format(size),
  135. user='root')
  136. def test_003_snapshot(self):
  137. '''Test snapshot volume data propagation'''
  138. self.loop.run_until_complete(self._test_003_snapshot())
  139. @asyncio.coroutine
  140. def _test_003_snapshot(self):
  141. size = 128 * 1024 * 1024
  142. volume_config = {
  143. 'pool': self.pool.name,
  144. 'size': size,
  145. 'internal': False,
  146. 'save_on_stop': True,
  147. 'rw': True,
  148. }
  149. testvol = self.vm1.storage.init_volume('testvol', volume_config)
  150. yield from self.vm1.storage.get_pool(testvol).create(testvol)
  151. volume_config = {
  152. 'pool': self.pool.name,
  153. 'size': size,
  154. 'internal': False,
  155. 'snap_on_start': True,
  156. 'source': testvol.vid,
  157. 'rw': True,
  158. }
  159. testvol_snap = self.vm2.storage.init_volume('testvol', volume_config)
  160. yield from self.vm2.storage.get_pool(testvol_snap).create(testvol_snap)
  161. self.app.save()
  162. yield from self.vm1.start()
  163. yield from self.vm2.start()
  164. # origin image not clean
  165. yield from self.vm1.run_for_stdio(
  166. 'head -c {} /dev/zero 2>&1 | diff -q /dev/xvde - 2>&1'.format(size),
  167. user='root')
  168. # snapshot image not clean
  169. yield from self.vm2.run_for_stdio(
  170. 'head -c {} /dev/zero | diff -q /dev/xvde -'.format(size),
  171. user='root')
  172. # Write to read-write volume failed
  173. yield from self.vm1.run_for_stdio('echo test123 > /dev/xvde && sync',
  174. user='root')
  175. # origin changes propagated to snapshot too early
  176. yield from self.vm2.run_for_stdio(
  177. 'head -c {} /dev/zero 2>&1 | diff -q /dev/xvde - 2>&1'.format(size),
  178. user='root')
  179. yield from self.vm1.shutdown(wait=True)
  180. # after origin shutdown there should be still no change
  181. # origin changes propagated to snapshot too early2
  182. yield from self.vm2.run_for_stdio(
  183. 'head -c {} /dev/zero 2>&1 | diff -q /dev/xvde - 2>&1'.format(size),
  184. user='root')
  185. yield from self.vm2.shutdown(wait=True)
  186. yield from self.vm2.start()
  187. # only after target VM restart changes should be visible
  188. # origin changes not visible in snapshot
  189. with self.assertRaises(subprocess.CalledProcessError):
  190. yield from self.vm2.run(
  191. 'head -c {} /dev/zero 2>&1 | diff -q /dev/xvde - 2>&1'.format(
  192. size),
  193. user='root')
  194. def test_004_snapshot_non_persistent(self):
  195. '''Test snapshot volume non-persistence'''
  196. return self.loop.run_until_complete(
  197. self._test_004_snapshot_non_persistent())
  198. @asyncio.coroutine
  199. def _test_004_snapshot_non_persistent(self):
  200. size = 128 * 1024 * 1024
  201. volume_config = {
  202. 'pool': self.pool.name,
  203. 'size': size,
  204. 'internal': False,
  205. 'save_on_stop': True,
  206. 'rw': True,
  207. }
  208. testvol = self.vm1.storage.init_volume('testvol', volume_config)
  209. yield from self.vm1.storage.get_pool(testvol).create(testvol)
  210. volume_config = {
  211. 'pool': self.pool.name,
  212. 'size': size,
  213. 'internal': False,
  214. 'snap_on_start': True,
  215. 'source': testvol.vid,
  216. 'rw': True,
  217. }
  218. testvol_snap = self.vm2.storage.init_volume('testvol', volume_config)
  219. yield from self.vm2.storage.get_pool(testvol_snap).create(testvol_snap)
  220. self.app.save()
  221. yield from self.vm2.start()
  222. # snapshot image not clean
  223. yield from self.vm2.run_for_stdio(
  224. 'head -c {} /dev/zero | diff -q /dev/xvde -'.format(size),
  225. user='root')
  226. # Write to read-write snapshot volume failed
  227. yield from self.vm2.run_for_stdio('echo test123 > /dev/xvde && sync',
  228. user='root')
  229. yield from self.vm2.shutdown(wait=True)
  230. yield from self.vm2.start()
  231. # changes on snapshot survived VM restart
  232. yield from self.vm2.run_for_stdio(
  233. 'head -c {} /dev/zero 2>&1 | diff -q /dev/xvde - 2>&1'.format(size),
  234. user='root')
  235. class StorageFile(StorageTestMixin, qubes.tests.QubesTestCase):
  236. def init_pool(self):
  237. self.dir_path = '/var/tmp/test-pool'
  238. self.pool = self.app.add_pool(dir_path=self.dir_path,
  239. name='test-pool', driver='file')
  240. os.mkdir(os.path.join(self.dir_path, 'appvms', self.vm1.name))
  241. os.mkdir(os.path.join(self.dir_path, 'appvms', self.vm2.name))
  242. def tearDown(self):
  243. self.app.remove_pool('test-pool')
  244. shutil.rmtree(self.dir_path)
  245. super(StorageFile, self).tearDown()
  246. @qubes.tests.storage_lvm.skipUnlessLvmPoolExists
  247. class StorageLVM(StorageTestMixin, qubes.tests.QubesTestCase):
  248. def init_pool(self):
  249. # check if the default LVM Thin pool qubes_dom0/pool00 exists
  250. volume_group, thin_pool = \
  251. qubes.tests.storage_lvm.DEFAULT_LVM_POOL.split('/', 1)
  252. self.pool = self._find_pool(volume_group, thin_pool)
  253. if not self.pool:
  254. self.pool = self.app.add_pool(**qubes.tests.storage_lvm.POOL_CONF)
  255. self.created_pool = True
  256. def tearDown(self):
  257. ''' Remove the default lvm pool if it was created only for this test '''
  258. if self.created_pool:
  259. self.app.remove_pool(self.pool.name)
  260. super(StorageLVM, self).tearDown()
  261. def _find_pool(self, volume_group, thin_pool):
  262. ''' Returns the pool matching the specified ``volume_group`` &
  263. ``thin_pool``, or None.
  264. '''
  265. pools = [p for p in self.app.pools
  266. if issubclass(p.__class__, qubes.storage.lvm.ThinPool)]
  267. for pool in pools:
  268. if pool.volume_group == volume_group \
  269. and pool.thin_pool == thin_pool:
  270. return pool
  271. return None