restore.py 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926
  1. # -*- encoding: utf8 -*-
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2017 Marek Marczykowski-Górecki
  6. # <marmarek@invisiblethingslab.com>
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU Lesser General Public License as published by
  10. # the Free Software Foundation; either version 2.1 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU Lesser General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public License along
  19. # with this program; if not, see <http://www.gnu.org/licenses/>.
  20. '''Backup restore module'''
  21. import errno
  22. import fcntl
  23. import functools
  24. import grp
  25. import logging
  26. import multiprocessing
  27. from multiprocessing import Queue, Process
  28. import os
  29. import pwd
  30. import re
  31. import shutil
  32. import subprocess
  33. import sys
  34. import tempfile
  35. import termios
  36. import time
  37. import qubesadmin
  38. import qubesadmin.vm
  39. from qubesadmin.backup import BackupVM
  40. from qubesadmin.backup.core2 import Core2Qubes
  41. from qubesadmin.backup.core3 import Core3Qubes
  42. from qubesadmin.devices import DeviceAssignment
  43. from qubesadmin.exc import QubesException
  44. from qubesadmin.utils import size_to_human
  45. # must be picklable
  46. QUEUE_FINISHED = "!!!FINISHED"
  47. QUEUE_ERROR = "!!!ERROR"
  48. HEADER_FILENAME = 'backup-header'
  49. DEFAULT_CRYPTO_ALGORITHM = 'aes-256-cbc'
  50. # 'scrypt' is not exactly HMAC algorithm, but a tool we use to
  51. # integrity-protect the data
  52. DEFAULT_HMAC_ALGORITHM = 'scrypt'
  53. DEFAULT_COMPRESSION_FILTER = 'gzip'
  54. # Maximum size of error message get from process stderr (including VM process)
  55. MAX_STDERR_BYTES = 1024
  56. # header + qubes.xml max size
  57. HEADER_QUBES_XML_MAX_SIZE = 1024 * 1024
  58. # hmac file max size - regardless of backup format version!
  59. HMAC_MAX_SIZE = 4096
  60. BLKSIZE = 512
  61. _re_alphanum = re.compile(r'^[A-Za-z0-9-]*$')
  62. _tar_msg_re = re.compile(r".*#[0-9].*restore_pipe")
  63. _tar_file_size_re = re.compile(r"^[^ ]+ [^ ]+/[^ ]+ *([0-9]+) .*")
  64. class BackupCanceledError(QubesException):
  65. '''Exception raised when backup/restore was cancelled'''
  66. def __init__(self, msg, tmpdir=None):
  67. super(BackupCanceledError, self).__init__(msg)
  68. self.tmpdir = tmpdir
  69. class BackupHeader(object):
  70. '''Structure describing backup-header file included as the first file in
  71. backup archive
  72. '''
  73. header_keys = {
  74. 'version': 'version',
  75. 'encrypted': 'encrypted',
  76. 'compressed': 'compressed',
  77. 'compression-filter': 'compression_filter',
  78. 'crypto-algorithm': 'crypto_algorithm',
  79. 'hmac-algorithm': 'hmac_algorithm',
  80. 'backup-id': 'backup_id'
  81. }
  82. bool_options = ['encrypted', 'compressed']
  83. int_options = ['version']
  84. def __init__(self,
  85. header_data=None,
  86. version=None,
  87. encrypted=None,
  88. compressed=None,
  89. compression_filter=None,
  90. hmac_algorithm=None,
  91. crypto_algorithm=None,
  92. backup_id=None):
  93. # repeat the list to help code completion...
  94. self.version = version
  95. self.encrypted = encrypted
  96. self.compressed = compressed
  97. # Options introduced in backup format 3+, which always have a header,
  98. # so no need for fallback in function parameter
  99. self.compression_filter = compression_filter
  100. self.hmac_algorithm = hmac_algorithm
  101. self.crypto_algorithm = crypto_algorithm
  102. self.backup_id = backup_id
  103. if header_data is not None:
  104. self.load(header_data)
  105. def load(self, untrusted_header_text):
  106. """Parse backup header file.
  107. :param untrusted_header_text: header content
  108. :type untrusted_header_text: basestring
  109. .. warning::
  110. This function may be exposed to not yet verified header,
  111. so is security critical.
  112. """
  113. try:
  114. untrusted_header_text = untrusted_header_text.decode('ascii')
  115. except UnicodeDecodeError:
  116. raise QubesException(
  117. "Non-ASCII characters in backup header")
  118. for untrusted_line in untrusted_header_text.splitlines():
  119. if untrusted_line.count('=') != 1:
  120. raise QubesException("Invalid backup header")
  121. key, value = untrusted_line.strip().split('=', 1)
  122. if not _re_alphanum.match(key):
  123. raise QubesException("Invalid backup header ("
  124. "key)")
  125. if key not in self.header_keys.keys():
  126. # Ignoring unknown option
  127. continue
  128. if not _re_alphanum.match(value):
  129. raise QubesException("Invalid backup header ("
  130. "value)")
  131. if getattr(self, self.header_keys[key]) is not None:
  132. raise QubesException(
  133. "Duplicated header line: {}".format(key))
  134. if key in self.bool_options:
  135. value = value.lower() in ["1", "true", "yes"]
  136. elif key in self.int_options:
  137. value = int(value)
  138. setattr(self, self.header_keys[key], value)
  139. self.validate()
  140. def validate(self):
  141. '''Validate header data, according to header version'''
  142. if self.version == 1:
  143. # header not really present
  144. pass
  145. elif self.version in [2, 3, 4]:
  146. expected_attrs = ['version', 'encrypted', 'compressed',
  147. 'hmac_algorithm']
  148. if self.encrypted and self.version < 4:
  149. expected_attrs += ['crypto_algorithm']
  150. if self.version >= 3 and self.compressed:
  151. expected_attrs += ['compression_filter']
  152. if self.version >= 4:
  153. expected_attrs += ['backup_id']
  154. for key in expected_attrs:
  155. if getattr(self, key) is None:
  156. raise QubesException(
  157. "Backup header lack '{}' info".format(key))
  158. else:
  159. raise QubesException(
  160. "Unsupported backup version {}".format(self.version))
  161. def save(self, filename):
  162. '''Save backup header into a file'''
  163. with open(filename, "w") as f_header:
  164. # make sure 'version' is the first key
  165. f_header.write('version={}\n'.format(self.version))
  166. for key, attr in self.header_keys.items():
  167. if key == 'version':
  168. continue
  169. if getattr(self, attr) is None:
  170. continue
  171. f_header.write("{!s}={!s}\n".format(key, getattr(self, attr)))
  172. def launch_proc_with_pty(args, stdin=None, stdout=None, stderr=None, echo=True):
  173. """Similar to pty.fork, but handle stdin/stdout according to parameters
  174. instead of connecting to the pty
  175. :return tuple (subprocess.Popen, pty_master)
  176. """
  177. def set_ctty(ctty_fd, master_fd):
  178. '''Set controlling terminal'''
  179. os.setsid()
  180. os.close(master_fd)
  181. fcntl.ioctl(ctty_fd, termios.TIOCSCTTY, 0)
  182. if not echo:
  183. termios_p = termios.tcgetattr(ctty_fd)
  184. # termios_p.c_lflags
  185. termios_p[3] &= ~termios.ECHO
  186. termios.tcsetattr(ctty_fd, termios.TCSANOW, termios_p)
  187. (pty_master, pty_slave) = os.openpty()
  188. p = subprocess.Popen(args, stdin=stdin, stdout=stdout,
  189. stderr=stderr,
  190. preexec_fn=lambda: set_ctty(pty_slave, pty_master))
  191. os.close(pty_slave)
  192. return p, open(pty_master, 'wb+', buffering=0)
  193. def launch_scrypt(action, input_name, output_name, passphrase):
  194. '''
  195. Launch 'scrypt' process, pass passphrase to it and return
  196. subprocess.Popen object.
  197. :param action: 'enc' or 'dec'
  198. :param input_name: input path or '-' for stdin
  199. :param output_name: output path or '-' for stdout
  200. :param passphrase: passphrase
  201. :return: subprocess.Popen object
  202. '''
  203. command_line = ['scrypt', action, input_name, output_name]
  204. (p, pty) = launch_proc_with_pty(command_line,
  205. stdin=subprocess.PIPE if input_name == '-' else None,
  206. stdout=subprocess.PIPE if output_name == '-' else None,
  207. stderr=subprocess.PIPE,
  208. echo=False)
  209. if action == 'enc':
  210. prompts = (b'Please enter passphrase: ', b'Please confirm passphrase: ')
  211. else:
  212. prompts = (b'Please enter passphrase: ',)
  213. for prompt in prompts:
  214. actual_prompt = p.stderr.read(len(prompt))
  215. if actual_prompt != prompt:
  216. raise QubesException(
  217. 'Unexpected prompt from scrypt: {}'.format(actual_prompt))
  218. pty.write(passphrase.encode('utf-8') + b'\n')
  219. pty.flush()
  220. # save it here, so garbage collector would not close it (which would kill
  221. # the child)
  222. p.pty = pty
  223. return p
  224. class ExtractWorker3(Process):
  225. '''Process for handling inner tar layer of backup archive'''
  226. # pylint: disable=too-many-instance-attributes
  227. def __init__(self, queue, base_dir, passphrase, encrypted,
  228. progress_callback, vmproc=None,
  229. compressed=False, crypto_algorithm=DEFAULT_CRYPTO_ALGORITHM,
  230. compression_filter=None, verify_only=False, handlers=None):
  231. '''Start inner tar extraction worker
  232. The purpose of this class is to process files extracted from outer
  233. archive layer and pass to appropriate handlers. Input files are given
  234. through a queue. Insert :py:obj:`QUEUE_FINISHED` or
  235. :py:obj:`QUEUE_ERROR` to end data processing (either cleanly,
  236. or forcefully).
  237. Handlers are given as a map filename -> (data_func, size_func),
  238. where data_func is called with file-like object to process,
  239. and size_func is called with file size as argument. Note that
  240. data_func and size_func may be called simultaneusly, in a different
  241. processes.
  242. :param multiprocessing.Queue queue: a queue with filenames to
  243. process; those files needs to be given as full path, inside *base_dir*
  244. :param str base_dir: directory where all files to process live
  245. :param str passphrase: passphrase to decrypt the data
  246. :param bool encrypted: is encryption applied?
  247. :param callable progress_callback: report extraction progress
  248. :param subprocess.Popen vmproc: process extracting outer layer,
  249. given here to monitor
  250. it for failures (when it exits with non-zero exit code, inner layer
  251. processing is stopped)
  252. :param bool compressed: is the data compressed?
  253. :param str crypto_algorithm: encryption algorithm, either `scrypt` or an
  254. algorithm supported by openssl
  255. :param str compression_filter: compression program, `gzip` by default
  256. :param bool verify_only: only verify data integrity, do not extract
  257. :param dict handlers: handlers for actual data
  258. '''
  259. super(ExtractWorker3, self).__init__()
  260. #: queue with files to extract
  261. self.queue = queue
  262. #: paths on the queue are relative to this dir
  263. self.base_dir = base_dir
  264. #: passphrase to decrypt/authenticate data
  265. self.passphrase = passphrase
  266. #: handlers for files; it should be dict filename -> (data_function,
  267. # size_function),
  268. # where data_function will get file-like object as the only argument and
  269. # might be called in a separate process (multiprocessing.Process),
  270. # and size_function will get file size (when known) in bytes
  271. self.handlers = handlers
  272. #: is the backup encrypted?
  273. self.encrypted = encrypted
  274. #: is the backup compressed?
  275. self.compressed = compressed
  276. #: what crypto algorithm is used for encryption?
  277. self.crypto_algorithm = crypto_algorithm
  278. #: only verify integrity, don't extract anything
  279. self.verify_only = verify_only
  280. #: progress
  281. self.blocks_backedup = 0
  282. #: inner tar layer extraction (subprocess.Popen instance)
  283. self.tar2_process = None
  284. #: current inner tar archive name
  285. self.tar2_current_file = None
  286. #: cat process feeding tar2_process
  287. self.tar2_feeder = None
  288. #: decompressor subprocess.Popen instance
  289. self.decompressor_process = None
  290. #: decryptor subprocess.Popen instance
  291. self.decryptor_process = None
  292. #: data import multiprocessing.Process instance
  293. self.import_process = None
  294. #: callback reporting progress to UI
  295. self.progress_callback = progress_callback
  296. #: process (subprocess.Popen instance) feeding the data into
  297. # extraction tool
  298. self.vmproc = vmproc
  299. self.log = logging.getLogger('qubesadmin.backup.extract')
  300. self.stderr_encoding = sys.stderr.encoding or 'utf-8'
  301. self.tar2_stderr = []
  302. self.compression_filter = compression_filter
  303. def collect_tar_output(self):
  304. '''Retrieve tar stderr and handle it appropriately
  305. Log errors, process file size if requested.
  306. This use :py:attr:`tar2_process`.
  307. '''
  308. if not self.tar2_process.stderr:
  309. return
  310. if self.tar2_process.poll() is None:
  311. try:
  312. new_lines = self.tar2_process.stderr \
  313. .read(MAX_STDERR_BYTES).splitlines()
  314. except IOError as e:
  315. if e.errno == errno.EAGAIN:
  316. return
  317. else:
  318. raise
  319. else:
  320. new_lines = self.tar2_process.stderr.readlines()
  321. new_lines = [x.decode(self.stderr_encoding) for x in new_lines]
  322. debug_msg = [msg for msg in new_lines if _tar_msg_re.match(msg)]
  323. self.log.debug('tar2_stderr: %s', '\n'.join(debug_msg))
  324. new_lines = [msg for msg in new_lines if not _tar_msg_re.match(msg)]
  325. self.tar2_stderr += new_lines
  326. def run(self):
  327. try:
  328. self.__run__()
  329. except Exception:
  330. # Cleanup children
  331. for process in [self.decompressor_process,
  332. self.decryptor_process,
  333. self.tar2_process]:
  334. if process:
  335. try:
  336. process.terminate()
  337. except OSError:
  338. pass
  339. process.wait()
  340. self.log.exception('ERROR')
  341. raise
  342. def handle_dir(self, dirname):
  343. ''' Relocate files in given director when it's already extracted
  344. :param dirname: directory path to handle (relative to backup root),
  345. without trailing slash
  346. '''
  347. for fname, (data_func, size_func) in self.handlers.items():
  348. if not fname.startswith(dirname + '/'):
  349. continue
  350. if not os.path.exists(fname):
  351. # for example firewall.xml
  352. continue
  353. if size_func is not None:
  354. size_func(os.path.getsize(fname))
  355. with open(fname, 'rb') as input_file:
  356. data_func(input_file)
  357. os.unlink(fname)
  358. shutil.rmtree(dirname)
  359. def cleanup_tar2(self, wait=True, terminate=False):
  360. '''Cleanup running :py:attr:`tar2_process`
  361. :param wait: wait for it termination, otherwise method exit early if
  362. process is still running
  363. :param terminate: terminate the process if still running
  364. '''
  365. if self.tar2_process is None:
  366. return
  367. if terminate:
  368. if self.import_process is not None:
  369. self.tar2_process.terminate()
  370. self.import_process.terminate()
  371. if wait:
  372. self.tar2_process.wait()
  373. if self.import_process is not None:
  374. self.import_process.join()
  375. elif self.tar2_process.poll() is None:
  376. return
  377. self.collect_tar_output()
  378. if self.tar2_process.stderr:
  379. self.tar2_process.stderr.close()
  380. if self.tar2_process.returncode != 0:
  381. self.log.error(
  382. "ERROR: unable to extract files for %s, tar "
  383. "output:\n %s",
  384. self.tar2_current_file,
  385. "\n ".join(self.tar2_stderr))
  386. else:
  387. # Finished extracting the tar file
  388. # if that was whole-directory archive, handle
  389. # relocated files now
  390. inner_name = self.tar2_current_file.rsplit('.', 1)[0] \
  391. .replace(self.base_dir + '/', '')
  392. if os.path.basename(inner_name) == '.':
  393. self.handle_dir(
  394. os.path.dirname(inner_name))
  395. self.tar2_current_file = None
  396. self.tar2_process = None
  397. def _data_import_wrapper(self, close_fds, data_func, size_func,
  398. tar2_process):
  399. '''Close not needed file descriptors, handle output size reported
  400. by tar (if needed) then call data_func(tar2_process.stdout).
  401. This is to prevent holding write end of a pipe in subprocess,
  402. preventing EOF transfer.
  403. '''
  404. for fd in close_fds:
  405. if fd in (tar2_process.stdout.fileno(),
  406. tar2_process.stderr.fileno()):
  407. continue
  408. try:
  409. os.close(fd)
  410. except OSError:
  411. pass
  412. # retrieve file size from tar's stderr; warning: we do
  413. # not read data from tar's stdout at this point, it will
  414. # hang if it tries to output file content before
  415. # reporting its size on stderr first
  416. if size_func:
  417. # process lines on stderr until we get file size
  418. # search for first file size reported by tar -
  419. # this is used only when extracting single-file archive, so don't
  420. # bother with checking file name
  421. # Also, this needs to be called before anything is retrieved
  422. # from tar stderr, otherwise the process may deadlock waiting for
  423. # size (at this point nothing is retrieving data from tar stdout
  424. # yet, so it will hang on write() when the output pipe fill up).
  425. while True:
  426. line = tar2_process.stderr.readline()
  427. if not line:
  428. self.log.warning('EOF from tar before got file size info')
  429. break
  430. line = line.decode()
  431. if _tar_msg_re.match(line):
  432. self.log.debug('tar2_stderr: %s', line)
  433. else:
  434. match = _tar_file_size_re.match(line)
  435. if match:
  436. file_size = match.groups()[0]
  437. size_func(file_size)
  438. break
  439. else:
  440. self.log.warning(
  441. 'unexpected tar output (no file size report): %s',
  442. line)
  443. return data_func(tar2_process.stdout)
  444. def feed_tar2(self, filename, input_pipe):
  445. '''Feed data from *filename* to *input_pipe*
  446. Start a cat process to do that (do not block this process). Cat
  447. subprocess instance will be in :py:attr:`tar2_feeder`
  448. '''
  449. assert self.tar2_feeder is None
  450. self.tar2_feeder = subprocess.Popen(['cat', filename],
  451. stdout=input_pipe)
  452. def check_processes(self, processes):
  453. '''Check if any process failed.
  454. And if so, wait for other relevant processes to cleanup.
  455. '''
  456. run_error = None
  457. for name, proc in processes.items():
  458. if proc is None:
  459. continue
  460. if isinstance(proc, Process):
  461. if not proc.is_alive() and proc.exitcode != 0:
  462. run_error = name
  463. break
  464. elif proc.poll():
  465. run_error = name
  466. break
  467. if run_error:
  468. if run_error == "target":
  469. self.collect_tar_output()
  470. details = "\n".join(self.tar2_stderr)
  471. else:
  472. details = "%s failed" % run_error
  473. if self.decryptor_process:
  474. self.decryptor_process.terminate()
  475. self.decryptor_process.wait()
  476. self.decryptor_process = None
  477. self.log.error('Error while processing \'%s\': %s',
  478. self.tar2_current_file, details)
  479. self.cleanup_tar2(wait=True, terminate=True)
  480. def __run__(self):
  481. self.log.debug("Started sending thread")
  482. self.log.debug("Moving to dir " + self.base_dir)
  483. os.chdir(self.base_dir)
  484. filename = None
  485. input_pipe = None
  486. for filename in iter(self.queue.get, None):
  487. if filename in (QUEUE_FINISHED, QUEUE_ERROR):
  488. break
  489. assert isinstance(filename, str)
  490. self.log.debug("Extracting file " + filename)
  491. if filename.endswith('.000'):
  492. # next file
  493. if self.tar2_process is not None:
  494. input_pipe.close()
  495. self.cleanup_tar2(wait=True, terminate=False)
  496. inner_name = filename[:-len('.000')].replace(
  497. self.base_dir + '/', '')
  498. redirect_stdout = None
  499. if os.path.basename(inner_name) == '.':
  500. if (inner_name in self.handlers or
  501. any(x.startswith(os.path.dirname(inner_name) + '/')
  502. for x in self.handlers)):
  503. tar2_cmdline = ['tar',
  504. '-%s' % ("t" if self.verify_only else "x"),
  505. inner_name]
  506. else:
  507. # ignore this directory
  508. tar2_cmdline = None
  509. elif inner_name in self.handlers:
  510. tar2_cmdline = ['tar',
  511. '-%svvO' % ("t" if self.verify_only else "x"),
  512. inner_name]
  513. redirect_stdout = subprocess.PIPE
  514. else:
  515. # no handlers for this file, ignore it
  516. tar2_cmdline = None
  517. if tar2_cmdline is None:
  518. # ignore the file
  519. os.remove(filename)
  520. continue
  521. if self.compressed:
  522. if self.compression_filter:
  523. tar2_cmdline.insert(-1,
  524. "--use-compress-program=%s" %
  525. self.compression_filter)
  526. else:
  527. tar2_cmdline.insert(-1, "--use-compress-program=%s" %
  528. DEFAULT_COMPRESSION_FILTER)
  529. self.log.debug("Running command " + str(tar2_cmdline))
  530. if self.encrypted:
  531. # Start decrypt
  532. self.decryptor_process = subprocess.Popen(
  533. ["openssl", "enc",
  534. "-d",
  535. "-" + self.crypto_algorithm,
  536. "-pass",
  537. "pass:" + self.passphrase],
  538. stdin=subprocess.PIPE,
  539. stdout=subprocess.PIPE)
  540. self.tar2_process = subprocess.Popen(
  541. tar2_cmdline,
  542. stdin=self.decryptor_process.stdout,
  543. stdout=redirect_stdout,
  544. stderr=subprocess.PIPE)
  545. self.decryptor_process.stdout.close()
  546. input_pipe = self.decryptor_process.stdin
  547. else:
  548. self.tar2_process = subprocess.Popen(
  549. tar2_cmdline,
  550. stdin=subprocess.PIPE,
  551. stdout=redirect_stdout,
  552. stderr=subprocess.PIPE)
  553. input_pipe = self.tar2_process.stdin
  554. self.feed_tar2(filename, input_pipe)
  555. if inner_name in self.handlers:
  556. assert redirect_stdout is subprocess.PIPE
  557. data_func, size_func = self.handlers[inner_name]
  558. self.import_process = multiprocessing.Process(
  559. target=self._data_import_wrapper,
  560. args=([input_pipe.fileno()],
  561. data_func, size_func, self.tar2_process))
  562. self.import_process.start()
  563. self.tar2_process.stdout.close()
  564. self.tar2_stderr = []
  565. elif not self.tar2_process:
  566. # Extracting of the current archive failed, skip to the next
  567. # archive
  568. os.remove(filename)
  569. continue
  570. else:
  571. # os.path.splitext fails to handle 'something/..000'
  572. (basename, ext) = self.tar2_current_file.rsplit('.', 1)
  573. previous_chunk_number = int(ext)
  574. expected_filename = basename + '.%03d' % (
  575. previous_chunk_number+1)
  576. if expected_filename != filename:
  577. self.cleanup_tar2(wait=True, terminate=True)
  578. self.log.error(
  579. 'Unexpected file in archive: %s, expected %s',
  580. filename, expected_filename)
  581. os.remove(filename)
  582. continue
  583. self.log.debug("Releasing next chunck")
  584. self.feed_tar2(filename, input_pipe)
  585. self.tar2_current_file = filename
  586. self.tar2_feeder.wait()
  587. # check if any process failed
  588. processes = {
  589. 'target': self.tar2_feeder,
  590. 'vmproc': self.vmproc,
  591. 'addproc': self.tar2_process,
  592. 'data_import': self.import_process,
  593. 'decryptor': self.decryptor_process,
  594. }
  595. self.check_processes(processes)
  596. self.tar2_feeder = None
  597. if callable(self.progress_callback):
  598. self.progress_callback(os.path.getsize(filename))
  599. # Delete the file as we don't need it anymore
  600. self.log.debug('Removing file %s', filename)
  601. os.remove(filename)
  602. if self.tar2_process is not None:
  603. input_pipe.close()
  604. if filename == QUEUE_ERROR:
  605. if self.decryptor_process:
  606. self.decryptor_process.terminate()
  607. self.decryptor_process.wait()
  608. self.decryptor_process = None
  609. self.cleanup_tar2(terminate=(filename == QUEUE_ERROR))
  610. self.log.debug('Finished extracting thread')
  611. def get_supported_hmac_algo(hmac_algorithm=None):
  612. '''Generate a list of supported hmac algorithms
  613. :param hmac_algorithm: default algorithm, if given, it is placed as a
  614. first element
  615. '''
  616. # Start with provided default
  617. if hmac_algorithm:
  618. yield hmac_algorithm
  619. if hmac_algorithm != 'scrypt':
  620. yield 'scrypt'
  621. proc = subprocess.Popen(['openssl', 'list-message-digest-algorithms'],
  622. stdout=subprocess.PIPE)
  623. try:
  624. for algo in proc.stdout.readlines():
  625. algo = algo.decode('ascii')
  626. if '=>' in algo:
  627. continue
  628. yield algo.strip()
  629. finally:
  630. proc.terminate()
  631. proc.wait()
  632. proc.stdout.close()
  633. class BackupRestoreOptions(object):
  634. '''Options for restore operation'''
  635. # pylint: disable=too-few-public-methods
  636. def __init__(self):
  637. #: use default NetVM if the one referenced in backup do not exists on
  638. # the host
  639. self.use_default_netvm = True
  640. #: set NetVM to "none" if the one referenced in backup do not exists
  641. # on the host
  642. self.use_none_netvm = False
  643. #: set template to default if the one referenced in backup do not
  644. # exists on the host
  645. self.use_default_template = True
  646. #: use default kernel if the one referenced in backup do not exists
  647. # on the host
  648. self.use_default_kernel = True
  649. #: restore dom0 home
  650. self.dom0_home = True
  651. #: restore dom0 home even if username is different
  652. self.ignore_username_mismatch = False
  653. #: do not restore data, only verify backup integrity
  654. self.verify_only = False
  655. #: automatically rename VM during restore, when it would conflict
  656. # with existing one
  657. self.rename_conflicting = True
  658. #: list of VM names to exclude
  659. self.exclude = []
  660. #: restore VMs into selected storage pool
  661. self.override_pool = None
  662. #: ignore size limit calculated from backup metadata
  663. self.ignore_size_limit = False
  664. class BackupRestore(object):
  665. """Usage:
  666. >>> restore_op = BackupRestore(...)
  667. >>> # adjust restore_op.options here
  668. >>> restore_info = restore_op.get_restore_info()
  669. >>> # manipulate restore_info to select VMs to restore here
  670. >>> restore_op.restore_do(restore_info)
  671. """
  672. class VMToRestore(object):
  673. '''Information about a single VM to be restored'''
  674. # pylint: disable=too-few-public-methods
  675. #: VM excluded from restore by user
  676. EXCLUDED = object()
  677. #: VM with such name already exists on the host
  678. ALREADY_EXISTS = object()
  679. #: NetVM used by the VM does not exists on the host
  680. MISSING_NETVM = object()
  681. #: TemplateVM used by the VM does not exists on the host
  682. MISSING_TEMPLATE = object()
  683. #: Kernel used by the VM does not exists on the host
  684. MISSING_KERNEL = object()
  685. def __init__(self, vm):
  686. assert isinstance(vm, BackupVM)
  687. self.vm = vm
  688. self.name = vm.name
  689. self.subdir = vm.backup_path
  690. self.size = vm.size
  691. self.problems = set()
  692. self.template = vm.template
  693. if vm.properties.get('netvm', None):
  694. self.netvm = vm.properties['netvm']
  695. else:
  696. self.netvm = None
  697. self.orig_template = None
  698. self.restored_vm = None
  699. @property
  700. def good_to_go(self):
  701. '''Is the VM ready for restore?'''
  702. return len(self.problems) == 0
  703. class Dom0ToRestore(VMToRestore):
  704. '''Information about dom0 home to restore'''
  705. # pylint: disable=too-few-public-methods
  706. #: backup was performed on system with different dom0 username
  707. USERNAME_MISMATCH = object()
  708. def __init__(self, vm, subdir=None):
  709. super(BackupRestore.Dom0ToRestore, self).__init__(vm)
  710. if subdir:
  711. self.subdir = subdir
  712. self.username = os.path.basename(subdir)
  713. def __init__(self, app, backup_location, backup_vm, passphrase):
  714. super(BackupRestore, self).__init__()
  715. #: qubes.Qubes instance
  716. self.app = app
  717. #: options how the backup should be restored
  718. self.options = BackupRestoreOptions()
  719. #: VM from which backup should be retrieved
  720. self.backup_vm = backup_vm
  721. if backup_vm and backup_vm.qid == 0:
  722. self.backup_vm = None
  723. #: backup path, inside VM pointed by :py:attr:`backup_vm`
  724. self.backup_location = backup_location
  725. #: passphrase protecting backup integrity and optionally decryption
  726. self.passphrase = passphrase
  727. #: temporary directory used to extract the data before moving to the
  728. # final location
  729. self.tmpdir = tempfile.mkdtemp(prefix="restore", dir="/var/tmp")
  730. #: list of processes (Popen objects) to kill on cancel
  731. self.processes_to_kill_on_cancel = []
  732. #: is the backup operation canceled
  733. self.canceled = False
  734. #: report restore progress, called with one argument - percents of
  735. # data restored
  736. # FIXME: convert to float [0,1]
  737. self.progress_callback = None
  738. self.log = logging.getLogger('qubesadmin.backup')
  739. #: basic information about the backup
  740. self.header_data = self._retrieve_backup_header()
  741. #: VMs included in the backup
  742. self.backup_app = self._process_qubes_xml()
  743. def _start_retrieval_process(self, filelist, limit_count, limit_bytes):
  744. """Retrieve backup stream and extract it to :py:attr:`tmpdir`
  745. :param filelist: list of files to extract; listing directory name
  746. will extract the whole directory; use empty list to extract the whole
  747. archive
  748. :param limit_count: maximum number of files to extract
  749. :param limit_bytes: maximum size of extracted data
  750. :return: a touple of (Popen object of started process, file-like
  751. object for reading extracted files list, file-like object for reading
  752. errors)
  753. """
  754. vmproc = None
  755. if self.backup_vm is not None:
  756. # If APPVM, STDOUT is a PIPE
  757. vmproc = self.backup_vm.run_service('qubes.Restore')
  758. vmproc.stdin.write(
  759. (self.backup_location.replace("\r", "").replace("\n",
  760. "") + "\n").encode())
  761. vmproc.stdin.flush()
  762. # Send to tar2qfile the VMs that should be extracted
  763. vmproc.stdin.write((" ".join(filelist) + "\n").encode())
  764. vmproc.stdin.flush()
  765. self.processes_to_kill_on_cancel.append(vmproc)
  766. backup_stdin = vmproc.stdout
  767. # FIXME use /usr/lib/qubes/qfile-unpacker in non-dom0
  768. tar1_command = ['/usr/libexec/qubes/qfile-dom0-unpacker',
  769. str(os.getuid()), self.tmpdir, '-v']
  770. else:
  771. backup_stdin = open(self.backup_location, 'rb')
  772. tar1_command = ['tar',
  773. '-ixv',
  774. '--occurrence=1',
  775. '-C', self.tmpdir] + filelist
  776. tar1_env = os.environ.copy()
  777. tar1_env['UPDATES_MAX_BYTES'] = str(limit_bytes)
  778. tar1_env['UPDATES_MAX_FILES'] = str(limit_count)
  779. self.log.debug("Run command" + str(tar1_command))
  780. command = subprocess.Popen(
  781. tar1_command,
  782. stdin=backup_stdin,
  783. stdout=vmproc.stdin if vmproc else subprocess.PIPE,
  784. stderr=subprocess.PIPE,
  785. env=tar1_env)
  786. backup_stdin.close()
  787. self.processes_to_kill_on_cancel.append(command)
  788. # qfile-dom0-unpacker output filelist on stderr
  789. # and have stdout connected to the VM), while tar output filelist
  790. # on stdout
  791. if self.backup_vm:
  792. filelist_pipe = command.stderr
  793. # let qfile-dom0-unpacker hold the only open FD to the write end of
  794. # pipe, otherwise qrexec-client will not receive EOF when
  795. # qfile-dom0-unpacker terminates
  796. vmproc.stdin.close()
  797. else:
  798. filelist_pipe = command.stdout
  799. if self.backup_vm:
  800. error_pipe = vmproc.stderr
  801. else:
  802. error_pipe = command.stderr
  803. return command, filelist_pipe, error_pipe
  804. def _verify_hmac(self, filename, hmacfile, algorithm=None):
  805. '''Verify hmac of a file using given algorithm.
  806. If algorithm is not specified, use the one from backup header (
  807. :py:attr:`header_data`).
  808. Raise :py:exc:`QubesException` on failure, return :py:obj:`True` on
  809. success.
  810. 'scrypt' algorithm is supported only for header file; hmac file is
  811. encrypted (and integrity protected) version of plain header.
  812. :param filename: path to file to be verified
  813. :param hmacfile: path to hmac file for *filename*
  814. :param algorithm: override algorithm
  815. '''
  816. def load_hmac(hmac_text):
  817. '''Parse hmac output by openssl.
  818. Return just hmac, without filename and other metadata.
  819. '''
  820. if any(ord(x) not in range(128) for x in hmac_text):
  821. raise QubesException(
  822. "Invalid content of {}".format(hmacfile))
  823. hmac_text = hmac_text.strip().split("=")
  824. if len(hmac_text) > 1:
  825. hmac_text = hmac_text[1].strip()
  826. else:
  827. raise QubesException(
  828. "ERROR: invalid hmac file content")
  829. return hmac_text
  830. if algorithm is None:
  831. algorithm = self.header_data.hmac_algorithm
  832. passphrase = self.passphrase.encode('utf-8')
  833. self.log.debug("Verifying file %s", filename)
  834. if os.stat(os.path.join(self.tmpdir, hmacfile)).st_size > \
  835. HMAC_MAX_SIZE:
  836. raise QubesException('HMAC file {} too large'.format(
  837. hmacfile))
  838. if hmacfile != filename + ".hmac":
  839. raise QubesException(
  840. "ERROR: expected hmac for {}, but got {}".
  841. format(filename, hmacfile))
  842. if algorithm == 'scrypt':
  843. # in case of 'scrypt' _verify_hmac is only used for backup header
  844. assert filename == HEADER_FILENAME
  845. self._verify_and_decrypt(hmacfile, HEADER_FILENAME + '.dec')
  846. f_name = os.path.join(self.tmpdir, filename)
  847. with open(f_name, 'rb') as f_one:
  848. with open(f_name + '.dec', 'rb') as f_two:
  849. if f_one.read() != f_two.read():
  850. raise QubesException(
  851. 'Invalid hmac on {}'.format(filename))
  852. else:
  853. return True
  854. with open(os.path.join(self.tmpdir, filename), 'rb') as f_input:
  855. hmac_proc = subprocess.Popen(
  856. ["openssl", "dgst", "-" + algorithm, "-hmac", passphrase],
  857. stdin=f_input,
  858. stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  859. hmac_stdout, hmac_stderr = hmac_proc.communicate()
  860. if hmac_stderr:
  861. raise QubesException(
  862. "ERROR: verify file {0}: {1}".format(filename, hmac_stderr))
  863. else:
  864. self.log.debug("Loading hmac for file %s", filename)
  865. try:
  866. with open(os.path.join(self.tmpdir, hmacfile), 'r',
  867. encoding='ascii') as f_hmac:
  868. hmac = load_hmac(f_hmac.read())
  869. except UnicodeDecodeError as err:
  870. raise QubesException('Cannot load hmac file: ' + str(err))
  871. if hmac and load_hmac(hmac_stdout.decode('ascii')) == hmac:
  872. os.unlink(os.path.join(self.tmpdir, hmacfile))
  873. self.log.debug(
  874. "File verification OK -> Sending file %s", filename)
  875. return True
  876. else:
  877. raise QubesException(
  878. "ERROR: invalid hmac for file {0}: {1}. "
  879. "Is the passphrase correct?".
  880. format(filename, load_hmac(hmac_stdout.decode('ascii'))))
  881. def _verify_and_decrypt(self, filename, output=None):
  882. '''Handle scrypt-wrapped file
  883. Decrypt the file, and verify its integrity - both tasks handled by
  884. 'scrypt' tool. Filename (without extension) is also validated.
  885. :param filename: Input file name (relative to :py:attr:`tmpdir`),
  886. needs to have `.enc` or `.hmac` extension
  887. :param output: Output file name (relative to :py:attr:`tmpdir`),
  888. use :py:obj:`None` to use *filename* without extension
  889. :return: *filename* without extension
  890. '''
  891. assert filename.endswith('.enc') or filename.endswith('.hmac')
  892. fullname = os.path.join(self.tmpdir, filename)
  893. (origname, _) = os.path.splitext(filename)
  894. if output:
  895. fulloutput = os.path.join(self.tmpdir, output)
  896. else:
  897. fulloutput = os.path.join(self.tmpdir, origname)
  898. if origname == HEADER_FILENAME:
  899. passphrase = u'{filename}!{passphrase}'.format(
  900. filename=origname,
  901. passphrase=self.passphrase)
  902. else:
  903. passphrase = u'{backup_id}!{filename}!{passphrase}'.format(
  904. backup_id=self.header_data.backup_id,
  905. filename=origname,
  906. passphrase=self.passphrase)
  907. try:
  908. p = launch_scrypt('dec', fullname, fulloutput, passphrase)
  909. except OSError as err:
  910. raise QubesException('failed to decrypt {}: {!s}'.format(
  911. fullname, err))
  912. (_, stderr) = p.communicate()
  913. if hasattr(p, 'pty'):
  914. p.pty.close()
  915. if p.returncode != 0:
  916. os.unlink(fulloutput)
  917. raise QubesException('failed to decrypt {}: {}'.format(
  918. fullname, stderr))
  919. # encrypted file is no longer needed
  920. os.unlink(fullname)
  921. return origname
  922. def _retrieve_backup_header_files(self, files, allow_none=False):
  923. '''Retrieve backup header.
  924. Start retrieval process (possibly involving network access from
  925. another VM). Returns a collection of retrieved file paths.
  926. '''
  927. (retrieve_proc, filelist_pipe, error_pipe) = \
  928. self._start_retrieval_process(
  929. files, len(files), 1024 * 1024)
  930. filelist = filelist_pipe.read()
  931. filelist_pipe.close()
  932. retrieve_proc_returncode = retrieve_proc.wait()
  933. if retrieve_proc in self.processes_to_kill_on_cancel:
  934. self.processes_to_kill_on_cancel.remove(retrieve_proc)
  935. extract_stderr = error_pipe.read(MAX_STDERR_BYTES)
  936. error_pipe.close()
  937. # wait for other processes (if any)
  938. for proc in self.processes_to_kill_on_cancel:
  939. if proc.wait() != 0:
  940. raise QubesException(
  941. "Backup header retrieval failed (exit code {})".format(
  942. proc.wait())
  943. )
  944. if retrieve_proc_returncode != 0:
  945. if not filelist and 'Not found in archive' in extract_stderr:
  946. if allow_none:
  947. return None
  948. else:
  949. raise QubesException(
  950. "unable to read the qubes backup file {0} ({1}): {2}".
  951. format(
  952. self.backup_location,
  953. retrieve_proc.wait(),
  954. extract_stderr
  955. ))
  956. actual_files = filelist.decode('ascii').splitlines()
  957. if sorted(actual_files) != sorted(files):
  958. raise QubesException(
  959. 'unexpected files in archive: got {!r}, expected {!r}'.format(
  960. actual_files, files
  961. ))
  962. for fname in files:
  963. if not os.path.exists(os.path.join(self.tmpdir, fname)):
  964. if allow_none:
  965. return None
  966. else:
  967. raise QubesException(
  968. 'Unable to retrieve file {} from backup {}: {}'.format(
  969. fname, self.backup_location, extract_stderr
  970. )
  971. )
  972. return files
  973. def _retrieve_backup_header(self):
  974. """Retrieve backup header and qubes.xml. Only backup header is
  975. analyzed, qubes.xml is left as-is
  976. (not even verified/decrypted/uncompressed)
  977. :return header_data
  978. :rtype :py:class:`BackupHeader`
  979. """
  980. if not self.backup_vm and os.path.exists(
  981. os.path.join(self.backup_location, 'qubes.xml')):
  982. # backup format version 1 doesn't have header
  983. header_data = BackupHeader()
  984. header_data.version = 1
  985. return header_data
  986. header_files = self._retrieve_backup_header_files(
  987. ['backup-header', 'backup-header.hmac'], allow_none=True)
  988. if not header_files:
  989. # R2-Beta3 didn't have backup header, so if none is found,
  990. # assume it's version=2 and use values present at that time
  991. header_data = BackupHeader(
  992. version=2,
  993. # place explicitly this value, because it is what format_version
  994. # 2 have
  995. hmac_algorithm='SHA1',
  996. crypto_algorithm='aes-256-cbc',
  997. # TODO: set encrypted to something...
  998. )
  999. else:
  1000. filename = HEADER_FILENAME
  1001. hmacfile = HEADER_FILENAME + '.hmac'
  1002. self.log.debug("Got backup header and hmac: %s, %s",
  1003. filename, hmacfile)
  1004. file_ok = False
  1005. hmac_algorithm = DEFAULT_HMAC_ALGORITHM
  1006. for hmac_algo in get_supported_hmac_algo(hmac_algorithm):
  1007. try:
  1008. if self._verify_hmac(filename, hmacfile, hmac_algo):
  1009. file_ok = True
  1010. break
  1011. except QubesException as err:
  1012. self.log.debug(
  1013. 'Failed to verify %s using %s: %r',
  1014. hmacfile, hmac_algo, err)
  1015. # Ignore exception here, try the next algo
  1016. if not file_ok:
  1017. raise QubesException(
  1018. "Corrupted backup header (hmac verification "
  1019. "failed). Is the password correct?")
  1020. filename = os.path.join(self.tmpdir, filename)
  1021. with open(filename, 'rb') as f_header:
  1022. header_data = BackupHeader(f_header.read())
  1023. os.unlink(filename)
  1024. return header_data
  1025. def _start_inner_extraction_worker(self, queue, handlers):
  1026. """Start a worker process, extracting inner layer of bacup archive,
  1027. extract them to :py:attr:`tmpdir`.
  1028. End the data by pushing QUEUE_FINISHED or QUEUE_ERROR to the queue.
  1029. :param queue :py:class:`Queue` object to handle files from
  1030. """
  1031. # Setup worker to extract encrypted data chunks to the restore dirs
  1032. # Create the process here to pass it options extracted from
  1033. # backup header
  1034. extractor_params = {
  1035. 'queue': queue,
  1036. 'base_dir': self.tmpdir,
  1037. 'passphrase': self.passphrase,
  1038. 'encrypted': self.header_data.encrypted,
  1039. 'compressed': self.header_data.compressed,
  1040. 'crypto_algorithm': self.header_data.crypto_algorithm,
  1041. 'verify_only': self.options.verify_only,
  1042. 'progress_callback': self.progress_callback,
  1043. 'handlers': handlers,
  1044. }
  1045. self.log.debug(
  1046. 'Starting extraction worker in %s, file handlers map: %s',
  1047. self.tmpdir, repr(handlers))
  1048. format_version = self.header_data.version
  1049. if format_version in [3, 4]:
  1050. extractor_params['compression_filter'] = \
  1051. self.header_data.compression_filter
  1052. if format_version == 4:
  1053. # encryption already handled
  1054. extractor_params['encrypted'] = False
  1055. extract_proc = ExtractWorker3(**extractor_params)
  1056. else:
  1057. raise NotImplementedError(
  1058. "Backup format version %d not supported" % format_version)
  1059. extract_proc.start()
  1060. return extract_proc
  1061. @staticmethod
  1062. def _save_qubes_xml(path, stream):
  1063. '''Handler for qubes.xml.000 content - just save the data to a file'''
  1064. with open(path, 'wb') as f_qubesxml:
  1065. f_qubesxml.write(stream.read())
  1066. def _process_qubes_xml(self):
  1067. """Verify, unpack and load qubes.xml. Possibly convert its format if
  1068. necessary. It expect that :py:attr:`header_data` is already populated,
  1069. and :py:meth:`retrieve_backup_header` was called.
  1070. """
  1071. if self.header_data.version == 1:
  1072. raise NotImplementedError('Backup format version 1 not supported')
  1073. elif self.header_data.version in [2, 3]:
  1074. self._retrieve_backup_header_files(
  1075. ['qubes.xml.000', 'qubes.xml.000.hmac'])
  1076. self._verify_hmac("qubes.xml.000", "qubes.xml.000.hmac")
  1077. else:
  1078. self._retrieve_backup_header_files(['qubes.xml.000.enc'])
  1079. self._verify_and_decrypt('qubes.xml.000.enc')
  1080. queue = Queue()
  1081. queue.put("qubes.xml.000")
  1082. queue.put(QUEUE_FINISHED)
  1083. qubes_xml_path = os.path.join(self.tmpdir, 'qubes-restored.xml')
  1084. handlers = {
  1085. 'qubes.xml': (
  1086. functools.partial(self._save_qubes_xml, qubes_xml_path),
  1087. None)
  1088. }
  1089. extract_proc = self._start_inner_extraction_worker(queue, handlers)
  1090. extract_proc.join()
  1091. if extract_proc.exitcode != 0:
  1092. raise QubesException(
  1093. "unable to extract the qubes backup. "
  1094. "Check extracting process errors.")
  1095. if self.header_data.version in [2, 3]:
  1096. backup_app = Core2Qubes(qubes_xml_path)
  1097. elif self.header_data.version in [4]:
  1098. backup_app = Core3Qubes(qubes_xml_path)
  1099. else:
  1100. raise QubesException(
  1101. 'Unsupported qubes.xml format version: {}'.format(
  1102. self.header_data.version))
  1103. # Not needed anymore - all the data stored in backup_app
  1104. os.unlink(qubes_xml_path)
  1105. return backup_app
  1106. def _restore_vm_data(self, vms_dirs, vms_size, handlers):
  1107. '''Restore data of VMs
  1108. :param vms_dirs: list of directories to extract (skip others)
  1109. :param vms_size: expected size (abort if source stream exceed this
  1110. value)
  1111. :param handlers: handlers for restored files - see
  1112. :py:class:`ExtractWorker3` for details
  1113. '''
  1114. # Currently each VM consists of at most 7 archives (count
  1115. # file_to_backup calls in backup_prepare()), but add some safety
  1116. # margin for further extensions. Each archive is divided into 100MB
  1117. # chunks. Additionally each file have own hmac file. So assume upper
  1118. # limit as 2*(10*COUNT_OF_VMS+TOTAL_SIZE/100MB)
  1119. limit_count = str(2 * (10 * len(vms_dirs) +
  1120. int(vms_size / (100 * 1024 * 1024))))
  1121. if self.options.ignore_size_limit:
  1122. limit_count = '0'
  1123. vms_size = 0
  1124. self.log.debug("Working in temporary dir: %s", self.tmpdir)
  1125. self.log.info("Extracting data: %s to restore", size_to_human(vms_size))
  1126. # retrieve backup from the backup stream (either VM, or dom0 file)
  1127. (retrieve_proc, filelist_pipe, error_pipe) = \
  1128. self._start_retrieval_process(
  1129. vms_dirs, limit_count, vms_size)
  1130. to_extract = Queue()
  1131. # extract data retrieved by retrieve_proc
  1132. extract_proc = self._start_inner_extraction_worker(
  1133. to_extract, handlers)
  1134. try:
  1135. filename = None
  1136. hmacfile = None
  1137. nextfile = None
  1138. while True:
  1139. if self.canceled:
  1140. break
  1141. if not extract_proc.is_alive():
  1142. retrieve_proc.terminate()
  1143. retrieve_proc.wait()
  1144. if retrieve_proc in self.processes_to_kill_on_cancel:
  1145. self.processes_to_kill_on_cancel.remove(retrieve_proc)
  1146. # wait for other processes (if any)
  1147. for proc in self.processes_to_kill_on_cancel:
  1148. proc.wait()
  1149. break
  1150. if nextfile is not None:
  1151. filename = nextfile
  1152. else:
  1153. filename = filelist_pipe.readline().decode('ascii').strip()
  1154. self.log.debug("Getting new file: %s", filename)
  1155. if not filename or filename == "EOF":
  1156. break
  1157. # if reading archive directly with tar, wait for next filename -
  1158. # tar prints filename before processing it, so wait for
  1159. # the next one to be sure that whole file was extracted
  1160. if not self.backup_vm:
  1161. nextfile = filelist_pipe.readline().decode('ascii').strip()
  1162. if self.header_data.version in [2, 3]:
  1163. if not self.backup_vm:
  1164. hmacfile = nextfile
  1165. nextfile = filelist_pipe.readline().\
  1166. decode('ascii').strip()
  1167. else:
  1168. hmacfile = filelist_pipe.readline().\
  1169. decode('ascii').strip()
  1170. if self.canceled:
  1171. break
  1172. self.log.debug("Getting hmac: %s", hmacfile)
  1173. if not hmacfile or hmacfile == "EOF":
  1174. # Premature end of archive, either of tar1_command or
  1175. # vmproc exited with error
  1176. break
  1177. else: # self.header_data.version == 4
  1178. if not filename.endswith('.enc'):
  1179. raise qubesadmin.exc.QubesException(
  1180. 'Invalid file extension found in archive: {}'.
  1181. format(filename))
  1182. if not any(filename.startswith(x) for x in vms_dirs):
  1183. self.log.debug("Ignoring VM not selected for restore")
  1184. os.unlink(os.path.join(self.tmpdir, filename))
  1185. if hmacfile:
  1186. os.unlink(os.path.join(self.tmpdir, hmacfile))
  1187. continue
  1188. if self.header_data.version in [2, 3]:
  1189. self._verify_hmac(filename, hmacfile)
  1190. else:
  1191. # _verify_and_decrypt will write output to a file with
  1192. # '.enc' extension cut off. This is safe because:
  1193. # - `scrypt` tool will override output, so if the file was
  1194. # already there (received from the VM), it will be removed
  1195. # - incoming archive extraction will refuse to override
  1196. # existing file, so if `scrypt` already created one,
  1197. # it can not be manipulated by the VM
  1198. # - when the file is retrieved from the VM, it appears at
  1199. # the final form - if it's visible, VM have no longer
  1200. # influence over its content
  1201. #
  1202. # This all means that if the file was correctly verified
  1203. # + decrypted, we will surely access the right file
  1204. filename = self._verify_and_decrypt(filename)
  1205. to_extract.put(os.path.join(self.tmpdir, filename))
  1206. if self.canceled:
  1207. raise BackupCanceledError("Restore canceled",
  1208. tmpdir=self.tmpdir)
  1209. if retrieve_proc.wait() != 0:
  1210. if retrieve_proc.returncode == errno.EDQUOT:
  1211. raise QubesException(
  1212. 'retrieved backup size exceed expected size, if you '
  1213. 'believe this is ok, use --ignore-size-limit option')
  1214. else:
  1215. raise QubesException(
  1216. "unable to read the qubes backup file {} ({}): {}"
  1217. .format(self.backup_location,
  1218. retrieve_proc.returncode, error_pipe.read(
  1219. MAX_STDERR_BYTES)))
  1220. # wait for other processes (if any)
  1221. for proc in self.processes_to_kill_on_cancel:
  1222. proc.wait()
  1223. if proc.returncode != 0:
  1224. raise QubesException(
  1225. "Backup completed, "
  1226. "but VM sending it reported an error (exit code {})".
  1227. format(proc.returncode))
  1228. if filename and filename != "EOF":
  1229. raise QubesException(
  1230. "Premature end of archive, the last file was %s" % filename)
  1231. except:
  1232. to_extract.put(QUEUE_ERROR)
  1233. extract_proc.join()
  1234. raise
  1235. else:
  1236. to_extract.put(QUEUE_FINISHED)
  1237. finally:
  1238. error_pipe.close()
  1239. filelist_pipe.close()
  1240. self.log.debug("Waiting for the extraction process to finish...")
  1241. extract_proc.join()
  1242. self.log.debug("Extraction process finished with code: %s",
  1243. extract_proc.exitcode)
  1244. if extract_proc.exitcode != 0:
  1245. raise QubesException(
  1246. "unable to extract the qubes backup. "
  1247. "Check extracting process errors.")
  1248. def new_name_for_conflicting_vm(self, orig_name, restore_info):
  1249. '''Generate new name for conflicting VM
  1250. Add a number suffix, until the name is unique. If no unique name can
  1251. be found using this strategy, return :py:obj:`None`
  1252. '''
  1253. number = 1
  1254. if len(orig_name) > 29:
  1255. orig_name = orig_name[0:29]
  1256. new_name = orig_name
  1257. while (new_name in restore_info.keys() or
  1258. new_name in [x.name for x in restore_info.values()] or
  1259. new_name in self.app.domains):
  1260. new_name = str('{}{}'.format(orig_name, number))
  1261. number += 1
  1262. if number == 100:
  1263. # give up
  1264. return None
  1265. return new_name
  1266. def restore_info_verify(self, restore_info):
  1267. '''Verify restore info - validate VM dependencies, name conflicts
  1268. etc.
  1269. '''
  1270. for vm in restore_info.keys():
  1271. if vm in ['dom0']:
  1272. continue
  1273. vm_info = restore_info[vm]
  1274. assert isinstance(vm_info, self.VMToRestore)
  1275. vm_info.problems.clear()
  1276. if vm in self.options.exclude:
  1277. vm_info.problems.add(self.VMToRestore.EXCLUDED)
  1278. if not self.options.verify_only and \
  1279. vm_info.name in self.app.domains:
  1280. if self.options.rename_conflicting:
  1281. new_name = self.new_name_for_conflicting_vm(
  1282. vm, restore_info
  1283. )
  1284. if new_name is not None:
  1285. vm_info.name = new_name
  1286. else:
  1287. vm_info.problems.add(self.VMToRestore.ALREADY_EXISTS)
  1288. else:
  1289. vm_info.problems.add(self.VMToRestore.ALREADY_EXISTS)
  1290. # check template
  1291. if vm_info.template:
  1292. template_name = vm_info.template
  1293. try:
  1294. host_template = self.app.domains[template_name]
  1295. except KeyError:
  1296. host_template = None
  1297. present_on_host = (host_template and
  1298. host_template.klass == 'TemplateVM')
  1299. present_in_backup = (template_name in restore_info.keys() and
  1300. restore_info[template_name].good_to_go and
  1301. restore_info[template_name].vm.klass ==
  1302. 'TemplateVM')
  1303. if not present_on_host and not present_in_backup:
  1304. if self.options.use_default_template and \
  1305. self.app.default_template:
  1306. if vm_info.orig_template is None:
  1307. vm_info.orig_template = template_name
  1308. vm_info.template = self.app.default_template.name
  1309. else:
  1310. vm_info.problems.add(
  1311. self.VMToRestore.MISSING_TEMPLATE)
  1312. # check netvm
  1313. if vm_info.vm.properties.get('netvm', None) is not None:
  1314. netvm_name = vm_info.netvm
  1315. try:
  1316. netvm_on_host = self.app.domains[netvm_name]
  1317. except KeyError:
  1318. netvm_on_host = None
  1319. present_on_host = (netvm_on_host is not None
  1320. and netvm_on_host.provides_network)
  1321. present_in_backup = (netvm_name in restore_info.keys() and
  1322. restore_info[netvm_name].good_to_go and
  1323. restore_info[netvm_name].vm.properties.get(
  1324. 'provides_network', False))
  1325. if not present_on_host and not present_in_backup:
  1326. if self.options.use_default_netvm:
  1327. del vm_info.vm.properties['netvm']
  1328. elif self.options.use_none_netvm:
  1329. vm_info.netvm = None
  1330. else:
  1331. vm_info.problems.add(self.VMToRestore.MISSING_NETVM)
  1332. return restore_info
  1333. def get_restore_info(self):
  1334. '''Get restore info
  1335. Return information about what is included in the backup.
  1336. That dictionary can be adjusted to select what VM should be restore.
  1337. '''
  1338. # Format versions:
  1339. # 1 - Qubes R1, Qubes R2 beta1, beta2
  1340. # 2 - Qubes R2 beta3+
  1341. # 3 - Qubes R2+
  1342. # 4 - Qubes R4+
  1343. vms_to_restore = {}
  1344. for vm in self.backup_app.domains.values():
  1345. if vm.klass == 'AdminVM':
  1346. # Handle dom0 as special case later
  1347. continue
  1348. if vm.included_in_backup:
  1349. self.log.debug("%s is included in backup", vm.name)
  1350. vms_to_restore[vm.name] = self.VMToRestore(vm)
  1351. if vm.template is not None:
  1352. templatevm_name = vm.template
  1353. vms_to_restore[vm.name].template = templatevm_name
  1354. vms_to_restore = self.restore_info_verify(vms_to_restore)
  1355. # ...and dom0 home
  1356. if self.options.dom0_home and \
  1357. self.backup_app.domains['dom0'].included_in_backup:
  1358. vm = self.backup_app.domains['dom0']
  1359. vms_to_restore['dom0'] = self.Dom0ToRestore(vm)
  1360. local_user = grp.getgrnam('qubes').gr_mem[0]
  1361. if vms_to_restore['dom0'].username != local_user:
  1362. if not self.options.ignore_username_mismatch:
  1363. vms_to_restore['dom0'].problems.add(
  1364. self.Dom0ToRestore.USERNAME_MISMATCH)
  1365. return vms_to_restore
  1366. @staticmethod
  1367. def get_restore_summary(restore_info):
  1368. '''Return a ASCII formatted table with restore info summary'''
  1369. fields = {
  1370. "name": {'func': lambda vm: vm.name},
  1371. "type": {'func': lambda vm: vm.klass},
  1372. "template": {'func': lambda vm:
  1373. 'n/a' if vm.template is None else vm.template},
  1374. "netvm": {'func': lambda vm:
  1375. '(default)' if 'netvm' not in vm.properties else
  1376. '-' if vm.properties['netvm'] is None else
  1377. vm.properties['netvm']},
  1378. "label": {'func': lambda vm: vm.label},
  1379. }
  1380. fields_to_display = ['name', 'type', 'template',
  1381. 'netvm', 'label']
  1382. # First calculate the maximum width of each field we want to display
  1383. total_width = 0
  1384. for field in fields_to_display:
  1385. fields[field]['max_width'] = len(field)
  1386. for vm_info in restore_info.values():
  1387. if vm_info.vm:
  1388. # noinspection PyUnusedLocal
  1389. field_len = len(str(fields[field]["func"](vm_info.vm)))
  1390. if field_len > fields[field]['max_width']:
  1391. fields[field]['max_width'] = field_len
  1392. total_width += fields[field]['max_width']
  1393. summary = ""
  1394. summary += "The following VMs are included in the backup:\n"
  1395. summary += "\n"
  1396. # Display the header
  1397. for field in fields_to_display:
  1398. # noinspection PyTypeChecker
  1399. fmt = "{{0:-^{0}}}-+".format(fields[field]["max_width"] + 1)
  1400. summary += fmt.format('-')
  1401. summary += "\n"
  1402. for field in fields_to_display:
  1403. # noinspection PyTypeChecker
  1404. fmt = "{{0:>{0}}} |".format(fields[field]["max_width"] + 1)
  1405. summary += fmt.format(field)
  1406. summary += "\n"
  1407. for field in fields_to_display:
  1408. # noinspection PyTypeChecker
  1409. fmt = "{{0:-^{0}}}-+".format(fields[field]["max_width"] + 1)
  1410. summary += fmt.format('-')
  1411. summary += "\n"
  1412. for vm_info in restore_info.values():
  1413. assert isinstance(vm_info, BackupRestore.VMToRestore)
  1414. # Skip non-VM here
  1415. if not vm_info.vm:
  1416. continue
  1417. # noinspection PyUnusedLocal
  1418. summary_line = ""
  1419. for field in fields_to_display:
  1420. # noinspection PyTypeChecker
  1421. fmt = "{{0:>{0}}} |".format(fields[field]["max_width"] + 1)
  1422. summary_line += fmt.format(fields[field]["func"](vm_info.vm))
  1423. if BackupRestore.VMToRestore.EXCLUDED in vm_info.problems:
  1424. summary_line += " <-- Excluded from restore"
  1425. elif BackupRestore.VMToRestore.ALREADY_EXISTS in vm_info.problems:
  1426. summary_line += \
  1427. " <-- A VM with the same name already exists on the host!"
  1428. elif BackupRestore.VMToRestore.MISSING_TEMPLATE in \
  1429. vm_info.problems:
  1430. summary_line += " <-- No matching template on the host " \
  1431. "or in the backup found!"
  1432. elif BackupRestore.VMToRestore.MISSING_NETVM in \
  1433. vm_info.problems:
  1434. summary_line += " <-- No matching netvm on the host " \
  1435. "or in the backup found!"
  1436. else:
  1437. if vm_info.template != vm_info.vm.template:
  1438. summary_line += " <-- Template change to '{}'".format(
  1439. vm_info.template)
  1440. if vm_info.name != vm_info.vm.name:
  1441. summary_line += " <-- Will be renamed to '{}'".format(
  1442. vm_info.name)
  1443. summary += summary_line + "\n"
  1444. if 'dom0' in restore_info.keys():
  1445. summary_line = ""
  1446. for field in fields_to_display:
  1447. # noinspection PyTypeChecker
  1448. fmt = "{{0:>{0}}} |".format(fields[field]["max_width"] + 1)
  1449. if field == "name":
  1450. summary_line += fmt.format("Dom0")
  1451. elif field == "type":
  1452. summary_line += fmt.format("Home")
  1453. else:
  1454. summary_line += fmt.format("")
  1455. if BackupRestore.Dom0ToRestore.USERNAME_MISMATCH in \
  1456. restore_info['dom0'].problems:
  1457. summary_line += " <-- username in backup and dom0 mismatch"
  1458. summary += summary_line + "\n"
  1459. return summary
  1460. @staticmethod
  1461. def _templates_first(vms):
  1462. '''Sort templates befor other VM types (AppVM etc)'''
  1463. def key_function(instance):
  1464. '''Key function for :py:func:`sorted`'''
  1465. if isinstance(instance, BackupVM):
  1466. return instance.klass == 'TemplateVM'
  1467. elif hasattr(instance, 'vm'):
  1468. return key_function(instance.vm)
  1469. return 0
  1470. return sorted(vms,
  1471. key=key_function,
  1472. reverse=True)
  1473. def _handle_dom0(self, backup_path):
  1474. '''Extract dom0 home'''
  1475. local_user = grp.getgrnam('qubes').gr_mem[0]
  1476. home_dir = pwd.getpwnam(local_user).pw_dir
  1477. backup_dom0_home_dir = os.path.join(self.tmpdir, backup_path)
  1478. restore_home_backupdir = "home-pre-restore-{0}".format(
  1479. time.strftime("%Y-%m-%d-%H%M%S"))
  1480. self.log.info("Restoring home of user '%s'...", local_user)
  1481. self.log.info("Existing files/dirs backed up in '%s' dir",
  1482. restore_home_backupdir)
  1483. os.mkdir(home_dir + '/' + restore_home_backupdir)
  1484. for f_name in os.listdir(backup_dom0_home_dir):
  1485. home_file = home_dir + '/' + f_name
  1486. if os.path.exists(home_file):
  1487. os.rename(home_file,
  1488. home_dir + '/' + restore_home_backupdir + '/' + f_name)
  1489. if self.header_data.version == 1:
  1490. subprocess.call(
  1491. ["cp", "-nrp", "--reflink=auto",
  1492. backup_dom0_home_dir + '/' + f_name, home_file])
  1493. elif self.header_data.version >= 2:
  1494. shutil.move(backup_dom0_home_dir + '/' + f_name, home_file)
  1495. retcode = subprocess.call(['sudo', 'chown', '-R',
  1496. local_user, home_dir])
  1497. if retcode != 0:
  1498. self.log.error("*** Error while setting home directory owner")
  1499. def _handle_appmenus_list(self, vm, stream):
  1500. '''Handle whitelisted-appmenus.list file'''
  1501. try:
  1502. subprocess.check_call(
  1503. ['qvm-appmenus', '--set-whitelist=-', vm.name],
  1504. stdin=stream)
  1505. except (subprocess.CalledProcessError, FileNotFoundError):
  1506. self.log.error('Failed to set application list for %s', vm.name)
  1507. def _handle_volume_data(self, vm, volume, stream):
  1508. '''Wrap volume data import with logging'''
  1509. try:
  1510. volume.import_data(stream)
  1511. except Exception as err: # pylint: disable=broad-except
  1512. self.log.error('Failed to restore volume %s of VM %s: %s',
  1513. volume.name, vm.name, err)
  1514. def _handle_volume_size(self, vm, volume, size):
  1515. '''Wrap volume resize with logging'''
  1516. try:
  1517. volume.resize(size)
  1518. except Exception as err: # pylint: disable=broad-except
  1519. self.log.error('Failed to resize volume %s of VM %s: %s',
  1520. volume.name, vm.name, err)
  1521. def restore_do(self, restore_info):
  1522. '''
  1523. High level workflow:
  1524. 1. Create VMs object in host collection (qubes.xml)
  1525. 2. Create them on disk (vm.create_on_disk)
  1526. 3. Restore VM data, overriding/converting VM files
  1527. 4. Apply possible fixups and save qubes.xml
  1528. :param restore_info:
  1529. :return:
  1530. '''
  1531. if self.header_data.version == 1:
  1532. raise NotImplementedError('Backup format version 1 not supported')
  1533. restore_info = self.restore_info_verify(restore_info)
  1534. self._restore_vms_metadata(restore_info)
  1535. # Perform VM restoration in backup order
  1536. vms_dirs = []
  1537. handlers = {}
  1538. vms_size = 0
  1539. for vm_info in self._templates_first(restore_info.values()):
  1540. vm = vm_info.restored_vm
  1541. if vm and vm_info.subdir:
  1542. vms_size += int(vm_info.size)
  1543. vms_dirs.append(vm_info.subdir)
  1544. for name, volume in vm.volumes.items():
  1545. if not volume.save_on_stop:
  1546. continue
  1547. data_func = functools.partial(
  1548. self._handle_volume_data, vm, volume)
  1549. size_func = functools.partial(
  1550. self._handle_volume_size, vm, volume)
  1551. handlers[os.path.join(vm_info.subdir, name + '.img')] = \
  1552. (data_func, size_func)
  1553. handlers[os.path.join(vm_info.subdir, 'firewall.xml')] = (
  1554. functools.partial(vm_info.vm.handle_firewall_xml, vm), None)
  1555. handlers[os.path.join(vm_info.subdir,
  1556. 'whitelisted-appmenus.list')] = (
  1557. functools.partial(self._handle_appmenus_list, vm), None)
  1558. if 'dom0' in restore_info.keys() and \
  1559. restore_info['dom0'].good_to_go:
  1560. vms_dirs.append(os.path.dirname(restore_info['dom0'].subdir))
  1561. vms_size += restore_info['dom0'].size
  1562. handlers[restore_info['dom0'].subdir] = (self._handle_dom0, None)
  1563. try:
  1564. self._restore_vm_data(vms_dirs=vms_dirs, vms_size=vms_size,
  1565. handlers=handlers)
  1566. except QubesException as err:
  1567. if self.options.verify_only:
  1568. raise
  1569. else:
  1570. self.log.error('Error extracting data: ' + str(err))
  1571. self.log.warning(
  1572. "Continuing anyway to restore at least some VMs")
  1573. if self.options.verify_only:
  1574. shutil.rmtree(self.tmpdir)
  1575. return
  1576. if self.canceled:
  1577. raise BackupCanceledError("Restore canceled",
  1578. tmpdir=self.tmpdir)
  1579. shutil.rmtree(self.tmpdir)
  1580. self.log.info("-> Done. Please install updates for all the restored "
  1581. "templates.")
  1582. def _restore_vms_metadata(self, restore_info):
  1583. '''Restore VM metadata
  1584. Create VMs, set their properties etc.
  1585. '''
  1586. vms = {}
  1587. for vm_info in restore_info.values():
  1588. assert isinstance(vm_info, self.VMToRestore)
  1589. if not vm_info.vm:
  1590. continue
  1591. if not vm_info.good_to_go:
  1592. continue
  1593. vm = vm_info.vm
  1594. vms[vm.name] = vm
  1595. # First load templates, then other VMs
  1596. for vm in self._templates_first(vms.values()):
  1597. if self.canceled:
  1598. return
  1599. self.log.info("-> Restoring %s...", vm.name)
  1600. kwargs = {}
  1601. if vm.template:
  1602. template = restore_info[vm.name].template
  1603. # handle potentially renamed template
  1604. if template in restore_info \
  1605. and restore_info[template].good_to_go:
  1606. template = restore_info[template].name
  1607. kwargs['template'] = template
  1608. new_vm = None
  1609. vm_name = restore_info[vm.name].name
  1610. try:
  1611. # first only create VMs, later setting may require other VMs
  1612. # be already created
  1613. new_vm = self.app.add_new_vm(
  1614. vm.klass,
  1615. name=vm_name,
  1616. label=vm.label,
  1617. pool=self.options.override_pool,
  1618. **kwargs)
  1619. except Exception as err: # pylint: disable=broad-except
  1620. self.log.error('Error restoring VM %s, skipping: %s',
  1621. vm.name, err)
  1622. if new_vm:
  1623. del self.app.domains[new_vm.name]
  1624. continue
  1625. restore_info[vm.name].restored_vm = new_vm
  1626. for vm in vms.values():
  1627. if self.canceled:
  1628. return
  1629. new_vm = restore_info[vm.name].restored_vm
  1630. if not new_vm:
  1631. # skipped/failed
  1632. continue
  1633. for prop, value in vm.properties.items():
  1634. # exclude VM references - handled manually according to
  1635. # restore options
  1636. if prop in ['template', 'netvm', 'default_dispvm']:
  1637. continue
  1638. try:
  1639. setattr(new_vm, prop, value)
  1640. except Exception as err: # pylint: disable=broad-except
  1641. self.log.error('Error setting %s.%s to %s: %s',
  1642. vm.name, prop, value, err)
  1643. for feature, value in vm.features.items():
  1644. try:
  1645. new_vm.features[feature] = value
  1646. except Exception as err: # pylint: disable=broad-except
  1647. self.log.error('Error setting %s.features[%s] to %s: %s',
  1648. vm.name, feature, value, err)
  1649. for tag in vm.tags:
  1650. try:
  1651. new_vm.tags.add(tag)
  1652. except Exception as err: # pylint: disable=broad-except
  1653. self.log.error('Error adding tag %s to %s: %s',
  1654. tag, vm.name, err)
  1655. for bus in vm.devices:
  1656. for backend_domain, ident in vm.devices[bus]:
  1657. options = vm.devices[bus][(backend_domain, ident)]
  1658. assignment = DeviceAssignment(
  1659. backend_domain=backend_domain,
  1660. ident=ident,
  1661. options=options,
  1662. persistent=True)
  1663. try:
  1664. new_vm.devices[bus].attach(assignment)
  1665. except Exception as err: # pylint: disable=broad-except
  1666. self.log.error('Error attaching device %s:%s to %s: %s',
  1667. bus, ident, vm.name, err)
  1668. # Set VM dependencies - only non-default setting
  1669. for vm in vms.values():
  1670. vm_info = restore_info[vm.name]
  1671. vm_name = vm_info.name
  1672. try:
  1673. host_vm = self.app.domains[vm_name]
  1674. except KeyError:
  1675. # Failed/skipped VM
  1676. continue
  1677. if 'netvm' in vm.properties:
  1678. if vm_info.netvm in restore_info:
  1679. value = restore_info[vm_info.netvm].name
  1680. else:
  1681. value = vm_info.netvm
  1682. try:
  1683. host_vm.netvm = value
  1684. except Exception as err: # pylint: disable=broad-except
  1685. self.log.error('Error setting %s.%s to %s: %s',
  1686. vm.name, 'netvm', value, err)
  1687. if 'default_dispvm' in vm.properties:
  1688. if vm.properties['default_dispvm'] in restore_info:
  1689. value = restore_info[vm.properties[
  1690. 'default_dispvm']].name
  1691. else:
  1692. value = vm.properties['default_dispvm']
  1693. try:
  1694. host_vm.default_dispvm = value
  1695. except Exception as err: # pylint: disable=broad-except
  1696. self.log.error('Error setting %s.%s to %s: %s',
  1697. vm.name, 'default_dispvm', value, err)