backup.py 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # The Qubes OS Project, http://www.qubes-os.org
  5. #
  6. # Copyright (C) 2013-2015 Marek Marczykowski-Górecki
  7. # <marmarek@invisiblethingslab.com>
  8. # Copyright (C) 2013 Olivier Médoc <o_medoc@yahoo.fr>
  9. #
  10. # This program is free software; you can redistribute it and/or
  11. # modify it under the terms of the GNU General Public License
  12. # as published by the Free Software Foundation; either version 2
  13. # of the License, or (at your option) any later version.
  14. #
  15. # This program is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. # GNU General Public License for more details.
  19. #
  20. # You should have received a copy of the GNU General Public License
  21. # along with this program. If not, see <http://www.gnu.org/licenses/>
  22. #
  23. #
  24. from __future__ import unicode_literals
  25. import itertools
  26. import logging
  27. from qubes.utils import size_to_human
  28. import sys
  29. import os
  30. import fcntl
  31. import subprocess
  32. import re
  33. import shutil
  34. import tempfile
  35. import time
  36. import grp
  37. import pwd
  38. import errno
  39. import datetime
  40. from multiprocessing import Queue, Process
  41. import qubes
  42. import qubes.core2migration
  43. import qubes.storage
  44. QUEUE_ERROR = "ERROR"
  45. QUEUE_FINISHED = "FINISHED"
  46. HEADER_FILENAME = 'backup-header'
  47. DEFAULT_CRYPTO_ALGORITHM = 'aes-256-cbc'
  48. DEFAULT_HMAC_ALGORITHM = 'SHA512'
  49. DEFAULT_COMPRESSION_FILTER = 'gzip'
  50. CURRENT_BACKUP_FORMAT_VERSION = '4'
  51. # Maximum size of error message get from process stderr (including VM process)
  52. MAX_STDERR_BYTES = 1024
  53. # header + qubes.xml max size
  54. HEADER_QUBES_XML_MAX_SIZE = 1024 * 1024
  55. BLKSIZE = 512
  56. _re_alphanum = re.compile(r'^[A-Za-z0-9-]*$')
  57. class BackupCanceledError(qubes.exc.QubesException):
  58. def __init__(self, msg, tmpdir=None):
  59. super(BackupCanceledError, self).__init__(msg)
  60. self.tmpdir = tmpdir
  61. class BackupHeader(object):
  62. header_keys = {
  63. 'version': 'version',
  64. 'encrypted': 'encrypted',
  65. 'compressed': 'compressed',
  66. 'compression-filter': 'compression_filter',
  67. 'crypto-algorithm': 'crypto_algorithm',
  68. 'hmac-algorithm': 'hmac_algorithm',
  69. }
  70. bool_options = ['encrypted', 'compressed']
  71. int_options = ['version']
  72. def __init__(self,
  73. header_data=None,
  74. version=None,
  75. encrypted=None,
  76. compressed=None,
  77. compression_filter=None,
  78. hmac_algorithm=None,
  79. crypto_algorithm=None):
  80. # repeat the list to help code completion...
  81. self.version = version
  82. self.encrypted = encrypted
  83. self.compressed = compressed
  84. # Options introduced in backup format 3+, which always have a header,
  85. # so no need for fallback in function parameter
  86. self.compression_filter = compression_filter
  87. self.hmac_algorithm = hmac_algorithm
  88. self.crypto_algorithm = crypto_algorithm
  89. if header_data is not None:
  90. self.load(header_data)
  91. def load(self, untrusted_header_text):
  92. """Parse backup header file.
  93. :param untrusted_header_text: header content
  94. :type untrusted_header_text: basestring
  95. .. warning::
  96. This function may be exposed to not yet verified header,
  97. so is security critical.
  98. """
  99. try:
  100. untrusted_header_text = untrusted_header_text.decode('ascii')
  101. except UnicodeDecodeError:
  102. raise qubes.exc.QubesException(
  103. "Non-ASCII characters in backup header")
  104. for untrusted_line in untrusted_header_text.splitlines():
  105. if untrusted_line.count('=') != 1:
  106. raise qubes.exc.QubesException("Invalid backup header")
  107. key, value = untrusted_line.strip().split('=', 1)
  108. if not _re_alphanum.match(key):
  109. raise qubes.exc.QubesException("Invalid backup header (key)")
  110. if key not in self.header_keys.keys():
  111. # Ignoring unknown option
  112. continue
  113. if not _re_alphanum.match(value):
  114. raise qubes.exc.QubesException("Invalid backup header (value)")
  115. if getattr(self, self.header_keys[key]) is not None:
  116. raise qubes.exc.QubesException(
  117. "Duplicated header line: {}".format(key))
  118. if key in self.bool_options:
  119. value = value.lower() in ["1", "true", "yes"]
  120. elif key in self.int_options:
  121. value = int(value)
  122. setattr(self, self.header_keys[key], value)
  123. self.validate()
  124. def validate(self):
  125. if self.version == 1:
  126. # header not really present
  127. pass
  128. elif self.version in [2, 3, 4]:
  129. expected_attrs = ['version', 'encrypted', 'compressed',
  130. 'hmac_algorithm']
  131. if self.encrypted:
  132. expected_attrs += ['crypto_algorithm']
  133. if self.version >= 3 and self.compressed:
  134. expected_attrs += ['compression_filter']
  135. for key in expected_attrs:
  136. if getattr(self, key) is None:
  137. raise qubes.exc.QubesException(
  138. "Backup header lack '{}' info".format(key))
  139. else:
  140. raise qubes.exc.QubesException(
  141. "Unsupported backup version {}".format(self.version))
  142. def save(self, filename):
  143. with open(filename, "w") as f:
  144. # make sure 'version' is the first key
  145. f.write('version={}\n'.format(self.version))
  146. for key, attr in self.header_keys.iteritems():
  147. if key == 'version':
  148. continue
  149. if getattr(self, attr) is None:
  150. continue
  151. f.write("{!s}={!s}\n".format(key, getattr(self, attr)))
  152. class SendWorker(Process):
  153. def __init__(self, queue, base_dir, backup_stdout):
  154. super(SendWorker, self).__init__()
  155. self.queue = queue
  156. self.base_dir = base_dir
  157. self.backup_stdout = backup_stdout
  158. self.log = logging.getLogger('qubes.backup')
  159. def run(self):
  160. self.log.debug("Started sending thread")
  161. self.log.debug("Moving to temporary dir".format(self.base_dir))
  162. os.chdir(self.base_dir)
  163. for filename in iter(self.queue.get, None):
  164. if filename in (QUEUE_FINISHED, QUEUE_ERROR):
  165. break
  166. self.log.debug("Sending file {}".format(filename))
  167. # This tar used for sending data out need to be as simple, as
  168. # simple, as featureless as possible. It will not be
  169. # verified before untaring.
  170. tar_final_cmd = ["tar", "-cO", "--posix",
  171. "-C", self.base_dir, filename]
  172. final_proc = subprocess.Popen(tar_final_cmd,
  173. stdin=subprocess.PIPE,
  174. stdout=self.backup_stdout)
  175. if final_proc.wait() >= 2:
  176. if self.queue.full():
  177. # if queue is already full, remove some entry to wake up
  178. # main thread, so it will be able to notice error
  179. self.queue.get()
  180. # handle only exit code 2 (tar fatal error) or
  181. # greater (call failed?)
  182. raise qubes.exc.QubesException(
  183. "ERROR: Failed to write the backup, out of disk space? "
  184. "Check console output or ~/.xsession-errors for details.")
  185. # Delete the file as we don't need it anymore
  186. self.log.debug("Removing file {}".format(filename))
  187. os.remove(filename)
  188. self.log.debug("Finished sending thread")
  189. class Backup(object):
  190. class FileToBackup(object):
  191. def __init__(self, file_path, subdir=None):
  192. sz = qubes.storage.get_disk_usage(file_path)
  193. if subdir is None:
  194. abs_file_path = os.path.abspath(file_path)
  195. abs_base_dir = os.path.abspath(
  196. qubes.config.system_path["qubes_base_dir"]) + '/'
  197. abs_file_dir = os.path.dirname(abs_file_path) + '/'
  198. (nothing, directory, subdir) = abs_file_dir.partition(abs_base_dir)
  199. assert nothing == ""
  200. assert directory == abs_base_dir
  201. else:
  202. if len(subdir) > 0 and not subdir.endswith('/'):
  203. subdir += '/'
  204. self.path = file_path
  205. self.size = sz
  206. self.subdir = subdir
  207. class VMToBackup(object):
  208. def __init__(self, vm, files, subdir):
  209. self.vm = vm
  210. self.files = files
  211. self.subdir = subdir
  212. @property
  213. def size(self):
  214. return reduce(lambda x, y: x + y.size, self.files, 0)
  215. def __init__(self, app, vms_list=None, exclude_list=None, **kwargs):
  216. """
  217. If vms = None, include all (sensible) VMs;
  218. exclude_list is always applied
  219. """
  220. super(Backup, self).__init__()
  221. #: progress of the backup - bytes handled of the current VM
  222. self.chunk_size = 100 * 1024 * 1024
  223. self._current_vm_bytes = 0
  224. #: progress of the backup - bytes handled of finished VMs
  225. self._done_vms_bytes = 0
  226. #: total backup size (set by :py:meth:`get_files_to_backup`)
  227. self.total_backup_bytes = 0
  228. #: application object
  229. self.app = app
  230. #: directory for temporary files - set after creating the directory
  231. self.tmpdir = None
  232. # Backup settings - defaults
  233. #: should the backup be encrypted?
  234. self.encrypted = True
  235. #: should the backup be compressed?
  236. self.compressed = True
  237. #: what passphrase should be used to intergrity protect (and encrypt)
  238. #: the backup; required
  239. self.passphrase = None
  240. #: custom hmac algorithm
  241. self.hmac_algorithm = DEFAULT_HMAC_ALGORITHM
  242. #: custom encryption algorithm
  243. self.crypto_algorithm = DEFAULT_CRYPTO_ALGORITHM
  244. #: custom compression filter; a program which process stdin to stdout
  245. self.compression_filter = DEFAULT_COMPRESSION_FILTER
  246. #: VM to which backup should be sent (if any)
  247. self.target_vm = None
  248. #: directory to save backup in (either in dom0 or target VM,
  249. #: depending on :py:attr:`target_vm`
  250. self.target_dir = None
  251. #: callback for progress reporting. Will be called with one argument
  252. #: - progress in percents
  253. self.progress_callback = None
  254. for key, value in kwargs.iteritems():
  255. if hasattr(self, key):
  256. setattr(self, key, value)
  257. else:
  258. raise AttributeError(key)
  259. #: whether backup was canceled
  260. self.canceled = False
  261. #: list of PIDs to kill on backup cancel
  262. self.processes_to_kill_on_cancel = []
  263. self.log = logging.getLogger('qubes.backup')
  264. # FIXME: drop this legacy feature?
  265. if isinstance(self.compressed, basestring):
  266. self.compression_filter = self.compressed
  267. self.compressed = True
  268. else:
  269. self.compression_filter = DEFAULT_COMPRESSION_FILTER
  270. if exclude_list is None:
  271. exclude_list = []
  272. if vms_list is None:
  273. vms_list = [vm for vm in app.domains if vm.include_in_backups]
  274. # Apply exclude list
  275. self.vms_for_backup = [vm for vm in vms_list
  276. if vm.name not in exclude_list]
  277. def __del__(self):
  278. if self.tmpdir and os.path.exists(self.tmpdir):
  279. shutil.rmtree(self.tmpdir)
  280. def cancel(self):
  281. """Cancel running backup operation. Can be called from another thread.
  282. """
  283. self.canceled = True
  284. for proc in self.processes_to_kill_on_cancel:
  285. try:
  286. proc.terminate()
  287. except OSError:
  288. pass
  289. def get_files_to_backup(self):
  290. files_to_backup = {}
  291. for vm in self.vms_for_backup:
  292. if vm.qid == 0:
  293. # handle dom0 later
  294. continue
  295. if self.encrypted:
  296. subdir = 'vm%d/' % vm.qid
  297. else:
  298. subdir = None
  299. vm_files = []
  300. if vm.private_img is not None:
  301. vm_files.append(self.FileToBackup(vm.private_img, subdir))
  302. vm_files.append(self.FileToBackup(vm.icon_path, subdir))
  303. vm_files.extend(self.FileToBackup(i, subdir)
  304. for i in vm.fire_event('backup-get-files'))
  305. # TODO: drop after merging firewall.xml into qubes.xml
  306. firewall_conf = os.path.join(vm.dir_path, vm.firewall_conf)
  307. if os.path.exists(firewall_conf):
  308. vm_files.append(self.FileToBackup(firewall_conf, subdir))
  309. if vm.updateable:
  310. vm_files.append(self.FileToBackup(vm.root_img, subdir))
  311. files_to_backup[vm.qid] = self.VMToBackup(vm, vm_files, subdir)
  312. # Dom0 user home
  313. if 0 in [vm.qid for vm in self.vms_for_backup]:
  314. local_user = grp.getgrnam('qubes').gr_mem[0]
  315. home_dir = pwd.getpwnam(local_user).pw_dir
  316. # Home dir should have only user-owned files, so fix it now
  317. # to prevent permissions problems - some root-owned files can
  318. # left after 'sudo bash' and similar commands
  319. subprocess.check_call(['sudo', 'chown', '-R', local_user, home_dir])
  320. home_to_backup = [
  321. self.FileToBackup(home_dir, 'dom0-home/')]
  322. vm_files = home_to_backup
  323. files_to_backup[0] = self.VMToBackup(self.app.domains[0],
  324. vm_files,
  325. os.path.join('dom0-home', os.path.basename(home_dir)))
  326. self.total_backup_bytes = reduce(
  327. lambda x, y: x + y.size, files_to_backup.values(), 0)
  328. return files_to_backup
  329. def get_backup_summary(self):
  330. summary = ""
  331. fields_to_display = [
  332. {"name": "VM", "width": 16},
  333. {"name": "type", "width": 12},
  334. {"name": "size", "width": 12}
  335. ]
  336. # Display the header
  337. for f in fields_to_display:
  338. fmt = "{{0:-^{0}}}-+".format(f["width"] + 1)
  339. summary += fmt.format('-')
  340. summary += "\n"
  341. for f in fields_to_display:
  342. fmt = "{{0:>{0}}} |".format(f["width"] + 1)
  343. summary += fmt.format(f["name"])
  344. summary += "\n"
  345. for f in fields_to_display:
  346. fmt = "{{0:-^{0}}}-+".format(f["width"] + 1)
  347. summary += fmt.format('-')
  348. summary += "\n"
  349. files_to_backup = self.get_files_to_backup()
  350. for qid, vm_info in files_to_backup.iteritems():
  351. s = ""
  352. fmt = "{{0:>{0}}} |".format(fields_to_display[0]["width"] + 1)
  353. s += fmt.format(vm_info['vm'].name)
  354. fmt = "{{0:>{0}}} |".format(fields_to_display[1]["width"] + 1)
  355. if qid == 0:
  356. s += fmt.format("User home")
  357. elif vm_info['vm'].is_template():
  358. s += fmt.format("Template VM")
  359. else:
  360. s += fmt.format("VM" + (" + Sys" if vm_info['vm'].updateable
  361. else ""))
  362. vm_size = vm_info['size']
  363. fmt = "{{0:>{0}}} |".format(fields_to_display[2]["width"] + 1)
  364. s += fmt.format(size_to_human(vm_size))
  365. if qid != 0 and vm_info['vm'].is_running():
  366. s += " <-- The VM is running, please shut it down before proceeding " \
  367. "with the backup!"
  368. summary += s + "\n"
  369. for f in fields_to_display:
  370. fmt = "{{0:-^{0}}}-+".format(f["width"] + 1)
  371. summary += fmt.format('-')
  372. summary += "\n"
  373. fmt = "{{0:>{0}}} |".format(fields_to_display[0]["width"] + 1)
  374. summary += fmt.format("Total size:")
  375. fmt = "{{0:>{0}}} |".format(
  376. fields_to_display[1]["width"] + 1 + 2 + fields_to_display[2][
  377. "width"] + 1)
  378. summary += fmt.format(size_to_human(self.total_backup_bytes))
  379. summary += "\n"
  380. for f in fields_to_display:
  381. fmt = "{{0:-^{0}}}-+".format(f["width"] + 1)
  382. summary += fmt.format('-')
  383. summary += "\n"
  384. vms_not_for_backup = [vm.name for vm in self.app.domains
  385. if vm not in self.vms_for_backup]
  386. summary += "VMs not selected for backup:\n - " + "\n - ".join(
  387. sorted(vms_not_for_backup))
  388. return summary
  389. def prepare_backup_header(self):
  390. header_file_path = os.path.join(self.tmpdir, HEADER_FILENAME)
  391. backup_header = BackupHeader(
  392. version=CURRENT_BACKUP_FORMAT_VERSION,
  393. hmac_algorithm=self.hmac_algorithm,
  394. crypto_algorithm=self.crypto_algorithm,
  395. encrypted=self.encrypted,
  396. compressed=self.compressed,
  397. compression_filter=self.compression_filter,
  398. )
  399. backup_header.save(header_file_path)
  400. hmac = subprocess.Popen(
  401. ["openssl", "dgst", "-" + self.hmac_algorithm,
  402. "-hmac", self.passphrase],
  403. stdin=open(header_file_path, "r"),
  404. stdout=open(header_file_path + ".hmac", "w"))
  405. if hmac.wait() != 0:
  406. raise qubes.exc.QubesException(
  407. "Failed to compute hmac of header file")
  408. return HEADER_FILENAME, HEADER_FILENAME + ".hmac"
  409. @staticmethod
  410. def _queue_put_with_check(proc, vmproc, queue, element):
  411. if queue.full():
  412. if not proc.is_alive():
  413. if vmproc:
  414. message = ("Failed to write the backup, VM output:\n" +
  415. vmproc.stderr.read())
  416. else:
  417. message = "Failed to write the backup. Out of disk space?"
  418. raise qubes.exc.QubesException(message)
  419. queue.put(element)
  420. def _send_progress_update(self):
  421. if callable(self.progress_callback):
  422. progress = (
  423. 100 * (self._done_vms_bytes + self._current_vm_bytes) /
  424. self.total_backup_bytes)
  425. self.progress_callback(progress)
  426. def _add_vm_progress(self, bytes_done):
  427. self._current_vm_bytes += bytes_done
  428. self._send_progress_update()
  429. def backup_do(self):
  430. if self.passphrase is None:
  431. raise qubes.exc.QubesException("No passphrase set")
  432. qubes_xml = self.app.store
  433. self.tmpdir = tempfile.mkdtemp()
  434. shutil.copy(qubes_xml, os.path.join(self.tmpdir, 'qubes.xml'))
  435. qubes_xml = os.path.join(self.tmpdir, 'qubes.xml')
  436. backup_app = qubes.Qubes(qubes_xml)
  437. # FIXME: cache it earlier?
  438. files_to_backup = self.get_files_to_backup()
  439. # make sure backup_content isn't set initially
  440. for vm in backup_app.domains:
  441. vm.features['backup-content'] = False
  442. for qid, vm_info in files_to_backup.iteritems():
  443. if qid != 0 and vm_info.vm.is_running():
  444. raise qubes.exc.QubesVMNotHaltedError(vm_info.vm)
  445. # VM is included in the backup
  446. backup_app.domains[qid].features['backup-content'] = True
  447. backup_app.domains[qid].features['backup-path'] = vm_info.subdir
  448. backup_app.domains[qid].features['backup-size'] = vm_info.size
  449. backup_app.save()
  450. passphrase = self.passphrase.encode('utf-8')
  451. vmproc = None
  452. tar_sparse = None
  453. if self.target_vm is not None:
  454. # Prepare the backup target (Qubes service call)
  455. # If APPVM, STDOUT is a PIPE
  456. vmproc = self.target_vm.run_service('qubes.Backup',
  457. passio_popen=True, passio_stderr=True)
  458. vmproc.stdin.write(self.target_dir.
  459. replace("\r", "").replace("\n", "") + "\n")
  460. backup_stdout = vmproc.stdin
  461. self.processes_to_kill_on_cancel.append(vmproc)
  462. else:
  463. # Prepare the backup target (local file)
  464. if os.path.isdir(self.target_dir):
  465. backup_target = self.target_dir + "/qubes-{0}". \
  466. format(time.strftime("%Y-%m-%dT%H%M%S"))
  467. else:
  468. backup_target = self.target_dir
  469. # Create the target directory
  470. if not os.path.exists(os.path.dirname(self.target_dir)):
  471. raise qubes.exc.QubesException(
  472. "ERROR: the backup directory for {0} does not exists".
  473. format(self.target_dir))
  474. # If not APPVM, STDOUT is a local file
  475. backup_stdout = open(backup_target, 'wb')
  476. # Tar with tape length does not deals well with stdout
  477. # (close stdout between two tapes)
  478. # For this reason, we will use named pipes instead
  479. self.log.debug("Working in {}".format(self.tmpdir))
  480. backup_pipe = os.path.join(self.tmpdir, "backup_pipe")
  481. self.log.debug("Creating pipe in: {}".format(backup_pipe))
  482. os.mkfifo(backup_pipe)
  483. self.log.debug("Will backup: {}".format(files_to_backup))
  484. header_files = self.prepare_backup_header()
  485. # Setup worker to send encrypted data chunks to the backup_target
  486. to_send = Queue(10)
  487. send_proc = SendWorker(to_send, self.tmpdir, backup_stdout)
  488. send_proc.start()
  489. for f in header_files:
  490. to_send.put(f)
  491. vm_files_to_backup = self.get_files_to_backup()
  492. qubes_xml_info = self.VMToBackup(
  493. None,
  494. [self.FileToBackup(qubes_xml, '')],
  495. ''
  496. )
  497. for vm_info in itertools.chain([qubes_xml_info],
  498. vm_files_to_backup.itervalues()):
  499. for file_info in vm_info.files:
  500. self.log.debug("Backing up {}".format(file_info))
  501. backup_tempfile = os.path.join(
  502. self.tmpdir, file_info.subdir,
  503. os.path.basename(file_info.path))
  504. self.log.debug("Using temporary location: {}".format(
  505. backup_tempfile))
  506. # Ensure the temporary directory exists
  507. if not os.path.isdir(os.path.dirname(backup_tempfile)):
  508. os.makedirs(os.path.dirname(backup_tempfile))
  509. # The first tar cmd can use any complex feature as we want.
  510. # Files will be verified before untaring this.
  511. # Prefix the path in archive with filename["subdir"] to have it
  512. # verified during untar
  513. tar_cmdline = (["tar", "-Pc", '--sparse',
  514. "-f", backup_pipe,
  515. '-C', os.path.dirname(file_info.path)] +
  516. (['--dereference'] if
  517. file_info.subdir != "dom0-home/" else []) +
  518. ['--xform', 's:^%s:%s\\0:' % (
  519. os.path.basename(file_info.path),
  520. file_info.subdir),
  521. os.path.basename(file_info.path)
  522. ])
  523. if self.compressed:
  524. tar_cmdline.insert(-1,
  525. "--use-compress-program=%s" % self.compression_filter)
  526. self.log.debug(" ".join(tar_cmdline))
  527. # Tips: Popen(bufsize=0)
  528. # Pipe: tar-sparse | encryptor [| hmac] | tar | backup_target
  529. # Pipe: tar-sparse [| hmac] | tar | backup_target
  530. # TODO: log handle stderr
  531. tar_sparse = subprocess.Popen(
  532. tar_cmdline, stdin=subprocess.PIPE)
  533. self.processes_to_kill_on_cancel.append(tar_sparse)
  534. # Wait for compressor (tar) process to finish or for any
  535. # error of other subprocesses
  536. i = 0
  537. run_error = "paused"
  538. encryptor = None
  539. if self.encrypted:
  540. # Start encrypt
  541. # If no cipher is provided,
  542. # the data is forwarded unencrypted !!!
  543. encryptor = subprocess.Popen([
  544. "openssl", "enc",
  545. "-e", "-" + self.crypto_algorithm,
  546. "-pass", "pass:" + passphrase],
  547. stdin=open(backup_pipe, 'rb'),
  548. stdout=subprocess.PIPE)
  549. pipe = encryptor.stdout
  550. else:
  551. pipe = open(backup_pipe, 'rb')
  552. while run_error == "paused":
  553. # Start HMAC
  554. hmac = subprocess.Popen([
  555. "openssl", "dgst", "-" + self.hmac_algorithm,
  556. "-hmac", passphrase],
  557. stdin=subprocess.PIPE,
  558. stdout=subprocess.PIPE)
  559. # Prepare a first chunk
  560. chunkfile = backup_tempfile + "." + "%03d" % i
  561. i += 1
  562. chunkfile_p = open(chunkfile, 'wb')
  563. common_args = {
  564. 'backup_target': chunkfile_p,
  565. 'hmac': hmac,
  566. 'vmproc': vmproc,
  567. 'addproc': tar_sparse,
  568. 'progress_callback': self._add_vm_progress,
  569. 'size_limit': self.chunk_size,
  570. }
  571. run_error = wait_backup_feedback(
  572. in_stream=pipe, streamproc=encryptor,
  573. **common_args)
  574. chunkfile_p.close()
  575. self.log.debug(
  576. "Wait_backup_feedback returned: {}".format(run_error))
  577. if self.canceled:
  578. try:
  579. tar_sparse.terminate()
  580. except OSError:
  581. pass
  582. try:
  583. hmac.terminate()
  584. except OSError:
  585. pass
  586. tar_sparse.wait()
  587. hmac.wait()
  588. to_send.put(QUEUE_ERROR)
  589. send_proc.join()
  590. shutil.rmtree(self.tmpdir)
  591. raise BackupCanceledError("Backup canceled")
  592. if run_error and run_error != "size_limit":
  593. send_proc.terminate()
  594. if run_error == "VM" and vmproc:
  595. raise qubes.exc.QubesException(
  596. "Failed to write the backup, VM output:\n" +
  597. vmproc.stderr.read(MAX_STDERR_BYTES))
  598. else:
  599. raise qubes.exc.QubesException(
  600. "Failed to perform backup: error in " +
  601. run_error)
  602. # Send the chunk to the backup target
  603. self._queue_put_with_check(
  604. send_proc, vmproc, to_send,
  605. os.path.relpath(chunkfile, self.tmpdir))
  606. # Close HMAC
  607. hmac.stdin.close()
  608. hmac.wait()
  609. self.log.debug("HMAC proc return code: {}".format(
  610. hmac.poll()))
  611. # Write HMAC data next to the chunk file
  612. hmac_data = hmac.stdout.read()
  613. self.log.debug(
  614. "Writing hmac to {}.hmac".format(chunkfile))
  615. with open(chunkfile + ".hmac", 'w') as hmac_file:
  616. hmac_file.write(hmac_data)
  617. # Send the HMAC to the backup target
  618. self._queue_put_with_check(
  619. send_proc, vmproc, to_send,
  620. os.path.relpath(chunkfile, self.tmpdir) + ".hmac")
  621. if tar_sparse.poll() is None or run_error == "size_limit":
  622. run_error = "paused"
  623. else:
  624. self.processes_to_kill_on_cancel.remove(tar_sparse)
  625. self.log.debug(
  626. "Finished tar sparse with exit code {}".format(
  627. tar_sparse.poll()))
  628. pipe.close()
  629. # This VM done, update progress
  630. self._done_vms_bytes += vm_info.size
  631. self._current_vm_bytes = 0
  632. self._send_progress_update()
  633. # Save date of last backup
  634. if vm_info.vm:
  635. vm_info.vm.backup_timestamp = datetime.datetime.now()
  636. self._queue_put_with_check(send_proc, vmproc, to_send, QUEUE_FINISHED)
  637. send_proc.join()
  638. shutil.rmtree(self.tmpdir)
  639. if self.canceled:
  640. raise BackupCanceledError("Backup canceled")
  641. if send_proc.exitcode != 0:
  642. raise qubes.exc.QubesException(
  643. "Failed to send backup: error in the sending process")
  644. if vmproc:
  645. self.log.debug("VMProc1 proc return code: {}".format(vmproc.poll()))
  646. if tar_sparse is not None:
  647. self.log.debug("Sparse1 proc return code: {}".format(
  648. tar_sparse.poll()))
  649. vmproc.stdin.close()
  650. self.app.save()
  651. def wait_backup_feedback(progress_callback, in_stream, streamproc,
  652. backup_target, hmac=None, vmproc=None,
  653. addproc=None,
  654. size_limit=None):
  655. '''
  656. Wait for backup chunk to finish
  657. - Monitor all the processes (streamproc, hmac, vmproc, addproc) for errors
  658. - Copy stdout of streamproc to backup_target and hmac stdin if available
  659. - Compute progress based on total_backup_sz and send progress to
  660. progress_callback function
  661. - Returns if
  662. - one of the monitored processes error out (streamproc, hmac, vmproc,
  663. addproc), along with the processe that failed
  664. - all of the monitored processes except vmproc finished successfully
  665. (vmproc termination is controlled by the python script)
  666. - streamproc does not delivers any data anymore (return with the error
  667. "")
  668. - size_limit is provided and is about to be exceeded
  669. '''
  670. buffer_size = 409600
  671. run_error = None
  672. run_count = 1
  673. bytes_copied = 0
  674. log = logging.getLogger('qubes.backup')
  675. while run_count > 0 and run_error is None:
  676. if size_limit and bytes_copied + buffer_size > size_limit:
  677. return "size_limit"
  678. buf = in_stream.read(buffer_size)
  679. if callable(progress_callback):
  680. progress_callback(len(buf))
  681. bytes_copied += len(buf)
  682. run_count = 0
  683. if hmac:
  684. retcode = hmac.poll()
  685. if retcode is not None:
  686. if retcode != 0:
  687. run_error = "hmac"
  688. else:
  689. run_count += 1
  690. if addproc:
  691. retcode = addproc.poll()
  692. if retcode is not None:
  693. if retcode != 0:
  694. run_error = "addproc"
  695. else:
  696. run_count += 1
  697. if vmproc:
  698. retcode = vmproc.poll()
  699. if retcode is not None:
  700. if retcode != 0:
  701. run_error = "VM"
  702. log.debug(vmproc.stdout.read())
  703. else:
  704. # VM should run until the end
  705. pass
  706. if streamproc:
  707. retcode = streamproc.poll()
  708. if retcode is not None:
  709. if retcode != 0:
  710. run_error = "streamproc"
  711. break
  712. elif retcode == 0 and len(buf) <= 0:
  713. return ""
  714. run_count += 1
  715. else:
  716. if len(buf) <= 0:
  717. return ""
  718. try:
  719. backup_target.write(buf)
  720. except IOError as e:
  721. if e.errno == errno.EPIPE:
  722. run_error = "target"
  723. else:
  724. raise
  725. if hmac:
  726. hmac.stdin.write(buf)
  727. return run_error
  728. class ExtractWorker2(Process):
  729. def __init__(self, queue, base_dir, passphrase, encrypted,
  730. progress_callback, vmproc=None,
  731. compressed=False, crypto_algorithm=DEFAULT_CRYPTO_ALGORITHM,
  732. verify_only=False):
  733. super(ExtractWorker2, self).__init__()
  734. self.queue = queue
  735. self.base_dir = base_dir
  736. self.passphrase = passphrase
  737. self.encrypted = encrypted
  738. self.compressed = compressed
  739. self.crypto_algorithm = crypto_algorithm
  740. self.verify_only = verify_only
  741. self.blocks_backedup = 0
  742. self.tar2_process = None
  743. self.tar2_current_file = None
  744. self.decompressor_process = None
  745. self.decryptor_process = None
  746. self.progress_callback = progress_callback
  747. self.vmproc = vmproc
  748. self.restore_pipe = os.path.join(self.base_dir, "restore_pipe")
  749. self.log = logging.getLogger('qubes.backup.extract')
  750. self.log.debug("Creating pipe in: {}".format(self.restore_pipe))
  751. os.mkfifo(self.restore_pipe)
  752. self.stderr_encoding = sys.stderr.encoding or 'utf-8'
  753. def collect_tar_output(self):
  754. if not self.tar2_process.stderr:
  755. return
  756. if self.tar2_process.poll() is None:
  757. try:
  758. new_lines = self.tar2_process.stderr \
  759. .read(MAX_STDERR_BYTES).splitlines()
  760. except IOError as e:
  761. if e.errno == errno.EAGAIN:
  762. return
  763. else:
  764. raise
  765. else:
  766. new_lines = self.tar2_process.stderr.readlines()
  767. new_lines = map(lambda x: x.decode(self.stderr_encoding), new_lines)
  768. msg_re = re.compile(r".*#[0-9].*restore_pipe")
  769. debug_msg = filter(msg_re.match, new_lines)
  770. self.log.debug('tar2_stderr: {}'.format('\n'.join(debug_msg)))
  771. new_lines = filter(lambda x: not msg_re.match(x), new_lines)
  772. self.tar2_stderr += new_lines
  773. def run(self):
  774. try:
  775. self.__run__()
  776. except Exception as e:
  777. exc_type, exc_value, exc_traceback = sys.exc_info()
  778. # Cleanup children
  779. for process in [self.decompressor_process,
  780. self.decryptor_process,
  781. self.tar2_process]:
  782. if process:
  783. # FIXME: kill()?
  784. try:
  785. process.terminate()
  786. except OSError:
  787. pass
  788. process.wait()
  789. self.log.error("ERROR: " + unicode(e))
  790. raise e, None, exc_traceback
  791. def __run__(self):
  792. self.log.debug("Started sending thread")
  793. self.log.debug("Moving to dir " + self.base_dir)
  794. os.chdir(self.base_dir)
  795. filename = None
  796. for filename in iter(self.queue.get, None):
  797. if filename in (QUEUE_FINISHED, QUEUE_ERROR):
  798. break
  799. self.log.debug("Extracting file " + filename)
  800. if filename.endswith('.000'):
  801. # next file
  802. if self.tar2_process is not None:
  803. if self.tar2_process.wait() != 0:
  804. self.collect_tar_output()
  805. self.log.error(
  806. "ERROR: unable to extract files for {0}, tar "
  807. "output:\n {1}".
  808. format(self.tar2_current_file,
  809. "\n ".join(self.tar2_stderr)))
  810. else:
  811. # Finished extracting the tar file
  812. self.tar2_process = None
  813. self.tar2_current_file = None
  814. tar2_cmdline = ['tar',
  815. '-%sMkvf' % ("t" if self.verify_only else "x"),
  816. self.restore_pipe,
  817. os.path.relpath(filename.rstrip('.000'))]
  818. self.log.debug("Running command " + unicode(tar2_cmdline))
  819. self.tar2_process = subprocess.Popen(tar2_cmdline,
  820. stdin=subprocess.PIPE,
  821. stderr=subprocess.PIPE)
  822. fcntl.fcntl(self.tar2_process.stderr.fileno(), fcntl.F_SETFL,
  823. fcntl.fcntl(self.tar2_process.stderr.fileno(),
  824. fcntl.F_GETFL) | os.O_NONBLOCK)
  825. self.tar2_stderr = []
  826. elif not self.tar2_process:
  827. # Extracting of the current archive failed, skip to the next
  828. # archive
  829. # TODO: some debug option to preserve it?
  830. os.remove(filename)
  831. continue
  832. else:
  833. self.collect_tar_output()
  834. self.log.debug("Releasing next chunck")
  835. self.tar2_process.stdin.write("\n")
  836. self.tar2_process.stdin.flush()
  837. self.tar2_current_file = filename
  838. pipe = open(self.restore_pipe, 'wb')
  839. common_args = {
  840. 'backup_target': pipe,
  841. 'hmac': None,
  842. 'vmproc': self.vmproc,
  843. 'addproc': self.tar2_process
  844. }
  845. if self.encrypted:
  846. # Start decrypt
  847. self.decryptor_process = subprocess.Popen(
  848. ["openssl", "enc",
  849. "-d",
  850. "-" + self.crypto_algorithm,
  851. "-pass",
  852. "pass:" + self.passphrase] +
  853. (["-z"] if self.compressed else []),
  854. stdin=open(filename, 'rb'),
  855. stdout=subprocess.PIPE)
  856. run_error = wait_backup_feedback(
  857. progress_callback=self.progress_callback,
  858. in_stream=self.decryptor_process.stdout,
  859. streamproc=self.decryptor_process,
  860. **common_args)
  861. elif self.compressed:
  862. self.decompressor_process = subprocess.Popen(
  863. ["gzip", "-d"],
  864. stdin=open(filename, 'rb'),
  865. stdout=subprocess.PIPE)
  866. run_error = wait_backup_feedback(
  867. progress_callback=self.progress_callback,
  868. in_stream=self.decompressor_process.stdout,
  869. streamproc=self.decompressor_process,
  870. **common_args)
  871. else:
  872. run_error = wait_backup_feedback(
  873. progress_callback=self.progress_callback,
  874. in_stream=open(filename, "rb"), streamproc=None,
  875. **common_args)
  876. try:
  877. pipe.close()
  878. except IOError as e:
  879. if e.errno == errno.EPIPE:
  880. self.log.debug(
  881. "Got EPIPE while closing pipe to "
  882. "the inner tar process")
  883. # ignore the error
  884. else:
  885. raise
  886. if len(run_error):
  887. if run_error == "target":
  888. self.collect_tar_output()
  889. details = "\n".join(self.tar2_stderr)
  890. else:
  891. details = "%s failed" % run_error
  892. self.tar2_process.terminate()
  893. self.tar2_process.wait()
  894. self.tar2_process = None
  895. self.log.error("Error while processing '{}': {}".format(
  896. self.tar2_current_file, details))
  897. # Delete the file as we don't need it anymore
  898. self.log.debug("Removing file " + filename)
  899. os.remove(filename)
  900. os.unlink(self.restore_pipe)
  901. if self.tar2_process is not None:
  902. if filename == QUEUE_ERROR:
  903. self.tar2_process.terminate()
  904. self.tar2_process.wait()
  905. elif self.tar2_process.wait() != 0:
  906. self.collect_tar_output()
  907. raise qubes.exc.QubesException(
  908. "unable to extract files for {0}.{1} Tar command "
  909. "output: %s".
  910. format(self.tar2_current_file,
  911. (" Perhaps the backup is encrypted?"
  912. if not self.encrypted else "",
  913. "\n".join(self.tar2_stderr))))
  914. else:
  915. # Finished extracting the tar file
  916. self.tar2_process = None
  917. self.log.debug("Finished extracting thread")
  918. class ExtractWorker3(ExtractWorker2):
  919. def __init__(self, queue, base_dir, passphrase, encrypted,
  920. progress_callback, vmproc=None,
  921. compressed=False, crypto_algorithm=DEFAULT_CRYPTO_ALGORITHM,
  922. compression_filter=None, verify_only=False):
  923. super(ExtractWorker3, self).__init__(queue, base_dir, passphrase,
  924. encrypted,
  925. progress_callback, vmproc,
  926. compressed, crypto_algorithm,
  927. verify_only)
  928. self.compression_filter = compression_filter
  929. os.unlink(self.restore_pipe)
  930. def __run__(self):
  931. self.log.debug("Started sending thread")
  932. self.log.debug("Moving to dir " + self.base_dir)
  933. os.chdir(self.base_dir)
  934. filename = None
  935. input_pipe = None
  936. for filename in iter(self.queue.get, None):
  937. if filename in (QUEUE_FINISHED, QUEUE_ERROR):
  938. break
  939. self.log.debug("Extracting file " + filename)
  940. if filename.endswith('.000'):
  941. # next file
  942. if self.tar2_process is not None:
  943. input_pipe.close()
  944. if self.tar2_process.wait() != 0:
  945. self.collect_tar_output()
  946. self.log.error(
  947. "ERROR: unable to extract files for {0}, tar "
  948. "output:\n {1}".
  949. format(self.tar2_current_file,
  950. "\n ".join(self.tar2_stderr)))
  951. else:
  952. # Finished extracting the tar file
  953. self.tar2_process = None
  954. self.tar2_current_file = None
  955. tar2_cmdline = ['tar',
  956. '-%sk' % ("t" if self.verify_only else "x"),
  957. os.path.relpath(filename.rstrip('.000'))]
  958. if self.compressed:
  959. if self.compression_filter:
  960. tar2_cmdline.insert(-1,
  961. "--use-compress-program=%s" %
  962. self.compression_filter)
  963. else:
  964. tar2_cmdline.insert(-1, "--use-compress-program=%s" %
  965. DEFAULT_COMPRESSION_FILTER)
  966. self.log.debug("Running command " + unicode(tar2_cmdline))
  967. if self.encrypted:
  968. # Start decrypt
  969. self.decryptor_process = subprocess.Popen(
  970. ["openssl", "enc",
  971. "-d",
  972. "-" + self.crypto_algorithm,
  973. "-pass",
  974. "pass:" + self.passphrase],
  975. stdin=subprocess.PIPE,
  976. stdout=subprocess.PIPE)
  977. self.tar2_process = subprocess.Popen(
  978. tar2_cmdline,
  979. stdin=self.decryptor_process.stdout,
  980. stderr=subprocess.PIPE)
  981. input_pipe = self.decryptor_process.stdin
  982. else:
  983. self.tar2_process = subprocess.Popen(
  984. tar2_cmdline,
  985. stdin=subprocess.PIPE,
  986. stderr=subprocess.PIPE)
  987. input_pipe = self.tar2_process.stdin
  988. fcntl.fcntl(self.tar2_process.stderr.fileno(), fcntl.F_SETFL,
  989. fcntl.fcntl(self.tar2_process.stderr.fileno(),
  990. fcntl.F_GETFL) | os.O_NONBLOCK)
  991. self.tar2_stderr = []
  992. elif not self.tar2_process:
  993. # Extracting of the current archive failed, skip to the next
  994. # archive
  995. # TODO: some debug option to preserve it?
  996. os.remove(filename)
  997. continue
  998. else:
  999. self.log.debug("Releasing next chunck")
  1000. self.tar2_current_file = filename
  1001. common_args = {
  1002. 'backup_target': input_pipe,
  1003. 'hmac': None,
  1004. 'vmproc': self.vmproc,
  1005. 'addproc': self.tar2_process
  1006. }
  1007. run_error = wait_backup_feedback(
  1008. progress_callback=self.progress_callback,
  1009. in_stream=open(filename, "rb"), streamproc=None,
  1010. **common_args)
  1011. if len(run_error):
  1012. if run_error == "target":
  1013. self.collect_tar_output()
  1014. details = "\n".join(self.tar2_stderr)
  1015. else:
  1016. details = "%s failed" % run_error
  1017. if self.decryptor_process:
  1018. self.decryptor_process.terminate()
  1019. self.decryptor_process.wait()
  1020. self.decryptor_process = None
  1021. self.tar2_process.terminate()
  1022. self.tar2_process.wait()
  1023. self.tar2_process = None
  1024. self.log.error("Error while processing '{}': {}".format(
  1025. self.tar2_current_file, details))
  1026. # Delete the file as we don't need it anymore
  1027. self.log.debug("Removing file " + filename)
  1028. os.remove(filename)
  1029. if self.tar2_process is not None:
  1030. input_pipe.close()
  1031. if filename == QUEUE_ERROR:
  1032. if self.decryptor_process:
  1033. self.decryptor_process.terminate()
  1034. self.decryptor_process.wait()
  1035. self.decryptor_process = None
  1036. self.tar2_process.terminate()
  1037. self.tar2_process.wait()
  1038. elif self.tar2_process.wait() != 0:
  1039. self.collect_tar_output()
  1040. raise qubes.exc.QubesException(
  1041. "unable to extract files for {0}.{1} Tar command "
  1042. "output: %s".
  1043. format(self.tar2_current_file,
  1044. (" Perhaps the backup is encrypted?"
  1045. if not self.encrypted else "",
  1046. "\n".join(self.tar2_stderr))))
  1047. else:
  1048. # Finished extracting the tar file
  1049. self.tar2_process = None
  1050. self.log.debug("Finished extracting thread")
  1051. def get_supported_hmac_algo(hmac_algorithm=None):
  1052. # Start with provided default
  1053. if hmac_algorithm:
  1054. yield hmac_algorithm
  1055. proc = subprocess.Popen(['openssl', 'list-message-digest-algorithms'],
  1056. stdout=subprocess.PIPE)
  1057. for algo in proc.stdout.readlines():
  1058. if '=>' in algo:
  1059. continue
  1060. yield algo.strip()
  1061. proc.wait()
  1062. class BackupRestoreOptions(object):
  1063. def __init__(self):
  1064. #: use default NetVM if the one referenced in backup do not exists on
  1065. # the host
  1066. self.use_default_netvm = True
  1067. #: set NetVM to "none" if the one referenced in backup do not exists
  1068. # on the host
  1069. self.use_none_netvm = False
  1070. #: set template to default if the one referenced in backup do not
  1071. # exists on the host
  1072. self.use_default_template = True
  1073. #: restore dom0 home
  1074. self.dom0_home = True
  1075. #: dictionary how what templates should be used instead of those
  1076. # referenced in backup
  1077. self.replace_template = {}
  1078. #: restore dom0 home even if username is different
  1079. self.ignore_username_mismatch = False
  1080. #: do not restore data, only verify backup integrity
  1081. self.verify_only = False
  1082. #: automatically rename VM during restore, when it would conflict
  1083. # with existing one
  1084. self.rename_conflicting = True
  1085. #: list of VM names to exclude
  1086. self.exclude = []
  1087. class BackupRestore(object):
  1088. """Usage:
  1089. >>> restore_op = BackupRestore(...)
  1090. >>> # adjust restore_op.options here
  1091. >>> restore_info = restore_op.get_restore_info()
  1092. >>> # manipulate restore_info to select VMs to restore here
  1093. >>> restore_op.restore_do(restore_info)
  1094. """
  1095. class VMToRestore(object):
  1096. #: VM excluded from restore by user
  1097. EXCLUDED = object()
  1098. #: VM with such name already exists on the host
  1099. ALREADY_EXISTS = object()
  1100. #: NetVM used by the VM does not exists on the host
  1101. MISSING_NETVM = object()
  1102. #: TemplateVM used by the VM does not exists on the host
  1103. MISSING_TEMPLATE = object()
  1104. def __init__(self, vm):
  1105. self.vm = vm
  1106. if 'backup-path' in vm.features:
  1107. self.subdir = vm.features['backup-path']
  1108. else:
  1109. self.subdir = None
  1110. if 'backup-size' in vm.features and vm.features['backup-size']:
  1111. self.size = int(vm.features['backup-size'])
  1112. else:
  1113. self.size = 0
  1114. self.problems = set()
  1115. if hasattr(vm, 'template') and vm.template:
  1116. self.template = vm.template.name
  1117. else:
  1118. self.template = None
  1119. if vm.netvm:
  1120. self.netvm = vm.netvm.name
  1121. else:
  1122. self.netvm = None
  1123. self.name = vm.name
  1124. self.orig_template = None
  1125. @property
  1126. def good_to_go(self):
  1127. return len(self.problems) == 0
  1128. class Dom0ToRestore(VMToRestore):
  1129. #: backup was performed on system with different dom0 username
  1130. USERNAME_MISMATCH = object()
  1131. def __init__(self, vm, subdir=None):
  1132. super(BackupRestore.Dom0ToRestore, self).__init__(vm)
  1133. if subdir:
  1134. self.subdir = subdir
  1135. self.username = os.path.basename(subdir)
  1136. def __init__(self, app, backup_location, backup_vm, passphrase):
  1137. super(BackupRestore, self).__init__()
  1138. #: qubes.Qubes instance
  1139. self.app = app
  1140. #: options how the backup should be restored
  1141. self.options = BackupRestoreOptions()
  1142. #: VM from which backup should be retrieved
  1143. self.backup_vm = backup_vm
  1144. if backup_vm and backup_vm.qid == 0:
  1145. self.backup_vm = None
  1146. #: backup path, inside VM pointed by :py:attr:`backup_vm`
  1147. self.backup_location = backup_location
  1148. #: passphrase protecting backup integrity and optionally decryption
  1149. self.passphrase = passphrase
  1150. #: temporary directory used to extract the data before moving to the
  1151. # final location; should be on the same filesystem as /var/lib/qubes
  1152. self.tmpdir = tempfile.mkdtemp(prefix="restore", dir="/var/tmp")
  1153. #: list of processes (Popen objects) to kill on cancel
  1154. self.processes_to_kill_on_cancel = []
  1155. #: is the backup operation canceled
  1156. self.canceled = False
  1157. #: report restore progress, called with one argument - percents of
  1158. # data restored
  1159. # FIXME: convert to float [0,1]
  1160. self.progress_callback = None
  1161. self.log = logging.getLogger('qubes.backup')
  1162. #: basic information about the backup
  1163. self.header_data = self._retrieve_backup_header()
  1164. #: VMs included in the backup
  1165. self.backup_app = self._process_qubes_xml()
  1166. def cancel(self):
  1167. """Cancel running backup operation. Can be called from another thread.
  1168. """
  1169. self.canceled = True
  1170. for proc in self.processes_to_kill_on_cancel:
  1171. try:
  1172. proc.terminate()
  1173. except OSError:
  1174. pass
  1175. def _start_retrieval_process(self, filelist, limit_count, limit_bytes):
  1176. """Retrieve backup stream and extract it to :py:attr:`tmpdir`
  1177. :param filelist: list of files to extract; listing directory name
  1178. will extract the whole directory; use empty list to extract the whole
  1179. archive
  1180. :param limit_count: maximum number of files to extract
  1181. :param limit_bytes: maximum size of extracted data
  1182. :return: a touple of (Popen object of started process, file-like
  1183. object for reading extracted files list, file-like object for reading
  1184. errors)
  1185. """
  1186. vmproc = None
  1187. if self.backup_vm is not None:
  1188. # If APPVM, STDOUT is a PIPE
  1189. vmproc = self.backup_vm.run_service('qubes.Restore',
  1190. passio_popen=True, passio_stderr=True)
  1191. vmproc.stdin.write(
  1192. self.backup_location.replace("\r", "").replace("\n", "") + "\n")
  1193. # Send to tar2qfile the VMs that should be extracted
  1194. vmproc.stdin.write(" ".join(filelist) + "\n")
  1195. self.processes_to_kill_on_cancel.append(vmproc)
  1196. backup_stdin = vmproc.stdout
  1197. tar1_command = ['/usr/libexec/qubes/qfile-dom0-unpacker',
  1198. str(os.getuid()), self.tmpdir, '-v']
  1199. else:
  1200. backup_stdin = open(self.backup_location, 'rb')
  1201. tar1_command = ['tar',
  1202. '-ixv',
  1203. '-C', self.tmpdir] + filelist
  1204. tar1_env = os.environ.copy()
  1205. tar1_env['UPDATES_MAX_BYTES'] = str(limit_bytes)
  1206. tar1_env['UPDATES_MAX_FILES'] = str(limit_count)
  1207. self.log.debug("Run command" + unicode(tar1_command))
  1208. command = subprocess.Popen(
  1209. tar1_command,
  1210. stdin=backup_stdin,
  1211. stdout=vmproc.stdin if vmproc else subprocess.PIPE,
  1212. stderr=subprocess.PIPE,
  1213. env=tar1_env)
  1214. self.processes_to_kill_on_cancel.append(command)
  1215. # qfile-dom0-unpacker output filelist on stderr
  1216. # and have stdout connected to the VM), while tar output filelist
  1217. # on stdout
  1218. if self.backup_vm:
  1219. filelist_pipe = command.stderr
  1220. # let qfile-dom0-unpacker hold the only open FD to the write end of
  1221. # pipe, otherwise qrexec-client will not receive EOF when
  1222. # qfile-dom0-unpacker terminates
  1223. vmproc.stdin.close()
  1224. else:
  1225. filelist_pipe = command.stdout
  1226. if self.backup_vm:
  1227. error_pipe = vmproc.stderr
  1228. else:
  1229. error_pipe = command.stderr
  1230. return command, filelist_pipe, error_pipe
  1231. def _verify_hmac(self, filename, hmacfile, algorithm=None):
  1232. def load_hmac(hmac_text):
  1233. hmac_text = hmac_text.strip().split("=")
  1234. if len(hmac_text) > 1:
  1235. hmac_text = hmac_text[1].strip()
  1236. else:
  1237. raise qubes.exc.QubesException(
  1238. "ERROR: invalid hmac file content")
  1239. return hmac_text
  1240. if algorithm is None:
  1241. algorithm = self.header_data.hmac_algorithm
  1242. passphrase = self.passphrase.encode('utf-8')
  1243. self.log.debug("Verifying file {}".format(filename))
  1244. if hmacfile != filename + ".hmac":
  1245. raise qubes.exc.QubesException(
  1246. "ERROR: expected hmac for {}, but got {}".
  1247. format(filename, hmacfile))
  1248. hmac_proc = subprocess.Popen(
  1249. ["openssl", "dgst", "-" + algorithm, "-hmac", passphrase],
  1250. stdin=open(os.path.join(self.tmpdir, filename), 'rb'),
  1251. stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  1252. hmac_stdout, hmac_stderr = hmac_proc.communicate()
  1253. if len(hmac_stderr) > 0:
  1254. raise qubes.exc.QubesException(
  1255. "ERROR: verify file {0}: {1}".format(filename, hmac_stderr))
  1256. else:
  1257. self.log.debug("Loading hmac for file {}".format(filename))
  1258. hmac = load_hmac(open(os.path.join(self.tmpdir, hmacfile),
  1259. 'r').read())
  1260. if len(hmac) > 0 and load_hmac(hmac_stdout) == hmac:
  1261. os.unlink(os.path.join(self.tmpdir, hmacfile))
  1262. self.log.debug(
  1263. "File verification OK -> Sending file {}".format(filename))
  1264. return True
  1265. else:
  1266. raise qubes.exc.QubesException(
  1267. "ERROR: invalid hmac for file {0}: {1}. "
  1268. "Is the passphrase correct?".
  1269. format(filename, load_hmac(hmac_stdout)))
  1270. def _retrieve_backup_header(self):
  1271. """Retrieve backup header and qubes.xml. Only backup header is
  1272. analyzed, qubes.xml is left as-is
  1273. (not even verified/decrypted/uncompressed)
  1274. :return header_data
  1275. :rtype :py:class:`BackupHeader`
  1276. """
  1277. if not self.backup_vm and os.path.exists(
  1278. os.path.join(self.backup_location, 'qubes.xml')):
  1279. # backup format version 1 doesn't have header
  1280. header_data = BackupHeader()
  1281. header_data.version = 1
  1282. return header_data
  1283. (retrieve_proc, filelist_pipe, error_pipe) = \
  1284. self._start_retrieval_process(
  1285. ['backup-header', 'backup-header.hmac',
  1286. 'qubes.xml.000', 'qubes.xml.000.hmac'], 4, 1024 * 1024)
  1287. expect_tar_error = False
  1288. filename = filelist_pipe.readline().strip()
  1289. hmacfile = filelist_pipe.readline().strip()
  1290. # tar output filename before actually extracting it, so wait for the
  1291. # next one before trying to access it
  1292. if not self.backup_vm:
  1293. filelist_pipe.readline().strip()
  1294. self.log.debug("Got backup header and hmac: {}, {}".format(
  1295. filename, hmacfile))
  1296. if not filename or filename == "EOF" or \
  1297. not hmacfile or hmacfile == "EOF":
  1298. retrieve_proc.wait()
  1299. proc_error_msg = error_pipe.read(MAX_STDERR_BYTES)
  1300. raise qubes.exc.QubesException(
  1301. "Premature end of archive while receiving "
  1302. "backup header. Process output:\n" + proc_error_msg)
  1303. file_ok = False
  1304. hmac_algorithm = DEFAULT_HMAC_ALGORITHM
  1305. for hmac_algo in get_supported_hmac_algo(hmac_algorithm):
  1306. try:
  1307. if self._verify_hmac(filename, hmacfile, hmac_algo):
  1308. file_ok = True
  1309. hmac_algorithm = hmac_algo
  1310. break
  1311. except qubes.exc.QubesException:
  1312. # Ignore exception here, try the next algo
  1313. pass
  1314. if not file_ok:
  1315. raise qubes.exc.QubesException(
  1316. "Corrupted backup header (hmac verification "
  1317. "failed). Is the password correct?")
  1318. if os.path.basename(filename) == HEADER_FILENAME:
  1319. filename = os.path.join(self.tmpdir, filename)
  1320. header_data = BackupHeader(open(filename, 'r').read())
  1321. os.unlink(filename)
  1322. else:
  1323. # if no header found, create one with guessed HMAC algo
  1324. header_data = BackupHeader(
  1325. version=2,
  1326. hmac_algorithm=hmac_algorithm,
  1327. # place explicitly this value, because it is what format_version
  1328. # 2 have
  1329. crypto_algorithm='aes-256-cbc',
  1330. # TODO: set encrypted to something...
  1331. )
  1332. # when tar do not find expected file in archive, it exit with
  1333. # code 2. This will happen because we've requested backup-header
  1334. # file, but the archive do not contain it. Ignore this particular
  1335. # error.
  1336. if not self.backup_vm:
  1337. expect_tar_error = True
  1338. if retrieve_proc.wait() != 0 and not expect_tar_error:
  1339. raise qubes.exc.QubesException(
  1340. "unable to read the qubes backup file {0} ({1}): {2}".format(
  1341. self.backup_location,
  1342. retrieve_proc.wait(),
  1343. error_pipe.read(MAX_STDERR_BYTES)
  1344. ))
  1345. if retrieve_proc in self.processes_to_kill_on_cancel:
  1346. self.processes_to_kill_on_cancel.remove(retrieve_proc)
  1347. # wait for other processes (if any)
  1348. for proc in self.processes_to_kill_on_cancel:
  1349. if proc.wait() != 0:
  1350. raise qubes.exc.QubesException(
  1351. "Backup header retrieval failed (exit code {})".format(
  1352. proc.wait())
  1353. )
  1354. return header_data
  1355. def _start_inner_extraction_worker(self, queue):
  1356. """Start a worker process, extracting inner layer of bacup archive,
  1357. extract them to :py:attr:`tmpdir`.
  1358. End the data by pushing QUEUE_FINISHED or QUEUE_ERROR to the queue.
  1359. :param queue :py:class:`Queue` object to handle files from
  1360. """
  1361. # Setup worker to extract encrypted data chunks to the restore dirs
  1362. # Create the process here to pass it options extracted from
  1363. # backup header
  1364. extractor_params = {
  1365. 'queue': queue,
  1366. 'base_dir': self.tmpdir,
  1367. 'passphrase': self.passphrase,
  1368. 'encrypted': self.header_data.encrypted,
  1369. 'compressed': self.header_data.compressed,
  1370. 'crypto_algorithm': self.header_data.crypto_algorithm,
  1371. 'verify_only': self.options.verify_only,
  1372. 'progress_callback': self.progress_callback,
  1373. }
  1374. format_version = self.header_data.version
  1375. if format_version == 2:
  1376. extract_proc = ExtractWorker2(**extractor_params)
  1377. elif format_version in [3, 4]:
  1378. extractor_params['compression_filter'] = \
  1379. self.header_data.compression_filter
  1380. extract_proc = ExtractWorker3(**extractor_params)
  1381. else:
  1382. raise NotImplementedError(
  1383. "Backup format version %d not supported" % format_version)
  1384. extract_proc.start()
  1385. return extract_proc
  1386. def _process_qubes_xml(self):
  1387. """Verify, unpack and load qubes.xml. Possibly convert its format if
  1388. necessary. It expect that :py:attr:`header_data` is already populated,
  1389. and :py:meth:`retrieve_backup_header` was called.
  1390. """
  1391. if self.header_data.version == 1:
  1392. backup_app = qubes.core2migration.Core2Qubes(
  1393. os.path.join(self.backup_location, 'qubes.xml'))
  1394. return backup_app
  1395. else:
  1396. self._verify_hmac("qubes.xml.000", "qubes.xml.000.hmac")
  1397. queue = Queue()
  1398. queue.put("qubes.xml.000")
  1399. queue.put(QUEUE_FINISHED)
  1400. extract_proc = self._start_inner_extraction_worker(queue)
  1401. extract_proc.join()
  1402. if extract_proc.exitcode != 0:
  1403. raise qubes.exc.QubesException(
  1404. "unable to extract the qubes backup. "
  1405. "Check extracting process errors.")
  1406. if self.header_data.version in [2, 3]:
  1407. backup_app = qubes.core2migration.Core2Qubes(
  1408. os.path.join(self.tmpdir, 'qubes.xml'))
  1409. else:
  1410. backup_app = qubes.Qubes(os.path.join(self.tmpdir, 'qubes.xml'))
  1411. # Not needed anymore - all the data stored in backup_app
  1412. os.unlink(os.path.join(self.tmpdir, 'qubes.xml'))
  1413. return backup_app
  1414. def _restore_vm_dirs(self, vms_dirs, vms_size):
  1415. # Currently each VM consists of at most 7 archives (count
  1416. # file_to_backup calls in backup_prepare()), but add some safety
  1417. # margin for further extensions. Each archive is divided into 100MB
  1418. # chunks. Additionally each file have own hmac file. So assume upper
  1419. # limit as 2*(10*COUNT_OF_VMS+TOTAL_SIZE/100MB)
  1420. limit_count = str(2 * (10 * len(vms_dirs) +
  1421. int(vms_size / (100 * 1024 * 1024))))
  1422. self.log.debug("Working in temporary dir:" + self.tmpdir)
  1423. self.log.info(
  1424. "Extracting data: " + size_to_human(vms_size) + " to restore")
  1425. # retrieve backup from the backup stream (either VM, or dom0 file)
  1426. # TODO: add some safety margin in vms_size?
  1427. (retrieve_proc, filelist_pipe, error_pipe) = \
  1428. self._start_retrieval_process(vms_dirs, limit_count, vms_size)
  1429. to_extract = Queue()
  1430. # extract data retrieved by retrieve_proc
  1431. extract_proc = self._start_inner_extraction_worker(to_extract)
  1432. try:
  1433. filename = None
  1434. nextfile = None
  1435. while True:
  1436. if self.canceled:
  1437. break
  1438. if not extract_proc.is_alive():
  1439. retrieve_proc.terminate()
  1440. retrieve_proc.wait()
  1441. if retrieve_proc in self.processes_to_kill_on_cancel:
  1442. self.processes_to_kill_on_cancel.remove(retrieve_proc)
  1443. # wait for other processes (if any)
  1444. for proc in self.processes_to_kill_on_cancel:
  1445. proc.wait()
  1446. break
  1447. if nextfile is not None:
  1448. filename = nextfile
  1449. else:
  1450. filename = filelist_pipe.readline().strip()
  1451. self.log.debug("Getting new file:" + filename)
  1452. if not filename or filename == "EOF":
  1453. break
  1454. hmacfile = filelist_pipe.readline().strip()
  1455. if self.canceled:
  1456. break
  1457. # if reading archive directly with tar, wait for next filename -
  1458. # tar prints filename before processing it, so wait for
  1459. # the next one to be sure that whole file was extracted
  1460. if not self.backup_vm:
  1461. nextfile = filelist_pipe.readline().strip()
  1462. self.log.debug("Getting hmac:" + hmacfile)
  1463. if not hmacfile or hmacfile == "EOF":
  1464. # Premature end of archive, either of tar1_command or
  1465. # vmproc exited with error
  1466. break
  1467. if not any(map(lambda x: filename.startswith(x), vms_dirs)):
  1468. self.log.debug("Ignoring VM not selected for restore")
  1469. os.unlink(os.path.join(self.tmpdir, filename))
  1470. os.unlink(os.path.join(self.tmpdir, hmacfile))
  1471. continue
  1472. if self._verify_hmac(filename, hmacfile):
  1473. to_extract.put(os.path.join(self.tmpdir, filename))
  1474. if self.canceled:
  1475. raise BackupCanceledError("Restore canceled",
  1476. tmpdir=self.tmpdir)
  1477. if retrieve_proc.wait() != 0:
  1478. raise qubes.exc.QubesException(
  1479. "unable to read the qubes backup file {0} ({1}): {2}"
  1480. .format(self.backup_location, error_pipe.read(
  1481. MAX_STDERR_BYTES)))
  1482. # wait for other processes (if any)
  1483. for proc in self.processes_to_kill_on_cancel:
  1484. # FIXME check 'vmproc' exit code?
  1485. proc.wait()
  1486. if filename and filename != "EOF":
  1487. raise qubes.exc.QubesException(
  1488. "Premature end of archive, the last file was %s" % filename)
  1489. except:
  1490. to_extract.put(QUEUE_ERROR)
  1491. extract_proc.join()
  1492. raise
  1493. else:
  1494. to_extract.put(QUEUE_FINISHED)
  1495. self.log.debug("Waiting for the extraction process to finish...")
  1496. extract_proc.join()
  1497. self.log.debug("Extraction process finished with code: {}".format(
  1498. extract_proc.exitcode))
  1499. if extract_proc.exitcode != 0:
  1500. raise qubes.exc.QubesException(
  1501. "unable to extract the qubes backup. "
  1502. "Check extracting process errors.")
  1503. def generate_new_name_for_conflicting_vm(self, orig_name, restore_info):
  1504. number = 1
  1505. if len(orig_name) > 29:
  1506. orig_name = orig_name[0:29]
  1507. new_name = orig_name
  1508. while (new_name in restore_info.keys() or
  1509. new_name in map(lambda x: x.name,
  1510. restore_info.values()) or
  1511. new_name in self.app.domains):
  1512. new_name = str('{}{}'.format(orig_name, number))
  1513. number += 1
  1514. if number == 100:
  1515. # give up
  1516. return None
  1517. return new_name
  1518. def restore_info_verify(self, restore_info):
  1519. for vm in restore_info.keys():
  1520. if vm in ['dom0']:
  1521. continue
  1522. vm_info = restore_info[vm]
  1523. assert isinstance(vm_info, self.VMToRestore)
  1524. vm_info.problems.clear()
  1525. if vm in self.options.exclude:
  1526. vm_info.problems.add(self.VMToRestore.EXCLUDED)
  1527. if not self.options.verify_only and \
  1528. vm in self.app.domains:
  1529. if self.options.rename_conflicting:
  1530. new_name = self.generate_new_name_for_conflicting_vm(
  1531. vm, restore_info
  1532. )
  1533. if new_name is not None:
  1534. vm_info.name = new_name
  1535. else:
  1536. vm_info.problems.add(self.VMToRestore.ALREADY_EXISTS)
  1537. else:
  1538. vm_info.problems.add(self.VMToRestore.ALREADY_EXISTS)
  1539. # check template
  1540. if vm_info.template:
  1541. template_name = vm_info.template
  1542. try:
  1543. host_template = self.app.domains[template_name]
  1544. except KeyError:
  1545. host_template = None
  1546. if not host_template or not host_template.is_template():
  1547. # Maybe the (custom) template is in the backup?
  1548. if not (template_name in restore_info.keys() and
  1549. restore_info[template_name].good_to_go and
  1550. restore_info[template_name].vm.is_template()):
  1551. if self.options.use_default_template and \
  1552. self.app.default_template:
  1553. if vm_info.orig_template is None:
  1554. vm_info.orig_template = template_name
  1555. vm_info.template = self.app.default_template.name
  1556. else:
  1557. vm_info.problems.add(
  1558. self.VMToRestore.MISSING_TEMPLATE)
  1559. # check netvm
  1560. if not vm_info.vm.property_is_default('netvm') and vm_info.netvm:
  1561. netvm_name = vm_info.netvm
  1562. try:
  1563. netvm_on_host = self.app.domains[netvm_name]
  1564. except KeyError:
  1565. netvm_on_host = None
  1566. # No netvm on the host?
  1567. if not ((netvm_on_host is not None)
  1568. and netvm_on_host.provides_network):
  1569. # Maybe the (custom) netvm is in the backup?
  1570. if not (netvm_name in restore_info.keys() and
  1571. restore_info[netvm_name].good_to_go and
  1572. restore_info[netvm_name].vm.provides_network):
  1573. if self.options.use_default_netvm:
  1574. vm_info.vm.netvm = qubes.property.DEFAULT
  1575. elif self.options.use_none_netvm:
  1576. vm_info.netvm = None
  1577. else:
  1578. vm_info.problems.add(self.VMToRestore.MISSING_NETVM)
  1579. return restore_info
  1580. def _is_vm_included_in_backup_v1(self, check_vm):
  1581. if check_vm.qid == 0:
  1582. return os.path.exists(
  1583. os.path.join(self.backup_location, 'dom0-home'))
  1584. # DisposableVM
  1585. if check_vm.dir_path is None:
  1586. return False
  1587. backup_vm_dir_path = check_vm.dir_path.replace(
  1588. qubes.config.system_path["qubes_base_dir"], self.backup_location)
  1589. if os.path.exists(backup_vm_dir_path):
  1590. return True
  1591. else:
  1592. return False
  1593. @staticmethod
  1594. def _is_vm_included_in_backup_v2(check_vm):
  1595. if 'backup-content' in check_vm.features:
  1596. return check_vm.features['backup-content']
  1597. else:
  1598. return False
  1599. def _find_template_name(self, template):
  1600. if template in self.options.replace_template:
  1601. return self.options.replace_template[template]
  1602. return template
  1603. def _is_vm_included_in_backup(self, vm):
  1604. if self.header_data.version == 1:
  1605. return self._is_vm_included_in_backup_v1(vm)
  1606. elif self.header_data.version in [2, 3, 4]:
  1607. return self._is_vm_included_in_backup_v2(vm)
  1608. else:
  1609. raise qubes.exc.QubesException(
  1610. "Unknown backup format version: {}".format(
  1611. self.header_data.version))
  1612. def get_restore_info(self):
  1613. # Format versions:
  1614. # 1 - Qubes R1, Qubes R2 beta1, beta2
  1615. # 2 - Qubes R2 beta3+
  1616. vms_to_restore = {}
  1617. for vm in self.backup_app.domains:
  1618. if vm.qid == 0:
  1619. # Handle dom0 as special case later
  1620. continue
  1621. if self._is_vm_included_in_backup(vm):
  1622. self.log.debug("{} is included in backup".format(vm.name))
  1623. vms_to_restore[vm.name] = self.VMToRestore(vm)
  1624. if hasattr(vm, 'template'):
  1625. templatevm_name = self._find_template_name(
  1626. vm.template.name)
  1627. vms_to_restore[vm.name].template = templatevm_name
  1628. # Set to None to not confuse QubesVm object from backup
  1629. # collection with host collection (further in clone_attrs).
  1630. vm.netvm = None
  1631. vms_to_restore = self.restore_info_verify(vms_to_restore)
  1632. # ...and dom0 home
  1633. if self.options.dom0_home and \
  1634. self._is_vm_included_in_backup(self.backup_app.domains[0]):
  1635. vm = self.backup_app.domains[0]
  1636. if self.header_data.version == 1:
  1637. subdir = os.listdir(os.path.join(self.backup_location,
  1638. 'dom0-home'))[0]
  1639. else:
  1640. subdir = None
  1641. vms_to_restore['dom0'] = self.Dom0ToRestore(vm, subdir)
  1642. local_user = grp.getgrnam('qubes').gr_mem[0]
  1643. if vms_to_restore['dom0'].username != local_user:
  1644. if not self.options.ignore_username_mismatch:
  1645. vms_to_restore['dom0'].problems.add(
  1646. self.Dom0ToRestore.USERNAME_MISMATCH)
  1647. return vms_to_restore
  1648. @staticmethod
  1649. def get_restore_summary(restore_info):
  1650. fields = {
  1651. "qid": {"func": "vm.qid"},
  1652. "name": {"func": "('[' if vm.is_template() else '')\
  1653. + ('{' if vm.is_netvm() else '')\
  1654. + vm.name \
  1655. + (']' if vm.is_template() else '')\
  1656. + ('}' if vm.is_netvm() else '')"},
  1657. "type": {"func": "'Tpl' if vm.is_template() else \
  1658. 'App' if isinstance(vm, qubes.vm.appvm.AppVM) else \
  1659. vm.__class__.__name__.replace('VM','')"},
  1660. "updbl": {"func": "'Yes' if vm.updateable else ''"},
  1661. "template": {"func": "'n/a' if not hasattr(vm, 'template') is None "
  1662. "else vm_info.template"},
  1663. "netvm": {"func": "'n/a' if vm.is_netvm() and not vm.is_proxyvm() else\
  1664. ('*' if vm.property_is_default('netvm') else '') +\
  1665. vm_info.netvm if vm_info.netvm is not None "
  1666. "else '-'"},
  1667. "label": {"func": "vm.label.name"},
  1668. }
  1669. fields_to_display = ["name", "type", "template", "updbl",
  1670. "netvm", "label"]
  1671. # First calculate the maximum width of each field we want to display
  1672. total_width = 0
  1673. for f in fields_to_display:
  1674. fields[f]["max_width"] = len(f)
  1675. for vm_info in restore_info.values():
  1676. if vm_info.vm:
  1677. # noinspection PyUnusedLocal
  1678. vm = vm_info.vm
  1679. l = len(unicode(eval(fields[f]["func"])))
  1680. if l > fields[f]["max_width"]:
  1681. fields[f]["max_width"] = l
  1682. total_width += fields[f]["max_width"]
  1683. summary = ""
  1684. summary += "The following VMs are included in the backup:\n"
  1685. summary += "\n"
  1686. # Display the header
  1687. for f in fields_to_display:
  1688. # noinspection PyTypeChecker
  1689. fmt = "{{0:-^{0}}}-+".format(fields[f]["max_width"] + 1)
  1690. summary += fmt.format('-')
  1691. summary += "\n"
  1692. for f in fields_to_display:
  1693. # noinspection PyTypeChecker
  1694. fmt = "{{0:>{0}}} |".format(fields[f]["max_width"] + 1)
  1695. summary += fmt.format(f)
  1696. summary += "\n"
  1697. for f in fields_to_display:
  1698. # noinspection PyTypeChecker
  1699. fmt = "{{0:-^{0}}}-+".format(fields[f]["max_width"] + 1)
  1700. summary += fmt.format('-')
  1701. summary += "\n"
  1702. for vm_info in restore_info.values():
  1703. assert isinstance(vm_info, BackupRestore.VMToRestore)
  1704. # Skip non-VM here
  1705. if not vm_info.vm:
  1706. continue
  1707. # noinspection PyUnusedLocal
  1708. vm = vm_info.vm
  1709. s = ""
  1710. for f in fields_to_display:
  1711. # noinspection PyTypeChecker
  1712. fmt = "{{0:>{0}}} |".format(fields[f]["max_width"] + 1)
  1713. s += fmt.format(eval(fields[f]["func"]))
  1714. if BackupRestore.VMToRestore.EXCLUDED in vm_info.problems:
  1715. s += " <-- Excluded from restore"
  1716. elif BackupRestore.VMToRestore.ALREADY_EXISTS in vm_info.problems:
  1717. s += " <-- A VM with the same name already exists on the host!"
  1718. elif BackupRestore.VMToRestore.MISSING_TEMPLATE in \
  1719. vm_info.problems:
  1720. s += " <-- No matching template on the host " \
  1721. "or in the backup found!"
  1722. elif BackupRestore.VMToRestore.MISSING_NETVM in \
  1723. vm_info.problems:
  1724. s += " <-- No matching netvm on the host " \
  1725. "or in the backup found!"
  1726. else:
  1727. if vm_info.orig_template:
  1728. s += " <-- Original template was '{}'".format(
  1729. vm_info.orig_template)
  1730. if vm_info.name != vm_info.vm.name:
  1731. s += " <-- Will be renamed to '{}'".format(
  1732. vm_info.name)
  1733. summary += s + "\n"
  1734. if 'dom0' in restore_info.keys():
  1735. s = ""
  1736. for f in fields_to_display:
  1737. # noinspection PyTypeChecker
  1738. fmt = "{{0:>{0}}} |".format(fields[f]["max_width"] + 1)
  1739. if f == "name":
  1740. s += fmt.format("Dom0")
  1741. elif f == "type":
  1742. s += fmt.format("Home")
  1743. else:
  1744. s += fmt.format("")
  1745. if BackupRestore.Dom0ToRestore.USERNAME_MISMATCH in \
  1746. restore_info['dom0'].problems:
  1747. s += " <-- username in backup and dom0 mismatch"
  1748. summary += s + "\n"
  1749. return summary
  1750. def _restore_vm_dir_v1(self, src_dir, dst_dir):
  1751. backup_src_dir = src_dir.replace(
  1752. qubes.config.system_path["qubes_base_dir"], self.backup_location)
  1753. # We prefer to use Linux's cp, because it nicely handles sparse files
  1754. cp_retcode = subprocess.call(
  1755. ["cp", "-rp", "--reflink=auto", backup_src_dir, dst_dir])
  1756. if cp_retcode != 0:
  1757. raise qubes.exc.QubesException(
  1758. "*** Error while copying file {0} to {1}".format(backup_src_dir,
  1759. dst_dir))
  1760. def restore_do(self, restore_info):
  1761. # FIXME handle locking
  1762. # Perform VM restoration in backup order
  1763. vms_dirs = []
  1764. vms_size = 0
  1765. vms = {}
  1766. for vm_info in restore_info.values():
  1767. assert isinstance(vm_info, self.VMToRestore)
  1768. if not vm_info.vm:
  1769. continue
  1770. if not vm_info.good_to_go:
  1771. continue
  1772. vm = vm_info.vm
  1773. if self.header_data.version >= 2:
  1774. if vm.features['backup-size']:
  1775. vms_size += int(vm.features['backup-size'])
  1776. vms_dirs.append(vm.features['backup-path'])
  1777. vms[vm.name] = vm
  1778. if self.header_data.version >= 2:
  1779. if 'dom0' in restore_info.keys() and \
  1780. restore_info['dom0'].good_to_go:
  1781. vms_dirs.append(os.path.dirname(restore_info['dom0'].subdir))
  1782. vms_size += restore_info['dom0'].size
  1783. try:
  1784. self._restore_vm_dirs(vms_dirs=vms_dirs, vms_size=vms_size)
  1785. except qubes.exc.QubesException:
  1786. if self.options.verify_only:
  1787. raise
  1788. else:
  1789. self.log.warning(
  1790. "Some errors occurred during data extraction, "
  1791. "continuing anyway to restore at least some "
  1792. "VMs")
  1793. else:
  1794. if self.options.verify_only:
  1795. self.log.warning(
  1796. "Backup verification not supported for this backup format.")
  1797. if self.options.verify_only:
  1798. shutil.rmtree(self.tmpdir)
  1799. return
  1800. # First load templates, then other VMs
  1801. for vm in sorted(vms.values(), key=lambda x: x.is_template(),
  1802. reverse=True):
  1803. if self.canceled:
  1804. # only break the loop to save qubes.xml
  1805. # with already restored VMs
  1806. break
  1807. self.log.info("-> Restoring {0}...".format(vm.name))
  1808. retcode = subprocess.call(
  1809. ["mkdir", "-p", os.path.dirname(vm.dir_path)])
  1810. if retcode != 0:
  1811. self.log.error("*** Cannot create directory: {0}?!".format(
  1812. vm.dir_path))
  1813. self.log.warning("Skipping VM {}...".format(vm.name))
  1814. continue
  1815. kwargs = {}
  1816. if hasattr(vm, 'template'):
  1817. template = restore_info[vm.name].template
  1818. # handle potentially renamed template
  1819. if template in restore_info \
  1820. and restore_info[template].good_to_go:
  1821. template = restore_info[template].name
  1822. kwargs['template'] = template
  1823. new_vm = None
  1824. vm_name = restore_info[vm.name].name
  1825. try:
  1826. # first only minimal set, later clone_properties
  1827. # will be called
  1828. new_vm = self.app.add_new_vm(
  1829. vm.__class__,
  1830. name=vm_name,
  1831. label=vm.label,
  1832. installed_by_rpm=False,
  1833. **kwargs)
  1834. if os.path.exists(new_vm.dir_path):
  1835. move_to_path = tempfile.mkdtemp('', os.path.basename(
  1836. new_vm.dir_path), os.path.dirname(new_vm.dir_path))
  1837. try:
  1838. os.rename(new_vm.dir_path, move_to_path)
  1839. self.log.warning(
  1840. "*** Directory {} already exists! It has "
  1841. "been moved to {}".format(new_vm.dir_path,
  1842. move_to_path))
  1843. except OSError:
  1844. self.log.error(
  1845. "*** Directory {} already exists and "
  1846. "cannot be moved!".format(new_vm.dir_path))
  1847. self.log.warning("Skipping VM {}...".format(
  1848. vm.name))
  1849. continue
  1850. if self.header_data.version == 1:
  1851. self._restore_vm_dir_v1(vm.dir_path,
  1852. os.path.dirname(new_vm.dir_path))
  1853. else:
  1854. shutil.move(os.path.join(self.tmpdir,
  1855. vm.features['backup-path']),
  1856. new_vm.dir_path)
  1857. new_vm.verify_files()
  1858. except Exception as err:
  1859. self.log.error("ERROR: {0}".format(err))
  1860. self.log.warning("*** Skipping VM: {0}".format(vm.name))
  1861. if new_vm:
  1862. del self.app.domains[new_vm.qid]
  1863. continue
  1864. if hasattr(vm, 'kernel'):
  1865. # TODO: add a setting for this?
  1866. if not vm.property_is_default('kernel') and vm.kernel and \
  1867. vm.kernel not in \
  1868. os.listdir(os.path.join(qubes.config.qubes_base_dir,
  1869. qubes.config.system_path[
  1870. 'qubes_kernels_base_dir'])):
  1871. self.log.warning("Kernel %s not installed, "
  1872. "using default one" % vm.kernel)
  1873. vm.kernel = qubes.property.DEFAULT
  1874. # remove no longer needed backup metadata
  1875. if 'backup-content' in vm.features:
  1876. del vm.features['backup-content']
  1877. del vm.features['backup-size']
  1878. del vm.features['backup-path']
  1879. try:
  1880. # exclude VM references - handled manually according to
  1881. # restore options
  1882. proplist = [prop for prop in new_vm.property_list()
  1883. if prop.clone and prop.__name__ not in
  1884. ['template', 'netvm', 'dispvm_netvm']]
  1885. new_vm.clone_properties(vm, proplist=proplist)
  1886. except Exception as err:
  1887. self.log.error("ERROR: {0}".format(err))
  1888. self.log.warning("*** Some VM property will not be "
  1889. "restored")
  1890. try:
  1891. new_vm.fire_event('domain-restore')
  1892. except Exception as err:
  1893. self.log.error("ERROR during appmenu restore: "
  1894. "{0}".format(err))
  1895. self.log.warning(
  1896. "*** VM '{0}' will not have appmenus".format(vm.name))
  1897. # Set network dependencies - only non-default netvm setting
  1898. for vm in vms.values():
  1899. vm_info = restore_info[vm.name]
  1900. vm_name = vm_info.name
  1901. try:
  1902. host_vm = self.app.domains[vm_name]
  1903. except KeyError:
  1904. # Failed/skipped VM
  1905. continue
  1906. if not vm.property_is_default('netvm'):
  1907. if vm_info.netvm in restore_info:
  1908. host_vm.netvm = restore_info[vm_info.netvm].name
  1909. else:
  1910. host_vm.netvm = vm_info.netvm
  1911. self.app.save()
  1912. if self.canceled:
  1913. if self.header_data.version >= 2:
  1914. raise BackupCanceledError("Restore canceled",
  1915. tmpdir=self.tmpdir)
  1916. else:
  1917. raise BackupCanceledError("Restore canceled")
  1918. # ... and dom0 home as last step
  1919. if 'dom0' in restore_info.keys() and restore_info['dom0'].good_to_go:
  1920. backup_path = restore_info['dom0'].subdir
  1921. local_user = grp.getgrnam('qubes').gr_mem[0]
  1922. home_dir = pwd.getpwnam(local_user).pw_dir
  1923. if self.header_data.version == 1:
  1924. backup_dom0_home_dir = os.path.join(self.backup_location,
  1925. backup_path)
  1926. else:
  1927. backup_dom0_home_dir = os.path.join(self.tmpdir, backup_path)
  1928. restore_home_backupdir = "home-pre-restore-{0}".format(
  1929. time.strftime("%Y-%m-%d-%H%M%S"))
  1930. self.log.info(
  1931. "Restoring home of user '{0}'...".format(local_user))
  1932. self.log.info(
  1933. "Existing files/dirs backed up in '{0}' dir".format(
  1934. restore_home_backupdir))
  1935. os.mkdir(home_dir + '/' + restore_home_backupdir)
  1936. for f in os.listdir(backup_dom0_home_dir):
  1937. home_file = home_dir + '/' + f
  1938. if os.path.exists(home_file):
  1939. os.rename(home_file,
  1940. home_dir + '/' + restore_home_backupdir + '/' + f)
  1941. if self.header_data.version == 1:
  1942. subprocess.call(
  1943. ["cp", "-nrp", "--reflink=auto",
  1944. backup_dom0_home_dir + '/' + f, home_file])
  1945. elif self.header_data.version >= 2:
  1946. shutil.move(backup_dom0_home_dir + '/' + f, home_file)
  1947. retcode = subprocess.call(['sudo', 'chown', '-R',
  1948. local_user, home_dir])
  1949. if retcode != 0:
  1950. self.log.error("*** Error while setting home directory owner")
  1951. shutil.rmtree(self.tmpdir)
  1952. # vim:sw=4:et: