unbind_all_network_devices 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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-9][0-9]:[0-9][0-9].[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. print "ERROR: Cannot load xen-pciback module!"
  52. exit(1)
  53. if options.verbose:
  54. print "Unbinding the following net devices:"
  55. net_devices = find_net_devices()
  56. for dev in net_devices:
  57. if options.verbose:
  58. print "--> {0}".format(dev)
  59. retcode = subprocess.call (["/usr/lib/qubes/unbind_pci_device.sh", dev])
  60. if (retcode != 0):
  61. print "WARNING: Could not unbind device {0}".format(dev)
  62. main()