create_new_vm.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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
  102. type_list = [self.tr("Qube based on a template (AppVM)"),
  103. self.tr("Standalone qube copied from a template"),
  104. self.tr("Empty standalone qube (install your own OS)")]
  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. if name in self.app.domains:
  116. QtGui.QMessageBox.warning(
  117. None,
  118. self.tr('Incorrect qube name!'),
  119. self.tr('A qube with the name <b>{}</b> already exists in the '
  120. 'system!').format(name))
  121. return
  122. label = self.label_list[self.label.currentIndex()]
  123. if self.template_vm.currentIndex() == -1:
  124. template = None
  125. else:
  126. template = self.template_list[self.template_vm.currentIndex()]
  127. properties = {}
  128. properties['provides_network'] = self.provides_network.isChecked()
  129. if self.netvm.currentIndex() != 0:
  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.msg:
  146. QtGui.QMessageBox.warning(None,
  147. self.tr("Error creating the qube!"),
  148. self.tr("ERROR: {}").format(self.thread.msg))
  149. self.done(0)
  150. if not self.thread.msg:
  151. if self.launch_settings.isChecked():
  152. subprocess.check_call(['qubes-vm-settings',
  153. str(self.name.text())])
  154. if self.install_system.isChecked():
  155. subprocess.check_call(
  156. ['qubes-vm-boot-from-device', str(self.name.text())])
  157. def type_change(self):
  158. # AppVM
  159. if self.vm_type.currentIndex() == 0:
  160. self.template_vm.setEnabled(True)
  161. self.template_vm.setCurrentIndex(0)
  162. self.install_system.setEnabled(False)
  163. self.install_system.setChecked(False)
  164. # Standalone - based on a template
  165. if self.vm_type.currentIndex() == 1:
  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 - not based on a template
  171. if self.vm_type.currentIndex() == 2:
  172. self.template_vm.setEnabled(False)
  173. self.template_vm.setCurrentIndex(-1)
  174. self.install_system.setEnabled(True)
  175. self.install_system.setChecked(True)
  176. def install_change(self):
  177. if self.install_system.isChecked():
  178. self.launch_settings.setChecked(False)
  179. def settings_change(self):
  180. if self.launch_settings.isChecked() and self.install_system.isEnabled():
  181. self.install_system.setChecked(False)
  182. parser = qubesadmin.tools.QubesArgumentParser()
  183. def main(args=None):
  184. args = parser.parse_args(args)
  185. qtapp = QtGui.QApplication(sys.argv)
  186. qtapp.setOrganizationName('Invisible Things Lab')
  187. qtapp.setOrganizationDomain('https://www.qubes-os.org/')
  188. qtapp.setApplicationName('Create qube')
  189. dialog = NewVmDlg(qtapp, args.app)
  190. dialog.exec_()