diff --git a/examples/bind.py b/examples/bind.py index af02254..c2e7902 100755 --- a/examples/bind.py +++ b/examples/bind.py @@ -20,7 +20,9 @@ from functools import partial def testHostWithPrivateDirs(): "Test bind mounts" topo = SingleSwitchTopo( 10 ) - privateDirs = [ '/var/log', '/var/run' ] + privateDirs = [ ( '/var/log', '/onos/%(name)s/var/log' ), + ( '/var/run', '/ovx/%(name)s/var/run' ), + '/mn' ] host = partial( HostWithPrivateDirs, privateDirs=privateDirs ) net = Mininet( topo=topo, host=host ) diff --git a/mininet/node.py b/mininet/node.py index a9852b2..9d5516c 100644 --- a/mininet/node.py +++ b/mininet/node.py @@ -728,14 +728,28 @@ class CPULimitedHost( Host ): class HostWithPrivateDirs( Host ): "Host with private directories" - def __init__(self, *args, **kwargs ): + def __init__(self, name, *args, **kwargs ): """privateDirs: list of private directories""" - + self.name = name self.privateDirs = kwargs.pop( 'privateDirs', [] ) - Host.__init__( self, *args, **kwargs ) + Host.__init__( self, name, *args, **kwargs ) self.mountPrivateDirs() def mountPrivateDirs( self ): + "mount the directories that have specified mountpoints" + for directory in self.privateDirs: + if isinstance( directory, tuple ): + 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: + self.cmd( 'mkdir -p %s' %directory ) + self.cmd( 'mount -n -t tmpfs tmpfs %s' %directory ) + + + def mountTempDirs( self ): "Mount tmpfs for each private directory" for dir_ in self.privateDirs: self.cmd( 'mkdir -p ' + dir_ ) diff --git a/mnexec.c b/mnexec.c index fee3d25..8f6830e 100644 --- a/mnexec.c +++ b/mnexec.c @@ -133,6 +133,7 @@ int main(int argc, char *argv[]) perror("mount"); return 1; } + break; case 'p': /* print pid */ @@ -140,7 +141,7 @@ int main(int argc, char *argv[]) fflush(stdout); break; case 'a': - /* Attach to pid's network namespace */ + /* Attach to pid's network namespace and mount namespace*/ pid = atoi(optarg); sprintf(path, "/proc/%d/ns/net", pid ); nsid = open(path, O_RDONLY); @@ -152,6 +153,18 @@ int main(int argc, char *argv[]) perror("setns"); return 1; } + sprintf(path, "/proc/%d/ns/mnt", pid ); + nsid = open(path, O_RDONLY); + if (nsid < 0) { + perror(path); + return 1; + } + if (setns(nsid, 0) != 0) { + perror("setns"); + return 1; + } + + break; case 'g': /* Attach to cgroup */