From c0d0d48c45258b6461c9601232d0633812da459b Mon Sep 17 00:00:00 2001 From: Stefan Agner Date: Mon, 27 Mar 2023 09:34:38 +0200 Subject: [PATCH] Fix swapfile creation for all memory sizes (#2427) * Fix swapfile creation for all memory sizes In certain situation awk prints the swapfile size in scientific notation. The script can't deal with that, in which case swap file creation fails. Use int to convert the number to an integer. Since pages are 4k, also make sure swapsize is aligned to 4k blocks. * Add info message --- .../rootfs-overlay/usr/libexec/haos-swapfile | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/buildroot-external/rootfs-overlay/usr/libexec/haos-swapfile b/buildroot-external/rootfs-overlay/usr/libexec/haos-swapfile index 8a3eb399a..add55a130 100755 --- a/buildroot-external/rootfs-overlay/usr/libexec/haos-swapfile +++ b/buildroot-external/rootfs-overlay/usr/libexec/haos-swapfile @@ -2,17 +2,19 @@ set -e swapfile="/mnt/data/swapfile" -swapsize="$(awk '/MemTotal/{ print $2 * 0.33 }' /proc/meminfo)" +# Swap space in 4k blocks +swapsize="$(awk '/MemTotal/{ print int($2 * 0.33 / 4) }' /proc/meminfo)" -if [ ! -s "${swapfile}" ] || [ "$(stat "${swapfile}" -c '%s')" -lt $((swapsize * 1024)) ]; then +if [ ! -s "${swapfile}" ] || [ "$(stat "${swapfile}" -c '%s')" -lt $((swapsize * 4096)) ]; then # Check free space (in 4k blocks) - if [ "$(stat -f /mnt/data -c '%f')" -lt $((swapsize / 4)) ]; then + if [ "$(stat -f /mnt/data -c '%f')" -lt "${swapsize}" ]; then echo "[WARNING] Not enough space to allocate swapfile" exit 1 fi - dd if=/dev/zero of="${swapfile}" bs=1k count="${swapsize}" + echo "[INFO] Creating swapfile of size $((swapsize *4))k" + dd if=/dev/zero of="${swapfile}" bs=4k count="${swapsize}" fi if ! swaplabel "${swapfile}" > /dev/null 2>&1; then