create_new_vm.py 9.6 KB

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