helpers: rewrite ynh_add_swap: cleaner code

This commit is contained in:
Salamandar 2024-05-31 16:32:15 +02:00 committed by Salamandar
parent 662bc6097a
commit 03837bd9b8

View file

@ -167,29 +167,21 @@ ynh_add_swap () {
sysctl --quiet --system sysctl --quiet --system
fi fi
# If there's enough space for a swap, and no existing swap here # Workaround for some copy-on-write filesystems like btrfs.
if [ $swap_size -ne 0 ] && [ ! -e /swap_file ] # Create the file
then truncate -s 0 "$swapfile_path"
# Create file # Set the No_COW attribute on the swapfile with chattr
truncate -s 0 /swap_file chattr +C "$swapfile_path" || true
# Set the final file size
dd if=/dev/zero of="$swapfile_path" bs=1024 count="$swap_size_kb"
chmod 0600 "$swapfile_path"
# set the No_COW attribute on the swapfile with chattr # Create the swap
# let's not fail here, as some filesystems like ext4 do not support COW mkswap "$swapfile_path"
chattr +C /swap_file || true # And activate it
swapon "$swapfile_path"
# Preallocate space for the swap file, fallocate may sometime not be used, use dd instead in this case # Then add an entry in fstab to load this swap at each boot.
if ! fallocate -l ${swap_size}K /swap_file echo -e "$swapfile_path swap swap defaults 0 0 #Swap added by $app" >> /etc/fstab
then
dd if=/dev/zero of=/swap_file bs=1024 count=${swap_size}
fi
chmod 0600 /swap_file
# Create the swap
mkswap /swap_file
# And activate it
swapon /swap_file
# Then add an entry in fstab to load this swap at each boot.
echo -e "/swap_file swap swap defaults 0 0 #Swap added by YunoHost config panel" >> /etc/fstab
fi
} }
# Delete the swap file created for the app by ynh_add_swap. # Delete the swap file created for the app by ynh_add_swap.