backup.py 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # The Qubes OS Project, http://www.qubes-os.org
  5. #
  6. # Copyright (C) 2013 Marek Marczykowski-Górecki <marmarek@invisiblethingslab.com>
  7. # Copyright (C) 2013 Olivier Médoc <o_medoc@yahoo.fr>
  8. #
  9. # This program is free software; you can redistribute it and/or
  10. # modify it under the terms of the GNU General Public License
  11. # as published by the Free Software Foundation; either version 2
  12. # of the License, or (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program; if not, write to the Free Software
  21. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22. #
  23. #
  24. from qubes import QubesException,QubesVmCollection
  25. from qubes import QubesVmClasses
  26. from qubes import system_path,vm_files
  27. from qubesutils import size_to_human, print_stdout, print_stderr, get_disk_usage
  28. import sys
  29. import os
  30. import subprocess
  31. import re
  32. import shutil
  33. import tempfile
  34. import time
  35. import grp,pwd
  36. import errno
  37. import datetime
  38. from multiprocessing import Queue,Process
  39. BACKUP_DEBUG = False
  40. HEADER_FILENAME = 'backup-header'
  41. DEFAULT_CRYPTO_ALGORITHM = 'aes-256-cbc'
  42. DEFAULT_HMAC_ALGORITHM = 'SHA1'
  43. # Maximum size of error message get from process stderr (including VM process)
  44. MAX_STDERR_BYTES = 1024
  45. # header + qubes.xml max size
  46. HEADER_QUBES_XML_MAX_SIZE = 1024 * 1024
  47. # global state for backup_cancel()
  48. running_backup_operation = None
  49. class BackupOperationInfo:
  50. def __init__(self):
  51. self.canceled = False
  52. self.processes_to_kill_on_cancel = []
  53. self.tmpdir_to_remove = None
  54. class BackupCanceledError(QubesException):
  55. def __init__(self, msg, tmpdir=None):
  56. super(BackupCanceledError, self).__init__(msg)
  57. self.tmpdir = tmpdir
  58. class BackupHeader:
  59. encrypted = 'encrypted'
  60. compressed = 'compressed'
  61. crypto_algorithm = 'crypto-algorithm'
  62. hmac_algorithm = 'hmac-algorithm'
  63. bool_options = ['encrypted', 'compressed']
  64. def file_to_backup (file_path, subdir = None):
  65. sz = get_disk_usage (file_path)
  66. if subdir is None:
  67. abs_file_path = os.path.abspath (file_path)
  68. abs_base_dir = os.path.abspath (system_path["qubes_base_dir"]) + '/'
  69. abs_file_dir = os.path.dirname (abs_file_path) + '/'
  70. (nothing, dir, subdir) = abs_file_dir.partition (abs_base_dir)
  71. assert nothing == ""
  72. assert dir == abs_base_dir
  73. else:
  74. if len(subdir) > 0 and not subdir.endswith('/'):
  75. subdir += '/'
  76. return [ { "path" : file_path, "size": sz, "subdir": subdir} ]
  77. def backup_cancel():
  78. """
  79. Cancel currently running backup/restore operation
  80. @return: True if any operation was signaled
  81. """
  82. if running_backup_operation is None:
  83. return False
  84. running_backup_operation.canceled = True
  85. for proc in running_backup_operation.processes_to_kill_on_cancel:
  86. try:
  87. proc.terminate()
  88. except:
  89. pass
  90. return True
  91. def backup_prepare(vms_list = None, exclude_list = None,
  92. print_callback = print_stdout, hide_vm_names=True):
  93. """If vms = None, include all (sensible) VMs; exclude_list is always applied"""
  94. files_to_backup = file_to_backup (system_path["qubes_store_filename"])
  95. if exclude_list is None:
  96. exclude_list = []
  97. qvm_collection = QubesVmCollection()
  98. qvm_collection.lock_db_for_writing()
  99. qvm_collection.load()
  100. if vms_list is None:
  101. all_vms = [vm for vm in qvm_collection.values()]
  102. selected_vms = [vm for vm in all_vms if vm.include_in_backups]
  103. appvms_to_backup = [vm for vm in selected_vms if vm.is_appvm() and not vm.internal]
  104. netvms_to_backup = [vm for vm in selected_vms if vm.is_netvm() and not vm.qid == 0]
  105. template_vms_worth_backingup = [vm for vm in selected_vms if (vm.is_template() and not vm.installed_by_rpm)]
  106. dom0 = [ qvm_collection[0] ]
  107. vms_list = appvms_to_backup + netvms_to_backup + template_vms_worth_backingup + dom0
  108. vms_for_backup = vms_list
  109. # Apply exclude list
  110. if exclude_list:
  111. vms_for_backup = [vm for vm in vms_list if vm.name not in exclude_list]
  112. no_vms = len (vms_for_backup)
  113. there_are_running_vms = False
  114. fields_to_display = [
  115. { "name": "VM", "width": 16},
  116. { "name": "type","width": 12 },
  117. { "name": "size", "width": 12}
  118. ]
  119. # Display the header
  120. s = ""
  121. for f in fields_to_display:
  122. fmt="{{0:-^{0}}}-+".format(f["width"] + 1)
  123. s += fmt.format('-')
  124. print_callback(s)
  125. s = ""
  126. for f in fields_to_display:
  127. fmt="{{0:>{0}}} |".format(f["width"] + 1)
  128. s += fmt.format(f["name"])
  129. print_callback(s)
  130. s = ""
  131. for f in fields_to_display:
  132. fmt="{{0:-^{0}}}-+".format(f["width"] + 1)
  133. s += fmt.format('-')
  134. print_callback(s)
  135. files_to_backup_index = 0
  136. for vm in vms_for_backup:
  137. if vm.is_template():
  138. # handle templates later
  139. continue
  140. if vm.qid == 0:
  141. # handle dom0 later
  142. continue
  143. if hide_vm_names:
  144. subdir = 'vm%d/' % vm.qid
  145. else:
  146. subdir = None
  147. if vm.private_img is not None:
  148. files_to_backup += file_to_backup(vm.private_img, subdir)
  149. if vm.is_appvm():
  150. files_to_backup += file_to_backup(vm.icon_path, subdir)
  151. if vm.updateable:
  152. if os.path.exists(vm.dir_path + "/apps.templates"):
  153. # template
  154. files_to_backup += file_to_backup(vm.dir_path + "/apps.templates", subdir)
  155. else:
  156. # standaloneVM
  157. files_to_backup += file_to_backup(vm.dir_path + "/apps", subdir)
  158. if os.path.exists(vm.dir_path + "/kernels"):
  159. files_to_backup += file_to_backup(vm.dir_path + "/kernels", subdir)
  160. if os.path.exists (vm.firewall_conf):
  161. files_to_backup += file_to_backup(vm.firewall_conf, subdir)
  162. if 'appmenus_whitelist' in vm_files and \
  163. os.path.exists(os.path.join(vm.dir_path, vm_files['appmenus_whitelist'])):
  164. files_to_backup += file_to_backup(
  165. os.path.join(vm.dir_path, vm_files['appmenus_whitelist']),
  166. subdir)
  167. if vm.updateable:
  168. files_to_backup += file_to_backup(vm.root_img, subdir)
  169. s = ""
  170. fmt="{{0:>{0}}} |".format(fields_to_display[0]["width"] + 1)
  171. s += fmt.format(vm.name)
  172. fmt="{{0:>{0}}} |".format(fields_to_display[1]["width"] + 1)
  173. if vm.is_netvm():
  174. s += fmt.format("NetVM" + (" + Sys" if vm.updateable else ""))
  175. else:
  176. s += fmt.format("AppVM" + (" + Sys" if vm.updateable else ""))
  177. vm_size = reduce(lambda x, y: x + y["size"],
  178. files_to_backup[files_to_backup_index:],
  179. 0)
  180. files_to_backup_index = len(files_to_backup)
  181. fmt="{{0:>{0}}} |".format(fields_to_display[2]["width"] + 1)
  182. s += fmt.format(size_to_human(vm_size))
  183. if vm.is_running():
  184. s += " <-- The VM is running, please shut it down before proceeding with the backup!"
  185. there_are_running_vms = True
  186. print_callback(s)
  187. for vm in vms_for_backup:
  188. if not vm.is_template():
  189. # already handled
  190. continue
  191. if vm.qid == 0:
  192. # handle dom0 later
  193. continue
  194. vm_sz = vm.get_disk_utilization()
  195. if hide_vm_names:
  196. template_subdir = 'vm%d/' % vm.qid
  197. else:
  198. template_subdir = os.path.relpath(
  199. vm.dir_path,
  200. system_path["qubes_base_dir"]) + '/'
  201. template_to_backup = [ {
  202. "path": vm.dir_path + '/.',
  203. "size": vm_sz,
  204. "subdir": template_subdir } ]
  205. files_to_backup += template_to_backup
  206. s = ""
  207. fmt="{{0:>{0}}} |".format(fields_to_display[0]["width"] + 1)
  208. s += fmt.format(vm.name)
  209. fmt="{{0:>{0}}} |".format(fields_to_display[1]["width"] + 1)
  210. s += fmt.format("Template VM")
  211. fmt="{{0:>{0}}} |".format(fields_to_display[2]["width"] + 1)
  212. s += fmt.format(size_to_human(vm_sz))
  213. if vm.is_running():
  214. s += " <-- The VM is running, please shut it down before proceeding with the backup!"
  215. there_are_running_vms = True
  216. print_callback(s)
  217. # Initialize backup flag on all VMs
  218. vms_for_backup_qid = [vm.qid for vm in vms_for_backup]
  219. for vm in qvm_collection.values():
  220. vm.backup_content = False
  221. if vm.qid == 0:
  222. # handle dom0 later
  223. continue
  224. if vm.qid in vms_for_backup_qid:
  225. vm.backup_content = True
  226. vm.backup_size = vm.get_disk_utilization()
  227. if hide_vm_names:
  228. vm.backup_path = 'vm%d' % vm.qid
  229. else:
  230. vm.backup_path = os.path.relpath(vm.dir_path, system_path["qubes_base_dir"])
  231. # Dom0 user home
  232. if 0 in vms_for_backup_qid:
  233. local_user = grp.getgrnam('qubes').gr_mem[0]
  234. home_dir = pwd.getpwnam(local_user).pw_dir
  235. # Home dir should have only user-owned files, so fix it now to prevent
  236. # permissions problems - some root-owned files can left after
  237. # 'sudo bash' and similar commands
  238. subprocess.check_call(['sudo', 'chown', '-R', local_user, home_dir])
  239. home_sz = get_disk_usage(home_dir)
  240. home_to_backup = [ { "path" : home_dir, "size": home_sz, "subdir": 'dom0-home/'} ]
  241. files_to_backup += home_to_backup
  242. vm = qvm_collection[0]
  243. vm.backup_content = True
  244. vm.backup_size = home_sz
  245. vm.backup_path = os.path.join('dom0-home', os.path.basename(home_dir))
  246. s = ""
  247. fmt="{{0:>{0}}} |".format(fields_to_display[0]["width"] + 1)
  248. s += fmt.format('Dom0')
  249. fmt="{{0:>{0}}} |".format(fields_to_display[1]["width"] + 1)
  250. s += fmt.format("User home")
  251. fmt="{{0:>{0}}} |".format(fields_to_display[2]["width"] + 1)
  252. s += fmt.format(size_to_human(home_sz))
  253. print_callback(s)
  254. qvm_collection.save()
  255. # FIXME: should be after backup completed
  256. qvm_collection.unlock_db()
  257. total_backup_sz = 0
  258. for file in files_to_backup:
  259. total_backup_sz += file["size"]
  260. s = ""
  261. for f in fields_to_display:
  262. fmt="{{0:-^{0}}}-+".format(f["width"] + 1)
  263. s += fmt.format('-')
  264. print_callback(s)
  265. s = ""
  266. fmt="{{0:>{0}}} |".format(fields_to_display[0]["width"] + 1)
  267. s += fmt.format("Total size:")
  268. fmt="{{0:>{0}}} |".format(fields_to_display[1]["width"] + 1 + 2 + fields_to_display[2]["width"] + 1)
  269. s += fmt.format(size_to_human(total_backup_sz))
  270. print_callback(s)
  271. s = ""
  272. for f in fields_to_display:
  273. fmt="{{0:-^{0}}}-+".format(f["width"] + 1)
  274. s += fmt.format('-')
  275. print_callback(s)
  276. vms_not_for_backup = [vm.name for vm in qvm_collection.values() if not vm
  277. .backup_content]
  278. print_callback("VMs not selected for backup: %s" % " ".join(
  279. vms_not_for_backup))
  280. if (there_are_running_vms):
  281. raise QubesException("Please shutdown all VMs before proceeding.")
  282. for fileinfo in files_to_backup:
  283. assert len(fileinfo["subdir"]) == 0 or fileinfo["subdir"][-1] == '/', \
  284. "'subdir' must ends with a '/': %s" % str(fileinfo)
  285. return files_to_backup
  286. class SendWorker(Process):
  287. def __init__(self, queue, base_dir, backup_stdout):
  288. super(SendWorker, self).__init__()
  289. self.queue = queue
  290. self.base_dir = base_dir
  291. self.backup_stdout = backup_stdout
  292. def run(self):
  293. if BACKUP_DEBUG:
  294. print "Started sending thread"
  295. if BACKUP_DEBUG:
  296. print "Moving to temporary dir", self.base_dir
  297. os.chdir(self.base_dir)
  298. for filename in iter(self.queue.get,None):
  299. if filename == "FINISHED" or filename == "ERROR":
  300. break
  301. if BACKUP_DEBUG:
  302. print "Sending file", filename
  303. # This tar used for sending data out need to be as simple, as
  304. # simple, as featureless as possible. It will not be
  305. # verified before untaring.
  306. tar_final_cmd = ["tar", "-cO", "--posix",
  307. "-C", self.base_dir, filename]
  308. final_proc = subprocess.Popen (tar_final_cmd,
  309. stdin=subprocess.PIPE, stdout=self.backup_stdout)
  310. if final_proc.wait() >= 2:
  311. # handle only exit code 2 (tar fatal error) or greater (call failed?)
  312. raise QubesException("ERROR: Failed to write the backup, out of disk space? "
  313. "Check console output or ~/.xsession-errors for details.")
  314. # Delete the file as we don't need it anymore
  315. if BACKUP_DEBUG:
  316. print "Removing file", filename
  317. os.remove(filename)
  318. if BACKUP_DEBUG:
  319. print "Finished sending thread"
  320. def prepare_backup_header(target_directory, passphrase, compressed=False,
  321. encrypted=False,
  322. hmac_algorithm=DEFAULT_HMAC_ALGORITHM,
  323. crypto_algorithm=DEFAULT_CRYPTO_ALGORITHM):
  324. header_file_path = os.path.join(target_directory, HEADER_FILENAME)
  325. with open(header_file_path, "w") as f:
  326. f.write("%s=%s\n" % (BackupHeader.hmac_algorithm, hmac_algorithm))
  327. f.write("%s=%s\n" % (BackupHeader.crypto_algorithm, crypto_algorithm))
  328. f.write("%s=%s\n" % (BackupHeader.encrypted, str(encrypted)))
  329. f.write("%s=%s\n" % (BackupHeader.compressed, str(compressed)))
  330. hmac = subprocess.Popen (["openssl", "dgst",
  331. "-" + hmac_algorithm, "-hmac", passphrase],
  332. stdin=open(header_file_path, "r"),
  333. stdout=open(header_file_path + ".hmac", "w"))
  334. if hmac.wait() != 0:
  335. raise QubesException("Failed to compute hmac of header file")
  336. return (HEADER_FILENAME, HEADER_FILENAME+".hmac")
  337. def backup_do(base_backup_dir, files_to_backup, passphrase,
  338. progress_callback = None, encrypted=False, appvm=None,
  339. compressed=False, hmac_algorithm=DEFAULT_HMAC_ALGORITHM,
  340. crypto_algorithm=DEFAULT_CRYPTO_ALGORITHM):
  341. global running_backup_operation
  342. total_backup_sz = 0
  343. passphrase = passphrase.encode('utf-8')
  344. for file in files_to_backup:
  345. total_backup_sz += file["size"]
  346. if compressed and encrypted:
  347. raise QubesException("Compressed and encrypted backups are not "
  348. "supported (yet).")
  349. running_backup_operation = BackupOperationInfo()
  350. vmproc = None
  351. if appvm != None:
  352. # Prepare the backup target (Qubes service call)
  353. backup_target = "QUBESRPC qubes.Backup dom0"
  354. # If APPVM, STDOUT is a PIPE
  355. vmproc = appvm.run(command=backup_target, passio_popen=True,
  356. passio_stderr=True)
  357. vmproc.stdin.write(base_backup_dir.
  358. replace("\r", "").replace("\n", "")+"\n")
  359. backup_stdout = vmproc.stdin
  360. running_backup_operation.processes_to_kill_on_cancel.append(vmproc)
  361. else:
  362. # Prepare the backup target (local file)
  363. if os.path.isdir(base_backup_dir):
  364. backup_target = base_backup_dir + "/qubes-{0}". \
  365. format(time.strftime("%Y-%m-%dT%H%M%S"))
  366. else:
  367. backup_target = base_backup_dir
  368. # Create the target directory
  369. if not os.path.exists (os.path.dirname(base_backup_dir)):
  370. raise QubesException(
  371. "ERROR: the backup directory for {0} does not exists".
  372. format(base_backup_dir))
  373. # If not APPVM, STDOUT is a local file
  374. backup_stdout = open(backup_target,'wb')
  375. global blocks_backedup
  376. blocks_backedup = 0
  377. progress = blocks_backedup * 11 / total_backup_sz
  378. progress_callback(progress)
  379. backup_tmpdir = tempfile.mkdtemp(prefix="/var/tmp/backup_")
  380. running_backup_operation.tmpdir_to_remove = backup_tmpdir
  381. # Tar with tapelength does not deals well with stdout (close stdout between
  382. # two tapes)
  383. # For this reason, we will use named pipes instead
  384. if BACKUP_DEBUG:
  385. print "Working in", backup_tmpdir
  386. backup_pipe = os.path.join(backup_tmpdir, "backup_pipe")
  387. if BACKUP_DEBUG:
  388. print "Creating pipe in:", backup_pipe
  389. os.mkfifo(backup_pipe)
  390. if BACKUP_DEBUG:
  391. print "Will backup:", files_to_backup
  392. header_files = prepare_backup_header(backup_tmpdir, passphrase,
  393. compressed=compressed,
  394. encrypted=encrypted,
  395. hmac_algorithm=hmac_algorithm,
  396. crypto_algorithm=crypto_algorithm)
  397. # Setup worker to send encrypted data chunks to the backup_target
  398. def compute_progress(new_size, total_backup_sz):
  399. global blocks_backedup
  400. blocks_backedup += new_size
  401. progress = blocks_backedup / float(total_backup_sz)
  402. progress_callback(int(round(progress*100,2)))
  403. to_send = Queue(10)
  404. send_proc = SendWorker(to_send, backup_tmpdir, backup_stdout)
  405. send_proc.start()
  406. for f in header_files:
  407. to_send.put(f)
  408. for filename in files_to_backup:
  409. if BACKUP_DEBUG:
  410. print "Backing up", filename
  411. backup_tempfile = os.path.join(backup_tmpdir,
  412. filename["subdir"],
  413. os.path.basename(filename["path"]))
  414. if BACKUP_DEBUG:
  415. print "Using temporary location:", backup_tempfile
  416. # Ensure the temporary directory exists
  417. if not os.path.isdir(os.path.dirname(backup_tempfile)):
  418. os.makedirs(os.path.dirname(backup_tempfile))
  419. # The first tar cmd can use any complex feature as we want. Files will
  420. # be verified before untaring this.
  421. # Prefix the path in archive with filename["subdir"] to have it verified during untar
  422. tar_cmdline = ["tar", "-Pc", '--sparse',
  423. "-f", backup_pipe,
  424. '--tape-length', str(100000),
  425. '-C', os.path.dirname(filename["path"]),
  426. '--xform', 's:^%s:%s\\0:' % (
  427. os.path.basename(filename["path"]),
  428. filename["subdir"]),
  429. os.path.basename(filename["path"])
  430. ]
  431. if BACKUP_DEBUG:
  432. print " ".join(tar_cmdline)
  433. # Tips: Popen(bufsize=0)
  434. # Pipe: tar-sparse | encryptor [| hmac] | tar | backup_target
  435. # Pipe: tar-sparse [| hmac] | tar | backup_target
  436. tar_sparse = subprocess.Popen (tar_cmdline, stdin=subprocess.PIPE,
  437. stderr=(open(os.devnull, 'w') if not BACKUP_DEBUG else None))
  438. running_backup_operation.processes_to_kill_on_cancel.append(tar_sparse)
  439. # Wait for compressor (tar) process to finish or for any error of other
  440. # subprocesses
  441. i = 0
  442. run_error = "paused"
  443. while run_error == "paused":
  444. pipe = open(backup_pipe,'rb')
  445. # Start HMAC
  446. hmac = subprocess.Popen (["openssl", "dgst",
  447. "-" + hmac_algorithm, "-hmac", passphrase],
  448. stdin=subprocess.PIPE, stdout=subprocess.PIPE)
  449. # Prepare a first chunk
  450. chunkfile = backup_tempfile + "." + "%03d" % i
  451. i += 1
  452. chunkfile_p = open(chunkfile,'wb')
  453. common_args = {
  454. 'backup_target': chunkfile_p,
  455. 'total_backup_sz': total_backup_sz,
  456. 'hmac': hmac,
  457. 'vmproc': vmproc,
  458. 'addproc': tar_sparse,
  459. 'progress_callback': compute_progress,
  460. }
  461. if encrypted:
  462. # Start encrypt
  463. # If no cipher is provided, the data is forwarded unencrypted !!!
  464. encryptor = subprocess.Popen (["openssl", "enc",
  465. "-e", "-" + crypto_algorithm,
  466. "-pass", "pass:"+passphrase] +
  467. (["-z"] if compressed else []),
  468. stdin=pipe, stdout=subprocess.PIPE)
  469. run_error = wait_backup_feedback(
  470. in_stream=encryptor.stdout, streamproc=encryptor,
  471. **common_args)
  472. elif compressed:
  473. compressor = subprocess.Popen (["gzip"],
  474. stdin=pipe, stdout=subprocess.PIPE)
  475. run_error = wait_backup_feedback(
  476. in_stream=compressor.stdout, streamproc=compressor,
  477. **common_args)
  478. else:
  479. run_error = wait_backup_feedback(
  480. in_stream=pipe, streamproc=None,
  481. **common_args)
  482. chunkfile_p.close()
  483. if BACKUP_DEBUG:
  484. print "Wait_backup_feedback returned:", run_error
  485. if running_backup_operation.canceled:
  486. try:
  487. tar_sparse.terminate()
  488. except:
  489. pass
  490. try:
  491. hmac.terminate()
  492. except:
  493. pass
  494. tar_sparse.wait()
  495. hmac.wait()
  496. to_send.put("ERROR")
  497. send_proc.join()
  498. shutil.rmtree(backup_tmpdir)
  499. running_backup_operation = None
  500. raise BackupCanceledError("Backup canceled")
  501. if len(run_error) > 0:
  502. send_proc.terminate()
  503. if run_error == "VM" and vmproc:
  504. raise QubesException("Failed to write the backup, VM output:\n" +
  505. vmproc.stderr.read(MAX_STDERR_BYTES))
  506. else:
  507. raise QubesException("Failed to perform backup: error in "+ \
  508. run_error)
  509. # Send the chunk to the backup target
  510. to_send.put(os.path.relpath(chunkfile, backup_tmpdir))
  511. # Close HMAC
  512. hmac.stdin.close()
  513. hmac.wait()
  514. if BACKUP_DEBUG:
  515. print "HMAC proc return code:", hmac.poll()
  516. # Write HMAC data next to the chunk file
  517. hmac_data = hmac.stdout.read()
  518. if BACKUP_DEBUG:
  519. print "Writing hmac to", chunkfile+".hmac"
  520. hmac_file = open(chunkfile+".hmac",'w')
  521. hmac_file.write(hmac_data)
  522. hmac_file.flush()
  523. hmac_file.close()
  524. pipe.close()
  525. # Send the HMAC to the backup target
  526. to_send.put(os.path.relpath(chunkfile, backup_tmpdir)+".hmac")
  527. if tar_sparse.poll() is None:
  528. # Release the next chunk
  529. if BACKUP_DEBUG:
  530. print "Release next chunk for process:", tar_sparse.poll()
  531. #tar_sparse.stdout = subprocess.PIPE
  532. tar_sparse.stdin.write("\n")
  533. tar_sparse.stdin.flush()
  534. run_error="paused"
  535. else:
  536. running_backup_operation.processes_to_kill_on_cancel.remove(
  537. tar_sparse)
  538. if BACKUP_DEBUG:
  539. print "Finished tar sparse with error", tar_sparse.poll()
  540. to_send.put("FINISHED")
  541. send_proc.join()
  542. shutil.rmtree(backup_tmpdir)
  543. if running_backup_operation.canceled:
  544. running_backup_operation = None
  545. raise BackupCanceledError("Backup canceled")
  546. running_backup_operation = None
  547. if send_proc.exitcode != 0:
  548. raise QubesException("Failed to send backup: error in the sending process")
  549. if vmproc:
  550. if BACKUP_DEBUG:
  551. print "VMProc1 proc return code:", vmproc.poll()
  552. print "Sparse1 proc return code:", tar_sparse.poll()
  553. vmproc.stdin.close()
  554. # Save date of last backup
  555. qvm_collection = QubesVmCollection()
  556. qvm_collection.lock_db_for_writing()
  557. qvm_collection.load()
  558. for vm in qvm_collection.values():
  559. if vm.backup_content:
  560. vm.backup_timestamp = datetime.datetime.now()
  561. qvm_collection.save()
  562. qvm_collection.unlock_db()
  563. '''
  564. ' Wait for backup chunk to finish
  565. ' - Monitor all the processes (streamproc, hmac, vmproc, addproc) for errors
  566. ' - Copy stdout of streamproc to backup_target and hmac stdin if available
  567. ' - Compute progress based on total_backup_sz and send progress to
  568. ' progress_callback function
  569. ' - Returns if
  570. ' - one of the monitored processes error out (streamproc, hmac, vmproc,
  571. ' addproc), along with the processe that failed
  572. ' - all of the monitored processes except vmproc finished successfully
  573. ' (vmproc termination is controlled by the python script)
  574. ' - streamproc does not delivers any data anymore (return with the error
  575. ' "")
  576. '''
  577. def wait_backup_feedback(progress_callback, in_stream, streamproc,
  578. backup_target, total_backup_sz, hmac=None, vmproc=None, addproc=None,
  579. remove_trailing_bytes=0):
  580. buffer_size = 409600
  581. run_error = None
  582. run_count = 1
  583. blocks_backedup = 0
  584. while run_count > 0 and run_error == None:
  585. buffer = in_stream.read(buffer_size)
  586. progress_callback(len(buffer), total_backup_sz)
  587. run_count = 0
  588. if hmac:
  589. retcode=hmac.poll()
  590. if retcode != None:
  591. if retcode != 0:
  592. run_error = "hmac"
  593. else:
  594. run_count += 1
  595. if addproc:
  596. retcode=addproc.poll()
  597. if retcode != None:
  598. if retcode != 0:
  599. run_error = "addproc"
  600. else:
  601. run_count += 1
  602. if vmproc:
  603. retcode = vmproc.poll()
  604. if retcode != None:
  605. if retcode != 0:
  606. run_error = "VM"
  607. if BACKUP_DEBUG:
  608. print vmproc.stdout.read()
  609. else:
  610. # VM should run until the end
  611. pass
  612. if streamproc:
  613. retcode=streamproc.poll()
  614. if retcode != None:
  615. if retcode != 0:
  616. run_error = "streamproc"
  617. break
  618. elif retcode == 0 and len(buffer) <= 0:
  619. return ""
  620. run_count += 1
  621. else:
  622. if len(buffer) <= 0:
  623. return ""
  624. backup_target.write(buffer)
  625. if hmac:
  626. hmac.stdin.write(buffer)
  627. return run_error
  628. def verify_hmac(filename, hmacfile, passphrase, algorithm):
  629. if BACKUP_DEBUG:
  630. print "Verifying file "+filename
  631. if hmacfile != filename + ".hmac":
  632. raise QubesException(
  633. "ERROR: expected hmac for {}, but got {}".\
  634. format(filename, hmacfile))
  635. hmac_proc = subprocess.Popen (["openssl", "dgst", "-" + algorithm,
  636. "-hmac", passphrase],
  637. stdin=open(filename,'rb'),
  638. stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  639. hmac_stdout, hmac_stderr = hmac_proc.communicate()
  640. if len(hmac_stderr) > 0:
  641. raise QubesException("ERROR: verify file {0}: {1}".format(filename, hmac_stderr))
  642. else:
  643. if BACKUP_DEBUG:
  644. print "Loading hmac for file " + filename
  645. hmac = load_hmac(open(hmacfile,'r').read())
  646. if len(hmac) > 0 and load_hmac(hmac_stdout) == hmac:
  647. os.unlink(hmacfile)
  648. if BACKUP_DEBUG:
  649. print "File verification OK -> Sending file " + filename
  650. return True
  651. else:
  652. raise QubesException(
  653. "ERROR: invalid hmac for file {0}: {1}. " \
  654. "Is the passphrase correct?".\
  655. format(filename, load_hmac(hmac_stdout)))
  656. # Not reachable
  657. return False
  658. class ExtractWorker(Process):
  659. def __init__(self, queue, base_dir, passphrase, encrypted, total_size,
  660. print_callback, error_callback, progress_callback, vmproc=None,
  661. compressed = False, crypto_algorithm=DEFAULT_CRYPTO_ALGORITHM):
  662. super(ExtractWorker, self).__init__()
  663. self.queue = queue
  664. self.base_dir = base_dir
  665. self.passphrase = passphrase
  666. self.encrypted = encrypted
  667. self.compressed = compressed
  668. self.crypto_algorithm = crypto_algorithm
  669. self.total_size = total_size
  670. self.blocks_backedup = 0
  671. self.tar2_process = None
  672. self.tar2_current_file = None
  673. self.decompressor_process = None
  674. self.decryptor_process = None
  675. self.print_callback = print_callback
  676. self.error_callback = error_callback
  677. self.progress_callback = progress_callback
  678. self.vmproc = vmproc
  679. self.restore_pipe = os.path.join(self.base_dir,"restore_pipe")
  680. if BACKUP_DEBUG:
  681. print "Creating pipe in:", self.restore_pipe
  682. os.mkfifo(self.restore_pipe)
  683. def compute_progress(self, new_size, total_size):
  684. if self.progress_callback:
  685. self.blocks_backedup += new_size
  686. progress = self.blocks_backedup / float(self.total_size)
  687. progress = int(round(progress*100,2))
  688. self.progress_callback(progress)
  689. def run(self):
  690. try:
  691. self.__run__()
  692. except Exception as e:
  693. exc_type, exc_value, exc_traceback = sys.exc_info()
  694. # Cleanup children
  695. for process in [self.decompressor_process,
  696. self.decryptor_process,
  697. self.tar2_process]:
  698. if process:
  699. # FIXME: kill()?
  700. try:
  701. process.terminate()
  702. except OSError:
  703. pass
  704. process.wait()
  705. self.error_callback(str(e))
  706. raise e, None, exc_traceback
  707. def __run__(self):
  708. if BACKUP_DEBUG:
  709. self.print_callback("Started sending thread")
  710. self.print_callback("Moving to dir "+self.base_dir)
  711. os.chdir(self.base_dir)
  712. filename = None
  713. for filename in iter(self.queue.get, None):
  714. if filename == "FINISHED" or filename == "ERROR":
  715. break
  716. if BACKUP_DEBUG:
  717. self.print_callback("Extracting file "+filename)
  718. if filename.endswith('.000'):
  719. # next file
  720. if self.tar2_process != None:
  721. if self.tar2_process.wait() != 0:
  722. raise QubesException(
  723. "ERROR: unable to extract files for {0}.".\
  724. format(self.tar2_current_file))
  725. else:
  726. # Finished extracting the tar file
  727. self.tar2_process = None
  728. self.tar2_current_file = None
  729. tar2_cmdline = ['tar',
  730. '-xMk%sf' % ("v" if BACKUP_DEBUG else ""), self.restore_pipe,
  731. os.path.relpath(filename.rstrip('.000'))]
  732. if BACKUP_DEBUG:
  733. self.print_callback("Running command "+str(tar2_cmdline))
  734. self.tar2_process = subprocess.Popen(tar2_cmdline,
  735. stdin=subprocess.PIPE,
  736. stderr=(None if BACKUP_DEBUG else open('/dev/null', 'w')))
  737. else:
  738. if BACKUP_DEBUG:
  739. self.print_callback("Releasing next chunck")
  740. self.tar2_process.stdin.write("\n")
  741. self.tar2_process.stdin.flush()
  742. self.tar2_current_file = filename
  743. pipe = open(self.restore_pipe,'wb')
  744. common_args = {
  745. 'backup_target': pipe,
  746. 'total_backup_sz': self.total_size,
  747. 'hmac': None,
  748. 'vmproc': self.vmproc,
  749. 'addproc': self.tar2_process
  750. }
  751. if self.encrypted:
  752. # Start decrypt
  753. self.decryptor_process = subprocess.Popen (["openssl", "enc",
  754. "-d", "-" + self.crypto_algorithm,
  755. "-pass", "pass:"+self.passphrase] +
  756. (["-z"] if self.compressed else []),
  757. stdin=open(filename,'rb'),
  758. stdout=subprocess.PIPE)
  759. run_error = wait_backup_feedback(
  760. progress_callback=self.compute_progress,
  761. in_stream=self.decryptor_process.stdout,
  762. streamproc=self.decryptor_process,
  763. **common_args)
  764. elif self.compressed:
  765. self.decompressor_process = subprocess.Popen (["gzip", "-d"],
  766. stdin=open(filename,'rb'),
  767. stdout=subprocess.PIPE)
  768. run_error = wait_backup_feedback(
  769. progress_callback=self.compute_progress,
  770. in_stream=self.decompressor_process.stdout,
  771. streamproc=self.decompressor_process,
  772. **common_args)
  773. else:
  774. run_error = wait_backup_feedback(
  775. progress_callback=self.compute_progress,
  776. in_stream=open(filename,"rb"), streamproc=None,
  777. **common_args)
  778. try:
  779. pipe.close()
  780. except IOError as e:
  781. if e.errno == errno.EPIPE:
  782. if BACKUP_DEBUG:
  783. self.error_callback("Got EPIPE while closing pipe to the inner tar process")
  784. # ignore the error
  785. else:
  786. raise
  787. if len(run_error):
  788. raise QubesException("Error while processing '%s': %s failed" % \
  789. (self.tar2_current_file, run_error))
  790. # Delete the file as we don't need it anymore
  791. if BACKUP_DEBUG:
  792. self.print_callback("Removing file "+filename)
  793. os.remove(filename)
  794. os.unlink(self.restore_pipe)
  795. if self.tar2_process is not None:
  796. if filename == "ERROR":
  797. self.tar2_process.terminate()
  798. self.tar2_process.wait()
  799. elif self.tar2_process.wait() != 0:
  800. raise QubesException(
  801. "ERROR: unable to extract files for {0}.{1}".
  802. format(self.tar2_current_file,
  803. (" Perhaps the backup is encrypted?"
  804. if not self.encrypted else "")))
  805. else:
  806. # Finished extracting the tar file
  807. self.tar2_process = None
  808. if BACKUP_DEBUG:
  809. self.print_callback("Finished extracting thread")
  810. def get_supported_hmac_algo(hmac_algorithm):
  811. # Start with provided default
  812. if hmac_algorithm:
  813. yield hmac_algorithm
  814. proc = subprocess.Popen(['openssl', 'list-message-digest-algorithms'],
  815. stdout=subprocess.PIPE)
  816. for algo in proc.stdout.readlines():
  817. if '=>' in algo:
  818. continue
  819. yield algo.strip()
  820. proc.wait()
  821. def parse_backup_header(filename):
  822. header_data = {}
  823. with open(filename, 'r') as f:
  824. for line in f.readlines():
  825. if line.count('=') != 1:
  826. raise QubesException("Invalid backup header (line %s)" % line)
  827. (key, value) = line.strip().split('=')
  828. if not any([key == getattr(BackupHeader, attr) for attr in dir(
  829. BackupHeader)]):
  830. # Ignoring unknown option
  831. continue
  832. if key in BackupHeader.bool_options:
  833. value = value.lower() in ["1", "true", "yes"]
  834. header_data[key] = value
  835. return header_data
  836. def restore_vm_dirs (backup_source, restore_tmpdir, passphrase, vms_dirs, vms,
  837. vms_size, print_callback=None, error_callback=None,
  838. progress_callback=None, encrypted=False, appvm=None,
  839. compressed = False, hmac_algorithm=DEFAULT_HMAC_ALGORITHM,
  840. crypto_algorithm=DEFAULT_CRYPTO_ALGORITHM):
  841. global running_backup_operation
  842. if BACKUP_DEBUG:
  843. print_callback("Working in temporary dir:"+restore_tmpdir)
  844. print_callback("Extracting data: " + size_to_human(vms_size)+" to restore")
  845. passphrase = passphrase.encode('utf-8')
  846. header_data = None
  847. vmproc = None
  848. if appvm != None:
  849. # Prepare the backup target (Qubes service call)
  850. backup_target = "QUBESRPC qubes.Restore dom0"
  851. # If APPVM, STDOUT is a PIPE
  852. vmproc = appvm.run(command = backup_target, passio_popen = True, passio_stderr=True)
  853. vmproc.stdin.write(backup_source.replace("\r","").replace("\n","")+"\n")
  854. # Send to tar2qfile the VMs that should be extracted
  855. vmproc.stdin.write(" ".join(vms_dirs)+"\n")
  856. if running_backup_operation:
  857. running_backup_operation.processes_to_kill_on_cancel.append(vmproc)
  858. backup_stdin = vmproc.stdout
  859. tar1_command = ['/usr/libexec/qubes/qfile-dom0-unpacker',
  860. str(os.getuid()), restore_tmpdir, '-v']
  861. else:
  862. backup_stdin = open(backup_source,'rb')
  863. tar1_command = ['tar',
  864. '-ixvf', backup_source,
  865. '-C', restore_tmpdir] + vms_dirs
  866. tar1_env = os.environ.copy()
  867. # TODO: add some safety margin?
  868. tar1_env['UPDATES_MAX_BYTES'] = str(vms_size)
  869. # Restoring only header
  870. if vms_dirs and vms_dirs[0] == HEADER_FILENAME:
  871. # backup-header, backup-header.hmac, qubes-xml.000, qubes-xml.000.hmac
  872. tar1_env['UPDATES_MAX_FILES'] = '4'
  873. else:
  874. # Currently each VM consists of at most 7 archives (count
  875. # file_to_backup calls in backup_prepare()), but add some safety
  876. # margin for further extensions. Each archive is divided into 100MB
  877. # chunks. Additionally each file have own hmac file. So assume upper
  878. # limit as 2*(10*COUNT_OF_VMS+TOTAL_SIZE/100MB)
  879. tar1_env['UPDATES_MAX_FILES'] = str(2*(10*len(vms_dirs) +
  880. int(vms_size/(100*1024*1024))))
  881. if BACKUP_DEBUG:
  882. print_callback("Run command"+str(tar1_command))
  883. command = subprocess.Popen(tar1_command,
  884. stdin=backup_stdin,
  885. stdout=vmproc.stdin if vmproc else subprocess.PIPE,
  886. stderr=subprocess.PIPE,
  887. env=tar1_env)
  888. if running_backup_operation:
  889. running_backup_operation.processes_to_kill_on_cancel.append(command)
  890. # qfile-dom0-unpacker output filelist on stderr (and have stdout connected
  891. # to the VM), while tar output filelist on stdout
  892. if appvm:
  893. filelist_pipe = command.stderr
  894. else:
  895. filelist_pipe = command.stdout
  896. expect_tar_error = False
  897. to_extract = Queue()
  898. nextfile = None
  899. # If want to analyze backup header, do it now
  900. if vms_dirs and vms_dirs[0] == HEADER_FILENAME:
  901. filename = filelist_pipe.readline().strip()
  902. hmacfile = filelist_pipe.readline().strip()
  903. if not appvm:
  904. nextfile = filelist_pipe.readline().strip()
  905. if BACKUP_DEBUG:
  906. print_callback("Got backup header and hmac: %s, %s" % (filename,
  907. hmacfile))
  908. if not filename or filename=="EOF" or \
  909. not hmacfile or hmacfile == "EOF":
  910. if appvm:
  911. vmproc.wait()
  912. proc_error_msg = vmproc.stderr.read(MAX_STDERR_BYTES)
  913. else:
  914. command.wait()
  915. proc_error_msg = command.stderr.read(MAX_STDERR_BYTES)
  916. raise QubesException("Premature end of archive while receiving "
  917. "backup header. Process output:\n" +
  918. proc_error_msg)
  919. filename = os.path.join(restore_tmpdir, filename)
  920. hmacfile = os.path.join(restore_tmpdir, hmacfile)
  921. file_ok = False
  922. for hmac_algo in get_supported_hmac_algo(hmac_algorithm):
  923. try:
  924. if verify_hmac(filename, hmacfile, passphrase, hmac_algo):
  925. file_ok = True
  926. hmac_algorithm = hmac_algo
  927. break
  928. except QubesException:
  929. # Ignore exception here, try the next algo
  930. pass
  931. if not file_ok:
  932. raise QubesException("Corrupted backup header (hmac verification "
  933. "failed). Is the password correct?")
  934. if os.path.basename(filename) == HEADER_FILENAME:
  935. header_data = parse_backup_header(filename)
  936. if BackupHeader.crypto_algorithm in header_data:
  937. crypto_algorithm = header_data[BackupHeader.crypto_algorithm]
  938. if BackupHeader.hmac_algorithm in header_data:
  939. hmac_algorithm = header_data[BackupHeader.hmac_algorithm]
  940. if BackupHeader.compressed in header_data:
  941. compressed = header_data[BackupHeader.compressed]
  942. if BackupHeader.encrypted in header_data:
  943. encrypted = header_data[BackupHeader.encrypted]
  944. os.unlink(filename)
  945. else:
  946. # If this isn't backup header, pass it to ExtractWorker
  947. to_extract.put(filename)
  948. # when tar do not find expected file in archive, it exit with
  949. # code 2. This will happen because we've requested backup-header
  950. # file, but the archive do not contain it. Ignore this particular
  951. # error.
  952. if not appvm:
  953. expect_tar_error = True
  954. # Setup worker to extract encrypted data chunks to the restore dirs
  955. # Create the process here to pass it options extracted from backup header
  956. extract_proc = ExtractWorker(queue=to_extract,
  957. base_dir=restore_tmpdir,
  958. passphrase=passphrase,
  959. encrypted=encrypted,
  960. compressed=compressed,
  961. crypto_algorithm = crypto_algorithm,
  962. total_size=vms_size,
  963. print_callback=print_callback,
  964. error_callback=error_callback,
  965. progress_callback=progress_callback)
  966. extract_proc.start()
  967. try:
  968. filename = None
  969. while True:
  970. if running_backup_operation and running_backup_operation.canceled:
  971. break
  972. if nextfile is not None:
  973. filename = nextfile
  974. else:
  975. filename = filelist_pipe.readline().strip()
  976. if BACKUP_DEBUG:
  977. print_callback("Getting new file:"+filename)
  978. if not filename or filename=="EOF":
  979. break
  980. hmacfile = filelist_pipe.readline().strip()
  981. if running_backup_operation and running_backup_operation.canceled:
  982. break
  983. # if reading archive directly with tar, wait for next filename -
  984. # tar prints filename before processing it, so wait for the next one to be
  985. # sure that whole file was extracted
  986. if not appvm:
  987. nextfile = filelist_pipe.readline().strip()
  988. if BACKUP_DEBUG:
  989. print_callback("Getting hmac:"+hmacfile)
  990. if not hmacfile or hmacfile=="EOF":
  991. # Premature end of archive, either of tar1_command or vmproc exited with error
  992. break
  993. if not any(map(lambda x: filename.startswith(x), vms_dirs)):
  994. if BACKUP_DEBUG:
  995. print_callback("Ignoring VM not selected for restore")
  996. os.unlink(os.path.join(restore_tmpdir, filename))
  997. os.unlink(os.path.join(restore_tmpdir, hmacfile))
  998. continue
  999. if verify_hmac(os.path.join(restore_tmpdir,filename),
  1000. os.path.join(restore_tmpdir,hmacfile),
  1001. passphrase, hmac_algorithm):
  1002. to_extract.put(os.path.join(restore_tmpdir, filename))
  1003. if running_backup_operation and running_backup_operation.canceled:
  1004. raise BackupCanceledError("Restore canceled",
  1005. tmpdir=restore_tmpdir)
  1006. if command.wait() != 0 and not expect_tar_error:
  1007. raise QubesException(
  1008. "ERROR: unable to read the qubes backup file {0} ({1}). " \
  1009. "Is it really a backup?".format(backup_source, command.wait()))
  1010. if vmproc:
  1011. if vmproc.wait() != 0:
  1012. raise QubesException(
  1013. "ERROR: unable to read the qubes backup {0} " \
  1014. "because of a VM error: {1}".format(
  1015. backup_source, vmproc.stderr.read(MAX_STDERR_BYTES)))
  1016. if filename and filename!="EOF":
  1017. raise QubesException("Premature end of archive, the last file was %s" % filename)
  1018. except:
  1019. to_extract.put("ERROR")
  1020. extract_proc.join()
  1021. running_backup_operation = None
  1022. raise
  1023. else:
  1024. to_extract.put("FINISHED")
  1025. if BACKUP_DEBUG:
  1026. print_callback("Waiting for the extraction process to finish...")
  1027. extract_proc.join()
  1028. if BACKUP_DEBUG:
  1029. print_callback("Extraction process finished with code:" + \
  1030. str(extract_proc.exitcode))
  1031. if extract_proc.exitcode != 0:
  1032. raise QubesException(
  1033. "ERROR: unable to extract the qubes backup. " \
  1034. "Check extracting process errors.")
  1035. return header_data
  1036. def backup_restore_set_defaults(options):
  1037. if 'use-default-netvm' not in options:
  1038. options['use-default-netvm'] = False
  1039. if 'use-none-netvm' not in options:
  1040. options['use-none-netvm'] = False
  1041. if 'use-default-template' not in options:
  1042. options['use-default-template'] = False
  1043. if 'dom0-home' not in options:
  1044. options['dom0-home'] = True
  1045. if 'replace-template' not in options:
  1046. options['replace-template'] = []
  1047. return options
  1048. def load_hmac(hmac):
  1049. hmac = hmac.strip().split("=")
  1050. if len(hmac) > 1:
  1051. hmac = hmac[1].strip()
  1052. else:
  1053. raise QubesException("ERROR: invalid hmac file content")
  1054. return hmac
  1055. def backup_detect_format_version(backup_location):
  1056. if os.path.exists(os.path.join(backup_location, 'qubes.xml')):
  1057. return 1
  1058. else:
  1059. return 2
  1060. def backup_restore_header(source, passphrase,
  1061. print_callback = print_stdout, error_callback = print_stderr,
  1062. encrypted=False, appvm=None, compressed = False, format_version = None,
  1063. hmac_algorithm = DEFAULT_HMAC_ALGORITHM,
  1064. crypto_algorithm = DEFAULT_CRYPTO_ALGORITHM):
  1065. global running_backup_operation
  1066. vmproc = None
  1067. running_backup_operation = None
  1068. restore_tmpdir = tempfile.mkdtemp(prefix="/var/tmp/restore_")
  1069. if format_version == None:
  1070. format_version = backup_detect_format_version(source)
  1071. if format_version == 1:
  1072. return (restore_tmpdir, os.path.join(source, 'qubes.xml'), None)
  1073. # tar2qfile matches only beginnings, while tar full path
  1074. if appvm:
  1075. extract_filter = [HEADER_FILENAME, 'qubes.xml.000']
  1076. else:
  1077. extract_filter = [HEADER_FILENAME, HEADER_FILENAME+'.hmac',
  1078. 'qubes.xml.000', 'qubes.xml.000.hmac']
  1079. header_data = restore_vm_dirs (source,
  1080. restore_tmpdir,
  1081. passphrase=passphrase,
  1082. vms_dirs=extract_filter,
  1083. vms=None,
  1084. vms_size=HEADER_QUBES_XML_MAX_SIZE,
  1085. hmac_algorithm=hmac_algorithm,
  1086. crypto_algorithm=crypto_algorithm,
  1087. print_callback=print_callback,
  1088. error_callback=error_callback,
  1089. progress_callback=None,
  1090. encrypted=encrypted,
  1091. compressed=compressed,
  1092. appvm=appvm)
  1093. return (restore_tmpdir, os.path.join(restore_tmpdir, "qubes.xml"),
  1094. header_data)
  1095. def restore_info_verify(restore_info, host_collection):
  1096. options = restore_info['$OPTIONS$']
  1097. for vm in restore_info.keys():
  1098. if vm in ['$OPTIONS$', 'dom0']:
  1099. continue
  1100. vm_info = restore_info[vm]
  1101. vm_info.pop('excluded', None)
  1102. if 'exclude' in options.keys():
  1103. if vm in options['exclude']:
  1104. vm_info['excluded'] = True
  1105. vm_info.pop('already-exists', None)
  1106. if host_collection.get_vm_by_name (vm) is not None:
  1107. vm_info['already-exists'] = True
  1108. # check template
  1109. vm_info.pop('missing-template', None)
  1110. if vm_info['template']:
  1111. template_name = vm_info['template']
  1112. host_template = host_collection.get_vm_by_name(template_name)
  1113. if not host_template or not host_template.is_template():
  1114. # Maybe the (custom) template is in the backup?
  1115. if not (template_name in restore_info.keys() and
  1116. restore_info[template_name]['vm'].is_template()):
  1117. if options['use-default-template']:
  1118. if 'orig-template' not in vm_info.keys():
  1119. vm_info['orig-template'] = template_name
  1120. vm_info['template'] = host_collection\
  1121. .get_default_template().name
  1122. else:
  1123. vm_info['missing-template'] = True
  1124. # check netvm
  1125. vm_info.pop('missing-netvm', None)
  1126. if vm_info['netvm']:
  1127. netvm_name = vm_info['netvm']
  1128. netvm_on_host = host_collection.get_vm_by_name (netvm_name)
  1129. # No netvm on the host?
  1130. if not ((netvm_on_host is not None) and netvm_on_host.is_netvm()):
  1131. # Maybe the (custom) netvm is in the backup?
  1132. if not (netvm_name in restore_info.keys() and \
  1133. restore_info[netvm_name]['vm'].is_netvm()):
  1134. if options['use-default-netvm']:
  1135. vm_info['netvm'] = host_collection\
  1136. .get_default_netvm().name
  1137. vm_info['vm'].uses_default_netvm = True
  1138. elif options['use-none-netvm']:
  1139. vm_info['netvm'] = None
  1140. else:
  1141. vm_info['missing-netvm'] = True
  1142. vm_info['good-to-go'] = not any([(prop in vm_info.keys()) for
  1143. prop in ['missing-netvm',
  1144. 'missing-template',
  1145. 'already-exists',
  1146. 'excluded']])
  1147. return restore_info
  1148. def backup_restore_prepare(backup_location, passphrase, options = {},
  1149. host_collection = None, encrypted=False, appvm=None,
  1150. compressed = False, print_callback = print_stdout, error_callback = print_stderr,
  1151. format_version=None, hmac_algorithm=DEFAULT_HMAC_ALGORITHM,
  1152. crypto_algorithm=DEFAULT_CRYPTO_ALGORITHM):
  1153. # Defaults
  1154. backup_restore_set_defaults(options)
  1155. #### Private functions begin
  1156. def is_vm_included_in_backup_v1 (backup_dir, vm):
  1157. if vm.qid == 0:
  1158. return os.path.exists(os.path.join(backup_dir,'dom0-home'))
  1159. backup_vm_dir_path = vm.dir_path.replace (system_path["qubes_base_dir"], backup_dir)
  1160. if os.path.exists (backup_vm_dir_path):
  1161. return True
  1162. else:
  1163. return False
  1164. def is_vm_included_in_backup_v2 (backup_dir, vm):
  1165. if vm.backup_content:
  1166. return True
  1167. else:
  1168. return False
  1169. def find_template_name(template, replaces):
  1170. rx_replace = re.compile("(.*):(.*)")
  1171. for r in replaces:
  1172. m = rx_replace.match(r)
  1173. if m.group(1) == template:
  1174. return m.group(2)
  1175. return template
  1176. #### Private functions end
  1177. # Format versions:
  1178. # 1 - Qubes R1, Qubes R2 beta1, beta2
  1179. # 2 - Qubes R2 beta3+
  1180. if format_version is None:
  1181. format_version = backup_detect_format_version(backup_location)
  1182. if format_version == 1:
  1183. is_vm_included_in_backup = is_vm_included_in_backup_v1
  1184. elif format_version == 2:
  1185. is_vm_included_in_backup = is_vm_included_in_backup_v2
  1186. if not appvm:
  1187. if not os.path.isfile(backup_location):
  1188. raise QubesException("Invalid backup location (not a file or "
  1189. "directory with qubes.xml)"
  1190. ": %s" % str(
  1191. backup_location))
  1192. else:
  1193. raise QubesException("Unknown backup format version: %s" % str(format_version))
  1194. (restore_tmpdir, qubes_xml, header_data) = backup_restore_header(
  1195. backup_location,
  1196. passphrase,
  1197. encrypted=encrypted,
  1198. appvm=appvm,
  1199. compressed=compressed,
  1200. hmac_algorithm=hmac_algorithm,
  1201. crypto_algorithm=crypto_algorithm,
  1202. print_callback=print_callback,
  1203. error_callback=error_callback,
  1204. format_version=format_version)
  1205. if header_data:
  1206. if BackupHeader.crypto_algorithm in header_data:
  1207. crypto_algorithm = header_data[BackupHeader.crypto_algorithm]
  1208. if BackupHeader.hmac_algorithm in header_data:
  1209. hmac_algorithm = header_data[BackupHeader.hmac_algorithm]
  1210. if BackupHeader.compressed in header_data:
  1211. compressed = header_data[BackupHeader.compressed]
  1212. if BackupHeader.encrypted in header_data:
  1213. encrypted = header_data[BackupHeader.encrypted]
  1214. if BACKUP_DEBUG:
  1215. print "Loading file", qubes_xml
  1216. backup_collection = QubesVmCollection(store_filename = qubes_xml)
  1217. backup_collection.lock_db_for_reading()
  1218. backup_collection.load()
  1219. if host_collection is None:
  1220. host_collection = QubesVmCollection()
  1221. host_collection.lock_db_for_reading()
  1222. host_collection.load()
  1223. host_collection.unlock_db()
  1224. backup_vms_list = [vm for vm in backup_collection.values()]
  1225. vms_to_restore = {}
  1226. # ... and the actual data
  1227. for vm in backup_vms_list:
  1228. if vm.qid == 0:
  1229. # Handle dom0 as special case later
  1230. continue
  1231. if is_vm_included_in_backup (backup_location, vm):
  1232. if BACKUP_DEBUG:
  1233. print vm.name,"is included in backup"
  1234. vms_to_restore[vm.name] = {}
  1235. vms_to_restore[vm.name]['vm'] = vm;
  1236. if vm.template is None:
  1237. vms_to_restore[vm.name]['template'] = None
  1238. else:
  1239. templatevm_name = find_template_name(vm.template.name, options['replace-template'])
  1240. vms_to_restore[vm.name]['template'] = templatevm_name
  1241. if vm.netvm is None:
  1242. vms_to_restore[vm.name]['netvm'] = None
  1243. else:
  1244. netvm_name = vm.netvm.name
  1245. vms_to_restore[vm.name]['netvm'] = netvm_name
  1246. # Set to None to not confuse QubesVm object from backup
  1247. # collection with host collection (further in clone_attrs). Set
  1248. # directly _netvm to suppress setter action, especially
  1249. # modifying firewall
  1250. vm._netvm = None
  1251. # Store restore parameters
  1252. options['location'] = backup_location
  1253. options['restore_tmpdir'] = restore_tmpdir
  1254. options['passphrase'] = passphrase
  1255. options['encrypted'] = encrypted
  1256. options['compressed'] = compressed
  1257. options['hmac_algorithm'] = hmac_algorithm
  1258. options['crypto_algorithm'] = crypto_algorithm
  1259. options['appvm'] = appvm
  1260. options['format_version'] = format_version
  1261. vms_to_restore['$OPTIONS$'] = options
  1262. vms_to_restore = restore_info_verify(vms_to_restore, host_collection)
  1263. # ...and dom0 home
  1264. if options['dom0-home'] and \
  1265. is_vm_included_in_backup(backup_location, backup_collection[0]):
  1266. vm = backup_collection[0]
  1267. vms_to_restore['dom0'] = {}
  1268. if format_version == 1:
  1269. vms_to_restore['dom0']['subdir'] = \
  1270. os.listdir(os.path.join(backup_location, 'dom0-home'))[0]
  1271. vms_to_restore['dom0']['size'] = 0 # unknown
  1272. else:
  1273. vms_to_restore['dom0']['subdir'] = vm.backup_path
  1274. vms_to_restore['dom0']['size'] = vm.backup_size
  1275. local_user = grp.getgrnam('qubes').gr_mem[0]
  1276. dom0_home = vms_to_restore['dom0']['subdir']
  1277. vms_to_restore['dom0']['username'] = os.path.basename(dom0_home)
  1278. if vms_to_restore['dom0']['username'] != local_user:
  1279. vms_to_restore['dom0']['username-mismatch'] = True
  1280. if not options['ignore-dom0-username-mismatch']:
  1281. vms_to_restore['dom0']['good-to-go'] = False
  1282. if 'good-to-go' not in vms_to_restore['dom0']:
  1283. vms_to_restore['dom0']['good-to-go'] = True
  1284. # Not needed - all the data stored in vms_to_restore
  1285. if format_version == 2:
  1286. os.unlink(qubes_xml)
  1287. return vms_to_restore
  1288. def backup_restore_print_summary(restore_info, print_callback = print_stdout):
  1289. fields = {
  1290. "qid": {"func": "vm.qid"},
  1291. "name": {"func": "('[' if vm.is_template() else '')\
  1292. + ('{' if vm.is_netvm() else '')\
  1293. + vm.name \
  1294. + (']' if vm.is_template() else '')\
  1295. + ('}' if vm.is_netvm() else '')"},
  1296. "type": {"func": "'Tpl' if vm.is_template() else \
  1297. 'HVM' if vm.type == 'HVM' else \
  1298. vm.type.replace('VM','')"},
  1299. "updbl" : {"func": "'Yes' if vm.updateable else ''"},
  1300. "template": {"func": "'n/a' if vm.is_template() or vm.template is None else\
  1301. vm_info['template']"},
  1302. "netvm": {"func": "'n/a' if vm.is_netvm() and not vm.is_proxyvm() else\
  1303. ('*' if vm.uses_default_netvm else '') +\
  1304. vm_info['netvm'] if vm_info['netvm'] is not None else '-'"},
  1305. "label" : {"func" : "vm.label.name"},
  1306. }
  1307. fields_to_display = ["name", "type", "template", "updbl", "netvm", "label" ]
  1308. # First calculate the maximum width of each field we want to display
  1309. total_width = 0
  1310. for f in fields_to_display:
  1311. fields[f]["max_width"] = len(f)
  1312. for vm_info in restore_info.values():
  1313. if 'vm' in vm_info.keys():
  1314. vm = vm_info['vm']
  1315. l = len(str(eval(fields[f]["func"])))
  1316. if l > fields[f]["max_width"]:
  1317. fields[f]["max_width"] = l
  1318. total_width += fields[f]["max_width"]
  1319. print_callback("")
  1320. print_callback("The following VMs are included in the backup:")
  1321. print_callback("")
  1322. # Display the header
  1323. s = ""
  1324. for f in fields_to_display:
  1325. fmt="{{0:-^{0}}}-+".format(fields[f]["max_width"] + 1)
  1326. s += fmt.format('-')
  1327. print_callback(s)
  1328. s = ""
  1329. for f in fields_to_display:
  1330. fmt="{{0:>{0}}} |".format(fields[f]["max_width"] + 1)
  1331. s += fmt.format(f)
  1332. print_callback(s)
  1333. s = ""
  1334. for f in fields_to_display:
  1335. fmt="{{0:-^{0}}}-+".format(fields[f]["max_width"] + 1)
  1336. s += fmt.format('-')
  1337. print_callback(s)
  1338. for vm_info in restore_info.values():
  1339. # Skip non-VM here
  1340. if not 'vm' in vm_info:
  1341. continue
  1342. vm = vm_info['vm']
  1343. s = ""
  1344. for f in fields_to_display:
  1345. fmt="{{0:>{0}}} |".format(fields[f]["max_width"] + 1)
  1346. s += fmt.format(eval(fields[f]["func"]))
  1347. if 'excluded' in vm_info and vm_info['excluded']:
  1348. s += " <-- Excluded from restore"
  1349. elif 'already-exists' in vm_info:
  1350. s += " <-- A VM with the same name already exists on the host!"
  1351. elif 'missing-template' in vm_info:
  1352. s += " <-- No matching template on the host or in the backup found!"
  1353. elif 'missing-netvm' in vm_info:
  1354. s += " <-- No matching netvm on the host or in the backup found!"
  1355. elif 'orig-template' in vm_info:
  1356. s += " <-- Original template was '%s'" % (vm_info['orig-template'])
  1357. print_callback(s)
  1358. if 'dom0' in restore_info.keys():
  1359. s = ""
  1360. for f in fields_to_display:
  1361. fmt="{{0:>{0}}} |".format(fields[f]["max_width"] + 1)
  1362. if f == "name":
  1363. s += fmt.format("Dom0")
  1364. elif f == "type":
  1365. s += fmt.format("Home")
  1366. else:
  1367. s += fmt.format("")
  1368. if 'username-mismatch' in restore_info['dom0']:
  1369. s += " <-- username in backup and dom0 mismatch"
  1370. print_callback(s)
  1371. def backup_restore_do(restore_info,
  1372. host_collection = None, print_callback = print_stdout,
  1373. error_callback = print_stderr, progress_callback = None,
  1374. ):
  1375. global running_backup_operation
  1376. ### Private functions begin
  1377. def restore_vm_dir_v1 (backup_dir, src_dir, dst_dir):
  1378. backup_src_dir = src_dir.replace (system_path["qubes_base_dir"], backup_dir)
  1379. # We prefer to use Linux's cp, because it nicely handles sparse files
  1380. retcode = subprocess.call (["cp", "-rp", backup_src_dir, dst_dir])
  1381. if retcode != 0:
  1382. raise QubesException(
  1383. "*** Error while copying file {0} to {1}".format(backup_src_dir,
  1384. dst_dir))
  1385. ### Private functions end
  1386. options = restore_info['$OPTIONS$']
  1387. backup_location = options['location']
  1388. restore_tmpdir = options['restore_tmpdir']
  1389. passphrase = options['passphrase']
  1390. encrypted = options['encrypted']
  1391. compressed = options['compressed']
  1392. hmac_algorithm = options['hmac_algorithm']
  1393. crypto_algorithm = options['crypto_algorithm']
  1394. appvm = options['appvm']
  1395. format_version = options['format_version']
  1396. if format_version is None:
  1397. format_version = backup_detect_format_version(backup_location)
  1398. lock_obtained = False
  1399. if host_collection is None:
  1400. host_collection = QubesVmCollection()
  1401. host_collection.lock_db_for_writing()
  1402. host_collection.load()
  1403. lock_obtained = True
  1404. # Perform VM restoration in backup order
  1405. vms_dirs = []
  1406. vms_size = 0
  1407. vms = {}
  1408. for vm_info in restore_info.values():
  1409. if 'vm' not in vm_info:
  1410. continue
  1411. if not vm_info['good-to-go']:
  1412. continue
  1413. vm = vm_info['vm']
  1414. if format_version == 2:
  1415. vms_size += vm.backup_size
  1416. vms_dirs.append(vm.backup_path)
  1417. vms[vm.name] = vm
  1418. running_backup_operation = BackupOperationInfo()
  1419. if format_version == 2:
  1420. if 'dom0' in restore_info.keys() and restore_info['dom0']['good-to-go']:
  1421. vms_dirs.append(os.path.dirname(restore_info['dom0']['subdir']))
  1422. vms_size += restore_info['dom0']['size']
  1423. restore_vm_dirs (backup_location,
  1424. restore_tmpdir,
  1425. passphrase=passphrase,
  1426. vms_dirs=vms_dirs,
  1427. vms=vms,
  1428. vms_size=vms_size,
  1429. hmac_algorithm=hmac_algorithm,
  1430. crypto_algorithm=crypto_algorithm,
  1431. print_callback=print_callback,
  1432. error_callback=error_callback,
  1433. progress_callback=progress_callback,
  1434. encrypted=encrypted,
  1435. compressed=compressed,
  1436. appvm=appvm)
  1437. # Add VM in right order
  1438. for (vm_class_name, vm_class) in sorted(QubesVmClasses.items(),
  1439. key=lambda _x: _x[1].load_order):
  1440. if running_backup_operation.canceled:
  1441. break
  1442. for vm in vms.values():
  1443. if running_backup_operation.canceled:
  1444. # only break the loop to save qubes.xml with already restored
  1445. # VMs
  1446. break
  1447. if not vm.__class__ == vm_class:
  1448. continue
  1449. print_callback("-> Restoring {type} {0}...".format(vm.name, type=vm_class_name))
  1450. retcode = subprocess.call (["mkdir", "-p", os.path.dirname(vm.dir_path)])
  1451. if retcode != 0:
  1452. error_callback("*** Cannot create directory: {0}?!".format(
  1453. vm.dir_path))
  1454. error_callback("Skipping...")
  1455. continue
  1456. template = None
  1457. if vm.template is not None:
  1458. template_name = restore_info[vm.name]['template']
  1459. template = host_collection.get_vm_by_name(template_name)
  1460. new_vm = None
  1461. try:
  1462. new_vm = host_collection.add_new_vm(vm_class_name, name=vm.name,
  1463. conf_file=vm.conf_file,
  1464. dir_path=vm.dir_path,
  1465. template=template,
  1466. installed_by_rpm=False)
  1467. if format_version == 1:
  1468. restore_vm_dir_v1(backup_location,
  1469. vm.dir_path,
  1470. os.path.dirname(new_vm.dir_path))
  1471. elif format_version == 2:
  1472. shutil.move(os.path.join(restore_tmpdir, vm.backup_path),
  1473. new_vm.dir_path)
  1474. new_vm.verify_files()
  1475. except Exception as err:
  1476. error_callback("ERROR: {0}".format(err))
  1477. error_callback("*** Skipping VM: {0}".format(vm.name))
  1478. if new_vm:
  1479. host_collection.pop(new_vm.qid)
  1480. continue
  1481. try:
  1482. new_vm.clone_attrs(vm)
  1483. except Exception as err:
  1484. error_callback("ERROR: {0}".format(err))
  1485. error_callback("*** Some VM property will not be restored")
  1486. try:
  1487. new_vm.appmenus_create(verbose=True)
  1488. except Exception as err:
  1489. error_callback("ERROR during appmenu restore: {0}".format(err))
  1490. error_callback("*** VM '{0}' will not have appmenus".format(vm.name))
  1491. # Set network dependencies - only non-default netvm setting
  1492. for vm in vms.values():
  1493. host_vm = host_collection.get_vm_by_name(vm.name)
  1494. if host_vm is None:
  1495. # Failed/skipped VM
  1496. continue
  1497. if not vm.uses_default_netvm:
  1498. if restore_info[vm.name]['netvm'] is not None:
  1499. host_vm.netvm = host_collection.get_vm_by_name (
  1500. restore_info[vm.name]['netvm'])
  1501. else:
  1502. host_vm.netvm = None
  1503. host_collection.save()
  1504. if lock_obtained:
  1505. host_collection.unlock_db()
  1506. if running_backup_operation.canceled:
  1507. if format_version == 2:
  1508. raise BackupCanceledError("Restore canceled",
  1509. tmpdir=restore_tmpdir)
  1510. else:
  1511. raise BackupCanceledError("Restore canceled")
  1512. # ... and dom0 home as last step
  1513. if 'dom0' in restore_info.keys() and restore_info['dom0']['good-to-go']:
  1514. backup_path = restore_info['dom0']['subdir']
  1515. local_user = grp.getgrnam('qubes').gr_mem[0]
  1516. home_dir = pwd.getpwnam(local_user).pw_dir
  1517. if format_version == 1:
  1518. backup_dom0_home_dir = os.path.join(backup_location, backup_path)
  1519. else:
  1520. backup_dom0_home_dir = os.path.join(restore_tmpdir, backup_path)
  1521. restore_home_backupdir = "home-pre-restore-{0}".format (time.strftime("%Y-%m-%d-%H%M%S"))
  1522. print_callback("-> Restoring home of user '{0}'...".format(local_user))
  1523. print_callback("--> Existing files/dirs backed up in '{0}' dir".format(restore_home_backupdir))
  1524. os.mkdir(home_dir + '/' + restore_home_backupdir)
  1525. for f in os.listdir(backup_dom0_home_dir):
  1526. home_file = home_dir + '/' + f
  1527. if os.path.exists(home_file):
  1528. os.rename(home_file, home_dir + '/' + restore_home_backupdir + '/' + f)
  1529. if format_version == 1:
  1530. retcode = subprocess.call (["cp", "-nrp", backup_dom0_home_dir + '/' + f, home_file])
  1531. elif format_version == 2:
  1532. shutil.move(backup_dom0_home_dir + '/' + f, home_file)
  1533. retcode = subprocess.call(['sudo', 'chown', '-R', local_user, home_dir])
  1534. if retcode != 0:
  1535. error_callback("*** Error while setting home directory owner")
  1536. shutil.rmtree(restore_tmpdir)
  1537. # vim:sw=4:et: