mirror of
https://github.com/YunoHost/yunohost.git
synced 2024-09-03 20:06:10 +02:00
Fixes and enhancements for swap management
This commit is contained in:
parent
a54ec11ae7
commit
38fc88dcbe
3 changed files with 28 additions and 30 deletions
|
@ -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= )
|
||||||
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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_print_info --message="Deleting swap first..."
|
||||||
ynh_del_swap
|
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
|
||||||
}
|
}
|
||||||
|
|
|
@ -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"
|
||||||
|
|
Loading…
Add table
Reference in a new issue