2013-01-20 21:44:13 +01:00
|
|
|
#!/usr/bin/python2
|
2015-05-03 20:25:08 +02:00
|
|
|
# coding=utf-8
|
2011-03-14 10:43:09 +01:00
|
|
|
#
|
|
|
|
# The Qubes OS Project, http://www.qubes-os.org
|
|
|
|
#
|
2015-05-03 20:25:08 +02:00
|
|
|
# Copyright (C) 2010 Rafal Wojtczuk <rafal@invisiblethingslab.com>
|
|
|
|
# Copyright (C) 2013-2015 Marek Marczykowski-Górecki
|
|
|
|
# <marmarek@invisiblethingslab.com>
|
2011-03-14 10:43:09 +01:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
# of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
2015-05-03 20:24:45 +02:00
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2011-03-14 10:43:09 +01:00
|
|
|
#
|
|
|
|
#
|
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
import sys
|
2011-10-31 01:28:46 +01:00
|
|
|
import shutil
|
2013-02-25 13:42:36 +01:00
|
|
|
import time
|
2011-03-14 10:43:09 +01:00
|
|
|
|
2015-11-27 00:25:24 +01:00
|
|
|
from qubes.qubes import QubesVmCollection, QubesException
|
2012-06-24 14:09:43 +02:00
|
|
|
from qubes.qubes import QubesDispVmLabels
|
2015-05-03 20:24:45 +02:00
|
|
|
from qubes.notify import tray_notify, tray_notify_error, tray_notify_init
|
|
|
|
|
2011-03-14 10:43:09 +01:00
|
|
|
|
2013-03-14 14:45:45 +01:00
|
|
|
current_savefile = '/var/run/qubes/current-savefile'
|
2013-05-04 04:50:37 +02:00
|
|
|
current_savefile_vmdir = '/var/lib/qubes/dvmdata/vmdir'
|
2011-03-14 10:43:09 +01:00
|
|
|
|
2015-05-03 20:24:45 +02:00
|
|
|
|
2011-03-14 10:43:09 +01:00
|
|
|
class QfileDaemonDvm:
|
|
|
|
def __init__(self, name):
|
|
|
|
self.name = name
|
2013-05-04 04:50:37 +02:00
|
|
|
|
2015-05-03 20:25:39 +02:00
|
|
|
@staticmethod
|
|
|
|
def get_disp_templ():
|
2013-05-04 04:50:37 +02:00
|
|
|
vmdir = os.readlink(current_savefile_vmdir)
|
|
|
|
return vmdir.split('/')[-1]
|
2011-03-14 10:43:09 +01:00
|
|
|
|
|
|
|
def do_get_dvm(self):
|
2014-07-02 02:31:05 +02:00
|
|
|
tray_notify("Starting new DispVM...", "red")
|
2012-07-12 03:44:41 +02:00
|
|
|
|
2011-03-14 10:43:09 +01:00
|
|
|
qvm_collection = QubesVmCollection()
|
|
|
|
qvm_collection.lock_db_for_writing()
|
2016-02-21 12:41:13 +01:00
|
|
|
try:
|
2013-05-04 04:50:37 +02:00
|
|
|
|
2016-02-21 12:41:13 +01:00
|
|
|
tar_process = subprocess.Popen(
|
|
|
|
['bsdtar', '-C', current_savefile_vmdir,
|
|
|
|
'-xSUf', os.path.join(current_savefile_vmdir, 'saved-cows.tar')])
|
2013-05-04 04:50:37 +02:00
|
|
|
|
2016-02-21 12:41:13 +01:00
|
|
|
qvm_collection.load()
|
|
|
|
print >>sys.stderr, "time=%s, collection loaded" % (str(time.time()))
|
2011-03-14 10:43:09 +01:00
|
|
|
|
2016-02-21 12:41:13 +01:00
|
|
|
vm = qvm_collection.get_vm_by_name(self.name)
|
|
|
|
if vm is None:
|
|
|
|
sys.stderr.write('Domain ' + self.name + ' does not exist ?')
|
|
|
|
return None
|
|
|
|
label = vm.label
|
|
|
|
if len(sys.argv) > 4 and len(sys.argv[4]) > 0:
|
|
|
|
assert sys.argv[4] in QubesDispVmLabels.keys(), "Invalid label"
|
|
|
|
label = QubesDispVmLabels[sys.argv[4]]
|
|
|
|
disp_templ = self.get_disp_templ()
|
|
|
|
vm_disptempl = qvm_collection.get_vm_by_name(disp_templ)
|
|
|
|
if vm_disptempl is None:
|
|
|
|
sys.stderr.write('Domain ' + disp_templ + ' does not exist ?')
|
|
|
|
return None
|
|
|
|
dispvm = qvm_collection.add_new_vm('QubesDisposableVm',
|
|
|
|
disp_template=vm_disptempl,
|
|
|
|
label=label)
|
|
|
|
print >>sys.stderr, "time=%s, VM created" % (str(time.time()))
|
|
|
|
# By default inherit firewall rules from calling VM
|
|
|
|
disp_firewall_conf = '/var/run/qubes/%s-firewall.xml' % dispvm.name
|
|
|
|
dispvm.firewall_conf = disp_firewall_conf
|
|
|
|
if os.path.exists(vm.firewall_conf):
|
|
|
|
shutil.copy(vm.firewall_conf, disp_firewall_conf)
|
|
|
|
elif vm.qid == 0 and os.path.exists(vm_disptempl.firewall_conf):
|
|
|
|
# for DispVM called from dom0, copy use rules from DispVM template
|
|
|
|
shutil.copy(vm_disptempl.firewall_conf, disp_firewall_conf)
|
|
|
|
if len(sys.argv) > 5 and len(sys.argv[5]) > 0:
|
|
|
|
assert os.path.exists(sys.argv[5]), "Invalid firewall.conf location"
|
|
|
|
dispvm.firewall_conf = sys.argv[5]
|
|
|
|
if vm.qid != 0:
|
|
|
|
dispvm.uses_default_netvm = False
|
|
|
|
# netvm can be changed before restore,
|
|
|
|
# but cannot be enabled/disabled
|
|
|
|
if (dispvm.netvm is None) == (vm.dispvm_netvm is None):
|
|
|
|
dispvm.netvm = vm.dispvm_netvm
|
|
|
|
# Wait for tar to finish
|
|
|
|
if tar_process.wait() != 0:
|
|
|
|
sys.stderr.write('Failed to unpack saved-cows.tar')
|
|
|
|
return None
|
|
|
|
print >>sys.stderr, "time=%s, VM starting" % (str(time.time()))
|
|
|
|
try:
|
|
|
|
dispvm.start()
|
|
|
|
except (MemoryError, QubesException) as e:
|
|
|
|
tray_notify_error(str(e))
|
|
|
|
raise
|
|
|
|
if vm.qid != 0:
|
|
|
|
# if need to enable/disable netvm, do it while DispVM is alive
|
|
|
|
if (dispvm.netvm is None) != (vm.dispvm_netvm is None):
|
|
|
|
dispvm.netvm = vm.dispvm_netvm
|
|
|
|
print >>sys.stderr, "time=%s, VM started" % (str(time.time()))
|
|
|
|
qvm_collection.save()
|
|
|
|
finally:
|
2011-03-14 10:43:09 +01:00
|
|
|
qvm_collection.unlock_db()
|
2011-07-09 17:54:23 +02:00
|
|
|
# Reload firewall rules
|
2013-02-25 06:48:29 +01:00
|
|
|
print >>sys.stderr, "time=%s, reloading firewall" % (str(time.time()))
|
2011-07-09 17:54:23 +02:00
|
|
|
for vm in qvm_collection.values():
|
|
|
|
if vm.is_proxyvm() and vm.is_running():
|
2015-03-28 22:36:28 +01:00
|
|
|
vm.write_iptables_qubesdb_entry()
|
2011-03-14 10:43:09 +01:00
|
|
|
|
2013-05-04 04:50:37 +02:00
|
|
|
return dispvm
|
2011-03-14 10:43:09 +01:00
|
|
|
|
2015-05-03 20:25:39 +02:00
|
|
|
@staticmethod
|
|
|
|
def dvm_setup_ok():
|
2011-03-14 10:43:09 +01:00
|
|
|
dvmdata_dir = '/var/lib/qubes/dvmdata/'
|
|
|
|
if not os.path.isfile(current_savefile):
|
|
|
|
return False
|
2015-05-03 20:24:45 +02:00
|
|
|
if not os.path.isfile(dvmdata_dir+'default-savefile') or \
|
|
|
|
not os.path.isfile(dvmdata_dir+'savefile-root'):
|
2011-03-14 10:43:09 +01:00
|
|
|
return False
|
|
|
|
dvm_mtime = os.stat(current_savefile).st_mtime
|
2013-03-14 14:45:45 +01:00
|
|
|
root_mtime = os.stat(dvmdata_dir+'savefile-root').st_mtime
|
2011-03-14 10:43:09 +01:00
|
|
|
if dvm_mtime < root_mtime:
|
2015-05-03 20:24:45 +02:00
|
|
|
template_name = os.path.basename(
|
|
|
|
os.path.dirname(os.readlink(dvmdata_dir+'savefile-root')))
|
2015-04-10 19:01:46 +02:00
|
|
|
if subprocess.call(["xl", "domid", template_name],
|
|
|
|
stdout=open(os.devnull, "w")) == 0:
|
2014-07-02 02:31:05 +02:00
|
|
|
tray_notify("For optimum performance, you should not "
|
2015-05-03 20:24:45 +02:00
|
|
|
"start DispVM when its template is running.", "red")
|
2011-03-14 10:43:09 +01:00
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
def get_dvm(self):
|
|
|
|
if not self.dvm_setup_ok():
|
2015-05-03 20:24:45 +02:00
|
|
|
if os.system("/usr/lib/qubes/"
|
|
|
|
"qubes-update-dispvm-savefile-with-progress.sh"
|
|
|
|
" >/dev/null </dev/null") != 0:
|
2014-06-04 02:47:04 +02:00
|
|
|
tray_notify_error("DVM savefile creation failed")
|
2011-03-14 10:43:09 +01:00
|
|
|
return None
|
|
|
|
return self.do_get_dvm()
|
|
|
|
|
2015-05-03 20:25:39 +02:00
|
|
|
@staticmethod
|
2016-06-28 15:26:10 +02:00
|
|
|
def finish_disposable(name):
|
2011-03-14 10:43:09 +01:00
|
|
|
qvm_collection = QubesVmCollection()
|
|
|
|
qvm_collection.lock_db_for_writing()
|
|
|
|
qvm_collection.load()
|
|
|
|
vm = qvm_collection.get_vm_by_name(name)
|
|
|
|
if vm is None:
|
|
|
|
qvm_collection.unlock_db()
|
|
|
|
return False
|
2016-06-28 15:26:10 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
vm.force_shutdown()
|
|
|
|
except QubesException:
|
|
|
|
# VM already destroyed
|
|
|
|
pass
|
2011-03-14 10:43:09 +01:00
|
|
|
qvm_collection.pop(vm.qid)
|
|
|
|
qvm_collection.save()
|
|
|
|
qvm_collection.unlock_db()
|
|
|
|
|
2015-05-03 20:24:45 +02:00
|
|
|
|
2011-03-14 10:43:09 +01:00
|
|
|
def main():
|
2011-07-06 12:32:20 +02:00
|
|
|
exec_index = sys.argv[1]
|
|
|
|
src_vmname = sys.argv[2]
|
2011-07-06 18:34:00 +02:00
|
|
|
user = sys.argv[3]
|
2015-05-03 20:24:45 +02:00
|
|
|
# accessed directly by get_dvm()
|
|
|
|
# sys.argv[4] - override label
|
|
|
|
# sys.argv[5] - override firewall
|
2011-07-06 18:34:00 +02:00
|
|
|
|
2013-02-25 06:48:29 +01:00
|
|
|
print >>sys.stderr, "time=%s, qfile-daemon-dvm init" % (str(time.time()))
|
2014-06-04 02:47:04 +02:00
|
|
|
tray_notify_init()
|
2013-02-25 06:48:29 +01:00
|
|
|
print >>sys.stderr, "time=%s, creating DispVM" % (str(time.time()))
|
2011-07-06 12:32:20 +02:00
|
|
|
qfile = QfileDaemonDvm(src_vmname)
|
2013-05-04 04:50:37 +02:00
|
|
|
dispvm = qfile.get_dvm()
|
|
|
|
if dispvm is not None:
|
2013-02-25 06:48:29 +01:00
|
|
|
print >>sys.stderr, "time=%s, starting VM process" % (str(time.time()))
|
2013-05-04 04:50:37 +02:00
|
|
|
subprocess.call(['/usr/lib/qubes/qrexec-client', '-d', dispvm.name,
|
2015-05-03 20:24:45 +02:00
|
|
|
user+':exec /usr/lib/qubes/qubes-rpc-multiplexer ' +
|
|
|
|
exec_index + " " + src_vmname])
|
2016-06-28 15:27:03 +02:00
|
|
|
QfileDaemonDvm.finish_disposable(dispvm.name)
|
2011-03-14 10:43:09 +01:00
|
|
|
|
2015-10-10 03:39:43 +02:00
|
|
|
main()
|