qfile-daemon-dvm 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. tar_process = subprocess.Popen(
  47. ['bsdtar', '-C', current_savefile_vmdir,
  48. '-xSUf', os.path.join(current_savefile_vmdir, 'saved-cows.tar')])
  49. qvm_collection.load()
  50. print >>sys.stderr, "time=%s, collection loaded" % (str(time.time()))
  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. 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. qvm_collection.unlock_db()
  65. return None
  66. dispvm = qvm_collection.add_new_vm('QubesDisposableVm',
  67. disp_template=vm_disptempl,
  68. label=label)
  69. print >>sys.stderr, "time=%s, VM created" % (str(time.time()))
  70. # By default inherit firewall rules from calling VM
  71. if os.path.exists(vm.firewall_conf):
  72. disp_firewall_conf = '/var/run/qubes/%s-firewall.xml' % dispvm.name
  73. shutil.copy(vm.firewall_conf, disp_firewall_conf)
  74. dispvm.firewall_conf = disp_firewall_conf
  75. if len(sys.argv) > 5 and len(sys.argv[5]) > 0:
  76. assert os.path.exists(sys.argv[5]), "Invalid firewall.conf location"
  77. dispvm.firewall_conf = sys.argv[5]
  78. if vm.qid != 0:
  79. dispvm.uses_default_netvm = False
  80. # netvm can be changed before restore,
  81. # but cannot be enabled/disabled
  82. if (dispvm.netvm is None) == (vm.dispvm_netvm is None):
  83. dispvm.netvm = vm.dispvm_netvm
  84. # Wait for tar to finish
  85. if tar_process.wait() != 0:
  86. sys.stderr.write('Failed to unpack saved-cows.tar')
  87. qvm_collection.unlock_db()
  88. return None
  89. print >>sys.stderr, "time=%s, VM starting" % (str(time.time()))
  90. try:
  91. dispvm.start()
  92. except (MemoryError, QubesException) as e:
  93. tray_notify_error(str(e))
  94. raise
  95. if vm.qid != 0:
  96. # if need to enable/disable netvm, do it while DispVM is alive
  97. if (dispvm.netvm is None) != (vm.dispvm_netvm is None):
  98. dispvm.netvm = vm.dispvm_netvm
  99. print >>sys.stderr, "time=%s, VM started" % (str(time.time()))
  100. qvm_collection.save()
  101. qvm_collection.unlock_db()
  102. # Reload firewall rules
  103. print >>sys.stderr, "time=%s, reloading firewall" % (str(time.time()))
  104. for vm in qvm_collection.values():
  105. if vm.is_proxyvm() and vm.is_running():
  106. vm.write_iptables_qubesdb_entry()
  107. return dispvm
  108. @staticmethod
  109. def dvm_setup_ok():
  110. dvmdata_dir = '/var/lib/qubes/dvmdata/'
  111. if not os.path.isfile(current_savefile):
  112. return False
  113. if not os.path.isfile(dvmdata_dir+'default-savefile') or \
  114. 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(
  120. os.path.dirname(os.readlink(dvmdata_dir+'savefile-root')))
  121. if subprocess.call(["xl", "domid", template_name],
  122. stdout=open(os.devnull, "w")) == 0:
  123. tray_notify("For optimum performance, you should not "
  124. "start DispVM when its template is running.", "red")
  125. return False
  126. return True
  127. def get_dvm(self):
  128. if not self.dvm_setup_ok():
  129. if os.system("/usr/lib/qubes/"
  130. "qubes-update-dispvm-savefile-with-progress.sh"
  131. " >/dev/null </dev/null") != 0:
  132. tray_notify_error("DVM savefile creation failed")
  133. return None
  134. return self.do_get_dvm()
  135. @staticmethod
  136. def remove_disposable_from_qdb(name):
  137. qvm_collection = QubesVmCollection()
  138. qvm_collection.lock_db_for_writing()
  139. qvm_collection.load()
  140. vm = qvm_collection.get_vm_by_name(name)
  141. if vm is None:
  142. qvm_collection.unlock_db()
  143. return False
  144. qvm_collection.pop(vm.qid)
  145. qvm_collection.save()
  146. qvm_collection.unlock_db()
  147. def main():
  148. exec_index = sys.argv[1]
  149. src_vmname = sys.argv[2]
  150. user = sys.argv[3]
  151. # accessed directly by get_dvm()
  152. # sys.argv[4] - override label
  153. # sys.argv[5] - override firewall
  154. print >>sys.stderr, "time=%s, qfile-daemon-dvm init" % (str(time.time()))
  155. tray_notify_init()
  156. print >>sys.stderr, "time=%s, creating DispVM" % (str(time.time()))
  157. qfile = QfileDaemonDvm(src_vmname)
  158. dispvm = qfile.get_dvm()
  159. if dispvm is not None:
  160. print >>sys.stderr, "time=%s, starting VM process" % (str(time.time()))
  161. subprocess.call(['/usr/lib/qubes/qrexec-client', '-d', dispvm.name,
  162. user+':exec /usr/lib/qubes/qubes-rpc-multiplexer ' +
  163. exec_index + " " + src_vmname])
  164. dispvm.force_shutdown()
  165. qfile.remove_disposable_from_qdb(dispvm.name)
  166. main()