storage.py 13 KB

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