unbind_all_network_devices 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 optparse import OptionParser
  23. import subprocess
  24. import shutil
  25. import re
  26. def find_net_devices():
  27. p = subprocess.Popen (["lspci", "-mm", "-n"], stdout=subprocess.PIPE)
  28. result = p.communicate()
  29. retcode = p.returncode
  30. if (retcode != 0):
  31. print "ERROR when executing lspci!"
  32. raise IOError
  33. net_devices = set()
  34. rx_netdev = re.compile (r"^([0-9a-f][0-9a-f]:[0-9a-f][0-9a-f].[0-9]) \"02")
  35. for dev in str(result[0]).splitlines():
  36. match = rx_netdev.match (dev)
  37. if match is not None:
  38. dev_bdf = match.group(1)
  39. assert dev_bdf is not None
  40. net_devices.add (dev_bdf)
  41. return net_devices
  42. def main():
  43. usage = "usage: %prog [options] <netvm-name>"
  44. parser = OptionParser (usage)
  45. parser.add_option ("-v", "--verbose", dest="verbose", action="store_true", default=False)
  46. (options, args) = parser.parse_args ()
  47. if options.verbose:
  48. print "Loading Xen PCI Backend..."
  49. retcode = subprocess.call (["/sbin/modprobe", "xen-pciback"])
  50. if retcode != 0:
  51. retcode = subprocess.call (["/sbin/modprobe", "pciback"])
  52. if retcode != 0:
  53. print "ERROR: Cannot load xen-pciback module!"
  54. exit(1)
  55. if options.verbose:
  56. print "Unbinding the following net devices:"
  57. net_devices = find_net_devices()
  58. for dev in net_devices:
  59. if options.verbose:
  60. print "--> {0}".format(dev)
  61. retcode = subprocess.call (["/usr/lib/qubes/unbind_pci_device.sh", dev])
  62. if (retcode != 0):
  63. print "WARNING: Could not unbind device {0}".format(dev)
  64. main()