create_new_vm.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. # Copyright (C) 2017 Wojtek Porczyk <woju@invisiblethingslab.com>
  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 Lesser General Public License along
  20. # with this program; if not, see <http://www.gnu.org/licenses/>.
  21. #
  22. #
  23. import sys
  24. import threading
  25. import time
  26. import subprocess
  27. from PyQt4 import QtCore, QtGui # pylint: disable=import-error
  28. import qubesadmin
  29. import qubesadmin.tools
  30. import qubesadmin.exc
  31. from . import utils
  32. from .ui_newappvmdlg import Ui_NewVMDlg # pylint: disable=import-error
  33. from .thread_monitor import ThreadMonitor
  34. class NewVmDlg(QtGui.QDialog, Ui_NewVMDlg):
  35. def __init__(self, qtapp, app, parent=None):
  36. super(NewVmDlg, self).__init__(parent)
  37. self.setupUi(self)
  38. self.qtapp = qtapp
  39. self.app = app
  40. # Theoretically we should be locking for writing here and unlock
  41. # only after the VM creation finished. But the code would be
  42. # more messy...
  43. # Instead we lock for writing in the actual worker thread
  44. self.label_list, self.label_idx = utils.prepare_label_choice(
  45. self.label,
  46. self.app, None,
  47. None,
  48. allow_default=False)
  49. self.template_list, self.template_idx = utils.prepare_vm_choice(
  50. self.template_vm,
  51. self.app, None,
  52. self.app.default_template,
  53. (lambda vm: vm.klass == 'TemplateVM'),
  54. allow_internal=False, allow_default=True, allow_none=False)
  55. self.netvm_list, self.netvm_idx = utils.prepare_vm_choice(
  56. self.netvm,
  57. self.app, None,
  58. self.app.default_netvm,
  59. (lambda vm: vm.provides_network),
  60. allow_internal=False, allow_default=True, allow_none=True)
  61. self.name.setValidator(QtGui.QRegExpValidator(
  62. QtCore.QRegExp("[a-zA-Z0-9-]*", QtCore.Qt.CaseInsensitive), None))
  63. self.name.selectAll()
  64. self.name.setFocus()
  65. if not self.template_list:
  66. QtGui.QMessageBox.warning(None,
  67. self.tr('No template available!'),
  68. self.tr('Cannot create a qube when no template exists.'))
  69. # Order of types is important and used elsewhere; if it's changed
  70. # check for changes needed in self.type_change and TODO
  71. type_list = [self.tr("AppVM"),
  72. self.tr("Standalone qube based on a template"),
  73. self.tr("Standalone qube not based on a template")]
  74. self.vm_type.addItems(type_list)
  75. self.vm_type.currentIndexChanged.connect(self.type_change)
  76. self.launch_settings.stateChanged.connect(self.settings_change)
  77. self.install_system.stateChanged.connect(self.install_change)
  78. def reject(self):
  79. self.done(0)
  80. def accept(self):
  81. vmclass = ('AppVM' if self.vm_type.currentIndex() == 0
  82. else 'StandaloneVM')
  83. name = str(self.name.text())
  84. try:
  85. self.app.domains[name]
  86. except LookupError:
  87. pass
  88. else:
  89. QtGui.QMessageBox.warning(None,
  90. self.tr('Incorrect qube name!'),
  91. self.tr('A qube with the name <b>{}</b> already exists in the '
  92. 'system!').format(name))
  93. return
  94. label = self.label_list[self.label.currentIndex()]
  95. if self.template_vm.currentIndex() == -1:
  96. template = None
  97. else:
  98. template = self.template_list[self.template_vm.currentIndex()]
  99. properties = {}
  100. properties['provides_network'] = self.provides_network.isChecked()
  101. properties['netvm'] = self.netvm_list[self.netvm.currentIndex()]
  102. if self.install_system.isChecked():
  103. properties['virt_mode'] = 'hvm'
  104. properties['kernel'] = None
  105. thread_monitor = ThreadMonitor()
  106. thread = threading.Thread(target=self.do_create_vm,
  107. args=(self.app, vmclass, name, label, template, properties,
  108. thread_monitor))
  109. thread.daemon = True
  110. thread.start()
  111. progress = QtGui.QProgressDialog(
  112. self.tr("Creating new qube <b>{}</b>...").format(name), "", 0, 0)
  113. progress.setCancelButton(None)
  114. progress.setModal(True)
  115. progress.show()
  116. while not thread_monitor.is_finished():
  117. self.qtapp.processEvents()
  118. time.sleep(0.1)
  119. progress.hide()
  120. if not thread_monitor.success:
  121. QtGui.QMessageBox.warning(None,
  122. self.tr("Error creating the qube!"),
  123. self.tr("ERROR: {}").format(thread_monitor.error_msg))
  124. self.done(0)
  125. if thread_monitor.success:
  126. if self.launch_settings.isChecked():
  127. subprocess.check_call(['qubes-vm-settings', name])
  128. if self.install_system.isChecked():
  129. subprocess.check_call(
  130. ['qubes-vm-boot-from-device', name])
  131. @staticmethod
  132. def do_create_vm(app, vmclass, name, label, template, properties,
  133. thread_monitor):
  134. try:
  135. if vmclass == 'StandaloneVM' and template is not None:
  136. if template is qubesadmin.DEFAULT:
  137. src_vm = app.default_template
  138. else:
  139. src_vm = template
  140. vm = app.clone_vm(src_vm, name, vmclass)
  141. vm.label = label
  142. for k, v in properties.items():
  143. setattr(vm, k, v)
  144. else:
  145. vm = app.add_new_vm(vmclass,
  146. name=name, label=label, template=template)
  147. for k, v in properties.items():
  148. setattr(vm, k, v)
  149. except qubesadmin.exc.QubesException as qex:
  150. thread_monitor.set_error_msg(str(qex))
  151. except Exception as ex: # pylint: disable=broad-except
  152. thread_monitor.set_error_msg(repr(ex))
  153. thread_monitor.set_finished()
  154. def type_change(self):
  155. # AppVM
  156. if self.vm_type.currentIndex() == 0:
  157. self.template_vm.setEnabled(True)
  158. self.template_vm.setCurrentIndex(0)
  159. self.install_system.setEnabled(False)
  160. self.install_system.setChecked(False)
  161. # Standalone - based on a template
  162. if self.vm_type.currentIndex() == 1:
  163. self.template_vm.setEnabled(True)
  164. self.template_vm.setCurrentIndex(0)
  165. self.install_system.setEnabled(False)
  166. self.install_system.setChecked(False)
  167. # Standalone - not based on a template
  168. if self.vm_type.currentIndex() == 2:
  169. self.template_vm.setEnabled(False)
  170. self.template_vm.setCurrentIndex(-1)
  171. self.install_system.setEnabled(True)
  172. self.install_system.setChecked(True)
  173. def install_change(self):
  174. if self.install_system.isChecked():
  175. self.launch_settings.setChecked(False)
  176. def settings_change(self):
  177. if self.launch_settings.isChecked() and self.install_system.isEnabled():
  178. self.install_system.setChecked(False)
  179. parser = qubesadmin.tools.QubesArgumentParser()
  180. def main(args=None):
  181. args = parser.parse_args(args)
  182. qtapp = QtGui.QApplication(sys.argv)
  183. qtapp.setOrganizationName('Invisible Things Lab')
  184. qtapp.setOrganizationDomain('https://www.qubes-os.org/')
  185. qtapp.setApplicationName('Create qube')
  186. dialog = NewVmDlg(qtapp, args.app)
  187. dialog.exec_()