backup.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. #!/usr/bin/python2.6
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2012 Agnieszka Kostrzewa <agnieszka.kostrzewa@gmail.com>
  6. # Copyright (C) 2012 Marek Marczykowski <marmarek@mimuw.edu.pl>
  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, write to the Free Software
  20. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  21. #
  22. #
  23. import sys
  24. import os
  25. import signal
  26. from PyQt4.QtCore import *
  27. from PyQt4.QtGui import *
  28. from qubes.qubes import QubesVmCollection
  29. from qubes.qubes import QubesException
  30. from qubes.qubes import QubesDaemonPidfile
  31. from qubes.qubes import QubesHost
  32. from qubes import qubesutils
  33. import qubesmanager.resources_rc
  34. from pyinotify import WatchManager, Notifier, ThreadedNotifier, EventsCodes, ProcessEvent
  35. import subprocess
  36. import time
  37. from thread_monitor import *
  38. from operator import itemgetter
  39. from datetime import datetime
  40. from string import replace
  41. from ui_backupdlg import *
  42. from multiselectwidget import *
  43. from backup_utils import *
  44. import grp,pwd
  45. class BackupVMsWindow(Ui_Backup, QWizard):
  46. __pyqtSignals__ = ("backup_progress(int)",)
  47. def __init__(self, app, qvm_collection, blk_manager, shutdown_vm_func, parent=None):
  48. super(BackupVMsWindow, self).__init__(parent)
  49. self.app = app
  50. self.qvm_collection = qvm_collection
  51. self.blk_manager = blk_manager
  52. self.shutdown_vm_func = shutdown_vm_func
  53. self.dev_mount_path = None
  54. self.backup_dir = None
  55. self.func_output = []
  56. self.excluded = []
  57. for vm in self.qvm_collection.values():
  58. if vm.qid == 0:
  59. self.vm = vm
  60. break;
  61. assert self.vm != None
  62. self.setupUi(self)
  63. self.show_running_vms_warning(False)
  64. self.dir_line_edit.setReadOnly(True)
  65. self.select_vms_widget = MultiSelectWidget(self)
  66. self.verticalLayout.insertWidget(1, self.select_vms_widget)
  67. self.connect(self, SIGNAL("currentIdChanged(int)"), self.current_page_changed)
  68. self.connect(self.select_vms_widget, SIGNAL("selected_changed()"), self.check_running)
  69. self.connect(self.select_vms_widget, SIGNAL("items_removed(list)"), self.vms_removed)
  70. self.connect(self.select_vms_widget, SIGNAL("items_added(list)"), self.vms_added)
  71. self.refresh_button.clicked.connect(self.check_running)
  72. self.shutdown_running_vms_button.clicked.connect(self.shutdown_all_running_selected)
  73. self.connect(self.dev_combobox, SIGNAL("activated(int)"), self.dev_combobox_activated)
  74. self.connect(self, SIGNAL("backup_progress(int)"), self.progress_bar.setValue)
  75. self.select_vms_page.isComplete = self.has_selected_vms
  76. self.select_dir_page.isComplete = self.has_selected_dir
  77. #FIXME
  78. #this causes to run isComplete() twice, I don't know why
  79. self.select_vms_page.connect(self.select_vms_widget, SIGNAL("selected_changed()"), SIGNAL("completeChanged()"))
  80. self.total_size = 0
  81. self.__fill_vms_list__()
  82. fill_devs_list(self)
  83. def show_running_vms_warning(self, show):
  84. self.running_vms_warning.setVisible(show)
  85. self.shutdown_running_vms_button.setVisible(show)
  86. self.refresh_button.setVisible(show)
  87. class VmListItem(QListWidgetItem):
  88. def __init__(self, vm):
  89. self.vm = vm
  90. if vm.qid == 0:
  91. local_user = grp.getgrnam('qubes').gr_mem[0]
  92. home_dir = pwd.getpwnam(local_user).pw_dir
  93. self.size = qubesutils.get_disk_usage(home_dir)
  94. else:
  95. self.size = self.get_vm_size(vm)
  96. super(BackupVMsWindow.VmListItem, self).__init__(vm.name+ " (" + qubesutils.size_to_human(self.size) + ")")
  97. def get_vm_size(self, vm):
  98. size = 0
  99. if vm.private_img is not None:
  100. size += vm.get_disk_usage (vm.private_img)
  101. if vm.updateable:
  102. size += vm.get_disk_usage(vm.root_img)
  103. return size
  104. def __fill_vms_list__(self):
  105. for vm in self.qvm_collection.values():
  106. if vm.is_appvm() and vm.internal:
  107. continue
  108. if vm.is_template() and vm.installed_by_rpm:
  109. continue
  110. item = BackupVMsWindow.VmListItem(vm)
  111. if vm.include_in_backups == True:
  112. self.select_vms_widget.selected_list.addItem(item)
  113. self.total_size += item.size
  114. else:
  115. self.select_vms_widget.available_list.addItem(item)
  116. self.check_running()
  117. self.total_size_label.setText(qubesutils.size_to_human(self.total_size))
  118. def vms_added(self, items):
  119. for i in items:
  120. self.total_size += i.size
  121. self.total_size_label.setText(qubesutils.size_to_human(self.total_size))
  122. def vms_removed(self, items):
  123. for i in items:
  124. self.total_size -= i.size
  125. self.total_size_label.setText(qubesutils.size_to_human(self.total_size))
  126. def check_running(self):
  127. some_selected_vms_running = False
  128. for i in range(self.select_vms_widget.selected_list.count()):
  129. item = self.select_vms_widget.selected_list.item(i)
  130. if item.vm.is_running() and item.vm.qid != 0:
  131. item.setForeground(QBrush(QColor(255, 0, 0)))
  132. some_selected_vms_running = True
  133. else:
  134. item.setForeground(QBrush(QColor(0, 0, 0)))
  135. self.show_running_vms_warning(some_selected_vms_running)
  136. for i in range(self.select_vms_widget.available_list.count()):
  137. item = self.select_vms_widget.available_list.item(i)
  138. if item.vm.is_running() and item.vm.qid != 0:
  139. item.setForeground(QBrush(QColor(255, 0, 0)))
  140. else:
  141. item.setForeground(QBrush(QColor(0, 0, 0)))
  142. return some_selected_vms_running
  143. def shutdown_all_running_selected(self):
  144. names = []
  145. vms = []
  146. for i in range(self.select_vms_widget.selected_list.count()):
  147. item = self.select_vms_widget.selected_list.item(i)
  148. if item.vm.is_running() and item.vm.qid != 0:
  149. names.append(item.vm.name)
  150. vms.append(item.vm)
  151. if len(vms) == 0:
  152. return;
  153. reply = QMessageBox.question(None, "VM Shutdown Confirmation",
  154. "Are you sure you want to power down the following VMs: <b>{0}</b>?<br>"
  155. "<small>This will shutdown all the running applications within them.</small>".format(', '.join(names)),
  156. QMessageBox.Yes | QMessageBox.Cancel)
  157. self.app.processEvents()
  158. if reply == QMessageBox.Yes:
  159. for vm in vms:
  160. self.shutdown_vm_func(vm)
  161. progress = QProgressDialog ("Shutting down VMs <b>{0}</b>...".format(', '.join(names)), "", 0, 0)
  162. progress.setModal(True)
  163. progress.show()
  164. wait_time = 15.0
  165. wait_for = wait_time
  166. while self.check_running() and wait_for > 0:
  167. self.app.processEvents()
  168. time.sleep (0.1)
  169. wait_for -= 0.1
  170. progress.hide()
  171. if self.check_running():
  172. QMessageBox.information(None, "Strange", "Could not power down the VMs in {0} seconds...".format(wait_time))
  173. def dev_combobox_activated(self, idx):
  174. dev_combobox_activated(self, idx)
  175. @pyqtSlot(name='on_select_path_button_clicked')
  176. def select_path_button_clicked(self):
  177. select_path_button_clicked(self)
  178. def validateCurrentPage(self):
  179. if self.currentPage() is self.select_vms_page:
  180. for i in range(self.select_vms_widget.selected_list.count()):
  181. if self.check_running() == True:
  182. QMessageBox.information(None, "Wait!", "Some selected VMs are running. Running VMs can not be backuped. Please shut them down or remove them from the list.")
  183. return False
  184. del self.excluded[:]
  185. for i in range(self.select_vms_widget.available_list.count()):
  186. vmname = self.select_vms_widget.available_list.item(i).vm.name
  187. self.excluded.append(vmname)
  188. return True
  189. def gather_output(self, s):
  190. self.func_output.append(s)
  191. def update_progress_bar(self, value):
  192. if value == 100:
  193. self.emit(SIGNAL("backup_progress(int)"), value)
  194. def check_backup_progress(self, initial_usage, total_backup_size):
  195. du = qubesutils.get_disk_usage(self.backup_dir)
  196. done = du - initial_usage
  197. percent = int((float(done)/total_backup_size)*100)
  198. return percent
  199. def __do_backup__(self, thread_monitor):
  200. msg = []
  201. try:
  202. qubesutils.backup_do(str(self.backup_dir), self.files_to_backup, self.update_progress_bar)
  203. #simulate_long_lasting_proces(10, self.update_progress_bar)
  204. except Exception as ex:
  205. msg.append(str(ex))
  206. if len(msg) > 0 :
  207. thread_monitor.set_error_msg('\n'.join(msg))
  208. thread_monitor.set_finished()
  209. def current_page_changed(self, id):
  210. if self.currentPage() is self.confirm_page:
  211. del self.func_output[:]
  212. try:
  213. self.files_to_backup = qubesutils.backup_prepare(str(self.backup_dir), exclude_list = self.excluded, print_callback = self.gather_output)
  214. except Exception as ex:
  215. QMessageBox.critical(None, "Error while prepering backup.", "ERROR: {0}".format(ex))
  216. self.textEdit.setReadOnly(True)
  217. self.textEdit.setFontFamily("Monospace")
  218. self.textEdit.setText("\n".join(self.func_output))
  219. elif self.currentPage() is self.commit_page:
  220. self.button(self.FinishButton).setDisabled(True)
  221. self.button(self.CancelButton).setDisabled(True)
  222. self.thread_monitor = ThreadMonitor()
  223. initial_usage = qubesutils.get_disk_usage(self.backup_dir)
  224. thread = threading.Thread (target= self.__do_backup__ , args=(self.thread_monitor,))
  225. thread.daemon = True
  226. thread.start()
  227. self.button(self.CancelButton).setDisabled(False)
  228. counter = 0
  229. while not self.thread_monitor.is_finished():
  230. self.app.processEvents()
  231. time.sleep (0.1)
  232. counter += 1
  233. if counter == 20:
  234. progress = self.check_backup_progress(initial_usage, self.total_size)
  235. self.progress_bar.setValue(progress)
  236. counter = 0
  237. if not self.thread_monitor.success:
  238. QMessageBox.warning (None, "Backup error!", "ERROR: {1}".format(self.vm.name, self.thread_monitor.error_msg))
  239. if self.dev_mount_path != None:
  240. umount_device(self.dev_mount_path)
  241. self.button(self.FinishButton).setEnabled(True)
  242. def reject(self):
  243. #cancell clicked while the backup is in progress.
  244. #calling kill on cp.
  245. if self.currentPage() is self.commit_page:
  246. manager_pid = os.getpid()
  247. cp_pid_cmd = ["ps" ,"--ppid", str(manager_pid)]
  248. pid = None
  249. while not self.thread_monitor.is_finished():
  250. cp_pid = subprocess.Popen(cp_pid_cmd, stdout = subprocess.PIPE)
  251. output = cp_pid.stdout.read().split("\n")
  252. for l in output:
  253. if l.endswith("cp"):
  254. pid = l.split(" ")[1]
  255. break
  256. if pid != None:
  257. os.kill(int(pid), signal.SIGTERM)
  258. break
  259. if self.dev_mount_path != None:
  260. umount_device(self.dev_mount_path)
  261. self.done(0)
  262. def has_selected_vms(self):
  263. return self.select_vms_widget.selected_list.count() > 0
  264. def has_selected_dir(self):
  265. return self.backup_dir != None
  266. # Bases on the original code by:
  267. # Copyright (c) 2002-2007 Pascal Varet <p.varet@gmail.com>
  268. def handle_exception( exc_type, exc_value, exc_traceback ):
  269. import sys
  270. import os.path
  271. import traceback
  272. filename, line, dummy, dummy = traceback.extract_tb( exc_traceback ).pop()
  273. filename = os.path.basename( filename )
  274. error = "%s: %s" % ( exc_type.__name__, exc_value )
  275. QMessageBox.critical(None, "Houston, we have a problem...",
  276. "Whoops. A critical error has occured. This is most likely a bug "
  277. "in Qubes Restore VMs application.<br><br>"
  278. "<b><i>%s</i></b>" % error +
  279. "at <b>line %d</b> of file <b>%s</b>.<br/><br/>"
  280. % ( line, filename ))
  281. def main():
  282. global qubes_host
  283. qubes_host = QubesHost()
  284. global app
  285. app = QApplication(sys.argv)
  286. app.setOrganizationName("The Qubes Project")
  287. app.setOrganizationDomain("http://qubes-os.org")
  288. app.setApplicationName("Qubes Backup VMs")
  289. sys.excepthook = handle_exception
  290. qvm_collection = QubesVmCollection()
  291. qvm_collection.lock_db_for_reading()
  292. qvm_collection.load()
  293. qvm_collection.unlock_db()
  294. global backup_window
  295. backup_window = BackupVMsWindow()
  296. backup_window.show()
  297. app.exec_()
  298. app.exit()
  299. if __name__ == "__main__":
  300. main()