2010-04-05 20:58:57 +02:00
|
|
|
#!/usr/bin/python2.6
|
|
|
|
#
|
|
|
|
# The Qubes OS Project, http://www.qubes-os.org
|
|
|
|
#
|
|
|
|
# Copyright (C) 2010 Joanna Rutkowska <joanna@invisiblethingslab.com>
|
|
|
|
# Copyright (C) 2010 Rafal Wojtczuk <rafal@invisiblethingslab.com>
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
from qubes.qubes import QubesVmCollection
|
|
|
|
from qubes.qubes import QubesException
|
|
|
|
from optparse import OptionParser
|
|
|
|
import subprocess
|
|
|
|
import socket
|
|
|
|
import errno
|
|
|
|
import dbus
|
|
|
|
import time
|
2011-03-07 15:58:04 +01:00
|
|
|
import os
|
|
|
|
import os.path
|
2010-04-05 20:58:57 +02:00
|
|
|
|
|
|
|
qubes_guid_path = "/usr/bin/qubes_guid"
|
|
|
|
qubes_clipd_path = "/usr/bin/qclipd"
|
2011-03-04 17:19:51 +01:00
|
|
|
qrexec_daemon_path = "/usr/lib/qubes/qrexec_daemon"
|
2011-03-07 15:58:04 +01:00
|
|
|
qrexec_client_path = "/usr/lib/qubes/qrexec_client"
|
2010-04-05 20:58:57 +02:00
|
|
|
notify_object = None
|
|
|
|
|
|
|
|
# how long (in sec) to wait for VMs to shutdown
|
|
|
|
# before killing them (when used with --wait option)
|
|
|
|
shutdown_counter_max = 30
|
|
|
|
|
|
|
|
def tray_notify(str, label, timeout = 3000):
|
|
|
|
notify_object.Notify("Qubes", 0, label.icon, "Qubes", str, [], [], timeout, dbus_interface="org.freedesktop.Notifications")
|
|
|
|
|
|
|
|
def tray_notify_error(str, timeout = 3000):
|
|
|
|
notify_object.Notify("Qubes", 0, "dialog-error", "Qubes", str, [], [], timeout, dbus_interface="org.freedesktop.Notifications")
|
|
|
|
|
2011-03-07 15:58:04 +01:00
|
|
|
def actually_execute(domid, cmd, options):
|
|
|
|
args = [qrexec_client_path, "-d", domid, cmd]
|
|
|
|
if options.localcmd is not None:
|
|
|
|
args += [ "-l", options.localcmd]
|
|
|
|
if options.passio and not options.run_on_all_running:
|
|
|
|
os.execv(qrexec_client_path, args)
|
|
|
|
exit(1)
|
|
|
|
args += ["-e"]
|
|
|
|
subprocess.call(args)
|
2010-04-05 20:58:57 +02:00
|
|
|
|
|
|
|
def vm_run_cmd(vm, cmd, options):
|
|
|
|
if options.shutdown:
|
|
|
|
if options.verbose:
|
|
|
|
print "Shutting down VM: '{0}'...".format(vm.name)
|
|
|
|
subprocess.call (["/usr/sbin/xm", "shutdown", vm.name])
|
|
|
|
return
|
|
|
|
|
2010-07-08 12:41:29 +02:00
|
|
|
if options.pause:
|
|
|
|
if options.verbose:
|
|
|
|
print "Pausing VM: '{0}'...".format(vm.name)
|
|
|
|
subprocess.call (["/usr/sbin/xm", "pause", vm.name])
|
|
|
|
return
|
|
|
|
|
|
|
|
if options.unpause:
|
|
|
|
if options.verbose:
|
|
|
|
print "UnPausing VM: '{0}'...".format(vm.name)
|
|
|
|
subprocess.call (["/usr/sbin/xm", "unpause", vm.name])
|
|
|
|
return
|
|
|
|
|
2010-04-05 20:58:57 +02:00
|
|
|
if options.verbose:
|
|
|
|
print "Running command on VM: '{0}'...".format(vm.name)
|
|
|
|
|
|
|
|
if not vm.is_running():
|
|
|
|
if not options.auto:
|
|
|
|
print "VM '{0}' is not running, please start it first, or use the '--auto' switch"
|
|
|
|
exit (1)
|
|
|
|
try:
|
|
|
|
if options.verbose:
|
|
|
|
print "Starting the VM '{0}'...".format(vm.name)
|
|
|
|
if options.tray:
|
|
|
|
tray_notify ("Starting the '{0}' VM...".format(vm.name), label=vm.label)
|
|
|
|
xid = vm.start(verbose=options.verbose)
|
2011-03-04 17:19:51 +01:00
|
|
|
|
2010-04-05 20:58:57 +02:00
|
|
|
except (IOError, OSError, QubesException) as err:
|
|
|
|
print "ERROR: {0}".format(err)
|
|
|
|
if options.tray:
|
|
|
|
tray_notify_error ("Error while starting the '{0}' VM: {1}".format(vm.name, err))
|
|
|
|
exit (1)
|
|
|
|
except (MemoryError) as err:
|
|
|
|
print "ERROR: {0}".format(err)
|
|
|
|
print "Close one or more running VMs and try again."
|
|
|
|
if options.tray:
|
|
|
|
subprocess.call(["kdialog", "--error", "Not enough memory to start '{0}' VM! Close one or more running VMs and try again.".format(vm.name)])
|
|
|
|
exit (1)
|
|
|
|
|
2011-03-07 15:58:04 +01:00
|
|
|
if os.getenv("DISPLAY") is not None:
|
|
|
|
if options.verbose:
|
|
|
|
print "--> Starting Qubes GUId..."
|
|
|
|
|
|
|
|
retcode = subprocess.call ([qubes_guid_path, "-d", str(xid), "-c", vm.label.color, "-i", vm.label.icon, "-l", str(vm.label.index)])
|
|
|
|
if (retcode != 0) :
|
|
|
|
print "ERROR: Cannot start qubes_guid!"
|
|
|
|
if options.tray:
|
|
|
|
tray_notify_error ("ERROR: Cannot start qubes_guid!")
|
|
|
|
exit (1)
|
|
|
|
|
2010-04-05 20:58:57 +02:00
|
|
|
if options.verbose:
|
2011-03-07 15:58:04 +01:00
|
|
|
print "--> Starting Qubes rexec daemon..."
|
2010-04-05 20:58:57 +02:00
|
|
|
|
2011-03-07 15:58:04 +01:00
|
|
|
retcode = subprocess.call ([qrexec_daemon_path, str(xid)])
|
2010-04-05 20:58:57 +02:00
|
|
|
if (retcode != 0) :
|
2011-03-07 15:58:04 +01:00
|
|
|
print "ERROR: Cannot start qrexec_daemon!"
|
2010-04-05 20:58:57 +02:00
|
|
|
exit (1)
|
2011-03-07 15:58:04 +01:00
|
|
|
|
|
|
|
actually_execute(str(xid), cmd, options);
|
|
|
|
|
2010-04-05 20:58:57 +02:00
|
|
|
else: # VM already running...
|
|
|
|
xid = vm.get_xid()
|
2011-03-07 15:58:04 +01:00
|
|
|
if os.getenv("DISPLAY") is not None and not os.path.isfile("/var/run/qubes/guid_running.{0}".format(xid)):
|
|
|
|
retcode = subprocess.call ([qubes_guid_path, "-d", str(xid), "-c", vm.label.color, "-i", vm.label.icon, "-l", str(vm.label.index)])
|
2010-04-05 20:58:57 +02:00
|
|
|
if (retcode != 0) :
|
|
|
|
print "ERROR: Cannot start qubes_guid!"
|
|
|
|
if options.tray:
|
|
|
|
tray_notify_error ("ERROR: Cannot start the GUI daemon for this VM!")
|
|
|
|
exit (1)
|
2011-04-01 01:23:57 +02:00
|
|
|
|
|
|
|
if not os.path.exists("/var/run/qubes/qrexec.{0}".format(xid)):
|
|
|
|
retcode = subprocess.call ([qrexec_daemon_path, str(xid)])
|
|
|
|
if (retcode != 0) :
|
|
|
|
print "ERROR: Cannot start qrexec!"
|
|
|
|
if options.tray:
|
|
|
|
tray_notify_error ("ERROR: Cannot start the QRexec daemon for this VM!")
|
|
|
|
exit (1)
|
|
|
|
|
2011-03-07 15:58:04 +01:00
|
|
|
actually_execute(str(xid), cmd, options);
|
2010-04-05 20:58:57 +02:00
|
|
|
|
|
|
|
def main():
|
|
|
|
usage = "usage: %prog [options] [<vm-name>] [<cmd>]"
|
|
|
|
parser = OptionParser (usage)
|
|
|
|
parser.add_option ("-q", "--quiet", action="store_false", dest="verbose", default=True)
|
|
|
|
parser.add_option ("-a", "--auto", action="store_true", dest="auto", default=False,
|
|
|
|
help="Auto start the VM if not running")
|
|
|
|
parser.add_option ("-u", "--user", action="store", dest="user", default="user",
|
|
|
|
help="Run command in a VM as a specified user")
|
|
|
|
parser.add_option ("--tray", action="store_true", dest="tray", default=False,
|
|
|
|
help="Use tray notifications instead of stdout" )
|
|
|
|
|
|
|
|
parser.add_option ("--all", action="store_true", dest="run_on_all_running", default=False,
|
2010-07-08 12:41:29 +02:00
|
|
|
help="Run command on all currently running VMs (or all paused, in case of --unpause)")
|
2010-04-05 20:58:57 +02:00
|
|
|
|
|
|
|
parser.add_option ("--exclude", action="append", dest="exclude_list",
|
|
|
|
help="When --all is used: exclude this VM name (might be repeated)")
|
|
|
|
|
|
|
|
parser.add_option ("--wait", action="store_true", dest="wait_for_shutdown", default=False,
|
|
|
|
help="Wait for the VM(s) to shutdown")
|
|
|
|
|
|
|
|
parser.add_option ("--shutdown", action="store_true", dest="shutdown", default=False,
|
|
|
|
help="Do 'xm shutdown' for the VM(s) (can be combined this with --all and --wait)")
|
|
|
|
|
2010-07-08 12:41:29 +02:00
|
|
|
parser.add_option ("--pause", action="store_true", dest="pause", default=False,
|
|
|
|
help="Do 'xm pause' for the VM(s) (can be combined this with --all and --wait)")
|
|
|
|
|
|
|
|
parser.add_option ("--unpause", action="store_true", dest="unpause", default=False,
|
|
|
|
help="Do 'xm unpause' for the VM(s) (can be combined this with --all and --wait)")
|
|
|
|
|
2011-03-07 15:58:04 +01:00
|
|
|
parser.add_option ("--pass_io", action="store_true", dest="passio", default=False,
|
|
|
|
help="Pass stdin/stdout/stderr from remote program")
|
|
|
|
|
|
|
|
parser.add_option ("--localcmd", action="store", dest="localcmd", default=None,
|
|
|
|
help="With --pass_io, pass stdin/stdout/stderr to the given program")
|
2010-07-08 12:41:29 +02:00
|
|
|
|
2010-04-05 20:58:57 +02:00
|
|
|
(options, args) = parser.parse_args ()
|
|
|
|
|
|
|
|
|
2010-07-08 12:41:29 +02:00
|
|
|
if (options.shutdown or options.pause or options.unpause):
|
|
|
|
takes_cmd_argument = False
|
|
|
|
else:
|
|
|
|
takes_cmd_argument = True
|
|
|
|
|
2010-04-05 20:58:57 +02:00
|
|
|
if options.run_on_all_running:
|
2010-07-08 12:41:29 +02:00
|
|
|
if len(args) < 1 and takes_cmd_argument:
|
2010-04-05 20:58:57 +02:00
|
|
|
parser.error ("You must provide a command to execute on all the VMs.")
|
2010-07-08 12:41:29 +02:00
|
|
|
if len(args) > 1 or ((not takes_cmd_argument) and len(args) > 0):
|
2010-04-05 20:58:57 +02:00
|
|
|
parser.error ("To many arguments...")
|
2010-07-08 12:41:29 +02:00
|
|
|
cmdstr = args[0] if takes_cmd_argument else None
|
2010-04-05 20:58:57 +02:00
|
|
|
else:
|
2010-07-08 12:41:29 +02:00
|
|
|
if len (args) < 1 and not takes_cmd_argument:
|
|
|
|
parser.error ("You must specify the VM name to shutdown/pause/unpause.")
|
|
|
|
if len (args) < 2 and takes_cmd_argument:
|
2010-04-05 20:58:57 +02:00
|
|
|
parser.error ("You must specify the VM name and the command to execute in the VM.")
|
2010-07-08 12:41:29 +02:00
|
|
|
if len (args) > 2 or ((not takes_cmd_argument) and len(args) > 1):
|
2010-04-05 20:58:57 +02:00
|
|
|
parser.error ("To many arguments...")
|
|
|
|
vmname = args[0]
|
2010-07-08 12:41:29 +02:00
|
|
|
cmdstr = args[1] if takes_cmd_argument else None
|
2010-04-05 20:58:57 +02:00
|
|
|
|
|
|
|
if options.tray:
|
|
|
|
global notify_object
|
|
|
|
notify_object = dbus.SessionBus().get_object("org.freedesktop.Notifications", "/org/freedesktop/Notifications")
|
|
|
|
|
|
|
|
qvm_collection = QubesVmCollection()
|
|
|
|
qvm_collection.lock_db_for_reading()
|
|
|
|
qvm_collection.load()
|
|
|
|
qvm_collection.unlock_db()
|
|
|
|
|
|
|
|
vms_list = []
|
|
|
|
if options.run_on_all_running:
|
|
|
|
all_vms = [vm for vm in qvm_collection.values()]
|
|
|
|
for vm in all_vms:
|
|
|
|
if options.exclude_list is not None and vm.name in options.exclude_list:
|
|
|
|
continue
|
|
|
|
if vm.qid == 0:
|
|
|
|
continue
|
2010-07-08 12:41:29 +02:00
|
|
|
if (options.unpause and vm.is_paused()) or (not options.unpause and vm.is_running()):
|
2010-04-05 20:58:57 +02:00
|
|
|
vms_list.append (vm)
|
|
|
|
else:
|
|
|
|
vm = qvm_collection.get_vm_by_name(vmname)
|
|
|
|
if vm is None:
|
|
|
|
print "A VM with the name '{0}' does not exist in the system!".format(vmname)
|
|
|
|
exit(1)
|
|
|
|
vms_list.append(vm)
|
2011-03-31 01:58:45 +02:00
|
|
|
|
|
|
|
# If stopping NetVM - stop connected VMs too
|
|
|
|
if options.shutdown and vm.is_netvm():
|
|
|
|
vms_list += [vm for vm in qvm_collection.get_vms_connected_to(vm.qid) if vm.is_running()]
|
|
|
|
vms_list.reverse()
|
2010-04-05 20:58:57 +02:00
|
|
|
|
2010-07-08 12:41:29 +02:00
|
|
|
if takes_cmd_argument:
|
2010-04-05 20:58:57 +02:00
|
|
|
cmd = "{user}:{cmd}".format(user=options.user, cmd=cmdstr)
|
2010-07-08 12:41:29 +02:00
|
|
|
else:
|
|
|
|
cmd = None
|
2010-04-05 20:58:57 +02:00
|
|
|
|
|
|
|
for vm in vms_list:
|
|
|
|
vm_run_cmd(vm, cmd, options)
|
|
|
|
|
|
|
|
|
|
|
|
if options.wait_for_shutdown:
|
|
|
|
if options.verbose:
|
|
|
|
print "Waiting for the VM(s) to shutdown..."
|
|
|
|
shutdown_counter = 0
|
|
|
|
|
|
|
|
while len (vms_list):
|
|
|
|
if options.verbose:
|
|
|
|
print "Waiting for VMs: ", [vm.name for vm in vms_list]
|
|
|
|
for vm in vms_list:
|
|
|
|
if not vm.is_running():
|
|
|
|
vms_list.remove (vm)
|
|
|
|
if shutdown_counter > shutdown_counter_max:
|
|
|
|
# kill the VM
|
|
|
|
if options.verbose:
|
|
|
|
print "Killing the (apparently hanging) VM '{0}'...".format(vm.name)
|
|
|
|
vm.force_shutdown()
|
|
|
|
#vms_list.remove(vm)
|
|
|
|
|
|
|
|
shutdown_counter += 1
|
|
|
|
time.sleep (1)
|
|
|
|
exit (0) # there is no point in executing the other daemons in the case of --wait
|
|
|
|
|
|
|
|
retcode = subprocess.call([qubes_clipd_path])
|
|
|
|
if retcode != 0:
|
|
|
|
print "ERROR: Cannot start qclipd!"
|
|
|
|
if options.tray:
|
|
|
|
tray_notify ("ERROR: Cannot start the Qubes Clipboard Notifier!")
|
|
|
|
|
|
|
|
|
|
|
|
main()
|