bootfromdevice.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #!/usr/bin/python3
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Lesser General Public License along
  16. # with this program; if not, see <http://www.gnu.org/licenses/>.
  17. #
  18. #
  19. import functools
  20. import subprocess
  21. from . import utils
  22. from . import ui_bootfromdevice # pylint: disable=no-name-in-module
  23. from PyQt5 import QtWidgets, QtGui # pylint: disable=import-error
  24. from qubesadmin import tools
  25. from qubesadmin import exc
  26. class VMBootFromDeviceWindow(ui_bootfromdevice.Ui_BootDialog,
  27. QtWidgets.QDialog):
  28. def __init__(self, vm, qapp, qubesapp=None, parent=None):
  29. super().__init__(parent)
  30. self.vm = vm
  31. self.qapp = qapp
  32. self.qubesapp = qubesapp
  33. self.cdrom_location = None
  34. self.setupUi(self)
  35. self.setWindowTitle(
  36. self.tr("Boot {vm} from device").format(vm=self.vm))
  37. self.buttonBox.accepted.connect(self.save_and_apply)
  38. # populate buttons and such
  39. self.__init_buttons__()
  40. # warn user if the VM is currently running
  41. self.__warn_if_running__()
  42. def setup_application(self):
  43. self.qapp.setApplicationName(self.tr("Boot Qube From Device"))
  44. self.qapp.setWindowIcon(QtGui.QIcon.fromTheme("qubes-manager"))
  45. def save_and_apply(self):
  46. if self.blockDeviceRadioButton.isChecked():
  47. self.cdrom_location = self.blockDeviceComboBox.currentText()
  48. elif self.fileRadioButton.isChecked():
  49. self.cdrom_location = str(self.fileVM.currentData()) + \
  50. ":" + self.pathText.text()
  51. else:
  52. QtWidgets.QMessageBox.warning(
  53. self,
  54. self.tr("ERROR!"),
  55. self.tr("No file or block device selected; please select one."))
  56. return
  57. # warn user if the VM is currently running
  58. self.__warn_if_running__()
  59. self.accept()
  60. def __warn_if_running__(self):
  61. try:
  62. if self.qubesapp.domains[self.vm].is_running():
  63. QtWidgets.QMessageBox.warning(
  64. self,
  65. self.tr("Warning!"),
  66. self.tr("Qube must be turned off before booting it from "
  67. "device. Please turn off the qube."))
  68. except exc.QubesDaemonAccessError:
  69. QtWidgets.QMessageBox.warning(
  70. self,
  71. self.tr("Warning!"),
  72. self.tr("Insufficient permissions to determine if qube is "
  73. "running. It must be turned off before booting it from "
  74. "device."))
  75. def __init_buttons__(self):
  76. self.fileVM.setEnabled(False)
  77. self.selectFileButton.setEnabled(False)
  78. self.blockDeviceComboBox.setEnabled(False)
  79. self.blockDeviceRadioButton.clicked.connect(self.radio_button_clicked)
  80. self.fileRadioButton.clicked.connect(self.radio_button_clicked)
  81. self.selectFileButton.clicked.connect(self.select_file_dialog)
  82. utils.initialize_widget_with_vms(
  83. widget=self.fileVM,
  84. qubes_app=self.qubesapp,
  85. allow_internal=True
  86. )
  87. device_choice = []
  88. for domain in self.qubesapp.domains:
  89. try:
  90. for device in domain.devices["block"]:
  91. device_choice.append((str(device), device))
  92. except exc.QubesDaemonAccessError:
  93. # insufficient permissions
  94. pass
  95. if device_choice:
  96. utils.initialize_widget(
  97. widget=self.blockDeviceComboBox,
  98. choices=device_choice,
  99. selected_value=device_choice[0][1],
  100. add_current_label=False
  101. )
  102. else:
  103. self.blockDeviceRadioButton.setEnabled(False)
  104. self.blockDeviceComboBox.setEnabled(False)
  105. self.blockDeviceComboBox.addItem("no block devices found!")
  106. self.blockDeviceComboBox.setCurrentIndex(0)
  107. def radio_button_clicked(self):
  108. self.blockDeviceComboBox.setEnabled(
  109. self.blockDeviceRadioButton.isChecked())
  110. self.fileVM.setEnabled(self.fileRadioButton.isChecked())
  111. self.selectFileButton.setEnabled(self.fileRadioButton.isChecked())
  112. self.pathText.setEnabled(self.fileRadioButton.isChecked())
  113. def select_file_dialog(self):
  114. backend_vm = self.fileVM.currentData()
  115. error_occurred = False
  116. try:
  117. new_path = utils.get_path_from_vm(backend_vm, "qubes.SelectFile")
  118. except subprocess.CalledProcessError as ex:
  119. if ex.returncode != 1:
  120. # Error other than 'user did not select a file'
  121. error_occurred = True
  122. new_path = None
  123. except Exception: # pylint: disable=broad-except
  124. error_occurred = True
  125. new_path = None
  126. if error_occurred:
  127. QtWidgets.QMessageBox.warning(
  128. None,
  129. self.tr("Failed to display file selection dialog"),
  130. self.tr("Check if the qube {0} can be started and has a file"
  131. " manager installed.").format(backend_vm)
  132. )
  133. if new_path:
  134. self.pathText.setText(new_path)
  135. parser = tools.QubesArgumentParser(vmname_nargs=1)
  136. def main(args=None):
  137. args = parser.parse_args(args)
  138. vm = args.domains.pop()
  139. utils.run_synchronous(functools.partial(VMBootFromDeviceWindow, vm))
  140. if __name__ == "__main__":
  141. main()