Statistics
| Branch: | Tag: | Revision:

mininet / mininet / moduledeps.py @ b47cdfea

History | View | Annotate | Download (2.18 KB)

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'
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 + '\n%s' % 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 + ';\n See INSTALL.\n%s' %
51
                       modprobeOutput )
52
                exit( 1 )
53
            if mod not in lsmod():
54
                error( 'Failed to insert ' + mod + '\n' )
55
                exit( 1 )
56
        else:
57
            debug( '*** ' + mod + ' already loaded\n' )
58

    
59
def pathCheck( *args ):
60
    "Make sure each program in *args can be found in $PATH."
61
    for arg in args:
62
        if not quietRun( 'which ' + arg ):
63
            error( 'Cannot find required executable %s -'
64
                ' is it installed somewhere in your $PATH?\n(%s)\n' %
65
                    ( arg, environ[ 'PATH' ] ) )
66
            exit( 1 )