create_new_vm.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. from PyQt4.QtCore import *
  26. from PyQt4.QtGui import *
  27. from qubes.qubes import QubesVmCollection
  28. from qubes.qubes import QubesVmLabels
  29. from qubes.qubes import QubesException
  30. from qubes.qubes import QubesVm,QubesHVm
  31. import qubesmanager.resources_rc
  32. import time
  33. import threading
  34. from ui_newappvmdlg import *
  35. from thread_monitor import *
  36. class NewVmDlg (QDialog, Ui_NewVMDlg):
  37. def __init__(self, app, qvm_collection, trayIcon, parent = None):
  38. super (NewVmDlg, self).__init__(parent)
  39. self.setupUi(self)
  40. self.app = app
  41. self.trayIcon = trayIcon
  42. self.qvm_collection = qvm_collection
  43. # Theoretically we should be locking for writing here and unlock
  44. # only after the VM creation finished. But the code would be more messy...
  45. # Instead we lock for writing in the actual worker thread
  46. try:
  47. from qubes.qubes import QubesHVm
  48. except ImportError:
  49. pass
  50. else:
  51. self.hvm_radio.setEnabled(True)
  52. self.hvmtpl_radio.setEnabled(True)
  53. self.qvm_collection.lock_db_for_reading()
  54. self.qvm_collection.load()
  55. self.qvm_collection.unlock_db()
  56. self.label_list = QubesVmLabels.values()
  57. self.label_list.sort(key=lambda l: l.index)
  58. for (i, label) in enumerate(self.label_list):
  59. self.vmlabel.insertItem(i, label.name)
  60. self.vmlabel.setItemIcon (i, QIcon(label.icon_path))
  61. self.fill_template_list()
  62. self.vmname.setValidator(QRegExpValidator(QRegExp("[a-zA-Z0-9-]*", Qt.CaseInsensitive), None))
  63. self.vmname.selectAll()
  64. self.vmname.setFocus()
  65. self.hvmtemplatewarningbox.hide()
  66. def fill_template_list(self):
  67. def filter_template(vm):
  68. if vm.internal:
  69. return False
  70. if not vm.is_template():
  71. return False
  72. if self.hvm_radio.isChecked():
  73. return QubesHVm.is_template_compatible(vm)
  74. elif self.hvmtpl_radio.isChecked():
  75. return False
  76. else:
  77. return QubesVm.is_template_compatible(vm)
  78. self.template_vm_list = filter(filter_template, self.qvm_collection.values())
  79. self.template_name.clear()
  80. default_index = 0
  81. for (i, vm) in enumerate(self.template_vm_list):
  82. if vm is self.qvm_collection.get_default_template():
  83. default_index = i
  84. self.template_name.insertItem(i, vm.name + " (default)")
  85. else:
  86. self.template_name.insertItem(i, vm.name)
  87. self.template_name.setCurrentIndex(default_index)
  88. def on_appvm_radio_toggled(self, checked):
  89. if checked:
  90. self.template_name.setEnabled(True)
  91. self.allow_networking.setEnabled(True)
  92. def on_netvm_radio_toggled(self, checked):
  93. if checked:
  94. self.template_name.setEnabled(True)
  95. self.allow_networking.setEnabled(False)
  96. def on_proxyvm_radio_toggled(self, checked):
  97. if checked:
  98. self.template_name.setEnabled(True)
  99. self.allow_networking.setEnabled(True)
  100. def on_hvm_radio_toggled(self, checked):
  101. if self.hvm_radio.isChecked() or self.hvmtpl_radio.isChecked():
  102. self.standalone.setChecked(True)
  103. self.allow_networking.setEnabled(True)
  104. self.standalone.setEnabled(self.hvm_radio.isChecked())
  105. else:
  106. self.standalone.setChecked(False)
  107. self.standalone.setEnabled(True)
  108. self.fill_template_list()
  109. def on_hvmtpl_radio_toggled(self, checked):
  110. return self.on_hvm_radio_toggled(checked)
  111. def on_standalone_toggled(self, checked):
  112. if checked and (self.hvm_radio.isChecked() or
  113. self.hvmtpl_radio.isChecked()):
  114. self.template_name.setEnabled(False)
  115. else:
  116. self.template_name.setEnabled(True)
  117. if not checked and self.hvm_radio.isChecked():
  118. self.hvmtemplatewarningbox.show()
  119. else:
  120. self.hvmtemplatewarningbox.hide()
  121. def reject(self):
  122. self.done(0)
  123. def accept(self):
  124. vmname = str(self.vmname.text())
  125. if self.qvm_collection.get_vm_by_name(vmname) is not None:
  126. QMessageBox.warning (None, "Incorrect AppVM Name!", "A VM with the name <b>{0}</b> already exists in the system!".format(vmname))
  127. return
  128. label = self.label_list[self.vmlabel.currentIndex()]
  129. template_vm = None
  130. if self.template_name.isEnabled():
  131. template_vm = self.template_vm_list[self.template_name.currentIndex()]
  132. standalone = self.standalone.isChecked()
  133. allow_networking = None
  134. if self.allow_networking.isEnabled():
  135. allow_networking = self.allow_networking.isChecked()
  136. if self.appvm_radio.isChecked():
  137. vmtype = "AppVM"
  138. elif self.netvm_radio.isChecked():
  139. vmtype = "NetVM"
  140. elif self.proxyvm_radio.isChecked():
  141. vmtype = "ProxyVM"
  142. elif self.hvm_radio.isChecked():
  143. vmtype = "HVM"
  144. elif self.hvmtpl_radio.isChecked():
  145. vmtype = "TemplateHVM"
  146. else:
  147. QErrorMessage.showMessage(None, "Error creating AppVM!", "Unknown "
  148. "VM type, this is error in Qubes Manager")
  149. self.done(0)
  150. vmclass = "Qubes" + vmtype.replace("VM", "Vm")
  151. thread_monitor = ThreadMonitor()
  152. thread = threading.Thread (target=self.do_create_vm, args=(vmclass, vmname, label, template_vm, standalone, allow_networking, thread_monitor))
  153. thread.daemon = True
  154. thread.start()
  155. progress = QProgressDialog ("Creating new {0} <b>{1}</b>...".format(vmtype, vmname), "", 0, 0)
  156. progress.setCancelButton(None)
  157. progress.setModal(True)
  158. progress.show()
  159. while not thread_monitor.is_finished():
  160. self.app.processEvents()
  161. time.sleep (0.1)
  162. progress.hide()
  163. if thread_monitor.success:
  164. self.trayIcon.showMessage ("VM '{0}' has been created.".format(vmname), msecs=3000)
  165. else:
  166. QMessageBox.warning (None, "Error creating AppVM!", "ERROR: {0}".format(thread_monitor.error_msg))
  167. self.done(0)
  168. def do_create_vm (self, vmclass, vmname, label, template_vm, standalone, allow_networking, thread_monitor):
  169. vm = None
  170. try:
  171. self.qvm_collection.lock_db_for_writing()
  172. self.qvm_collection.load()
  173. if not standalone:
  174. vm = self.qvm_collection.add_new_vm(vmclass, name=vmname, template=template_vm, label=label)
  175. else:
  176. vm = self.qvm_collection.add_new_vm(vmclass, name=vmname, template=None, label=label)
  177. vm.create_on_disk(verbose=False, source_template = template_vm)
  178. if allow_networking == False:
  179. vm.uses_default_netvm = False
  180. vm.netvm = None
  181. self.qvm_collection.save()
  182. except Exception as ex:
  183. thread_monitor.set_error_msg (str(ex))
  184. if vm:
  185. vm.remove_from_disk()
  186. self.qvm_collection.pop(vm.qid)
  187. finally:
  188. self.qvm_collection.unlock_db()
  189. thread_monitor.set_finished()