create_new_vm.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 .ui_newappvmdlg import Ui_NewVMDlg # pylint: disable=import-error
  32. # pylint: disable=too-few-public-methods
  33. class CreateVMThread(QtCore.QThread):
  34. def __init__(self, app, vmclass, name, label, template, properties,
  35. pool):
  36. QtCore.QThread.__init__(self)
  37. self.app = app
  38. self.vmclass = vmclass
  39. self.name = name
  40. self.label = label
  41. self.template = template
  42. self.properties = properties
  43. self.pool = pool
  44. self.msg = None
  45. def run(self):
  46. try:
  47. if self.vmclass == 'StandaloneVM' and self.template is not None:
  48. args = {
  49. 'ignore_volumes': ['private']
  50. }
  51. if self.pool:
  52. args['pool'] = self.pool
  53. vm = self.app.clone_vm(self.template, self.name,
  54. self.vmclass, **args)
  55. vm.label = self.label
  56. for k, v in self.properties.items():
  57. setattr(vm, k, v)
  58. else:
  59. args = {
  60. "name": self.name,
  61. "label": self.label,
  62. "template": self.template
  63. }
  64. if self.pool:
  65. args['pool'] = self.pool
  66. vm = self.app.add_new_vm(self.vmclass, **args)
  67. for k, v in self.properties.items():
  68. setattr(vm, k, v)
  69. except qubesadmin.exc.QubesException as qex:
  70. self.msg = str(qex)
  71. except Exception as ex: # pylint: disable=broad-except
  72. self.msg = repr(ex)
  73. class NewVmDlg(QtWidgets.QDialog, Ui_NewVMDlg):
  74. def __init__(self, qtapp, app, parent=None):
  75. super(NewVmDlg, self).__init__(parent)
  76. self.setupUi(self)
  77. self.qtapp = qtapp
  78. self.app = app
  79. self.thread = None
  80. self.progress = None
  81. utils.initialize_widget_with_labels(
  82. widget=self.label,
  83. qubes_app=self.app)
  84. utils.initialize_widget_with_default(
  85. widget=self.template_vm,
  86. choices=[(vm.name, vm) for vm in self.app.domains
  87. if not utils.is_internal(vm) and vm.klass == 'TemplateVM'],
  88. mark_existing_as_default=True,
  89. default_value=self.app.default_template)
  90. utils.initialize_widget_with_default(
  91. widget=self.netvm,
  92. choices=[(vm.name, vm) for vm in self.app.domains
  93. if not utils.is_internal(vm) and vm.provides_network],
  94. add_none=True,
  95. add_qubes_default=True,
  96. default_value=self.app.default_netvm)
  97. utils.initialize_widget_with_default(
  98. widget=self.storage_pool,
  99. choices=[(str(pool), pool) for pool in self.app.pools.values()],
  100. add_qubes_default=True,
  101. mark_existing_as_default=True,
  102. default_value=self.app.default_pool)
  103. self.name.setValidator(QtGui.QRegExpValidator(
  104. QtCore.QRegExp("[a-zA-Z0-9_-]*", QtCore.Qt.CaseInsensitive), None))
  105. self.name.selectAll()
  106. self.name.setFocus()
  107. if self.template_vm.count() < 1:
  108. QtWidgets.QMessageBox.warning(
  109. self,
  110. self.tr('No template available!'),
  111. self.tr('Cannot create a qube when no template exists.'))
  112. # Order of types is important and used elsewhere; if it's changed
  113. # check for changes needed in self.type_change
  114. type_list = [
  115. (self.tr("Qube based on a template (AppVM)"), 'AppVM'),
  116. (self.tr("Standalone qube copied from a template"),
  117. 'StandaloneVM-copy'),
  118. (self.tr("Empty standalone qube (install your own OS)"),
  119. 'StandaloneVM-empty')]
  120. utils.initialize_widget(widget=self.vm_type,
  121. choices=type_list,
  122. selected_value='AppVM',
  123. add_current_label=False)
  124. self.vm_type.currentIndexChanged.connect(self.type_change)
  125. self.launch_settings.stateChanged.connect(self.settings_change)
  126. self.install_system.stateChanged.connect(self.install_change)
  127. def reject(self):
  128. self.done(0)
  129. def accept(self):
  130. selected_type = self.vm_type.currentData()
  131. vmclass = selected_type.split('-')[0]
  132. name = str(self.name.text())
  133. if name in self.app.domains:
  134. QtWidgets.QMessageBox.warning(
  135. self,
  136. self.tr('Incorrect qube name!'),
  137. self.tr('A qube with the name <b>{}</b> already exists in the '
  138. 'system!').format(name))
  139. return
  140. label = self.label.currentData()
  141. template = self.template_vm.currentData()
  142. properties = {'provides_network': self.provides_network.isChecked()}
  143. if self.netvm.currentIndex() != 0:
  144. properties['netvm'] = self.netvm.currentData()
  145. # Standalone - not based on a template
  146. if selected_type == 'StandaloneVM-empty':
  147. properties['virt_mode'] = 'hvm'
  148. properties['kernel'] = None
  149. if self.storage_pool.currentData() is not qubesadmin.DEFAULT:
  150. pool = self.storage_pool.currentData()
  151. else:
  152. pool = None
  153. if self.init_ram.value() > 0:
  154. properties['memory'] = self.init_ram.value()
  155. self.thread = CreateVMThread(
  156. self.app, vmclass, name, label, template, properties, pool)
  157. self.thread.finished.connect(self.create_finished)
  158. self.thread.start()
  159. self.progress = QtWidgets.QProgressDialog(
  160. self.tr("Creating new qube <b>{0}</b>...").format(name), "", 0, 0)
  161. self.progress.setCancelButton(None)
  162. self.progress.setModal(True)
  163. self.progress.show()
  164. def create_finished(self):
  165. self.progress.hide()
  166. if self.thread.msg:
  167. QtWidgets.QMessageBox.warning(
  168. self,
  169. self.tr("Error creating the qube!"),
  170. self.tr("ERROR: {0}").format(self.thread.msg))
  171. self.done(0)
  172. if not self.thread.msg:
  173. if self.launch_settings.isChecked():
  174. subprocess.check_call(['qubes-vm-settings',
  175. str(self.name.text())])
  176. if self.install_system.isChecked():
  177. subprocess.check_call(
  178. ['qubes-vm-boot-from-device', str(self.name.text())])
  179. def type_change(self):
  180. if self.vm_type.currentData() == 'AppVM':
  181. self.template_vm.setEnabled(True)
  182. if self.template_vm.currentIndex() == -1:
  183. self.template_vm.setCurrentIndex(0)
  184. self.install_system.setEnabled(False)
  185. self.install_system.setChecked(False)
  186. if self.vm_type.currentData() == 'StandaloneVM-copy':
  187. self.template_vm.setEnabled(True)
  188. if self.template_vm.currentIndex() == -1:
  189. self.template_vm.setCurrentIndex(0)
  190. self.install_system.setEnabled(False)
  191. self.install_system.setChecked(False)
  192. if self.vm_type.currentData() == 'StandaloneVM-empty':
  193. self.template_vm.setEnabled(False)
  194. self.template_vm.setCurrentIndex(-1)
  195. self.install_system.setEnabled(True)
  196. self.install_system.setChecked(True)
  197. def install_change(self):
  198. if self.install_system.isChecked():
  199. self.launch_settings.setChecked(False)
  200. def settings_change(self):
  201. if self.launch_settings.isChecked() and self.install_system.isEnabled():
  202. self.install_system.setChecked(False)
  203. parser = qubesadmin.tools.QubesArgumentParser()
  204. def main(args=None):
  205. args = parser.parse_args(args)
  206. qtapp = QtWidgets.QApplication(sys.argv)
  207. translator = QtCore.QTranslator(qtapp)
  208. locale = QtCore.QLocale.system().name()
  209. i18n_dir = os.path.join(
  210. os.path.dirname(os.path.realpath(__file__)),
  211. 'i18n')
  212. translator.load("qubesmanager_{!s}.qm".format(locale), i18n_dir)
  213. qtapp.installTranslator(translator)
  214. QtCore.QCoreApplication.installTranslator(translator)
  215. qtapp.setOrganizationName('Invisible Things Lab')
  216. qtapp.setOrganizationDomain('https://www.qubes-os.org/')
  217. qtapp.setApplicationName(QtCore.QCoreApplication.translate(
  218. "appname", 'Create qube'))
  219. dialog = NewVmDlg(qtapp, args.app)
  220. dialog.exec_()