qfile-daemon-dvm 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #!/usr/bin/python2
  2. # coding=utf-8
  3. #
  4. # The Qubes OS Project, http://www.qubes-os.org
  5. #
  6. # Copyright (C) 2010 Rafal Wojtczuk <rafal@invisiblethingslab.com>
  7. # Copyright (C) 2013-2015 Marek Marczykowski-Górecki
  8. # <marmarek@invisiblethingslab.com>
  9. #
  10. # This program is free software; you can redistribute it and/or
  11. # modify it under the terms of the GNU General Public License
  12. # as published by the Free Software Foundation; either version 2
  13. # of the License, or (at your option) any later version.
  14. #
  15. # This program is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. # GNU General Public License for more details.
  19. #
  20. # You should have received a copy of the GNU General Public License
  21. # along with this program; if not, write to the Free Software
  22. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  23. #
  24. #
  25. import os
  26. import subprocess
  27. import sys
  28. import shutil
  29. import time
  30. from qubes.qubes import QubesVmCollection, QubesException
  31. from qubes.qubes import QubesDispVmLabels
  32. from qubes.notify import tray_notify, tray_notify_error, tray_notify_init
  33. current_savefile = '/var/run/qubes/current-savefile'
  34. current_savefile_vmdir = '/var/lib/qubes/dvmdata/vmdir'
  35. class QfileDaemonDvm:
  36. def __init__(self, name):
  37. self.name = name
  38. @staticmethod
  39. def get_disp_templ():
  40. vmdir = os.readlink(current_savefile_vmdir)
  41. return vmdir.split('/')[-1]
  42. def do_get_dvm(self):
  43. tray_notify("Starting new DispVM...", "red")
  44. qvm_collection = QubesVmCollection()
  45. qvm_collection.lock_db_for_writing()
  46. try:
  47. tar_process = subprocess.Popen(
  48. ['bsdtar', '-C', current_savefile_vmdir,
  49. '-xSUf', os.path.join(current_savefile_vmdir, 'saved-cows.tar')])
  50. qvm_collection.load()
  51. print >>sys.stderr, "time=%s, collection loaded" % (str(time.time()))
  52. vm = qvm_collection.get_vm_by_name(self.name)
  53. if vm is None:
  54. sys.stderr.write('Domain ' + self.name + ' does not exist ?')
  55. return None
  56. label = vm.label
  57. if len(sys.argv) > 4 and len(sys.argv[4]) > 0:
  58. assert sys.argv[4] in QubesDispVmLabels.keys(), "Invalid label"
  59. label = QubesDispVmLabels[sys.argv[4]]
  60. disp_templ = self.get_disp_templ()
  61. vm_disptempl = qvm_collection.get_vm_by_name(disp_templ)
  62. if vm_disptempl is None:
  63. sys.stderr.write('Domain ' + disp_templ + ' does not exist ?')
  64. return None
  65. dispvm = qvm_collection.add_new_vm('QubesDisposableVm',
  66. disp_template=vm_disptempl,
  67. label=label)
  68. print >>sys.stderr, "time=%s, VM created" % (str(time.time()))
  69. # By default inherit firewall rules from calling VM
  70. disp_firewall_conf = '/var/run/qubes/%s-firewall.xml' % dispvm.name
  71. dispvm.firewall_conf = disp_firewall_conf
  72. if os.path.exists(vm.firewall_conf):
  73. shutil.copy(vm.firewall_conf, disp_firewall_conf)
  74. elif vm.qid == 0 and os.path.exists(vm_disptempl.firewall_conf):
  75. # for DispVM called from dom0, copy use rules from DispVM template
  76. shutil.copy(vm_disptempl.firewall_conf, disp_firewall_conf)
  77. if len(sys.argv) > 5 and len(sys.argv[5]) > 0:
  78. assert os.path.exists(sys.argv[5]), "Invalid firewall.conf location"
  79. dispvm.firewall_conf = sys.argv[5]
  80. if vm.qid != 0:
  81. dispvm.uses_default_netvm = False
  82. # netvm can be changed before restore,
  83. # but cannot be enabled/disabled
  84. if (dispvm.netvm is None) == (vm.dispvm_netvm is None):
  85. dispvm.netvm = vm.dispvm_netvm
  86. # Wait for tar to finish
  87. if tar_process.wait() != 0:
  88. sys.stderr.write('Failed to unpack saved-cows.tar')
  89. return None
  90. print >>sys.stderr, "time=%s, VM starting" % (str(time.time()))
  91. try:
  92. dispvm.start()
  93. except (MemoryError, QubesException) as e:
  94. tray_notify_error(str(e))
  95. raise
  96. if vm.qid != 0:
  97. # if need to enable/disable netvm, do it while DispVM is alive
  98. if (dispvm.netvm is None) != (vm.dispvm_netvm is None):
  99. dispvm.netvm = vm.dispvm_netvm
  100. print >>sys.stderr, "time=%s, VM started" % (str(time.time()))
  101. qvm_collection.save()
  102. finally:
  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_qubesdb_entry()
  109. return dispvm
  110. @staticmethod
  111. def dvm_setup_ok():
  112. dvmdata_dir = '/var/lib/qubes/dvmdata/'
  113. if not os.path.isfile(current_savefile):
  114. return False
  115. if not os.path.isfile(dvmdata_dir+'default-savefile') or \
  116. not os.path.isfile(dvmdata_dir+'savefile-root'):
  117. return False
  118. dvm_mtime = os.stat(current_savefile).st_mtime
  119. root_mtime = os.stat(dvmdata_dir+'savefile-root').st_mtime
  120. if dvm_mtime < root_mtime:
  121. template_name = os.path.basename(
  122. os.path.dirname(os.readlink(dvmdata_dir+'savefile-root')))
  123. if subprocess.call(["xl", "domid", template_name],
  124. stdout=open(os.devnull, "w")) == 0:
  125. tray_notify("For optimum performance, you should not "
  126. "start DispVM when its template is running.", "red")
  127. return False
  128. return True
  129. def get_dvm(self):
  130. if not self.dvm_setup_ok():
  131. if os.system("/usr/lib/qubes/"
  132. "qubes-update-dispvm-savefile-with-progress.sh"
  133. " >/dev/null </dev/null") != 0:
  134. tray_notify_error("DVM savefile creation failed")
  135. return None
  136. return self.do_get_dvm()
  137. @staticmethod
  138. def remove_disposable_from_qdb(name):
  139. qvm_collection = QubesVmCollection()
  140. qvm_collection.lock_db_for_writing()
  141. qvm_collection.load()
  142. vm = qvm_collection.get_vm_by_name(name)
  143. if vm is None:
  144. qvm_collection.unlock_db()
  145. return False
  146. qvm_collection.pop(vm.qid)
  147. qvm_collection.save()
  148. qvm_collection.unlock_db()
  149. def main():
  150. exec_index = sys.argv[1]
  151. src_vmname = sys.argv[2]
  152. user = sys.argv[3]
  153. # accessed directly by get_dvm()
  154. # sys.argv[4] - override label
  155. # sys.argv[5] - override firewall
  156. print >>sys.stderr, "time=%s, qfile-daemon-dvm init" % (str(time.time()))
  157. tray_notify_init()
  158. print >>sys.stderr, "time=%s, creating DispVM" % (str(time.time()))
  159. qfile = QfileDaemonDvm(src_vmname)
  160. dispvm = qfile.get_dvm()
  161. if dispvm is not None:
  162. print >>sys.stderr, "time=%s, starting VM process" % (str(time.time()))
  163. subprocess.call(['/usr/lib/qubes/qrexec-client', '-d', dispvm.name,
  164. user+':exec /usr/lib/qubes/qubes-rpc-multiplexer ' +
  165. exec_index + " " + src_vmname])
  166. try:
  167. dispvm.force_shutdown()
  168. except QubesException:
  169. # VM already destroyed
  170. pass
  171. qfile.remove_disposable_from_qdb(dispvm.name)
  172. main()