Fixes and enhancements for swap management

This commit is contained in:
tituspijean 2024-05-30 21:34:49 +00:00
parent a54ec11ae7
commit 38fc88dcbe
No known key found for this signature in database
GPG key ID: EF3B0D7CC0A94720
3 changed files with 28 additions and 30 deletions

View file

@ -106,8 +106,8 @@ ynh_require_ram() {
# Add swap # Add swap
# #
# usage: ynh_add_swap --size=SWAP in MB # usage: ynh_add_swap --size=SWAP in MiB
# | arg: -s, --size= - Amount of SWAP to add in MB. # | arg: -s, --size= - Amount of SWAP to add in MiB.
ynh_add_swap () { ynh_add_swap () {
# Declare an array to define the options of this helper. # Declare an array to define the options of this helper.
declare -Ar args_array=( [s]=size= ) declare -Ar args_array=( [s]=size= )
@ -121,7 +121,7 @@ ynh_add_swap () {
# Because we don't want to fill the disk with a swap file, divide by 2 the available space. # Because we don't want to fill the disk with a swap file, divide by 2 the available space.
local usable_space=$(( $free_space / 2 )) local usable_space=$(( $free_space / 2 ))
SD_CARD_CAN_SWAP=${SD_CARD_CAN_SWAP:-0} SD_CARD_CAN_SWAP=${SD_CARD_CAN_SWAP:-0}
# Swap on SD card only if it's is specified # Swap on SD card only if it's is specified
if ynh_is_main_device_a_sd_card && [ "$SD_CARD_CAN_SWAP" == "0" ] if ynh_is_main_device_a_sd_card && [ "$SD_CARD_CAN_SWAP" == "0" ]
@ -134,21 +134,19 @@ ynh_add_swap () {
# And set a acceptable size from the request # And set a acceptable size from the request
if [ $usable_space -ge $swap_max_size ] if [ $usable_space -ge $swap_max_size ]
then then
local swap_size=$swap_max_size swap_size=$swap_max_size
elif [ $usable_space -ge $(( $swap_max_size / 2 )) ] elif [ $usable_space -ge $(( $swap_max_size / 2 )) ]
then then
local swap_size=$(( $swap_max_size / 2 )) swap_size=$(( $swap_max_size / 2 ))
elif [ $usable_space -ge $(( $swap_max_size / 3 )) ] elif [ $usable_space -ge $(( $swap_max_size / 3 )) ]
then then
local swap_size=$(( $swap_max_size / 3 )) swap_size=$(( $swap_max_size / 3 ))
elif [ $usable_space -ge $(( $swap_max_size / 4 )) ] elif [ $usable_space -ge $(( $swap_max_size / 4 )) ]
then then
local swap_size=$(( $swap_max_size / 4 )) swap_size=$(( $swap_max_size / 4 ))
else else
echo "Not enough space left for a swap file" >&2 echo "Not enough space left for a swap file" >&2
local swap_size=0 swap_size=0
# Store the swap size
yunohost settings set 'swap.swapfile.swapfile_size' -v ${swap_size}
fi fi
# If there's enough space for a swap, and no existing swap here # If there's enough space for a swap, and no existing swap here
@ -158,7 +156,8 @@ ynh_add_swap () {
truncate -s 0 /swap_file truncate -s 0 /swap_file
# set the No_COW attribute on the swapfile with chattr # set the No_COW attribute on the swapfile with chattr
chattr +C /swap_file # let's not fail here, as some filesystems like ext4 do not support COW
chattr +C /swap_file || true
# Preallocate space for the swap file, fallocate may sometime not be used, use dd instead in this case # Preallocate space for the swap file, fallocate may sometime not be used, use dd instead in this case
if ! fallocate -l ${swap_size}K /swap_file if ! fallocate -l ${swap_size}K /swap_file
@ -172,8 +171,6 @@ ynh_add_swap () {
swapon /swap_file swapon /swap_file
# Then add an entry in fstab to load this swap at each boot. # 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 echo -e "/swap_file swap swap defaults 0 0 #Swap added by YunoHost config panel" >> /etc/fstab
# Store the swap size
yunohost settings set 'swap.swapfile.swapfile_size' -v ${swap_size}
fi fi
} }

View file

@ -1,27 +1,29 @@
#!/bin/bash #!/bin/bash
set -e source /usr/share/yunohost/helpers
. /usr/share/yunohost/helpers
do_pre_regen() { do_pre_regen() {
:
} }
do_post_regen() { do_post_regen() {
swapfile_enabled="$(yunohost settings get 'swap.swapfile.swapfile_enabled')" swapfile_enabled="$(yunohost settings get 'swap.swapfile.swapfile_enabled')"
swapfile_size="$(yunohost settings get 'swap.swapfile.swapfile_size')" swapfile_size="$(yunohost settings get 'swap.swapfile.swapfile_size')"
if [ "${swapfile_enabled}" == "True" ]; then if [ ${swapfile_enabled} -eq 1 ]; then
# If a swapfile is requested # If a swapfile is requested
if [ $(stat -c%s /swap_file) -ne $swapfile_size ]; then if [ -f /swap_file ] && [ $(stat -c%s /swap_file) -ne $swapfile_size ]; then
# Let's delete it first if it is not of the requested size # Let's delete it first if it is not of the requested size
ynh_del_swap ynh_print_info --message="Deleting swap first..."
ynh_del_swap
fi fi
# create the swapfile # create the swapfile
ynh_print_info --message="Attempting to create $swapfile_size MB swap..."
ynh_add_swap --size=$swapfile_size ynh_add_swap --size=$swapfile_size
ynh_print_info --message="A $((swap_size / 1024)) MB swap has been created."
else else
# If not, make sure it is deleted # If not, make sure it is deleted
ynh_print_info --message="Deleting swap..."
ynh_del_swap ynh_del_swap
fi fi
} }

View file

@ -173,22 +173,21 @@ name = "Other"
name = "Swap" name = "Swap"
[swap.swapfile] [swap.swapfile]
name = "Swap file configuration" name = "Swap file configuration"
[swap.swapfile.swapfile_warning]
ask = "Absolutely do not create a swap file if your root partition is on a SD card!"
help = "The intensive writing on the SD card will degrade it fast."
type = "alert"
style = "warning"
[swap.swapfile.swapfile_enabled] [swap.swapfile.swapfile_enabled]
ask = "Do you need to create a swap file?" ask = "Do you need to create a swap file?"
help = "A swap file will extend the available RAM by using some of your storage space." help = "A swap file will extend the available RAM by using some of your storage space."
type = "boolean" type = "boolean"
default = false default = false
visible = "swapfile_enabled"
[swap.swapfile.swapfile_warning]
ask = "Absolutely do not create a swap file if your root partition is on a SD card!"
help = "The intensive writing on the SD card will degrade it fast."
type = "alert"
style = "danger"
[swap.swapfile.swapfile_size] [swap.swapfile.swapfile_size]
ask = "How many megabytes should the swap file be?" ask = "How many Mebibytes should the swap file be?"
help = "Only integers are accepted. Beware, if you reduce its current size, you may crash your server." help = "Only integers are accepted ( 1 MiB = 1024 B). Beware, if you reduce its current size, you may crash your server."
type = "number" type = "number"
default = 1 default = 1024
visible = "swapfile_enabled" visible = "swapfile_enabled"