qvm_remove.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #
  2. # The Qubes OS Project, http://www.qubes-os.org
  3. #
  4. # Copyright (C) 2017 Marek Marczykowski-Górecki
  5. # <marmarek@invisiblethingslab.com>
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU Lesser General Public License as published by
  9. # the Free Software Foundation; either version 2.1 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Lesser General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Lesser General Public License along
  18. # with this program; if not, write to the Free Software Foundation, Inc.,
  19. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. #
  21. ''' Remove domains from the system '''
  22. import sys
  23. import qubesadmin.exc
  24. from qubesadmin.tools import QubesArgumentParser
  25. import qubesadmin.utils
  26. parser = QubesArgumentParser(description=__doc__,
  27. want_app=True,
  28. vmname_nargs='+')
  29. parser.add_argument("--force", "-f", action="store_true", dest="no_confirm",
  30. default=False, help="Do not prompt for confirmation")
  31. def main(args=None, app=None): # pylint: disable=missing-docstring
  32. args = parser.parse_args(args, app=app)
  33. go_ahead = ""
  34. if not args.no_confirm:
  35. print("This will completely remove the selected VM(s)...")
  36. for vm in args.domains:
  37. print(" ", vm.name)
  38. go_ahead = input("Are you sure? [y/N] ").upper()
  39. if args.no_confirm or go_ahead == "Y":
  40. for vm in args.domains:
  41. try:
  42. del args.app.domains[vm.name]
  43. except qubesadmin.exc.QubesVMInUseError:
  44. dependencies = qubesadmin.utils.vm_dependencies(vm.app, vm)
  45. print("VM {} cannot be removed. It is in use as:".format(
  46. vm.name))
  47. for (holder, prop) in dependencies:
  48. if holder:
  49. print(" - {} for {}".format(prop, holder.name))
  50. else:
  51. print(" - global property {}".format(prop))
  52. except qubesadmin.exc.QubesException as e:
  53. parser.error_runtime(e)
  54. retcode = 0
  55. else:
  56. print("Remove cancelled.")
  57. retcode = 1
  58. return retcode
  59. if __name__ == '__main__':
  60. sys.exit(main())