create_new_vm.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #!/usr/bin/python2
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2012 Agnieszka Kostrzewa <agnieszka.kostrzewa@gmail.com>
  6. # Copyright (C) 2012 Marek Marczykowski <marmarek@mimuw.edu.pl>
  7. #
  8. # This program is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU General Public License
  10. # as published by the Free Software Foundation; either version 2
  11. # of the License, or (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program; if not, write to the Free Software
  20. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  21. #
  22. #
  23. import sys
  24. import os
  25. from PyQt4.QtCore import *
  26. from PyQt4.QtGui import *
  27. from qubes.qubes import QubesVmCollection
  28. from qubes.qubes import QubesVmLabels
  29. from qubes.qubes import QubesException
  30. import qubesmanager.resources_rc
  31. from pyinotify import WatchManager, Notifier, ThreadedNotifier, EventsCodes, ProcessEvent
  32. import subprocess
  33. import time
  34. import threading
  35. from ui_newappvmdlg import *
  36. from thread_monitor import *
  37. class NewVmDlg (QDialog, Ui_NewVMDlg):
  38. def __init__(self, app, qvm_collection, trayIcon, parent = None):
  39. super (NewVmDlg, self).__init__(parent)
  40. self.setupUi(self)
  41. self.app = app
  42. self.trayIcon = trayIcon
  43. self.qvm_collection = qvm_collection
  44. # Theoretically we should be locking for writing here and unlock
  45. # only after the VM creation finished. But the code would be more messy...
  46. # Instead we lock for writing in the actual worker thread
  47. try:
  48. from qubes.qubes import QubesHVm
  49. except ImportError:
  50. pass
  51. else:
  52. self.hvm_radio.setEnabled(True)
  53. self.qvm_collection.lock_db_for_reading()
  54. self.qvm_collection.load()
  55. self.qvm_collection.unlock_db()
  56. self.label_list = QubesVmLabels.values()
  57. self.label_list.sort(key=lambda l: l.index)
  58. for (i, label) in enumerate(self.label_list):
  59. self.vmlabel.insertItem(i, label.name)
  60. self.vmlabel.setItemIcon (i, QIcon(label.icon_path))
  61. self.template_vm_list = [vm for vm in self.qvm_collection.values() if not vm.internal and vm.is_template()]
  62. default_index = 0
  63. for (i, vm) in enumerate(self.template_vm_list):
  64. if vm is self.qvm_collection.get_default_template():
  65. default_index = i
  66. self.template_name.insertItem(i, vm.name + " (default)")
  67. else:
  68. self.template_name.insertItem(i, vm.name)
  69. self.template_name.setCurrentIndex(default_index)
  70. self.vmname.setValidator(QRegExpValidator(QRegExp("[a-zA-Z0-9-]*", Qt.CaseInsensitive), None))
  71. self.vmname.selectAll()
  72. self.vmname.setFocus()
  73. def on_appvm_radio_toggled(self, checked):
  74. if checked:
  75. self.template_name.setEnabled(True)
  76. self.allow_networking.setEnabled(True)
  77. def on_netvm_radio_toggled(self, checked):
  78. if checked:
  79. self.template_name.setEnabled(True)
  80. self.allow_networking.setEnabled(False)
  81. def on_proxyvm_radio_toggled(self, checked):
  82. if checked:
  83. self.template_name.setEnabled(True)
  84. self.allow_networking.setEnabled(True)
  85. def on_hvm_radio_toggled(self, checked):
  86. if checked:
  87. self.template_name.setEnabled(False)
  88. self.standalone.setEnabled(False)
  89. self.standalone.setChecked(True)
  90. self.allow_networking.setEnabled(True)
  91. else:
  92. self.standalone.setEnabled(True)
  93. self.standalone.setChecked(False)
  94. def reject(self):
  95. self.done(0)
  96. def accept(self):
  97. vmname = str(self.vmname.text())
  98. if self.qvm_collection.get_vm_by_name(vmname) is not None:
  99. QMessageBox.warning (None, "Incorrect AppVM Name!", "A VM with the name <b>{0}</b> already exists in the system!".format(vmname))
  100. return
  101. label = self.label_list[self.vmlabel.currentIndex()]
  102. template_vm = None
  103. if self.template_name.isEnabled():
  104. template_vm = self.template_vm_list[self.template_name.currentIndex()]
  105. standalone = self.standalone.isChecked()
  106. allow_networking = None
  107. if self.allow_networking.isEnabled():
  108. allow_networking = self.allow_networking.isChecked()
  109. if self.appvm_radio.isChecked():
  110. createvm_method = self.qvm_collection.add_new_appvm
  111. vmtype = "AppVM"
  112. elif self.netvm_radio.isChecked():
  113. createvm_method = self.qvm_collection.add_new_netvm
  114. vmtype = "NetVM"
  115. elif self.proxyvm_radio.isChecked():
  116. createvm_method = self.qvm_collection.add_new_proxyvm
  117. vmtype = "ProxyVM"
  118. else: #hvm_radio.isChecked()
  119. createvm_method = self.qvm_collection.add_new_hvm
  120. vmtype = "HVM"
  121. thread_monitor = ThreadMonitor()
  122. thread = threading.Thread (target=self.do_create_vm, args=(createvm_method, vmname, label, template_vm, standalone, allow_networking, thread_monitor))
  123. thread.daemon = True
  124. thread.start()
  125. progress = QProgressDialog ("Creating new {0} <b>{1}</b>...".format(vmtype, vmname), "", 0, 0)
  126. progress.setCancelButton(None)
  127. progress.setModal(True)
  128. progress.show()
  129. while not thread_monitor.is_finished():
  130. self.app.processEvents()
  131. time.sleep (0.1)
  132. progress.hide()
  133. if thread_monitor.success:
  134. self.trayIcon.showMessage ("VM '{0}' has been created.".format(vmname), msecs=3000)
  135. else:
  136. QMessageBox.warning (None, "Error creating AppVM!", "ERROR: {0}".format(thread_monitor.error_msg))
  137. self.done(0)
  138. def do_create_vm (self, createvm_method, vmname, label, template_vm, standalone, allow_networking, thread_monitor):
  139. vm = None
  140. try:
  141. self.qvm_collection.lock_db_for_writing()
  142. self.qvm_collection.load()
  143. if template_vm is not None:
  144. if not standalone:
  145. vm = createvm_method(vmname, template_vm, label = label)
  146. else:
  147. vm = createvm_method(vmname, None, label = label)
  148. vm.create_on_disk(verbose=False, source_template = template_vm)
  149. else:
  150. vm = createvm_method(vmname, label = label)
  151. vm.create_on_disk(verbose=False)
  152. if allow_networking == False:
  153. vm.uses_default_netvm = False
  154. vm.netvm = None
  155. self.qvm_collection.save()
  156. except Exception as ex:
  157. thread_monitor.set_error_msg (str(ex))
  158. if vm:
  159. vm.remove_from_disk()
  160. finally:
  161. self.qvm_collection.unlock_db()
  162. thread_monitor.set_finished()