-def umount(fs_dir):
- output = os.popen('/bin/umount %s 2>&1'%fs_dir).read()
- return ('device is busy' not in fs_dir)
-
-
-"""
-parser = OptionParser()
-parser.add_option("-n", "--net",
- action="store_true", dest="netns", default=False,
- help="Enter network namespace")
-parser.add_option("-m", "--mnt",
- action="store_true", dest="mntns", default=False,
- help="Enter mount namespace")
-parser.add_option("-p", "--pid",
- action="store_true", dest="pidns", default=False,
- help="Enter pid namespace")
-
-(options, args) = parser.parse_args()
-"""
-args = sys.argv[1:]
-
-try:
- slice_name = args[0]
-except IndexError:
- print "You must specify a vm name"
+# 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)]
+
+ # support for either setting debug at the top of this file, or on the command-line
+ if args.debug:
+ global debug
+ debug=True
+
+ try:
+ cmd = '/usr/bin/virsh --connect lxc:/// domid %s'%slice_name
+ # convert to int as a minimal raincheck
+ driver_pid = int(os.popen(cmd).read().strip())
+ # locate the pid for the - expected - single child, that would be the init for that VM
+ init_pid = int(open("/proc/%s/task/%s/children"%(driver_pid,driver_pid)).read().strip())
+ # Thierry: I am changing the code below to use child_pid instead of driver_pid
+ # for the namespace handling features, that I was able to check
+ # I've left the other ones as they were, i.e. using driver_pid, but I suspect
+ # they chould be changed as well
+
+ except:
+ print "Domain %s not found"%slice_name