create_new_vm.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. #!/usr/bin/python3
  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 os
  24. import sys
  25. import subprocess
  26. from PyQt5 import QtCore, QtWidgets, QtGui # pylint: disable=import-error
  27. import qubesadmin
  28. import qubesadmin.tools
  29. import qubesadmin.exc
  30. from . import utils
  31. from . import bootfromdevice
  32. from .ui_newappvmdlg import Ui_NewVMDlg # pylint: disable=import-error
  33. # pylint: disable=too-few-public-methods
  34. class CreateVMThread(QtCore.QThread):
  35. def __init__(self, app, vmclass, name, label, template, properties,
  36. pool):
  37. QtCore.QThread.__init__(self)
  38. self.app = app
  39. self.vmclass = vmclass
  40. self.name = name
  41. self.label = label
  42. self.template = template
  43. self.properties = properties
  44. self.pool = pool
  45. self.msg = None
  46. def run(self):
  47. try:
  48. if self.vmclass == 'TemplateVM' and self.template is not None:
  49. args = {}
  50. if self.pool:
  51. args['pool'] = self.pool
  52. vm = self.app.clone_vm(self.template, self.name,
  53. self.vmclass, **args)
  54. vm.label = self.label
  55. elif self.vmclass == 'StandaloneVM' and self.template is not None:
  56. args = {
  57. 'ignore_volumes': ['private']
  58. }
  59. if self.pool:
  60. args['pool'] = self.pool
  61. vm = self.app.clone_vm(self.template, self.name,
  62. self.vmclass, **args)
  63. vm.label = self.label
  64. else:
  65. args = {
  66. "name": self.name,
  67. "label": self.label,
  68. "template": self.template
  69. }
  70. if self.pool:
  71. args['pool'] = self.pool
  72. vm = self.app.add_new_vm(self.vmclass, **args)
  73. for k, v in self.properties.items():
  74. setattr(vm, k, v)
  75. except qubesadmin.exc.QubesException as qex:
  76. self.msg = str(qex)
  77. except Exception as ex: # pylint: disable=broad-except
  78. self.msg = repr(ex)
  79. class NewVmDlg(QtWidgets.QDialog, Ui_NewVMDlg):
  80. def __init__(self, qtapp, app, parent=None):
  81. super().__init__(parent)
  82. self.setupUi(self)
  83. self.qtapp = qtapp
  84. self.app = app
  85. self.thread = None
  86. self.progress = None
  87. self.boot_dialog = None
  88. utils.initialize_widget_with_labels(
  89. widget=self.label,
  90. qubes_app=self.app)
  91. utils.initialize_widget_with_vms(
  92. widget=self.template_vm,
  93. qubes_app=self.app,
  94. filter_function=(lambda vm: not utils.is_internal(vm) and
  95. vm.klass == 'TemplateVM'),
  96. allow_none=True)
  97. default_template = self.app.default_template
  98. for i in range(self.template_vm.count()):
  99. if self.template_vm.itemData(i) == default_template:
  100. self.template_vm.setCurrentIndex(i)
  101. self.template_vm.setItemText(
  102. i, str(default_template) + " (default)")
  103. self.template_type = "template"
  104. utils.initialize_widget_with_default(
  105. widget=self.netvm,
  106. choices=[(vm.name, vm) for vm in self.app.domains
  107. if not utils.is_internal(vm) and
  108. getattr(vm, 'provides_network', False)],
  109. add_none=True,
  110. add_qubes_default=True,
  111. default_value=getattr(self.app, 'default_netvm', None))
  112. try:
  113. utils.initialize_widget_with_default(
  114. widget=self.storage_pool,
  115. choices=[(str(pool), pool) for pool in self.app.pools.values()],
  116. add_qubes_default=True,
  117. mark_existing_as_default=True,
  118. default_value=self.app.default_pool)
  119. except qubesadmin.exc.QubesDaemonAccessError:
  120. self.storage_pool.clear()
  121. self.storage_pool.addItem("(default)", qubesadmin.DEFAULT)
  122. self.name.setValidator(QtGui.QRegExpValidator(
  123. QtCore.QRegExp("[a-zA-Z0-9_-]*", QtCore.Qt.CaseInsensitive), None))
  124. self.name.selectAll()
  125. self.name.setFocus()
  126. if self.template_vm.count() < 1:
  127. QtWidgets.QMessageBox.warning(
  128. self,
  129. self.tr('No template available!'),
  130. self.tr('Cannot create a qube when no template exists.'))
  131. type_list = [
  132. (self.tr("AppVM (persistent home, volatile root)"), 'AppVM'),
  133. (self.tr("TemplateVM (template home, persistent root)"),
  134. 'TemplateVM'),
  135. (self.tr("StandaloneVM (fully persistent)"), 'StandaloneVM'),
  136. (self.tr("DisposableVM (fully volatile)"), 'DispVM')]
  137. utils.initialize_widget(widget=self.vm_type,
  138. choices=type_list,
  139. selected_value='AppVM',
  140. add_current_label=False)
  141. self.vm_type.currentIndexChanged.connect(self.type_change)
  142. self.template_vm.currentIndexChanged.connect(self.template_change)
  143. self.launch_settings.stateChanged.connect(self.settings_change)
  144. self.install_system.stateChanged.connect(self.install_change)
  145. def accept(self):
  146. vmclass = self.vm_type.currentData()
  147. name = str(self.name.text())
  148. if self.install_system.isChecked():
  149. self.boot_dialog = bootfromdevice.VMBootFromDeviceWindow(
  150. name, self.qtapp, self.app, self, True)
  151. if not self.boot_dialog.exec_():
  152. return
  153. if name in self.app.domains:
  154. QtWidgets.QMessageBox.warning(
  155. self,
  156. self.tr('Incorrect qube name!'),
  157. self.tr('A qube with the name <b>{}</b> already exists in the '
  158. 'system!').format(name))
  159. return
  160. label = self.label.currentData()
  161. template = self.template_vm.currentData()
  162. if vmclass in ['AppVM', 'DispVM'] and template is None:
  163. QtWidgets.QMessageBox.warning(
  164. self,
  165. self.tr('Unspecified template'),
  166. self.tr('{}s must be based on a template!'.format(vmclass)))
  167. return
  168. properties = {'provides_network': self.provides_network.isChecked()}
  169. if self.netvm.currentIndex() != 0:
  170. properties['netvm'] = self.netvm.currentData()
  171. # Standalone - not based on a template
  172. if vmclass == 'StandaloneVM' and template is None:
  173. properties['virt_mode'] = 'hvm'
  174. properties['kernel'] = None
  175. if self.storage_pool.currentData() is not qubesadmin.DEFAULT:
  176. pool = self.storage_pool.currentData()
  177. else:
  178. pool = None
  179. if self.init_ram.value() > 0:
  180. properties['memory'] = self.init_ram.value()
  181. self.thread = CreateVMThread(
  182. self.app, vmclass, name, label, template, properties, pool)
  183. self.thread.finished.connect(self.create_finished)
  184. self.thread.start()
  185. self.progress = QtWidgets.QProgressDialog(
  186. self.tr("Creating new qube <b>{0}</b>...").format(name), "", 0, 0)
  187. self.progress.setCancelButton(None)
  188. self.progress.setModal(True)
  189. self.progress.show()
  190. def create_finished(self):
  191. if self.thread.msg:
  192. QtWidgets.QMessageBox.warning(
  193. self,
  194. self.tr("Error creating the qube!"),
  195. self.tr("ERROR: {0}").format(self.thread.msg))
  196. else:
  197. if self.launch_settings.isChecked():
  198. subprocess.check_call(['qubes-vm-settings',
  199. str(self.name.text())])
  200. if self.install_system.isChecked():
  201. qubesadmin.tools.qvm_start.main(
  202. ['--cdrom', self.boot_dialog.cdrom_location,
  203. self.name.text()])
  204. self.progress.hide()
  205. self.done(0)
  206. def type_change(self):
  207. template = self.template_vm.currentData()
  208. klass = self.vm_type.currentData()
  209. if klass in ['TemplateVM', 'StandaloneVM'] and template is None:
  210. self.install_system.setEnabled(True)
  211. self.install_system.setChecked(True)
  212. else:
  213. self.install_system.setEnabled(False)
  214. self.install_system.setChecked(False)
  215. if klass == 'DispVM':
  216. self.template_vm.clear()
  217. for vm in self.app.domains:
  218. if utils.is_internal(vm):
  219. continue
  220. if vm.klass != 'AppVM':
  221. continue
  222. if getattr(vm, 'template_for_dispvms', True):
  223. self.template_vm.addItem(vm.name, userData=vm)
  224. self.template_vm.insertItem(self.template_vm.count(),
  225. utils.translate("(none)"), None)
  226. self.template_vm.setCurrentIndex(0)
  227. self.template_type = "dispvm"
  228. elif self.template_type == "dispvm":
  229. self.template_vm.clear()
  230. for vm in self.app.domains:
  231. if utils.is_internal(vm):
  232. continue
  233. if vm.klass == 'TemplateVM':
  234. self.template_vm.addItem(vm.name, userData=vm)
  235. self.template_vm.insertItem(self.template_vm.count(),
  236. utils.translate("(none)"), None)
  237. self.template_vm.setCurrentIndex(0)
  238. self.template_type = "template"
  239. def template_change(self):
  240. template = self.template_vm.currentData()
  241. klass = self.vm_type.currentData()
  242. if klass in ['TemplateVM', 'StandaloneVM'] and template is None:
  243. self.install_system.setEnabled(True)
  244. self.install_system.setChecked(True)
  245. else:
  246. self.install_system.setEnabled(False)
  247. self.install_system.setChecked(False)
  248. def install_change(self):
  249. if self.install_system.isChecked():
  250. self.launch_settings.setChecked(False)
  251. def settings_change(self):
  252. if self.launch_settings.isChecked() and self.install_system.isEnabled():
  253. self.install_system.setChecked(False)
  254. parser = qubesadmin.tools.QubesArgumentParser()
  255. def main(args=None):
  256. args = parser.parse_args(args)
  257. qtapp = QtWidgets.QApplication(sys.argv)
  258. translator = QtCore.QTranslator(qtapp)
  259. locale = QtCore.QLocale.system().name()
  260. i18n_dir = os.path.join(
  261. os.path.dirname(os.path.realpath(__file__)),
  262. 'i18n')
  263. translator.load("qubesmanager_{!s}.qm".format(locale), i18n_dir)
  264. qtapp.installTranslator(translator)
  265. QtCore.QCoreApplication.installTranslator(translator)
  266. qtapp.setOrganizationName('Invisible Things Lab')
  267. qtapp.setOrganizationDomain('https://www.qubes-os.org/')
  268. qtapp.setApplicationName(QtCore.QCoreApplication.translate(
  269. "appname", 'Create qube'))
  270. dialog = NewVmDlg(qtapp, args.app)
  271. dialog.exec_()