__init__.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. # -*- encoding: utf8 -*-
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2017 Marek Marczykowski-Górecki
  6. # <marmarek@invisiblethingslab.com>
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU Lesser General Public License as published by
  10. # the Free Software Foundation; either version 2.1 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU Lesser General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public License along
  19. # with this program; if not, see <http://www.gnu.org/licenses/>.
  20. import hashlib
  21. import logging
  22. import multiprocessing
  23. import os
  24. import shutil
  25. import qubesadmin.backup.restore
  26. import qubesadmin.exc
  27. import qubesadmin.tests
  28. SIGNATURE_LEN = 512
  29. class BackupTestCase(qubesadmin.tests.QubesTestCase):
  30. class BackupErrorHandler(logging.Handler):
  31. def __init__(self, errors_queue, level=logging.NOTSET):
  32. super(BackupTestCase.BackupErrorHandler, self).__init__(level)
  33. self.errors_queue = errors_queue
  34. def emit(self, record):
  35. self.errors_queue.put(record.getMessage())
  36. def make_vm_name(self, name):
  37. try:
  38. return super(BackupTestCase, self).make_vm_name(name)
  39. except AttributeError:
  40. return 'test-' + name
  41. def setUp(self):
  42. super(BackupTestCase, self).setUp()
  43. self.error_detected = multiprocessing.Queue()
  44. self.log = logging.getLogger('qubesadmin.tests.backup')
  45. self.log.debug("Creating backupvm")
  46. self.backupdir = os.path.join(os.environ["HOME"], "test-backup")
  47. if os.path.exists(self.backupdir):
  48. shutil.rmtree(self.backupdir)
  49. os.mkdir(self.backupdir)
  50. self.error_handler = self.BackupErrorHandler(self.error_detected,
  51. level=logging.WARNING)
  52. backup_log = logging.getLogger('qubesadmin.backup')
  53. backup_log.addHandler(self.error_handler)
  54. def tearDown(self):
  55. super(BackupTestCase, self).tearDown()
  56. shutil.rmtree(self.backupdir)
  57. backup_log = logging.getLogger('qubes.backup')
  58. backup_log.removeHandler(self.error_handler)
  59. def fill_image(self, path, size=None, sparse=False, signature=b''):
  60. block_size = 4096
  61. self.log.debug("Filling %s" % path)
  62. f = open(path, 'wb+')
  63. if size is None:
  64. f.seek(0, 2)
  65. size = f.tell()
  66. f.seek(0)
  67. f.write(signature)
  68. f.write(b'\0' * (SIGNATURE_LEN - len(signature)))
  69. for block_num in range(int(size/block_size)):
  70. if sparse:
  71. f.seek(block_size, 1)
  72. f.write(b'a' * block_size)
  73. f.close()
  74. # NOTE: this was create_basic_vms
  75. def create_backup_vms(self, pool=None):
  76. template = self.app.default_template
  77. vms = []
  78. vmname = self.make_vm_name('test-net')
  79. self.log.debug("Creating %s" % vmname)
  80. testnet = self.app.add_new_vm('AppVM',
  81. name=vmname,
  82. label='red')
  83. testnet.provides_network = True
  84. testnet.create_on_disk(pool=pool)
  85. testnet.features['services/ntpd'] = True
  86. vms.append(testnet)
  87. self.fill_image(testnet.storage.export('private'), 20*1024*1024)
  88. vmname = self.make_vm_name('test1')
  89. self.log.debug("Creating %s" % vmname)
  90. testvm1 = self.app.add_new_vm('AppVM',
  91. name=vmname, template=template, label='red')
  92. testvm1.uses_default_netvm = False
  93. testvm1.netvm = testnet
  94. testvm1.create_on_disk(pool=pool)
  95. vms.append(testvm1)
  96. self.fill_image(testvm1.storage.export('private'), 100 * 1024 * 1024)
  97. vmname = self.make_vm_name('testhvm1')
  98. self.log.debug("Creating %s" % vmname)
  99. testvm2 = self.app.add_new_vm('StandaloneVM',
  100. name=vmname,
  101. label='red')
  102. testvm2.virt_mode = 'hvm'
  103. testvm2.create_on_disk(pool=pool)
  104. self.fill_image(testvm2.storage.export('root'), 1024 * 1024 * 1024, \
  105. True)
  106. vms.append(testvm2)
  107. vmname = self.make_vm_name('template')
  108. self.log.debug("Creating %s" % vmname)
  109. testvm3 = self.app.add_new_vm('TemplateVM',
  110. name=vmname, label='red')
  111. testvm3.create_on_disk(pool=pool)
  112. self.fill_image(testvm3.storage.export('root'), 100 * 1024 * 1024, True)
  113. vms.append(testvm3)
  114. vmname = self.make_vm_name('custom')
  115. self.log.debug("Creating %s" % vmname)
  116. testvm4 = self.app.add_new_vm('AppVM',
  117. name=vmname, template=testvm3, label='red')
  118. testvm4.create_on_disk(pool=pool)
  119. vms.append(testvm4)
  120. self.app.save()
  121. return vms
  122. def make_backup(self, vms, target=None, expect_failure=False, **kwargs):
  123. if target is None:
  124. target = self.backupdir
  125. try:
  126. backup = qubesadmin.backup.Backup(self.app, vms, **kwargs)
  127. except qubesadmin.exc.QubesException as e:
  128. if not expect_failure:
  129. self.fail("QubesException during backup_prepare: %s" % str(e))
  130. else:
  131. raise
  132. if 'passphrase' not in kwargs:
  133. backup.passphrase = 'qubes'
  134. backup.target_dir = target
  135. try:
  136. backup.backup_do()
  137. except qubesadmin.exc.QubesException as e:
  138. if not expect_failure:
  139. self.fail("QubesException during backup_do: %s" % str(e))
  140. else:
  141. raise
  142. def restore_backup(self, source=None, appvm=None, options=None,
  143. expect_errors=None, manipulate_restore_info=None,
  144. passphrase='qubes', force_compression_filter=None,
  145. tmpdir=None):
  146. if source is None:
  147. backupfile = os.path.join(self.backupdir,
  148. sorted(os.listdir(self.backupdir))[-1])
  149. else:
  150. backupfile = source
  151. kwargs = {}
  152. if tmpdir:
  153. kwargs['tmpdir'] = tmpdir
  154. with self.assertNotRaises(qubesadmin.exc.QubesException):
  155. restore_op = qubesadmin.backup.restore.BackupRestore(
  156. self.app, backupfile, appvm, passphrase,
  157. force_compression_filter=force_compression_filter,
  158. **kwargs)
  159. if options:
  160. for key, value in options.items():
  161. setattr(restore_op.options, key, value)
  162. restore_info = restore_op.get_restore_info()
  163. if callable(manipulate_restore_info):
  164. restore_info = manipulate_restore_info(restore_info)
  165. self.log.debug(restore_op.get_restore_summary(restore_info))
  166. with self.assertNotRaises(qubesadmin.exc.QubesException):
  167. restore_op.restore_do(restore_info)
  168. errors = []
  169. if expect_errors is None:
  170. expect_errors = []
  171. else:
  172. self.assertFalse(self.error_detected.empty(),
  173. "Restore errors expected, but none detected")
  174. while not self.error_detected.empty():
  175. current_error = self.error_detected.get()
  176. if any(map(current_error.startswith, expect_errors)):
  177. continue
  178. errors.append(current_error)
  179. self.assertTrue(len(errors) == 0,
  180. "Error(s) detected during backup_restore_do: %s" %
  181. '\n'.join(errors))
  182. if not appvm and not os.path.isdir(backupfile):
  183. os.unlink(backupfile)
  184. def create_sparse(self, path, size, signature=b''):
  185. f = open(path, "wb")
  186. f.write(signature)
  187. f.write(b'\0' * (SIGNATURE_LEN - len(signature)))
  188. f.truncate(size)
  189. f.close()
  190. def create_full_image(self, path, size, signature=b''):
  191. f = open(path, "wb")
  192. f.write(signature)
  193. f.write(b'\0' * (SIGNATURE_LEN - len(signature)))
  194. block_size = 1024 ** 2
  195. f.write(b'\0' * (block_size - SIGNATURE_LEN))
  196. for _ in range(size // block_size - 1):
  197. f.write(b'\1' * block_size)
  198. f.close()
  199. def vm_checksum(self, vms):
  200. hashes = {}
  201. for vm in vms:
  202. assert isinstance(vm, qubesadmin.vm.QubesVM)
  203. hashes[vm.name] = {}
  204. for name, volume in vm.volumes.items():
  205. if not volume.rw or not volume.save_on_stop:
  206. continue
  207. vol_path = vm.storage.get_pool(volume).export(volume)
  208. hasher = hashlib.sha1()
  209. with open(vol_path, 'rb') as afile:
  210. for buf in iter(lambda: afile.read(4096000), b''):
  211. hasher.update(buf)
  212. hashes[vm.name][name] = hasher.hexdigest()
  213. return hashes
  214. def assertCorrectlyRestored(self, orig_vms, orig_hashes):
  215. ''' Verify if restored VMs are identical to those before backup.
  216. :param orig_vms: collection of original QubesVM objects
  217. :param orig_hashes: result of :py:meth:`vm_checksum` on original VMs,
  218. before backup
  219. :return:
  220. '''
  221. for vm in orig_vms:
  222. self.assertIn(vm.name, self.app.domains)
  223. restored_vm = self.app.domains[vm.name]
  224. for prop in ('name', 'kernel',
  225. 'memory', 'maxmem', 'kernelopts',
  226. 'services', 'vcpus', 'features'
  227. 'include_in_backups', 'default_user', 'qrexec_timeout',
  228. 'autostart', 'pci_strictreset', 'debug',
  229. 'internal'):
  230. if not hasattr(vm, prop):
  231. continue
  232. self.assertEqual(
  233. getattr(vm, prop), getattr(restored_vm, prop),
  234. "VM {} - property {} not properly restored".format(
  235. vm.name, prop))
  236. for prop in ('netvm', 'template', 'label'):
  237. if not hasattr(vm, prop):
  238. continue
  239. orig_value = getattr(vm, prop)
  240. restored_value = getattr(restored_vm, prop)
  241. if orig_value and restored_value:
  242. self.assertEqual(orig_value.name, restored_value.name,
  243. "VM {} - property {} not properly restored".format(
  244. vm.name, prop))
  245. else:
  246. self.assertEqual(orig_value, restored_value,
  247. "VM {} - property {} not properly restored".format(
  248. vm.name, prop))
  249. for dev_class in vm.devices.keys():
  250. for dev in vm.devices[dev_class]:
  251. self.assertIn(dev, restored_vm.devices[dev_class],
  252. "VM {} - {} device not restored".format(
  253. vm.name, dev_class))
  254. if orig_hashes:
  255. hashes = self.vm_checksum([restored_vm])[restored_vm.name]
  256. self.assertEqual(orig_hashes[vm.name], hashes,
  257. "VM {} - disk images are not properly restored".format(
  258. vm.name))