small refactor: put function to ensure root in util
Two benefits: - One place to change if in the future, a more granular method of root access is used (like the BigSwitch patch). - Makes this reusable by stuff like examples/baresshd.py that use the low-level Mininet API.
This commit is contained in:
+2
-7
@@ -96,7 +96,7 @@ from mininet.cli import CLI
|
||||
from mininet.log import info, error, debug, output
|
||||
from mininet.node import Host, OVSKernelSwitch, Controller
|
||||
from mininet.link import Link, Intf
|
||||
from mininet.util import quietRun, fixLimits, numCores
|
||||
from mininet.util import quietRun, fixLimits, numCores, ensureRoot
|
||||
from mininet.util import macColonHex, ipStr, ipParse, netParse, ipAdd
|
||||
from mininet.term import cleanUpScreens, makeTerms
|
||||
|
||||
@@ -571,12 +571,7 @@ class Mininet( object ):
|
||||
"Initialize Mininet"
|
||||
if cls.inited:
|
||||
return
|
||||
if os.getuid() != 0:
|
||||
# Note: this script must be run as root
|
||||
# Probably we should only sudo when we need
|
||||
# to as per Big Switch's patch
|
||||
print "*** Mininet must run as root."
|
||||
exit( 1 )
|
||||
ensureRoot()
|
||||
fixLimits()
|
||||
cls.inited = True
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ from subprocess import call, check_call, Popen, PIPE, STDOUT
|
||||
import re
|
||||
from fcntl import fcntl, F_GETFL, F_SETFL
|
||||
from os import O_NONBLOCK
|
||||
import os
|
||||
|
||||
# Command execution support
|
||||
|
||||
@@ -456,3 +457,13 @@ def buildTopo( topos, topoStr ):
|
||||
if topo not in topos:
|
||||
raise Exception( 'Invalid topo name %s' % topo )
|
||||
return topos[ topo ]( *args, **kwargs )
|
||||
|
||||
def ensureRoot():
|
||||
"""Ensure that we are running as root.
|
||||
|
||||
Probably we should only sudo when needed as per Big Switch's patch.
|
||||
"""
|
||||
if os.getuid() != 0:
|
||||
print "*** Mininet must run as root."
|
||||
exit( 1 )
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user