2017-09-08 22:43:43 +02:00
|
|
|
#!/usr/bin/python3
|
|
|
|
#
|
|
|
|
# The Qubes OS Project, http://www.qubes-os.org
|
|
|
|
#
|
|
|
|
# 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/>.
|
2017-09-08 22:43:43 +02:00
|
|
|
#
|
|
|
|
#
|
|
|
|
|
2019-09-26 22:31:39 +02:00
|
|
|
import functools
|
2017-09-08 22:43:43 +02:00
|
|
|
import subprocess
|
|
|
|
from . import utils
|
2017-11-14 15:29:57 +01:00
|
|
|
from . import ui_bootfromdevice # pylint: disable=no-name-in-module
|
2019-11-08 23:35:26 +01:00
|
|
|
from PyQt5 import QtWidgets, QtGui # pylint: disable=import-error
|
2017-11-17 17:34:38 +01:00
|
|
|
from qubesadmin import tools
|
2020-08-04 22:50:20 +02:00
|
|
|
from qubesadmin import exc
|
2021-04-10 18:54:28 +02:00
|
|
|
from qubesadmin.tools import qvm_start
|
2017-09-08 22:43:43 +02:00
|
|
|
|
|
|
|
|
2019-05-22 23:10:09 +02:00
|
|
|
class VMBootFromDeviceWindow(ui_bootfromdevice.Ui_BootDialog,
|
|
|
|
QtWidgets.QDialog):
|
2021-03-14 13:27:00 +01:00
|
|
|
def __init__(self, vm, qapp, qubesapp=None, parent=None, new_vm=False):
|
2020-08-23 02:53:36 +02:00
|
|
|
super().__init__(parent)
|
2017-09-08 22:43:43 +02:00
|
|
|
|
|
|
|
self.vm = vm
|
|
|
|
self.qapp = qapp
|
2019-09-26 22:40:40 +02:00
|
|
|
self.qubesapp = qubesapp
|
2021-03-14 00:20:08 +01:00
|
|
|
self.cdrom_location = None
|
2021-03-14 13:23:43 +01:00
|
|
|
self.new_vm = new_vm
|
2017-09-08 22:43:43 +02:00
|
|
|
|
|
|
|
self.setupUi(self)
|
2017-11-06 23:18:18 +01:00
|
|
|
self.setWindowTitle(
|
2021-03-14 13:21:24 +01:00
|
|
|
self.tr("Boot {vm} from device").format(vm=self.vm))
|
2017-09-08 22:43:43 +02:00
|
|
|
|
2019-05-22 23:10:09 +02:00
|
|
|
self.buttonBox.accepted.connect(self.save_and_apply)
|
2021-03-14 18:29:32 +01:00
|
|
|
self.buttonBox.rejected.connect(self.reject)
|
2017-09-08 22:43:43 +02:00
|
|
|
|
|
|
|
# populate buttons and such
|
|
|
|
self.__init_buttons__()
|
2018-01-08 20:05:25 +01:00
|
|
|
# warn user if the VM is currently running
|
|
|
|
self.__warn_if_running__()
|
2017-09-08 22:43:43 +02:00
|
|
|
|
2019-11-08 23:35:26 +01:00
|
|
|
def setup_application(self):
|
|
|
|
self.qapp.setApplicationName(self.tr("Boot Qube From Device"))
|
|
|
|
self.qapp.setWindowIcon(QtGui.QIcon.fromTheme("qubes-manager"))
|
|
|
|
|
2017-09-08 22:43:43 +02:00
|
|
|
def save_and_apply(self):
|
|
|
|
if self.blockDeviceRadioButton.isChecked():
|
2021-03-14 00:20:08 +01:00
|
|
|
self.cdrom_location = self.blockDeviceComboBox.currentText()
|
2017-09-08 22:43:43 +02:00
|
|
|
elif self.fileRadioButton.isChecked():
|
2021-03-14 00:20:08 +01:00
|
|
|
self.cdrom_location = str(self.fileVM.currentData()) + \
|
2017-11-06 23:18:18 +01:00
|
|
|
":" + self.pathText.text()
|
2017-09-08 22:43:43 +02:00
|
|
|
else:
|
2019-05-22 23:10:09 +02:00
|
|
|
QtWidgets.QMessageBox.warning(
|
|
|
|
self,
|
2017-11-06 23:18:18 +01:00
|
|
|
self.tr("ERROR!"),
|
|
|
|
self.tr("No file or block device selected; please select one."))
|
2017-09-08 22:43:43 +02:00
|
|
|
return
|
2018-01-08 20:05:25 +01:00
|
|
|
|
|
|
|
# warn user if the VM is currently running
|
|
|
|
self.__warn_if_running__()
|
2021-03-14 00:20:08 +01:00
|
|
|
self.accept()
|
2018-02-11 00:20:14 +01:00
|
|
|
|
2018-01-08 20:05:25 +01:00
|
|
|
def __warn_if_running__(self):
|
2021-03-14 13:23:43 +01:00
|
|
|
if self.new_vm:
|
|
|
|
return
|
|
|
|
|
2020-08-04 22:50:20 +02:00
|
|
|
try:
|
2021-03-14 13:21:24 +01:00
|
|
|
if self.qubesapp.domains[self.vm].is_running():
|
2020-08-04 22:50:20 +02:00
|
|
|
QtWidgets.QMessageBox.warning(
|
|
|
|
self,
|
|
|
|
self.tr("Warning!"),
|
|
|
|
self.tr("Qube must be turned off before booting it from "
|
|
|
|
"device. Please turn off the qube."))
|
2020-08-11 01:08:48 +02:00
|
|
|
except exc.QubesDaemonAccessError:
|
2019-05-22 23:10:09 +02:00
|
|
|
QtWidgets.QMessageBox.warning(
|
|
|
|
self,
|
2018-01-08 20:05:25 +01:00
|
|
|
self.tr("Warning!"),
|
2020-08-04 22:50:20 +02:00
|
|
|
self.tr("Insufficient permissions to determine if qube is "
|
|
|
|
"running. It must be turned off before booting it from "
|
|
|
|
"device."))
|
2018-01-08 20:05:25 +01:00
|
|
|
|
2017-09-08 22:43:43 +02:00
|
|
|
def __init_buttons__(self):
|
|
|
|
self.fileVM.setEnabled(False)
|
|
|
|
self.selectFileButton.setEnabled(False)
|
|
|
|
self.blockDeviceComboBox.setEnabled(False)
|
|
|
|
|
|
|
|
self.blockDeviceRadioButton.clicked.connect(self.radio_button_clicked)
|
|
|
|
self.fileRadioButton.clicked.connect(self.radio_button_clicked)
|
|
|
|
self.selectFileButton.clicked.connect(self.select_file_dialog)
|
|
|
|
|
2020-07-10 14:08:22 +02:00
|
|
|
utils.initialize_widget_with_vms(
|
|
|
|
widget=self.fileVM,
|
|
|
|
qubes_app=self.qubesapp,
|
|
|
|
allow_internal=True
|
|
|
|
)
|
|
|
|
|
2020-08-04 22:50:20 +02:00
|
|
|
device_choice = []
|
2021-03-14 13:21:24 +01:00
|
|
|
|
|
|
|
for domain in self.qubesapp.domains:
|
2020-08-04 22:50:20 +02:00
|
|
|
try:
|
|
|
|
for device in domain.devices["block"]:
|
|
|
|
device_choice.append((str(device), device))
|
2020-08-11 01:08:48 +02:00
|
|
|
except exc.QubesDaemonAccessError:
|
2020-08-04 22:50:20 +02:00
|
|
|
# insufficient permissions
|
|
|
|
pass
|
2020-07-10 14:08:22 +02:00
|
|
|
|
2020-08-31 14:19:50 +02:00
|
|
|
if device_choice:
|
|
|
|
utils.initialize_widget(
|
|
|
|
widget=self.blockDeviceComboBox,
|
|
|
|
choices=device_choice,
|
|
|
|
selected_value=device_choice[0][1],
|
|
|
|
add_current_label=False
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
self.blockDeviceRadioButton.setEnabled(False)
|
|
|
|
self.blockDeviceComboBox.setEnabled(False)
|
|
|
|
self.blockDeviceComboBox.addItem("no block devices found!")
|
|
|
|
self.blockDeviceComboBox.setCurrentIndex(0)
|
2017-09-08 22:43:43 +02:00
|
|
|
|
|
|
|
def radio_button_clicked(self):
|
2017-11-06 23:18:18 +01:00
|
|
|
self.blockDeviceComboBox.setEnabled(
|
|
|
|
self.blockDeviceRadioButton.isChecked())
|
2017-09-08 22:43:43 +02:00
|
|
|
self.fileVM.setEnabled(self.fileRadioButton.isChecked())
|
|
|
|
self.selectFileButton.setEnabled(self.fileRadioButton.isChecked())
|
|
|
|
self.pathText.setEnabled(self.fileRadioButton.isChecked())
|
|
|
|
|
|
|
|
def select_file_dialog(self):
|
2020-07-10 14:08:22 +02:00
|
|
|
backend_vm = self.fileVM.currentData()
|
2019-07-29 19:13:23 +02:00
|
|
|
error_occurred = False
|
2017-09-08 22:43:43 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
new_path = utils.get_path_from_vm(backend_vm, "qubes.SelectFile")
|
2019-07-29 19:13:23 +02:00
|
|
|
except subprocess.CalledProcessError as ex:
|
|
|
|
if ex.returncode != 1:
|
|
|
|
# Error other than 'user did not select a file'
|
|
|
|
error_occurred = True
|
2017-09-08 22:43:43 +02:00
|
|
|
new_path = None
|
2019-07-29 19:13:23 +02:00
|
|
|
except Exception: # pylint: disable=broad-except
|
|
|
|
error_occurred = True
|
|
|
|
new_path = None
|
|
|
|
|
|
|
|
if error_occurred:
|
2019-08-28 15:48:29 +02:00
|
|
|
QtWidgets.QMessageBox.warning(
|
2019-07-29 19:13:23 +02:00
|
|
|
None,
|
|
|
|
self.tr("Failed to display file selection dialog"),
|
|
|
|
self.tr("Check if the qube {0} can be started and has a file"
|
|
|
|
" manager installed.").format(backend_vm)
|
|
|
|
)
|
2017-09-08 22:43:43 +02:00
|
|
|
|
|
|
|
if new_path:
|
|
|
|
self.pathText.setText(new_path)
|
|
|
|
|
|
|
|
|
2017-11-17 17:34:38 +01:00
|
|
|
parser = tools.QubesArgumentParser(vmname_nargs=1)
|
2017-09-08 22:43:43 +02:00
|
|
|
|
2018-01-08 20:00:55 +01:00
|
|
|
|
2017-09-08 22:43:43 +02:00
|
|
|
def main(args=None):
|
|
|
|
args = parser.parse_args(args)
|
|
|
|
vm = args.domains.pop()
|
|
|
|
|
2021-05-29 23:23:52 +02:00
|
|
|
window = utils.run_synchronous(
|
|
|
|
functools.partial(VMBootFromDeviceWindow, vm))
|
2021-04-10 19:16:12 +02:00
|
|
|
if window.result() == 1 and window.cdrom_location is not None:
|
|
|
|
qvm_start.main(['--cdrom', window.cdrom_location, vm.name])
|
2019-05-22 23:10:09 +02:00
|
|
|
|
2017-09-08 22:43:43 +02:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|