qvm-service 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #!/usr/bin/python
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2012 Marek Marczykowski <marmarek@invisiblethingslab.com>
  6. #
  7. # This program is free software; you can redistribute it and/or
  8. # modify it under the terms of the GNU General Public License
  9. # as published by the Free Software Foundation; either version 2
  10. # of the License, or (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 General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program; if not, write to the Free Software
  19. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. #
  21. #
  22. from qubes.qubes import QubesVmCollection
  23. from optparse import OptionParser;
  24. import subprocess
  25. import sys
  26. import re
  27. def do_list(vm):
  28. max_len = 0
  29. for s in vm.services.keys():
  30. max_len = max(max_len, len(s))
  31. fmt="{{0:<{0}}}: {{1}}".format(max_len)
  32. for s in vm.services.keys():
  33. print fmt.format (s, "Enabled" if vm.services[s] else "Disabled")
  34. def main():
  35. usage = "usage: %prog <vm-name> [action] [service]\n"
  36. parser = OptionParser (usage)
  37. parser.add_option ("-l", "--list", dest="do_list", action="store_true", default=True,
  38. help="List services (default action)")
  39. parser.add_option ("-e", "--enable", dest="set_enable", action="store_true", default=False,
  40. help="Enable service")
  41. parser.add_option ("-d", "--disable", dest="set_disable", action="store_true", default=False,
  42. help="Disable service")
  43. parser.add_option ("-D", "--default", dest="set_default", action="store_true", default=False,
  44. help="Reset service to its default state (remove from the list)")
  45. (options, args) = parser.parse_args ()
  46. if (len (args) < 1):
  47. parser.error ("You must specify VM name!")
  48. vmname = args[0]
  49. args = args[1:]
  50. if options.set_enable or options.set_disable or options.set_default:
  51. if (len(args) < 1):
  52. parser.error("You must specify service name!")
  53. options.do_list = False
  54. qvm_collection = QubesVmCollection()
  55. if options.do_list:
  56. qvm_collection.lock_db_for_reading()
  57. qvm_collection.load()
  58. qvm_collection.unlock_db()
  59. else:
  60. qvm_collection.lock_db_for_writing()
  61. qvm_collection.load()
  62. vm = qvm_collection.get_vm_by_name(vmname)
  63. if vm is None:
  64. print >> sys.stderr, "A VM with the name '{0}' does not exist in the system.".format(vmname)
  65. exit(1)
  66. changed = False
  67. if options.do_list:
  68. do_list(vm)
  69. elif options.set_enable:
  70. vm.services[args[0]] = True
  71. changed = True
  72. elif options.set_disable:
  73. vm.services[args[0]] = False
  74. changed = True
  75. elif options.set_default:
  76. if vm.services.has_key(args[0]):
  77. vm.services.pop(args[0])
  78. changed = True
  79. if changed:
  80. qvm_collection.save()
  81. if not options.do_list:
  82. qvm_collection.unlock_db()
  83. main()