backup.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # The Qubes OS Project, http://www.qubes-os.org
  5. #
  6. # Copyright (C) 2014 Marek Marczykowski-Górecki <marmarek@invisiblethingslab.com>
  7. #
  8. # This program is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU General Public License
  10. # as published by the Free Software Foundation; either version 2
  11. # of the License, or (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 General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program; if not, write to the Free Software
  20. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  21. #
  22. #
  23. from multiprocessing import Event, Queue
  24. import os
  25. import shutil
  26. import unittest
  27. import sys
  28. from qubes.qubes import QubesVmCollection, QubesException
  29. from qubes import backup
  30. VM_PREFIX = "test-"
  31. class BackupTests(unittest.TestCase):
  32. def setUp(self):
  33. self.error_detected = Queue()
  34. self.verbose = False
  35. self.qc = QubesVmCollection()
  36. self.qc.lock_db_for_writing()
  37. self.qc.load()
  38. if self.verbose:
  39. print >>sys.stderr, "-> Creating backupvm"
  40. self.backupvm = self.qc.add_new_vm("QubesAppVm",
  41. name="%sbackupvm" % VM_PREFIX,
  42. template=self.qc.get_default_template())
  43. self.backupvm.create_on_disk(verbose=self.verbose)
  44. self.qc.save()
  45. self.qc.unlock_db()
  46. self.backupdir = os.path.join(os.environ["HOME"], "test-backup")
  47. os.mkdir(self.backupdir)
  48. def tearDown(self):
  49. if self.backupvm.is_running():
  50. self.backupvm.force_shutdown()
  51. vmlist = [vm for vm in self.qc.values() if vm.name.startswith(
  52. VM_PREFIX)]
  53. self.remove_vms(vmlist)
  54. shutil.rmtree(self.backupdir)
  55. def print_progress(self, progress):
  56. if self.verbose:
  57. print >> sys.stderr, "\r-> Backing up files: {0}%...".format(progress)
  58. def error_callback(self, message):
  59. self.error_detected.put(message)
  60. if self.verbose:
  61. print >> sys.stderr, "ERROR: {0}".format(message)
  62. def print_callback(self, msg):
  63. if self.verbose:
  64. print msg
  65. def fill_image(self, path, size=None, sparse=False):
  66. block_size = 4096
  67. if self.verbose:
  68. print >>sys.stderr, "-> Filling %s" % path
  69. f = open(path, 'w+')
  70. if size is None:
  71. f.seek(0, 2)
  72. size = f.tell()
  73. f.seek(0)
  74. for block_num in xrange(size/block_size):
  75. f.write('a' * block_size)
  76. if sparse:
  77. f.seek(block_size, 1)
  78. f.close()
  79. def create_basic_vms(self, leave_locked=False):
  80. template=self.qc.get_default_template()
  81. vms = []
  82. self.qc.lock_db_for_writing()
  83. self.qc.load()
  84. vmname = "%stest1" % VM_PREFIX
  85. if self.verbose:
  86. print >>sys.stderr, "-> Creating %s" % vmname
  87. testvm1 = self.qc.add_new_vm("QubesAppVm",
  88. name=vmname,
  89. template=template)
  90. testvm1.create_on_disk(verbose=self.verbose)
  91. vms.append(testvm1)
  92. self.fill_image(testvm1.private_img, 100*1024*1024)
  93. vmname = "%stesthvm1" % VM_PREFIX
  94. if self.verbose:
  95. print >>sys.stderr, "-> Creating %s" % vmname
  96. testvm2 = self.qc.add_new_vm("QubesHVm",
  97. name=vmname)
  98. testvm2.create_on_disk(verbose=self.verbose)
  99. self.fill_image(testvm2.root_img, 1024*1024*1024, True)
  100. vms.append(testvm2)
  101. if not leave_locked:
  102. self.qc.save()
  103. self.qc.unlock_db()
  104. return vms
  105. def remove_vms(self, vms):
  106. self.qc.lock_db_for_writing()
  107. self.qc.load()
  108. for vm in vms:
  109. if isinstance(vm, str):
  110. vm = self.qc.get_vm_by_name(vm)
  111. else:
  112. vm = self.qc[vm.qid]
  113. if self.verbose:
  114. print >>sys.stderr, "-> Removing %s" % vm.name
  115. vm.remove_from_disk()
  116. self.qc.pop(vm.qid)
  117. self.qc.save()
  118. self.qc.unlock_db()
  119. def make_backup(self, vms, prepare_kwargs=dict(), do_kwargs=dict(),
  120. target=None):
  121. if target is None:
  122. target = self.backupdir
  123. try:
  124. files_to_backup = \
  125. backup.backup_prepare(vms,
  126. print_callback=self.print_callback,
  127. **prepare_kwargs)
  128. except QubesException as e:
  129. self.fail("QubesException during backup_prepare: %s" % str(e))
  130. try:
  131. backup.backup_do(target, files_to_backup, "qubes",
  132. progress_callback=self.print_progress,
  133. **do_kwargs)
  134. except QubesException as e:
  135. self.fail("QubesException during backup_do: %s" % str(e))
  136. def restore_backup(self, source=None, appvm=None):
  137. if source is None:
  138. backupfile = os.path.join(self.backupdir,
  139. sorted(os.listdir(self.backupdir))[-1])
  140. else:
  141. backupfile = source
  142. try:
  143. backup_info = backup.backup_restore_prepare(
  144. backupfile, "qubes", print_callback=self.print_callback,
  145. appvm=appvm)
  146. except QubesException as e: self.fail(
  147. "QubesException during backup_restore_prepare: %s" % str(e))
  148. if self.verbose:
  149. backup.backup_restore_print_summary(backup_info)
  150. try:
  151. backup.backup_restore_do(
  152. backup_info,
  153. print_callback=self.print_callback if self.verbose else None,
  154. error_callback=self.error_callback)
  155. except QubesException as e:
  156. self.fail("QubesException during backup_restore_do: %s" % str(e))
  157. errors = []
  158. while not self.error_detected.empty():
  159. errors.append(self.error_detected.get())
  160. self.assertTrue(len(errors) == 0,
  161. "Error(s) detected during backup_restore_do: %s" %
  162. '\n'.join(errors))
  163. if not appvm:
  164. os.unlink(backupfile)
  165. def test_basic_backup(self):
  166. vms = self.create_basic_vms()
  167. self.make_backup(vms)
  168. self.remove_vms(vms)
  169. self.restore_backup()
  170. self.remove_vms(vms)
  171. def test_compressed_backup(self):
  172. vms = self.create_basic_vms()
  173. self.make_backup(vms, do_kwargs={'compressed': True})
  174. self.remove_vms(vms)
  175. self.restore_backup()
  176. self.remove_vms(vms)
  177. def test_encrypted_backup(self):
  178. vms = self.create_basic_vms()
  179. self.make_backup(vms, do_kwargs={'encrypted': True})
  180. self.remove_vms(vms)
  181. self.restore_backup()
  182. self.remove_vms(vms)
  183. def test_compressed_encrypted_backup(self):
  184. vms = self.create_basic_vms()
  185. self.make_backup(vms,
  186. do_kwargs={
  187. 'compressed': True,
  188. 'encrypted': True})
  189. self.remove_vms(vms)
  190. self.restore_backup()
  191. self.remove_vms(vms)
  192. def test_send_to_vm(self):
  193. vms = self.create_basic_vms()
  194. self.backupvm.start()
  195. self.make_backup(vms,
  196. do_kwargs={
  197. 'appvm': self.backupvm,
  198. 'compressed': True,
  199. 'encrypted': True},
  200. target='dd of=/var/tmp/backup-test')
  201. self.remove_vms(vms)
  202. self.restore_backup(source='dd if=/var/tmp/backup-test',
  203. appvm=self.backupvm)
  204. self.remove_vms(vms)
  205. def test_sparse_multipart(self):
  206. vms = []
  207. self.qc.lock_db_for_writing()
  208. self.qc.load()
  209. vmname = "%stesthvm2" % VM_PREFIX
  210. if self.verbose:
  211. print >>sys.stderr, "-> Creating %s" % vmname
  212. hvmtemplate = self.qc.add_new_vm("QubesTemplateHVm",
  213. name=vmname)
  214. hvmtemplate.create_on_disk(verbose=self.verbose)
  215. self.fill_image(os.path.join(hvmtemplate.dir_path, '00file'),
  216. 195*1024*1024-4096*3)
  217. self.fill_image(hvmtemplate.private_img, 195*1024*1024-4096*3)
  218. self.fill_image(hvmtemplate.root_img, 1024*1024*1024, sparse=True)
  219. vms.append(hvmtemplate)
  220. self.qc.save()
  221. self.qc.unlock_db()
  222. self.make_backup(vms)
  223. self.remove_vms(vms)
  224. self.restore_backup()
  225. self.remove_vms(vms)