bootfromdevice.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 sys
  20. import subprocess
  21. from . import utils
  22. from . import ui_bootfromdevice # pylint: disable=no-name-in-module
  23. from PyQt4 import QtGui, QtCore # pylint: disable=import-error
  24. from qubesadmin import tools
  25. from qubesadmin.tools import qvm_start
  26. class VMBootFromDeviceWindow(ui_bootfromdevice.Ui_BootDialog, QtGui.QDialog):
  27. def __init__(self, vm, qapp, parent=None):
  28. super(VMBootFromDeviceWindow, self).__init__(parent)
  29. self.vm = vm
  30. self.qapp = qapp
  31. self.setupUi(self)
  32. self.setWindowTitle(
  33. self.tr("Boot {vm} from device").format(vm=self.vm.name))
  34. self.connect(
  35. self.buttonBox,
  36. QtCore.SIGNAL("accepted()"),
  37. self.save_and_apply)
  38. self.connect(self.buttonBox, QtCore.SIGNAL("rejected()"), self.reject)
  39. # populate buttons and such
  40. self.__init_buttons__()
  41. # warn user if the VM is currently running
  42. self.__warn_if_running__()
  43. def reject(self):
  44. self.done(0)
  45. def save_and_apply(self):
  46. if self.blockDeviceRadioButton.isChecked():
  47. cdrom_location = self.blockDeviceComboBox.currentText()
  48. elif self.fileRadioButton.isChecked():
  49. cdrom_location = str(
  50. self.vm_list[self.fileVM.currentIndex()]) + \
  51. ":" + self.pathText.text()
  52. else:
  53. QtGui.QMessageBox.warning(
  54. None,
  55. self.tr("ERROR!"),
  56. self.tr("No file or block device selected; please select one."))
  57. return
  58. # warn user if the VM is currently running
  59. self.__warn_if_running__()
  60. qvm_start.main(['--cdrom', cdrom_location, self.vm.name])
  61. self.done(0)
  62. def __warn_if_running__(self):
  63. if self.vm.is_running():
  64. QtGui.QMessageBox.warning(
  65. None,
  66. self.tr("Warning!"),
  67. self.tr("Qube must be turned off before booting it from "
  68. "device. Please turn off the qube.")
  69. )
  70. def __init_buttons__(self):
  71. self.fileVM.setEnabled(False)
  72. self.selectFileButton.setEnabled(False)
  73. self.blockDeviceComboBox.setEnabled(False)
  74. self.blockDeviceRadioButton.clicked.connect(self.radio_button_clicked)
  75. self.fileRadioButton.clicked.connect(self.radio_button_clicked)
  76. self.selectFileButton.clicked.connect(self.select_file_dialog)
  77. self.vm_list, self.vm_idx = utils.prepare_vm_choice(
  78. self.fileVM,
  79. self.vm, None,
  80. None,
  81. None,
  82. allow_default=False, allow_none=False)
  83. self.block_list, self.block_idx = utils.prepare_choice(
  84. self.blockDeviceComboBox,
  85. self.vm,
  86. None,
  87. [device for domain in self.vm.app.domains
  88. for device in domain.devices["block"]],
  89. None,
  90. None,
  91. allow_default=False, allow_none=False
  92. )
  93. def radio_button_clicked(self):
  94. self.blockDeviceComboBox.setEnabled(
  95. self.blockDeviceRadioButton.isChecked())
  96. self.fileVM.setEnabled(self.fileRadioButton.isChecked())
  97. self.selectFileButton.setEnabled(self.fileRadioButton.isChecked())
  98. self.pathText.setEnabled(self.fileRadioButton.isChecked())
  99. def select_file_dialog(self):
  100. backend_vm = self.vm_list[self.fileVM.currentIndex()]
  101. error_occurred = False
  102. try:
  103. new_path = utils.get_path_from_vm(backend_vm, "qubes.SelectFile")
  104. except subprocess.CalledProcessError as ex:
  105. if ex.returncode != 1:
  106. # Error other than 'user did not select a file'
  107. error_occurred = True
  108. new_path = None
  109. except Exception: # pylint: disable=broad-except
  110. error_occurred = True
  111. new_path = None
  112. if error_occurred:
  113. QtGui.QMessageBox.warning(
  114. None,
  115. self.tr("Failed to display file selection dialog"),
  116. self.tr("Check if the qube {0} can be started and has a file"
  117. " manager installed.").format(backend_vm)
  118. )
  119. if new_path:
  120. self.pathText.setText(new_path)
  121. parser = tools.QubesArgumentParser(vmname_nargs=1)
  122. def main(args=None):
  123. args = parser.parse_args(args)
  124. vm = args.domains.pop()
  125. qapp = QtGui.QApplication(sys.argv)
  126. qapp.setOrganizationName('Invisible Things Lab')
  127. qapp.setOrganizationDomain("https://www.qubes-os.org/")
  128. qapp.setApplicationName("Boot Qube From Device")
  129. # if not utils.is_debug(): #FIXME
  130. # sys.excepthook = handle_exception
  131. bootfromdevice_window = VMBootFromDeviceWindow(vm, qapp)
  132. bootfromdevice_window.show()
  133. qapp.exec_()
  134. qapp.exit()
  135. if __name__ == "__main__":
  136. main()