qvm-pci 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. import re
  30. def find_devices_of_class(klass):
  31. p = subprocess.Popen(["/sbin/lspci", "-mm", "-n"], stdout=subprocess.PIPE)
  32. result = p.communicate()
  33. retcode = p.returncode
  34. if retcode != 0:
  35. print "ERROR when executing lspci!"
  36. raise IOError
  37. rx_netdev = re.compile(r"^([0-9a-f]{2}:[0-9a-f]{2}.[0-9a-f]) \"" +
  38. klass)
  39. for dev in str(result[0]).splitlines():
  40. match = rx_netdev.match(dev)
  41. if match is not None:
  42. dev_bdf = match.group(1)
  43. assert dev_bdf is not None
  44. yield dev_bdf
  45. def main():
  46. usage = "usage: %prog -l [options] <vm-name>\n"\
  47. "usage: %prog -a [options] <vm-name> <device>\n"\
  48. "usage: %prog -d [options] <vm-name> <device>\n"\
  49. "List/set VM PCI devices."
  50. parser = OptionParser (usage)
  51. parser.add_option ("-l", "--list", action="store_true", dest="do_list", default=False)
  52. parser.add_option ("-a", "--add", action="store_true", dest="do_add", default=False)
  53. parser.add_option ("-d", "--delete", action="store_true", dest="do_delete", default=False)
  54. parser.add_option("-C", "--add-class", action="store_true",
  55. dest="do_add_class", default=False,
  56. help="Add all devices of given class (net, usb)")
  57. parser.add_option ("--offline-mode", dest="offline_mode",
  58. action="store_true", default=False,
  59. help="Offline mode")
  60. (options, args) = parser.parse_args ()
  61. if (len (args) < 1):
  62. parser.error ("You must provide at least the vmname!")
  63. vmname = args[0]
  64. if options.do_list + options.do_add + options.do_delete + \
  65. options.do_add_class > 1:
  66. print >> sys.stderr, "Only one of -l -a -d -C is allowed!"
  67. exit(1)
  68. if options.offline_mode:
  69. vmm.offline_mode = True
  70. if options.do_add or options.do_delete or options.do_add_class:
  71. qvm_collection = QubesVmCollection()
  72. qvm_collection.lock_db_for_writing()
  73. qvm_collection.load()
  74. else:
  75. qvm_collection = QubesVmCollection()
  76. qvm_collection.lock_db_for_reading()
  77. qvm_collection.load()
  78. qvm_collection.unlock_db()
  79. vm = qvm_collection.get_vm_by_name(vmname)
  80. if vm is None or vm.qid not in qvm_collection:
  81. print >> sys.stderr, "A VM with the name '{0}' does not exist in the system.".format(vmname)
  82. exit(1)
  83. if options.do_add:
  84. if len (args) < 2:
  85. print >> sys.stderr, "You must specify the PCI device to add"
  86. exit (1)
  87. pci = args[1]
  88. vm.pci_add(pci)
  89. qvm_collection.save()
  90. qvm_collection.unlock_db()
  91. elif options.do_add_class:
  92. if len(args) < 2:
  93. print >> sys.stderr, "You must specify the PCI device class to add"
  94. exit(1)
  95. klass = args[1]
  96. if klass == 'net':
  97. devs = find_devices_of_class("02")
  98. elif klass == 'usb':
  99. devs = find_devices_of_class("0c03")
  100. else:
  101. print >> sys.stderr, "Supported classes: net, usb"
  102. exit(1)
  103. for dev in devs:
  104. vm.pci_add(dev)
  105. qvm_collection.save()
  106. qvm_collection.unlock_db()
  107. elif options.do_delete:
  108. if len (args) < 2:
  109. print >> sys.stderr, "You must specify the PCI device to delete"
  110. exit (1)
  111. pci = args[1]
  112. vm.pci_remove(pci)
  113. qvm_collection.save()
  114. qvm_collection.unlock_db()
  115. else:
  116. # do_list
  117. print str(vm.pcidevs)
  118. main()