backupcompatibility.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. #
  2. # The Qubes OS Project, http://www.qubes-os.org
  3. #
  4. # Copyright (C) 2014 Marek Marczykowski-Górecki <marmarek@invisiblethingslab.com>
  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. from multiprocessing import Queue
  20. import asyncio
  21. import os
  22. import shutil
  23. import subprocess
  24. import unittest
  25. import sys
  26. import re
  27. import qubes.tests
  28. import qubes.tests.integ.backup
  29. QUBESXML_R2 = '''
  30. <QubesVmCollection updatevm="3" default_kernel="3.7.6-2" default_netvm="3" default_fw_netvm="2" default_template="1" clockvm="2">
  31. <QubesTemplateVm installed_by_rpm="True" kernel="3.7.6-2" uses_default_kernelopts="True" qid="1" include_in_backups="True" uses_default_kernel="True" qrexec_timeout="60" internal="False" conf_file="fedora-20-x64.conf" label="black" template_qid="none" kernelopts="" memory="400" default_user="user" netvm_qid="3" uses_default_netvm="True" volatile_img="volatile.img" services="{ 'meminfo-writer': True}" maxmem="1535" pcidevs="[]" name="fedora-20-x64" private_img="private.img" vcpus="2" root_img="root.img" debug="False" dir_path="/var/lib/qubes/vm-templates/fedora-20-x64"/>
  32. <QubesNetVm installed_by_rpm="False" kernel="3.7.6-2" uses_default_kernelopts="True" qid="2" include_in_backups="True" uses_default_kernel="True" qrexec_timeout="60" internal="False" conf_file="netvm.conf" label="red" template_qid="1" kernelopts="iommu=soft swiotlb=4096" memory="200" default_user="user" volatile_img="volatile.img" services="{'ntpd': False, 'meminfo-writer': False}" maxmem="1535" pcidevs="['02:00.0', '03:00.0']" name="netvm" netid="1" private_img="private.img" vcpus="2" root_img="root.img" debug="False" dir_path="/var/lib/qubes/servicevms/netvm"/>
  33. <QubesProxyVm installed_by_rpm="False" kernel="3.7.6-2" uses_default_kernelopts="True" qid="3" include_in_backups="True" uses_default_kernel="True" qrexec_timeout="60" internal="False" conf_file="firewallvm.conf" label="green" template_qid="1" kernelopts="" memory="200" default_user="user" netvm_qid="2" volatile_img="volatile.img" services="{'meminfo-writer': True}" maxmem="1535" pcidevs="[]" name="firewallvm" netid="2" private_img="private.img" vcpus="2" root_img="root.img" debug="False" dir_path="/var/lib/qubes/servicevms/firewallvm"/>
  34. <QubesAppVm installed_by_rpm="False" kernel="3.7.6-2" uses_default_kernelopts="True" qid="4" include_in_backups="True" uses_default_kernel="True" qrexec_timeout="60" internal="True" conf_file="fedora-20-x64-dvm.conf" label="gray" template_qid="1" kernelopts="" memory="400" default_user="user" netvm_qid="3" uses_default_netvm="True" volatile_img="volatile.img" services="{ 'meminfo-writer': True}" maxmem="1535" pcidevs="[]" name="fedora-20-x64-dvm" private_img="private.img" vcpus="1" root_img="root.img" debug="False" dir_path="/var/lib/qubes/appvms/fedora-20-x64-dvm"/>
  35. <QubesAppVm backup_content="True" backup_path="appvms/test-work" installed_by_rpm="False" kernel="3.7.6-2" uses_default_kernelopts="True" qid="5" include_in_backups="True" uses_default_kernel="True" qrexec_timeout="60" internal="False" conf_file="test-work.conf" label="green" template_qid="1" kernelopts="" memory="400" default_user="user" netvm_qid="3" uses_default_netvm="True" volatile_img="volatile.img" services="{'meminfo-writer': True}" maxmem="1535" pcidevs="[]" name="test-work" private_img="private.img" vcpus="2" root_img="root.img" debug="False" dir_path="/var/lib/qubes/appvms/test-work"/>
  36. <QubesAppVm installed_by_rpm="False" kernel="3.7.6-2" uses_default_kernelopts="True" qid="6" include_in_backups="True" uses_default_kernel="True" qrexec_timeout="60" internal="False" conf_file="banking.conf" label="green" template_qid="1" kernelopts="" memory="400" default_user="user" netvm_qid="3" uses_default_netvm="True" volatile_img="volatile.img" services="{'meminfo-writer': True}" maxmem="1535" pcidevs="[]" name="banking" private_img="private.img" vcpus="2" root_img="root.img" debug="False" dir_path="/var/lib/qubes/appvms/banking"/>
  37. <QubesAppVm installed_by_rpm="False" kernel="3.7.6-2" uses_default_kernelopts="True" qid="7" include_in_backups="True" uses_default_kernel="True" qrexec_timeout="60" internal="False" conf_file="personal.conf" label="yellow" template_qid="1" kernelopts="" memory="400" default_user="user" netvm_qid="3" uses_default_netvm="True" volatile_img="volatile.img" services="{'meminfo-writer': True}" maxmem="1535" pcidevs="[]" name="personal" private_img="private.img" vcpus="2" root_img="root.img" debug="False" dir_path="/var/lib/qubes/appvms/personal"/>
  38. <QubesAppVm installed_by_rpm="False" kernel="3.7.6-2" uses_default_kernelopts="True" qid="8" include_in_backups="True" uses_default_kernel="True" qrexec_timeout="60" internal="False" conf_file="untrusted.conf" label="red" template_qid="1" kernelopts="" memory="400" default_user="user" netvm_qid="12" uses_default_netvm="False" volatile_img="volatile.img" services="{'meminfo-writer': True}" maxmem="1535" pcidevs="[]" name="untrusted" private_img="private.img" vcpus="2" root_img="root.img" debug="False" dir_path="/var/lib/qubes/appvms/untrusted"/>
  39. <QubesTemplateVm backup_size="104857600" backup_content="True" backup_path="vm-templates/test-template-clone" installed_by_rpm="False" kernel="3.7.6-2" uses_default_kernelopts="True" qid="9" include_in_backups="True" uses_default_kernel="True" qrexec_timeout="60" internal="False" conf_file="test-template-clone.conf" label="green" template_qid="none" kernelopts="" memory="400" default_user="user" netvm_qid="3" uses_default_netvm="True" volatile_img="volatile.img" services="{'meminfo-writer': True}" maxmem="1535" pcidevs="[]" name="test-template-clone" private_img="private.img" vcpus="2" root_img="root.img" debug="False" dir_path="/var/lib/qubes/vm-templates/test-template-clone"/>
  40. <QubesAppVm backup_size="104857600" backup_content="True" backup_path="appvms/test-custom-template-appvm" installed_by_rpm="False" kernel="3.7.6-2" uses_default_kernelopts="True" qid="10" include_in_backups="True" uses_default_kernel="True" qrexec_timeout="60" internal="False" conf_file="test-custom-template-appvm.conf" label="yellow" template_qid="9" kernelopts="" memory="400" default_user="user" netvm_qid="3" uses_default_netvm="True" volatile_img="volatile.img" services="{'meminfo-writer': True}" maxmem="1535" pcidevs="[]" name="test-custom-template-appvm" private_img="private.img" vcpus="2" root_img="root.img" debug="False" dir_path="/var/lib/qubes/appvms/test-custom-template-appvm"/>
  41. <QubesAppVm backup_size="104857600" backup_content="True" backup_path="appvms/test-standalonevm" installed_by_rpm="False" kernel="3.7.6-2" uses_default_kernelopts="True" qid="11" include_in_backups="True" uses_default_kernel="True" qrexec_timeout="60" internal="False" conf_file="test-standalonevm.conf" label="blue" template_qid="none" kernelopts="" memory="400" default_user="user" netvm_qid="3" uses_default_netvm="True" volatile_img="volatile.img" services="{'meminfo-writer': True}" maxmem="1535" pcidevs="[]" name="test-standalonevm" private_img="private.img" vcpus="2" root_img="root.img" debug="False" dir_path="/var/lib/qubes/appvms/test-standalonevm"/>
  42. <QubesProxyVm backup_size="104857600" backup_content="True" backup_path="servicevms/test-testproxy" installed_by_rpm="False" kernel="3.7.6-2" uses_default_kernelopts="True" qid="12" include_in_backups="True" uses_default_kernel="True" qrexec_timeout="60" internal="False" conf_file="test-testproxy.conf" label="red" template_qid="1" kernelopts="" memory="200" default_user="user" netvm_qid="3" volatile_img="volatile.img" services="{'meminfo-writer': True}" maxmem="1535" pcidevs="[]" name="test-testproxy" netid="3" private_img="private.img" vcpus="2" root_img="root.img" debug="False" dir_path="/var/lib/qubes/servicevms/test-testproxy"/>
  43. <QubesProxyVm installed_by_rpm="False" kernel="3.7.6-2" uses_default_kernelopts="True" qid="13" include_in_backups="True" uses_default_kernel="True" qrexec_timeout="60" internal="False" conf_file="testproxy2.conf" label="red" template_qid="9" kernelopts="" memory="200" default_user="user" netvm_qid="2" volatile_img="volatile.img" services="{'meminfo-writer': True}" maxmem="1535" pcidevs="[]" name="testproxy2" netid="4" private_img="private.img" vcpus="2" root_img="root.img" debug="False" dir_path="/var/lib/qubes/servicevms/testproxy2"/>
  44. <QubesHVm backup_size="104857600" backup_content="True" backup_path="appvms/test-testhvm" installed_by_rpm="False" netvm_qid="none" qid="14" include_in_backups="True" timezone="localtime" qrexec_timeout="60" conf_file="test-testhvm.conf" label="purple" template_qid="none" internal="False" memory="512" uses_default_netvm="True" services="{'meminfo-writer': False}" default_user="user" pcidevs="[]" name="test-testhvm" qrexec_installed="False" private_img="private.img" drive="None" vcpus="2" root_img="root.img" guiagent_installed="False" debug="False" dir_path="/var/lib/qubes/appvms/test-testhvm"/>
  45. <QubesDisposableVm dispid="50" firewall_conf="firewall.xml" label="red" name="disp50" netvm_qid="2" qid="15" template_qid="1"/>
  46. </QubesVmCollection>
  47. '''
  48. MANGLED_SUBDIRS_R2 = {
  49. "test-work": "vm5",
  50. "test-template-clone": "vm9",
  51. "test-custom-template-appvm": "vm10",
  52. "test-standalonevm": "vm11",
  53. "test-testproxy": "vm12",
  54. "test-testhvm": "vm14",
  55. }
  56. APPTEMPLATE_R2B2 = '''
  57. [Desktop Entry]
  58. Name=%VMNAME%: {name}
  59. GenericName=%VMNAME%: {name}
  60. GenericName[ca]=%VMNAME%: Navegador web
  61. GenericName[cs]=%VMNAME%: Webový prohlížeč
  62. GenericName[es]=%VMNAME%: Navegador web
  63. GenericName[fa]=%VMNAME%: مرورر اینترنتی
  64. GenericName[fi]=%VMNAME%: WWW-selain
  65. GenericName[fr]=%VMNAME%: Navigateur Web
  66. GenericName[hu]=%VMNAME%: Webböngésző
  67. GenericName[it]=%VMNAME%: Browser Web
  68. GenericName[ja]=%VMNAME%: ウェブ・ブラウザ
  69. GenericName[ko]=%VMNAME%: 웹 브라우저
  70. GenericName[nb]=%VMNAME%: Nettleser
  71. GenericName[nl]=%VMNAME%: Webbrowser
  72. GenericName[nn]=%VMNAME%: Nettlesar
  73. GenericName[no]=%VMNAME%: Nettleser
  74. GenericName[pl]=%VMNAME%: Przeglądarka WWW
  75. GenericName[pt]=%VMNAME%: Navegador Web
  76. GenericName[pt_BR]=%VMNAME%: Navegador Web
  77. GenericName[sk]=%VMNAME%: Internetový prehliadač
  78. GenericName[sv]=%VMNAME%: Webbläsare
  79. Comment={comment}
  80. Comment[ca]=Navegueu per el web
  81. Comment[cs]=Prohlížení stránek World Wide Webu
  82. Comment[de]=Im Internet surfen
  83. Comment[es]=Navegue por la web
  84. Comment[fa]=صفحات شبه جهانی اینترنت را مرور نمایید
  85. Comment[fi]=Selaa Internetin WWW-sivuja
  86. Comment[fr]=Navigue sur Internet
  87. Comment[hu]=A világháló böngészése
  88. Comment[it]=Esplora il web
  89. Comment[ja]=ウェブを閲覧します
  90. Comment[ko]=웹을 돌아 다닙니다
  91. Comment[nb]=Surf på nettet
  92. Comment[nl]=Verken het internet
  93. Comment[nn]=Surf på nettet
  94. Comment[no]=Surf på nettet
  95. Comment[pl]=Przeglądanie stron WWW
  96. Comment[pt]=Navegue na Internet
  97. Comment[pt_BR]=Navegue na Internet
  98. Comment[sk]=Prehliadanie internetu
  99. Comment[sv]=Surfa på webben
  100. Exec=qvm-run -q --tray -a %VMNAME% '{command} %u'
  101. Categories=Network;WebBrowser;
  102. X-Qubes-VmName=%VMNAME%
  103. Icon=%VMDIR%/icon.png
  104. '''
  105. BACKUP_HEADER_R2 = '''version=3
  106. hmac-algorithm=SHA512
  107. crypto-algorithm=aes-256-cbc
  108. encrypted={encrypted}
  109. compressed={compressed}
  110. compression-filter=gzip
  111. '''
  112. class TC_00_BackupCompatibility(
  113. qubes.tests.integ.backup.BackupTestsMixin, qubes.tests.SystemTestCase):
  114. def tearDown(self):
  115. self.remove_test_vms(prefix="test-")
  116. super(TC_00_BackupCompatibility, self).tearDown()
  117. def create_whitelisted_appmenus(self, filename):
  118. f = open(filename, "w")
  119. f.write("gnome-terminal.desktop\n")
  120. f.write("nautilus.desktop\n")
  121. f.write("firefox.desktop\n")
  122. f.write("mozilla-thunderbird.desktop\n")
  123. f.write("libreoffice-startcenter.desktop\n")
  124. f.close()
  125. def create_appmenus(self, dir, template, list):
  126. for name in list:
  127. f = open(os.path.join(dir, name + ".desktop"), "w")
  128. f.write(template.format(name=name, comment=name, command=name))
  129. f.close()
  130. def create_private_img(self, filename):
  131. self.create_sparse(filename, 2*2**30)
  132. subprocess.check_call(["/usr/sbin/mkfs.ext4", "-q", "-F", filename])
  133. def create_volatile_img(self, filename):
  134. self.create_sparse(filename, 11.5*2**30)
  135. # here used to be sfdisk call with "0,1024,S\n,10240,L\n" input,
  136. # but since sfdisk folks like to change command arguments in
  137. # incompatible way, have an partition table verbatim here
  138. ptable = (
  139. '\x00\x00\x00\x00\x00\x00\x00\x00\xab\x39\xd5\xd4\x00\x00\x20\x00'
  140. '\x00\x21\xaa\x82\x82\x28\x08\x00\x00\x00\x00\x00\x00\x20\xaa\x00'
  141. '\x82\x29\x15\x83\x9c\x79\x08\x00\x00\x20\x00\x00\x01\x40\x00\x00'
  142. '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
  143. '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xaa\x55'
  144. )
  145. with open(filename, 'r+') as f:
  146. f.seek(0x1b0)
  147. f.write(ptable)
  148. # TODO: mkswap
  149. def fullpath(self, name):
  150. return os.path.join(self.backupdir, name)
  151. def create_v1_files(self, r2b2=False):
  152. appmenus_list = [
  153. "firefox", "gnome-terminal", "evince", "evolution",
  154. "mozilla-thunderbird", "libreoffice-startcenter", "nautilus",
  155. "gedit", "gpk-update-viewer", "gpk-application"
  156. ]
  157. os.mkdir(self.fullpath("appvms"))
  158. os.mkdir(self.fullpath("servicevms"))
  159. os.mkdir(self.fullpath("vm-templates"))
  160. # normal AppVM
  161. os.mkdir(self.fullpath("appvms/test-work"))
  162. self.create_whitelisted_appmenus(self.fullpath(
  163. "appvms/test-work/whitelisted-appmenus.list"))
  164. os.symlink("/usr/share/qubes/icons/green.png",
  165. self.fullpath("appvms/test-work/icon.png"))
  166. self.create_private_img(self.fullpath("appvms/test-work/private.img"))
  167. # StandaloneVM
  168. os.mkdir(self.fullpath("appvms/test-standalonevm"))
  169. self.create_whitelisted_appmenus(self.fullpath(
  170. "appvms/test-standalonevm/whitelisted-appmenus.list"))
  171. os.symlink("/usr/share/qubes/icons/blue.png",
  172. self.fullpath("appvms/test-standalonevm/icon.png"))
  173. self.create_private_img(self.fullpath(
  174. "appvms/test-standalonevm/private.img"))
  175. self.create_sparse(
  176. self.fullpath("appvms/test-standalonevm/root.img"), 10*2**30)
  177. self.fill_image(self.fullpath("appvms/test-standalonevm/root.img"),
  178. 100*1024*1024, True)
  179. os.mkdir(self.fullpath("appvms/test-standalonevm/apps.templates"))
  180. self.create_appmenus(self.fullpath("appvms/test-standalonevm/apps"
  181. ".templates"),
  182. APPTEMPLATE_R2B2,
  183. appmenus_list)
  184. os.mkdir(self.fullpath("appvms/test-standalonevm/kernels"))
  185. for k_file in ["initramfs", "vmlinuz", "modules.img"]:
  186. self.fill_image(self.fullpath("appvms/test-standalonevm/kernels/"
  187. + k_file), 10*1024*1024)
  188. # VM based on custom template
  189. subprocess.check_call(
  190. ["/bin/cp", "-a", self.fullpath("appvms/test-work"),
  191. self.fullpath("appvms/test-custom-template-appvm")])
  192. # HVM
  193. if r2b2:
  194. subprocess.check_call(
  195. ["/bin/cp", "-a", self.fullpath("appvms/test-standalonevm"),
  196. self.fullpath("appvms/test-testhvm")])
  197. # ProxyVM
  198. os.mkdir(self.fullpath("servicevms/test-testproxy"))
  199. self.create_whitelisted_appmenus(self.fullpath(
  200. "servicevms/test-testproxy/whitelisted-appmenus.list"))
  201. self.create_private_img(
  202. self.fullpath("servicevms/test-testproxy/private.img"))
  203. # Custom template
  204. os.mkdir(self.fullpath("vm-templates/test-template-clone"))
  205. self.create_private_img(
  206. self.fullpath("vm-templates/test-template-clone/private.img"))
  207. self.create_sparse(self.fullpath(
  208. "vm-templates/test-template-clone/root-cow.img"), 10*2**30)
  209. self.create_sparse(self.fullpath(
  210. "vm-templates/test-template-clone/root.img"), 10*2**30)
  211. self.fill_image(self.fullpath(
  212. "vm-templates/test-template-clone/root.img"), 1*2**30, True)
  213. self.create_volatile_img(self.fullpath(
  214. "vm-templates/test-template-clone/volatile.img"))
  215. subprocess.check_call([
  216. "/bin/tar", "cS",
  217. "-f", self.fullpath(
  218. "vm-templates/test-template-clone/clean-volatile.img.tar"),
  219. "-C", self.fullpath("vm-templates/test-template-clone"),
  220. "volatile.img"])
  221. self.create_whitelisted_appmenus(self.fullpath(
  222. "vm-templates/test-template-clone/whitelisted-appmenus.list"))
  223. self.create_whitelisted_appmenus(self.fullpath(
  224. "vm-templates/test-template-clone/vm-whitelisted-appmenus.list"))
  225. if r2b2:
  226. self.create_whitelisted_appmenus(self.fullpath(
  227. "vm-templates/test-template-clone/netvm-whitelisted-appmenus"
  228. ".list"))
  229. os.symlink("/usr/share/qubes/icons/green.png",
  230. self.fullpath("vm-templates/test-template-clone/icon.png"))
  231. os.mkdir(
  232. self.fullpath("vm-templates/test-template-clone/apps.templates"))
  233. self.create_appmenus(
  234. self.fullpath("vm-templates/test-template-clone/apps.templates"),
  235. APPTEMPLATE_R2B2,
  236. appmenus_list)
  237. os.mkdir(self.fullpath("vm-templates/test-template-clone/apps"))
  238. self.create_appmenus(
  239. self.fullpath("vm-templates/test-template-clone/apps"),
  240. APPTEMPLATE_R2B2.replace("%VMNAME%", "test-template-clone")
  241. .replace("%VMDIR%", self.fullpath(
  242. "vm-templates/test-template-clone")),
  243. appmenus_list)
  244. def calculate_hmac(self, f_name, algorithm="sha512", password="qubes"):
  245. subprocess.check_call(["openssl", "dgst", "-"+algorithm, "-hmac",
  246. password],
  247. stdin=open(self.fullpath(f_name), "r"),
  248. stdout=open(self.fullpath(f_name+".hmac"), "w"))
  249. def append_backup_stream(self, f_name, stream, basedir=None):
  250. if not basedir:
  251. basedir = self.backupdir
  252. subprocess.check_call(["tar", "-cO", "--posix", "-C", basedir,
  253. f_name],
  254. stdout=stream)
  255. def handle_v3_file(self, f_name, subdir, stream, compressed=True,
  256. encrypted=True):
  257. # create inner archive
  258. tar_cmdline = ["tar", "-Pc", '--sparse',
  259. '-C', self.fullpath(os.path.dirname(f_name)),
  260. '--xform', 's:^%s:%s\\0:' % (
  261. os.path.basename(f_name),
  262. subdir),
  263. os.path.basename(f_name)
  264. ]
  265. if compressed:
  266. tar_cmdline.insert(-1, "--use-compress-program=%s" % "gzip")
  267. tar = subprocess.Popen(tar_cmdline, stdout=subprocess.PIPE)
  268. if encrypted:
  269. encryptor = subprocess.Popen(
  270. ["openssl", "enc", "-e", "-aes-256-cbc", "-pass", "pass:qubes"],
  271. stdin=tar.stdout,
  272. stdout=subprocess.PIPE)
  273. data = encryptor.stdout
  274. else:
  275. data = tar.stdout
  276. stage1_dir = self.fullpath(os.path.join("stage1", subdir))
  277. if not os.path.exists(stage1_dir):
  278. os.makedirs(stage1_dir)
  279. subprocess.check_call(["split", "--numeric-suffixes",
  280. "--suffix-length=3",
  281. "--bytes="+str(100*1024*1024), "-",
  282. os.path.join(stage1_dir,
  283. os.path.basename(f_name+"."))],
  284. stdin=data)
  285. for part in sorted(os.listdir(stage1_dir)):
  286. if not re.match(
  287. r"^{}.[0-9][0-9][0-9]$".format(os.path.basename(f_name)),
  288. part):
  289. continue
  290. part_with_dir = os.path.join(subdir, part)
  291. self.calculate_hmac(os.path.join("stage1", part_with_dir))
  292. self.append_backup_stream(part_with_dir, stream,
  293. basedir=self.fullpath("stage1"))
  294. self.append_backup_stream(part_with_dir+".hmac", stream,
  295. basedir=self.fullpath("stage1"))
  296. def create_v3_backup(self, encrypted=True, compressed=True):
  297. """
  298. Create "backup format 3" backup - used in R2 and R3.0
  299. :param encrypt: Should the backup be encrypted
  300. :return:
  301. """
  302. output = open(self.fullpath("backup.bin"), "w")
  303. f = open(self.fullpath("backup-header"), "w")
  304. f.write(BACKUP_HEADER_R2.format(
  305. encrypted=str(encrypted),
  306. compressed=str(compressed)
  307. ))
  308. f.close()
  309. self.calculate_hmac("backup-header")
  310. self.append_backup_stream("backup-header", output)
  311. self.append_backup_stream("backup-header.hmac", output)
  312. f = open(self.fullpath("qubes.xml"), "w")
  313. if encrypted:
  314. qubesxml = QUBESXML_R2
  315. for vmname, subdir in MANGLED_SUBDIRS_R2.items():
  316. qubesxml = re.sub(r"[a-z-]*/{}".format(vmname),
  317. subdir, qubesxml)
  318. f.write(qubesxml)
  319. else:
  320. f.write(QUBESXML_R2)
  321. f.close()
  322. self.handle_v3_file("qubes.xml", "", output, encrypted=encrypted,
  323. compressed=compressed)
  324. self.create_v1_files(r2b2=True)
  325. for vm_type in ["appvms", "servicevms"]:
  326. for vm_name in os.listdir(self.fullpath(vm_type)):
  327. vm_dir = os.path.join(vm_type, vm_name)
  328. for f_name in os.listdir(self.fullpath(vm_dir)):
  329. if encrypted:
  330. subdir = MANGLED_SUBDIRS_R2[vm_name]
  331. else:
  332. subdir = vm_dir
  333. self.handle_v3_file(
  334. os.path.join(vm_dir, f_name),
  335. subdir+'/', output, encrypted=encrypted)
  336. for vm_name in os.listdir(self.fullpath("vm-templates")):
  337. vm_dir = os.path.join("vm-templates", vm_name)
  338. if encrypted:
  339. subdir = MANGLED_SUBDIRS_R2[vm_name]
  340. else:
  341. subdir = vm_dir
  342. self.handle_v3_file(
  343. os.path.join(vm_dir, "."),
  344. subdir+'/', output, encrypted=encrypted)
  345. output.close()
  346. def assertRestored(self, name, **kwargs):
  347. with self.assertNotRaises((KeyError, qubes.exc.QubesException)):
  348. vm = self.app.domains[name]
  349. asyncio.get_event_loop().run_until_complete(vm.storage.verify())
  350. for prop, value in kwargs.items():
  351. if prop == 'klass':
  352. self.assertIsInstance(vm, value)
  353. elif prop == 'features':
  354. self.assertEqual(dict(vm.features), value,
  355. 'VM {} - features mismatch'.format(vm.name))
  356. elif value is qubes.property.DEFAULT:
  357. self.assertTrue(vm.property_is_default(prop),
  358. 'VM {} - property {} not default'.format(vm.name, prop))
  359. else:
  360. actual_value = getattr(vm, prop)
  361. if isinstance(actual_value, qubes.vm.BaseVM):
  362. self.assertEqual(value, actual_value.name,
  363. 'VM {} - property {}'.format(vm.name, prop))
  364. elif isinstance(actual_value, qubes.Label):
  365. self.assertEqual(value, actual_value.name,
  366. 'VM {} - property {}'.format(vm.name, prop))
  367. else:
  368. self.assertEqual(value, actual_value,
  369. 'VM {} - property {}'.format(vm.name, prop))
  370. def test_210_r2(self):
  371. self.create_v3_backup(False)
  372. self.restore_backup(self.fullpath("backup.bin"), options={
  373. 'use-default-template': True,
  374. 'use-default-netvm': True,
  375. })
  376. common_props = {
  377. 'installed_by_rpm': False,
  378. 'kernel': qubes.property.DEFAULT,
  379. 'kernelopts': qubes.property.DEFAULT,
  380. 'qrexec_timeout': qubes.property.DEFAULT,
  381. 'netvm': qubes.property.DEFAULT,
  382. 'default_user': qubes.property.DEFAULT,
  383. 'include_in_backups': True,
  384. 'debug': False,
  385. 'maxmem': 1535,
  386. 'memory': 400,
  387. 'features': {
  388. 'service.meminfo-writer': '1',
  389. },
  390. }
  391. template_standalone_props = common_props.copy()
  392. template_standalone_props['features'] = {
  393. 'qrexec': '1',
  394. 'gui': '1',
  395. 'service.meminfo-writer': '1',
  396. }
  397. self.assertRestored("test-template-clone",
  398. klass=qubes.vm.templatevm.TemplateVM,
  399. label='green',
  400. provides_network=False,
  401. **template_standalone_props)
  402. testproxy_props = common_props.copy()
  403. testproxy_props.update(
  404. label='red',
  405. provides_network=True,
  406. memory=200,
  407. template=self.app.default_template.name,
  408. )
  409. self.assertRestored("test-testproxy",
  410. klass=qubes.vm.appvm.AppVM,
  411. **testproxy_props)
  412. self.assertRestored("test-work",
  413. klass=qubes.vm.appvm.AppVM,
  414. template=self.app.default_template.name,
  415. label='green',
  416. **common_props)
  417. self.assertRestored("test-standalonevm",
  418. klass=qubes.vm.standalonevm.StandaloneVM,
  419. label='blue',
  420. **template_standalone_props)
  421. self.assertRestored("test-custom-template-appvm",
  422. klass=qubes.vm.appvm.AppVM,
  423. template='test-template-clone',
  424. label='yellow',
  425. **common_props)
  426. def test_220_r2_encrypted(self):
  427. self.create_v3_backup(True)
  428. self.restore_backup(self.fullpath("backup.bin"), options={
  429. 'use-default-template': True,
  430. 'use-default-netvm': True,
  431. })
  432. common_props = {
  433. 'installed_by_rpm': False,
  434. 'kernel': qubes.property.DEFAULT,
  435. 'kernelopts': qubes.property.DEFAULT,
  436. 'qrexec_timeout': qubes.property.DEFAULT,
  437. 'netvm': qubes.property.DEFAULT,
  438. 'default_user': qubes.property.DEFAULT,
  439. 'include_in_backups': True,
  440. 'debug': False,
  441. 'maxmem': 1535, # 4063 caped by 10*400
  442. 'memory': 400,
  443. 'features': {
  444. 'service.meminfo-writer': '1',
  445. },
  446. }
  447. template_standalone_props = common_props.copy()
  448. template_standalone_props['features'] = {
  449. 'qrexec': '1',
  450. 'gui': '1',
  451. 'service.meminfo-writer': '1',
  452. }
  453. self.assertRestored("test-template-clone",
  454. klass=qubes.vm.templatevm.TemplateVM,
  455. label='green',
  456. provides_network=False,
  457. **template_standalone_props)
  458. testproxy_props = common_props.copy()
  459. testproxy_props.update(
  460. label='red',
  461. provides_network=True,
  462. memory=200,
  463. template=self.app.default_template.name,
  464. )
  465. self.assertRestored("test-testproxy",
  466. klass=qubes.vm.appvm.AppVM,
  467. **testproxy_props)
  468. self.assertRestored("test-work",
  469. klass=qubes.vm.appvm.AppVM,
  470. template=self.app.default_template.name,
  471. label='green',
  472. **common_props)
  473. self.assertRestored("test-standalonevm",
  474. klass=qubes.vm.standalonevm.StandaloneVM,
  475. label='blue',
  476. **template_standalone_props)
  477. self.assertRestored("test-custom-template-appvm",
  478. klass=qubes.vm.appvm.AppVM,
  479. template='test-template-clone',
  480. label='yellow',
  481. **common_props)
  482. class TC_01_BackupCompatibilityIntoLVM(TC_00_BackupCompatibility):
  483. def setUp(self):
  484. super(TC_01_BackupCompatibilityIntoLVM, self).setUp()
  485. self.init_lvm_pool()
  486. def restore_backup(self, source=None, appvm=None, options=None,
  487. **kwargs):
  488. if options is None:
  489. options = {}
  490. options['override_pool'] = self.pool.name
  491. super(TC_01_BackupCompatibilityIntoLVM, self).restore_backup(source,
  492. appvm, options, **kwargs)