create_new_vm.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #!/usr/bin/python2.6
  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.selectAll()
  71. self.vmname.setFocus()
  72. def on_appvm_radio_toggled(self, checked):
  73. if checked:
  74. self.template_name.setEnabled(True)
  75. self.allow_networking.setEnabled(True)
  76. def on_netvm_radio_toggled(self, checked):
  77. if checked:
  78. self.template_name.setEnabled(True)
  79. self.allow_networking.setEnabled(False)
  80. def on_proxyvm_radio_toggled(self, checked):
  81. if checked:
  82. self.template_name.setEnabled(True)
  83. self.allow_networking.setEnabled(True)
  84. def on_hvm_radio_toggled(self, checked):
  85. if checked:
  86. self.template_name.setEnabled(False)
  87. self.allow_networking.setEnabled(True)
  88. def reject(self):
  89. self.done(0)
  90. def accept(self):
  91. vmname = str(self.vmname.text())
  92. if self.qvm_collection.get_vm_by_name(vmname) is not None:
  93. QMessageBox.warning (None, "Incorrect AppVM Name!", "A VM with the name <b>{0}</b> already exists in the system!".format(vmname))
  94. return
  95. label = self.label_list[self.vmlabel.currentIndex()]
  96. template_vm = None
  97. if self.template_name.isEnabled():
  98. template_vm = self.template_vm_list[self.template_name.currentIndex()]
  99. allow_networking = None
  100. if self.allow_networking.isEnabled():
  101. allow_networking = self.allow_networking.isChecked()
  102. if self.appvm_radio.isChecked():
  103. createvm_method = self.qvm_collection.add_new_appvm
  104. vmtype = "AppVM"
  105. elif self.netvm_radio.isChecked():
  106. createvm_method = self.qvm_collection.add_new_netvm
  107. vmtype = "NetVM"
  108. elif self.proxyvm_radio.isChecked():
  109. createvm_method = self.qvm_collection.add_new_proxyvm
  110. vmtype = "ProxyVM"
  111. else: #hvm_radio.isChecked()
  112. createvm_method = self.qvm_collection.add_new_hvm
  113. vmtype = "HVM"
  114. thread_monitor = ThreadMonitor()
  115. thread = threading.Thread (target=self.do_create_vm, args=(createvm_method, vmname, label, template_vm, allow_networking, thread_monitor))
  116. thread.daemon = True
  117. thread.start()
  118. progress = QProgressDialog ("Creating new {0} <b>{1}</b>...".format(vmtype, vmname), "", 0, 0)
  119. progress.setCancelButton(None)
  120. progress.setModal(True)
  121. progress.show()
  122. while not thread_monitor.is_finished():
  123. self.app.processEvents()
  124. time.sleep (0.1)
  125. progress.hide()
  126. if thread_monitor.success:
  127. self.trayIcon.showMessage ("Qubes VM Manager", "VM '{0}' has been created.".format(vmname), msecs=3000)
  128. else:
  129. QMessageBox.warning (None, "Error creating AppVM!", "ERROR: {0}".format(thread_monitor.error_msg))
  130. self.done(0)
  131. def do_create_vm (self, createvm_method, vmname, label, template_vm, allow_networking, thread_monitor):
  132. vm = None
  133. try:
  134. self.qvm_collection.lock_db_for_writing()
  135. self.qvm_collection.load()
  136. if template_vm is not None:
  137. vm = createvm_method(vmname, template_vm, label = label)
  138. vm.create_on_disk(verbose=False, source_template = template_vm)
  139. else:
  140. vm = createvm_method(vmname, label = label)
  141. vm.create_on_disk(verbose=False)
  142. if allow_networking is not None:
  143. firewall = vm.get_firewall_conf()
  144. firewall["allow"] = allow_networking
  145. firewall["allowDns"] = allow_networking
  146. vm.write_firewall_conf(firewall)
  147. self.qvm_collection.save()
  148. except Exception as ex:
  149. thread_monitor.set_error_msg (str(ex))
  150. if vm:
  151. vm.remove_from_disk()
  152. finally:
  153. self.qvm_collection.unlock_db()
  154. thread_monitor.set_finished()