bootfromdevice.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. from qubesadmin.tools import qvm_start
  27. class VMBootFromDeviceWindow(ui_bootfromdevice.Ui_BootDialog,
  28. QtWidgets.QDialog):
  29. def __init__(self, vm, qapp, qubesapp=None, parent=None, new_vm=False):
  30. super().__init__(parent)
  31. self.vm = vm
  32. self.qapp = qapp
  33. self.qubesapp = qubesapp
  34. self.cdrom_location = None
  35. self.new_vm = new_vm
  36. self.setupUi(self)
  37. self.setWindowTitle(
  38. self.tr("Boot {vm} from device").format(vm=self.vm))
  39. self.buttonBox.accepted.connect(self.save_and_apply)
  40. self.buttonBox.rejected.connect(self.reject)
  41. # populate buttons and such
  42. self.__init_buttons__()
  43. # warn user if the VM is currently running
  44. self.__warn_if_running__()
  45. def setup_application(self):
  46. self.qapp.setApplicationName(self.tr("Boot Qube From Device"))
  47. self.qapp.setWindowIcon(QtGui.QIcon.fromTheme("qubes-manager"))
  48. def save_and_apply(self):
  49. if self.blockDeviceRadioButton.isChecked():
  50. self.cdrom_location = self.blockDeviceComboBox.currentText()
  51. elif self.fileRadioButton.isChecked():
  52. self.cdrom_location = str(self.fileVM.currentData()) + \
  53. ":" + self.pathText.text()
  54. else:
  55. QtWidgets.QMessageBox.warning(
  56. self,
  57. self.tr("ERROR!"),
  58. self.tr("No file or block device selected; please select one."))
  59. return
  60. # warn user if the VM is currently running
  61. self.__warn_if_running__()
  62. self.accept()
  63. def __warn_if_running__(self):
  64. if self.new_vm:
  65. return
  66. try:
  67. if self.qubesapp.domains[self.vm].is_running():
  68. QtWidgets.QMessageBox.warning(
  69. self,
  70. self.tr("Warning!"),
  71. self.tr("Qube must be turned off before booting it from "
  72. "device. Please turn off the qube."))
  73. except exc.QubesDaemonAccessError:
  74. QtWidgets.QMessageBox.warning(
  75. self,
  76. self.tr("Warning!"),
  77. self.tr("Insufficient permissions to determine if qube is "
  78. "running. It must be turned off before booting it from "
  79. "device."))
  80. def __init_buttons__(self):
  81. self.fileVM.setEnabled(False)
  82. self.selectFileButton.setEnabled(False)
  83. self.blockDeviceComboBox.setEnabled(False)
  84. self.blockDeviceRadioButton.clicked.connect(self.radio_button_clicked)
  85. self.fileRadioButton.clicked.connect(self.radio_button_clicked)
  86. self.selectFileButton.clicked.connect(self.select_file_dialog)
  87. utils.initialize_widget_with_vms(
  88. widget=self.fileVM,
  89. qubes_app=self.qubesapp,
  90. allow_internal=True
  91. )
  92. device_choice = []
  93. for domain in self.qubesapp.domains:
  94. try:
  95. for device in domain.devices["block"]:
  96. device_choice.append((str(device), device))
  97. except exc.QubesDaemonAccessError:
  98. # insufficient permissions
  99. pass
  100. if device_choice:
  101. utils.initialize_widget(
  102. widget=self.blockDeviceComboBox,
  103. choices=device_choice,
  104. selected_value=device_choice[0][1],
  105. add_current_label=False
  106. )
  107. else:
  108. self.blockDeviceRadioButton.setEnabled(False)
  109. self.blockDeviceComboBox.setEnabled(False)
  110. self.blockDeviceComboBox.addItem("no block devices found!")
  111. self.blockDeviceComboBox.setCurrentIndex(0)
  112. def radio_button_clicked(self):
  113. self.blockDeviceComboBox.setEnabled(
  114. self.blockDeviceRadioButton.isChecked())
  115. self.fileVM.setEnabled(self.fileRadioButton.isChecked())
  116. self.selectFileButton.setEnabled(self.fileRadioButton.isChecked())
  117. self.pathText.setEnabled(self.fileRadioButton.isChecked())
  118. def select_file_dialog(self):
  119. backend_vm = self.fileVM.currentData()
  120. error_occurred = False
  121. try:
  122. new_path = utils.get_path_from_vm(backend_vm, "qubes.SelectFile")
  123. except subprocess.CalledProcessError as ex:
  124. if ex.returncode != 1:
  125. # Error other than 'user did not select a file'
  126. error_occurred = True
  127. new_path = None
  128. except Exception: # pylint: disable=broad-except
  129. error_occurred = True
  130. new_path = None
  131. if error_occurred:
  132. QtWidgets.QMessageBox.warning(
  133. None,
  134. self.tr("Failed to display file selection dialog"),
  135. self.tr("Check if the qube {0} can be started and has a file"
  136. " manager installed.").format(backend_vm)
  137. )
  138. if new_path:
  139. self.pathText.setText(new_path)
  140. parser = tools.QubesArgumentParser(vmname_nargs=1)
  141. def main(args=None):
  142. args = parser.parse_args(args)
  143. vm = args.domains.pop()
  144. window = utils.run_synchronous(
  145. functools.partial(VMBootFromDeviceWindow, vm))
  146. if window.result() == 1 and window.cdrom_location is not None:
  147. qvm_start.main(['--cdrom', window.cdrom_location, vm.name])
  148. if __name__ == "__main__":
  149. main()