More sanity in free memory calculation

Co-authored-by: ljf (zamentur) <zamentur@users.noreply.github.com>
This commit is contained in:
tituspijean 2024-08-17 09:48:35 +02:00 committed by GitHub
parent e039d2e195
commit 43992e1338
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -144,21 +144,18 @@ ynh_add_swap () {
return return
fi fi
local swap_max_size_kb=$(( size * 1024 ))
local free_space_kb local free_space_kb
free_space_kb=$(df --block-size=K --output=avail "$swapfile_dir" | sed 1d | sed -e 's/K$//') free_space_kb=$(df --block-size=K --output=avail "$swapfile_dir" | sed 1d | sed -e 's/K$//')
# Because we don't want to fill the disk with a swap file, divide by 2 the available space. # We don't want to fill the disk with a swap file.
local usable_space_kb=$(( free_space_kb / 2 )) local usable_space=$(( free_space_kb / 1024 - 1024 ))
# Compare the available space with the size of the swap. # Compare the available space with the size of the swap.
# And set a acceptable size from the request if (( size > usable_space )); then
size_space_ratio=$(( swap_max_size_kb / usable_space_kb + 1 ))
if (( size_space_ratio > 4)); then
ynh_print_warn --message="Not enough space for a swap file at $swapfile_path." ynh_print_warn --message="Not enough space for a swap file at $swapfile_path."
return return
fi fi
swap_size_kb=$(( swap_max_size_kb / size_space_ratio )) swap_size_kb=$(( size * 1024 ))
# Configure swappiness # Configure swappiness
if [ ! -f /etc/sysctl.d/999-yunohost-swap.conf ]; then if [ ! -f /etc/sysctl.d/999-yunohost-swap.conf ]; then