qubes-prefs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 qubes.qubes import QubesHost
  24. from qubes.qubes import system_path
  25. from optparse import OptionParser
  26. import subprocess
  27. import os
  28. import sys
  29. def handle_vm(vms, label, new_value = None):
  30. functions = { # label: [ getter, setter ],
  31. 'default-netvm': [ 'get_default_netvm', 'set_default_netvm' ],
  32. 'default-fw-netvm': [ 'get_default_fw_netvm', 'set_default_fw_netvm' ],
  33. 'default-template': [ 'get_default_template', 'set_default_template' ],
  34. 'clockvm': [ 'get_clockvm_vm', 'set_clockvm_vm' ],
  35. 'updatevm': [ 'get_updatevm_vm', 'set_updatevm_vm' ],
  36. }
  37. assert label in functions.keys()
  38. if new_value:
  39. vm = vms.get_vm_by_name (new_value)
  40. if vm is None:
  41. print >> sys.stderr, "A VM with the name '{0}' does not exist in the system.".format(new_value)
  42. exit(1)
  43. try:
  44. vms.__getattribute__(functions[label][1])(vm)
  45. except Exception as e:
  46. print >> sys.stderr, "ERROR: {0}".format(str(e))
  47. exit(1)
  48. else:
  49. vm = vms.__getattribute__(functions[label][0])()
  50. if vm is not None:
  51. return vm.name
  52. else:
  53. return "none"
  54. def handle_kernel(vms, label, new_value = None):
  55. if new_value is not None:
  56. if not os.path.exists(os.path.join(system_path["qubes_kernels_base_dir"], new_value)):
  57. print >> sys.stderr, "Kernel version {0} not installed.".format(new_value)
  58. print >> sys.stderr, "Available versions:"
  59. for k in os.listdir(system_path["qubes_kernels_base_dir"]):
  60. print >> sys.stderr, " -", k
  61. exit(1)
  62. vms.set_default_kernel(new_value)
  63. else:
  64. return vms.get_default_kernel()
  65. preferences = {
  66. "default-netvm": handle_vm,
  67. "default-fw-netvm": handle_vm,
  68. "default-template": handle_vm,
  69. "clockvm": handle_vm,
  70. "updatevm": handle_vm,
  71. "default-kernel": handle_kernel,
  72. }
  73. def do_list(vms):
  74. label_width = 18
  75. fmt="{{0:<{0}}}: {{1}}".format(label_width)
  76. for pref in sorted(preferences.items()):
  77. print fmt.format (pref[0], pref[1](vms, pref[0]))
  78. def main():
  79. usage = "usage: %prog [-l]\n"\
  80. "usage: %prog [-g] <property>\n"\
  81. "usage: %prog [-s] <property> <new-value>\n"\
  82. "List/set various global properties."
  83. parser = OptionParser (usage)
  84. parser.add_option ("-l", "--list", action="store_true", dest="do_list", default=False)
  85. parser.add_option ("-s", "--set", action="store_true", dest="do_set", default=False)
  86. parser.add_option ("-g", "--get", action="store_true", dest="do_get", default=False)
  87. (options, args) = parser.parse_args ()
  88. if options.do_list + options.do_set + options.do_get > 1:
  89. print >> sys.stderr, "You can provide only one action at once!"
  90. exit (1)
  91. # Select action based on args count:
  92. if not options.do_list and not options.do_get and not options.do_set:
  93. if (len (args) < 1):
  94. options.do_list = True
  95. elif (len (args) == 1):
  96. options.do_get = True
  97. else:
  98. options.do_set = True
  99. if options.do_set:
  100. qvm_collection = QubesVmCollection()
  101. qvm_collection.lock_db_for_writing()
  102. qvm_collection.load()
  103. else:
  104. qvm_collection = QubesVmCollection()
  105. qvm_collection.lock_db_for_reading()
  106. qvm_collection.load()
  107. qvm_collection.unlock_db()
  108. if options.do_set:
  109. if len (args) < 2 or args[0] not in preferences.keys():
  110. print >> sys.stderr, "You must specify the property and the new value you wish to set..."
  111. print >> sys.stderr, "Available properties:"
  112. for p in sorted(preferences.keys()):
  113. print >> sys.stderr, "--> '{0}'".format(p)
  114. exit (1)
  115. pref = args[0]
  116. new_value = args[1]
  117. preferences[pref](qvm_collection, pref, new_value)
  118. qvm_collection.save()
  119. qvm_collection.unlock_db()
  120. elif options.do_get:
  121. if len (args) < 1 or args[0] not in preferences.keys():
  122. print >> sys.stderr, "You must specify the property you wish to get..."
  123. print >> sys.stderr, "Available properties:"
  124. for p in sorted(preferences.keys()):
  125. print >> sys.stderr, "--> '{0}'".format(p)
  126. exit (1)
  127. pref = args[0]
  128. print preferences[pref](qvm_collection, pref)
  129. else:
  130. # do_list
  131. do_list(qvm_collection)
  132. main()