Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
mininet
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Releases
Package Registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Olaf Bergmann
mininet
Commits
f3442903
Commit
f3442903
authored
11 years ago
by
Bob Lantz
Committed by
Brian O'Connor
11 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Change API for more efficient remount and unmount.
parent
5ae8c936
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
examples/bind.py
+51
-21
51 additions, 21 deletions
examples/bind.py
with
51 additions
and
21 deletions
examples/bind.py
+
51
−
21
View file @
f3442903
...
...
@@ -7,17 +7,20 @@
"""
from
mininet.net
import
Mininet
from
mininet.node
import
Host
,
Switch
,
Controller
from
mininet.node
import
Host
from
mininet.cli
import
CLI
from
mininet.util
import
errFail
,
quietRun
,
errRun
from
mininet.topo
import
SingleSwitchTopo
from
mininet.log
import
setLogLevel
,
info
,
debug
from
os.path
import
join
,
realpath
from
os.path
import
realpath
from
functools
import
partial
# Utility functions for unmounting a tree
MNRUNDIR
=
realpath
(
'
/var/run/mn
'
)
def
mountPoints
():
"
Return list of mounted file systems
"
mtab
,
_err
,
_ret
=
errFail
(
'
cat /proc/mounts
'
)
...
...
@@ -31,7 +34,7 @@ def mountPoints():
mounts
.
append
(
mount
)
return
mounts
def
unmountAll
(
dir
=
'
/var/run/mn
'
):
def
unmountAll
(
dir
=
MNRUNDIR
):
"
Unmount all mounts under a directory tree
"
dir
=
realpath
(
dir
)
# Find all mounts below dir
...
...
@@ -53,11 +56,17 @@ def unmountAll( dir='/var/run/mn' ):
class
HostWithPrivateDirs
(
Host
):
"
Host with private directories
"
mnRunDir
=
realpath
(
'
/var/run/mn
'
)
mnRunDir
=
MNRUNDIR
def
__init__
(
self
,
name
,
*
args
,
**
kwargs
):
"
privateDirs: list of private directories
"
"""
privateDirs: list of private directories
remounts: dirs to remount
unmount: unmount dirs in cleanup? (True)
Note: if unmount is False, you must call unmountAll()
manually.
"""
self
.
privateDirs
=
kwargs
.
pop
(
'
privateDirs
'
,
[]
)
self
.
remounts
=
kwargs
.
pop
(
'
remounts
'
,
[]
)
self
.
unmount
=
kwargs
.
pop
(
'
unmount
'
,
True
)
Host
.
__init__
(
self
,
name
,
*
args
,
**
kwargs
)
self
.
rundir
=
'
%s/%s
'
%
(
self
.
mnRunDir
,
name
)
if
self
.
privateDirs
:
...
...
@@ -86,19 +95,28 @@ def mountPrivateDirs( self ):
errFail
(
'
mount -B %s %s
'
%
(
privateDir
,
mountPoint
)
)
def
remountDirs
(
self
,
fstypes
=
[
'
nfs
'
]
):
"
Remount mounted file systems
"
dirs
=
self
.
cmd
(
'
cat /proc/mounts
'
).
strip
().
split
(
'
\n
'
)
def
mountDirs
(
self
,
dirs
):
"
Mount a list of directories
"
for
dir
in
dirs
:
mountpoint
=
self
.
root
+
dir
errFail
(
'
mount -B %s %s
'
%
(
dir
,
mountpoint
)
)
@classmethod
def
findRemounts
(
cls
,
fstypes
=
[
'
nfs
'
]
):
"""
Identify mount points in /proc/mounts to remount
fstypes: file system types to match
"""
dirs
=
quietRun
(
'
cat /proc/mounts
'
).
strip
().
split
(
'
\n
'
)
remounts
=
[]
for
dir
in
dirs
:
line
=
dir
.
split
()
mountpoint
,
fstype
=
line
[
1
],
line
[
2
]
# Don't re-remount directories!!!
if
mountpoint
.
find
(
self
.
mnRunDir
)
==
0
:
if
mountpoint
.
find
(
cls
.
mnRunDir
)
==
0
:
continue
if
fstype
in
fstypes
:
print
"
remounting:
"
,
mountpoint
errFail
(
'
mount -B %s %s
'
%
(
mountpoint
,
self
.
root
+
mountpoint
)
)
remounts
.
append
(
mountpoint
)
return
remounts
def
createBindMounts
(
self
):
"""
Create a chroot directory structure,
...
...
@@ -113,7 +131,7 @@ def createBindMounts( self ):
# Recursively mount / in private doort
# note we'll remount /sys and /proc later
errFail
(
'
mount -B /
'
+
self
.
root
)
self
.
re
mountDirs
()
self
.
mountDirs
(
self
.
remounts
)
self
.
mountPrivateDirs
()
def
unmountBindMounts
(
self
):
...
...
@@ -131,33 +149,45 @@ def popen( self, *args, **kwargs ):
return
Host
.
popen
(
self
,
*
args
,
**
kwargs
)
def
cleanup
(
self
):
"
Clean up, then unmount bind mounts
"
"""
Clean up, then unmount bind mounts
unmount: actually unmount bind mounts?
"""
# Wait for process to actually terminate
self
.
shell
.
wait
()
Host
.
cleanup
(
self
)
self
.
unmountBindMounts
()
errFail
(
'
rmdir
'
+
self
.
root
)
if
self
.
unmount
:
self
.
unmountBindMounts
()
errFail
(
'
rmdir
'
+
self
.
root
)
# Convenience aliases
findRemounts
=
HostWithPrivateDirs
.
findRemounts
# Sample usage
def
testHostWithPrivateDirs
():
"
Test bind mounts
"
topo
=
SingleSwitchTopo
(
2
)
topo
=
SingleSwitchTopo
(
10
)
remounts
=
findRemounts
(
fstypes
=
[
'
nfs
'
]
)
privateDirs
=
[
'
/var/log
'
,
'
/var/run
'
]
host
=
partial
(
HostWithPrivateDirs
,
privateDirs
=
privateDirs
)
host
=
partial
(
HostWithPrivateDirs
,
remounts
=
remounts
,
privateDirs
=
privateDirs
,
unmount
=
False
)
net
=
Mininet
(
topo
=
topo
,
host
=
host
)
net
.
start
()
print
'
Private Directories:
'
,
privateDirs
info
(
'
Private Directories:
'
,
privateDirs
,
'
\n
'
)
CLI
(
net
)
net
.
stop
()
# We do this all at once to save a bit of time
info
(
'
Unmounting host bind mounts...
\n
'
)
unmountAll
()
if
__name__
==
'
__main__
'
:
unmountAll
()
setLogLevel
(
'
info
'
)
testHostWithPrivateDirs
()
unmountAll
()
info
(
'
Done.
\n
'
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment