blob: 862c1f64d38833645f53a5bf5bcfd73334acec15 [file] [log] [blame]
carlosmscabralf40ecd12013-02-01 18:15:58 -02001"Module dependency utility functions for Mininet."
2
3from mininet.util import quietRun
4from mininet.log import info, error, debug
5from os import environ
6
7def lsmod():
8 "Return output of lsmod."
9 return quietRun( 'lsmod' )
10
11def rmmod( mod ):
12 """Return output of lsmod.
13 mod: module string"""
14 return quietRun( [ 'rmmod', mod ] )
15
16def modprobe( mod ):
17 """Return output of modprobe
18 mod: module string"""
19 return quietRun( [ 'modprobe', mod ] )
20
21OF_KMOD = 'ofdatapath'
22OVS_KMOD = 'openvswitch_mod' # Renamed 'openvswitch' in OVS 1.7+/Linux 3.5+
23TUN = 'tun'
24
25def 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
60def 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 )