mirror of
https://github.com/YunoHost/yunohost.git
synced 2024-09-03 20:06:10 +02:00
Implement config panel for swap management
This commit is contained in:
parent
06dc3da3f4
commit
a54ec11ae7
4 changed files with 160 additions and 0 deletions
|
@ -103,3 +103,105 @@ ynh_require_ram() {
|
|||
return 0
|
||||
fi
|
||||
}
|
||||
|
||||
# Add swap
|
||||
#
|
||||
# usage: ynh_add_swap --size=SWAP in MB
|
||||
# | arg: -s, --size= - Amount of SWAP to add in MB.
|
||||
ynh_add_swap () {
|
||||
# Declare an array to define the options of this helper.
|
||||
declare -Ar args_array=( [s]=size= )
|
||||
local size
|
||||
# Manage arguments with getopts
|
||||
ynh_handle_getopts_args "$@"
|
||||
|
||||
local swap_max_size=$(( $size * 1024 ))
|
||||
|
||||
local free_space=$(df --output=avail / | sed 1d)
|
||||
# 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}
|
||||
|
||||
# Swap on SD card only if it's is specified
|
||||
if ynh_is_main_device_a_sd_card && [ "$SD_CARD_CAN_SWAP" == "0" ]
|
||||
then
|
||||
ynh_print_warn --message="The main mountpoint of your system '/' is on an SD card, swap will not be added to prevent some damage to it. If you still want activate the swap, you can relaunch the command preceded by 'SD_CARD_CAN_SWAP=1'"
|
||||
return
|
||||
fi
|
||||
|
||||
# Compare the available space with the size of the swap.
|
||||
# And set a acceptable size from the request
|
||||
if [ $usable_space -ge $swap_max_size ]
|
||||
then
|
||||
local swap_size=$swap_max_size
|
||||
elif [ $usable_space -ge $(( $swap_max_size / 2 )) ]
|
||||
then
|
||||
local swap_size=$(( $swap_max_size / 2 ))
|
||||
elif [ $usable_space -ge $(( $swap_max_size / 3 )) ]
|
||||
then
|
||||
local swap_size=$(( $swap_max_size / 3 ))
|
||||
elif [ $usable_space -ge $(( $swap_max_size / 4 )) ]
|
||||
then
|
||||
local 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}
|
||||
fi
|
||||
|
||||
# If there's enough space for a swap, and no existing swap here
|
||||
if [ $swap_size -ne 0 ] && [ ! -e /swap_file ]
|
||||
then
|
||||
# Create file
|
||||
truncate -s 0 /swap_file
|
||||
|
||||
# set the No_COW attribute on the swapfile with chattr
|
||||
chattr +C /swap_file
|
||||
|
||||
# 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
|
||||
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
|
||||
# Store the swap size
|
||||
yunohost settings set 'swap.swapfile.swapfile_size' -v ${swap_size}
|
||||
fi
|
||||
}
|
||||
|
||||
ynh_del_swap () {
|
||||
# If there a swap at this place
|
||||
if [ -e /swap_file ]
|
||||
then
|
||||
# Clean the fstab
|
||||
sed -i "/#Swap added by YunoHost config panel/d" /etc/fstab
|
||||
# Desactive the swap file
|
||||
swapoff /swap_file
|
||||
# And remove it
|
||||
rm /swap_file
|
||||
fi
|
||||
}
|
||||
|
||||
# Check if the device of the main mountpoint "/" is an SD card
|
||||
#
|
||||
# [internal]
|
||||
#
|
||||
# return 0 if it's an SD card, else 1
|
||||
ynh_is_main_device_a_sd_card () {
|
||||
local main_device=$(lsblk --output PKNAME --noheadings $(findmnt / --nofsroot --uniq --output source --noheadings --first-only))
|
||||
|
||||
if echo $main_device | grep --quiet "mmc" && [ $(tail -n1 /sys/block/$main_device/queue/rotational) == "0" ]
|
||||
then
|
||||
return 0
|
||||
else
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
|
29
hooks/conf_regen/53-swap
Executable file
29
hooks/conf_regen/53-swap
Executable file
|
@ -0,0 +1,29 @@
|
|||
#!/bin/bash
|
||||
|
||||
set -e
|
||||
|
||||
. /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 a swapfile is requested
|
||||
if [ $(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
|
||||
fi
|
||||
# create the swapfile
|
||||
ynh_add_swap --size=$swapfile_size
|
||||
else
|
||||
# If not, make sure it is deleted
|
||||
ynh_del_swap
|
||||
fi
|
||||
}
|
||||
|
||||
do_$1_regen ${@:2}
|
|
@ -169,3 +169,26 @@ name = "Other"
|
|||
choices.ipv4 = "IPv4 Only"
|
||||
choices.ipv6 = "IPv6 Only"
|
||||
default = "both"
|
||||
[swap]
|
||||
name = "Swap"
|
||||
[swap.swapfile]
|
||||
name = "Swap file configuration"
|
||||
[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."
|
||||
type = "number"
|
||||
default = 1
|
||||
visible = "swapfile_enabled"
|
||||
|
|
|
@ -366,3 +366,9 @@ def reconfigure_dovecot(setting_name, old_value, new_value):
|
|||
regen_conf(names=["dovecot"])
|
||||
command = ["apt-get", "-y", "remove", dovecot_package]
|
||||
subprocess.call(command, env=environment)
|
||||
|
||||
@post_change_hook("swapfile_enabled")
|
||||
@post_change_hook("swapfile_size")
|
||||
def reconfigure_swapfile(setting_name, old_value, new_value):
|
||||
if old_value != new_value:
|
||||
regen_conf(names=["swap"])
|
||||
|
|
Loading…
Add table
Reference in a new issue