fix(ci): claim the loop device atomically and survive a docker-less node

The data-root task runs on all five swarm nodes at once, and they are
containers on one host kernel sharing its loop devices. losetup -f only names
the next free one; by the time the following losetup attached it, a sibling had
taken it - 'failed to set up loop device: Device or resource busy'. Claim with
--find --show, which picks and attaches in one call, and retry with a short
pause when a sibling wins anyway.

The step runs before anything is pulled, so on some nodes docker is not
installed yet and the closing 'systemctl start docker' died with rc=5, taking
the whole task with it under set -e. The two systemctl stop calls above it had
tolerated that all along. Start only when the unit exists, and report
applied-pending-docker instead of applied so the job summary stops claiming a
data root that no daemon has picked up yet.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
kevinveenbirkenbachandClaude Opus 5 committed 2026-08-01 08:47:28 +02:00
1 parent 6c816e893e
commit a8a8396fa3
1 file changed
+21 -5
@@ -128,9 +128,20 @@ systemctl stop docker 2>/dev/null || true
mkdir -p "${MOUNT}" "$(dirname "${IMAGE}")"
rm -f "${IMAGE}"
truncate -s "${SIZE}" "${IMAGE}"
LOOP="$(losetup -f | awk '{print $1}')"
[ -b "${LOOP}" ] || mknod "${LOOP}" b 7 "${LOOP#/dev/loop}"
losetup "${LOOP}" "${IMAGE}"
LOOP=""
attempt=0
while [ -z "${LOOP}" ] && [ "${attempt}" -lt 16 ]; do
attempt=$((attempt + 1))
[ "${attempt}" -eq 1 ] || sleep 0.5
candidate=""
if candidate="$(losetup -f 2>/dev/null)" && [ -n "${candidate}" ] && [ ! -b "${candidate}" ]; then
mknod "${candidate}" b 7 "${candidate#/dev/loop}" 2>/dev/null || candidate=""
fi
if attached="$(losetup --find --show "${IMAGE}" 2>/dev/null)"; then
LOOP="${attached}"
fi
done
[ -n "${LOOP}" ] || decline "no loop device could be claimed after ${attempt} attempts"
if [ "${FSTYPE}" = zfs ]; then
zpool create -f -m "${MOUNT}" "${POOL}" "${LOOP}"
@@ -157,5 +168,10 @@ config["data-root"] = root
path.write_text(json.dumps(config, indent=2) + "\n")
PYTHON
systemctl start docker
verdict applied
if systemctl cat docker.service >/dev/null 2>&1; then
systemctl start docker
verdict applied
else
report "docker.service is not installed yet; the data root takes effect when it is"
verdict applied-pending-docker
fi