qfile-daemon-dvm 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. if os.path.exists('/usr/bin/kdialog'):
  76. subprocess.call(['/usr/bin/kdialog', '--sorry', 'DisposableVM creation failed, see qubes-restore.log'])
  77. else:
  78. subprocess.call(['/usr/bin/zenity', '--warning', '--text', 'DisposableVM creation failed, see qubes-restore.log'])
  79. qvm_collection.unlock_db()
  80. return None
  81. f = open('/var/run/qubes/dispVM.xid', 'r');
  82. disp_xid = f.readline().rstrip('\n')
  83. disp_name = f.readline().rstrip('\n')
  84. disptempl = f.readline().rstrip('\n')
  85. f.close()
  86. print >>sys.stderr, "time=%s, adding to qubes.xml" % (str(time.time()))
  87. vm_disptempl = qvm_collection.get_vm_by_name(disptempl);
  88. if vm_disptempl is None:
  89. sys.stderr.write( 'Domain ' + disptempl + ' does not exist ?')
  90. qvm_collection.unlock_db()
  91. return None
  92. dispid=int(disp_name[4:])
  93. dispvm=qvm_collection.add_new_disposablevm(disp_name, vm_disptempl.template, label=label, dispid=dispid, netvm=vm_disptempl.netvm)
  94. # By default inherit firewall rules from calling VM
  95. if os.path.exists(vm.firewall_conf):
  96. disp_firewall_conf = '/var/run/qubes/%s-firewall.xml' % disp_name
  97. shutil.copy(vm.firewall_conf, disp_firewall_conf)
  98. dispvm.firewall_conf = disp_firewall_conf
  99. if len(sys.argv) > 5 and len(sys.argv[5]) > 0:
  100. assert os.path.exists(sys.argv[5]), "Invalid firewall.conf location"
  101. dispvm.firewall_conf = sys.argv[5]
  102. qvm_collection.save()
  103. qvm_collection.unlock_db()
  104. # Reload firewall rules
  105. print >>sys.stderr, "time=%s, reloading firewall" % (str(time.time()))
  106. for vm in qvm_collection.values():
  107. if vm.is_proxyvm() and vm.is_running():
  108. vm.write_iptables_xenstore_entry()
  109. return disp_name
  110. def dvm_setup_ok(self):
  111. dvmdata_dir = '/var/lib/qubes/dvmdata/'
  112. if not os.path.isfile(current_savefile):
  113. return False
  114. if not os.path.isfile(dvmdata_dir+'default-savefile') or not os.path.isfile(dvmdata_dir+'savefile-root'):
  115. return False
  116. dvm_mtime = os.stat(current_savefile).st_mtime
  117. root_mtime = os.stat(dvmdata_dir+'savefile-root').st_mtime
  118. if dvm_mtime < root_mtime:
  119. template_name = os.path.basename(os.path.dirname(os.readlink(dvmdata_dir+'savefile-root')))
  120. if subprocess.call(["xl", "domid", template_name]) == 0:
  121. self.tray_notify("For optimum performance, you should not start DispVM when its template is running.")
  122. return False
  123. return True
  124. def tray_notify(self, str, timeout = 3000):
  125. notify_object.Notify("Qubes", 0, "red", "Qubes", str, [], [], timeout, dbus_interface="org.freedesktop.Notifications")
  126. def tray_notify_error(self, str, timeout = 3000):
  127. notify_object.Notify("Qubes", 0, "dialog-error", "Qubes", str, [], [], timeout, dbus_interface="org.freedesktop.Notifications")
  128. def get_dvm(self):
  129. if not self.dvm_setup_ok():
  130. if os.system("/usr/lib/qubes/qubes-update-dispvm-savefile-with-progress.sh >/dev/null </dev/null" ) != 0:
  131. self.tray_notify_error("DVM savefile creation failed")
  132. return None
  133. return self.do_get_dvm()
  134. def remove_disposable_from_qdb(self, name):
  135. qvm_collection = QubesVmCollection()
  136. qvm_collection.lock_db_for_writing()
  137. qvm_collection.load()
  138. vm = qvm_collection.get_vm_by_name(name)
  139. if vm is None:
  140. qvm_collection.unlock_db()
  141. return False
  142. qvm_collection.pop(vm.qid)
  143. qvm_collection.save()
  144. qvm_collection.unlock_db()
  145. def main():
  146. global notify_object
  147. exec_index = sys.argv[1]
  148. src_vmname = sys.argv[2]
  149. user = sys.argv[3]
  150. #accessed directly by get_dvm()
  151. # sys.argv[4] - override label
  152. # sys.argv[5] - override firewall
  153. print >>sys.stderr, "time=%s, qfile-daemon-dvm init" % (str(time.time()))
  154. notify_object = dbus.SessionBus().get_object("org.freedesktop.Notifications", "/org/freedesktop/Notifications")
  155. print >>sys.stderr, "time=%s, creating DispVM" % (str(time.time()))
  156. qfile = QfileDaemonDvm(src_vmname)
  157. lockf = open("/var/run/qubes/qfile-daemon-dvm.lock", 'a')
  158. fcntl.fcntl(lockf, fcntl.F_SETFD, fcntl.FD_CLOEXEC)
  159. fcntl.flock(lockf, fcntl.LOCK_EX)
  160. dispname = qfile.get_dvm()
  161. lockf.close()
  162. if dispname is not None:
  163. print >>sys.stderr, "time=%s, starting VM process" % (str(time.time()))
  164. subprocess.call(['/usr/lib/qubes/qrexec-client', '-d', dispname,
  165. user+':exec /usr/lib/qubes/qubes-rpc-multiplexer ' + exec_index + " " + src_vmname])
  166. subprocess.call(['/usr/sbin/xl', 'destroy', dispname])
  167. qfile.remove_disposable_from_qdb(dispname)
  168. main()