storage.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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 library is free software; you can redistribute it and/or
  8. # modify it under the terms of the GNU Lesser General Public
  9. # License as published by the Free Software Foundation; either
  10. # version 2.1 of the License, or (at your option) any later version.
  11. #
  12. # This library 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 GNU
  15. # Lesser General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Lesser General Public
  18. # License along with this library; if not, see <https://www.gnu.org/licenses/>.
  19. #
  20. import asyncio
  21. import os
  22. import shutil
  23. import subprocess
  24. from contextlib import suppress
  25. import qubes.storage.lvm
  26. import qubes.tests
  27. import qubes.tests.storage_lvm
  28. import qubes.tests.storage_reflink
  29. import qubes.utils
  30. import qubes.vm.appvm
  31. class StorageTestMixin(object):
  32. def setUp(self):
  33. super(StorageTestMixin, self).setUp()
  34. self.init_default_template()
  35. self.old_default_pool = self.app.default_pool
  36. self.vm1 = self.app.add_new_vm(qubes.vm.appvm.AppVM,
  37. name=self.make_vm_name('vm1'),
  38. label='red')
  39. self.loop.run_until_complete(self.vm1.create_on_disk())
  40. self.vm2 = self.app.add_new_vm(qubes.vm.appvm.AppVM,
  41. name=self.make_vm_name('vm2'),
  42. label='red')
  43. self.loop.run_until_complete(self.vm2.create_on_disk())
  44. self.pool = None
  45. self.init_pool()
  46. self.app.save()
  47. def tearDown(self):
  48. with suppress(qubes.exc.QubesException):
  49. self.loop.run_until_complete(self.vm1.kill())
  50. with suppress(qubes.exc.QubesException):
  51. self.loop.run_until_complete(self.vm2.kill())
  52. del self.app.domains[self.vm1]
  53. del self.app.domains[self.vm2]
  54. del self.vm1
  55. del self.vm2
  56. self.app.default_pool = self.old_default_pool
  57. self.cleanup_pool()
  58. del self.pool
  59. super(StorageTestMixin, self).tearDown()
  60. def init_pool(self):
  61. ''' Initialize storage pool to be tested, store it in self.pool'''
  62. raise NotImplementedError
  63. def cleanup_pool(self):
  64. ''' Remove tested storage pool'''
  65. raise NotImplementedError
  66. def test_000_volatile(self):
  67. '''Test if volatile volume is really volatile'''
  68. return self.loop.run_until_complete(self._test_000_volatile())
  69. @asyncio.coroutine
  70. def _test_000_volatile(self):
  71. size = 32*1024*1024
  72. volume_config = {
  73. 'pool': self.pool.name,
  74. 'size': size,
  75. 'save_on_stop': False,
  76. 'rw': True,
  77. }
  78. testvol = self.vm1.storage.init_volume('testvol', volume_config)
  79. yield from qubes.utils.coro_maybe(testvol.create())
  80. del testvol
  81. self.app.save()
  82. yield from (self.vm1.start())
  83. yield from self.wait_for_session(self.vm1)
  84. # volatile image not clean
  85. yield from (self.vm1.run_for_stdio(
  86. 'head -c {} /dev/zero 2>&1 | diff -q /dev/xvde - 2>&1'.format(size),
  87. user='root'))
  88. # volatile image not volatile
  89. yield from (
  90. self.vm1.run_for_stdio('echo test123 > /dev/xvde', user='root'))
  91. yield from (self.vm1.shutdown(wait=True))
  92. yield from (self.vm1.start())
  93. yield from (self.vm1.run_for_stdio(
  94. 'head -c {} /dev/zero 2>&1 | diff -q /dev/xvde - 2>&1'.format(size),
  95. user='root'))
  96. def test_001_non_volatile(self):
  97. '''Test if non-volatile volume is really non-volatile'''
  98. return self.loop.run_until_complete(self._test_001_non_volatile())
  99. @asyncio.coroutine
  100. def _test_001_non_volatile(self):
  101. size = 32*1024*1024
  102. volume_config = {
  103. 'pool': self.pool.name,
  104. 'size': size,
  105. 'save_on_stop': True,
  106. 'rw': True,
  107. }
  108. testvol = self.vm1.storage.init_volume('testvol', volume_config)
  109. yield from qubes.utils.coro_maybe(testvol.create())
  110. del testvol
  111. self.app.save()
  112. yield from self.vm1.start()
  113. yield from self.wait_for_session(self.vm1)
  114. # non-volatile image not clean
  115. yield from self.vm1.run_for_stdio(
  116. 'head -c {} /dev/zero 2>&1 | diff -q /dev/xvde - 2>&1'.format(size),
  117. user='root')
  118. yield from self.vm1.run_for_stdio('echo test123 > /dev/xvde',
  119. user='root')
  120. yield from self.vm1.shutdown(wait=True)
  121. yield from self.vm1.start()
  122. # non-volatile image volatile
  123. with self.assertRaises(subprocess.CalledProcessError):
  124. yield from self.vm1.run_for_stdio(
  125. 'head -c {} /dev/zero 2>&1 | diff -q /dev/xvde - 2>&1'.format(
  126. size),
  127. user='root')
  128. def test_002_read_only(self):
  129. '''Test read-only volume'''
  130. self.loop.run_until_complete(self._test_002_read_only())
  131. @asyncio.coroutine
  132. def _test_002_read_only(self):
  133. size = 32 * 1024 * 1024
  134. volume_config = {
  135. 'pool': self.pool.name,
  136. 'size': size,
  137. 'save_on_stop': False,
  138. 'rw': False,
  139. }
  140. testvol = self.vm1.storage.init_volume('testvol', volume_config)
  141. yield from qubes.utils.coro_maybe(testvol.create())
  142. del testvol
  143. self.app.save()
  144. yield from self.vm1.start()
  145. # non-volatile image not clean
  146. yield from self.vm1.run_for_stdio(
  147. 'head -c {} /dev/zero 2>&1 | diff -q /dev/xvde - 2>&1'.format(size),
  148. user='root')
  149. # Write to read-only volume unexpectedly succeeded
  150. with self.assertRaises(subprocess.CalledProcessError):
  151. yield from self.vm1.run_for_stdio('echo test123 > /dev/xvde',
  152. user='root')
  153. # read-only volume modified
  154. yield from self.vm1.run_for_stdio(
  155. 'head -c {} /dev/zero 2>&1 | diff -q /dev/xvde - 2>&1'.format(size),
  156. user='root')
  157. def test_003_snapshot(self):
  158. '''Test snapshot volume data propagation'''
  159. self.loop.run_until_complete(self._test_003_snapshot())
  160. @asyncio.coroutine
  161. def _test_003_snapshot(self):
  162. size = 128 * 1024 * 1024
  163. volume_config = {
  164. 'pool': self.pool.name,
  165. 'size': size,
  166. 'save_on_stop': True,
  167. 'rw': True,
  168. }
  169. testvol = self.vm1.storage.init_volume('testvol', volume_config)
  170. yield from qubes.utils.coro_maybe(testvol.create())
  171. volume_config = {
  172. 'pool': self.pool.name,
  173. 'size': size,
  174. 'snap_on_start': True,
  175. 'source': testvol.vid,
  176. 'rw': True,
  177. }
  178. del testvol
  179. testvol_snap = self.vm2.storage.init_volume('testvol', volume_config)
  180. yield from qubes.utils.coro_maybe(testvol_snap.create())
  181. del testvol_snap
  182. self.app.save()
  183. yield from self.vm1.start()
  184. yield from self.vm2.start()
  185. yield from asyncio.wait(
  186. [self.wait_for_session(self.vm1), self.wait_for_session(self.vm2)])
  187. try:
  188. yield from self.vm1.run_for_stdio(
  189. 'head -c {} /dev/zero 2>&1 | diff -q /dev/xvde - 2>&1'.
  190. format(size),
  191. user='root')
  192. except subprocess.CalledProcessError:
  193. self.fail('origin image not clean')
  194. try:
  195. yield from self.vm2.run_for_stdio(
  196. 'head -c {} /dev/zero | diff -q /dev/xvde -'.format(size),
  197. user='root')
  198. except subprocess.CalledProcessError:
  199. self.fail('snapshot image not clean')
  200. try:
  201. yield from self.vm1.run_for_stdio(
  202. 'echo test123 > /dev/xvde && sync',
  203. user='root')
  204. except subprocess.CalledProcessError:
  205. self.fail('Write to read-write volume failed')
  206. try:
  207. yield from self.vm2.run_for_stdio(
  208. 'head -c {} /dev/zero 2>&1 | diff -q /dev/xvde - 2>&1'.
  209. format(size),
  210. user='root')
  211. except subprocess.CalledProcessError:
  212. self.fail('origin changes propagated to snapshot too early')
  213. yield from self.vm1.shutdown(wait=True)
  214. # after origin shutdown there should be still no change
  215. try:
  216. yield from self.vm2.run_for_stdio(
  217. 'head -c {} /dev/zero 2>&1 | diff -q /dev/xvde - 2>&1'.
  218. format(size),
  219. user='root')
  220. except subprocess.CalledProcessError:
  221. self.fail('origin changes propagated to snapshot too early2')
  222. yield from self.vm2.shutdown(wait=True)
  223. yield from self.vm2.start()
  224. # only after target VM restart changes should be visible
  225. with self.assertRaises(subprocess.CalledProcessError,
  226. msg='origin changes not visible in snapshot'):
  227. yield from self.vm2.run_for_stdio(
  228. 'head -c {} /dev/zero 2>&1 | diff -q /dev/xvde - 2>&1'.format(
  229. size),
  230. user='root')
  231. def test_004_snapshot_non_persistent(self):
  232. '''Test snapshot volume non-persistence'''
  233. return self.loop.run_until_complete(
  234. self._test_004_snapshot_non_persistent())
  235. @asyncio.coroutine
  236. def _test_004_snapshot_non_persistent(self):
  237. size = 128 * 1024 * 1024
  238. volume_config = {
  239. 'pool': self.pool.name,
  240. 'size': size,
  241. 'save_on_stop': True,
  242. 'rw': True,
  243. }
  244. testvol = self.vm1.storage.init_volume('testvol', volume_config)
  245. yield from qubes.utils.coro_maybe(testvol.create())
  246. volume_config = {
  247. 'pool': self.pool.name,
  248. 'size': size,
  249. 'snap_on_start': True,
  250. 'source': testvol.vid,
  251. 'rw': True,
  252. }
  253. del testvol
  254. testvol_snap = self.vm2.storage.init_volume('testvol', volume_config)
  255. yield from qubes.utils.coro_maybe(testvol_snap.create())
  256. del testvol_snap
  257. self.app.save()
  258. yield from self.vm2.start()
  259. yield from self.wait_for_session(self.vm2)
  260. # snapshot image not clean
  261. yield from self.vm2.run_for_stdio(
  262. 'head -c {} /dev/zero | diff -q /dev/xvde -'.format(size),
  263. user='root')
  264. # Write to read-write snapshot volume failed
  265. yield from self.vm2.run_for_stdio('echo test123 > /dev/xvde && sync',
  266. user='root')
  267. yield from self.vm2.shutdown(wait=True)
  268. yield from self.vm2.start()
  269. # changes on snapshot survived VM restart
  270. yield from self.vm2.run_for_stdio(
  271. 'head -c {} /dev/zero 2>&1 | diff -q /dev/xvde - 2>&1'.format(size),
  272. user='root')
  273. class StorageFile(StorageTestMixin, qubes.tests.SystemTestCase):
  274. def init_pool(self):
  275. self.dir_path = '/var/tmp/test-pool'
  276. self.pool = self.loop.run_until_complete(
  277. self.app.add_pool(dir_path=self.dir_path,
  278. name='test-pool', driver='file'))
  279. os.makedirs(os.path.join(self.dir_path, 'appvms', self.vm1.name),
  280. exist_ok=True)
  281. os.makedirs(os.path.join(self.dir_path, 'appvms', self.vm2.name),
  282. exist_ok=True)
  283. def cleanup_pool(self):
  284. self.loop.run_until_complete(self.app.remove_pool('test-pool'))
  285. shutil.rmtree(self.dir_path)
  286. class StorageReflinkMixin(StorageTestMixin):
  287. def cleanup_pool(self):
  288. self.loop.run_until_complete(self.app.remove_pool(self.pool.name))
  289. def init_pool(self, fs_type, **kwargs):
  290. name = 'test-reflink-integration-on-' + fs_type
  291. dir_path = os.path.join('/var/tmp', name)
  292. qubes.tests.storage_reflink.mkdir_fs(dir_path, fs_type,
  293. cleanup_via=self.addCleanup)
  294. self.pool = self.loop.run_until_complete(
  295. self.app.add_pool(name=name, dir_path=dir_path,
  296. driver='file-reflink', **kwargs))
  297. class StorageReflinkOnBtrfs(StorageReflinkMixin, qubes.tests.SystemTestCase):
  298. def init_pool(self):
  299. super().init_pool('btrfs')
  300. class StorageReflinkOnExt4(StorageReflinkMixin, qubes.tests.SystemTestCase):
  301. def init_pool(self):
  302. super().init_pool('ext4', setup_check='no')
  303. @qubes.tests.storage_lvm.skipUnlessLvmPoolExists
  304. class StorageLVM(StorageTestMixin, qubes.tests.SystemTestCase):
  305. def init_pool(self):
  306. self.created_pool = False
  307. # check if the default LVM Thin pool qubes_dom0/pool00 exists
  308. volume_group, thin_pool = \
  309. qubes.tests.storage_lvm.DEFAULT_LVM_POOL.split('/', 1)
  310. self.pool = self._find_pool(volume_group, thin_pool)
  311. if not self.pool:
  312. self.pool = self.loop.run_until_complete(
  313. self.app.add_pool(**qubes.tests.storage_lvm.POOL_CONF))
  314. self.created_pool = True
  315. def cleanup_pool(self):
  316. ''' Remove the default lvm pool if it was created only for this test '''
  317. if self.created_pool:
  318. self.loop.run_until_complete(self.app.remove_pool(self.pool.name))
  319. def _find_pool(self, volume_group, thin_pool):
  320. ''' Returns the pool matching the specified ``volume_group`` &
  321. ``thin_pool``, or None.
  322. '''
  323. pools = [p for p in self.app.pools
  324. if issubclass(p.__class__, qubes.storage.lvm.ThinPool)]
  325. for pool in pools:
  326. if pool.volume_group == volume_group \
  327. and pool.thin_pool == thin_pool:
  328. return pool
  329. return None