restore.py 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939
  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. if not self.options.verify_only:
  1206. to_extract.put(os.path.join(self.tmpdir, filename))
  1207. else:
  1208. os.unlink(os.path.join(self.tmpdir, filename))
  1209. if self.canceled:
  1210. raise BackupCanceledError("Restore canceled",
  1211. tmpdir=self.tmpdir)
  1212. if retrieve_proc.wait() != 0:
  1213. if retrieve_proc.returncode == errno.EDQUOT:
  1214. raise QubesException(
  1215. 'retrieved backup size exceed expected size, if you '
  1216. 'believe this is ok, use --ignore-size-limit option')
  1217. else:
  1218. raise QubesException(
  1219. "unable to read the qubes backup file {} ({}): {}"
  1220. .format(self.backup_location,
  1221. retrieve_proc.returncode, error_pipe.read(
  1222. MAX_STDERR_BYTES)))
  1223. # wait for other processes (if any)
  1224. for proc in self.processes_to_kill_on_cancel:
  1225. proc.wait()
  1226. if proc.returncode != 0:
  1227. raise QubesException(
  1228. "Backup completed, "
  1229. "but VM sending it reported an error (exit code {})".
  1230. format(proc.returncode))
  1231. if filename and filename != "EOF":
  1232. raise QubesException(
  1233. "Premature end of archive, the last file was %s" % filename)
  1234. except:
  1235. to_extract.put(QUEUE_ERROR)
  1236. extract_proc.join()
  1237. raise
  1238. else:
  1239. to_extract.put(QUEUE_FINISHED)
  1240. finally:
  1241. error_pipe.close()
  1242. filelist_pipe.close()
  1243. self.log.debug("Waiting for the extraction process to finish...")
  1244. extract_proc.join()
  1245. self.log.debug("Extraction process finished with code: %s",
  1246. extract_proc.exitcode)
  1247. if extract_proc.exitcode != 0:
  1248. raise QubesException(
  1249. "unable to extract the qubes backup. "
  1250. "Check extracting process errors.")
  1251. def new_name_for_conflicting_vm(self, orig_name, restore_info):
  1252. '''Generate new name for conflicting VM
  1253. Add a number suffix, until the name is unique. If no unique name can
  1254. be found using this strategy, return :py:obj:`None`
  1255. '''
  1256. number = 1
  1257. if len(orig_name) > 29:
  1258. orig_name = orig_name[0:29]
  1259. new_name = orig_name
  1260. while (new_name in restore_info.keys() or
  1261. new_name in [x.name for x in restore_info.values()] or
  1262. new_name in self.app.domains):
  1263. new_name = str('{}{}'.format(orig_name, number))
  1264. number += 1
  1265. if number == 100:
  1266. # give up
  1267. return None
  1268. return new_name
  1269. def restore_info_verify(self, restore_info):
  1270. '''Verify restore info - validate VM dependencies, name conflicts
  1271. etc.
  1272. '''
  1273. for vm in restore_info.keys():
  1274. if vm in ['dom0']:
  1275. continue
  1276. vm_info = restore_info[vm]
  1277. assert isinstance(vm_info, self.VMToRestore)
  1278. vm_info.problems.clear()
  1279. if vm in self.options.exclude:
  1280. vm_info.problems.add(self.VMToRestore.EXCLUDED)
  1281. if not self.options.verify_only and \
  1282. vm_info.name in self.app.domains:
  1283. if self.options.rename_conflicting:
  1284. new_name = self.new_name_for_conflicting_vm(
  1285. vm, restore_info
  1286. )
  1287. if new_name is not None:
  1288. vm_info.name = new_name
  1289. else:
  1290. vm_info.problems.add(self.VMToRestore.ALREADY_EXISTS)
  1291. else:
  1292. vm_info.problems.add(self.VMToRestore.ALREADY_EXISTS)
  1293. # check template
  1294. if vm_info.template:
  1295. template_name = vm_info.template
  1296. try:
  1297. host_template = self.app.domains[template_name]
  1298. except KeyError:
  1299. host_template = None
  1300. present_on_host = (host_template and
  1301. host_template.klass == 'TemplateVM')
  1302. present_in_backup = (template_name in restore_info.keys() and
  1303. restore_info[template_name].good_to_go and
  1304. restore_info[template_name].vm.klass ==
  1305. 'TemplateVM')
  1306. if not present_on_host and not present_in_backup:
  1307. if self.options.use_default_template and \
  1308. self.app.default_template:
  1309. if vm_info.orig_template is None:
  1310. vm_info.orig_template = template_name
  1311. vm_info.template = self.app.default_template.name
  1312. else:
  1313. vm_info.problems.add(
  1314. self.VMToRestore.MISSING_TEMPLATE)
  1315. # check netvm
  1316. if vm_info.vm.properties.get('netvm', None) is not None:
  1317. netvm_name = vm_info.netvm
  1318. try:
  1319. netvm_on_host = self.app.domains[netvm_name]
  1320. except KeyError:
  1321. netvm_on_host = None
  1322. present_on_host = (netvm_on_host is not None
  1323. and netvm_on_host.provides_network)
  1324. present_in_backup = (netvm_name in restore_info.keys() and
  1325. restore_info[netvm_name].good_to_go and
  1326. restore_info[netvm_name].vm.properties.get(
  1327. 'provides_network', False))
  1328. if not present_on_host and not present_in_backup:
  1329. if self.options.use_default_netvm:
  1330. del vm_info.vm.properties['netvm']
  1331. elif self.options.use_none_netvm:
  1332. vm_info.netvm = None
  1333. else:
  1334. vm_info.problems.add(self.VMToRestore.MISSING_NETVM)
  1335. return restore_info
  1336. def get_restore_info(self):
  1337. '''Get restore info
  1338. Return information about what is included in the backup.
  1339. That dictionary can be adjusted to select what VM should be restore.
  1340. '''
  1341. # Format versions:
  1342. # 1 - Qubes R1, Qubes R2 beta1, beta2
  1343. # 2 - Qubes R2 beta3+
  1344. # 3 - Qubes R2+
  1345. # 4 - Qubes R4+
  1346. vms_to_restore = {}
  1347. for vm in self.backup_app.domains.values():
  1348. if vm.klass == 'AdminVM':
  1349. # Handle dom0 as special case later
  1350. continue
  1351. if vm.included_in_backup:
  1352. self.log.debug("%s is included in backup", vm.name)
  1353. vms_to_restore[vm.name] = self.VMToRestore(vm)
  1354. if vm.template is not None:
  1355. templatevm_name = vm.template
  1356. vms_to_restore[vm.name].template = templatevm_name
  1357. vms_to_restore = self.restore_info_verify(vms_to_restore)
  1358. # ...and dom0 home
  1359. if self.options.dom0_home and \
  1360. self.backup_app.domains['dom0'].included_in_backup:
  1361. vm = self.backup_app.domains['dom0']
  1362. vms_to_restore['dom0'] = self.Dom0ToRestore(vm)
  1363. local_user = grp.getgrnam('qubes').gr_mem[0]
  1364. if vms_to_restore['dom0'].username != local_user:
  1365. if not self.options.ignore_username_mismatch:
  1366. vms_to_restore['dom0'].problems.add(
  1367. self.Dom0ToRestore.USERNAME_MISMATCH)
  1368. return vms_to_restore
  1369. @staticmethod
  1370. def get_restore_summary(restore_info):
  1371. '''Return a ASCII formatted table with restore info summary'''
  1372. fields = {
  1373. "name": {'func': lambda vm: vm.name},
  1374. "type": {'func': lambda vm: vm.klass},
  1375. "template": {'func': lambda vm:
  1376. 'n/a' if vm.template is None else vm.template},
  1377. "netvm": {'func': lambda vm:
  1378. '(default)' if 'netvm' not in vm.properties else
  1379. '-' if vm.properties['netvm'] is None else
  1380. vm.properties['netvm']},
  1381. "label": {'func': lambda vm: vm.label},
  1382. }
  1383. fields_to_display = ['name', 'type', 'template',
  1384. 'netvm', 'label']
  1385. # First calculate the maximum width of each field we want to display
  1386. total_width = 0
  1387. for field in fields_to_display:
  1388. fields[field]['max_width'] = len(field)
  1389. for vm_info in restore_info.values():
  1390. if vm_info.vm:
  1391. # noinspection PyUnusedLocal
  1392. field_len = len(str(fields[field]["func"](vm_info.vm)))
  1393. if field_len > fields[field]['max_width']:
  1394. fields[field]['max_width'] = field_len
  1395. total_width += fields[field]['max_width']
  1396. summary = ""
  1397. summary += "The following VMs are included in the backup:\n"
  1398. summary += "\n"
  1399. # Display the header
  1400. for field in fields_to_display:
  1401. # noinspection PyTypeChecker
  1402. fmt = "{{0:-^{0}}}-+".format(fields[field]["max_width"] + 1)
  1403. summary += fmt.format('-')
  1404. summary += "\n"
  1405. for field in fields_to_display:
  1406. # noinspection PyTypeChecker
  1407. fmt = "{{0:>{0}}} |".format(fields[field]["max_width"] + 1)
  1408. summary += fmt.format(field)
  1409. summary += "\n"
  1410. for field in fields_to_display:
  1411. # noinspection PyTypeChecker
  1412. fmt = "{{0:-^{0}}}-+".format(fields[field]["max_width"] + 1)
  1413. summary += fmt.format('-')
  1414. summary += "\n"
  1415. for vm_info in restore_info.values():
  1416. assert isinstance(vm_info, BackupRestore.VMToRestore)
  1417. # Skip non-VM here
  1418. if not vm_info.vm:
  1419. continue
  1420. # noinspection PyUnusedLocal
  1421. summary_line = ""
  1422. for field in fields_to_display:
  1423. # noinspection PyTypeChecker
  1424. fmt = "{{0:>{0}}} |".format(fields[field]["max_width"] + 1)
  1425. summary_line += fmt.format(fields[field]["func"](vm_info.vm))
  1426. if BackupRestore.VMToRestore.EXCLUDED in vm_info.problems:
  1427. summary_line += " <-- Excluded from restore"
  1428. elif BackupRestore.VMToRestore.ALREADY_EXISTS in vm_info.problems:
  1429. summary_line += \
  1430. " <-- A VM with the same name already exists on the host!"
  1431. elif BackupRestore.VMToRestore.MISSING_TEMPLATE in \
  1432. vm_info.problems:
  1433. summary_line += " <-- No matching template on the host " \
  1434. "or in the backup found!"
  1435. elif BackupRestore.VMToRestore.MISSING_NETVM in \
  1436. vm_info.problems:
  1437. summary_line += " <-- No matching netvm on the host " \
  1438. "or in the backup found!"
  1439. else:
  1440. if vm_info.template != vm_info.vm.template:
  1441. summary_line += " <-- Template change to '{}'".format(
  1442. vm_info.template)
  1443. if vm_info.name != vm_info.vm.name:
  1444. summary_line += " <-- Will be renamed to '{}'".format(
  1445. vm_info.name)
  1446. summary += summary_line + "\n"
  1447. if 'dom0' in restore_info.keys():
  1448. summary_line = ""
  1449. for field in fields_to_display:
  1450. # noinspection PyTypeChecker
  1451. fmt = "{{0:>{0}}} |".format(fields[field]["max_width"] + 1)
  1452. if field == "name":
  1453. summary_line += fmt.format("Dom0")
  1454. elif field == "type":
  1455. summary_line += fmt.format("Home")
  1456. else:
  1457. summary_line += fmt.format("")
  1458. if BackupRestore.Dom0ToRestore.USERNAME_MISMATCH in \
  1459. restore_info['dom0'].problems:
  1460. summary_line += " <-- username in backup and dom0 mismatch"
  1461. summary += summary_line + "\n"
  1462. return summary
  1463. @staticmethod
  1464. def _templates_first(vms):
  1465. '''Sort templates befor other VM types (AppVM etc)'''
  1466. def key_function(instance):
  1467. '''Key function for :py:func:`sorted`'''
  1468. if isinstance(instance, BackupVM):
  1469. return instance.klass == 'TemplateVM'
  1470. elif hasattr(instance, 'vm'):
  1471. return key_function(instance.vm)
  1472. return 0
  1473. return sorted(vms,
  1474. key=key_function,
  1475. reverse=True)
  1476. def _handle_dom0(self, backup_path):
  1477. '''Extract dom0 home'''
  1478. local_user = grp.getgrnam('qubes').gr_mem[0]
  1479. home_dir = pwd.getpwnam(local_user).pw_dir
  1480. backup_dom0_home_dir = os.path.join(self.tmpdir, backup_path)
  1481. restore_home_backupdir = "home-pre-restore-{0}".format(
  1482. time.strftime("%Y-%m-%d-%H%M%S"))
  1483. self.log.info("Restoring home of user '%s'...", local_user)
  1484. self.log.info("Existing files/dirs backed up in '%s' dir",
  1485. restore_home_backupdir)
  1486. os.mkdir(home_dir + '/' + restore_home_backupdir)
  1487. for f_name in os.listdir(backup_dom0_home_dir):
  1488. home_file = home_dir + '/' + f_name
  1489. if os.path.exists(home_file):
  1490. os.rename(home_file,
  1491. home_dir + '/' + restore_home_backupdir + '/' + f_name)
  1492. if self.header_data.version == 1:
  1493. subprocess.call(
  1494. ["cp", "-nrp", "--reflink=auto",
  1495. backup_dom0_home_dir + '/' + f_name, home_file])
  1496. elif self.header_data.version >= 2:
  1497. shutil.move(backup_dom0_home_dir + '/' + f_name, home_file)
  1498. retcode = subprocess.call(['sudo', 'chown', '-R',
  1499. local_user, home_dir])
  1500. if retcode != 0:
  1501. self.log.error("*** Error while setting home directory owner")
  1502. def _handle_appmenus_list(self, vm, stream):
  1503. '''Handle whitelisted-appmenus.list file'''
  1504. try:
  1505. subprocess.check_call(
  1506. ['qvm-appmenus', '--set-whitelist=-', vm.name],
  1507. stdin=stream)
  1508. except (subprocess.CalledProcessError, FileNotFoundError):
  1509. self.log.error('Failed to set application list for %s', vm.name)
  1510. def _handle_volume_data(self, vm, volume, stream):
  1511. '''Wrap volume data import with logging'''
  1512. try:
  1513. volume.import_data(stream)
  1514. except Exception as err: # pylint: disable=broad-except
  1515. self.log.error('Failed to restore volume %s of VM %s: %s',
  1516. volume.name, vm.name, err)
  1517. def _handle_volume_size(self, vm, volume, size):
  1518. '''Wrap volume resize with logging'''
  1519. try:
  1520. volume.resize(size)
  1521. except Exception as err: # pylint: disable=broad-except
  1522. self.log.error('Failed to resize volume %s of VM %s: %s',
  1523. volume.name, vm.name, err)
  1524. def restore_do(self, restore_info):
  1525. '''
  1526. High level workflow:
  1527. 1. Create VMs object in host collection (qubes.xml)
  1528. 2. Create them on disk (vm.create_on_disk)
  1529. 3. Restore VM data, overriding/converting VM files
  1530. 4. Apply possible fixups and save qubes.xml
  1531. :param restore_info:
  1532. :return:
  1533. '''
  1534. if self.header_data.version == 1:
  1535. raise NotImplementedError('Backup format version 1 not supported')
  1536. restore_info = self.restore_info_verify(restore_info)
  1537. self._restore_vms_metadata(restore_info)
  1538. # Perform VM restoration in backup order
  1539. vms_dirs = []
  1540. handlers = {}
  1541. vms_size = 0
  1542. for vm_info in self._templates_first(restore_info.values()):
  1543. vm = vm_info.restored_vm
  1544. if vm and vm_info.subdir:
  1545. vms_size += int(vm_info.size)
  1546. vms_dirs.append(vm_info.subdir)
  1547. if self.options.verify_only:
  1548. continue
  1549. for name, volume in vm.volumes.items():
  1550. if not volume.save_on_stop:
  1551. continue
  1552. data_func = functools.partial(
  1553. self._handle_volume_data, vm, volume)
  1554. size_func = functools.partial(
  1555. self._handle_volume_size, vm, volume)
  1556. handlers[os.path.join(vm_info.subdir, name + '.img')] = \
  1557. (data_func, size_func)
  1558. handlers[os.path.join(vm_info.subdir, 'firewall.xml')] = (
  1559. functools.partial(vm_info.vm.handle_firewall_xml, vm), None)
  1560. handlers[os.path.join(vm_info.subdir,
  1561. 'whitelisted-appmenus.list')] = (
  1562. functools.partial(self._handle_appmenus_list, vm), None)
  1563. if 'dom0' in restore_info.keys() and \
  1564. restore_info['dom0'].good_to_go:
  1565. vms_dirs.append(os.path.dirname(restore_info['dom0'].subdir))
  1566. vms_size += restore_info['dom0'].size
  1567. if not self.options.verify_only:
  1568. handlers[restore_info['dom0'].subdir] = \
  1569. (self._handle_dom0, None)
  1570. try:
  1571. self._restore_vm_data(vms_dirs=vms_dirs, vms_size=vms_size,
  1572. handlers=handlers)
  1573. except QubesException as err:
  1574. if self.options.verify_only:
  1575. raise
  1576. else:
  1577. self.log.error('Error extracting data: ' + str(err))
  1578. finally:
  1579. if self.log.getEffectiveLevel() > logging.DEBUG:
  1580. shutil.rmtree(self.tmpdir)
  1581. if self.canceled:
  1582. raise BackupCanceledError("Restore canceled",
  1583. tmpdir=self.tmpdir)
  1584. self.log.info("-> Done.")
  1585. if not self.options.verify_only:
  1586. self.log.info("-> Please install updates for all the restored "
  1587. "templates.")
  1588. def _restore_vms_metadata(self, restore_info):
  1589. '''Restore VM metadata
  1590. Create VMs, set their properties etc.
  1591. '''
  1592. vms = {}
  1593. for vm_info in restore_info.values():
  1594. assert isinstance(vm_info, self.VMToRestore)
  1595. if not vm_info.vm:
  1596. continue
  1597. if not vm_info.good_to_go:
  1598. continue
  1599. vm = vm_info.vm
  1600. vms[vm.name] = vm
  1601. # First load templates, then other VMs
  1602. for vm in self._templates_first(vms.values()):
  1603. if self.canceled:
  1604. return
  1605. if self.options.verify_only:
  1606. self.log.info("-> Verifying %s...", vm.name)
  1607. else:
  1608. self.log.info("-> Restoring %s...", vm.name)
  1609. kwargs = {}
  1610. if vm.template:
  1611. template = restore_info[vm.name].template
  1612. # handle potentially renamed template
  1613. if template in restore_info \
  1614. and restore_info[template].good_to_go:
  1615. template = restore_info[template].name
  1616. kwargs['template'] = template
  1617. new_vm = None
  1618. vm_name = restore_info[vm.name].name
  1619. if self.options.verify_only:
  1620. new_vm = self.backup_app.domains[vm_name]
  1621. else:
  1622. try:
  1623. # first only create VMs, later setting may require other VMs
  1624. # be already created
  1625. new_vm = self.app.add_new_vm(
  1626. vm.klass,
  1627. name=vm_name,
  1628. label=vm.label,
  1629. pool=self.options.override_pool,
  1630. **kwargs)
  1631. except Exception as err: # pylint: disable=broad-except
  1632. self.log.error('Error restoring VM %s, skipping: %s',
  1633. vm.name, err)
  1634. if new_vm:
  1635. del self.app.domains[new_vm.name]
  1636. continue
  1637. restore_info[vm.name].restored_vm = new_vm
  1638. for vm in vms.values():
  1639. if self.canceled:
  1640. return
  1641. new_vm = restore_info[vm.name].restored_vm
  1642. if not new_vm:
  1643. # skipped/failed
  1644. continue
  1645. for prop, value in vm.properties.items():
  1646. # exclude VM references - handled manually according to
  1647. # restore options
  1648. if prop in ['template', 'netvm', 'default_dispvm']:
  1649. continue
  1650. try:
  1651. setattr(new_vm, prop, value)
  1652. except Exception as err: # pylint: disable=broad-except
  1653. self.log.error('Error setting %s.%s to %s: %s',
  1654. vm.name, prop, value, err)
  1655. for feature, value in vm.features.items():
  1656. try:
  1657. new_vm.features[feature] = value
  1658. except Exception as err: # pylint: disable=broad-except
  1659. self.log.error('Error setting %s.features[%s] to %s: %s',
  1660. vm.name, feature, value, err)
  1661. for tag in vm.tags:
  1662. try:
  1663. new_vm.tags.add(tag)
  1664. except Exception as err: # pylint: disable=broad-except
  1665. self.log.error('Error adding tag %s to %s: %s',
  1666. tag, vm.name, err)
  1667. for bus in vm.devices:
  1668. for backend_domain, ident in vm.devices[bus]:
  1669. options = vm.devices[bus][(backend_domain, ident)]
  1670. assignment = DeviceAssignment(
  1671. backend_domain=backend_domain,
  1672. ident=ident,
  1673. options=options,
  1674. persistent=True)
  1675. try:
  1676. new_vm.devices[bus].attach(assignment)
  1677. except Exception as err: # pylint: disable=broad-except
  1678. self.log.error('Error attaching device %s:%s to %s: %s',
  1679. bus, ident, vm.name, err)
  1680. # Set VM dependencies - only non-default setting
  1681. for vm in vms.values():
  1682. vm_info = restore_info[vm.name]
  1683. vm_name = vm_info.name
  1684. try:
  1685. host_vm = self.app.domains[vm_name]
  1686. except KeyError:
  1687. # Failed/skipped VM
  1688. continue
  1689. if 'netvm' in vm.properties:
  1690. if vm_info.netvm in restore_info:
  1691. value = restore_info[vm_info.netvm].name
  1692. else:
  1693. value = vm_info.netvm
  1694. try:
  1695. host_vm.netvm = value
  1696. except Exception as err: # pylint: disable=broad-except
  1697. self.log.error('Error setting %s.%s to %s: %s',
  1698. vm.name, 'netvm', value, err)
  1699. if 'default_dispvm' in vm.properties:
  1700. if vm.properties['default_dispvm'] in restore_info:
  1701. value = restore_info[vm.properties[
  1702. 'default_dispvm']].name
  1703. else:
  1704. value = vm.properties['default_dispvm']
  1705. try:
  1706. host_vm.default_dispvm = value
  1707. except Exception as err: # pylint: disable=broad-except
  1708. self.log.error('Error setting %s.%s to %s: %s',
  1709. vm.name, 'default_dispvm', value, err)