storage_lvm.py 40 KB

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