storage_lvm.py 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180
  1. #
  2. # The Qubes OS Project, http://www.qubes-os.org
  3. #
  4. # Copyright (C) 2016 Bahtiar `kalkin-` Gadimov <bahtiar@gadimov.de>
  5. #
  6. # This library is free software; you can redistribute it and/or
  7. # modify it under the terms of the GNU Lesser General Public
  8. # License as published by the Free Software Foundation; either
  9. # version 2.1 of the License, or (at your option) any later version.
  10. #
  11. # This library is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. # Lesser General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Lesser General Public
  17. # License along with this library; if not, see <https://www.gnu.org/licenses/>.
  18. #
  19. ''' Tests for lvm storage driver. By default tests are going to use the
  20. 'qubes_dom0/pool00'. An alternative LVM thin pool may be provided via
  21. :envvar:`DEFAULT_LVM_POOL` shell variable.
  22. Any pool variables prefixed with 'LVM_' or 'lvm_' represent a LVM
  23. 'volume_group/thin_pool' combination. Pool variables without a prefix
  24. represent a :py:class:`qubes.storage.lvm.ThinPool`.
  25. '''
  26. import os
  27. import subprocess
  28. import tempfile
  29. import unittest
  30. import unittest.mock
  31. import asyncio
  32. import qubes.tests
  33. import qubes.tests.storage
  34. import qubes.storage
  35. from qubes.storage.lvm import ThinPool, ThinVolume, qubes_lvm
  36. if 'DEFAULT_LVM_POOL' in os.environ.keys():
  37. DEFAULT_LVM_POOL = os.environ['DEFAULT_LVM_POOL']
  38. else:
  39. DEFAULT_LVM_POOL = 'qubes_dom0/pool00'
  40. def lvm_pool_exists(volume_group, thin_pool):
  41. ''' Returns ``True`` if thin pool exists in the volume group. '''
  42. path = "/dev/mapper/{!s}-{!s}".format(
  43. volume_group.replace('-', '--'),
  44. thin_pool.replace('-', '--'))
  45. return os.path.exists(path)
  46. def skipUnlessLvmPoolExists(test_item): # pylint: disable=invalid-name
  47. ''' Decorator that skips LVM tests if the default pool is missing. '''
  48. volume_group, thin_pool = DEFAULT_LVM_POOL.split('/', 1)
  49. result = lvm_pool_exists(volume_group, thin_pool)
  50. msg = 'LVM thin pool {!r} does not exist'.format(DEFAULT_LVM_POOL)
  51. return unittest.skipUnless(result, msg)(test_item)
  52. POOL_CONF = {'name': 'test-lvm',
  53. 'driver': 'lvm_thin',
  54. 'volume_group': DEFAULT_LVM_POOL.split('/')[0],
  55. 'thin_pool': DEFAULT_LVM_POOL.split('/')[1]}
  56. class ThinPoolBase(qubes.tests.QubesTestCase):
  57. ''' Sanity tests for :py:class:`qubes.storage.lvm.ThinPool` '''
  58. created_pool = False
  59. def setUp(self):
  60. super(ThinPoolBase, self).setUp()
  61. volume_group, thin_pool = DEFAULT_LVM_POOL.split('/', 1)
  62. self.pool = self._find_pool(volume_group, thin_pool)
  63. if not self.pool:
  64. self.pool = self.loop.run_until_complete(
  65. self.app.add_pool(**POOL_CONF))
  66. self.created_pool = True
  67. def cleanup_test_volumes(self):
  68. p = self.loop.run_until_complete(asyncio.create_subprocess_exec(
  69. 'sudo', 'lvs', '--noheadings', '-o', 'lv_name', self.pool.volume_group,
  70. stdout=subprocess.PIPE
  71. ))
  72. volumes, _ = self.loop.run_until_complete(p.communicate())
  73. for volume in volumes.decode().splitlines():
  74. volume = volume.strip()
  75. if not volume.startswith('vm-' + qubes.tests.VMPREFIX):
  76. continue
  77. p = self.loop.run_until_complete(asyncio.create_subprocess_exec(
  78. 'sudo', 'lvremove', '-f', '/'.join([self.pool.volume_group, volume])
  79. ))
  80. self.loop.run_until_complete(p.wait())
  81. def tearDown(self):
  82. ''' Remove the default lvm pool if it was created only for this test '''
  83. self.cleanup_test_volumes()
  84. if self.created_pool:
  85. self.loop.run_until_complete(self.app.remove_pool(self.pool.name))
  86. super(ThinPoolBase, self).tearDown()
  87. def _find_pool(self, volume_group, thin_pool):
  88. ''' Returns the pool matching the specified ``volume_group`` &
  89. ``thin_pool``, or None.
  90. '''
  91. pools = [p for p in self.app.pools.values()
  92. if issubclass(p.__class__, ThinPool)]
  93. for pool in pools:
  94. if pool.volume_group == volume_group \
  95. and pool.thin_pool == thin_pool:
  96. return pool
  97. return None
  98. @skipUnlessLvmPoolExists
  99. class TC_00_ThinPool(ThinPoolBase):
  100. ''' Sanity tests for :py:class:`qubes.storage.lvm.ThinPool` '''
  101. def setUp(self):
  102. xml_path = '/tmp/qubes-test.xml'
  103. self.app = qubes.Qubes.create_empty_store(store=xml_path,
  104. clockvm=None,
  105. updatevm=None,
  106. offline_mode=True,
  107. )
  108. os.environ['QUBES_XML_PATH'] = xml_path
  109. super(TC_00_ThinPool, self).setUp()
  110. def tearDown(self):
  111. super(TC_00_ThinPool, self).tearDown()
  112. os.unlink(self.app.store)
  113. del self.app
  114. for attr in dir(self):
  115. if isinstance(getattr(self, attr), qubes.vm.BaseVM):
  116. delattr(self, attr)
  117. def test_000_default_thin_pool(self):
  118. ''' Check whether :py:data`DEFAULT_LVM_POOL` exists. This pool is
  119. created by default, if at installation time LVM + Thin was chosen.
  120. '''
  121. msg = 'Thin pool {!r} does not exist'.format(DEFAULT_LVM_POOL)
  122. self.assertTrue(self.pool, msg)
  123. def test_001_origin_volume(self):
  124. ''' Test origin volume creation '''
  125. config = {
  126. 'name': 'root',
  127. 'pool': self.pool.name,
  128. 'save_on_stop': True,
  129. 'rw': True,
  130. 'size': qubes.config.defaults['root_img_size'],
  131. }
  132. vm = qubes.tests.storage.TestVM(self)
  133. volume = self.app.get_pool(self.pool.name).init_volume(vm, config)
  134. self.assertIsInstance(volume, ThinVolume)
  135. self.assertEqual(volume.name, 'root')
  136. self.assertEqual(volume.pool, self.pool.name)
  137. self.assertEqual(volume.size, qubes.config.defaults['root_img_size'])
  138. self.loop.run_until_complete(volume.create())
  139. path = "/dev/%s" % volume.vid
  140. self.assertTrue(os.path.exists(path), path)
  141. self.loop.run_until_complete(volume.remove())
  142. def test_003_read_write_volume(self):
  143. ''' Test read-write volume creation '''
  144. config = {
  145. 'name': 'root',
  146. 'pool': self.pool.name,
  147. 'rw': True,
  148. 'save_on_stop': True,
  149. 'size': qubes.config.defaults['root_img_size'],
  150. }
  151. vm = qubes.tests.storage.TestVM(self)
  152. volume = self.app.get_pool(self.pool.name).init_volume(vm, config)
  153. self.assertIsInstance(volume, ThinVolume)
  154. self.assertEqual(volume.name, 'root')
  155. self.assertEqual(volume.pool, self.pool.name)
  156. self.assertEqual(volume.size, qubes.config.defaults['root_img_size'])
  157. self.loop.run_until_complete(volume.create())
  158. path = "/dev/%s" % volume.vid
  159. self.assertTrue(os.path.exists(path), path)
  160. self.loop.run_until_complete(volume.remove())
  161. def test_004_size(self):
  162. with self.assertNotRaises(NotImplementedError):
  163. size = self.pool.size
  164. environ = os.environ.copy()
  165. environ['LC_ALL'] = 'C.utf8'
  166. pool_size = subprocess.check_output(['sudo', 'lvs', '--noheadings',
  167. '-o', 'lv_size',
  168. '--units', 'b', self.pool.volume_group + '/' + self.pool.thin_pool],
  169. env=environ)
  170. self.assertEqual(size, int(pool_size.strip()[:-1]))
  171. def test_005_usage(self):
  172. with self.assertNotRaises(NotImplementedError):
  173. usage = self.pool.usage
  174. environ = os.environ.copy()
  175. environ['LC_ALL'] = 'C.utf8'
  176. pool_info = subprocess.check_output(['sudo', 'lvs', '--noheadings',
  177. '-o', 'lv_size,data_percent',
  178. '--units', 'b', self.pool.volume_group + '/' + self.pool.thin_pool],
  179. env=environ)
  180. pool_size, pool_usage = pool_info.strip().split()
  181. pool_size = int(pool_size[:-1])
  182. pool_usage = float(pool_usage)
  183. self.assertEqual(usage, int(pool_size * pool_usage / 100))
  184. def _get_size(self, path):
  185. if os.getuid() != 0:
  186. return int(
  187. subprocess.check_output(
  188. ['sudo', 'blockdev', '--getsize64', path]))
  189. fd = os.open(path, os.O_RDONLY)
  190. try:
  191. return os.lseek(fd, 0, os.SEEK_END)
  192. finally:
  193. os.close(fd)
  194. def test_006_resize(self):
  195. config = {
  196. 'name': 'root',
  197. 'pool': self.pool.name,
  198. 'rw': True,
  199. 'save_on_stop': True,
  200. 'size': 32 * 1024**2,
  201. }
  202. vm = qubes.tests.storage.TestVM(self)
  203. volume = self.app.get_pool(self.pool.name).init_volume(vm, config)
  204. self.loop.run_until_complete(volume.create())
  205. self.addCleanup(self.loop.run_until_complete, volume.remove())
  206. path = "/dev/%s" % volume.vid
  207. new_size = 64 * 1024 ** 2
  208. self.loop.run_until_complete(volume.resize(new_size))
  209. self.assertEqual(self._get_size(path), new_size)
  210. self.assertEqual(volume.size, new_size)
  211. def test_007_resize_running(self):
  212. old_size = 32 * 1024**2
  213. config = {
  214. 'name': 'root',
  215. 'pool': self.pool.name,
  216. 'rw': True,
  217. 'save_on_stop': True,
  218. 'size': old_size,
  219. }
  220. vm = qubes.tests.storage.TestVM(self)
  221. volume = self.app.get_pool(self.pool.name).init_volume(vm, config)
  222. self.loop.run_until_complete(volume.create())
  223. self.addCleanup(self.loop.run_until_complete, volume.remove())
  224. self.loop.run_until_complete(volume.start())
  225. path = "/dev/%s" % volume.vid
  226. path2 = "/dev/%s" % volume._vid_snap
  227. new_size = 64 * 1024 ** 2
  228. self.loop.run_until_complete(volume.resize(new_size))
  229. self.assertEqual(self._get_size(path), old_size)
  230. self.assertEqual(self._get_size(path2), new_size)
  231. self.assertEqual(volume.size, new_size)
  232. self.loop.run_until_complete(volume.stop())
  233. self.assertEqual(self._get_size(path), new_size)
  234. self.assertEqual(volume.size, new_size)
  235. def _get_lv_uuid(self, lv):
  236. sudo = [] if os.getuid() == 0 else ['sudo']
  237. lvs_output = subprocess.check_output(
  238. sudo + ['lvs', '--noheadings', '-o', 'lv_uuid', lv])
  239. return lvs_output.strip()
  240. def _get_lv_origin_uuid(self, lv):
  241. sudo = [] if os.getuid() == 0 else ['sudo']
  242. if qubes.storage.lvm.lvm_is_very_old:
  243. # no support for origin_uuid directly
  244. lvs_output = subprocess.check_output(
  245. sudo + ['lvs', '--noheadings', '-o', 'origin', lv])
  246. lvs_output = subprocess.check_output(
  247. sudo + ['lvs', '--noheadings', '-o', 'lv_uuid',
  248. lv.rsplit('/', 1)[0] + '/' + lvs_output.strip().decode()])
  249. else:
  250. lvs_output = subprocess.check_output(
  251. sudo + ['lvs', '--noheadings', '-o', 'origin_uuid', lv])
  252. return lvs_output.strip()
  253. def test_008_commit(self):
  254. ''' Test volume changes commit'''
  255. config = {
  256. 'name': 'root',
  257. 'pool': self.pool.name,
  258. 'save_on_stop': True,
  259. 'rw': True,
  260. 'size': qubes.config.defaults['root_img_size'],
  261. }
  262. vm = qubes.tests.storage.TestVM(self)
  263. volume = self.app.get_pool(self.pool.name).init_volume(vm, config)
  264. self.loop.run_until_complete(volume.create())
  265. path_snap = '/dev/' + volume._vid_snap
  266. self.assertFalse(os.path.exists(path_snap), path_snap)
  267. origin_uuid = self._get_lv_uuid(volume.path)
  268. self.loop.run_until_complete(volume.start())
  269. snap_uuid = self._get_lv_uuid(path_snap)
  270. self.assertNotEqual(origin_uuid, snap_uuid)
  271. path = volume.path
  272. self.assertTrue(path.startswith('/dev/' + volume.vid),
  273. '{} does not start with /dev/{}'.format(path, volume.vid))
  274. self.assertTrue(os.path.exists(path), path)
  275. self.loop.run_until_complete(volume.remove())
  276. def test_009_interrupted_commit(self):
  277. ''' Test volume changes commit'''
  278. config = {
  279. 'name': 'root',
  280. 'pool': self.pool.name,
  281. 'save_on_stop': True,
  282. 'rw': True,
  283. 'revisions_to_keep': 2,
  284. 'size': qubes.config.defaults['root_img_size'],
  285. }
  286. vm = qubes.tests.storage.TestVM(self)
  287. volume = self.app.get_pool(self.pool.name).init_volume(vm, config)
  288. # mock logging, to not interfere with time.time() mock
  289. volume.log = unittest.mock.Mock()
  290. # do not call volume.create(), do it manually to simulate
  291. # interrupted commit
  292. revisions = ['-1521065904-back', '-1521065905-back', '-snap']
  293. orig_uuids = {}
  294. for rev in revisions:
  295. cmd = ['create', self.pool._pool_id,
  296. volume.vid.split('/')[1] + rev, str(config['size'])]
  297. qubes_lvm(cmd)
  298. orig_uuids[rev] = self._get_lv_uuid(volume.vid + rev)
  299. qubes.storage.lvm.reset_cache()
  300. path_snap = '/dev/' + volume._vid_snap
  301. self.assertTrue(volume.is_dirty())
  302. self.assertEqual(volume.path,
  303. '/dev/' + volume.vid + revisions[1])
  304. expected_revisions = {
  305. revisions[0].lstrip('-'): '2018-03-14T22:18:24',
  306. revisions[1].lstrip('-'): '2018-03-14T22:18:25',
  307. }
  308. self.assertEqual(volume.revisions, expected_revisions)
  309. self.loop.run_until_complete(volume.start())
  310. self.assertEqual(volume.revisions, expected_revisions)
  311. snap_uuid = self._get_lv_uuid(path_snap)
  312. self.assertEqual(orig_uuids['-snap'], snap_uuid)
  313. self.assertTrue(volume.is_dirty())
  314. self.assertEqual(volume.path,
  315. '/dev/' + volume.vid + revisions[1])
  316. with unittest.mock.patch('time.time') as mock_time:
  317. mock_time.side_effect = [521065906]
  318. self.loop.run_until_complete(volume.stop())
  319. expected_revisions = {
  320. revisions[0].lstrip('-'): '2018-03-14T22:18:24',
  321. revisions[1].lstrip('-'): '2018-03-14T22:18:25',
  322. }
  323. self.assertFalse(volume.is_dirty())
  324. self.assertEqual(volume.revisions, expected_revisions)
  325. self.assertEqual(volume.path, '/dev/' + volume.vid)
  326. self.assertEqual(snap_uuid, self._get_lv_uuid(volume.path))
  327. self.assertFalse(os.path.exists(path_snap), path_snap)
  328. self.loop.run_until_complete(volume.remove())
  329. def test_010_migration1(self):
  330. '''Start with old revisions, then start interacting using new code'''
  331. config = {
  332. 'name': 'root',
  333. 'pool': self.pool.name,
  334. 'save_on_stop': True,
  335. 'rw': True,
  336. 'revisions_to_keep': 2,
  337. 'size': qubes.config.defaults['root_img_size'],
  338. }
  339. vm = qubes.tests.storage.TestVM(self)
  340. volume = self.app.get_pool(self.pool.name).init_volume(vm, config)
  341. # mock logging, to not interfere with time.time() mock
  342. volume.log = unittest.mock.Mock()
  343. # do not call volume.create(), do it manually to have old LV naming
  344. revisions = ['', '-1521065904-back', '-1521065905-back']
  345. orig_uuids = {}
  346. for rev in revisions:
  347. cmd = ['create', self.pool._pool_id,
  348. volume.vid.split('/')[1] + rev, str(config['size'])]
  349. qubes_lvm(cmd)
  350. orig_uuids[rev] = self._get_lv_uuid(volume.vid + rev)
  351. qubes.storage.lvm.reset_cache()
  352. path_snap = '/dev/' + volume._vid_snap
  353. self.assertFalse(os.path.exists(path_snap), path_snap)
  354. expected_revisions = {
  355. revisions[1].lstrip('-'): '2018-03-14T22:18:24',
  356. revisions[2].lstrip('-'): '2018-03-14T22:18:25',
  357. }
  358. self.assertEqual(volume.revisions, expected_revisions)
  359. self.assertEqual(volume.path, '/dev/' + volume.vid)
  360. self.loop.run_until_complete(volume.start())
  361. snap_uuid = self._get_lv_uuid(path_snap)
  362. self.assertNotEqual(orig_uuids[''], snap_uuid)
  363. snap_origin_uuid = self._get_lv_origin_uuid(path_snap)
  364. self.assertEqual(orig_uuids[''], snap_origin_uuid)
  365. path = volume.path
  366. self.assertEqual(path, '/dev/' + volume.vid)
  367. self.assertTrue(os.path.exists(path), path)
  368. with unittest.mock.patch('time.time') as mock_time:
  369. mock_time.side_effect = ('1521065906', '1521065907')
  370. self.loop.run_until_complete(volume.stop())
  371. revisions.extend(['-1521065906-back'])
  372. expected_revisions = {
  373. revisions[2].lstrip('-'): '2018-03-14T22:18:25',
  374. revisions[3].lstrip('-'): '2018-03-14T22:18:26',
  375. }
  376. self.assertEqual(volume.revisions, expected_revisions)
  377. self.assertEqual(volume.path, '/dev/' + volume.vid)
  378. path_snap = '/dev/' + volume._vid_snap
  379. self.assertFalse(os.path.exists(path_snap), path_snap)
  380. self.assertTrue(os.path.exists('/dev/' + volume.vid))
  381. self.assertEqual(self._get_lv_uuid(volume.path), snap_uuid)
  382. prev_path = '/dev/' + volume.vid + revisions[3]
  383. self.assertEqual(self._get_lv_uuid(prev_path), orig_uuids[''])
  384. self.loop.run_until_complete(volume.remove())
  385. for rev in revisions:
  386. path = '/dev/' + volume.vid + rev
  387. self.assertFalse(os.path.exists(path), path)
  388. def test_011_migration2(self):
  389. '''VM started with old code, stopped with new'''
  390. config = {
  391. 'name': 'root',
  392. 'pool': self.pool.name,
  393. 'save_on_stop': True,
  394. 'rw': True,
  395. 'revisions_to_keep': 1,
  396. 'size': qubes.config.defaults['root_img_size'],
  397. }
  398. vm = qubes.tests.storage.TestVM(self)
  399. volume = self.app.get_pool(self.pool.name).init_volume(vm, config)
  400. # mock logging, to not interfere with time.time() mock
  401. volume.log = unittest.mock.Mock()
  402. # do not call volume.create(), do it manually to have old LV naming
  403. revisions = ['', '-snap']
  404. orig_uuids = {}
  405. for rev in revisions:
  406. cmd = ['create', self.pool._pool_id,
  407. volume.vid.split('/')[1] + rev, str(config['size'])]
  408. qubes_lvm(cmd)
  409. orig_uuids[rev] = self._get_lv_uuid(volume.vid + rev)
  410. qubes.storage.lvm.reset_cache()
  411. path_snap = '/dev/' + volume._vid_snap
  412. self.assertTrue(os.path.exists(path_snap), path_snap)
  413. expected_revisions = {}
  414. self.assertEqual(volume.revisions, expected_revisions)
  415. self.assertEqual(volume.path, '/dev/' + volume.vid)
  416. self.assertTrue(volume.is_dirty())
  417. path = volume.path
  418. self.assertEqual(path, '/dev/' + volume.vid)
  419. self.assertTrue(os.path.exists(path), path)
  420. with unittest.mock.patch('time.time') as mock_time:
  421. mock_time.side_effect = ('1521065906', '1521065907')
  422. self.loop.run_until_complete(volume.stop())
  423. revisions.extend(['-1521065906-back'])
  424. expected_revisions = {
  425. revisions[2].lstrip('-'): '2018-03-14T22:18:26',
  426. }
  427. self.assertEqual(volume.revisions, expected_revisions)
  428. self.assertEqual(volume.path, '/dev/' + volume.vid)
  429. path_snap = '/dev/' + volume._vid_snap
  430. self.assertFalse(os.path.exists(path_snap), path_snap)
  431. self.assertTrue(os.path.exists('/dev/' + volume.vid))
  432. self.assertEqual(self._get_lv_uuid(volume.path), orig_uuids['-snap'])
  433. prev_path = '/dev/' + volume.vid + revisions[2]
  434. self.assertEqual(self._get_lv_uuid(prev_path), orig_uuids[''])
  435. self.loop.run_until_complete(volume.remove())
  436. for rev in revisions:
  437. path = '/dev/' + volume.vid + rev
  438. self.assertFalse(os.path.exists(path), path)
  439. def test_012_migration3(self):
  440. '''VM started with old code, started again with new, stopped with new'''
  441. config = {
  442. 'name': 'root',
  443. 'pool': self.pool.name,
  444. 'save_on_stop': True,
  445. 'rw': True,
  446. 'revisions_to_keep': 1,
  447. 'size': qubes.config.defaults['root_img_size'],
  448. }
  449. vm = qubes.tests.storage.TestVM(self)
  450. volume = self.app.get_pool(self.pool.name).init_volume(vm, config)
  451. # mock logging, to not interfere with time.time() mock
  452. volume.log = unittest.mock.Mock()
  453. # do not call volume.create(), do it manually to have old LV naming
  454. revisions = ['', '-snap']
  455. orig_uuids = {}
  456. for rev in revisions:
  457. cmd = ['create', self.pool._pool_id,
  458. volume.vid.split('/')[1] + rev, str(config['size'])]
  459. qubes_lvm(cmd)
  460. orig_uuids[rev] = self._get_lv_uuid(volume.vid + rev)
  461. qubes.storage.lvm.reset_cache()
  462. path_snap = '/dev/' + volume._vid_snap
  463. self.assertTrue(os.path.exists(path_snap), path_snap)
  464. expected_revisions = {}
  465. self.assertEqual(volume.revisions, expected_revisions)
  466. self.assertTrue(volume.path, '/dev/' + volume.vid)
  467. self.assertTrue(volume.is_dirty())
  468. self.loop.run_until_complete(volume.start())
  469. self.assertEqual(volume.revisions, expected_revisions)
  470. self.assertEqual(volume.path, '/dev/' + volume.vid)
  471. # -snap LV should be unchanged
  472. self.assertEqual(self._get_lv_uuid(volume._vid_snap),
  473. orig_uuids['-snap'])
  474. self.loop.run_until_complete(volume.remove())
  475. for rev in revisions:
  476. path = '/dev/' + volume.vid + rev
  477. self.assertFalse(os.path.exists(path), path)
  478. def test_013_migration4(self):
  479. '''revisions_to_keep=0, VM started with old code, stopped with new'''
  480. config = {
  481. 'name': 'root',
  482. 'pool': self.pool.name,
  483. 'save_on_stop': True,
  484. 'rw': True,
  485. 'revisions_to_keep': 0,
  486. 'size': qubes.config.defaults['root_img_size'],
  487. }
  488. vm = qubes.tests.storage.TestVM(self)
  489. volume = self.app.get_pool(self.pool.name).init_volume(vm, config)
  490. # mock logging, to not interfere with time.time() mock
  491. volume.log = unittest.mock.Mock()
  492. # do not call volume.create(), do it manually to have old LV naming
  493. revisions = ['', '-snap']
  494. orig_uuids = {}
  495. for rev in revisions:
  496. cmd = ['create', self.pool._pool_id,
  497. volume.vid.split('/')[1] + rev, str(config['size'])]
  498. qubes_lvm(cmd)
  499. orig_uuids[rev] = self._get_lv_uuid(volume.vid + rev)
  500. qubes.storage.lvm.reset_cache()
  501. path_snap = '/dev/' + volume._vid_snap
  502. self.assertTrue(os.path.exists(path_snap), path_snap)
  503. expected_revisions = {}
  504. self.assertEqual(volume.revisions, expected_revisions)
  505. self.assertEqual(volume.path, '/dev/' + volume.vid)
  506. self.assertTrue(volume.is_dirty())
  507. with unittest.mock.patch('time.time') as mock_time:
  508. mock_time.side_effect = ('1521065906', '1521065907')
  509. self.loop.run_until_complete(volume.stop())
  510. expected_revisions = {}
  511. self.assertEqual(volume.revisions, expected_revisions)
  512. self.assertEqual(volume.path, '/dev/' + volume.vid)
  513. self.loop.run_until_complete(volume.remove())
  514. for rev in revisions:
  515. path = '/dev/' + volume.vid + rev
  516. self.assertFalse(os.path.exists(path), path)
  517. def test_014_commit_keep_0(self):
  518. ''' Test volume changes commit, with revisions_to_keep=0'''
  519. config = {
  520. 'name': 'root',
  521. 'pool': self.pool.name,
  522. 'save_on_stop': True,
  523. 'rw': True,
  524. 'revisions_to_keep': 0,
  525. 'size': qubes.config.defaults['root_img_size'],
  526. }
  527. vm = qubes.tests.storage.TestVM(self)
  528. volume = self.app.get_pool(self.pool.name).init_volume(vm, config)
  529. # mock logging, to not interfere with time.time() mock
  530. volume.log = unittest.mock.Mock()
  531. self.loop.run_until_complete(volume.create())
  532. self.assertFalse(volume.is_dirty())
  533. path = volume.path
  534. expected_revisions = {}
  535. self.assertEqual(volume.revisions, expected_revisions)
  536. self.loop.run_until_complete(volume.start())
  537. self.assertEqual(volume.revisions, expected_revisions)
  538. path_snap = '/dev/' + volume._vid_snap
  539. snap_uuid = self._get_lv_uuid(path_snap)
  540. self.assertTrue(volume.is_dirty())
  541. self.assertEqual(volume.path, path)
  542. with unittest.mock.patch('time.time') as mock_time:
  543. mock_time.side_effect = [521065906]
  544. self.loop.run_until_complete(volume.stop())
  545. self.assertFalse(volume.is_dirty())
  546. self.assertEqual(volume.revisions, {})
  547. self.assertEqual(volume.path, '/dev/' + volume.vid)
  548. self.assertEqual(snap_uuid, self._get_lv_uuid(volume.path))
  549. self.assertFalse(os.path.exists(path_snap), path_snap)
  550. self.loop.run_until_complete(volume.remove())
  551. def test_020_revert_last(self):
  552. ''' Test volume revert'''
  553. config = {
  554. 'name': 'root',
  555. 'pool': self.pool.name,
  556. 'save_on_stop': True,
  557. 'rw': True,
  558. 'revisions_to_keep': 2,
  559. 'size': qubes.config.defaults['root_img_size'],
  560. }
  561. vm = qubes.tests.storage.TestVM(self)
  562. volume = self.app.get_pool(self.pool.name).init_volume(vm, config)
  563. # mock logging, to not interfere with time.time() mock
  564. volume.log = unittest.mock.Mock()
  565. self.loop.run_until_complete(volume.create())
  566. self.loop.run_until_complete(volume.start())
  567. with unittest.mock.patch('time.time') as mock_time:
  568. mock_time.side_effect = [521065906]
  569. self.loop.run_until_complete(volume.stop())
  570. self.loop.run_until_complete(volume.start())
  571. with unittest.mock.patch('time.time') as mock_time:
  572. mock_time.side_effect = [521065907]
  573. self.loop.run_until_complete(volume.stop())
  574. self.assertEqual(len(volume.revisions), 2)
  575. revisions = volume.revisions
  576. revision_id = max(revisions.keys())
  577. current_path = volume.path
  578. current_uuid = self._get_lv_uuid(volume.path)
  579. rev_uuid = self._get_lv_uuid(volume.vid + '-' + revision_id)
  580. self.assertFalse(volume.is_dirty())
  581. self.assertNotEqual(current_uuid, rev_uuid)
  582. self.loop.run_until_complete(volume.revert())
  583. path_snap = '/dev/' + volume._vid_snap
  584. self.assertFalse(os.path.exists(path_snap), path_snap)
  585. self.assertEqual(current_path, volume.path)
  586. new_uuid = self._get_lv_origin_uuid(volume.path)
  587. self.assertEqual(new_uuid, rev_uuid)
  588. self.assertEqual(volume.revisions, revisions)
  589. self.loop.run_until_complete(volume.remove())
  590. def test_021_revert_earlier(self):
  591. ''' Test volume revert'''
  592. config = {
  593. 'name': 'root',
  594. 'pool': self.pool.name,
  595. 'save_on_stop': True,
  596. 'rw': True,
  597. 'revisions_to_keep': 2,
  598. 'size': qubes.config.defaults['root_img_size'],
  599. }
  600. vm = qubes.tests.storage.TestVM(self)
  601. volume = self.app.get_pool(self.pool.name).init_volume(vm, config)
  602. # mock logging, to not interfere with time.time() mock
  603. volume.log = unittest.mock.Mock()
  604. self.loop.run_until_complete(volume.create())
  605. self.loop.run_until_complete(volume.start())
  606. with unittest.mock.patch('time.time') as mock_time:
  607. mock_time.side_effect = [521065906]
  608. self.loop.run_until_complete(volume.stop())
  609. self.loop.run_until_complete(volume.start())
  610. with unittest.mock.patch('time.time') as mock_time:
  611. mock_time.side_effect = [521065907]
  612. self.loop.run_until_complete(volume.stop())
  613. self.assertEqual(len(volume.revisions), 2)
  614. revisions = volume.revisions
  615. revision_id = min(revisions.keys())
  616. current_path = volume.path
  617. current_uuid = self._get_lv_uuid(volume.path)
  618. rev_uuid = self._get_lv_uuid(volume.vid + '-' + revision_id)
  619. self.assertFalse(volume.is_dirty())
  620. self.assertNotEqual(current_uuid, rev_uuid)
  621. self.loop.run_until_complete(volume.revert(revision_id))
  622. path_snap = '/dev/' + volume._vid_snap
  623. self.assertFalse(os.path.exists(path_snap), path_snap)
  624. self.assertEqual(current_path, volume.path)
  625. new_uuid = self._get_lv_origin_uuid(volume.path)
  626. self.assertEqual(new_uuid, rev_uuid)
  627. self.assertEqual(volume.revisions, revisions)
  628. self.loop.run_until_complete(volume.remove())
  629. def test_030_import_data(self):
  630. ''' Test volume import'''
  631. config = {
  632. 'name': 'root',
  633. 'pool': self.pool.name,
  634. 'save_on_stop': True,
  635. 'rw': True,
  636. 'revisions_to_keep': 2,
  637. 'size': qubes.config.defaults['root_img_size'],
  638. }
  639. vm = qubes.tests.storage.TestVM(self)
  640. volume = self.app.get_pool(self.pool.name).init_volume(vm, config)
  641. self.loop.run_until_complete(volume.create())
  642. current_uuid = self._get_lv_uuid(volume.path)
  643. self.assertFalse(volume.is_dirty())
  644. import_path = self.loop.run_until_complete(
  645. volume.import_data(volume.size))
  646. import_uuid = self._get_lv_uuid(import_path)
  647. self.assertNotEqual(current_uuid, import_uuid)
  648. # success - commit data
  649. self.loop.run_until_complete(volume.import_data_end(True))
  650. new_current_uuid = self._get_lv_uuid(volume.path)
  651. self.assertEqual(new_current_uuid, import_uuid)
  652. revisions = volume.revisions
  653. self.assertEqual(len(revisions), 1)
  654. revision = revisions.popitem()[0]
  655. self.assertEqual(current_uuid,
  656. self._get_lv_uuid(volume.vid + '-' + revision))
  657. self.assertFalse(os.path.exists(import_path), import_path)
  658. self.loop.run_until_complete(volume.remove())
  659. def test_031_import_data_fail(self):
  660. ''' Test volume import'''
  661. config = {
  662. 'name': 'root',
  663. 'pool': self.pool.name,
  664. 'save_on_stop': True,
  665. 'rw': True,
  666. 'revisions_to_keep': 2,
  667. 'size': qubes.config.defaults['root_img_size'],
  668. }
  669. vm = qubes.tests.storage.TestVM(self)
  670. volume = self.app.get_pool(self.pool.name).init_volume(vm, config)
  671. self.loop.run_until_complete(volume.create())
  672. current_uuid = self._get_lv_uuid(volume.path)
  673. self.assertFalse(volume.is_dirty())
  674. import_path = self.loop.run_until_complete(
  675. volume.import_data(volume.size))
  676. import_uuid = self._get_lv_uuid(import_path)
  677. self.assertNotEqual(current_uuid, import_uuid)
  678. # fail - discard data
  679. self.loop.run_until_complete(volume.import_data_end(False))
  680. new_current_uuid = self._get_lv_uuid(volume.path)
  681. self.assertEqual(new_current_uuid, current_uuid)
  682. revisions = volume.revisions
  683. self.assertEqual(len(revisions), 0)
  684. self.assertFalse(os.path.exists(import_path), import_path)
  685. self.loop.run_until_complete(volume.remove())
  686. def test_032_import_volume_same_pool(self):
  687. '''Import volume from the same pool'''
  688. # source volume
  689. config = {
  690. 'name': 'root',
  691. 'pool': self.pool.name,
  692. 'save_on_stop': True,
  693. 'rw': True,
  694. 'revisions_to_keep': 2,
  695. 'size': qubes.config.defaults['root_img_size'],
  696. }
  697. vm = qubes.tests.storage.TestVM(self)
  698. source_volume = self.app.get_pool(self.pool.name).init_volume(vm, config)
  699. self.loop.run_until_complete(source_volume.create())
  700. source_uuid = self._get_lv_uuid(source_volume.path)
  701. # destination volume
  702. config = {
  703. 'name': 'root2',
  704. 'pool': self.pool.name,
  705. 'save_on_stop': True,
  706. 'rw': True,
  707. 'revisions_to_keep': 2,
  708. 'size': qubes.config.defaults['root_img_size'],
  709. }
  710. volume = self.app.get_pool(self.pool.name).init_volume(vm, config)
  711. volume.log = unittest.mock.Mock()
  712. with unittest.mock.patch('time.time') as mock_time:
  713. mock_time.side_effect = [1521065905]
  714. self.loop.run_until_complete(volume.create())
  715. self.assertEqual(volume.revisions, {})
  716. uuid_before = self._get_lv_uuid(volume.path)
  717. with unittest.mock.patch('time.time') as mock_time:
  718. mock_time.side_effect = [1521065906]
  719. self.loop.run_until_complete(
  720. volume.import_volume(source_volume))
  721. uuid_after = self._get_lv_uuid(volume.path)
  722. self.assertNotEqual(uuid_after, uuid_before)
  723. # also should be different than source volume (clone, not the same LV)
  724. self.assertNotEqual(uuid_after, source_uuid)
  725. self.assertEqual(self._get_lv_origin_uuid(volume.path), source_uuid)
  726. expected_revisions = {
  727. '1521065906-back': '2018-03-14T22:18:26',
  728. }
  729. self.assertEqual(volume.revisions, expected_revisions)
  730. self.loop.run_until_complete(volume.remove())
  731. self.loop.run_until_complete(source_volume.remove())
  732. def test_033_import_volume_different_pool(self):
  733. '''Import volume from a different pool'''
  734. source_volume = unittest.mock.Mock()
  735. # destination volume
  736. config = {
  737. 'name': 'root2',
  738. 'pool': self.pool.name,
  739. 'save_on_stop': True,
  740. 'rw': True,
  741. 'revisions_to_keep': 2,
  742. 'size': qubes.config.defaults['root_img_size'],
  743. }
  744. vm = qubes.tests.storage.TestVM(self)
  745. volume = self.app.get_pool(self.pool.name).init_volume(vm, config)
  746. volume.log = unittest.mock.Mock()
  747. with unittest.mock.patch('time.time') as mock_time:
  748. mock_time.side_effect = [1521065905]
  749. self.loop.run_until_complete(volume.create())
  750. self.assertEqual(volume.revisions, {})
  751. uuid_before = self._get_lv_uuid(volume.path)
  752. with tempfile.NamedTemporaryFile() as source_volume_file:
  753. source_volume_file.write(b'test-content')
  754. source_volume_file.flush()
  755. source_volume.size = 16 * 1024 * 1024 # 16MiB
  756. source_volume.export.return_value = source_volume_file.name
  757. with unittest.mock.patch('time.time') as mock_time:
  758. mock_time.side_effect = [1521065906]
  759. self.loop.run_until_complete(
  760. volume.import_volume(source_volume))
  761. uuid_after = self._get_lv_uuid(volume.path)
  762. self.assertNotEqual(uuid_after, uuid_before)
  763. self.assertEqual(volume.size, 16 * 1024 * 1024)
  764. volume_content = subprocess.check_output(['sudo', 'cat', volume.path])
  765. self.assertEqual(volume_content.rstrip(b'\0'), b'test-content')
  766. expected_revisions = {
  767. '1521065906-back': '2018-03-14T22:18:26',
  768. }
  769. self.assertEqual(volume.revisions, expected_revisions)
  770. self.loop.run_until_complete(volume.remove())
  771. def test_034_import_data_empty(self):
  772. config = {
  773. 'name': 'root',
  774. 'pool': self.pool.name,
  775. 'save_on_stop': True,
  776. 'rw': True,
  777. 'size': 1024 * 1024,
  778. }
  779. vm = qubes.tests.storage.TestVM(self)
  780. volume = self.app.get_pool(self.pool.name).init_volume(vm, config)
  781. with unittest.mock.patch('time.time') as mock_time:
  782. mock_time.side_effect = [1521065905]
  783. self.loop.run_until_complete(volume.create())
  784. p = self.loop.run_until_complete(asyncio.create_subprocess_exec(
  785. 'sudo', 'dd', 'if=/dev/urandom', 'of=' + volume.path, 'count=1', 'bs=1M'
  786. ))
  787. self.loop.run_until_complete(p.wait())
  788. import_path = self.loop.run_until_complete(
  789. volume.import_data(volume.size))
  790. self.assertNotEqual(volume.path, import_path)
  791. p = self.loop.run_until_complete(asyncio.create_subprocess_exec(
  792. 'sudo', 'touch', import_path))
  793. self.loop.run_until_complete(p.wait())
  794. self.loop.run_until_complete(volume.import_data_end(True))
  795. self.assertFalse(os.path.exists(import_path), import_path)
  796. p = self.loop.run_until_complete(asyncio.create_subprocess_exec(
  797. 'sudo', 'cat', volume.path,
  798. stdout=subprocess.PIPE
  799. ))
  800. volume_data, _ = self.loop.run_until_complete(p.communicate())
  801. self.assertEqual(volume_data.strip(b'\0'), b'')
  802. def test_035_import_data_new_size(self):
  803. ''' Test volume import'''
  804. config = {
  805. 'name': 'root',
  806. 'pool': self.pool.name,
  807. 'save_on_stop': True,
  808. 'rw': True,
  809. 'revisions_to_keep': 2,
  810. 'size': qubes.config.defaults['root_img_size'],
  811. }
  812. new_size = 2 * qubes.config.defaults['root_img_size']
  813. vm = qubes.tests.storage.TestVM(self)
  814. volume = self.app.get_pool(self.pool.name).init_volume(vm, config)
  815. self.loop.run_until_complete(volume.create())
  816. self.loop.run_until_complete(
  817. volume.import_data(new_size))
  818. self.loop.run_until_complete(volume.import_data_end(True))
  819. self.assertEqual(volume.size, new_size)
  820. self.loop.run_until_complete(volume.remove())
  821. def test_040_volatile(self):
  822. '''Volatile volume test'''
  823. config = {
  824. 'name': 'volatile',
  825. 'pool': self.pool.name,
  826. 'rw': True,
  827. 'size': qubes.config.defaults['root_img_size'],
  828. }
  829. vm = qubes.tests.storage.TestVM(self)
  830. volume = self.app.get_pool(self.pool.name).init_volume(vm, config)
  831. # volatile volume don't need any file, verify should succeed
  832. self.assertTrue(volume.verify())
  833. self.loop.run_until_complete(volume.create())
  834. self.assertTrue(volume.verify())
  835. self.assertFalse(volume.save_on_stop)
  836. self.assertFalse(volume.snap_on_start)
  837. path = volume.path
  838. self.assertEqual(path, '/dev/' + volume.vid)
  839. self.assertFalse(os.path.exists(path))
  840. self.loop.run_until_complete(volume.start())
  841. self.assertTrue(os.path.exists(path))
  842. vol_uuid = self._get_lv_uuid(path)
  843. self.loop.run_until_complete(volume.start())
  844. self.assertTrue(os.path.exists(path))
  845. vol_uuid2 = self._get_lv_uuid(path)
  846. self.assertNotEqual(vol_uuid, vol_uuid2)
  847. self.loop.run_until_complete(volume.stop())
  848. self.assertFalse(os.path.exists(path))
  849. def test_050_snapshot_volume(self):
  850. ''' Test snapshot volume creation '''
  851. config_origin = {
  852. 'name': 'root',
  853. 'pool': self.pool.name,
  854. 'save_on_stop': True,
  855. 'rw': True,
  856. 'size': qubes.config.defaults['root_img_size'],
  857. }
  858. vm = qubes.tests.storage.TestVM(self)
  859. volume_origin = self.app.get_pool(self.pool.name).init_volume(
  860. vm, config_origin)
  861. self.loop.run_until_complete(volume_origin.create())
  862. config_snapshot = {
  863. 'name': 'root2',
  864. 'pool': self.pool.name,
  865. 'snap_on_start': True,
  866. 'source': volume_origin,
  867. 'rw': True,
  868. 'size': qubes.config.defaults['root_img_size'],
  869. }
  870. volume = self.app.get_pool(self.pool.name).init_volume(
  871. vm, config_snapshot)
  872. self.assertIsInstance(volume, ThinVolume)
  873. self.assertEqual(volume.name, 'root2')
  874. self.assertEqual(volume.pool, self.pool.name)
  875. self.assertEqual(volume.size, qubes.config.defaults['root_img_size'])
  876. # only origin volume really needs to exist, verify should succeed
  877. # even before create
  878. self.assertTrue(volume.verify())
  879. self.loop.run_until_complete(volume.create())
  880. path = volume.path
  881. self.assertEqual(path, '/dev/' + volume.vid)
  882. self.assertFalse(os.path.exists(path), path)
  883. self.loop.run_until_complete(volume.start())
  884. # snapshot volume isn't considered dirty at any time
  885. self.assertFalse(volume.is_dirty())
  886. # not outdated yet
  887. self.assertFalse(volume.is_outdated())
  888. origin_uuid = self._get_lv_uuid(volume_origin.path)
  889. snap_origin_uuid = self._get_lv_origin_uuid(volume._vid_snap)
  890. self.assertEqual(origin_uuid, snap_origin_uuid)
  891. # now make it outdated
  892. self.loop.run_until_complete(volume_origin.start())
  893. self.loop.run_until_complete(volume_origin.stop())
  894. self.assertTrue(volume.is_outdated())
  895. origin_uuid = self._get_lv_uuid(volume_origin.path)
  896. self.assertNotEqual(origin_uuid, snap_origin_uuid)
  897. self.loop.run_until_complete(volume.stop())
  898. # stopped volume is never outdated
  899. self.assertFalse(volume.is_outdated())
  900. path = volume.path
  901. self.assertFalse(os.path.exists(path), path)
  902. path = '/dev/' + volume._vid_snap
  903. self.assertFalse(os.path.exists(path), path)
  904. self.loop.run_until_complete(volume.remove())
  905. self.loop.run_until_complete(volume_origin.remove())
  906. def test_100_pool_list_volumes(self):
  907. config = {
  908. 'name': 'root',
  909. 'pool': self.pool.name,
  910. 'save_on_stop': True,
  911. 'rw': True,
  912. 'revisions_to_keep': 2,
  913. 'size': qubes.config.defaults['root_img_size'],
  914. }
  915. config2 = config.copy()
  916. vm = qubes.tests.storage.TestVM(self)
  917. volume1 = self.app.get_pool(self.pool.name).init_volume(vm, config)
  918. self.loop.run_until_complete(volume1.create())
  919. config2['name'] = 'private'
  920. volume2 = self.app.get_pool(self.pool.name).init_volume(vm, config2)
  921. self.loop.run_until_complete(volume2.create())
  922. # create some revisions
  923. self.loop.run_until_complete(volume1.start())
  924. self.loop.run_until_complete(volume1.stop())
  925. # and have one in dirty state
  926. self.loop.run_until_complete(volume2.start())
  927. self.assertIn(volume1, list(self.pool.volumes))
  928. self.assertIn(volume2, list(self.pool.volumes))
  929. self.loop.run_until_complete(volume1.remove())
  930. self.assertNotIn(volume1, list(self.pool.volumes))
  931. self.assertIn(volume2, list(self.pool.volumes))
  932. self.loop.run_until_complete(volume2.remove())
  933. self.assertNotIn(volume1, list(self.pool.volumes))
  934. self.assertNotIn(volume1, list(self.pool.volumes))
  935. def test_110_metadata_size(self):
  936. with self.assertNotRaises(NotImplementedError):
  937. usage = self.pool.usage_details
  938. metadata_size = usage['metadata_size']
  939. environ = os.environ.copy()
  940. environ['LC_ALL'] = 'C.utf8'
  941. pool_size = subprocess.check_output(['sudo', 'lvs', '--noheadings',
  942. '-o', 'lv_metadata_size',
  943. '--units', 'b',
  944. self.pool.volume_group + '/' + self.pool.thin_pool],
  945. env=environ)
  946. self.assertEqual(metadata_size, int(pool_size.strip()[:-1]))
  947. def test_111_metadata_usage(self):
  948. with self.assertNotRaises(NotImplementedError):
  949. usage = self.pool.usage_details
  950. metadata_usage = usage['metadata_usage']
  951. environ = os.environ.copy()
  952. environ['LC_ALL'] = 'C.utf8'
  953. pool_info = subprocess.check_output(['sudo', 'lvs', '--noheadings',
  954. '-o', 'lv_metadata_size,metadata_percent',
  955. '--units', 'b', self.pool.volume_group + '/' + self.pool.thin_pool],
  956. env=environ)
  957. pool_size, pool_usage = pool_info.strip().split()
  958. pool_size = int(pool_size[:-1])
  959. pool_usage = float(pool_usage)
  960. self.assertEqual(metadata_usage, int(pool_size * pool_usage / 100))
  961. @skipUnlessLvmPoolExists
  962. class TC_01_ThinPool(ThinPoolBase, qubes.tests.SystemTestCase):
  963. ''' Sanity tests for :py:class:`qubes.storage.lvm.ThinPool` '''
  964. def setUp(self):
  965. super(TC_01_ThinPool, self).setUp()
  966. self.init_default_template()
  967. def test_004_import(self):
  968. template_vm = self.app.default_template
  969. name = self.make_vm_name('import')
  970. vm = self.app.add_new_vm(qubes.vm.templatevm.TemplateVM, name=name,
  971. label='red')
  972. vm.clone_properties(template_vm)
  973. self.loop.run_until_complete(
  974. vm.clone_disk_files(template_vm, pool=self.pool.name))
  975. for v_name, volume in vm.volumes.items():
  976. if volume.save_on_stop:
  977. expected = "/dev/{!s}/vm-{!s}-{!s}".format(
  978. DEFAULT_LVM_POOL.split('/')[0], vm.name, v_name)
  979. self.assertEqual(volume.path, expected)
  980. with self.assertNotRaises(qubes.exc.QubesException):
  981. self.loop.run_until_complete(vm.start())
  982. def test_005_create_appvm(self):
  983. vm = self.app.add_new_vm(cls=qubes.vm.appvm.AppVM,
  984. name=self.make_vm_name('appvm'), label='red')
  985. self.loop.run_until_complete(vm.create_on_disk(pool=self.pool.name))
  986. for v_name, volume in vm.volumes.items():
  987. if volume.save_on_stop:
  988. expected = "/dev/{!s}/vm-{!s}-{!s}".format(
  989. DEFAULT_LVM_POOL.split('/')[0], vm.name, v_name)
  990. self.assertEqual(volume.path, expected)
  991. with self.assertNotRaises(qubes.exc.QubesException):
  992. self.loop.run_until_complete(vm.start())
  993. def test_006_name_suffix_parse(self):
  994. '''Regression test for #4680'''
  995. vm1 = self.app.add_new_vm(cls=qubes.vm.appvm.AppVM,
  996. name=self.make_vm_name('appvm'), label='red')
  997. vm2 = self.app.add_new_vm(cls=qubes.vm.appvm.AppVM,
  998. name=self.make_vm_name('appvm-root'), label='red')
  999. self.loop.run_until_complete(asyncio.wait([
  1000. vm1.create_on_disk(pool=self.pool.name),
  1001. vm2.create_on_disk(pool=self.pool.name)]))
  1002. self.loop.run_until_complete(vm2.start())
  1003. self.loop.run_until_complete(vm2.shutdown(wait=True))
  1004. with self.assertNotRaises(ValueError):
  1005. vm1.volumes['root'].size
  1006. @skipUnlessLvmPoolExists
  1007. class TC_02_StorageHelpers(ThinPoolBase):
  1008. def setUp(self):
  1009. xml_path = '/tmp/qubes-test.xml'
  1010. self.app = qubes.Qubes.create_empty_store(store=xml_path,
  1011. clockvm=None,
  1012. updatevm=None,
  1013. offline_mode=True,
  1014. )
  1015. os.environ['QUBES_XML_PATH'] = xml_path
  1016. super(TC_02_StorageHelpers, self).setUp()
  1017. # reset cache
  1018. qubes.storage.DirectoryThinPool._thin_pool = {}
  1019. self.thin_dir = tempfile.TemporaryDirectory()
  1020. subprocess.check_call(
  1021. ['sudo', 'lvcreate', '-q', '-V', '32M',
  1022. '-T', DEFAULT_LVM_POOL, '-n',
  1023. 'test-file-pool'], stdout=subprocess.DEVNULL)
  1024. self.thin_dev = '/dev/{}/test-file-pool'.format(
  1025. DEFAULT_LVM_POOL.split('/')[0])
  1026. subprocess.check_call(['udevadm', 'settle'])
  1027. subprocess.check_call(
  1028. ['sudo', 'mkfs.ext4', '-q', self.thin_dev])
  1029. subprocess.check_call(['sudo', 'mount', self.thin_dev,
  1030. self.thin_dir.name])
  1031. subprocess.check_call(['sudo', 'chmod', '777',
  1032. self.thin_dir.name])
  1033. def tearDown(self):
  1034. subprocess.check_call(['sudo', 'umount', self.thin_dir.name])
  1035. subprocess.check_call(
  1036. ['sudo', 'lvremove', '-q', '-f', self.thin_dev],
  1037. stdout = subprocess.DEVNULL)
  1038. self.thin_dir.cleanup()
  1039. super(TC_02_StorageHelpers, self).tearDown()
  1040. os.unlink(self.app.store)
  1041. del self.app
  1042. for attr in dir(self):
  1043. if isinstance(getattr(self, attr), qubes.vm.BaseVM):
  1044. delattr(self, attr)
  1045. def test_000_search_thin_pool(self):
  1046. pool = qubes.storage.search_pool_containing_dir(
  1047. self.app.pools.values(), self.thin_dir.name)
  1048. self.assertEqual(pool, self.pool)
  1049. def test_001_search_none(self):
  1050. pool = qubes.storage.search_pool_containing_dir(
  1051. self.app.pools.values(), '/tmp')
  1052. self.assertIsNone(pool)
  1053. def test_002_search_subdir(self):
  1054. subdir = os.path.join(self.thin_dir.name, 'some-dir')
  1055. os.mkdir(subdir)
  1056. pool = qubes.storage.search_pool_containing_dir(
  1057. self.app.pools.values(), subdir)
  1058. self.assertEqual(pool, self.pool)
  1059. def test_003_search_file_pool(self):
  1060. subdir = os.path.join(self.thin_dir.name, 'some-dir')
  1061. file_pool_config = {
  1062. 'name': 'test-file-pool',
  1063. 'driver': 'file',
  1064. 'dir_path': subdir
  1065. }
  1066. pool2 = self.loop.run_until_complete(
  1067. self.app.add_pool(**file_pool_config))
  1068. pool = qubes.storage.search_pool_containing_dir(
  1069. self.app.pools.values(), subdir)
  1070. self.assertEqual(pool, pool2)
  1071. pool = qubes.storage.search_pool_containing_dir(
  1072. self.app.pools.values(), self.thin_dir.name)
  1073. self.assertEqual(pool, self.pool)