bootfromdevice.py 5.2 KB

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