qubes-prefs 5.6 KB

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