carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 1 | "Module dependency utility functions for Mininet." |
| 2 | |
| 3 | from mininet.util import quietRun |
| 4 | from mininet.log import info, error, debug |
| 5 | from os import environ |
| 6 | |
| 7 | def lsmod(): |
| 8 | "Return output of lsmod." |
| 9 | return quietRun( 'lsmod' ) |
| 10 | |
| 11 | def rmmod( mod ): |
| 12 | """Return output of lsmod. |
| 13 | mod: module string""" |
| 14 | return quietRun( [ 'rmmod', mod ] ) |
| 15 | |
| 16 | def modprobe( mod ): |
| 17 | """Return output of modprobe |
| 18 | mod: module string""" |
| 19 | return quietRun( [ 'modprobe', mod ] ) |
| 20 | |
| 21 | OF_KMOD = 'ofdatapath' |
| 22 | OVS_KMOD = 'openvswitch_mod' # Renamed 'openvswitch' in OVS 1.7+/Linux 3.5+ |
| 23 | TUN = 'tun' |
| 24 | |
| 25 | def moduleDeps( subtract=None, add=None ): |
| 26 | """Handle module dependencies. |
| 27 | subtract: string or list of module names to remove, if already loaded |
| 28 | add: string or list of module names to add, if not already loaded""" |
| 29 | subtract = subtract if subtract is not None else [] |
| 30 | add = add if add is not None else [] |
| 31 | if type( subtract ) is str: |
| 32 | subtract = [ subtract ] |
| 33 | if type( add ) is str: |
| 34 | add = [ add ] |
| 35 | for mod in subtract: |
| 36 | if mod in lsmod(): |
| 37 | info( '*** Removing ' + mod + '\n' ) |
| 38 | rmmodOutput = rmmod( mod ) |
| 39 | if rmmodOutput: |
| 40 | error( 'Error removing ' + mod + ': "%s">\n' % rmmodOutput ) |
| 41 | exit( 1 ) |
| 42 | if mod in lsmod(): |
| 43 | error( 'Failed to remove ' + mod + '; still there!\n' ) |
| 44 | exit( 1 ) |
| 45 | for mod in add: |
| 46 | if mod not in lsmod(): |
| 47 | info( '*** Loading ' + mod + '\n' ) |
| 48 | modprobeOutput = modprobe( mod ) |
| 49 | if modprobeOutput: |
| 50 | error( 'Error inserting ' + mod + |
| 51 | ' - is it installed and available via modprobe?\n' + |
| 52 | 'Error was: "%s"\n' % modprobeOutput ) |
| 53 | if mod not in lsmod(): |
| 54 | error( 'Failed to insert ' + mod + ' - quitting.\n' ) |
| 55 | exit( 1 ) |
| 56 | else: |
| 57 | debug( '*** ' + mod + ' already loaded\n' ) |
| 58 | |
| 59 | |
| 60 | def pathCheck( *args, **kwargs ): |
| 61 | "Make sure each program in *args can be found in $PATH." |
| 62 | moduleName = kwargs.get( 'moduleName', 'it' ) |
| 63 | for arg in args: |
| 64 | if not quietRun( 'which ' + arg ): |
| 65 | error( 'Cannot find required executable %s.\n' % arg + |
| 66 | 'Please make sure that %s is installed ' % moduleName + |
| 67 | 'and available in your $PATH:\n(%s)\n' % environ[ 'PATH' ] ) |
| 68 | exit( 1 ) |