storage.py 13 KB

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