Skip to content

Commit

Permalink
Fixes and enhancements for swap management
Browse files Browse the repository at this point in the history
  • Loading branch information
tituspijean committed May 30, 2024
1 parent a54ec11 commit 38fc88d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 30 deletions.
23 changes: 10 additions & 13 deletions helpers/helpers.v1.d/hardware
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ ynh_require_ram() {

# Add swap
#
# usage: ynh_add_swap --size=SWAP in MB
# | arg: -s, --size= - Amount of SWAP to add in MB.
# usage: ynh_add_swap --size=SWAP in MiB
# | arg: -s, --size= - Amount of SWAP to add in MiB.
ynh_add_swap () {
# Declare an array to define the options of this helper.
declare -Ar args_array=( [s]=size= )
Expand All @@ -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.
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
if ynh_is_main_device_a_sd_card && [ "$SD_CARD_CAN_SWAP" == "0" ]
Expand All @@ -134,21 +134,19 @@ ynh_add_swap () {
# And set a acceptable size from the request
if [ $usable_space -ge $swap_max_size ]
then
local swap_size=$swap_max_size
swap_size=$swap_max_size
elif [ $usable_space -ge $(( $swap_max_size / 2 )) ]
then
local swap_size=$(( $swap_max_size / 2 ))
swap_size=$(( $swap_max_size / 2 ))
elif [ $usable_space -ge $(( $swap_max_size / 3 )) ]
then
local swap_size=$(( $swap_max_size / 3 ))
swap_size=$(( $swap_max_size / 3 ))
elif [ $usable_space -ge $(( $swap_max_size / 4 )) ]
then
local swap_size=$(( $swap_max_size / 4 ))
swap_size=$(( $swap_max_size / 4 ))
else
echo "Not enough space left for a swap file" >&2
local swap_size=0
# Store the swap size
yunohost settings set 'swap.swapfile.swapfile_size' -v ${swap_size}
swap_size=0
fi

# If there's enough space for a swap, and no existing swap here
Expand All @@ -158,7 +156,8 @@ ynh_add_swap () {
truncate -s 0 /swap_file

# 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
if ! fallocate -l ${swap_size}K /swap_file
Expand All @@ -172,8 +171,6 @@ ynh_add_swap () {
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
# Store the swap size
yunohost settings set 'swap.swapfile.swapfile_size' -v ${swap_size}
fi
}

Expand Down
16 changes: 9 additions & 7 deletions hooks/conf_regen/53-swap
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
#!/bin/bash

set -e

. /usr/share/yunohost/helpers
source /usr/share/yunohost/helpers

do_pre_regen() {
:
}

do_post_regen() {

swapfile_enabled="$(yunohost settings get 'swap.swapfile.swapfile_enabled')"
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 [ $(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
ynh_del_swap
ynh_print_info --message="Deleting swap first..."
ynh_del_swap
fi
# create the swapfile
ynh_print_info --message="Attempting to create $swapfile_size MB swap..."
ynh_add_swap --size=$swapfile_size
ynh_print_info --message="A $((swap_size / 1024)) MB swap has been created."
else
# If not, make sure it is deleted
ynh_print_info --message="Deleting swap..."
ynh_del_swap
fi
}
Expand Down
19 changes: 9 additions & 10 deletions share/config_global.toml
Original file line number Diff line number Diff line change
Expand Up @@ -173,22 +173,21 @@ name = "Other"
name = "Swap"
[swap.swapfile]
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]
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."
type = "boolean"
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]
ask = "How many megabytes should the swap file be?"
help = "Only integers are accepted. Beware, if you reduce its current size, you may crash your server."
ask = "How many Mebibytes should the swap file be?"
help = "Only integers are accepted ( 1 MiB = 1024 B). Beware, if you reduce its current size, you may crash your server."
type = "number"
default = 1
default = 1024
visible = "swapfile_enabled"

0 comments on commit 38fc88d

Please sign in to comment.