qfile-daemon-dvm 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. return False
  120. return True
  121. def tray_notify(self, str, timeout = 3000):
  122. notify_object.Notify("Qubes", 0, "red", "Qubes", str, [], [], timeout, dbus_interface="org.freedesktop.Notifications")
  123. def tray_notify_error(self, str, timeout = 3000):
  124. notify_object.Notify("Qubes", 0, "dialog-error", "Qubes", str, [], [], timeout, dbus_interface="org.freedesktop.Notifications")
  125. def get_dvm(self):
  126. if not self.dvm_setup_ok():
  127. if os.system("/usr/lib/qubes/qubes-update-dispvm-savefile-with-progress.sh >/dev/null </dev/null" ) != 0:
  128. self.tray_notify_error("DVM savefile creation failed")
  129. return None
  130. return self.do_get_dvm()
  131. def remove_disposable_from_qdb(self, name):
  132. qvm_collection = QubesVmCollection()
  133. qvm_collection.lock_db_for_writing()
  134. qvm_collection.load()
  135. vm = qvm_collection.get_vm_by_name(name)
  136. if vm is None:
  137. qvm_collection.unlock_db()
  138. return False
  139. qvm_collection.pop(vm.qid)
  140. qvm_collection.save()
  141. qvm_collection.unlock_db()
  142. def main():
  143. global notify_object
  144. exec_index = sys.argv[1]
  145. src_vmname = sys.argv[2]
  146. user = sys.argv[3]
  147. #accessed directly by get_dvm()
  148. # sys.argv[4] - override label
  149. # sys.argv[5] - override firewall
  150. print >>sys.stderr, "time=%s, qfile-daemon-dvm init" % (str(time.time()))
  151. notify_object = dbus.SessionBus().get_object("org.freedesktop.Notifications", "/org/freedesktop/Notifications")
  152. print >>sys.stderr, "time=%s, creating DispVM" % (str(time.time()))
  153. qfile = QfileDaemonDvm(src_vmname)
  154. lockf = open("/var/run/qubes/qfile-daemon-dvm.lock", 'a')
  155. fcntl.fcntl(lockf, fcntl.F_SETFD, fcntl.FD_CLOEXEC)
  156. fcntl.flock(lockf, fcntl.LOCK_EX)
  157. dispname = qfile.get_dvm()
  158. lockf.close()
  159. if dispname is not None:
  160. print >>sys.stderr, "time=%s, starting VM process" % (str(time.time()))
  161. subprocess.call(['/usr/lib/qubes/qrexec-client', '-d', dispname,
  162. user+':exec /usr/lib/qubes/qubes-rpc-multiplexer ' + exec_index + " " + src_vmname])
  163. subprocess.call(['/usr/sbin/xl', 'destroy', dispname])
  164. qfile.remove_disposable_from_qdb(dispname)
  165. main()