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