dom0/qvm-run: move run code to qubesutils

This commit is contained in:
Marek Marczykowski 2011-10-31 13:31:17 +01:00
parent a4e11dedd9
commit 870dea1502
3 changed files with 71 additions and 71 deletions

View File

@ -49,6 +49,7 @@ if not dry_run:
qubes_guid_path = "/usr/bin/qubes_guid" qubes_guid_path = "/usr/bin/qubes_guid"
qrexec_daemon_path = "/usr/lib/qubes/qrexec_daemon" qrexec_daemon_path = "/usr/lib/qubes/qrexec_daemon"
qrexec_client_path = "/usr/lib/qubes/qrexec_client" qrexec_client_path = "/usr/lib/qubes/qrexec_client"
qubes_clipd_path = "/usr/bin/qclipd"
qubes_base_dir = "/var/lib/qubes" qubes_base_dir = "/var/lib/qubes"

View File

@ -21,9 +21,9 @@
# #
from qubes import QubesVm,QubesException from qubes import QubesVm,QubesException
from qubes import xs, xl_ctx from qubes import xs, xl_ctx, qubes_guid_path, qubes_clipd_path, qrexec_client_path
import sys import sys
#import os import os
#import os.path #import os.path
import subprocess import subprocess
#import fcntl #import fcntl
@ -229,4 +229,56 @@ def block_detach(vm, frontend = "xvdi", vm_xid = None):
xl_cmd = [ '/usr/sbin/xl', 'block-detach', str(vm_xid), str(frontend)] xl_cmd = [ '/usr/sbin/xl', 'block-detach', str(vm_xid), str(frontend)]
subprocess.check_call(xl_cmd) subprocess.check_call(xl_cmd)
def start_guid(vm, verbose = True, notify_function = None):
if verbose:
print >> sys.stderr, "--> Starting Qubes GUId..."
xid = vm.get_xid()
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) :
raise QubesException("Cannot start qubes_guid!")
if verbose:
print >> sys.stderr, "--> Waiting for qubes-session..."
subprocess.call([qrexec_client_path, "-d", str(xid), "user:echo $$ >> /tmp/qubes-session-waiter; [ ! -f /tmp/qubes-session-env ] && exec sleep 365d"])
retcode = subprocess.call([qubes_clipd_path])
if retcode != 0:
print >> sys.stderr, "ERROR: Cannot start qclipd!"
if notify_function is not None:
notify_function("error", "ERROR: Cannot start the Qubes Clipboard Notifier!")
def run_in_vm(vm, command, verbose = True, autostart = False, notify_function = None, passio = False, localcmd = None):
assert vm is not None
if not vm.is_running():
if not autostart:
raise QubesException("VM not running")
try:
if verbose:
print >> sys.stderr, "Starting the VM '{0}'...".format(vm.name)
if notify_function is not None:
notify_function ("info", "Starting the '{0}' VM...".format(vm.name), label=vm.label)
xid = vm.start(verbose=verbose)
except (IOError, OSError, QubesException) as err:
raise QubesException("Error while starting the '{0}' VM: {1}".format(vm.name, err))
except (MemoryError) as err:
raise QubesException("Not enough memory to start '{0}' VM! Close one or more running VMs and try again.".format(vm.name))
xid = vm.get_xid()
if os.getenv("DISPLAY") is not None and not os.path.isfile("/var/run/qubes/guid_running.{0}".format(xid)):
start_guid(vm, verbose = verbose, notify_function = notify_function)
args = [qrexec_client_path, "-d", str(xid), command]
if localcmd is not None:
args += [ "-l", localcmd]
if passio:
os.execv(qrexec_client_path, args)
exit(1)
args += ["-e"]
return subprocess.call(args)
# vim:sw=4:et: # vim:sw=4:et:

View File

@ -24,6 +24,7 @@
from qubes.qubes import QubesVmCollection from qubes.qubes import QubesVmCollection
from qubes.qubes import QubesException from qubes.qubes import QubesException
from optparse import OptionParser from optparse import OptionParser
from qubes import qubesutils
import subprocess import subprocess
import socket import socket
import errno import errno
@ -33,9 +34,7 @@ import sys
import os import os
import os.path import os.path
qubes_guid_path = "/usr/bin/qubes_guid"
qubes_clipd_path = "/usr/bin/qclipd" qubes_clipd_path = "/usr/bin/qclipd"
qrexec_client_path = "/usr/lib/qubes/qrexec_client"
notify_object = None notify_object = None
# how long (in sec) to wait for VMs to shutdown # how long (in sec) to wait for VMs to shutdown
@ -48,33 +47,11 @@ def tray_notify(str, label, timeout = 3000):
def tray_notify_error(str, timeout = 3000): def tray_notify_error(str, timeout = 3000):
notify_object.Notify("Qubes", 0, "dialog-error", "Qubes", str, [], [], timeout, dbus_interface="org.freedesktop.Notifications") notify_object.Notify("Qubes", 0, "dialog-error", "Qubes", str, [], [], timeout, dbus_interface="org.freedesktop.Notifications")
def actually_execute(domid, cmd, options): def tray_notify_generic(level, str):
args = [qrexec_client_path, "-d", domid, cmd] if level == "info":
if options.localcmd is not None: tray_notify(str)
args += [ "-l", options.localcmd] elif level == "error":
if options.passio and not options.run_on_all_running: tray_notify_error(str)
os.execv(qrexec_client_path, args)
exit(1)
args += ["-e"]
subprocess.call(args)
def start_guid(vm, options):
if options.verbose:
print >> sys.stderr, "--> Starting Qubes GUId..."
xid = vm.get_xid()
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 >> sys.stderr, "ERROR: Cannot start qubes_guid!"
if options.tray:
tray_notify_error ("ERROR: Cannot start qubes_guid!")
exit (1)
if options.verbose:
print >> sys.stderr, "--> Waiting for qubes-session..."
subprocess.call([qrexec_client_path, "-d", str(xid), "user:echo $$ >> /tmp/qubes-session-waiter; [ ! -f /tmp/qubes-session-env ] && exec sleep 365d"])
def vm_run_cmd(vm, cmd, options): def vm_run_cmd(vm, cmd, options):
if options.shutdown: if options.shutdown:
@ -105,39 +82,14 @@ def vm_run_cmd(vm, cmd, options):
if options.verbose: if options.verbose:
print >> sys.stderr, "Running command on VM: '{0}'...".format(vm.name) print >> sys.stderr, "Running command on VM: '{0}'...".format(vm.name)
if not vm.is_running():
if not options.auto:
print >> sys.stderr, "VM '{0}' is not running. Please start it first or use the '--auto' switch".format(vm.name)
exit (1)
try: try:
if options.verbose: return qubesutils.run_in_vm(vm, cmd, autostart = options.auto,
print >> sys.stderr, "Starting the VM '{0}'...".format(vm.name) verbose = options.verbose,
if options.tray: notify_function = tray_notify_generic if options.tray else None,
tray_notify ("Starting the '{0}' VM...".format(vm.name), label=vm.label) passio = options.passio, localcmd = options.localcmd)
xid = vm.start(verbose=options.verbose) except QubesException as err:
print >> sys.stderr, "ERROR: %s" % str(err)
except (IOError, OSError, QubesException) as err:
print >> sys.stderr, "ERROR: {0}".format(err)
if options.tray:
tray_notify_error ("Error while starting the '{0}' VM: {1}".format(vm.name, err))
exit(1) exit(1)
except (MemoryError) as err:
print >> sys.stderr, "ERROR: {0}".format(err)
print >> sys.stderr, "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)
if os.getenv("DISPLAY") is not None:
start_guid(vm, options)
actually_execute(str(xid), cmd, options);
else: # VM already running...
xid = vm.get_xid()
if os.getenv("DISPLAY") is not None and not os.path.isfile("/var/run/qubes/guid_running.{0}".format(xid)):
start_guid(vm, options)
actually_execute(str(xid), cmd, options);
def main(): def main():
usage = "usage: %prog [options] [<vm-name>] [<cmd>]" usage = "usage: %prog [options] [<vm-name>] [<cmd>]"
@ -223,6 +175,8 @@ def main():
continue continue
if (options.unpause and vm.is_paused()) or (not options.unpause and vm.is_running()): if (options.unpause and vm.is_paused()) or (not options.unpause and vm.is_running()):
vms_list.append (vm) vms_list.append (vm)
# disable options incompatible with --all
options.passio = False
else: else:
vm = qvm_collection.get_vm_by_name(vmname) vm = qvm_collection.get_vm_by_name(vmname)
if vm is None: if vm is None:
@ -261,11 +215,4 @@ def main():
time.sleep (1) time.sleep (1)
exit (0) # there is no point in executing the other daemons in the case of --wait 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 >> sys.stderr, "ERROR: Cannot start qclipd!"
if options.tray:
tray_notify ("ERROR: Cannot start the Qubes Clipboard Notifier!")
main() main()