create_new_vm.py 8.4 KB

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