qubes-prefs 5.6 KB

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