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
|
||||
#
|
||||
# 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= )
|
||||
|
@ -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" ]
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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"
|
||||
|
|
Loading…
Add table
Reference in a new issue