qvm-prefs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #!/usr/bin/python2.6
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2010 Joanna Rutkowska <joanna@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 QubesVmLabels
  24. from optparse import OptionParser
  25. import subprocess
  26. def do_list(vm):
  27. label_width = 18
  28. fmt="{{0:<{0}}}: {{1}}".format(label_width)
  29. print fmt.format ("name", vm.name)
  30. print fmt.format ("label", vm.label.name)
  31. print fmt.format ("type", vm.type)
  32. if vm.is_appvm():
  33. print fmt.format ("template", vm.template_vm.name)
  34. if vm.netvm_vm is not None:
  35. print fmt.format ("netvm", vm.netvm_vm.name)
  36. print fmt.format ("updateable?", vm.is_updateable())
  37. print fmt.format ("installed by RPM?", vm.installed_by_rpm)
  38. print fmt.format ("dir", vm.dir_path)
  39. print fmt.format ("config", vm.conf_file)
  40. if not vm.is_appvm():
  41. print fmt.format ("root img", vm.root_img)
  42. if vm.is_appvm():
  43. print fmt.format ("root img", vm.template_vm.root_img)
  44. print fmt.format ("root COW img", vm.rootcow_img)
  45. print fmt.format ("private img", vm.private_img)
  46. def set_label(vms, vm, args):
  47. if len (args) != 1:
  48. print "Missing label name argument!"
  49. label = args[0]
  50. if label not in QubesVmLabels:
  51. print "Wrong label name, supported values are the following:"
  52. for l in QubesVmLabels.values():
  53. print "* {0}".format(l.name)
  54. exit (1)
  55. vm.label = QubesVmLabels[label]
  56. subprocess.check_call (["ln", "-sf", vm.label.icon_path, vm.icon_path])
  57. def set_netvm(vms, vm, args):
  58. if len (args) != 1:
  59. print "Missing netvm name argument!"
  60. print "Possible values:"
  61. print "1) default"
  62. print "2) none"
  63. print "3) <vmaname>"
  64. return
  65. netvm = args[0]
  66. if netvm == "none":
  67. netvm_vm = None
  68. vm.uses_default_netvm = False
  69. elif netvm == "default":
  70. netvm_vm = vms.get_default_netvm_vm()
  71. vm.uses_default_netvm = True
  72. else:
  73. netvm_vm = vms.get_vm_by_name (netvm)
  74. if netvm_vm is None:
  75. print "A VM with the name '{0}' does not exist in the system.".format(netvm)
  76. exit(1)
  77. if not netvm_vm.is_netvm():
  78. print "VM '{0}' is not a NetVM".format(netvm)
  79. exit (1)
  80. vm.uses_default_netvm = False
  81. vm.netvm_vm = netvm_vm
  82. def set_updateable(vms, vm, args):
  83. if vm.is_updateable():
  84. print "VM '{0}' is already set 'updateable', no action required.".format(vm.name)
  85. return True
  86. if vm.is_running():
  87. print "Cannot change 'updateable' attribute of a running VM. Shut it down first."
  88. return False
  89. if vm.is_appvm():
  90. # Check if the Template is *non* updateable...
  91. if not vm.template_vm.is_updateable():
  92. print "VM '{0}': Setting 'updateable' attribute to True.".format(vm.name)
  93. vm.set_updateable()
  94. else:
  95. print "The Template VM ('{0}') is marked as 'updateable' itself!".format(vm.template_vm.name)
  96. print "Cannot make the AppVM updateable too, as this might cause COW-backed storage incoherency."
  97. print "If you want to make this AppVM updateable, you must first make the Template VM nonupdateable."
  98. return False
  99. if vm.is_templete():
  100. # Make sure that all the AppVMs are non-updateable...
  101. for appvm in vm.appvms.values():
  102. if appvm.is_updateable():
  103. print "At least one of the AppVMs ('{0}') of this Template VM is also marked 'updateable'.".format(appvm.name)
  104. print "Cannot make the Template VM updateable too, as this might cause COW-backed storage incoherency."
  105. print "If you want to make this Template VM updateable, you must first make all its decedent AppVMs nonupdateable."
  106. return False
  107. print "VM '{0}': Setting 'updateable' attribute to True.".format(vm.name)
  108. vm.set_updateable()
  109. return True
  110. def set_nonupdateable(vms, vm, args):
  111. if not vm.is_updateable():
  112. print "VM '{0}' is already set 'nonupdateable', no action required.".format(vm.name)
  113. return True
  114. if vm.is_running():
  115. print "Cannot change 'updateable' attribute of a running VM. Shut it down first."
  116. return False
  117. if vm.is_netvm():
  118. print "Why, on earth, would you want to make a NetVM 'nonupdateable'?"
  119. return False
  120. print "VM '{0}': Setting 'updateable' attribute to False.".format(vm.name)
  121. vm.set_nonupdateable()
  122. return True
  123. properties = {
  124. "updateable": set_updateable,
  125. "nonupdateable": set_nonupdateable,
  126. "label" : set_label,
  127. "netvm" : set_netvm,
  128. }
  129. def do_set(vms, vm, property, args):
  130. if property not in properties.keys():
  131. print "ERROR: Wrong property name: '{0}'".format(property)
  132. return False
  133. return properties[property](vms, vm, args)
  134. def main():
  135. usage = "usage: %prog -l [options] <vm-name>\n"\
  136. "usage: %prog -s [options] <vm-name> <property> [...]\n"\
  137. "List/set various per-VM properties."
  138. parser = OptionParser (usage)
  139. parser.add_option ("-l", "--list", action="store_true", dest="do_list", default=False)
  140. parser.add_option ("-s", "--set", action="store_true", dest="do_set", default=False)
  141. (options, args) = parser.parse_args ()
  142. if (len (args) < 1):
  143. parser.error ("You must provide at least the vmname!")
  144. vmname = args[0]
  145. if options.do_list and options.do_set:
  146. print "You cannot provide -l and -s at the same time!"
  147. exit (1)
  148. if options.do_set:
  149. qvm_collection = QubesVmCollection()
  150. qvm_collection.lock_db_for_writing()
  151. qvm_collection.load()
  152. else:
  153. qvm_collection = QubesVmCollection()
  154. qvm_collection.lock_db_for_reading()
  155. qvm_collection.load()
  156. qvm_collection.unlock_db()
  157. vm = qvm_collection.get_vm_by_name(vmname)
  158. if vm is None or vm.qid not in qvm_collection:
  159. print "A VM with the name '{0}' does not exist in the system.".format(vmname)
  160. exit(1)
  161. if options.do_set:
  162. if len (args) < 2:
  163. print "You must specify the property you wish to set..."
  164. print "Available properties:"
  165. for p in properties.keys():
  166. print "--> '{0}'".format(p)
  167. exit (1)
  168. property = args[1]
  169. do_set(qvm_collection, vm, property, args[2:])
  170. qvm_collection.save()
  171. qvm_collection.unlock_db()
  172. else:
  173. # do_list
  174. do_list(vm)
  175. main()