qubes-prefs 5.6 KB

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