qvm-pci 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. from qubes.qubes import vmm
  29. def main():
  30. usage = "usage: %prog -l [options] <vm-name>\n"\
  31. "usage: %prog -a [options] <vm-name> <device>\n"\
  32. "usage: %prog -d [options] <vm-name> <device>\n"\
  33. "List/set VM PCI devices."
  34. parser = OptionParser (usage)
  35. parser.add_option ("-l", "--list", action="store_true", dest="do_list", default=False)
  36. parser.add_option ("-a", "--add", action="store_true", dest="do_add", default=False)
  37. parser.add_option ("-d", "--delete", action="store_true", dest="do_delete", default=False)
  38. parser.add_option ("--offline-mode", dest="offline_mode",
  39. action="store_true", default=False,
  40. help="Offline mode")
  41. (options, args) = parser.parse_args ()
  42. if (len (args) < 1):
  43. parser.error ("You must provide at least the vmname!")
  44. vmname = args[0]
  45. if options.do_list + options.do_add + options.do_delete > 1:
  46. print >> sys.stderr, "Only one of -l -a -d is allowed!"
  47. exit (1)
  48. if options.offline_mode:
  49. vmm.offline_mode = True
  50. if options.do_add or options.do_delete:
  51. qvm_collection = QubesVmCollection()
  52. qvm_collection.lock_db_for_writing()
  53. qvm_collection.load()
  54. else:
  55. qvm_collection = QubesVmCollection()
  56. qvm_collection.lock_db_for_reading()
  57. qvm_collection.load()
  58. qvm_collection.unlock_db()
  59. vm = qvm_collection.get_vm_by_name(vmname)
  60. if vm is None or vm.qid not in qvm_collection:
  61. print >> sys.stderr, "A VM with the name '{0}' does not exist in the system.".format(vmname)
  62. exit(1)
  63. if options.do_add:
  64. if len (args) < 2:
  65. print >> sys.stderr, "You must specify the PCI device to add"
  66. exit (1)
  67. pci = args[1]
  68. vm.pci_add(pci)
  69. qvm_collection.save()
  70. qvm_collection.unlock_db()
  71. elif options.do_delete:
  72. if len (args) < 2:
  73. print >> sys.stderr, "You must specify the PCI device to delete"
  74. exit (1)
  75. pci = args[1]
  76. vm.pci_remove(pci)
  77. qvm_collection.save()
  78. qvm_collection.unlock_db()
  79. else:
  80. # do_list
  81. print str(vm.pcidevs)
  82. main()