backup.py 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  1. #
  2. # The Qubes OS Project, http://www.qubes-os.org
  3. #
  4. # Copyright (C) 2013-2015 Marek Marczykowski-Górecki
  5. # <marmarek@invisiblethingslab.com>
  6. # Copyright (C) 2013 Olivier Médoc <o_medoc@yahoo.fr>
  7. #
  8. # This program is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU General Public License
  10. # as published by the Free Software Foundation; either version 2
  11. # of the License, or (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>
  20. #
  21. #
  22. from __future__ import unicode_literals
  23. import itertools
  24. import logging
  25. import functools
  26. import termios
  27. from qubes.utils import size_to_human
  28. import stat
  29. import os
  30. import fcntl
  31. import subprocess
  32. import re
  33. import shutil
  34. import tempfile
  35. import time
  36. import grp
  37. import pwd
  38. import datetime
  39. from multiprocessing import Queue, Process
  40. import qubes
  41. import qubes.core2migration
  42. import qubes.storage
  43. import qubes.storage.file
  44. import qubes.vm.templatevm
  45. QUEUE_ERROR = "ERROR"
  46. QUEUE_FINISHED = "FINISHED"
  47. HEADER_FILENAME = 'backup-header'
  48. DEFAULT_CRYPTO_ALGORITHM = 'aes-256-cbc'
  49. # 'scrypt' is not exactly HMAC algorithm, but a tool we use to
  50. # integrity-protect the data
  51. DEFAULT_HMAC_ALGORITHM = 'scrypt'
  52. DEFAULT_COMPRESSION_FILTER = 'gzip'
  53. CURRENT_BACKUP_FORMAT_VERSION = '4'
  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. class BackupCanceledError(qubes.exc.QubesException):
  63. def __init__(self, msg, tmpdir=None):
  64. super(BackupCanceledError, self).__init__(msg)
  65. self.tmpdir = tmpdir
  66. class BackupHeader(object):
  67. '''Structure describing backup-header file included as the first file in
  68. backup archive
  69. '''
  70. header_keys = {
  71. 'version': 'version',
  72. 'encrypted': 'encrypted',
  73. 'compressed': 'compressed',
  74. 'compression-filter': 'compression_filter',
  75. 'crypto-algorithm': 'crypto_algorithm',
  76. 'hmac-algorithm': 'hmac_algorithm',
  77. 'backup-id': 'backup_id'
  78. }
  79. bool_options = ['encrypted', 'compressed']
  80. int_options = ['version']
  81. def __init__(self,
  82. header_data=None,
  83. version=None,
  84. encrypted=None,
  85. compressed=None,
  86. compression_filter=None,
  87. hmac_algorithm=None,
  88. crypto_algorithm=None,
  89. backup_id=None):
  90. # repeat the list to help code completion...
  91. self.version = version
  92. self.encrypted = encrypted
  93. self.compressed = compressed
  94. # Options introduced in backup format 3+, which always have a header,
  95. # so no need for fallback in function parameter
  96. self.compression_filter = compression_filter
  97. self.hmac_algorithm = hmac_algorithm
  98. self.crypto_algorithm = crypto_algorithm
  99. self.backup_id = backup_id
  100. if header_data is not None:
  101. self.load(header_data)
  102. def load(self, untrusted_header_text):
  103. """Parse backup header file.
  104. :param untrusted_header_text: header content
  105. :type untrusted_header_text: basestring
  106. .. warning::
  107. This function may be exposed to not yet verified header,
  108. so is security critical.
  109. """
  110. try:
  111. untrusted_header_text = untrusted_header_text.decode('ascii')
  112. except UnicodeDecodeError:
  113. raise qubes.exc.QubesException(
  114. "Non-ASCII characters in backup header")
  115. for untrusted_line in untrusted_header_text.splitlines():
  116. if untrusted_line.count('=') != 1:
  117. raise qubes.exc.QubesException("Invalid backup header")
  118. key, value = untrusted_line.strip().split('=', 1)
  119. if not _re_alphanum.match(key):
  120. raise qubes.exc.QubesException("Invalid backup header (key)")
  121. if key not in self.header_keys.keys():
  122. # Ignoring unknown option
  123. continue
  124. if not _re_alphanum.match(value):
  125. raise qubes.exc.QubesException("Invalid backup header (value)")
  126. if getattr(self, self.header_keys[key]) is not None:
  127. raise qubes.exc.QubesException(
  128. "Duplicated header line: {}".format(key))
  129. if key in self.bool_options:
  130. value = value.lower() in ["1", "true", "yes"]
  131. elif key in self.int_options:
  132. value = int(value)
  133. setattr(self, self.header_keys[key], value)
  134. self.validate()
  135. def validate(self):
  136. if self.version == 1:
  137. # header not really present
  138. pass
  139. elif self.version in [2, 3, 4]:
  140. expected_attrs = ['version', 'encrypted', 'compressed',
  141. 'hmac_algorithm']
  142. if self.encrypted:
  143. expected_attrs += ['crypto_algorithm']
  144. if self.version >= 3 and self.compressed:
  145. expected_attrs += ['compression_filter']
  146. if self.version >= 4:
  147. expected_attrs += ['backup_id']
  148. for key in expected_attrs:
  149. if getattr(self, key) is None:
  150. raise qubes.exc.QubesException(
  151. "Backup header lack '{}' info".format(key))
  152. else:
  153. raise qubes.exc.QubesException(
  154. "Unsupported backup version {}".format(self.version))
  155. def save(self, filename):
  156. with open(filename, "w") as f_header:
  157. # make sure 'version' is the first key
  158. f_header.write('version={}\n'.format(self.version))
  159. for key, attr in self.header_keys.items():
  160. if key == 'version':
  161. continue
  162. if getattr(self, attr) is None:
  163. continue
  164. f_header.write("{!s}={!s}\n".format(key, getattr(self, attr)))
  165. class SendWorker(Process):
  166. def __init__(self, queue, base_dir, backup_stdout):
  167. super(SendWorker, self).__init__()
  168. self.queue = queue
  169. self.base_dir = base_dir
  170. self.backup_stdout = backup_stdout
  171. self.log = logging.getLogger('qubes.backup')
  172. def run(self):
  173. self.log.debug("Started sending thread")
  174. self.log.debug("Moving to temporary dir %s", self.base_dir)
  175. os.chdir(self.base_dir)
  176. for filename in iter(self.queue.get, None):
  177. if filename in (QUEUE_FINISHED, QUEUE_ERROR):
  178. break
  179. self.log.debug("Sending file {}".format(filename))
  180. # This tar used for sending data out need to be as simple, as
  181. # simple, as featureless as possible. It will not be
  182. # verified before untaring.
  183. tar_final_cmd = ["tar", "-cO", "--posix",
  184. "-C", self.base_dir, filename]
  185. final_proc = subprocess.Popen(tar_final_cmd,
  186. stdin=subprocess.PIPE,
  187. stdout=self.backup_stdout)
  188. if final_proc.wait() >= 2:
  189. if self.queue.full():
  190. # if queue is already full, remove some entry to wake up
  191. # main thread, so it will be able to notice error
  192. self.queue.get()
  193. # handle only exit code 2 (tar fatal error) or
  194. # greater (call failed?)
  195. raise qubes.exc.QubesException(
  196. "ERROR: Failed to write the backup, out of disk space? "
  197. "Check console output or ~/.xsession-errors for details.")
  198. # Delete the file as we don't need it anymore
  199. self.log.debug("Removing file {}".format(filename))
  200. os.remove(filename)
  201. self.log.debug("Finished sending thread")
  202. def launch_proc_with_pty(args, stdin=None, stdout=None, stderr=None, echo=True):
  203. """Similar to pty.fork, but handle stdin/stdout according to parameters
  204. instead of connecting to the pty
  205. :return tuple (subprocess.Popen, pty_master)
  206. """
  207. def set_ctty(ctty_fd, master_fd):
  208. os.setsid()
  209. os.close(master_fd)
  210. fcntl.ioctl(ctty_fd, termios.TIOCSCTTY, 0)
  211. if not echo:
  212. termios_p = termios.tcgetattr(ctty_fd)
  213. # termios_p.c_lflags
  214. termios_p[3] &= ~termios.ECHO
  215. termios.tcsetattr(ctty_fd, termios.TCSANOW, termios_p)
  216. (pty_master, pty_slave) = os.openpty()
  217. p = subprocess.Popen(args, stdin=stdin, stdout=stdout, stderr=stderr,
  218. preexec_fn=lambda: set_ctty(pty_slave, pty_master))
  219. os.close(pty_slave)
  220. return p, os.fdopen(pty_master, 'wb+', buffering=0)
  221. def launch_scrypt(action, input_name, output_name, passphrase):
  222. '''
  223. Launch 'scrypt' process, pass passphrase to it and return
  224. subprocess.Popen object.
  225. :param action: 'enc' or 'dec'
  226. :param input_name: input path or '-' for stdin
  227. :param output_name: output path or '-' for stdout
  228. :param passphrase: passphrase
  229. :return: subprocess.Popen object
  230. '''
  231. command_line = ['scrypt', action, input_name, output_name]
  232. (p, pty) = launch_proc_with_pty(command_line,
  233. stdin=subprocess.PIPE if input_name == '-' else None,
  234. stdout=subprocess.PIPE if output_name == '-' else None,
  235. stderr=subprocess.PIPE,
  236. echo=False)
  237. if action == 'enc':
  238. prompts = (b'Please enter passphrase: ', b'Please confirm passphrase: ')
  239. else:
  240. prompts = (b'Please enter passphrase: ',)
  241. for prompt in prompts:
  242. actual_prompt = p.stderr.read(len(prompt))
  243. if actual_prompt != prompt:
  244. raise qubes.exc.QubesException(
  245. 'Unexpected prompt from scrypt: {}'.format(actual_prompt))
  246. pty.write(passphrase.encode('utf-8') + b'\n')
  247. pty.flush()
  248. # save it here, so garbage collector would not close it (which would kill
  249. # the child)
  250. p.pty = pty
  251. return p
  252. class Backup(object):
  253. '''Backup operation manager. Usage:
  254. >>> app = qubes.Qubes()
  255. >>> # optional - you can use 'None' to use default list (based on
  256. >>> # vm.include_in_backups property)
  257. >>> vms = [app.domains[name] for name in ['my-vm1', 'my-vm2', 'my-vm3']]
  258. >>> exclude_vms = []
  259. >>> options = {
  260. >>> 'encrypted': True,
  261. >>> 'compressed': True,
  262. >>> 'passphrase': 'This is very weak backup passphrase',
  263. >>> 'target_vm': app.domains['sys-usb'],
  264. >>> 'target_dir': '/media/disk',
  265. >>> }
  266. >>> backup_op = Backup(app, vms, exclude_vms, **options)
  267. >>> print(backup_op.get_backup_summary())
  268. >>> backup_op.backup_do()
  269. See attributes of this object for all available options.
  270. '''
  271. # pylint: disable=too-many-instance-attributes
  272. class FileToBackup(object):
  273. # pylint: disable=too-few-public-methods
  274. def __init__(self, file_path, subdir=None, name=None):
  275. file_size = qubes.storage.file.get_disk_usage(file_path)
  276. if subdir is None:
  277. abs_file_path = os.path.abspath(file_path)
  278. abs_base_dir = os.path.abspath(
  279. qubes.config.system_path["qubes_base_dir"]) + '/'
  280. abs_file_dir = os.path.dirname(abs_file_path) + '/'
  281. (nothing, directory, subdir) = \
  282. abs_file_dir.partition(abs_base_dir)
  283. assert nothing == ""
  284. assert directory == abs_base_dir
  285. else:
  286. if subdir and not subdir.endswith('/'):
  287. subdir += '/'
  288. #: real path to the file
  289. self.path = file_path
  290. #: size of the file
  291. self.size = file_size
  292. #: directory in backup archive where file should be placed
  293. self.subdir = subdir
  294. #: use this name in the archive (aka rename)
  295. self.name = os.path.basename(file_path)
  296. if name is not None:
  297. self.name = name
  298. class VMToBackup(object):
  299. # pylint: disable=too-few-public-methods
  300. def __init__(self, vm, files, subdir):
  301. self.vm = vm
  302. self.files = files
  303. self.subdir = subdir
  304. @property
  305. def size(self):
  306. return functools.reduce(lambda x, y: x + y.size, self.files, 0)
  307. def __init__(self, app, vms_list=None, exclude_list=None, **kwargs):
  308. """
  309. If vms = None, include all (sensible) VMs;
  310. exclude_list is always applied
  311. """
  312. super(Backup, self).__init__()
  313. #: progress of the backup - bytes handled of the current VM
  314. self.chunk_size = 100 * 1024 * 1024
  315. self._current_vm_bytes = 0
  316. #: progress of the backup - bytes handled of finished VMs
  317. self._done_vms_bytes = 0
  318. #: total backup size (set by :py:meth:`get_files_to_backup`)
  319. self.total_backup_bytes = 0
  320. #: application object
  321. self.app = app
  322. #: directory for temporary files - set after creating the directory
  323. self.tmpdir = None
  324. # Backup settings - defaults
  325. #: should the backup be compressed?
  326. self.compressed = True
  327. #: what passphrase should be used to intergrity protect (and encrypt)
  328. #: the backup; required
  329. self.passphrase = None
  330. #: custom compression filter; a program which process stdin to stdout
  331. self.compression_filter = DEFAULT_COMPRESSION_FILTER
  332. #: VM to which backup should be sent (if any)
  333. self.target_vm = None
  334. #: directory to save backup in (either in dom0 or target VM,
  335. #: depending on :py:attr:`target_vm`
  336. self.target_dir = None
  337. #: callback for progress reporting. Will be called with one argument
  338. #: - progress in percents
  339. self.progress_callback = None
  340. #: backup ID, needs to be unique (for a given user),
  341. #: not necessary unpredictable; automatically generated
  342. self.backup_id = datetime.datetime.now().strftime(
  343. '%Y%m%dT%H%M%S-' + str(os.getpid()))
  344. for key, value in kwargs.items():
  345. if hasattr(self, key):
  346. setattr(self, key, value)
  347. else:
  348. raise AttributeError(key)
  349. #: whether backup was canceled
  350. self.canceled = False
  351. #: list of PIDs to kill on backup cancel
  352. self.processes_to_kill_on_cancel = []
  353. self.log = logging.getLogger('qubes.backup')
  354. if exclude_list is None:
  355. exclude_list = []
  356. if vms_list is None:
  357. vms_list = [vm for vm in app.domains if vm.include_in_backups]
  358. # Apply exclude list
  359. self.vms_for_backup = [vm for vm in vms_list
  360. if vm.name not in exclude_list]
  361. self._files_to_backup = self.get_files_to_backup()
  362. def __del__(self):
  363. if self.tmpdir and os.path.exists(self.tmpdir):
  364. shutil.rmtree(self.tmpdir)
  365. def cancel(self):
  366. """Cancel running backup operation. Can be called from another thread.
  367. """
  368. self.canceled = True
  369. for proc in self.processes_to_kill_on_cancel:
  370. try:
  371. proc.terminate()
  372. except OSError:
  373. pass
  374. def get_files_to_backup(self):
  375. files_to_backup = {}
  376. for vm in self.vms_for_backup:
  377. if vm.qid == 0:
  378. # handle dom0 later
  379. continue
  380. subdir = 'vm%d/' % vm.qid
  381. vm_files = []
  382. if vm.volumes['private'] is not None:
  383. path_to_private_img = vm.storage.export('private')
  384. vm_files.append(self.FileToBackup(path_to_private_img, subdir,
  385. 'private.img'))
  386. vm_files.append(self.FileToBackup(vm.icon_path, subdir))
  387. vm_files.extend(self.FileToBackup(i, subdir)
  388. for i in vm.fire_event('backup-get-files'))
  389. # TODO: drop after merging firewall.xml into qubes.xml
  390. firewall_conf = os.path.join(vm.dir_path, vm.firewall_conf)
  391. if os.path.exists(firewall_conf):
  392. vm_files.append(self.FileToBackup(firewall_conf, subdir))
  393. if vm.updateable:
  394. path_to_root_img = vm.storage.export('root')
  395. vm_files.append(self.FileToBackup(path_to_root_img, subdir,
  396. 'root.img'))
  397. files_to_backup[vm.qid] = self.VMToBackup(vm, vm_files, subdir)
  398. # Dom0 user home
  399. if 0 in [vm.qid for vm in self.vms_for_backup]:
  400. local_user = grp.getgrnam('qubes').gr_mem[0]
  401. home_dir = pwd.getpwnam(local_user).pw_dir
  402. # Home dir should have only user-owned files, so fix it now
  403. # to prevent permissions problems - some root-owned files can
  404. # left after 'sudo bash' and similar commands
  405. subprocess.check_call(['sudo', 'chown', '-R', local_user, home_dir])
  406. home_to_backup = [
  407. self.FileToBackup(home_dir, 'dom0-home/')]
  408. vm_files = home_to_backup
  409. files_to_backup[0] = self.VMToBackup(self.app.domains[0],
  410. vm_files,
  411. os.path.join('dom0-home', os.path.basename(home_dir)))
  412. self.total_backup_bytes = functools.reduce(
  413. lambda x, y: x + y.size, files_to_backup.values(), 0)
  414. return files_to_backup
  415. def get_backup_summary(self):
  416. summary = ""
  417. fields_to_display = [
  418. {"name": "VM", "width": 16},
  419. {"name": "type", "width": 12},
  420. {"name": "size", "width": 12}
  421. ]
  422. # Display the header
  423. for field in fields_to_display:
  424. fmt = "{{0:-^{0}}}-+".format(field["width"] + 1)
  425. summary += fmt.format('-')
  426. summary += "\n"
  427. for field in fields_to_display:
  428. fmt = "{{0:>{0}}} |".format(field["width"] + 1)
  429. summary += fmt.format(field["name"])
  430. summary += "\n"
  431. for field in fields_to_display:
  432. fmt = "{{0:-^{0}}}-+".format(field["width"] + 1)
  433. summary += fmt.format('-')
  434. summary += "\n"
  435. files_to_backup = self._files_to_backup
  436. for qid, vm_info in files_to_backup.items():
  437. summary_line = ""
  438. fmt = "{{0:>{0}}} |".format(fields_to_display[0]["width"] + 1)
  439. summary_line += fmt.format(vm_info['vm'].name)
  440. fmt = "{{0:>{0}}} |".format(fields_to_display[1]["width"] + 1)
  441. if qid == 0:
  442. summary_line += fmt.format("User home")
  443. elif isinstance(vm_info['vm'], qubes.vm.templatevm.TemplateVM):
  444. summary_line += fmt.format("Template VM")
  445. else:
  446. summary_line += fmt.format("VM" + (" + Sys" if
  447. vm_info['vm'].updateable else ""))
  448. vm_size = vm_info['size']
  449. fmt = "{{0:>{0}}} |".format(fields_to_display[2]["width"] + 1)
  450. summary_line += fmt.format(size_to_human(vm_size))
  451. if qid != 0 and vm_info['vm'].is_running():
  452. summary_line += " <-- The VM is running, please shut down it " \
  453. "before proceeding with the backup!"
  454. summary += summary_line + "\n"
  455. for field in fields_to_display:
  456. fmt = "{{0:-^{0}}}-+".format(field["width"] + 1)
  457. summary += fmt.format('-')
  458. summary += "\n"
  459. fmt = "{{0:>{0}}} |".format(fields_to_display[0]["width"] + 1)
  460. summary += fmt.format("Total size:")
  461. fmt = "{{0:>{0}}} |".format(
  462. fields_to_display[1]["width"] + 1 + 2 + fields_to_display[2][
  463. "width"] + 1)
  464. summary += fmt.format(size_to_human(self.total_backup_bytes))
  465. summary += "\n"
  466. for field in fields_to_display:
  467. fmt = "{{0:-^{0}}}-+".format(field["width"] + 1)
  468. summary += fmt.format('-')
  469. summary += "\n"
  470. vms_not_for_backup = [vm.name for vm in self.app.domains
  471. if vm not in self.vms_for_backup]
  472. summary += "VMs not selected for backup:\n - " + "\n - ".join(
  473. sorted(vms_not_for_backup))
  474. return summary
  475. def prepare_backup_header(self):
  476. header_file_path = os.path.join(self.tmpdir, HEADER_FILENAME)
  477. backup_header = BackupHeader(
  478. version=CURRENT_BACKUP_FORMAT_VERSION,
  479. hmac_algorithm=DEFAULT_HMAC_ALGORITHM,
  480. encrypted=True,
  481. compressed=self.compressed,
  482. compression_filter=self.compression_filter,
  483. backup_id=self.backup_id,
  484. )
  485. backup_header.save(header_file_path)
  486. # Start encrypt, scrypt will also handle integrity
  487. # protection
  488. scrypt_passphrase = u'{filename}!{passphrase}'.format(
  489. filename=HEADER_FILENAME, passphrase=self.passphrase)
  490. scrypt = launch_scrypt(
  491. 'enc', header_file_path, header_file_path + '.hmac',
  492. scrypt_passphrase)
  493. if scrypt.wait() != 0:
  494. raise qubes.exc.QubesException(
  495. "Failed to compute hmac of header file: "
  496. + scrypt.stderr.read())
  497. return HEADER_FILENAME, HEADER_FILENAME + ".hmac"
  498. @staticmethod
  499. def _queue_put_with_check(proc, vmproc, queue, element):
  500. if queue.full():
  501. if not proc.is_alive():
  502. if vmproc:
  503. message = ("Failed to write the backup, VM output:\n" +
  504. vmproc.stderr.read())
  505. else:
  506. message = "Failed to write the backup. Out of disk space?"
  507. raise qubes.exc.QubesException(message)
  508. queue.put(element)
  509. def _send_progress_update(self):
  510. if callable(self.progress_callback):
  511. progress = (
  512. 100 * (self._done_vms_bytes + self._current_vm_bytes) /
  513. self.total_backup_bytes)
  514. # pylint: disable=not-callable
  515. self.progress_callback(progress)
  516. def _add_vm_progress(self, bytes_done):
  517. self._current_vm_bytes += bytes_done
  518. self._send_progress_update()
  519. def backup_do(self):
  520. # pylint: disable=too-many-statements
  521. if self.passphrase is None:
  522. raise qubes.exc.QubesException("No passphrase set")
  523. qubes_xml = self.app.store
  524. self.tmpdir = tempfile.mkdtemp()
  525. shutil.copy(qubes_xml, os.path.join(self.tmpdir, 'qubes.xml'))
  526. qubes_xml = os.path.join(self.tmpdir, 'qubes.xml')
  527. backup_app = qubes.Qubes(qubes_xml)
  528. files_to_backup = self._files_to_backup
  529. # make sure backup_content isn't set initially
  530. for vm in backup_app.domains:
  531. vm.features['backup-content'] = False
  532. for qid, vm_info in files_to_backup.items():
  533. if qid != 0 and vm_info.vm.is_running():
  534. raise qubes.exc.QubesVMNotHaltedError(vm_info.vm)
  535. # VM is included in the backup
  536. backup_app.domains[qid].features['backup-content'] = True
  537. backup_app.domains[qid].features['backup-path'] = vm_info.subdir
  538. backup_app.domains[qid].features['backup-size'] = vm_info.size
  539. backup_app.save()
  540. vmproc = None
  541. tar_sparse = None
  542. if self.target_vm is not None:
  543. # Prepare the backup target (Qubes service call)
  544. # If APPVM, STDOUT is a PIPE
  545. vmproc = self.target_vm.run_service('qubes.Backup',
  546. passio_popen=True, passio_stderr=True)
  547. vmproc.stdin.write((self.target_dir.
  548. replace("\r", "").replace("\n", "") + "\n").encode())
  549. vmproc.stdin.flush()
  550. backup_stdout = vmproc.stdin
  551. self.processes_to_kill_on_cancel.append(vmproc)
  552. else:
  553. # Prepare the backup target (local file)
  554. if os.path.isdir(self.target_dir):
  555. backup_target = self.target_dir + "/qubes-{0}". \
  556. format(time.strftime("%Y-%m-%dT%H%M%S"))
  557. else:
  558. backup_target = self.target_dir
  559. # Create the target directory
  560. if not os.path.exists(os.path.dirname(self.target_dir)):
  561. raise qubes.exc.QubesException(
  562. "ERROR: the backup directory for {0} does not exists".
  563. format(self.target_dir))
  564. # If not APPVM, STDOUT is a local file
  565. backup_stdout = open(backup_target, 'wb')
  566. # Tar with tape length does not deals well with stdout
  567. # (close stdout between two tapes)
  568. # For this reason, we will use named pipes instead
  569. self.log.debug("Working in {}".format(self.tmpdir))
  570. backup_pipe = os.path.join(self.tmpdir, "backup_pipe")
  571. self.log.debug("Creating pipe in: {}".format(backup_pipe))
  572. os.mkfifo(backup_pipe)
  573. self.log.debug("Will backup: {}".format(files_to_backup))
  574. header_files = self.prepare_backup_header()
  575. # Setup worker to send encrypted data chunks to the backup_target
  576. to_send = Queue(10)
  577. send_proc = SendWorker(to_send, self.tmpdir, backup_stdout)
  578. send_proc.start()
  579. for file_name in header_files:
  580. to_send.put(file_name)
  581. qubes_xml_info = self.VMToBackup(
  582. None,
  583. [self.FileToBackup(qubes_xml, '')],
  584. ''
  585. )
  586. for vm_info in itertools.chain([qubes_xml_info],
  587. files_to_backup.values()):
  588. for file_info in vm_info.files:
  589. self.log.debug("Backing up {}".format(file_info))
  590. backup_tempfile = os.path.join(
  591. self.tmpdir, file_info.subdir,
  592. file_info.name)
  593. self.log.debug("Using temporary location: {}".format(
  594. backup_tempfile))
  595. # Ensure the temporary directory exists
  596. if not os.path.isdir(os.path.dirname(backup_tempfile)):
  597. os.makedirs(os.path.dirname(backup_tempfile))
  598. # The first tar cmd can use any complex feature as we want.
  599. # Files will be verified before untaring this.
  600. # Prefix the path in archive with filename["subdir"] to have it
  601. # verified during untar
  602. tar_cmdline = (["tar", "-Pc", '--sparse',
  603. "-f", backup_pipe,
  604. '-C', os.path.dirname(file_info.path)] +
  605. (['--dereference'] if
  606. file_info.subdir != "dom0-home/" else []) +
  607. ['--xform=s:^%s:%s\\0:' % (
  608. os.path.basename(file_info.path),
  609. file_info.subdir),
  610. os.path.basename(file_info.path)
  611. ])
  612. file_stat = os.stat(file_info.path)
  613. if stat.S_ISBLK(file_stat.st_mode) or \
  614. file_info.name != os.path.basename(file_info.path):
  615. # tar doesn't handle content of block device, use our
  616. # writer
  617. # also use our tar writer when renaming file
  618. assert not stat.S_ISDIR(file_stat.st_mode),\
  619. "Renaming directories not supported"
  620. tar_cmdline = ['python3', '-m', 'qubes.tarwriter',
  621. '--override-name=%s' % (
  622. os.path.join(file_info.subdir, os.path.basename(
  623. file_info.name))),
  624. file_info.path,
  625. backup_pipe]
  626. if self.compressed:
  627. tar_cmdline.insert(-2,
  628. "--use-compress-program=%s" % self.compression_filter)
  629. self.log.debug(" ".join(tar_cmdline))
  630. # Pipe: tar-sparse | scrypt | tar | backup_target
  631. # TODO: log handle stderr
  632. tar_sparse = subprocess.Popen(
  633. tar_cmdline)
  634. self.processes_to_kill_on_cancel.append(tar_sparse)
  635. # Wait for compressor (tar) process to finish or for any
  636. # error of other subprocesses
  637. i = 0
  638. pipe = open(backup_pipe, 'rb')
  639. run_error = "paused"
  640. while run_error == "paused":
  641. # Prepare a first chunk
  642. chunkfile = backup_tempfile + ".%03d.enc" % i
  643. i += 1
  644. # Start encrypt, scrypt will also handle integrity
  645. # protection
  646. scrypt_passphrase = \
  647. u'{backup_id}!{filename}!{passphrase}'.format(
  648. backup_id=self.backup_id,
  649. filename=os.path.relpath(chunkfile[:-4],
  650. self.tmpdir),
  651. passphrase=self.passphrase)
  652. scrypt = launch_scrypt(
  653. "enc", "-", chunkfile, scrypt_passphrase)
  654. run_error = handle_streams(
  655. pipe,
  656. {'backup_target': scrypt.stdin},
  657. {'vmproc': vmproc,
  658. 'addproc': tar_sparse,
  659. 'scrypt': scrypt,
  660. },
  661. self.chunk_size,
  662. self._add_vm_progress
  663. )
  664. self.log.debug(
  665. "Wait_backup_feedback returned: {}".format(run_error))
  666. if self.canceled:
  667. try:
  668. tar_sparse.terminate()
  669. except OSError:
  670. pass
  671. tar_sparse.wait()
  672. to_send.put(QUEUE_ERROR)
  673. send_proc.join()
  674. shutil.rmtree(self.tmpdir)
  675. raise BackupCanceledError("Backup canceled")
  676. if run_error and run_error != "size_limit":
  677. send_proc.terminate()
  678. if run_error == "VM" and vmproc:
  679. raise qubes.exc.QubesException(
  680. "Failed to write the backup, VM output:\n" +
  681. vmproc.stderr.read(MAX_STDERR_BYTES))
  682. else:
  683. raise qubes.exc.QubesException(
  684. "Failed to perform backup: error in " +
  685. run_error)
  686. scrypt.stdin.close()
  687. scrypt.wait()
  688. self.log.debug("scrypt return code: {}".format(
  689. scrypt.poll()))
  690. # Send the chunk to the backup target
  691. self._queue_put_with_check(
  692. send_proc, vmproc, to_send,
  693. os.path.relpath(chunkfile, self.tmpdir))
  694. if tar_sparse.poll() is None or run_error == "size_limit":
  695. run_error = "paused"
  696. else:
  697. self.processes_to_kill_on_cancel.remove(tar_sparse)
  698. self.log.debug(
  699. "Finished tar sparse with exit code {}".format(
  700. tar_sparse.poll()))
  701. pipe.close()
  702. # This VM done, update progress
  703. self._done_vms_bytes += vm_info.size
  704. self._current_vm_bytes = 0
  705. self._send_progress_update()
  706. # Save date of last backup
  707. if vm_info.vm:
  708. vm_info.vm.backup_timestamp = datetime.datetime.now()
  709. self._queue_put_with_check(send_proc, vmproc, to_send, QUEUE_FINISHED)
  710. send_proc.join()
  711. shutil.rmtree(self.tmpdir)
  712. if self.canceled:
  713. raise BackupCanceledError("Backup canceled")
  714. if send_proc.exitcode != 0:
  715. raise qubes.exc.QubesException(
  716. "Failed to send backup: error in the sending process")
  717. if vmproc:
  718. self.log.debug("VMProc1 proc return code: {}".format(vmproc.poll()))
  719. if tar_sparse is not None:
  720. self.log.debug("Sparse1 proc return code: {}".format(
  721. tar_sparse.poll()))
  722. vmproc.stdin.close()
  723. self.app.save()
  724. def handle_streams(stream_in, streams_out, processes, size_limit=None,
  725. progress_callback=None):
  726. '''
  727. Copy stream_in to all streams_out and monitor all mentioned processes.
  728. If any of them terminate with non-zero code, interrupt the process. Copy
  729. at most `size_limit` data (if given).
  730. :param stream_in: file-like object to read data from
  731. :param streams_out: dict of file-like objects to write data to
  732. :param processes: dict of subprocess.Popen objects to monitor
  733. :param size_limit: int maximum data amount to process
  734. :param progress_callback: callable function to report progress, will be
  735. given copied data size (it should accumulate internally)
  736. :return: failed process name, failed stream name, "size_limit" or None (
  737. no error)
  738. '''
  739. buffer_size = 409600
  740. bytes_copied = 0
  741. while True:
  742. if size_limit:
  743. to_copy = min(buffer_size, size_limit - bytes_copied)
  744. if to_copy <= 0:
  745. return "size_limit"
  746. else:
  747. to_copy = buffer_size
  748. buf = stream_in.read(to_copy)
  749. if not buf:
  750. # done
  751. return None
  752. if callable(progress_callback):
  753. progress_callback(len(buf))
  754. for name, stream in streams_out.items():
  755. if stream is None:
  756. continue
  757. try:
  758. stream.write(buf)
  759. except IOError:
  760. return name
  761. bytes_copied += len(buf)
  762. for name, proc in processes.items():
  763. if proc is None:
  764. continue
  765. if proc.poll():
  766. return name
  767. def get_supported_hmac_algo(hmac_algorithm=None):
  768. # Start with provided default
  769. if hmac_algorithm:
  770. yield hmac_algorithm
  771. if hmac_algorithm != 'scrypt':
  772. yield 'scrypt'
  773. proc = subprocess.Popen(['openssl', 'list-message-digest-algorithms'],
  774. stdout=subprocess.PIPE)
  775. for algo in proc.stdout.readlines():
  776. algo = algo.decode('ascii')
  777. if '=>' in algo:
  778. continue
  779. yield algo.strip()
  780. proc.wait()
  781. # vim:sw=4:et: