create_new_vm.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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=getattr(self.app, 'default_template', None))
  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
  94. getattr(vm, 'provides_network', False)],
  95. add_none=True,
  96. add_qubes_default=True,
  97. default_value=getattr(self.app, 'default_netvm', None))
  98. try:
  99. utils.initialize_widget_with_default(
  100. widget=self.storage_pool,
  101. choices=[(str(pool), pool) for pool in self.app.pools.values()],
  102. add_qubes_default=True,
  103. mark_existing_as_default=True,
  104. default_value=self.app.default_pool)
  105. except qubesadmin.exc.QubesDaemonAccessError:
  106. self.storage_pool.clear()
  107. self.storage_pool.addItem("(default)", qubesadmin.DEFAULT)
  108. self.name.setValidator(QtGui.QRegExpValidator(
  109. QtCore.QRegExp("[a-zA-Z0-9_-]*", QtCore.Qt.CaseInsensitive), None))
  110. self.name.selectAll()
  111. self.name.setFocus()
  112. if self.template_vm.count() < 1:
  113. QtWidgets.QMessageBox.warning(
  114. self,
  115. self.tr('No template available!'),
  116. self.tr('Cannot create a qube when no template exists.'))
  117. type_list = [
  118. (self.tr("Qube based on a template (AppVM)"), 'AppVM'),
  119. (self.tr("Standalone qube copied from a template"),
  120. 'StandaloneVM-copy'),
  121. (self.tr("Empty standalone qube (install your own OS)"),
  122. 'StandaloneVM-empty')]
  123. utils.initialize_widget(widget=self.vm_type,
  124. choices=type_list,
  125. selected_value='AppVM',
  126. add_current_label=False)
  127. self.vm_type.currentIndexChanged.connect(self.type_change)
  128. self.launch_settings.stateChanged.connect(self.settings_change)
  129. self.install_system.stateChanged.connect(self.install_change)
  130. def reject(self):
  131. self.done(0)
  132. def accept(self):
  133. selected_type = self.vm_type.currentData()
  134. vmclass = selected_type.split('-')[0]
  135. name = str(self.name.text())
  136. if name in self.app.domains:
  137. QtWidgets.QMessageBox.warning(
  138. self,
  139. self.tr('Incorrect qube name!'),
  140. self.tr('A qube with the name <b>{}</b> already exists in the '
  141. 'system!').format(name))
  142. return
  143. label = self.label.currentData()
  144. template = self.template_vm.currentData()
  145. properties = {'provides_network': self.provides_network.isChecked()}
  146. if self.netvm.currentIndex() != 0:
  147. properties['netvm'] = self.netvm.currentData()
  148. # Standalone - not based on a template
  149. if selected_type == 'StandaloneVM-empty':
  150. properties['virt_mode'] = 'hvm'
  151. properties['kernel'] = None
  152. if self.storage_pool.currentData() is not qubesadmin.DEFAULT:
  153. pool = self.storage_pool.currentData()
  154. else:
  155. pool = None
  156. if self.init_ram.value() > 0:
  157. properties['memory'] = self.init_ram.value()
  158. self.thread = CreateVMThread(
  159. self.app, vmclass, name, label, template, properties, pool)
  160. self.thread.finished.connect(self.create_finished)
  161. self.thread.start()
  162. self.progress = QtWidgets.QProgressDialog(
  163. self.tr("Creating new qube <b>{0}</b>...").format(name), "", 0, 0)
  164. self.progress.setCancelButton(None)
  165. self.progress.setModal(True)
  166. self.progress.show()
  167. def create_finished(self):
  168. self.progress.hide()
  169. if self.thread.msg:
  170. QtWidgets.QMessageBox.warning(
  171. self,
  172. self.tr("Error creating the qube!"),
  173. self.tr("ERROR: {0}").format(self.thread.msg))
  174. self.done(0)
  175. if not self.thread.msg:
  176. if self.launch_settings.isChecked():
  177. subprocess.check_call(['qubes-vm-settings',
  178. str(self.name.text())])
  179. if self.install_system.isChecked():
  180. subprocess.check_call(
  181. ['qubes-vm-boot-from-device', str(self.name.text())])
  182. def type_change(self):
  183. if self.vm_type.currentData() == 'AppVM':
  184. self.template_vm.setEnabled(True)
  185. if self.template_vm.currentIndex() == -1:
  186. self.template_vm.setCurrentIndex(0)
  187. self.install_system.setEnabled(False)
  188. self.install_system.setChecked(False)
  189. if self.vm_type.currentData() == 'StandaloneVM-copy':
  190. self.template_vm.setEnabled(True)
  191. if self.template_vm.currentIndex() == -1:
  192. self.template_vm.setCurrentIndex(0)
  193. self.install_system.setEnabled(False)
  194. self.install_system.setChecked(False)
  195. if self.vm_type.currentData() == 'StandaloneVM-empty':
  196. self.template_vm.setEnabled(False)
  197. self.template_vm.setCurrentIndex(-1)
  198. self.install_system.setEnabled(True)
  199. self.install_system.setChecked(True)
  200. def install_change(self):
  201. if self.install_system.isChecked():
  202. self.launch_settings.setChecked(False)
  203. def settings_change(self):
  204. if self.launch_settings.isChecked() and self.install_system.isEnabled():
  205. self.install_system.setChecked(False)
  206. parser = qubesadmin.tools.QubesArgumentParser()
  207. def main(args=None):
  208. args = parser.parse_args(args)
  209. qtapp = QtWidgets.QApplication(sys.argv)
  210. translator = QtCore.QTranslator(qtapp)
  211. locale = QtCore.QLocale.system().name()
  212. i18n_dir = os.path.join(
  213. os.path.dirname(os.path.realpath(__file__)),
  214. 'i18n')
  215. translator.load("qubesmanager_{!s}.qm".format(locale), i18n_dir)
  216. qtapp.installTranslator(translator)
  217. QtCore.QCoreApplication.installTranslator(translator)
  218. qtapp.setOrganizationName('Invisible Things Lab')
  219. qtapp.setOrganizationDomain('https://www.qubes-os.org/')
  220. qtapp.setApplicationName(QtCore.QCoreApplication.translate(
  221. "appname", 'Create qube'))
  222. dialog = NewVmDlg(qtapp, args.app)
  223. dialog.exec_()