bootfromdevice.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 PyQt5 import QtWidgets # 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, parent=None):
  29. super(VMBootFromDeviceWindow, self).__init__(parent)
  30. self.vm = vm
  31. self.qapp = qapp
  32. self.setupUi(self)
  33. self.setWindowTitle(
  34. self.tr("Boot {vm} from device").format(vm=self.vm.name))
  35. self.buttonBox.accepted.connect(self.save_and_apply)
  36. self.buttonBox.rejected.connect(self.reject)
  37. # populate buttons and such
  38. self.__init_buttons__()
  39. # warn user if the VM is currently running
  40. self.__warn_if_running__()
  41. def reject(self):
  42. self.done(0)
  43. def save_and_apply(self):
  44. if self.blockDeviceRadioButton.isChecked():
  45. cdrom_location = self.blockDeviceComboBox.currentText()
  46. elif self.fileRadioButton.isChecked():
  47. cdrom_location = str(
  48. self.vm_list[self.fileVM.currentIndex()]) + \
  49. ":" + self.pathText.text()
  50. else:
  51. QtWidgets.QMessageBox.warning(
  52. self,
  53. self.tr("ERROR!"),
  54. self.tr("No file or block device selected; please select one."))
  55. return
  56. # warn user if the VM is currently running
  57. self.__warn_if_running__()
  58. qvm_start.main(['--cdrom', cdrom_location, self.vm.name])
  59. self.done(0)
  60. def __warn_if_running__(self):
  61. if self.vm.is_running():
  62. QtWidgets.QMessageBox.warning(
  63. self,
  64. self.tr("Warning!"),
  65. self.tr("Qube must be turned off before booting it from "
  66. "device. Please turn off the qube.")
  67. )
  68. def __init_buttons__(self):
  69. self.fileVM.setEnabled(False)
  70. self.selectFileButton.setEnabled(False)
  71. self.blockDeviceComboBox.setEnabled(False)
  72. self.blockDeviceRadioButton.clicked.connect(self.radio_button_clicked)
  73. self.fileRadioButton.clicked.connect(self.radio_button_clicked)
  74. self.selectFileButton.clicked.connect(self.select_file_dialog)
  75. self.vm_list, self.vm_idx = utils.prepare_vm_choice(
  76. self.fileVM,
  77. self.vm, None,
  78. None,
  79. None,
  80. allow_default=False, allow_none=False)
  81. self.block_list, self.block_idx = utils.prepare_choice(
  82. self.blockDeviceComboBox,
  83. self.vm,
  84. None,
  85. [device for domain in self.vm.app.domains
  86. for device in domain.devices["block"]],
  87. None,
  88. None,
  89. allow_default=False, allow_none=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.vm_list[self.fileVM.currentIndex()]
  99. try:
  100. new_path = utils.get_path_from_vm(backend_vm, "qubes.SelectFile")
  101. except subprocess.CalledProcessError:
  102. new_path = None
  103. if new_path:
  104. self.pathText.setText(new_path)
  105. parser = tools.QubesArgumentParser(vmname_nargs=1)
  106. def main(args=None):
  107. args = parser.parse_args(args)
  108. vm = args.domains.pop()
  109. qapp = QtWidgets.QApplication(sys.argv)
  110. qapp.setOrganizationName('Invisible Things Lab')
  111. qapp.setOrganizationDomain("https://www.qubes-os.org/")
  112. qapp.setApplicationName("Boot Qube From Device")
  113. # if not utils.is_debug(): #FIXME
  114. # sys.excepthook = handle_exception
  115. bootfromdevice_window = VMBootFromDeviceWindow(vm, qapp)
  116. bootfromdevice_window.show()
  117. qapp.exec_()
  118. qapp.exit()
  119. if __name__ == "__main__":
  120. main()