qvm-pci 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/usr/bin/python2
  2. # -*- encoding: utf8 -*-
  3. #
  4. # The Qubes OS Project, http://www.qubes-os.org
  5. #
  6. # Copyright (C) 2010 Marek Marczykowski <marmarek@mimuw.edu.pl>
  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 optparse import OptionParser
  25. import subprocess
  26. import os
  27. import sys
  28. def main():
  29. usage = "usage: %prog -l [options] <vm-name>\n"\
  30. "usage: %prog -a [options] <vm-name> <device>\n"\
  31. "usage: %prog -d [options] <vm-name> <device>\n"\
  32. "List/set VM PCI devices."
  33. parser = OptionParser (usage)
  34. parser.add_option ("-l", "--list", action="store_true", dest="do_list", default=False)
  35. parser.add_option ("-a", "--add", action="store_true", dest="do_add", default=False)
  36. parser.add_option ("-d", "--delete", action="store_true", dest="do_delete", default=False)
  37. (options, args) = parser.parse_args ()
  38. if (len (args) < 1):
  39. parser.error ("You must provide at least the vmname!")
  40. vmname = args[0]
  41. if options.do_list + options.do_add + options.do_delete > 1:
  42. print >> sys.stderr, "Only one of -l -a -d is allowed!"
  43. exit (1)
  44. if options.do_add or options.do_delete:
  45. qvm_collection = QubesVmCollection()
  46. qvm_collection.lock_db_for_writing()
  47. qvm_collection.load()
  48. else:
  49. qvm_collection = QubesVmCollection()
  50. qvm_collection.lock_db_for_reading()
  51. qvm_collection.load()
  52. qvm_collection.unlock_db()
  53. vm = qvm_collection.get_vm_by_name(vmname)
  54. if vm is None or vm.qid not in qvm_collection:
  55. print >> sys.stderr, "A VM with the name '{0}' does not exist in the system.".format(vmname)
  56. exit(1)
  57. if options.do_add:
  58. if len (args) < 2:
  59. print >> sys.stderr, "You must specify the PCI device to add"
  60. exit (1)
  61. pci = args[1]
  62. vm.pci_add(pci)
  63. qvm_collection.save()
  64. qvm_collection.unlock_db()
  65. elif options.do_delete:
  66. if len (args) < 2:
  67. print >> sys.stderr, "You must specify the PCI device to delete"
  68. exit (1)
  69. pci = args[1]
  70. vm.pci_remove(pci)
  71. qvm_collection.save()
  72. qvm_collection.unlock_db()
  73. else:
  74. # do_list
  75. print str(vm.pcidevs)
  76. main()