diff --git a/examples/bind.py b/examples/bind.py index 287c97abfff8a03e726891acf2e420ddb0ea417e..4bc30a873d6598bf595a796071057237841390ec 100755 --- a/examples/bind.py +++ b/examples/bind.py @@ -35,7 +35,7 @@ """ from mininet.net import Mininet -from mininet.node import Host, HostWithPrivateDirs +from mininet.node import Host from mininet.cli import CLI from mininet.topo import SingleSwitchTopo from mininet.log import setLogLevel, info, debug @@ -51,15 +51,12 @@ def testHostWithPrivateDirs(): privateDirs = [ ( '/var/log', '/tmp/%(name)s/var/log' ), ( '/var/run', '/tmp/%(name)s/var/run' ), '/var/mn' ] - host = partial( HostWithPrivateDirs, + host = partial( Host, privateDirs=privateDirs ) net = Mininet( topo=topo, host=host ) net.start() - directories = [] - for directory in privateDirs: - directories.append( directory[ 0 ] - if isinstance( directory, tuple ) - else directory ) + directories = [ directory[ 0 ] if isinstance( directory, tuple ) + else directory for directory in privateDirs ] info( 'Private Directories:', directories, '\n' ) CLI( net ) net.stop() diff --git a/examples/miniedit.py b/examples/miniedit.py index 0fcae604b359962f969b6fc61579dae70181560b..f0d84688abbbb0fe51368da00d8503232e19ebd1 100755 --- a/examples/miniedit.py +++ b/examples/miniedit.py @@ -43,7 +43,7 @@ from mininet.util import custom, customConstructor from mininet.term import makeTerm, cleanUpScreens from mininet.node import Controller, RemoteController, NOX, OVSController -from mininet.node import CPULimitedHost, Host, Node, HostWithPrivateDirs +from mininet.node import CPULimitedHost, Host, Node from mininet.node import OVSKernelSwitch, OVSSwitch, UserSwitch from mininet.link import TCLink, Intf, Link from mininet.cli import CLI @@ -78,32 +78,6 @@ 'cfs': custom( CPULimitedHost, sched='cfs' ) } -class CPULimitedHostWithPrivateDirs( CPULimitedHost ): - "Host with private directories" - - def __init__( self, name, sched='cfs', **kwargs ): - "privateDirs: list of private directory strings or tuples" - self.name = name - self.privateDirs = kwargs.pop( 'privateDirs', [] ) - CPULimitedHost.__init__( self, name, sched, **kwargs ) - self.mountPrivateDirs() - - def mountPrivateDirs( self ): - "mount private directories" - for directory in self.privateDirs: - if isinstance( directory, tuple ): - # mount given private directory - privateDir = directory[ 1 ] % self.__dict__ - mountPoint = directory[ 0 ] - self.cmd( 'mkdir -p %s' % privateDir ) - self.cmd( 'mkdir -p %s' % mountPoint ) - self.cmd( 'mount --bind %s %s' % - ( privateDir, mountPoint ) ) - else: - # mount temporary filesystem on directory - self.cmd( 'mkdir -p %s' % directory ) - self.cmd( 'mount -n -t tmpfs tmpfs %s' % directory ) - class InbandController( RemoteController ): def checkListening( self ): @@ -2784,16 +2758,17 @@ def buildNodes( self, net): # Create the correct host class if 'cores' in opts or 'cpu' in opts: if ('privateDirectory' in opts): - hostCls = partial( CPULimitedHostWithPrivateDirs, + hostCls = partial( CPULimitedHost, privateDirs=opts['privateDirectory'] ) else: hostCls=CPULimitedHost else: if ('privateDirectory' in opts): - hostCls = partial( HostWithPrivateDirs, + hostCls = partial( Host, privateDirs=opts['privateDirectory'] ) else: hostCls=Host + print hostCls newHost = net.addHost( name, cls=hostCls, ip=ip, diff --git a/mininet/node.py b/mininet/node.py index c82d2932bc50a462ed897e91ca1a4661aaffa527..299433a683b7f5bbc27ca44b2b9dfbfeedb6ea73 100644 --- a/mininet/node.py +++ b/mininet/node.py @@ -75,12 +75,14 @@ class Node( object ): def __init__( self, name, inNamespace=True, **params ): """name: name of node inNamespace: in network namespace? + privateDirs: list of private directory strings or tuples params: Node parameters (see config() for details)""" # Make sure class actually works self.checkSetup() self.name = params.get( 'name', name ) + self.privateDirs = params.get( 'privateDirs', [] ) self.inNamespace = params.get( 'inNamespace', inNamespace ) # Stash configuration parameters for future reference @@ -100,6 +102,7 @@ def __init__( self, name, inNamespace=True, **params ): # Start command interpreter shell self.startShell() + self.mountPrivateDirs() # File descriptor to node mapping support # Class variables and methods @@ -161,6 +164,30 @@ def startShell( self, mnopts=None ): self.cmd( 'stty -echo' ) self.cmd( 'set +m' ) + def mountPrivateDirs( self ): + "mount private directories" + for directory in self.privateDirs: + if isinstance( directory, tuple ): + # mount given private directory + privateDir = directory[ 1 ] % self.__dict__ + mountPoint = directory[ 0 ] + self.cmd( 'mkdir -p %s' % privateDir ) + self.cmd( 'mkdir -p %s' % mountPoint ) + self.cmd( 'mount --bind %s %s' % + ( privateDir, mountPoint ) ) + else: + # mount temporary filesystem on directory + self.cmd( 'mkdir -p %s' % directory ) + self.cmd( 'mount -n -t tmpfs tmpfs %s' % directory ) + + def unmountPrivateDirs( self ): + "mount private directories" + for directory in self.privateDirs: + if isinstance( directory, tuple ): + self.cmd( 'umount ', directory[ 0 ] ) + else: + self.cmd( 'umount ', directory ) + def _popen( self, cmd, **params ): """Internal method: spawn and return a process cmd: command to run (list) @@ -210,6 +237,7 @@ def write( self, data ): def terminate( self ): "Send kill signal to Node and clean up after it." + self.unmountPrivateDirs() if self.shell: if self.shell.poll() is None: os.killpg( self.shell.pid, signal.SIGHUP ) @@ -590,12 +618,10 @@ def setup( cls ): "Make sure our class dependencies are available" pathCheck( 'mnexec', 'ifconfig', moduleName='Mininet') - class Host( Node ): "A host is simply a Node" pass - class CPULimitedHost( Host ): "CPU limited host" @@ -762,33 +788,6 @@ def init( cls ): mountCgroups() cls.inited = True -class HostWithPrivateDirs( Host ): - "Host with private directories" - - def __init__( self, name, *args, **kwargs ): - "privateDirs: list of private directory strings or tuples" - self.name = name - self.privateDirs = kwargs.pop( 'privateDirs', [] ) - Host.__init__( self, name, *args, **kwargs ) - self.mountPrivateDirs() - - def mountPrivateDirs( self ): - "mount private directories" - for directory in self.privateDirs: - if isinstance( directory, tuple ): - # mount given private directory - privateDir = directory[ 1 ] % self.__dict__ - mountPoint = directory[ 0 ] - self.cmd( 'mkdir -p %s' % privateDir ) - self.cmd( 'mkdir -p %s' % mountPoint ) - self.cmd( 'mount --bind %s %s' % - ( privateDir, mountPoint ) ) - else: - # mount temporary filesystem on directory - self.cmd( 'mkdir -p %s' % directory ) - self.cmd( 'mount -n -t tmpfs tmpfs %s' % directory ) - - # Some important things to note: #