qfile-daemon-dvm 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #!/usr/bin/python2
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2010 Rafal Wojtczuk <rafal@invisiblethingslab.com>
  6. #
  7. # This program is free software; you can redistribute it and/or
  8. # modify it under the terms of the GNU General Public License
  9. # as published by the Free Software Foundation; either version 2
  10. # of the License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program; if not, write to the Free Software
  19. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. #
  21. #
  22. import os
  23. import dbus
  24. import subprocess
  25. import sys
  26. import fcntl
  27. import shutil
  28. import time
  29. from qubes.qubes import QubesVmCollection
  30. from qubes.qubes import QubesException
  31. from qubes.qubes import QubesDaemonPidfile
  32. from qubes.qubes import QubesDispVmLabels
  33. from qubes.qmemman_client import QMemmanClient
  34. current_savefile = '/var/run/qubes/current-savefile'
  35. current_dvm_conf = '/var/run/qubes/current-dvm.conf'
  36. notify_object = None
  37. class QfileDaemonDvm:
  38. def __init__(self, name):
  39. self.name = name
  40. def do_get_dvm(self):
  41. qmemman_client = QMemmanClient()
  42. if not qmemman_client.request_memory(400*1024*1024):
  43. qmemman_client.close()
  44. errmsg = 'Not enough memory to create DVM. '
  45. errmsg +='Terminate some appVM and retry.'
  46. if os.path.exists('/usr/bin/kdialog'):
  47. subprocess.call(['/usr/bin/kdialog', '--sorry', errmsg])
  48. else:
  49. subprocess.call(['/usr/bin/zenity', '--warning', '--text', errmsg])
  50. return None
  51. self.tray_notify("Starting new DispVM...")
  52. qvm_collection = QubesVmCollection()
  53. qvm_collection.lock_db_for_writing()
  54. qvm_collection.load()
  55. vm = qvm_collection.get_vm_by_name(self.name)
  56. if vm is None:
  57. sys.stderr.write( 'Domain ' + self.name + ' does not exist ?')
  58. qvm_collection.unlock_db()
  59. qmemman_client.close()
  60. return None
  61. label = vm.label
  62. if len(sys.argv) > 4 and len(sys.argv[4]) > 0:
  63. assert sys.argv[4] in QubesDispVmLabels.keys(), "Invalid label"
  64. label = QubesDispVmLabels[sys.argv[4]]
  65. print >>sys.stderr, "time=%s, starting qubes-restore" % (str(time.time()))
  66. retcode = subprocess.call(['/usr/lib/qubes/qubes-restore',
  67. current_savefile,
  68. current_dvm_conf,
  69. '-u', str(vm.default_user),
  70. '-c', label.color,
  71. '-i', label.icon_path,
  72. '-l', str(label.index)])
  73. qmemman_client.close()
  74. if retcode != 0:
  75. tray_notify_error('DisposableVM creation failed, see qubes-restore.log')
  76. qvm_collection.unlock_db()
  77. return None
  78. f = open('/var/run/qubes/dispVM.xid', 'r');
  79. disp_xid = f.readline().rstrip('\n')
  80. disp_name = f.readline().rstrip('\n')
  81. disptempl = f.readline().rstrip('\n')
  82. f.close()
  83. print >>sys.stderr, "time=%s, adding to qubes.xml" % (str(time.time()))
  84. vm_disptempl = qvm_collection.get_vm_by_name(disptempl);
  85. if vm_disptempl is None:
  86. sys.stderr.write( 'Domain ' + disptempl + ' does not exist ?')
  87. qvm_collection.unlock_db()
  88. return None
  89. dispid=int(disp_name[4:])
  90. dispvm=qvm_collection.add_new_disposablevm(disp_name, vm_disptempl.template, label=label, dispid=dispid, netvm=vm_disptempl.netvm)
  91. # By default inherit firewall rules from calling VM
  92. if os.path.exists(vm.firewall_conf):
  93. disp_firewall_conf = '/var/run/qubes/%s-firewall.xml' % disp_name
  94. shutil.copy(vm.firewall_conf, disp_firewall_conf)
  95. dispvm.firewall_conf = disp_firewall_conf
  96. if len(sys.argv) > 5 and len(sys.argv[5]) > 0:
  97. assert os.path.exists(sys.argv[5]), "Invalid firewall.conf location"
  98. dispvm.firewall_conf = sys.argv[5]
  99. qvm_collection.save()
  100. qvm_collection.unlock_db()
  101. # Reload firewall rules
  102. print >>sys.stderr, "time=%s, reloading firewall" % (str(time.time()))
  103. for vm in qvm_collection.values():
  104. if vm.is_proxyvm() and vm.is_running():
  105. vm.write_iptables_xenstore_entry()
  106. return disp_name
  107. def dvm_setup_ok(self):
  108. dvmdata_dir = '/var/lib/qubes/dvmdata/'
  109. if not os.path.isfile(current_savefile):
  110. return False
  111. if not os.path.isfile(dvmdata_dir+'default-savefile') or not os.path.isfile(dvmdata_dir+'savefile-root'):
  112. return False
  113. dvm_mtime = os.stat(current_savefile).st_mtime
  114. root_mtime = os.stat(dvmdata_dir+'savefile-root').st_mtime
  115. if dvm_mtime < root_mtime:
  116. template_name = os.path.basename(os.path.dirname(os.readlink(dvmdata_dir+'savefile-root')))
  117. if subprocess.call(["xl", "domid", template_name]) == 0:
  118. self.tray_notify("For optimum performance, you should not start DispVM when its template is running.")
  119. return False
  120. return True
  121. def tray_notify(self, str, timeout = 3000):
  122. if notify_object:
  123. notify_object.Notify("Qubes", 0, "red", "Qubes", str, [], [], timeout, dbus_interface="org.freedesktop.Notifications")
  124. def tray_notify_error(self, str, timeout = 3000):
  125. if notify_object:
  126. notify_object.Notify("Qubes", 0, "dialog-error", "Qubes", str, [], [], timeout, dbus_interface="org.freedesktop.Notifications")
  127. def get_dvm(self):
  128. if not self.dvm_setup_ok():
  129. if os.system("/usr/lib/qubes/qubes-update-dispvm-savefile-with-progress.sh >/dev/null </dev/null" ) != 0:
  130. self.tray_notify_error("DVM savefile creation failed")
  131. return None
  132. return self.do_get_dvm()
  133. def remove_disposable_from_qdb(self, name):
  134. qvm_collection = QubesVmCollection()
  135. qvm_collection.lock_db_for_writing()
  136. qvm_collection.load()
  137. vm = qvm_collection.get_vm_by_name(name)
  138. if vm is None:
  139. qvm_collection.unlock_db()
  140. return False
  141. qvm_collection.pop(vm.qid)
  142. qvm_collection.save()
  143. qvm_collection.unlock_db()
  144. def main():
  145. global notify_object
  146. exec_index = sys.argv[1]
  147. src_vmname = sys.argv[2]
  148. user = sys.argv[3]
  149. #accessed directly by get_dvm()
  150. # sys.argv[4] - override label
  151. # sys.argv[5] - override firewall
  152. print >>sys.stderr, "time=%s, qfile-daemon-dvm init" % (str(time.time()))
  153. try:
  154. notify_object = dbus.SessionBus().get_object("org.freedesktop.Notifications", "/org/freedesktop/Notifications")
  155. except dbus.DBusException as e:
  156. print >>sys.stderr, "Warning: notification service not available: %s" % str(e)
  157. notify_object = None
  158. print >>sys.stderr, "time=%s, creating DispVM" % (str(time.time()))
  159. qfile = QfileDaemonDvm(src_vmname)
  160. lockf = open("/var/run/qubes/qfile-daemon-dvm.lock", 'a')
  161. fcntl.fcntl(lockf, fcntl.F_SETFD, fcntl.FD_CLOEXEC)
  162. fcntl.flock(lockf, fcntl.LOCK_EX)
  163. dispname = qfile.get_dvm()
  164. lockf.close()
  165. if dispname is not None:
  166. print >>sys.stderr, "time=%s, starting VM process" % (str(time.time()))
  167. subprocess.call(['/usr/lib/qubes/qrexec-client', '-d', dispname,
  168. user+':exec /usr/lib/qubes/qubes-rpc-multiplexer ' + exec_index + " " + src_vmname])
  169. subprocess.call(['/usr/sbin/xl', 'destroy', dispname])
  170. qfile.remove_disposable_from_qdb(dispname)
  171. main()