From db36150ae28898815a9f86ab7d716f8728504d15 Mon Sep 17 00:00:00 2001 From: smbaker Date: Mon, 5 Aug 2013 12:11:06 -0700 Subject: [PATCH 01/16] fix perms --- lxcsu | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 lxcsu diff --git a/lxcsu b/lxcsu old mode 100644 new mode 100755 -- 2.43.0 From 4626c7937b54f1bb7c8717f275ebf85dd0feb8c6 Mon Sep 17 00:00:00 2001 From: Scott Baker Date: Mon, 12 Aug 2013 12:45:55 -0700 Subject: [PATCH 02/16] fix lxcsu not handling nodemanager interfaces.py ('/sbin/service network restart') properly --- lxcsu | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) mode change 100755 => 100644 lxcsu diff --git a/lxcsu b/lxcsu old mode 100755 new mode 100644 index 3f22cc3..9880360 --- a/lxcsu +++ b/lxcsu @@ -47,6 +47,8 @@ def main (): parser.add_argument("-d","--debug", action='store_true', dest='debug', default=False, help="debug option") + parser.add_argument("-s","--nosliceuid", dest="nosliceuid", default=False, + help="do not change to slice uid inside of slice") parser.add_argument ("slice_name") parser.add_argument ("command_to_run",nargs="*") @@ -56,11 +58,16 @@ def main (): # 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 - # plain lxcsu + 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' ] + args.command_to_run + 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: @@ -220,13 +227,17 @@ def main (): cap_arg = '--drop='+drop_capabilities if (not args.root): - uid = getuid (slice_name) - if not uid: - print "lxcsu could not spot %s in /etc/passwd - exiting"%slice_name - exit(1) - exec_args = [arch,'/usr/sbin/capsh',cap_arg,'--uid=%s'%uid,'--','--login',]+args.command_to_run + if (args.nosliceuid): + # we still want to drop capabilities, but don't want to switch UIDs + exec_args = [arch,'/usr/sbin/capsh',cap_arg,'--','--login',]+args.command_to_run + else: + uid = getuid (slice_name) + if not uid: + print "lxcsu could not spot %s in /etc/passwd - exiting"%slice_name + exit(1) + exec_args = [arch,'/usr/sbin/capsh',cap_arg,'--uid=%s'%uid,'--','--login',]+args.command_to_run # once we can drop f12, it would be nicer to instead go for -# exec_args = [arch,'/usr/sbin/capsh',cap_arg,'--user=%s'%slice_name,'--','--login',]+args.command_to_run +# exec_args = [arch,'/usr/sbin/capsh',cap_arg,'--user=%s'%slice_name,'--','--login',]+args.command_to_run else: exec_args = [arch,'/usr/sbin/capsh','--','--login']+args.command_to_run -- 2.43.0 From 5985e6f9de65189b238b60121b1c6f8d10d69175 Mon Sep 17 00:00:00 2001 From: Scott Baker Date: Mon, 12 Aug 2013 12:46:20 -0700 Subject: [PATCH 03/16] fix perms --- lxcsu | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 lxcsu diff --git a/lxcsu b/lxcsu old mode 100644 new mode 100755 -- 2.43.0 From 4dd3660b7612a0d6a87320645c2c93490b84b837 Mon Sep 17 00:00:00 2001 From: Sapan Bhatia Date: Mon, 12 Aug 2013 16:34:06 -0400 Subject: [PATCH 04/16] asm/unistd.h is a more standard include than asm-generic/unistd.h --- setns.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setns.c b/setns.c index 971665a..e82bd26 100644 --- a/setns.c +++ b/setns.c @@ -1,7 +1,7 @@ #include #include #include -#include +#include #include static PyObject * -- 2.43.0 From 7fdcd2fcbf71d97e15218a79fdc40e9a6f235276 Mon Sep 17 00:00:00 2001 From: Sapan Bhatia Date: Mon, 12 Aug 2013 16:39:46 -0400 Subject: [PATCH 05/16] Support conventional invocation of make --- Makefile | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 453ddc4..51f7cdb 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,12 @@ -all: - python setup.py build +all: vsh setns.so + +vsh: vsh.c gcc vsh.c -o vsh +setns: setns.c + python setup.py build + + ########## sync # for use with the test framework; push local stuff on a test node # howto use: go on testmaster in the build you want to use and just run @@ -59,3 +64,13 @@ else @echo "FETCHING key" +scp $(KEYURL) $@ endif + +### poor man's install + +install: setns vsh + mkdir -p /usr/sbin + install -D -m 755 vsh /usr/sbin/vsh + install -D -m 755 lxcsu /usr/sbin/lxcsu + install -D -m 755 lxcsu-internal /usr/sbin/lxcsu-internal + chmod u+s /usr/sbin/lxcsu + cp build/lib*/setns.so /usr/sbin -- 2.43.0 From a605a1e7f503d977adb47d7549efdca2b524fcb2 Mon Sep 17 00:00:00 2001 From: Scott Baker Date: Thu, 15 Aug 2013 17:06:26 -0700 Subject: [PATCH 06/16] remove unnecessary proc remounts, prevent setting LD_PRELOAD if the library doesn't exist in image --- lxcsu | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) mode change 100755 => 100644 lxcsu diff --git a/lxcsu b/lxcsu old mode 100755 new mode 100644 index 9880360..26c1e9d --- a/lxcsu +++ b/lxcsu @@ -180,9 +180,7 @@ def main (): for (sysctl_file, sysctl_name, sysctl_val) in sysctls: for fn in ["/sbin/sysctl", "/usr/sbin/sysctl", "/bin/sysctl", "/usr/bin/sysctl"]: if os.path.exists(fn): - os.system("mount -o remount,rw none /proc/sys") os.system('%s -w %s=%s'%(fn, sysctl_name,sysctl_val)) - os.system("mount -o remount,ro none /proc/sys") break else: print "Error: image does not have a sysctl binary" @@ -243,7 +241,8 @@ def main (): os.environ['SHELL'] = '/bin/sh' os.environ['HOME'] = '/home/%s'%slice_name - os.environ['LD_PRELOAD'] = '/etc/planetlab/lib/bind_public.so' + if os.path.exists('/etc/planetlab/lib/bind_public.so'): + os.environ['LD_PRELOAD'] = '/etc/planetlab/lib/bind_public.so' os.chdir("/home/%s"%(slice_name)) if debug: print 'lxcsu:execv:','/usr/bin/setarch',exec_args os.execv('/usr/bin/setarch',exec_args) -- 2.43.0 From 6d2a0b07a3619ab801337024897c5b2f23f584a1 Mon Sep 17 00:00:00 2001 From: Scott Baker Date: Thu, 15 Aug 2013 17:06:40 -0700 Subject: [PATCH 07/16] fix permissions --- lxcsu | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 lxcsu diff --git a/lxcsu b/lxcsu old mode 100644 new mode 100755 -- 2.43.0 From e3e49243aba5abb3998e9f77937e33230d7f7fd9 Mon Sep 17 00:00:00 2001 From: Thierry Parmentelat Date: Mon, 26 Aug 2013 13:40:22 +0200 Subject: [PATCH 08/16] Thomas's fix on Makefile + remove lines with tabs --- Makefile | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 51f7cdb..f6298c4 100644 --- a/Makefile +++ b/Makefile @@ -1,11 +1,10 @@ -all: vsh setns.so - +all: vsh setns + vsh: vsh.c gcc vsh.c -o vsh setns: setns.c python setup.py build - ########## sync # for use with the test framework; push local stuff on a test node -- 2.43.0 From bc916290e9e64b040b6c063b733c5fc63efca7c6 Mon Sep 17 00:00:00 2001 From: Scott Baker Date: Fri, 30 Aug 2013 11:08:29 -0700 Subject: [PATCH 09/16] add --noslicehome option, and check /etc/lxcsu_default for default arguments --- lxcsu | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/lxcsu b/lxcsu index 26c1e9d..c1ad9d1 100755 --- a/lxcsu +++ b/lxcsu @@ -47,8 +47,17 @@ def main (): parser.add_argument("-d","--debug", action='store_true', dest='debug', default=False, help="debug option") - parser.add_argument("-s","--nosliceuid", dest="nosliceuid", default=False, + 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="*") @@ -240,10 +249,11 @@ def main (): exec_args = [arch,'/usr/sbin/capsh','--','--login']+args.command_to_run os.environ['SHELL'] = '/bin/sh' - os.environ['HOME'] = '/home/%s'%slice_name if os.path.exists('/etc/planetlab/lib/bind_public.so'): os.environ['LD_PRELOAD'] = '/etc/planetlab/lib/bind_public.so' - os.chdir("/home/%s"%(slice_name)) + if not args.noslicehome: + os.environ['HOME'] = '/home/%s'%slice_name + os.chdir("/home/%s"%(slice_name)) if debug: print 'lxcsu:execv:','/usr/bin/setarch',exec_args os.execv('/usr/bin/setarch',exec_args) else: -- 2.43.0 From fef0f0ca2c9c0f118f8c2419649265c5a88fa50f Mon Sep 17 00:00:00 2001 From: Thierry Parmentelat Date: Sat, 31 Aug 2013 11:29:26 +0200 Subject: [PATCH 10/16] Setting tag lxc-userspace-1.0-7 perform vsys sysctl inside of container remove unnecessary proc remounts prevent setting LD_PRELOAD if the library doesn't exist in image add --noslicehome option check /etc/lxcsu_default for default arguments fix permissions support conventional invocation of make --- lxc-userspace.spec | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lxc-userspace.spec b/lxc-userspace.spec index f29dafa..26c3d1c 100644 --- a/lxc-userspace.spec +++ b/lxc-userspace.spec @@ -1,6 +1,6 @@ %define name lxc-userspace %define version 1.0 -%define taglevel 6 +%define taglevel 7 %define percent % %define braop \{ @@ -55,6 +55,15 @@ chmod u+s /usr/sbin/vsh %postun %changelog +* Sat Aug 31 2013 Thierry Parmentelat - lxc-userspace-1.0-7 +- perform vsys sysctl inside of container +- remove unnecessary proc remounts +- prevent setting LD_PRELOAD if the library doesn't exist in image +- add --noslicehome option +- check /etc/lxcsu_default for default arguments +- fix permissions +- support conventional invocation of make + * Sun Jul 14 2013 Thierry Parmentelat - lxc-userspace-1.0-6 - merge back lxcsu and -internal into a single source file - capsh --uid instead of --user that was not yet supported in f12 (hopefully tmp) -- 2.43.0 From 085591ee34b8ae79b8950510970d5d273fc9467b Mon Sep 17 00:00:00 2001 From: Scott Baker Date: Thu, 5 Sep 2013 16:04:06 -0700 Subject: [PATCH 11/16] set u+s on /usr/sbin/vsh --- Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/Makefile b/Makefile index f6298c4..15dacd5 100644 --- a/Makefile +++ b/Makefile @@ -72,4 +72,5 @@ install: setns vsh install -D -m 755 lxcsu /usr/sbin/lxcsu install -D -m 755 lxcsu-internal /usr/sbin/lxcsu-internal chmod u+s /usr/sbin/lxcsu + chmod u+s /usr/sbin/vsh cp build/lib*/setns.so /usr/sbin -- 2.43.0 From 6b08f6e703308f8327c802171ff4ecfdd17f5341 Mon Sep 17 00:00:00 2001 From: Thierry Parmentelat Date: Fri, 20 Sep 2013 11:43:58 +0200 Subject: [PATCH 12/16] Setting tag lxc-userspace-1.0-8 fix vsh permissions --- lxc-userspace.spec | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lxc-userspace.spec b/lxc-userspace.spec index 26c3d1c..c38e385 100644 --- a/lxc-userspace.spec +++ b/lxc-userspace.spec @@ -1,6 +1,6 @@ %define name lxc-userspace %define version 1.0 -%define taglevel 7 +%define taglevel 8 %define percent % %define braop \{ @@ -55,6 +55,9 @@ chmod u+s /usr/sbin/vsh %postun %changelog +* Fri Sep 20 2013 Thierry Parmentelat - lxc-userspace-1.0-8 +- fix vsh permissions + * Sat Aug 31 2013 Thierry Parmentelat - lxc-userspace-1.0-7 - perform vsys sysctl inside of container - remove unnecessary proc remounts -- 2.43.0 From b6f35c7cb1502c0af36f6c3055149574a4964925 Mon Sep 17 00:00:00 2001 From: Sapan Bhatia Date: Tue, 8 Oct 2013 22:43:10 -0400 Subject: [PATCH 13/16] Fix for the "error disabling cgroup" error --- lxcsu | 494 +++++++++++++++++++++++++++++----------------------------- 1 file changed, 248 insertions(+), 246 deletions(-) diff --git a/lxcsu b/lxcsu index c1ad9d1..4f4a2f7 100755 --- a/lxcsu +++ b/lxcsu @@ -12,254 +12,256 @@ drop_capabilities='cap_sys_admin,cap_sys_boot,cap_sys_module' 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 umount(fs_dir): - output = os.popen('/bin/umount %s 2>&1'%fs_dir).read() - return ('device is busy' not in output) + 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 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="netns", default=False, - help="Don't enter network namespace") - parser.add_argument("-m", "--nomnt", - action="store_true", dest="mntns", default=False, - help="Don't enter mount namespace") - parser.add_argument("-p", "--nopid", - action="store_true", dest="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 = 'grep %s /proc/*/cgroup | grep freezer'%slice_name - output = os.popen(cmd).readlines() - except: - print "Error finding slice %s"%slice_name - exit(1) - - slice_spec = None - - # provide a default as this is not always properly computed - arch = None - - for e in output: - try: - l = e.rstrip() - path = l.split(':')[0] - comp = l.rsplit(':')[-1] - slice_name_check = comp.rsplit('/')[-1] - if debug: print "dealing with >%s<"%slice_name_check - - if (slice_name_check == slice_name): - if debug: print "found %s"%slice_name - slice_path = path - pid = slice_path.split('/')[2] - cmdline = open('/proc/%s/cmdline'%pid).read().rstrip('\n\x00') - if (cmdline == '/sbin/init') or (cmdline.startswith("init [")): - slice_spec = slice_path - arch = getarch('/proc/%s/exe'%pid) - break - except Exception,e: - if debug: - import traceback - print "BEG lxcsu - ignoring exception" - traceback.print_exc() - print "END lxcsu - ignoring exception" - pass - - if (not slice_spec or not pid): - print "Not started: %s"%slice_name - exit(1) - - if arch is None: - arch = 'x86_64' - - # Set sysctls specific to slice - sysctls = [] - sysctl_dir = '/etc/planetlab/vsys-attributes/%s'%slice_name - if (os.access(sysctl_dir,0)): - entries = os.listdir(sysctl_dir) - for e in entries: - prefix = 'vsys_sysctl.' - if (e.startswith(prefix)): - sysctl_file = '/'.join([sysctl_dir,e]) - sysctl_name = e[len(prefix):] - sysctl_val = open(sysctl_file).read() - sysctls.append((sysctl_file, sysctl_name, sysctl_val)) - - # Enter cgroups - try: - for subsystem in ['cpuset','memory','blkio']: - open('/sys/fs/cgroup/%s/libvirt/lxc/%s/tasks'%(subsystem,slice_name),'w').write(str(os.getpid())) - - except: - print "Error assigning resources: %s"%slice_name - exit(1) - - try: - open('/sys/fs/cgroup/cpuacct/system/libvirtd.service/libvirt/lxc/%s/tasks'%slice_name,'w').write(str(os.getpid())) - except: - print "Error assigning cpuacct: %s" % slice_name - exit(1) - - # If the slice is frozen, then we'll get an EBUSY when trying to write to the task - # list for the freezer cgroup. Since the user couldn't do anything anyway, it's best - # in this case to error out the shell. (an alternative would be to un-freeze it, - # add the task, and re-freeze it) - try: - f=open('/sys/fs/cgroup/freezer/libvirt/lxc/%s/tasks'%(slice_name),'w') - f.write(str(os.getpid())) - # note: we need to call f.close() explicitly, or we'll get an exception in - # the object destructor, which will not be caught - f.close() - except: - print "Error adding task to freezer cgroup. Slice is probably frozen: %s" % slice_name - exit(1) - - setns.chcontext('/proc/%s/ns/uts'%pid) - setns.chcontext('/proc/%s/ns/ipc'%pid) - - if (not args.pidns): - setns.chcontext('/proc/%s/ns/pid'%pid) - - if (not args.netns): - setns.chcontext('/proc/%s/ns/net'%pid) - - if (not args.mntns): - setns.chcontext('/proc/%s/ns/mnt'%pid) - - proc_mounted = False - if (not os.access('/proc/self',0)): - proc_mounted = True - setns.proc_mount() - - for (sysctl_file, sysctl_name, sysctl_val) in sysctls: - for fn in ["/sbin/sysctl", "/usr/sbin/sysctl", "/bin/sysctl", "/usr/bin/sysctl"]: - if os.path.exists(fn): - os.system('%s -w %s=%s'%(fn, sysctl_name,sysctl_val)) - break - else: - print "Error: image does not have a sysctl binary" - - # cgroups is not yet LXC-safe, so we need to use the coarse grained access control - # strategy of unmounting the filesystem - - umount_result = True - for subsystem in ['cpuset','cpu,cpuacct','memory','devices','freezer','net_cls','blkio','perf_event']: - fs_path = '/sys/fs/cgroup/%s'%subsystem - if (not umount(fs_path)): - print "Error disabling cgroup access" - exit(1) - - if (not umount('/sys/fs/cgroup')): - print "Error disabling cgroup access" - exit(1) - - pid = os.fork() - - # capsh has a --user option starting with f14 - # so if only for f12 we need to fake this one - # - # capsh.c does essentially the following when invoked with --user: - # pwd = getpwnam(user); ... - # ngroups = MAX_GROUPS; - # status = getgrouplist(user, pwd->pw_gid, groups, &ngroups); ... - # status = setgroups(ngroups, groups); ... - # status = setgid(pwd->pw_gid); ... - # status = setuid(pwd->pw_uid); ... - # however we cannot simulate that ourselves because if we did in this process then - # capsh could not be allowed to mess with caps any more - - def getuid (slicename): - import pwd - try: - return pwd.getpwnam(slicename).pw_uid - except: - return - - if (pid == 0): - cap_arg = '--drop='+drop_capabilities - - if (not args.root): - if (args.nosliceuid): - # we still want to drop capabilities, but don't want to switch UIDs - exec_args = [arch,'/usr/sbin/capsh',cap_arg,'--','--login',]+args.command_to_run - else: - uid = getuid (slice_name) - if not uid: - print "lxcsu could not spot %s in /etc/passwd - exiting"%slice_name - exit(1) - exec_args = [arch,'/usr/sbin/capsh',cap_arg,'--uid=%s'%uid,'--','--login',]+args.command_to_run + parser = ArgumentParser() + parser.add_argument("-n", "--nonet", + action="store_true", dest="netns", default=False, + help="Don't enter network namespace") + parser.add_argument("-m", "--nomnt", + action="store_true", dest="mntns", default=False, + help="Don't enter mount namespace") + parser.add_argument("-p", "--nopid", + action="store_true", dest="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 = 'grep %s /proc/*/cgroup | grep freezer'%slice_name + output = os.popen(cmd).readlines() + except: + print "Error finding slice %s"%slice_name + exit(1) + + slice_spec = None + + # provide a default as this is not always properly computed + arch = None + + for e in output: + try: + l = e.rstrip() + path = l.split(':')[0] + comp = l.rsplit(':')[-1] + slice_name_check = comp.rsplit('/')[-1] + if debug: print "dealing with >%s<"%slice_name_check + + if (slice_name_check == slice_name): + if debug: print "found %s"%slice_name + slice_path = path + pid = slice_path.split('/')[2] + cmdline = open('/proc/%s/cmdline'%pid).read().rstrip('\n\x00') + if (cmdline == '/sbin/init') or (cmdline.startswith("init [")): + slice_spec = slice_path + arch = getarch('/proc/%s/exe'%pid) + break + except Exception,e: + if debug: + import traceback + print "BEG lxcsu - ignoring exception" + traceback.print_exc() + print "END lxcsu - ignoring exception" + pass + + if (not slice_spec or not pid): + print "Not started: %s"%slice_name + exit(1) + + if arch is None: + arch = 'x86_64' + + # Set sysctls specific to slice + sysctls = [] + sysctl_dir = '/etc/planetlab/vsys-attributes/%s'%slice_name + if (os.access(sysctl_dir,0)): + entries = os.listdir(sysctl_dir) + for e in entries: + prefix = 'vsys_sysctl.' + if (e.startswith(prefix)): + sysctl_file = '/'.join([sysctl_dir,e]) + sysctl_name = e[len(prefix):] + sysctl_val = open(sysctl_file).read() + sysctls.append((sysctl_file, sysctl_name, sysctl_val)) + + # Enter cgroups + try: + for subsystem in ['cpuset','memory','blkio']: + open('/sys/fs/cgroup/%s/libvirt/lxc/%s/tasks'%(subsystem,slice_name),'w').write(str(os.getpid())) + + except: + print "Error assigning resources: %s"%slice_name + exit(1) + + try: + open('/sys/fs/cgroup/cpuacct/system/libvirtd.service/libvirt/lxc/%s/tasks'%slice_name,'w').write(str(os.getpid())) + except: + print "Error assigning cpuacct: %s" % slice_name + exit(1) + + # If the slice is frozen, then we'll get an EBUSY when trying to write to the task + # list for the freezer cgroup. Since the user couldn't do anything anyway, it's best + # in this case to error out the shell. (an alternative would be to un-freeze it, + # add the task, and re-freeze it) + try: + f=open('/sys/fs/cgroup/freezer/libvirt/lxc/%s/tasks'%(slice_name),'w') + f.write(str(os.getpid())) + # note: we need to call f.close() explicitly, or we'll get an exception in + # the object destructor, which will not be caught + f.close() + except: + print "Error adding task to freezer cgroup. Slice is probably frozen: %s" % slice_name + exit(1) + + setns.chcontext('/proc/%s/ns/uts'%pid) + setns.chcontext('/proc/%s/ns/ipc'%pid) + + if (not args.pidns): + setns.chcontext('/proc/%s/ns/pid'%pid) + + if (not args.netns): + setns.chcontext('/proc/%s/ns/net'%pid) + + if (not args.mntns): + setns.chcontext('/proc/%s/ns/mnt'%pid) + + proc_mounted = False + if (not os.access('/proc/self',0)): + proc_mounted = True + setns.proc_mount() + + for (sysctl_file, sysctl_name, sysctl_val) in sysctls: + for fn in ["/sbin/sysctl", "/usr/sbin/sysctl", "/bin/sysctl", "/usr/bin/sysctl"]: + if os.path.exists(fn): + os.system('%s -w %s=%s'%(fn, sysctl_name,sysctl_val)) + break + else: + print "Error: image does not have a sysctl binary" + + # cgroups is not yet LXC-safe, so we need to use the coarse grained access control + # strategy of unmounting the filesystem + + umount_result = True + for subsystem in ['cpuset','cpu,cpuacct','memory','devices','freezer','net_cls','blkio','perf_event','systemd']: + fs_path = '/sys/fs/cgroup/%s'%subsystem + if (not umount(fs_path,'-l')): + pass + # Leaving these comments for historical reference + #print "Error disabling cgroup access" + #exit(1) - Don't need this because failure here implies failure in the call to umount /sys/fs/cgroup + + if (not umount('/sys/fs/cgroup')): + print "Error disabling cgroup access" + exit(1) + + pid = os.fork() + + # capsh has a --user option starting with f14 + # so if only for f12 we need to fake this one + # + # capsh.c does essentially the following when invoked with --user: + # pwd = getpwnam(user); ... + # ngroups = MAX_GROUPS; + # status = getgrouplist(user, pwd->pw_gid, groups, &ngroups); ... + # status = setgroups(ngroups, groups); ... + # status = setgid(pwd->pw_gid); ... + # status = setuid(pwd->pw_uid); ... + # however we cannot simulate that ourselves because if we did in this process then + # capsh could not be allowed to mess with caps any more + + def getuid (slicename): + import pwd + try: + return pwd.getpwnam(slicename).pw_uid + except: + return + + if (pid == 0): + cap_arg = '--drop='+drop_capabilities + + if (not args.root): + if (args.nosliceuid): + # we still want to drop capabilities, but don't want to switch UIDs + exec_args = [arch,'/usr/sbin/capsh',cap_arg,'--','--login',]+args.command_to_run + else: + uid = getuid (slice_name) + if not uid: + print "lxcsu could not spot %s in /etc/passwd - exiting"%slice_name + exit(1) + exec_args = [arch,'/usr/sbin/capsh',cap_arg,'--uid=%s'%uid,'--','--login',]+args.command_to_run # once we can drop f12, it would be nicer to instead go for -# exec_args = [arch,'/usr/sbin/capsh',cap_arg,'--user=%s'%slice_name,'--','--login',]+args.command_to_run - else: - exec_args = [arch,'/usr/sbin/capsh','--','--login']+args.command_to_run - - os.environ['SHELL'] = '/bin/sh' - if os.path.exists('/etc/planetlab/lib/bind_public.so'): - os.environ['LD_PRELOAD'] = '/etc/planetlab/lib/bind_public.so' - if not args.noslicehome: - os.environ['HOME'] = '/home/%s'%slice_name - os.chdir("/home/%s"%(slice_name)) - if debug: print 'lxcsu:execv:','/usr/bin/setarch',exec_args - os.execv('/usr/bin/setarch',exec_args) - else: - setns.proc_umount() - _,status = os.waitpid(pid,0) - exit(os.WEXITSTATUS(status)) +# exec_args = [arch,'/usr/sbin/capsh',cap_arg,'--user=%s'%slice_name,'--','--login',]+args.command_to_run + else: + exec_args = [arch,'/usr/sbin/capsh','--','--login']+args.command_to_run + + os.environ['SHELL'] = '/bin/sh' + if os.path.exists('/etc/planetlab/lib/bind_public.so'): + os.environ['LD_PRELOAD'] = '/etc/planetlab/lib/bind_public.so' + if not args.noslicehome: + os.environ['HOME'] = '/home/%s'%slice_name + os.chdir("/home/%s"%(slice_name)) + if debug: print 'lxcsu:execv:','/usr/bin/setarch',exec_args + os.execv('/usr/bin/setarch',exec_args) + else: + setns.proc_umount() + _,status = os.waitpid(pid,0) + exit(os.WEXITSTATUS(status)) if __name__ == '__main__': - main() + main() -- 2.43.0 From fd8647e16735947f0a67fa8c8e55b664cfac816b Mon Sep 17 00:00:00 2001 From: Scott Baker Date: Mon, 2 Dec 2013 16:07:42 -0800 Subject: [PATCH 14/16] fix sysctl support causing scp to break due to printing text to ssh connection --- lxcsu | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) mode change 100755 => 100644 lxcsu diff --git a/lxcsu b/lxcsu old mode 100755 new mode 100644 index 4f4a2f7..090cea1 --- a/lxcsu +++ b/lxcsu @@ -189,7 +189,7 @@ def main (): for (sysctl_file, sysctl_name, sysctl_val) in sysctls: for fn in ["/sbin/sysctl", "/usr/sbin/sysctl", "/bin/sysctl", "/usr/bin/sysctl"]: if os.path.exists(fn): - os.system('%s -w %s=%s'%(fn, sysctl_name,sysctl_val)) + os.system('%s -w %s=%s >/dev/null 2>&1'%(fn, sysctl_name,sysctl_val)) break else: print "Error: image does not have a sysctl binary" -- 2.43.0 From 849667ff197e204ee1337ee3f145f4ce1c07dcb8 Mon Sep 17 00:00:00 2001 From: Scott Baker Date: Mon, 2 Dec 2013 16:07:56 -0800 Subject: [PATCH 15/16] fix perms --- lxcsu | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 lxcsu diff --git a/lxcsu b/lxcsu old mode 100644 new mode 100755 -- 2.43.0 From 4fdff3db03d99c6c986d41cb1f662ab7a61f3733 Mon Sep 17 00:00:00 2001 From: Thierry Parmentelat Date: Fri, 10 Jan 2014 11:05:02 +0100 Subject: [PATCH 16/16] =?utf8?q?Simplify=20lxcsu=20by=20querying=20libvirt?= =?utf8?q?=20for=20pid=20=E2=80=94=20this=20is=20a=20manual=20re-injection?= =?utf8?q?=20of=20Sapan=E2=80=99s=20change=20it=20was=20a=20big=20mistake?= =?utf8?q?=20to=20reindent=20automatically=20as=20this=20broke=20the=20log?= =?utf8?q?ic=20entirely?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- lxcsu | 38 ++++++-------------------------------- 1 file changed, 6 insertions(+), 32 deletions(-) diff --git a/lxcsu b/lxcsu index 090cea1..eb3c5ce 100755 --- a/lxcsu +++ b/lxcsu @@ -84,43 +84,17 @@ def main (): debug=True try: - cmd = 'grep %s /proc/*/cgroup | grep freezer'%slice_name - output = os.popen(cmd).readlines() + cmd = '/usr/bin/virsh domid %s'%slice_name + pidnum = int(os.popen(cmd).read().rstrip()) except: print "Error finding slice %s"%slice_name exit(1) - slice_spec = None + pid = '%s'%pidnum + cmdline = open('/proc/%s/cmdline'%pidnum).read().rstrip('\n\x00') + arch = getarch('/proc/%s/exe'%pid) - # provide a default as this is not always properly computed - arch = None - - for e in output: - try: - l = e.rstrip() - path = l.split(':')[0] - comp = l.rsplit(':')[-1] - slice_name_check = comp.rsplit('/')[-1] - if debug: print "dealing with >%s<"%slice_name_check - - if (slice_name_check == slice_name): - if debug: print "found %s"%slice_name - slice_path = path - pid = slice_path.split('/')[2] - cmdline = open('/proc/%s/cmdline'%pid).read().rstrip('\n\x00') - if (cmdline == '/sbin/init') or (cmdline.startswith("init [")): - slice_spec = slice_path - arch = getarch('/proc/%s/exe'%pid) - break - except Exception,e: - if debug: - import traceback - print "BEG lxcsu - ignoring exception" - traceback.print_exc() - print "END lxcsu - ignoring exception" - pass - - if (not slice_spec or not pid): + if (not pid): print "Not started: %s"%slice_name exit(1) -- 2.43.0