[Fix, CI] (.github/actions, .github/workflows, scripts/ci): stop creating the swap file over a zram or partition device

This commit is contained in:
2026-11-16 02:46:55 -04:00
parent 8d3d3d13a7
commit 9ce8caa1d0
4 changed files with 272 additions and 17 deletions
+3 -17
View File
@@ -7,23 +7,9 @@ inputs:
runs:
using: composite
steps:
# Hosted images differ: some ship /swapfile, some ship no swap at all. Turn off whatever is
# active, then create the file at the path the image used, or /swapfile when there was none.
# scripts/ci/set_swap_space.sh says which swap it replaces, where the file goes and when it
# is sized down; it prints the swap state and free space before and after.
- shell: bash
env:
SWAP_SIZE_GB: ${{ inputs.swap-size-gb }}
run: |
set -euo pipefail
active="$(swapon --show=NAME --noheadings)"
swap_file="$(printf '%s\n' "$active" | grep '^/' | tail -n 1 || true)"
swap_file="${swap_file:-/swapfile}"
if [ -n "$active" ]; then
printf '%s\n' "$active" | xargs -r -n1 sudo swapoff
fi
sudo rm -f "$swap_file"
sudo fallocate -l "${SWAP_SIZE_GB}G" "$swap_file"
sudo chmod 600 "$swap_file"
sudo mkswap "$swap_file"
sudo swapon "$swap_file"
free -h
swapon --show
run: bash "${GITHUB_ACTION_PATH}/../../../scripts/ci/set_swap_space.sh"
+1
View File
@@ -3802,6 +3802,7 @@ jobs:
python3 tools/trace_replay/test_run_trace_case.py
python3 scripts/ci/build_fingerprint.py --self-test
bash scripts/ci/split_reuse_build_type.sh --self-test
bash scripts/ci/test_set_swap_space.sh
python3 MobileGL/MG_IntegrationTest/Harness/test_split_log_paths.py
python3 scripts/ci/transport_pinned.py --self-test
+120
View File
@@ -0,0 +1,120 @@
#!/bin/bash
# MobileGL - scripts/ci/set_swap_space.sh
# Copyright (c) 2025-2026 MobileGL-Dev
# Licensed under the GNU Lesser General Public License v3.0:
# https://www.gnu.org/licenses/gpl-3.0.txt
# https://www.gnu.org/licenses/lgpl-3.0.txt
# SPDX-License-Identifier: LGPL-3.0-only
# End of Source File Header
# Replaces the runner's swap with one swap file of SWAP_SIZE_GB GiB (.github/actions/set-swap-space).
# Hosted images differ: some ship a /swapfile, some no swap, and a swap partition or zram device is
# possible. Every active swap area is turned off, but only a swap file is replaced in place; with
# none the file is /swapfile, or /mnt/swapfile when that file system has more room. The file is
# sized down to what fits with SWAP_MARGIN_MIB to spare, and the step fails only below 1 GiB.
# df only informs the size: when it reports nothing usable, the requested size is tried as is.
#
# usage: set_swap_space.sh (SWAP_SIZE_GB from the action input; SWAP_MARGIN_MIB and
# SWAP_ALT_DIR, the /mnt fallback directory, are optional)
set -euo pipefail
size_gb="${SWAP_SIZE_GB:-}"
margin_mib="${SWAP_MARGIN_MIB:-2048}"
alt_dir="${SWAP_ALT_DIR:-/mnt}"
floor_gb=1
case "${size_gb}" in
'' | *[!0-9]* | 0)
echo "::error::swap-size-gb must be a positive whole number of GiB, got '${size_gb}'"
exit 1
;;
esac
# Prints the MiB available on the file system that holds $1, or nothing when df fails or prints
# something else.
avail_mib() {
local value
value="$(df --output=avail -B1M "$1" 2> /dev/null | tail -n 1 | tr -d ' ' || true)"
case "${value}" in
'' | *[!0-9]*) ;;
*) printf '%s\n' "${value}" ;;
esac
}
show_state() {
echo "Swap $1:"
swapon --show || true
free -h || true
}
show_state before
swap_file=""
swap_names=()
while read -r name type _; do
[ -n "${name}" ] || continue
swap_names+=("${name}")
if [ "${type}" = file ]; then
swap_file="${name}"
fi
done < <(swapon --show=NAME,TYPE --noheadings)
swap_file="${swap_file:-/swapfile}"
for name in "${swap_names[@]}"; do
sudo swapoff "${name}"
done
sudo rm -f "${swap_file}"
want_mib=$((size_gb * 1024))
swap_dir="$(dirname "${swap_file}")"
avail="$(avail_mib "${swap_dir}")"
if [ -z "${avail}" ]; then
echo "df did not report the space available on ${swap_dir}; trying the requested ${size_gb} GiB"
elif [ "${avail}" -lt $((want_mib + margin_mib)) ] && [ "${swap_dir}" != "${alt_dir}" ] &&
[ -d "${alt_dir}" ]; then
alt_avail="$(avail_mib "${alt_dir}")"
if [ -n "${alt_avail}" ] && [ "${alt_avail}" -gt "${avail}" ]; then
echo "${swap_dir} has ${avail} MiB available and ${alt_dir} has ${alt_avail} MiB; using ${alt_dir}/swapfile"
swap_file="${alt_dir}/swapfile"
swap_dir="${alt_dir}"
avail="${alt_avail}"
sudo rm -f "${swap_file}"
fi
fi
df -h "${swap_dir}" || true
if [ -n "${avail}" ] && [ "${avail}" -lt $((want_mib + margin_mib)) ]; then
# df only shrinks the file; whether even the floor fits is left to fallocate, which may use
# blocks df does not count as available.
fit_gb=$(((avail - margin_mib) / 1024))
if [ "${fit_gb}" -lt "${floor_gb}" ]; then
fit_gb="${floor_gb}"
fi
echo "::warning::${swap_dir} has ${avail} MiB available; the swap file is ${fit_gb} GiB instead of the requested ${size_gb} GiB"
size_gb="${fit_gb}"
fi
# A refusal that df did not predict halves the size down to the floor, printing what the file
# system looked like each time.
df_note="df reported ${avail} MiB available"
if [ -z "${avail}" ]; then
df_note="df did not report the available space"
fi
while ! sudo fallocate -l "${size_gb}G" "${swap_file}"; do
sudo rm -f "${swap_file}"
df -h "${swap_dir}" || true
findmnt -T "${swap_dir}" || true
lsblk || true
if [ "${size_gb}" -le "${floor_gb}" ]; then
echo "::error::fallocate of ${size_gb} GiB at ${swap_file} failed; ${df_note}"
exit 1
fi
next_gb=$((size_gb / 2 > floor_gb ? size_gb / 2 : floor_gb))
echo "::warning::fallocate of ${size_gb} GiB at ${swap_file} failed; ${df_note}; trying ${next_gb} GiB"
size_gb="${next_gb}"
done
sudo chmod 600 "${swap_file}"
sudo mkswap "${swap_file}"
sudo swapon "${swap_file}"
show_state after
df -h "${swap_dir}" || true
+148
View File
@@ -0,0 +1,148 @@
#!/bin/bash
# MobileGL - scripts/ci/test_set_swap_space.sh
# Copyright (c) 2025-2026 MobileGL-Dev
# Licensed under the GNU Lesser General Public License v3.0:
# https://www.gnu.org/licenses/gpl-3.0.txt
# https://www.gnu.org/licenses/lgpl-3.0.txt
# SPDX-License-Identifier: LGPL-3.0-only
# End of Source File Header
# Runs scripts/ci/set_swap_space.sh against stubbed sudo (which logs swapoff, fallocate and the
# other privileged commands instead of running them), swapon, df, free, findmnt and lsblk, so no
# case touches the host's swap. STUB_SWAPS lists the active swap areas as "NAME TYPE" lines and
# STUB_FS the file systems as "mount=available-MiB" words; fallocate fails with ENOSPC when the
# size does not fit, as a small devtmpfs on /dev does, or exceeds STUB_FALLOCATE_MAX_GB, and
# STUB_DF=fail or STUB_DF=garbage makes df fail or print no number. The /mnt fallback is a
# temporary directory, so the result does not depend on the host's /mnt.
# Each case checks the exit status and the privileged commands the script ran.
#
# usage: test_set_swap_space.sh [script] (defaults to set_swap_space.sh next to this file)
set -euo pipefail
script="${1:-$(cd "$(dirname "$0")" && pwd)/set_swap_space.sh}"
stubs="$(mktemp -d)"
trap 'rm -rf "${stubs}"' EXIT
cat > "${stubs}/stub-lib" << 'EOF'
mount_of() {
local path="$1" best="" mount
for entry in ${STUB_FS}; do
mount="${entry%%=*}"
case "${path}" in
"${mount}" | "${mount%/}"/*)
if [ "${#mount}" -gt "${#best}" ]; then
best="${mount}"
fi
;;
esac
done
printf '%s\n' "${best}"
}
avail_of() {
local mount entry
mount="$(mount_of "$1")"
for entry in ${STUB_FS}; do
if [ "${entry%%=*}" = "${mount}" ]; then
printf '%s\n' "${entry#*=}"
fi
done
}
EOF
stub() {
printf '#!/bin/bash\n. "%s/stub-lib"\n%s\n' "${stubs}" "$2" > "${stubs}/$1"
chmod +x "${stubs}/$1"
}
stub sudo 'echo "$*" >> "${STUB_LOG}"
if [ "$1" = fallocate ]; then
want="${3%G}"
if [ $((want * 1024)) -gt "$(avail_of "$(dirname "$4")")" ] ||
[ "${want}" -gt "${STUB_FALLOCATE_MAX_GB:-${want}}" ]; then
echo "fallocate: fallocate failed: No space left on device" >&2
exit 1
fi
fi'
stub swapon 'if [ "$1" = --show=NAME,TYPE ] && [ "${2:-}" = --noheadings ]; then
printf "%s\n" "${STUB_SWAPS}" | sed "/^$/d"
elif [ "$1" = --show=NAME ] && [ "${2:-}" = --noheadings ]; then
printf "%s\n" "${STUB_SWAPS}" | sed "/^$/d" | cut -d" " -f1
else
printf "%s\n" "${STUB_SWAPS}"
fi'
stub df 'path="${*: -1}"
if [ "${STUB_DF:-}" = fail ]; then
echo "df: stub failure" >&2
exit 1
elif [ "${STUB_DF:-}" = garbage ]; then
echo "Avail"
echo "-"
elif [ "$1" = --output=avail ]; then
echo "1M-blocks"
avail_of "${path}"
else
echo "Filesystem Avail Mounted on"
echo "stub $(avail_of "${path}")M $(mount_of "${path}")"
fi'
stub free 'echo "Mem: stub"'
stub findmnt 'true'
stub lsblk 'true'
failures=0
# expect <case> <exit> <expected sudo log, ";"-separated> <swaps> <file systems> [size]
expect() {
local name="$1" want_exit="$2" want_log="$3" got_exit=0 got_log
: > "${stubs}/log"
PATH="${stubs}:${PATH}" STUB_LOG="${stubs}/log" STUB_SWAPS="$4" STUB_FS="$5" \
SWAP_SIZE_GB="${6:-8}" SWAP_ALT_DIR="${SWAP_ALT_DIR:-${mnt}}" \
bash -euo pipefail "${script}" > "${stubs}/out" 2>&1 || got_exit=$?
got_log="$(paste -sd ';' "${stubs}/log")"
if [ "${got_exit}" -ne "${want_exit}" ] || [ "${got_log}" != "${want_log}" ]; then
echo "NOT OK ${name}: exit ${got_exit} (expected ${want_exit})"
echo " ran: ${got_log}"
echo " expected: ${want_log}"
sed 's/^/ | /' "${stubs}/out"
failures=$((failures + 1))
fi
}
# The /mnt fallback directory; ${mnt}/missing stands for a host without /mnt.
mnt="${stubs}/mnt"
mkdir "${mnt}"
roomy="/=93000 /dev=4 ${mnt}=70000"
replace() {
echo "rm -f $1;fallocate -l $2G $1;chmod 600 $1;mkswap $1;swapon $1"
}
expect "image swap file" 0 "swapoff /swapfile;$(replace /swapfile 8)" "/swapfile file" "${roomy}"
expect "no swap" 0 "$(replace /swapfile 32)" "" "${roomy}" 32
expect "swap file on /mnt" 0 "swapoff ${mnt}/swapfile;$(replace "${mnt}/swapfile" 8)" \
"${mnt}/swapfile file" "${roomy}"
expect "zram device" 0 "swapoff /dev/zram0;$(replace /swapfile 8)" "/dev/zram0 partition" "${roomy}"
expect "swap partition and file" 0 "swapoff /dev/sdb2;swapoff /swapfile;$(replace /swapfile 8)" \
"/dev/sdb2 partition
/swapfile file" "${roomy}"
expect "short root, roomy /mnt" 0 "rm -f /swapfile;$(replace "${mnt}/swapfile" 16)" \
"" "/=6000 /dev=4 ${mnt}=70000" 16
expect "short root, no /mnt file system" 0 "$(replace /swapfile 3)" "" "/=6000 /dev=4" 16
SWAP_ALT_DIR="${mnt}/missing" expect "short root, no /mnt directory" 0 "$(replace /swapfile 3)" \
"" "/=6000 /dev=4 ${mnt}/missing=70000" 16
expect "room for the floor only" 0 "$(replace /swapfile 1)" "" "/=2500 /dev=4" 8
expect "no room anywhere" 1 "rm -f /swapfile;fallocate -l 1G /swapfile;rm -f /swapfile" "" \
"/=900 /dev=4" 8
expect "size not a number" 1 "" "" "${roomy}" 8G
STUB_DF=fail expect "df fails" 0 "swapoff /swapfile;$(replace /swapfile 8)" "/swapfile file" "${roomy}"
STUB_DF=garbage expect "df prints no number" 0 "$(replace /swapfile 8)" "" "${roomy}"
STUB_DF=fail STUB_FALLOCATE_MAX_GB=4 expect "df fails and fallocate refuses" 0 \
"rm -f /swapfile;fallocate -l 8G /swapfile;$(replace /swapfile 4)" "" "${roomy}"
STUB_FALLOCATE_MAX_GB=2 expect "fallocate refuses although df has room" 0 \
"rm -f /swapfile;fallocate -l 8G /swapfile;rm -f /swapfile;fallocate -l 4G /swapfile;$(replace /swapfile 2)" \
"" "${roomy}"
STUB_FALLOCATE_MAX_GB=0 expect "fallocate refuses every size" 1 \
"rm -f /swapfile;fallocate -l 2G /swapfile;rm -f /swapfile;fallocate -l 1G /swapfile;rm -f /swapfile" \
"" "${roomy}" 2
if [ "${failures}" -ne 0 ]; then
echo "set_swap_space self-test: FAILED"
exit 1
fi
echo "set_swap_space self-test: ok"