create_new_vm.py 8.1 KB

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