create_new_vm.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 VM based on a template"),
  73. self.tr("Standalone VM 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['virt_mode'] = 'hvm'
  102. properties['netvm'] = self.netvm_list[self.netvm.currentIndex()]
  103. thread_monitor = ThreadMonitor()
  104. thread = threading.Thread(target=self.do_create_vm,
  105. args=(self.app, vmclass, name, label, template, properties,
  106. thread_monitor))
  107. thread.daemon = True
  108. thread.start()
  109. progress = QtGui.QProgressDialog(
  110. self.tr("Creating new qube <b>{}</b>...").format(name), "", 0, 0)
  111. progress.setCancelButton(None)
  112. progress.setModal(True)
  113. progress.show()
  114. while not thread_monitor.is_finished():
  115. self.qtapp.processEvents()
  116. time.sleep(0.1)
  117. progress.hide()
  118. if not thread_monitor.success:
  119. QtGui.QMessageBox.warning(None,
  120. self.tr("Error creating the qube!"),
  121. self.tr("ERROR: {}").format(thread_monitor.error_msg))
  122. self.done(0)
  123. if thread_monitor.success:
  124. if self.launch_settings.isChecked():
  125. subprocess.check_call(['qubes-vm-settings', name])
  126. if self.install_system.isChecked():
  127. subprocess.check_call(
  128. ['qubes-vm-boot-from-device', name])
  129. @staticmethod
  130. def do_create_vm(app, vmclass, name, label, template, properties,
  131. thread_monitor):
  132. try:
  133. if vmclass == 'StandaloneVM' and template is not None:
  134. if template is qubesadmin.DEFAULT:
  135. src_vm = app.default_template
  136. else:
  137. src_vm = template
  138. vm = app.clone_vm(src_vm, name, vmclass)
  139. vm.label = label
  140. for k, v in properties.items():
  141. setattr(vm, k, v)
  142. else:
  143. vm = app.add_new_vm(vmclass,
  144. name=name, label=label, template=template)
  145. for k, v in properties.items():
  146. setattr(vm, k, v)
  147. except qubesadmin.exc.QubesException as qex:
  148. thread_monitor.set_error_msg(str(qex))
  149. except Exception as ex: # pylint: disable=broad-except
  150. thread_monitor.set_error_msg(repr(ex))
  151. thread_monitor.set_finished()
  152. def type_change(self):
  153. # AppVM
  154. if self.vm_type.currentIndex() == 0:
  155. self.template_vm.setEnabled(True)
  156. self.template_vm.setCurrentIndex(0)
  157. self.install_system.setEnabled(False)
  158. self.install_system.setChecked(False)
  159. # Standalone - based on a template
  160. if self.vm_type.currentIndex() == 1:
  161. self.template_vm.setEnabled(True)
  162. self.template_vm.setCurrentIndex(0)
  163. self.install_system.setEnabled(False)
  164. self.install_system.setChecked(False)
  165. # Standalone - not based on a template
  166. if self.vm_type.currentIndex() == 2:
  167. self.template_vm.setEnabled(False)
  168. self.template_vm.setCurrentIndex(-1)
  169. self.install_system.setEnabled(True)
  170. self.install_system.setChecked(True)
  171. def install_change(self):
  172. if self.install_system.isChecked():
  173. self.launch_settings.setChecked(False)
  174. def settings_change(self):
  175. if self.launch_settings.isChecked() and self.install_system.isEnabled():
  176. self.install_system.setChecked(False)
  177. parser = qubesadmin.tools.QubesArgumentParser()
  178. def main(args=None):
  179. args = parser.parse_args(args)
  180. qtapp = QtGui.QApplication(sys.argv)
  181. qtapp.setOrganizationName('Invisible Things Lab')
  182. qtapp.setOrganizationDomain('https://www.qubes-os.org/')
  183. qtapp.setApplicationName('Create qube')
  184. dialog = NewVmDlg(qtapp, args.app)
  185. dialog.exec_()