Skip to content

Commit 5744509

Browse files
authored
Allow for fluctuation of MemTotal for automatic swapfile size (#4016)
As the reported MemTotal can fluctuate a bit on some systems, e.g. because the reserved memory changes between kernel version or other factors affect it like VRAM, the swap file can be recreated unnecessarily between boots. Allow for some fluctuation (up to +-32MB) before the swapfile is recreated. This was a problem already before the recent haos-swapfile changes, however, before it checked if the existing swapfile isn't smaller than the desired value. If the MemTotal fluctuated there, the swapfile size eventually settled on the highest value seen and it wasn't recreated anymore. With this change, things should be stable even more.
1 parent f3916bb commit 5744509

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

buildroot-external/rootfs-overlay/usr/libexec/haos-swapfile

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ SWAPFILE="/mnt/data/swapfile"
2121

2222
# Swap size in kilobytes (as it's also what meminfo shows)
2323
SWAPSIZE="$(size2kilobytes "${SWAPSIZE}")"
24+
SWAPSIZE_TOLERANCE=0
2425

2526
if [ -z "${SWAPSIZE}" ] || [ "${SWAPSIZE}" = "-1" ]; then
2627
# Default to 33% of total memory
2728
SWAPSIZE="$(awk '/MemTotal/{ print int($2 * 0.33) }' /proc/meminfo)"
2829
echo "[INFO] Using default swapsize of 33% RAM (${SWAPSIZE} kB)"
30+
SWAPSIZE_TOLERANCE=$((32*1024)) # allow for 32MB fluctuations
2931
fi
3032

3133
# Swap space in 4k blocks
@@ -42,7 +44,12 @@ if [ "${SWAPSIZE_BLOCKS}" -lt 10 ]; then
4244
exit 0
4345
fi
4446

45-
if [ ! -s "${SWAPFILE}" ] || [ "$(stat "${SWAPFILE}" -c '%s')" -ne $((SWAPSIZE_BLOCKS * 4096)) ]; then
47+
CURRENT_SIZE="$([ -f "${SWAPFILE}" ] && stat "${SWAPFILE}" -c '%s' || echo 0)"
48+
49+
if [ -s "${SWAPFILE}" ] && [ "${CURRENT_SIZE}" -ge $(((SWAPSIZE - SWAPSIZE_TOLERANCE) * 1024)) ] \
50+
&& [ "${CURRENT_SIZE}" -le $(((SWAPSIZE + SWAPSIZE_TOLERANCE) * 1024)) ]; then
51+
echo "[INFO] Swapfile already exists with size ${CURRENT_SIZE} bytes"
52+
elif [ ! -s "${SWAPFILE}" ] || [ "${CURRENT_SIZE}" -ne $((SWAPSIZE_BLOCKS * 4096)) ]; then
4653
# Check free space (in 4k blocks)
4754
if [ "$(stat -f /mnt/data -c '%f')" -lt "${SWAPSIZE_BLOCKS}" ]; then
4855
echo "[ERROR] Not enough space to allocate swapfile"

0 commit comments

Comments
 (0)