+import os
+import setns
+import pwd
+
+from argparse import ArgumentParser
+
+# can set to True here, but also use the -d option
+debug = False
+
+def getarch(pid):
+ exe_filename='/proc/%s/exe'%pid
+ output = os.popen('readelf -h %s 2>&1'%exe_filename).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 for pid %s'%pid)
+
+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
+
+ # support for either setting debug at the top of this file, or on the command-line
+ if args.debug:
+ global debug
+ debug=True