qvm-prefs 7.2 KB

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