qvm_run.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. # -*- encoding: utf8 -*-
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2017 Marek Marczykowski-Górecki
  6. # <marmarek@invisiblethingslab.com>
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU Lesser General Public License as published by
  10. # the Free Software Foundation; either version 2.1 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU Lesser General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public License along
  19. # with this program; if not, see <http://www.gnu.org/licenses/>.
  20. ''' qvm-run tool'''
  21. import contextlib
  22. import os
  23. import signal
  24. import subprocess
  25. import sys
  26. import multiprocessing
  27. import select
  28. import qubesadmin.tools
  29. import qubesadmin.exc
  30. parser = qubesadmin.tools.QubesArgumentParser()
  31. parser.add_argument('--user', '-u', metavar='USER',
  32. help='run command in a qube as USER (available only from dom0)')
  33. parser.add_argument('--autostart', '--auto', '-a',
  34. action='store_true', default=True,
  35. help='option ignored, this is default')
  36. parser.add_argument('--no-autostart', '--no-auto', '-n',
  37. action='store_false', dest='autostart',
  38. help='do not autostart qube')
  39. parser.add_argument('--pass-io', '-p',
  40. action='store_true', dest='passio', default=False,
  41. help='pass stdio from remote program')
  42. parser.add_argument('--localcmd', metavar='COMMAND',
  43. help='with --pass-io, pass stdio to the given program')
  44. parser.add_argument('--gui',
  45. action='store_true', default=True,
  46. help='run the command with GUI (default on)')
  47. parser.add_argument('--no-gui', '--nogui',
  48. action='store_false', dest='gui',
  49. help='run the command without GUI')
  50. parser.add_argument('--colour-output', '--color-output', metavar='COLOUR',
  51. action='store', dest='color_output', default=None,
  52. help='mark the qube output with given ANSI colour (ie. "31" for red)')
  53. parser.add_argument('--colour-stderr', '--color-stderr', metavar='COLOUR',
  54. action='store', dest='color_stderr', default=None,
  55. help='mark the qube stderr with given ANSI colour (ie. "31" for red)')
  56. parser.add_argument('--no-colour-output', '--no-color-output',
  57. action='store_false', dest='color_output',
  58. help='disable colouring the stdio')
  59. parser.add_argument('--no-colour-stderr', '--no-color-stderr',
  60. action='store_false', dest='color_stderr',
  61. help='disable colouring the stderr')
  62. parser.add_argument('--filter-escape-chars',
  63. action='store_true', dest='filter_esc',
  64. default=os.isatty(sys.stdout.fileno()),
  65. help='filter terminal escape sequences (default if output is terminal)')
  66. parser.add_argument('--no-filter-escape-chars',
  67. action='store_false', dest='filter_esc',
  68. help='do not filter terminal escape sequences; DANGEROUS when output is a'
  69. ' terminal emulator')
  70. parser.add_argument('--service',
  71. action='store_true', dest='service',
  72. help='run a qrexec service (named by COMMAND) instead of shell command')
  73. target_parser = parser.add_mutually_exclusive_group()
  74. target_parser.add_argument('--dispvm', action='store', nargs='?',
  75. const=True, metavar='BASE_APPVM',
  76. help='start a service in new Disposable VM; '
  77. 'optionally specify base AppVM for DispVM')
  78. target_parser.add_argument('VMNAME',
  79. nargs='?',
  80. action=qubesadmin.tools.VmNameAction)
  81. # add those manually instead of vmname_args, because of mutually exclusive
  82. # group with --dispvm; parsing is still handled by QubesArgumentParser
  83. target_parser.add_argument('--all', action='store_true', dest='all_domains',
  84. help='run command on all running qubes')
  85. parser.add_argument('--exclude', action='append', default=[],
  86. help='exclude the qube from --all')
  87. parser.add_argument('cmd', metavar='COMMAND',
  88. help='command or service to run')
  89. def copy_stdin(stream):
  90. '''Copy stdin to *stream*'''
  91. # multiprocessing.Process have sys.stdin connected to /dev/null, use fd 0
  92. # directly
  93. while True:
  94. try:
  95. # select so this code works even if fd 0 is non-blocking
  96. select.select([0], [], [])
  97. data = os.read(0, 65536)
  98. if data is None or data == b'':
  99. break
  100. stream.write(data)
  101. stream.flush()
  102. except KeyboardInterrupt:
  103. break
  104. stream.close()
  105. def print_no_color(msg, file, color):
  106. '''Print a *msg* to *file* without coloring it.
  107. Namely reset to base color first, print a message, then restore color.
  108. '''
  109. if color:
  110. print('\033[0m{}\033[0;{}m'.format(msg, color), file=file)
  111. else:
  112. print(msg, file=file)
  113. def run_command_single(args, vm):
  114. '''Handle a single VM to run the command in'''
  115. run_kwargs = {}
  116. if not args.passio:
  117. run_kwargs['stdout'] = subprocess.DEVNULL
  118. run_kwargs['stderr'] = subprocess.DEVNULL
  119. elif args.localcmd:
  120. run_kwargs['stdin'] = subprocess.PIPE
  121. run_kwargs['stdout'] = subprocess.PIPE
  122. run_kwargs['stderr'] = None
  123. else:
  124. # connect process output to stdout/err directly if --pass-io is given
  125. run_kwargs['stdout'] = None
  126. run_kwargs['stderr'] = None
  127. if args.filter_esc:
  128. run_kwargs['filter_esc'] = True
  129. if isinstance(args.app, qubesadmin.app.QubesLocal) and \
  130. not args.passio and \
  131. not args.localcmd and \
  132. args.service and \
  133. not args.dispvm:
  134. # wait=False works only in dom0; but it's still useful, to save on
  135. # simultaneous vchan connections
  136. run_kwargs['wait'] = False
  137. copy_proc = None
  138. local_proc = None
  139. if args.service:
  140. service = args.cmd
  141. else:
  142. service = 'qubes.VMShell'
  143. if args.gui and args.dispvm:
  144. service += '+WaitForSession'
  145. proc = vm.run_service(service,
  146. user=args.user,
  147. **run_kwargs)
  148. if not args.service:
  149. proc.stdin.write(vm.prepare_input_for_vmshell(args.cmd))
  150. proc.stdin.flush()
  151. if args.localcmd:
  152. local_proc = subprocess.Popen(args.localcmd,
  153. shell=True,
  154. stdout=proc.stdin,
  155. stdin=proc.stdout)
  156. # stdin is closed below
  157. proc.stdout.close()
  158. elif args.passio:
  159. copy_proc = multiprocessing.Process(target=copy_stdin,
  160. args=(proc.stdin,))
  161. copy_proc.start()
  162. # keep the copying process running
  163. proc.stdin.close()
  164. return proc, copy_proc, local_proc
  165. def main(args=None, app=None):
  166. '''Main function of qvm-run tool'''
  167. args = parser.parse_args(args, app=app)
  168. if args.color_output is None and args.filter_esc:
  169. args.color_output = '31'
  170. if args.color_stderr is None and os.isatty(sys.stderr.fileno()):
  171. args.color_stderr = 31
  172. if len(args.domains) > 1 and args.passio and not args.localcmd:
  173. parser.error('--passio cannot be used when more than 1 qube is chosen '
  174. 'and no --localcmd is used')
  175. if args.localcmd and not args.passio:
  176. parser.error('--localcmd have no effect without --pass-io')
  177. if args.color_output and not args.filter_esc:
  178. parser.error('--color-output must be used with --filter-escape-chars')
  179. retcode = 0
  180. verbose = args.verbose - args.quiet
  181. if args.passio:
  182. verbose -= 1
  183. # --all and --exclude are handled by QubesArgumentParser
  184. domains = args.domains
  185. dispvm = None
  186. if args.dispvm:
  187. if args.exclude:
  188. parser.error('Cannot use --exclude with --dispvm')
  189. dispvm = qubesadmin.vm.DispVM.from_appvm(args.app,
  190. None if args.dispvm is True else args.dispvm)
  191. domains = [dispvm]
  192. elif args.all_domains:
  193. # --all consider only running VMs
  194. domains = [vm for vm in domains if vm.is_running()]
  195. if args.color_output:
  196. sys.stdout.write('\033[0;{}m'.format(args.color_output))
  197. sys.stdout.flush()
  198. if args.color_stderr:
  199. sys.stderr.write('\033[0;{}m'.format(args.color_stderr))
  200. sys.stderr.flush()
  201. copy_proc = None
  202. try:
  203. procs = []
  204. for vm in domains:
  205. if not args.autostart and not vm.is_running():
  206. if verbose > 0:
  207. print_no_color('Qube \'{}\' not started'.format(vm.name),
  208. file=sys.stderr, color=args.color_stderr)
  209. retcode = max(retcode, 1)
  210. continue
  211. try:
  212. if verbose > 0:
  213. print_no_color(
  214. 'Running \'{}\' on {}'.format(args.cmd, vm.name),
  215. file=sys.stderr, color=args.color_stderr)
  216. if args.gui and not args.dispvm:
  217. wait_session = vm.run_service('qubes.WaitForSession',
  218. stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
  219. try:
  220. wait_session.communicate(vm.default_user.encode())
  221. except KeyboardInterrupt:
  222. with contextlib.suppress(ProcessLookupError):
  223. wait_session.send_signal(signal.SIGINT)
  224. break
  225. proc, copy_proc, local_proc = run_command_single(args, vm)
  226. procs.append((vm, proc))
  227. if local_proc:
  228. procs.append((vm, local_proc))
  229. except qubesadmin.exc.QubesException as e:
  230. if args.color_output:
  231. sys.stdout.write('\033[0m')
  232. sys.stdout.flush()
  233. vm.log.error(str(e))
  234. return -1
  235. try:
  236. for vm, proc in procs:
  237. this_retcode = proc.wait()
  238. if this_retcode and args.verbose > 0:
  239. print_no_color(
  240. '{}: command failed with code: {}'.format(
  241. vm.name, this_retcode),
  242. file=sys.stderr, color=args.color_stderr)
  243. retcode = max(retcode, proc.wait())
  244. except KeyboardInterrupt:
  245. for vm, proc in procs:
  246. with contextlib.suppress(ProcessLookupError):
  247. proc.send_signal(signal.SIGINT)
  248. for vm, proc in procs:
  249. retcode = max(retcode, proc.wait())
  250. finally:
  251. if dispvm:
  252. dispvm.cleanup()
  253. if args.color_output:
  254. sys.stdout.write('\033[0m')
  255. sys.stdout.flush()
  256. if args.color_stderr:
  257. sys.stderr.write('\033[0m')
  258. sys.stderr.flush()
  259. if copy_proc is not None:
  260. copy_proc.terminate()
  261. return retcode
  262. if __name__ == '__main__':
  263. sys.exit(main())