#!/bin/bash #################################################################################################### #### author: SlickStack ############################################################################ #### link: https://slickstack.io ################################################################### #### mirror: http://mirrors.slickstack.io/bash/ss-install-ubuntu-swap.txt ########################## #### path: /var/www/ss-install-ubuntu-swap ######################################################### #### destination: n/a (not a boilerplate) ########################################################## #### purpose: Reinstalls the Ubuntu (OS) swap file as backup virtual memory (idempotent) ########### #### module version: Ubuntu 20.04 LTS ############################################################## #### sourced by: ss-install ######################################################################## #### bash aliases: ss install ubuntu kernel, ss install kernel ##################################### #################################################################################################### ## SS-CONFIG MUST BE PROPERLY CONFIGURED AND ON CURRENT BUILD BEFORE RUNNING SS-INSTALL ## ## ENSURE YOUR SS-CONFIG BUILD REMAINS CURRENT BY RUNNING SS-UPDATE OCCASIONALLY ## ## include SlickStack configuration ## source /var/www/ss-config ## include SlickStack functions ## source /var/www/ss-functions #################################################################################################### #### SS-Install-Ubuntu-Swap: Message (Begin Script) ################################################ #################################################################################################### ## this is a simple message that announces to the shell the purpose of this bash script ## ## it will only be noticed by sudo users who manually call ss core bash scripts ## ## echo message ## echo -e "${PURPLE}Running ss-install-ubuntu-swap: Reinstalls the Ubuntu (OS) swap file as backup virtual memory (idempotent)... ${NOCOLOR}" sleep "$SLEEP_MESSAGE_BEGIN" #################################################################################################### #### SS-Install-Ubuntu-Swap: Cleanup Temporary Files ############################################### #################################################################################################### ## delete tmp files ## # n/a #################################################################################################### #### SS-Install-Ubuntu-Swap: Install System Swap File ########################## #################################################################################################### mkdir /var/www/cache/system chown root:root /var/www/cache/system ## must be root:root chmod 0600 /var/www/cache/system ## must be 0600 ## ensure at least 3GB free disk space remains ## if [[ "$SS_SWAPFILE" != "false" ]] && [[ ! $(grep "swapfile" /etc/fstab) ]] && [[ $DISK_FREE -lt 32212254 ]]; then ## 3GB free space needed to make new 2GB swapfile ## create new swap file ## if [[ -z "$SS_SWAPFILE_SIZE" ]]; then sudo fallocate -l 2G /var/www/cache/system/swapfile else sudo fallocate -l "$SS_SWAPFILE_SIZE" /var/www/cache/system/swapfile fi chown root:root /var/www/cache/system/swapfile ## must be root:root chmod 0600 /var/www/cache/system/swapfile ## must be 0600 ## tell Ubuntu about the swap file ## sudo mkswap /var/www/cache/system/swapfile ## enable the new swap file ## sudo swapon /var/www/cache/system/swapfile ## permanently add swapfile to /etc/fstab ## echo ' /var/www/cache/system/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab fi #################################################################################################### #### SS-Install-Ubuntu-Swap: Reset Permissions (Ubuntu Swap) ####################################### #################################################################################################### ## run ss-perms-ubuntu-swap ## source "$PATH_SS_PERMS_UBUNTU_SWAP" #################################################################################################### #### SS-Install-Ubuntu-Swap: Cleanup Temporary Files ############################################### #################################################################################################### ## delete tmp files ## # n/a #################################################################################################### #### SS-Install-Ubuntu-Swap: Touch Timestamp File (End Script) ##################################### #################################################################################################### ## this is a dummy timestamp file that will remember the last time this script was run ## ## it can be useful for developer reference and is sometimes used by SlickStack ## ## script timestamp ## touch "$TIMESTAMP_SS_INSTALL_UBUNTU_SWAP" #################################################################################################### #### SlickStack: External References Used To Improve This Script (Thanks, Interwebz) ############### #################################################################################################### ## manually disable swap ## # sudo swapoff -v /swapfile # Remove entry from /etc/fstab # Remove swapfile with command sudo rm /swapfile ## Ref: https://haydenjames.io/linux-performance-almost-always-add-swap-space/ ## Ref: https://devanswers.co/creating-swap-space-ubuntu-18-04/ ## Ref: https://www.howtoforge.com/ubuntu-swap-file ## Ref: https://linuxize.com/post/how-to-add-swap-space-on-ubuntu-20-04/ ## Ref: https://itsfoss.com/create-swap-file-linux/ ## Ref: https://www.cyberciti.biz/faq/ubuntu-linux-create-add-swap-file/ ## Ref: https://serverfault.com/questions/279248/linux-where-to-put-the-swap-file ## Ref: https://www.digitalocean.com/community/tutorials/how-to-add-swap-space-on-ubuntu-20-04 ## Ref: https://askubuntu.com/questions/652337/why-no-swap-partitions-on-ssd-drives ## Ref: https://askubuntu.com/questions/408340/is-there-any-significance-to-using-tee ## Ref: https://superuser.com/questions/136646/how-to-append-to-a-file-as-sudo ## Ref: https://searchservervirtualization.techtarget.com/tip/Memory-swap-strategies-for-KVM ## Ref: https://www.redhat.com/sysadmin/clear-swap-linux ## Ref: https://askubuntu.com/questions/1357/how-to-empty-swap-if-there-is-free-ram ## Ref: https://askubuntu.com/questions/1189240/swap-file-full-while-memory-is-half-empty ## SS_EOF