Skip to content

Commit bb8e9ac

Browse files
kv-ybradh352
authored andcommitted
[Build] mount cgroup2 in chroot to fix build on ubuntu 22.04 (sonic-net#13030)
Why I did it Ubuntu 22.04 uses cgroup2 by default, but docker.sh doesn't mount it. As a result we get an error when trying to run docker info in chroot env: ERROR: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running? How I did it mount cgroup2 in chroot if all enabled kernel cgroup controllers are currently not in use by cgroup1 So we need to mount cgroup in chroot environment on /sys/fs/cgroup. Because inside chroot we don't know which cgroup version is used by the host we have two possible solutions: cgroup tree for chroot is mounted by the host (it was my 1st version of this fix) cgroup tree is mounted inside chroot based on info from /proc/cgroups (it's current version of this fix) My 2nd version based on this code from systemd: https://github.com/systemd/systemd/blob/5c6c587ce24096d36826418b5390599d1e5ad55c/src/shared/cgroup-setup.c#L35-L74 We parse info from /proc/cgroups Skip header line started from # Skip controller if it's disabled (4th column = 0) Count number of controllers with non-zero of hierarchy_id (2nd column) If this number is not zero then we assume some of controllers are used by host system and the host system uses hybrid or legacy cgroup tree. In this case we can't use unified cgroup tree inside chroot and mount old cgroup tree (v1). If this number is zero then we assume host system uses unified cgroup tree and we need to mount cgroup2 inside chroot. Signed-off-by: Konstantin Vasin <[email protected]>
1 parent 134460a commit bb8e9ac

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

files/docker/docker

+9-1
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,18 @@ cgroupfs_mount() {
6363
# see also https://github.com/tianon/cgroupfs-mount/blob/master/cgroupfs-mount
6464
if grep -v '^#' /etc/fstab | grep -q cgroup \
6565
|| [ ! -e /proc/cgroups ] \
66-
|| [ ! -d /sys/fs/cgroup ]; then
66+
|| [ ! -d /sys/fs/cgroup ] \
67+
|| [ "$(stat -fc '%T' /sys/fs/cgroup)" = "cgroup2fs" ]; then
6768
return
6869
fi
6970
if ! mountpoint -q /sys/fs/cgroup; then
71+
# cgroup2
72+
# https://github.com/systemd/systemd/blob/5c6c587ce24096d36826418b5390599d1e5ad55c/src/shared/cgroup-setup.c#L35-L74
73+
if awk '!/^#/ && $4 == 1 && $2 != 0 { count++ } END { exit count != 0 }' /proc/cgroups; then
74+
mount -t cgroup2 cgroup2 /sys/fs/cgroup
75+
return
76+
fi
77+
# cgroup1
7078
mount -t tmpfs -o uid=0,gid=0,mode=0755 cgroup /sys/fs/cgroup
7179
fi
7280
(

0 commit comments

Comments
 (0)