2013-01-23 15:55:14 +01:00
|
|
|
#!/usr/bin/python2
|
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
|
|
|
#
|
|
|
|
#
|
|
|
|
|
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
|
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):
|
|
|
|
def __init__(self, app, vmclass, name, label, template, properties):
|
|
|
|
QtCore.QThread.__init__(self)
|
|
|
|
self.app = app
|
|
|
|
self.vmclass = vmclass
|
|
|
|
self.name = name
|
|
|
|
self.label = label
|
|
|
|
self.template = template
|
|
|
|
self.properties = properties
|
2018-10-22 20:00:37 +02:00
|
|
|
self.msg = None
|
2018-10-20 18:34:15 +02:00
|
|
|
|
|
|
|
def run(self):
|
|
|
|
try:
|
|
|
|
if self.vmclass == 'StandaloneVM' and self.template is not None:
|
|
|
|
if self.template is qubesadmin.DEFAULT:
|
|
|
|
src_vm = self.app.default_template
|
|
|
|
else:
|
|
|
|
src_vm = self.template
|
2018-12-08 22:24:48 +01:00
|
|
|
vm = self.app.clone_vm(src_vm, self.name, self.vmclass,
|
|
|
|
ignore_volumes=['private'])
|
2018-10-20 18:34:15 +02:00
|
|
|
vm.label = self.label
|
|
|
|
for k, v in self.properties.items():
|
|
|
|
setattr(vm, k, v)
|
|
|
|
else:
|
2019-05-22 23:10:09 +02:00
|
|
|
vm = self.app.add_new_vm(
|
|
|
|
self.vmclass, name=self.name,
|
|
|
|
label=self.label, template=self.template)
|
2018-10-20 18:34:15 +02:00
|
|
|
for k, v in self.properties.items():
|
|
|
|
setattr(vm, k, v)
|
|
|
|
|
|
|
|
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):
|
2017-07-12 14:10:15 +02:00
|
|
|
super(NewVmDlg, self).__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
|
|
|
|
|
2012-05-11 22:52:27 +02:00
|
|
|
# Theoretically we should be locking for writing here and unlock
|
2017-11-06 23:18:18 +01:00
|
|
|
# only after the VM creation finished. But the code would be
|
|
|
|
# more messy...
|
2012-05-11 22:52:27 +02:00
|
|
|
# Instead we lock for writing in the actual worker thread
|
2017-07-12 14:10:15 +02:00
|
|
|
self.label_list, self.label_idx = utils.prepare_label_choice(
|
|
|
|
self.label,
|
|
|
|
self.app, None,
|
|
|
|
None,
|
|
|
|
allow_default=False)
|
|
|
|
|
|
|
|
self.template_list, self.template_idx = utils.prepare_vm_choice(
|
|
|
|
self.template_vm,
|
|
|
|
self.app, None,
|
|
|
|
self.app.default_template,
|
2017-10-07 00:22:41 +02:00
|
|
|
(lambda vm: vm.klass == 'TemplateVM'),
|
2017-07-12 14:10:15 +02:00
|
|
|
allow_internal=False, allow_default=True, allow_none=False)
|
|
|
|
|
|
|
|
self.netvm_list, self.netvm_idx = utils.prepare_vm_choice(
|
|
|
|
self.netvm,
|
|
|
|
self.app, None,
|
|
|
|
self.app.default_netvm,
|
|
|
|
(lambda vm: vm.provides_network),
|
|
|
|
allow_internal=False, allow_default=True, allow_none=True)
|
|
|
|
|
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()
|
|
|
|
|
2017-11-14 15:29:57 +01:00
|
|
|
if not self.template_list:
|
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
|
|
|
|
2017-09-13 20:17:24 +02:00
|
|
|
# Order of types is important and used elsewhere; if it's changed
|
2019-05-06 17:37:32 +02:00
|
|
|
# check for changes needed in self.type_change
|
2019-04-08 23:50:05 +02:00
|
|
|
type_list = [self.tr("Qube based on a template (AppVM)"),
|
2019-05-06 17:37:32 +02:00
|
|
|
self.tr("Standalone qube copied from a template"),
|
|
|
|
self.tr("Empty standalone qube (install your own OS)")]
|
2017-09-13 20:17:24 +02:00
|
|
|
self.vm_type.addItems(type_list)
|
|
|
|
|
|
|
|
self.vm_type.currentIndexChanged.connect(self.type_change)
|
|
|
|
|
|
|
|
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 reject(self):
|
|
|
|
self.done(0)
|
|
|
|
|
|
|
|
def accept(self):
|
2017-11-06 23:18:18 +01:00
|
|
|
vmclass = ('AppVM' if self.vm_type.currentIndex() == 0
|
|
|
|
else 'StandaloneVM')
|
2017-07-12 14:10:15 +02:00
|
|
|
|
|
|
|
name = str(self.name.text())
|
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
|
|
|
|
|
2017-07-12 14:10:15 +02:00
|
|
|
label = self.label_list[self.label.currentIndex()]
|
2017-09-13 20:17:24 +02:00
|
|
|
|
|
|
|
if self.template_vm.currentIndex() == -1:
|
|
|
|
template = None
|
|
|
|
else:
|
|
|
|
template = self.template_list[self.template_vm.currentIndex()]
|
2012-05-11 22:52:27 +02:00
|
|
|
|
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:
|
|
|
|
properties['netvm'] = self.netvm_list[self.netvm.currentIndex()]
|
2019-07-29 21:53:44 +02:00
|
|
|
|
|
|
|
# Standalone - not based on a template
|
|
|
|
if self.vm_type.currentIndex() == 2:
|
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
|
|
|
|
2019-05-22 23:10:09 +02:00
|
|
|
self.thread = CreateVMThread(
|
|
|
|
self.app, vmclass, name, label, template, properties)
|
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(
|
2017-07-12 14:10:15 +02:00
|
|
|
self.tr("Creating new qube <b>{}</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):
|
|
|
|
self.progress.hide()
|
2012-05-11 22:52:27 +02:00
|
|
|
|
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!"),
|
2018-10-22 20:00:37 +02:00
|
|
|
self.tr("ERROR: {}").format(self.thread.msg))
|
2012-05-11 22:52:27 +02:00
|
|
|
|
|
|
|
self.done(0)
|
|
|
|
|
2018-10-22 20:00:37 +02:00
|
|
|
if not self.thread.msg:
|
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():
|
|
|
|
subprocess.check_call(
|
2018-10-25 09:45:12 +02:00
|
|
|
['qubes-vm-boot-from-device', str(self.name.text())])
|
2017-09-13 20:17:24 +02:00
|
|
|
|
|
|
|
def type_change(self):
|
|
|
|
# AppVM
|
|
|
|
if self.vm_type.currentIndex() == 0:
|
|
|
|
self.template_vm.setEnabled(True)
|
|
|
|
self.template_vm.setCurrentIndex(0)
|
|
|
|
self.install_system.setEnabled(False)
|
|
|
|
self.install_system.setChecked(False)
|
|
|
|
|
|
|
|
# Standalone - based on a template
|
|
|
|
if self.vm_type.currentIndex() == 1:
|
|
|
|
self.template_vm.setEnabled(True)
|
|
|
|
self.template_vm.setCurrentIndex(0)
|
|
|
|
self.install_system.setEnabled(False)
|
|
|
|
self.install_system.setChecked(False)
|
|
|
|
|
|
|
|
# Standalone - not based on a template
|
|
|
|
if self.vm_type.currentIndex() == 2:
|
|
|
|
self.template_vm.setEnabled(False)
|
|
|
|
self.template_vm.setCurrentIndex(-1)
|
|
|
|
self.install_system.setEnabled(True)
|
|
|
|
self.install_system.setChecked(True)
|
|
|
|
|
|
|
|
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)
|
2017-07-12 14:10:15 +02:00
|
|
|
qtapp.setOrganizationName('Invisible Things Lab')
|
|
|
|
qtapp.setOrganizationDomain('https://www.qubes-os.org/')
|
|
|
|
qtapp.setApplicationName('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_()
|