Skip to content

Commit 2e67957

Browse files
committed
runc run: refuse a frozen cgroup
Sometimes a container cgroup already exists but is frozen. When this happens, runc init hangs, and it's not clear what is going on. Refuse to run in a frozen cgroup; add a test case. Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent 356f926 commit 2e67957

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

libcontainer/factory_linux.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,16 @@ func (l *LinuxFactory) Create(id string, config *configs.Config) (Container, err
182182
}
183183
}
184184

185+
// Check that cgroup is not frozen. Do it even if Exists() above returned
186+
// false, since in cgroup v1 it only checks "devices" controller.
187+
st, err := cm.GetFreezerState()
188+
if err != nil {
189+
return nil, fmt.Errorf("unable to get cgroup freezer state: %w", err)
190+
}
191+
if st == configs.Frozen {
192+
return nil, errors.New("container's cgroup unexpectedly frozen")
193+
}
194+
185195
if err := os.MkdirAll(containerRoot, 0o711); err != nil {
186196
return nil, err
187197
}

tests/integration/cgroups.bats

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,3 +339,44 @@ function setup() {
339339
[ "$status" -ne 0 ]
340340
[[ "$output" == *"container's cgroup is not empty"* ]]
341341
}
342+
343+
@test "runc run/create should refuse pre-existing frozen cgroup" {
344+
requires cgroups_freezer
345+
if [[ "$ROOTLESS" -ne 0 ]]; then
346+
requires rootless_cgroup
347+
fi
348+
349+
set_cgroups_path
350+
351+
case $CGROUP_UNIFIED in
352+
no)
353+
FREEZER_DIR="${CGROUP_FREEZER_BASE_PATH}/${REL_CGROUPS_PATH}"
354+
FREEZER="${FREEZER_DIR}/freezer.state"
355+
STATE="FROZEN"
356+
;;
357+
yes)
358+
FREEZER_DIR="${CGROUP_PATH}"
359+
FREEZER="${FREEZER_DIR}/cgroup.freeze"
360+
STATE="1"
361+
;;
362+
esac
363+
364+
# Create and freeze the cgroup.
365+
mkdir -p "$FREEZER_DIR"
366+
echo "$STATE" >"$FREEZER"
367+
368+
# Start a container.
369+
runc run -d --console-socket "$CONSOLE_SOCKET" ct1
370+
[ "$status" -eq 1 ]
371+
# A warning should be printed.
372+
[[ "$output" == *"container's cgroup unexpectedly frozen"* ]]
373+
374+
# Same check for runc create.
375+
runc create --console-socket "$CONSOLE_SOCKET" ct2
376+
[ "$status" -eq 1 ]
377+
# A warning should be printed.
378+
[[ "$output" == *"container's cgroup unexpectedly frozen"* ]]
379+
380+
# Cleanup.
381+
rmdir "$FREEZER_DIR"
382+
}

0 commit comments

Comments
 (0)