2019-09-06 15:59:13 +02:00
|
|
|
#!/usr/bin/python3
|
2012-05-11 22:52:27 +02:00
|
|
|
#
|
|
|
|
# The Qubes OS Project, http://www.qubes-os.org
|
|
|
|
#
|
|
|
|
# Copyright (C) 2012 Agnieszka Kostrzewa <agnieszka.kostrzewa@gmail.com>
|
|
|
|
# Copyright (C) 2012 Marek Marczykowski <marmarek@mimuw.edu.pl>
|
2017-07-12 14:10:15 +02:00
|
|
|
# Copyright (C) 2017 Wojtek Porczyk <woju@invisiblethingslab.com>
|
2012-05-11 22:52:27 +02:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
# of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
2017-11-06 21:06:30 +01:00
|
|
|
# You should have received a copy of the GNU Lesser General Public License along
|
|
|
|
# with this program; if not, see <http://www.gnu.org/licenses/>.
|
2012-05-11 22:52:27 +02:00
|
|
|
#
|
|
|
|
#
|
|
|
|
|
2019-10-23 18:18:39 +02:00
|
|
|
import os
|
2017-07-12 14:10:15 +02:00
|
|
|
import sys
|
2017-09-13 20:17:24 +02:00
|
|
|
import subprocess
|
2017-07-12 14:10:15 +02:00
|
|
|
|
2019-05-22 23:10:09 +02:00
|
|
|
from PyQt5 import QtCore, QtWidgets, QtGui # pylint: disable=import-error
|
2012-05-11 22:52:27 +02:00
|
|
|
|
2017-07-12 14:10:15 +02:00
|
|
|
import qubesadmin
|
|
|
|
import qubesadmin.tools
|
2017-11-08 15:40:35 +01:00
|
|
|
import qubesadmin.exc
|
2012-05-11 22:52:27 +02:00
|
|
|
|
2017-07-12 14:10:15 +02:00
|
|
|
from . import utils
|
2021-03-14 13:23:43 +01:00
|
|
|
from . import bootfromdevice
|
2012-05-11 22:52:27 +02:00
|
|
|
|
2017-11-14 15:29:57 +01:00
|
|
|
from .ui_newappvmdlg import Ui_NewVMDlg # pylint: disable=import-error
|
2018-10-20 18:34:15 +02:00
|
|
|
|
2019-05-22 23:10:09 +02:00
|
|
|
|
2018-10-22 10:14:26 +02:00
|
|
|
# pylint: disable=too-few-public-methods
|
2018-10-20 18:34:15 +02:00
|
|
|
class CreateVMThread(QtCore.QThread):
|
2019-09-06 15:59:13 +02:00
|
|
|
def __init__(self, app, vmclass, name, label, template, properties,
|
|
|
|
pool):
|
2018-10-20 18:34:15 +02:00
|
|
|
QtCore.QThread.__init__(self)
|
|
|
|
self.app = app
|
|
|
|
self.vmclass = vmclass
|
|
|
|
self.name = name
|
|
|
|
self.label = label
|
|
|
|
self.template = template
|
|
|
|
self.properties = properties
|
2019-09-06 15:59:13 +02:00
|
|
|
self.pool = pool
|
2018-10-22 20:00:37 +02:00
|
|
|
self.msg = None
|
2018-10-20 18:34:15 +02:00
|
|
|
|
|
|
|
def run(self):
|
|
|
|
try:
|
2020-12-09 22:55:22 +01:00
|
|
|
if self.vmclass == 'TemplateVM' and self.template is not None:
|
|
|
|
args = {}
|
|
|
|
if self.pool:
|
|
|
|
args['pool'] = self.pool
|
|
|
|
|
|
|
|
vm = self.app.clone_vm(self.template, self.name,
|
|
|
|
self.vmclass, **args)
|
2020-12-13 18:23:39 +01:00
|
|
|
|
2020-12-09 22:55:22 +01:00
|
|
|
vm.label = self.label
|
|
|
|
elif self.vmclass == 'StandaloneVM' and self.template is not None:
|
2019-09-06 15:59:13 +02:00
|
|
|
args = {
|
|
|
|
'ignore_volumes': ['private']
|
|
|
|
}
|
|
|
|
if self.pool:
|
|
|
|
args['pool'] = self.pool
|
|
|
|
|
2020-07-10 14:08:22 +02:00
|
|
|
vm = self.app.clone_vm(self.template, self.name,
|
|
|
|
self.vmclass, **args)
|
2019-09-06 15:59:13 +02:00
|
|
|
|
2018-10-20 18:34:15 +02:00
|
|
|
vm.label = self.label
|
|
|
|
else:
|
2019-09-06 15:59:13 +02:00
|
|
|
args = {
|
|
|
|
"name": self.name,
|
|
|
|
"label": self.label,
|
|
|
|
"template": self.template
|
|
|
|
}
|
|
|
|
if self.pool:
|
|
|
|
args['pool'] = self.pool
|
|
|
|
|
|
|
|
vm = self.app.add_new_vm(self.vmclass, **args)
|
|
|
|
|
2020-12-09 22:55:22 +01:00
|
|
|
for k, v in self.properties.items():
|
|
|
|
setattr(vm, k, v)
|
2018-10-20 18:34:15 +02:00
|
|
|
|
|
|
|
except qubesadmin.exc.QubesException as qex:
|
2018-10-22 20:00:37 +02:00
|
|
|
self.msg = str(qex)
|
2018-10-20 18:34:15 +02:00
|
|
|
except Exception as ex: # pylint: disable=broad-except
|
2018-10-22 20:00:37 +02:00
|
|
|
self.msg = repr(ex)
|
2012-05-11 22:52:27 +02:00
|
|
|
|
|
|
|
|
2019-05-22 23:10:09 +02:00
|
|
|
class NewVmDlg(QtWidgets.QDialog, Ui_NewVMDlg):
|
2017-11-06 23:54:33 +01:00
|
|
|
def __init__(self, qtapp, app, parent=None):
|
2020-08-23 02:53:36 +02:00
|
|
|
super().__init__(parent)
|
2012-05-11 22:52:27 +02:00
|
|
|
self.setupUi(self)
|
|
|
|
|
2017-07-12 14:10:15 +02:00
|
|
|
self.qtapp = qtapp
|
2012-05-11 22:52:27 +02:00
|
|
|
self.app = app
|
|
|
|
|
2018-10-20 23:01:35 +02:00
|
|
|
self.thread = None
|
|
|
|
self.progress = None
|
2021-03-14 13:23:43 +01:00
|
|
|
self.boot_dialog = None
|
2018-10-20 23:01:35 +02:00
|
|
|
|
2020-07-09 21:38:23 +02:00
|
|
|
utils.initialize_widget_with_labels(
|
|
|
|
widget=self.label,
|
|
|
|
qubes_app=self.app)
|
|
|
|
|
2020-08-20 00:16:01 +02:00
|
|
|
utils.initialize_widget_with_vms(
|
2020-07-09 21:38:23 +02:00
|
|
|
widget=self.template_vm,
|
2020-08-20 00:16:01 +02:00
|
|
|
qubes_app=self.app,
|
|
|
|
filter_function=(lambda vm: not utils.is_internal(vm) and
|
2020-12-09 22:55:22 +01:00
|
|
|
vm.klass == 'TemplateVM'),
|
|
|
|
allow_none=True)
|
2020-08-20 00:16:01 +02:00
|
|
|
|
|
|
|
default_template = self.app.default_template
|
|
|
|
for i in range(self.template_vm.count()):
|
|
|
|
if self.template_vm.itemData(i) == default_template:
|
|
|
|
self.template_vm.setCurrentIndex(i)
|
|
|
|
self.template_vm.setItemText(
|
|
|
|
i, str(default_template) + " (default)")
|
2020-07-09 21:38:23 +02:00
|
|
|
|
2020-12-13 18:23:39 +01:00
|
|
|
self.template_type = "template"
|
2020-12-09 22:55:22 +01:00
|
|
|
|
2020-07-09 21:38:23 +02:00
|
|
|
utils.initialize_widget_with_default(
|
|
|
|
widget=self.netvm,
|
2020-07-10 14:08:22 +02:00
|
|
|
choices=[(vm.name, vm) for vm in self.app.domains
|
2020-08-04 22:50:20 +02:00
|
|
|
if not utils.is_internal(vm) and
|
|
|
|
getattr(vm, 'provides_network', False)],
|
2020-07-09 21:38:23 +02:00
|
|
|
add_none=True,
|
|
|
|
add_qubes_default=True,
|
2020-08-04 22:50:20 +02:00
|
|
|
default_value=getattr(self.app, 'default_netvm', None))
|
2020-07-09 21:38:23 +02:00
|
|
|
|
2020-08-04 22:50:20 +02:00
|
|
|
try:
|
|
|
|
utils.initialize_widget_with_default(
|
|
|
|
widget=self.storage_pool,
|
|
|
|
choices=[(str(pool), pool) for pool in self.app.pools.values()],
|
|
|
|
add_qubes_default=True,
|
|
|
|
mark_existing_as_default=True,
|
|
|
|
default_value=self.app.default_pool)
|
2020-08-11 01:08:48 +02:00
|
|
|
except qubesadmin.exc.QubesDaemonAccessError:
|
2020-08-04 22:50:20 +02:00
|
|
|
self.storage_pool.clear()
|
|
|
|
self.storage_pool.addItem("(default)", qubesadmin.DEFAULT)
|
2019-09-06 15:59:13 +02:00
|
|
|
|
2017-11-06 22:46:35 +01:00
|
|
|
self.name.setValidator(QtGui.QRegExpValidator(
|
2018-07-13 20:40:17 +02:00
|
|
|
QtCore.QRegExp("[a-zA-Z0-9_-]*", QtCore.Qt.CaseInsensitive), None))
|
2017-07-12 14:10:15 +02:00
|
|
|
self.name.selectAll()
|
|
|
|
self.name.setFocus()
|
|
|
|
|
2020-07-09 21:38:23 +02:00
|
|
|
if self.template_vm.count() < 1:
|
2019-05-22 23:10:09 +02:00
|
|
|
QtWidgets.QMessageBox.warning(
|
|
|
|
self,
|
2017-07-12 14:10:15 +02:00
|
|
|
self.tr('No template available!'),
|
|
|
|
self.tr('Cannot create a qube when no template exists.'))
|
2013-11-21 04:19:41 +01:00
|
|
|
|
2020-07-09 21:38:23 +02:00
|
|
|
type_list = [
|
2020-12-09 22:55:22 +01:00
|
|
|
(self.tr("AppVM (persistent home, volatile root)"), 'AppVM'),
|
2020-12-13 18:23:39 +01:00
|
|
|
(self.tr("TemplateVM (template home, persistent root)"),
|
2020-12-09 22:55:22 +01:00
|
|
|
'TemplateVM'),
|
|
|
|
(self.tr("StandaloneVM (fully persistent)"), 'StandaloneVM'),
|
|
|
|
(self.tr("DisposableVM (fully volatile)"), 'DispVM')]
|
|
|
|
|
2020-07-09 21:38:23 +02:00
|
|
|
utils.initialize_widget(widget=self.vm_type,
|
|
|
|
choices=type_list,
|
|
|
|
selected_value='AppVM',
|
|
|
|
add_current_label=False)
|
2017-09-13 20:17:24 +02:00
|
|
|
|
|
|
|
self.vm_type.currentIndexChanged.connect(self.type_change)
|
|
|
|
|
2021-01-09 05:53:53 +01:00
|
|
|
self.template_vm.currentIndexChanged.connect(self.template_change)
|
|
|
|
|
2017-09-13 20:17:24 +02:00
|
|
|
self.launch_settings.stateChanged.connect(self.settings_change)
|
|
|
|
self.install_system.stateChanged.connect(self.install_change)
|
|
|
|
|
2012-05-11 22:52:27 +02:00
|
|
|
def accept(self):
|
2020-12-09 22:55:22 +01:00
|
|
|
vmclass = self.vm_type.currentData()
|
2017-07-12 14:10:15 +02:00
|
|
|
name = str(self.name.text())
|
2019-07-29 21:52:40 +02:00
|
|
|
|
2021-03-14 13:23:43 +01:00
|
|
|
if self.install_system.isChecked():
|
|
|
|
self.boot_dialog = bootfromdevice.VMBootFromDeviceWindow(
|
|
|
|
name, self.qtapp, self.app, self, True)
|
|
|
|
if not self.boot_dialog.exec_():
|
|
|
|
return
|
|
|
|
|
2019-07-29 21:52:40 +02:00
|
|
|
if name in self.app.domains:
|
2019-05-22 23:10:09 +02:00
|
|
|
QtWidgets.QMessageBox.warning(
|
|
|
|
self,
|
2017-07-12 14:10:15 +02:00
|
|
|
self.tr('Incorrect qube name!'),
|
|
|
|
self.tr('A qube with the name <b>{}</b> already exists in the '
|
|
|
|
'system!').format(name))
|
2012-05-11 22:52:27 +02:00
|
|
|
return
|
|
|
|
|
2020-07-09 21:38:23 +02:00
|
|
|
label = self.label.currentData()
|
2017-09-13 20:17:24 +02:00
|
|
|
|
2020-07-09 21:38:23 +02:00
|
|
|
template = self.template_vm.currentData()
|
2012-05-11 22:52:27 +02:00
|
|
|
|
2020-12-09 22:55:22 +01:00
|
|
|
if vmclass in ['AppVM', 'DispVM'] and template is None:
|
|
|
|
QtWidgets.QMessageBox.warning(
|
|
|
|
self,
|
|
|
|
self.tr('Unspecified template'),
|
|
|
|
self.tr('{}s must be based on a template!'.format(vmclass)))
|
|
|
|
return
|
|
|
|
|
2019-05-22 23:10:09 +02:00
|
|
|
properties = {'provides_network': self.provides_network.isChecked()}
|
2018-12-08 23:09:39 +01:00
|
|
|
if self.netvm.currentIndex() != 0:
|
2020-07-09 21:38:23 +02:00
|
|
|
properties['netvm'] = self.netvm.currentData()
|
2019-07-29 21:53:44 +02:00
|
|
|
|
|
|
|
# Standalone - not based on a template
|
2020-12-09 22:55:22 +01:00
|
|
|
if vmclass == 'StandaloneVM' and template is None:
|
2018-02-06 15:47:00 +01:00
|
|
|
properties['virt_mode'] = 'hvm'
|
2018-02-10 23:34:59 +01:00
|
|
|
properties['kernel'] = None
|
2012-05-11 22:52:27 +02:00
|
|
|
|
2020-07-09 21:38:23 +02:00
|
|
|
if self.storage_pool.currentData() is not qubesadmin.DEFAULT:
|
|
|
|
pool = self.storage_pool.currentData()
|
2019-09-06 15:59:13 +02:00
|
|
|
else:
|
|
|
|
pool = None
|
|
|
|
|
|
|
|
if self.init_ram.value() > 0:
|
|
|
|
properties['memory'] = self.init_ram.value()
|
|
|
|
|
2019-05-22 23:10:09 +02:00
|
|
|
self.thread = CreateVMThread(
|
2019-09-06 15:59:13 +02:00
|
|
|
self.app, vmclass, name, label, template, properties, pool)
|
2018-10-20 18:34:15 +02:00
|
|
|
self.thread.finished.connect(self.create_finished)
|
|
|
|
self.thread.start()
|
2012-05-11 22:52:27 +02:00
|
|
|
|
2019-05-22 23:10:09 +02:00
|
|
|
self.progress = QtWidgets.QProgressDialog(
|
2019-10-23 18:18:39 +02:00
|
|
|
self.tr("Creating new qube <b>{0}</b>...").format(name), "", 0, 0)
|
2018-10-20 18:34:15 +02:00
|
|
|
self.progress.setCancelButton(None)
|
|
|
|
self.progress.setModal(True)
|
|
|
|
self.progress.show()
|
2012-05-11 22:52:27 +02:00
|
|
|
|
2018-10-20 18:34:15 +02:00
|
|
|
def create_finished(self):
|
2018-10-22 20:00:37 +02:00
|
|
|
if self.thread.msg:
|
2019-05-22 23:10:09 +02:00
|
|
|
QtWidgets.QMessageBox.warning(
|
|
|
|
self,
|
2017-07-12 14:10:15 +02:00
|
|
|
self.tr("Error creating the qube!"),
|
2019-10-23 18:18:39 +02:00
|
|
|
self.tr("ERROR: {0}").format(self.thread.msg))
|
2012-05-11 22:52:27 +02:00
|
|
|
|
2021-03-14 13:23:43 +01:00
|
|
|
else:
|
2017-09-13 20:17:24 +02:00
|
|
|
if self.launch_settings.isChecked():
|
2018-10-27 13:21:33 +02:00
|
|
|
subprocess.check_call(['qubes-vm-settings',
|
2019-05-22 23:10:09 +02:00
|
|
|
str(self.name.text())])
|
2017-09-13 20:17:24 +02:00
|
|
|
if self.install_system.isChecked():
|
2021-03-14 13:23:43 +01:00
|
|
|
qubesadmin.tools.qvm_start.main(
|
|
|
|
['--cdrom', self.boot_dialog.cdrom_location,
|
|
|
|
self.name.text()])
|
|
|
|
|
2021-03-14 14:02:31 +01:00
|
|
|
self.progress.hide()
|
|
|
|
self.done(0)
|
2017-09-13 20:17:24 +02:00
|
|
|
|
|
|
|
def type_change(self):
|
2020-12-09 22:55:22 +01:00
|
|
|
template = self.template_vm.currentData()
|
|
|
|
klass = self.vm_type.currentData()
|
2017-09-13 20:17:24 +02:00
|
|
|
|
2020-12-09 22:55:22 +01:00
|
|
|
if klass in ['TemplateVM', 'StandaloneVM'] and template is None:
|
|
|
|
self.install_system.setEnabled(True)
|
|
|
|
self.install_system.setChecked(True)
|
|
|
|
else:
|
2017-09-13 20:17:24 +02:00
|
|
|
self.install_system.setEnabled(False)
|
|
|
|
self.install_system.setChecked(False)
|
|
|
|
|
2020-12-09 22:55:22 +01:00
|
|
|
if klass == 'DispVM':
|
|
|
|
self.template_vm.clear()
|
|
|
|
|
|
|
|
for vm in self.app.domains:
|
|
|
|
if utils.is_internal(vm):
|
|
|
|
continue
|
|
|
|
if vm.klass != 'AppVM':
|
|
|
|
continue
|
|
|
|
if getattr(vm, 'template_for_dispvms', True):
|
|
|
|
self.template_vm.addItem(vm.name, userData=vm)
|
|
|
|
|
|
|
|
self.template_vm.insertItem(self.template_vm.count(),
|
|
|
|
utils.translate("(none)"), None)
|
|
|
|
|
|
|
|
self.template_vm.setCurrentIndex(0)
|
2020-12-13 18:23:39 +01:00
|
|
|
self.template_type = "dispvm"
|
|
|
|
elif self.template_type == "dispvm":
|
2020-12-09 22:55:22 +01:00
|
|
|
self.template_vm.clear()
|
2020-12-13 18:23:39 +01:00
|
|
|
|
2020-12-09 22:55:22 +01:00
|
|
|
for vm in self.app.domains:
|
|
|
|
if utils.is_internal(vm):
|
|
|
|
continue
|
|
|
|
if vm.klass == 'TemplateVM':
|
|
|
|
self.template_vm.addItem(vm.name, userData=vm)
|
|
|
|
|
|
|
|
self.template_vm.insertItem(self.template_vm.count(),
|
|
|
|
utils.translate("(none)"), None)
|
|
|
|
|
|
|
|
self.template_vm.setCurrentIndex(0)
|
2020-12-13 18:23:39 +01:00
|
|
|
self.template_type = "template"
|
2017-09-13 20:17:24 +02:00
|
|
|
|
2021-01-09 05:53:53 +01:00
|
|
|
def template_change(self):
|
|
|
|
template = self.template_vm.currentData()
|
|
|
|
klass = self.vm_type.currentData()
|
|
|
|
|
|
|
|
if klass in ['TemplateVM', 'StandaloneVM'] and template is None:
|
|
|
|
self.install_system.setEnabled(True)
|
|
|
|
self.install_system.setChecked(True)
|
|
|
|
else:
|
|
|
|
self.install_system.setEnabled(False)
|
|
|
|
self.install_system.setChecked(False)
|
|
|
|
|
2017-09-13 20:17:24 +02:00
|
|
|
def install_change(self):
|
|
|
|
if self.install_system.isChecked():
|
|
|
|
self.launch_settings.setChecked(False)
|
|
|
|
|
|
|
|
def settings_change(self):
|
|
|
|
if self.launch_settings.isChecked() and self.install_system.isEnabled():
|
|
|
|
self.install_system.setChecked(False)
|
|
|
|
|
2019-05-22 23:10:09 +02:00
|
|
|
|
2017-07-12 14:10:15 +02:00
|
|
|
parser = qubesadmin.tools.QubesArgumentParser()
|
|
|
|
|
2019-05-22 23:10:09 +02:00
|
|
|
|
2017-07-12 14:10:15 +02:00
|
|
|
def main(args=None):
|
|
|
|
args = parser.parse_args(args)
|
|
|
|
|
2019-05-22 23:10:09 +02:00
|
|
|
qtapp = QtWidgets.QApplication(sys.argv)
|
2019-10-23 18:18:39 +02:00
|
|
|
|
|
|
|
translator = QtCore.QTranslator(qtapp)
|
|
|
|
locale = QtCore.QLocale.system().name()
|
|
|
|
i18n_dir = os.path.join(
|
|
|
|
os.path.dirname(os.path.realpath(__file__)),
|
|
|
|
'i18n')
|
|
|
|
translator.load("qubesmanager_{!s}.qm".format(locale), i18n_dir)
|
|
|
|
qtapp.installTranslator(translator)
|
2019-11-08 23:35:26 +01:00
|
|
|
QtCore.QCoreApplication.installTranslator(translator)
|
2019-10-23 18:18:39 +02:00
|
|
|
|
2017-07-12 14:10:15 +02:00
|
|
|
qtapp.setOrganizationName('Invisible Things Lab')
|
|
|
|
qtapp.setOrganizationDomain('https://www.qubes-os.org/')
|
2019-10-23 18:18:39 +02:00
|
|
|
qtapp.setApplicationName(QtCore.QCoreApplication.translate(
|
|
|
|
"appname", 'Create qube'))
|
2012-05-11 22:52:27 +02:00
|
|
|
|
2017-07-12 14:10:15 +02:00
|
|
|
dialog = NewVmDlg(qtapp, args.app)
|
|
|
|
dialog.exec_()
|