qfile-daemon-dvm 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 subprocess
  24. import sys
  25. import fcntl
  26. import shutil
  27. import time
  28. from qubes.qubes import QubesVmCollection
  29. from qubes.qubes import QubesException
  30. from qubes.qubes import QubesDaemonPidfile
  31. from qubes.qubes import QubesDispVmLabels
  32. from qubes.qmemman_client import QMemmanClient
  33. from qubes.notify import tray_notify,tray_notify_error,tray_notify_init
  34. current_savefile = '/var/run/qubes/current-savefile'
  35. current_dvm_conf = '/var/run/qubes/current-dvm.conf'
  36. class QfileDaemonDvm:
  37. def __init__(self, name):
  38. self.name = name
  39. def do_get_dvm(self):
  40. qmemman_client = QMemmanClient()
  41. if not qmemman_client.request_memory(400*1024*1024):
  42. qmemman_client.close()
  43. errmsg = 'Not enough memory to create DVM. '
  44. errmsg +='Terminate some appVM and retry.'
  45. tray_notify_error(errmsg)
  46. return None
  47. tray_notify("Starting new DispVM...", "red")
  48. qvm_collection = QubesVmCollection()
  49. qvm_collection.lock_db_for_writing()
  50. qvm_collection.load()
  51. vm = qvm_collection.get_vm_by_name(self.name)
  52. if vm is None:
  53. sys.stderr.write( 'Domain ' + self.name + ' does not exist ?')
  54. qvm_collection.unlock_db()
  55. qmemman_client.close()
  56. return None
  57. label = vm.label
  58. if len(sys.argv) > 4 and len(sys.argv[4]) > 0:
  59. assert sys.argv[4] in QubesDispVmLabels.keys(), "Invalid label"
  60. label = QubesDispVmLabels[sys.argv[4]]
  61. print >>sys.stderr, "time=%s, starting qubes-restore" % (str(time.time()))
  62. retcode = subprocess.call(['/usr/lib/qubes/qubes-restore',
  63. current_savefile,
  64. current_dvm_conf,
  65. '-u', str(vm.default_user),
  66. '-c', label.color,
  67. '-i', label.icon_path,
  68. '-l', str(label.index)])
  69. qmemman_client.close()
  70. if retcode != 0:
  71. tray_notify_error('DisposableVM creation failed, see qubes-restore.log')
  72. qvm_collection.unlock_db()
  73. return None
  74. f = open('/var/run/qubes/dispVM.xid', 'r');
  75. disp_xid = f.readline().rstrip('\n')
  76. disp_name = f.readline().rstrip('\n')
  77. disptempl = f.readline().rstrip('\n')
  78. f.close()
  79. print >>sys.stderr, "time=%s, adding to qubes.xml" % (str(time.time()))
  80. vm_disptempl = qvm_collection.get_vm_by_name(disptempl);
  81. if vm_disptempl is None:
  82. sys.stderr.write( 'Domain ' + disptempl + ' does not exist ?')
  83. qvm_collection.unlock_db()
  84. return None
  85. dispid=int(disp_name[4:])
  86. dispvm=qvm_collection.add_new_disposablevm(disp_name, vm_disptempl.template, label=label, dispid=dispid, netvm=vm_disptempl.netvm)
  87. # By default inherit firewall rules from calling VM
  88. if os.path.exists(vm.firewall_conf):
  89. disp_firewall_conf = '/var/run/qubes/%s-firewall.xml' % disp_name
  90. shutil.copy(vm.firewall_conf, disp_firewall_conf)
  91. dispvm.firewall_conf = disp_firewall_conf
  92. if len(sys.argv) > 5 and len(sys.argv[5]) > 0:
  93. assert os.path.exists(sys.argv[5]), "Invalid firewall.conf location"
  94. dispvm.firewall_conf = sys.argv[5]
  95. qvm_collection.save()
  96. qvm_collection.unlock_db()
  97. # Reload firewall rules
  98. print >>sys.stderr, "time=%s, reloading firewall" % (str(time.time()))
  99. for vm in qvm_collection.values():
  100. if vm.is_proxyvm() and vm.is_running():
  101. vm.write_iptables_xenstore_entry()
  102. return disp_name
  103. def dvm_setup_ok(self):
  104. dvmdata_dir = '/var/lib/qubes/dvmdata/'
  105. if not os.path.isfile(current_savefile):
  106. return False
  107. if not os.path.isfile(dvmdata_dir+'default-savefile') or not os.path.isfile(dvmdata_dir+'savefile-root'):
  108. return False
  109. dvm_mtime = os.stat(current_savefile).st_mtime
  110. root_mtime = os.stat(dvmdata_dir+'savefile-root').st_mtime
  111. if dvm_mtime < root_mtime:
  112. template_name = os.path.basename(os.path.dirname(os.readlink(dvmdata_dir+'savefile-root')))
  113. if subprocess.call(["xl", "domid", template_name]) == 0:
  114. tray_notify("For optimum performance, you should not "
  115. "start DispVM when its template is running.", "red")
  116. return False
  117. return True
  118. def get_dvm(self):
  119. if not self.dvm_setup_ok():
  120. if os.system("/usr/lib/qubes/qubes-update-dispvm-savefile-with-progress.sh >/dev/null </dev/null" ) != 0:
  121. tray_notify_error("DVM savefile creation failed")
  122. return None
  123. return self.do_get_dvm()
  124. def remove_disposable_from_qdb(self, name):
  125. qvm_collection = QubesVmCollection()
  126. qvm_collection.lock_db_for_writing()
  127. qvm_collection.load()
  128. vm = qvm_collection.get_vm_by_name(name)
  129. if vm is None:
  130. qvm_collection.unlock_db()
  131. return False
  132. qvm_collection.pop(vm.qid)
  133. qvm_collection.save()
  134. qvm_collection.unlock_db()
  135. def main():
  136. global notify_object
  137. exec_index = sys.argv[1]
  138. src_vmname = sys.argv[2]
  139. user = sys.argv[3]
  140. #accessed directly by get_dvm()
  141. # sys.argv[4] - override label
  142. # sys.argv[5] - override firewall
  143. print >>sys.stderr, "time=%s, qfile-daemon-dvm init" % (str(time.time()))
  144. tray_notify_init()
  145. print >>sys.stderr, "time=%s, creating DispVM" % (str(time.time()))
  146. qfile = QfileDaemonDvm(src_vmname)
  147. lockf = open("/var/run/qubes/qfile-daemon-dvm.lock", 'a')
  148. fcntl.fcntl(lockf, fcntl.F_SETFD, fcntl.FD_CLOEXEC)
  149. fcntl.flock(lockf, fcntl.LOCK_EX)
  150. dispname = qfile.get_dvm()
  151. lockf.close()
  152. if dispname is not None:
  153. print >>sys.stderr, "time=%s, starting VM process" % (str(time.time()))
  154. subprocess.call(['/usr/lib/qubes/qrexec-client', '-d', dispname,
  155. user+':exec /usr/lib/qubes/qubes-rpc-multiplexer ' + exec_index + " " + src_vmname])
  156. subprocess.call(['/usr/sbin/xl', 'destroy', dispname])
  157. qfile.remove_disposable_from_qdb(dispname)
  158. main()