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