backup.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. #!/usr/bin/python2
  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(PyQt_PyObject)"), self.vms_removed)
  70. self.connect(self.select_vms_widget, SIGNAL("items_added(PyQt_PyObject)"), 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, vms) = self.get_running_vms()
  145. if len(vms) == 0:
  146. return;
  147. for vm in vms:
  148. self.blk_manager.check_if_serves_as_backend(vm)
  149. reply = QMessageBox.question(None, "VM Shutdown Confirmation",
  150. "Are you sure you want to power down the following VMs: <b>{0}</b>?<br>"
  151. "<small>This will shutdown all the running applications within them.</small>".format(', '.join(names)),
  152. QMessageBox.Yes | QMessageBox.Cancel)
  153. self.app.processEvents()
  154. if reply == QMessageBox.Yes:
  155. wait_time = 60.0
  156. for vm in vms:
  157. self.shutdown_vm_func(vm, wait_time*1000)
  158. progress = QProgressDialog ("Shutting down VMs <b>{0}</b>...".format(', '.join(names)), "", 0, 0)
  159. progress.setModal(True)
  160. progress.show()
  161. wait_for = wait_time
  162. while self.check_running() and wait_for > 0:
  163. self.app.processEvents()
  164. time.sleep (0.5)
  165. wait_for -= 0.5
  166. progress.hide()
  167. def get_running_vms(self):
  168. names = []
  169. vms = []
  170. for i in range(self.select_vms_widget.selected_list.count()):
  171. item = self.select_vms_widget.selected_list.item(i)
  172. if item.vm.is_running() and item.vm.qid != 0:
  173. names.append(item.vm.name)
  174. vms.append(item.vm)
  175. return (names, vms)
  176. def dev_combobox_activated(self, idx):
  177. dev_combobox_activated(self, idx)
  178. @pyqtSlot(name='on_select_path_button_clicked')
  179. def select_path_button_clicked(self):
  180. select_path_button_clicked(self)
  181. def validateCurrentPage(self):
  182. if self.currentPage() is self.select_vms_page:
  183. for i in range(self.select_vms_widget.selected_list.count()):
  184. if self.check_running() == True:
  185. 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.")
  186. return False
  187. del self.excluded[:]
  188. for i in range(self.select_vms_widget.available_list.count()):
  189. vmname = self.select_vms_widget.available_list.item(i).vm.name
  190. self.excluded.append(vmname)
  191. return True
  192. def gather_output(self, s):
  193. self.func_output.append(s)
  194. def update_progress_bar(self, value):
  195. if value == 100:
  196. self.emit(SIGNAL("backup_progress(int)"), value)
  197. def check_backup_progress(self, initial_usage, total_backup_size):
  198. du = qubesutils.get_disk_usage(self.backup_dir)
  199. done = du - initial_usage
  200. percent = int((float(done)/total_backup_size)*100)
  201. return percent
  202. def __do_backup__(self, thread_monitor):
  203. msg = []
  204. try:
  205. qubesutils.backup_do(str(self.backup_dir), self.files_to_backup, self.update_progress_bar)
  206. #simulate_long_lasting_proces(10, self.update_progress_bar)
  207. except Exception as ex:
  208. msg.append(str(ex))
  209. if len(msg) > 0 :
  210. thread_monitor.set_error_msg('\n'.join(msg))
  211. thread_monitor.set_finished()
  212. def current_page_changed(self, id):
  213. if self.currentPage() is self.confirm_page:
  214. del self.func_output[:]
  215. try:
  216. self.files_to_backup = qubesutils.backup_prepare(str(self.backup_dir), exclude_list = self.excluded, print_callback = self.gather_output)
  217. except Exception as ex:
  218. QMessageBox.critical(None, "Error while prepering backup.", "ERROR: {0}".format(ex))
  219. self.textEdit.setReadOnly(True)
  220. self.textEdit.setFontFamily("Monospace")
  221. self.textEdit.setText("\n".join(self.func_output))
  222. elif self.currentPage() is self.commit_page:
  223. self.button(self.FinishButton).setDisabled(True)
  224. self.button(self.CancelButton).setDisabled(True)
  225. self.thread_monitor = ThreadMonitor()
  226. initial_usage = qubesutils.get_disk_usage(self.backup_dir)
  227. thread = threading.Thread (target= self.__do_backup__ , args=(self.thread_monitor,))
  228. thread.daemon = True
  229. thread.start()
  230. self.button(self.CancelButton).setDisabled(False)
  231. counter = 0
  232. while not self.thread_monitor.is_finished():
  233. self.app.processEvents()
  234. time.sleep (0.1)
  235. counter += 1
  236. if counter == 20:
  237. progress = self.check_backup_progress(initial_usage, self.total_size)
  238. self.progress_bar.setValue(progress)
  239. counter = 0
  240. if not self.thread_monitor.success:
  241. QMessageBox.warning (None, "Backup error!", "ERROR: {1}".format(self.vm.name, self.thread_monitor.error_msg))
  242. if self.dev_mount_path != None:
  243. umount_device(self.dev_mount_path)
  244. self.button(self.FinishButton).setEnabled(True)
  245. def reject(self):
  246. #cancell clicked while the backup is in progress.
  247. #calling kill on cp.
  248. if self.currentPage() is self.commit_page:
  249. manager_pid = os.getpid()
  250. cp_pid_cmd = ["ps" ,"--ppid", str(manager_pid)]
  251. pid = None
  252. while not self.thread_monitor.is_finished():
  253. cp_pid = subprocess.Popen(cp_pid_cmd, stdout = subprocess.PIPE)
  254. output = cp_pid.stdout.read().split("\n")
  255. for l in output:
  256. if l.endswith("cp"):
  257. pid = l.split(" ")[1]
  258. break
  259. if pid != None:
  260. os.kill(int(pid), signal.SIGTERM)
  261. break
  262. if self.dev_mount_path != None:
  263. umount_device(self.dev_mount_path)
  264. self.done(0)
  265. def has_selected_vms(self):
  266. return self.select_vms_widget.selected_list.count() > 0
  267. def has_selected_dir(self):
  268. return self.backup_dir != None
  269. # Bases on the original code by:
  270. # Copyright (c) 2002-2007 Pascal Varet <p.varet@gmail.com>
  271. def handle_exception( exc_type, exc_value, exc_traceback ):
  272. import sys
  273. import os.path
  274. import traceback
  275. filename, line, dummy, dummy = traceback.extract_tb( exc_traceback ).pop()
  276. filename = os.path.basename( filename )
  277. error = "%s: %s" % ( exc_type.__name__, exc_value )
  278. QMessageBox.critical(None, "Houston, we have a problem...",
  279. "Whoops. A critical error has occured. This is most likely a bug "
  280. "in Qubes Restore VMs application.<br><br>"
  281. "<b><i>%s</i></b>" % error +
  282. "at <b>line %d</b> of file <b>%s</b>.<br/><br/>"
  283. % ( line, filename ))
  284. def main():
  285. global qubes_host
  286. qubes_host = QubesHost()
  287. global app
  288. app = QApplication(sys.argv)
  289. app.setOrganizationName("The Qubes Project")
  290. app.setOrganizationDomain("http://qubes-os.org")
  291. app.setApplicationName("Qubes Backup VMs")
  292. sys.excepthook = handle_exception
  293. qvm_collection = QubesVmCollection()
  294. qvm_collection.lock_db_for_reading()
  295. qvm_collection.load()
  296. qvm_collection.unlock_db()
  297. global backup_window
  298. backup_window = BackupVMsWindow()
  299. backup_window.show()
  300. app.exec_()
  301. app.exit()
  302. if __name__ == "__main__":
  303. main()