- 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)
+ # Enter cgroups
+ current_cgroup = ''
+ for subsystem in ['cpuset', 'memory', 'blkio', 'cpuacct', 'cpuacct,cpu', 'freezer']:
+ try:
+ current_cgroup = subsystem
+
+ # There seems to be a bug in the cgroup schema: cpuacct,cpu can become cpu,cpuacct
+ # We need to handle both
+ task_path_alt = None
+ try:
+ subsystem_comps = subsystem.split(',')
+ subsystem_comps.reverse()
+ subsystem_alt = ','.join(subsystem_comps)
+ tasks_path_alt = [sysfs_root, subsystem_alt,
+ subdirs[subsystem], 'tasks']
+ except Exception as e:
+ pass
+
+ tasks_path = [sysfs_root, subsystem, subdirs[subsystem], 'tasks']
+ tasks_path_str = '/'.join(tasks_path)
+
+ try:
+ f = open(tasks_path_str, 'w')
+ except:
+ tasks_path_alt_str = '/'.join(tasks_path_alt)
+ f = open(tasks_path_alt_str, 'w')
+
+ f.write(str(os.getpid()))
+ if (subsystem == 'freezer'):
+ f.close()
+
+ except Exception as e:
+ if (subsystem not in subdirs):
+ pass
+ else:
+ if debug:
+ print(e)
+ print("Error assigning cgroup %s (pid=%s) for slice %s" %
+ (current_cgroup, driver_pid, slice_name))
+ exit(1)
+
+ def chcontext(path):
+ retcod = setns.chcontext(path)
+ if retcod != 0:
+ print('WARNING - setns(%s)=>%s (ignored)' % (path, retcod))
+ return retcod
+
+ # Use init_pid and not driver_pid to locate reference namespaces
+ ref_ns = "/proc/%s/ns/" % init_pid
+
+ if True:
+ chcontext(ref_ns+'uts')
+ if True:
+ chcontext(ref_ns+'ipc')
+
+ if (not args.no_pidns):
+ chcontext(ref_ns+'pid')
+ if (not args.no_netns):
+ chcontext(ref_ns+'net')
+ if (not args.no_mntns):
+ chcontext(ref_ns+'mnt')