storage.py 11 KB

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