qvm_shutdown.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #
  2. # The Qubes OS Project, http://www.qubes-os.org
  3. #
  4. # Copyright (C) 2010-2016 Joanna Rutkowska <joanna@invisiblethingslab.com>
  5. # Copyright (C) 2011-2016 Marek Marczykowski-Górecki
  6. # <marmarek@invisiblethingslab.com>
  7. # Copyright (C) 2016 Wojtek Porczyk <woju@invisiblethingslab.com>
  8. #
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation; either version 2 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License along
  20. # with this program; if not, write to the Free Software Foundation, Inc.,
  21. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22. #
  23. ''' Shutdown a qube '''
  24. from __future__ import print_function
  25. import sys
  26. import time
  27. import qubes.config
  28. import qubes.tools
  29. parser = qubes.tools.QubesArgumentParser(
  30. description=__doc__, vmname_nargs='+')
  31. parser.add_argument('--force',
  32. action='store_true', default=False,
  33. help='force operation, even if may damage other VMs (eg. shutdown of'
  34. ' network provider)')
  35. parser.add_argument('--wait',
  36. action='store_true', default=False,
  37. help='wait for the VMs to shut down')
  38. parser.add_argument('--timeout',
  39. action='store', type=float,
  40. default=qubes.config.defaults['shutdown_counter_max'],
  41. help='timeout after which domains are killed when using --wait'
  42. ' (default: %d)')
  43. def main(args=None): # pylint: disable=missing-docstring
  44. args = parser.parse_args(args)
  45. for vm in args.domains:
  46. if not vm.is_halted():
  47. vm.shutdown(force=args.force)
  48. if not args.wait:
  49. return
  50. timeout = args.timeout
  51. current_vms = list(sorted(args.domains))
  52. while timeout >= 0:
  53. current_vms = [vm for vm in current_vms
  54. if vm.get_power_state() != 'Halted']
  55. if not current_vms:
  56. return 0
  57. args.app.log.info('Waiting for shutdown ({}): {}'.format(
  58. timeout, ', '.join([str(vm) for vm in current_vms])))
  59. time.sleep(1)
  60. timeout -= 1
  61. args.app.log.info(
  62. 'Killing remaining qubes: {}'
  63. .format(', '.join([str(vm) for vm in current_vms])))
  64. for vm in current_vms:
  65. vm.force_shutdown()
  66. if __name__ == '__main__':
  67. sys.exit(main())