source: common/run-in-cgroup.sh@ cff36a7

ablfs-more trunk
Last change on this file since cff36a7 was cff36a7, checked in by Pierre Labastie <pierre.labastie@…>, 7 months ago

Fix session management in run-in-cgroup.sh

Still WIP but some progress...
When moving a shell to a cgroup not associated with a session,
then subsequent calls to pam_systemd or pam_elogind create
a new session, and a new cgroup for that session, so that the
cgroup of the calling process is not used (this is a problem
with both systemd and elogind). For systemd, the problem can be
solved by passing --slice <user slice> to systemd-run. For elogind,
we need to first move the shell to a non session cgroup, then run
sudo so that a new session is created, then pass the cpuset to that
session's cgroup. Hopefully, if neither systemd nor elgind is used,
then the former solution should work (to be tested!!!).

  • Property mode set to 100755
File size: 1.2 KB
Line 
1#!/bin/bash
2
3if [ -z "$CPUSPEC" ] || [ "$#" -lt 1 ]; then
4 echo "usage: CPUSPEC=... $0 command"
5 exit 1
6fi
7
8set +e
9
10if type systemd-run >/dev/null 2>&1 ; then # systemd
11 sudo systemd-run -G --pty -d --uid=$(whoami) \
12 -p AllowedCPUs="$CPUSPEC" \
13 --slice "user-$(whoami).slice" \
14 "$@"
15elif type loginctl >/dev/null 2>&1 ; then #elogind
16 sudo mkdir /sys/fs/cgroup/jhalfs
17 sudo sh -c "echo +cpuset > /sys/fs/cgroup/cgroup.subtree_control"
18 (
19 sudo sh -c "echo $BASHPID > /sys/fs/cgroup/jhalfs/cgroup.procs"
20 sudo -u $(whoami) sh <<EOF
21 SESS_CGROUP=/sys/fs/cgroup/\$XDG_SESSION_ID
22 sudo sh -c "echo \\"$CPUSPEC\\" > \$SESS_CGROUP/cpuset.cpus"
23 (sudo sh -c "echo \$BASHPID > \$SESS_CGROUP/cgroup.procs" &&
24 exec $@)
25EOF
26 )
27 sudo rmdir /sys/fs/cgroup/jhalfs
28else # no session manager
29 sudo mkdir /sys/fs/cgroup/jhalfs
30 sudo sh -c "echo +cpuset > /sys/fs/cgroup/cgroup.subtree_control"
31 sudo sh -c "echo \"$CPUSPEC\" > /sys/fs/cgroup/jhalfs/cpuset.cpus"
32 (sudo sh -c "echo $BASHPID > /sys/fs/cgroup/jhalfs/cgroup.procs" &&
33 exec "$@")
34 sudo rmdir /sys/fs/cgroup/jhalfs
35fi
Note: See TracBrowser for help on using the repository browser.