qvm_run.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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.passio:
  169. if args.color_output is None and args.filter_esc:
  170. args.color_output = 31
  171. if args.color_stderr is None and os.isatty(sys.stderr.fileno()):
  172. args.color_stderr = 31
  173. if len(args.domains) > 1 and args.passio and not args.localcmd:
  174. parser.error('--passio cannot be used when more than 1 qube is chosen '
  175. 'and no --localcmd is used')
  176. if args.localcmd and not args.passio:
  177. parser.error('--localcmd have no effect without --pass-io')
  178. if args.color_output and not args.filter_esc:
  179. parser.error('--color-output must be used with --filter-escape-chars')
  180. retcode = 0
  181. verbose = args.verbose - args.quiet
  182. if args.passio:
  183. verbose -= 1
  184. # --all and --exclude are handled by QubesArgumentParser
  185. domains = args.domains
  186. dispvm = None
  187. if args.dispvm:
  188. if args.exclude:
  189. parser.error('Cannot use --exclude with --dispvm')
  190. dispvm = qubesadmin.vm.DispVM.from_appvm(args.app,
  191. None if args.dispvm is True else args.dispvm)
  192. domains = [dispvm]
  193. elif args.all_domains:
  194. # --all consider only running VMs
  195. domains = [vm for vm in domains if vm.is_running()]
  196. if args.color_output:
  197. sys.stdout.write('\033[0;{}m'.format(args.color_output))
  198. sys.stdout.flush()
  199. if args.color_stderr:
  200. sys.stderr.write('\033[0;{}m'.format(args.color_stderr))
  201. sys.stderr.flush()
  202. copy_proc = None
  203. try:
  204. procs = []
  205. for vm in domains:
  206. if not args.autostart and not vm.is_running():
  207. if verbose > 0:
  208. print_no_color('Qube \'{}\' not started'.format(vm.name),
  209. file=sys.stderr, color=args.color_stderr)
  210. retcode = max(retcode, 1)
  211. continue
  212. try:
  213. if verbose > 0:
  214. print_no_color(
  215. 'Running \'{}\' on {}'.format(args.cmd, vm.name),
  216. file=sys.stderr, color=args.color_stderr)
  217. if args.gui and not args.dispvm:
  218. wait_session = vm.run_service('qubes.WaitForSession',
  219. stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
  220. try:
  221. wait_session.communicate(vm.default_user.encode())
  222. except KeyboardInterrupt:
  223. with contextlib.suppress(ProcessLookupError):
  224. wait_session.send_signal(signal.SIGINT)
  225. break
  226. proc, copy_proc, local_proc = run_command_single(args, vm)
  227. procs.append((vm, proc))
  228. if local_proc:
  229. procs.append((vm, local_proc))
  230. except qubesadmin.exc.QubesException as e:
  231. if args.color_output:
  232. sys.stdout.write('\033[0m')
  233. sys.stdout.flush()
  234. vm.log.error(str(e))
  235. return -1
  236. try:
  237. for vm, proc in procs:
  238. this_retcode = proc.wait()
  239. if this_retcode and verbose > 0:
  240. print_no_color(
  241. '{}: command failed with code: {}'.format(
  242. vm.name, this_retcode),
  243. file=sys.stderr, color=args.color_stderr)
  244. retcode = max(retcode, proc.wait())
  245. except KeyboardInterrupt:
  246. for vm, proc in procs:
  247. with contextlib.suppress(ProcessLookupError):
  248. proc.send_signal(signal.SIGINT)
  249. for vm, proc in procs:
  250. retcode = max(retcode, proc.wait())
  251. finally:
  252. if dispvm:
  253. dispvm.cleanup()
  254. if args.color_output:
  255. sys.stdout.write('\033[0m')
  256. sys.stdout.flush()
  257. if args.color_stderr:
  258. sys.stderr.write('\033[0m')
  259. sys.stderr.flush()
  260. if copy_proc is not None:
  261. copy_proc.terminate()
  262. return retcode
  263. if __name__ == '__main__':
  264. sys.exit(main())