qvm-check 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #!/usr/bin/python2
  2. # -*- encoding: utf8 -*-
  3. #
  4. # The Qubes OS Project, http://www.qubes-os.org
  5. #
  6. # Copyright (C) 2011 Marek Marczykowski <marmarek@invisiblethingslab.com>
  7. #
  8. # This program is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU General Public License
  10. # as published by the Free Software Foundation; either version 2
  11. # of the License, or (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 General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program; if not, write to the Free Software
  20. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  21. #
  22. #
  23. from qubes.qubes import QubesVmCollection,QubesException
  24. from optparse import OptionParser
  25. import sys
  26. import time
  27. def main():
  28. usage = """usage: %prog [options] <vm-name>\n
  29. Specify no state options to check if VM exists"""
  30. parser = OptionParser (usage)
  31. parser.add_option ("-q", "--quiet", action="store_false", dest="verbose", default=True)
  32. parser.add_option ("--running", action="store_true", dest="running", default=False,
  33. help="Determine if VM is running")
  34. parser.add_option ("--paused", action="store_true", dest="paused", default=False,
  35. help="Determine if VM is paused")
  36. parser.add_option ("--template", action="store_true", dest="template", default=False,
  37. help="Determine if VM is a template")
  38. (options, args) = parser.parse_args ()
  39. if (len (args) != 1):
  40. parser.error ("You must specify VM name!")
  41. qvm_collection = QubesVmCollection()
  42. qvm_collection.lock_db_for_reading()
  43. qvm_collection.load()
  44. qvm_collection.unlock_db()
  45. vmname = args[0]
  46. vm = qvm_collection.get_vm_by_name(vmname)
  47. if vm is None:
  48. if options.verbose:
  49. print >> sys.stderr, "A VM with the name '{0}' does not exist in the system!".format(vmname)
  50. exit(1)
  51. elif options.running:
  52. vm_state = not vm.is_running()
  53. if options.verbose:
  54. print >> sys.stderr, "A VM with the name {0} is {1}running.".format(vmname, "not " * vm_state)
  55. exit(vm_state)
  56. elif options.paused:
  57. vm_state = not vm.is_paused()
  58. if options.verbose:
  59. print >> sys.stderr, "A VM with the name {0} is {1}paused.".format(vmname, "not " * vm_state)
  60. exit(vm_state)
  61. elif options.template:
  62. vm_state = not vm.is_template()
  63. if options.verbose:
  64. print >> sys.stderr, "A VM with the name {0} is {1}a template.".format(vmname, "not " * vm_state)
  65. exit(vm_state)
  66. else:
  67. if options.verbose:
  68. print >> sys.stderr, "A VM with the name '{0}' does exist.".format(vmname)
  69. exit(0)
  70. main()