qvm-pci 3.4 KB

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