+import os
+import setns
+import pdb
+
+from argparse import ArgumentParser
+
+drop_capabilities='cap_sys_admin,cap_sys_boot,cap_sys_module'
+
+# can set to True here, but also use the -d option
+debug = False
+
+def getarch(f):
+ output = os.popen('readelf -h %s 2>&1'%f).readlines()
+ classlines = [x for x in output if ('Class' in x.split(':')[0])]
+ line = classlines[0]
+ c = line.split(':')[1]
+ if ('ELF64' in c):
+ return 'x86_64'
+ elif ('ELF32' in c):
+ return 'i686'
+ else:
+ raise Exception('Could not determine architecture')
+
+def get_cgroup_subdirs_for_pid(pid):
+ cgroup_info_file = '/proc/%s/cgroup'%pid
+ cgroup_lines = open(cgroup_info_file).read().splitlines()
+
+ subdirs = {}
+ for line in cgroup_lines:
+ try:
+ _, cgroup_name, subdir = line.split(':')
+ subdirs[cgroup_name] = subdir
+ except Exception, e:
+ print "Error reading cgroup info: %s"%str(e)
+ pass
+
+ return subdirs
+
+
+def umount(fs_dir, opts=''):
+ output = os.popen('/bin/umount %s %s 2>&1'%(opts, fs_dir)).read()
+ return ('device is busy' not in output)
+
+def main ():
+ parser = ArgumentParser()
+ parser.add_argument("-n", "--nonet",
+ action="store_true", dest="no_netns", default=False,
+ help="Don't enter network namespace")
+ parser.add_argument("-m", "--nomnt",
+ action="store_true", dest="no_mntns", default=False,
+ help="Don't enter mount namespace")
+ parser.add_argument("-p", "--nopid",
+ action="store_true", dest="no_pidns", default=False,
+ help="Don't enter pid namespace")
+ parser.add_argument("-r", "--root",
+ action="store_true", dest="root", default=False,
+ help="Enter as root: be careful")
+ parser.add_argument("-i","--internal",
+ action="store_true", dest="internal", default=False,
+ help="does *not* prepend '-- -c' to arguments - or invoke lxcsu-internal")
+ parser.add_argument("-d","--debug",
+ action='store_true', dest='debug', default=False,
+ help="debug option")
+ parser.add_argument("-s","--nosliceuid",
+ action='store_true', dest="nosliceuid", default=False,
+ help="do not change to slice uid inside of slice")
+ parser.add_argument("-o","--noslicehome",
+ action='store_true', dest="noslicehome", default=False,
+ help="do not change to slice home directory inside of slice")
+
+ if os.path.exists("/etc/lxcsu_default"):
+ defaults = parser.parse_args(file("/etc/lxcsu_default","r").read().split())
+ parser.set_defaults(**defaults.__dict__)
+
+ parser.add_argument ("slice_name")
+ parser.add_argument ("command_to_run",nargs="*")
+
+ args = parser.parse_args()
+ slice_name=args.slice_name
+
+ # unless we run the symlink 'lxcsu-internal', or we specify the -i option, prepend '--' '-c'
+ if sys.argv[0].find('internal')>=0: args.internal=True
+
+ if len(args.command_to_run)>0 and (args.command_to_run[0] == "/sbin/service"):
+ # A quick hack to support nodemanager interfaces.py when restarting
+ # networking in a slice.
+ args.nosliceuid = True
+
+ # plain lxcsu
+ if not args.internal:
+ # no command given: enter interactive shell
+ if not args.command_to_run: args.command_to_run=['/bin/sh']
+ args.command_to_run = [ '-c' ] + [" ".join(args.command_to_run)]