#!/bin/bash #################################################################################################### #### author: SlickStack ############################################################################ #### link: https://slickstack.io ################################################################### #### mirror: https://mirrors.slickstack.io/bash/ss-install-ubuntu-swapfile.txt ##################### #### path: /var/www/ss-install-ubuntu-swapfile ##################################################### #### destination: n/a (not a boilerplate) ########################################################## #### purpose: Reinstalls the Ubuntu (OS) swapfile as backup virtual memory (idempotent) ############ #### module version: Ubuntu 22.04 LTS ############################################################## #### sourced by: ss-install ######################################################################## #### bash aliases: ss install swap, ss install swapfile, ss install ubuntu swapfile ################ #################################################################################################### ## 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 ## ## source ss-config ## source /var/www/ss-config ## source ss-functions ## source /var/www/ss-functions ## BELOW THIS RELIES ON SS-CONFIG AND SS-FUNCTIONS #################################################################################################### #### TABLE OF CONTENTS (SS-Install-Ubuntu-Swapfile) ################################################ #################################################################################################### ## this is a brief summary of the different code snippets you will find in this script ## ## each section should be commented so you understand what is being accomplished ## ## A. Touch Timestamp File ## B. Message (Begin Script) ## C. Install Swapfile (Conditional) ## D. Reset Permissions (Ubuntu Swapfile) #################################################################################################### #### A. SS-Install-Ubuntu-Swapfile: Touch Timestamp File ########################################### #################################################################################################### ## 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 ## ss_touch "${TIMESTAMP_SS_INSTALL_UBUNTU_SWAPFILE}" #################################################################################################### #### B. SS-Install-Ubuntu-Swapfile: 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 ## ss_echo "${COLOR_INFO}Running ss-install-ubuntu-swapfile... ${COLOR_RESET}" #################################################################################################### ## C. SS-Install-Ubuntu-Swapfile: Install Swapfile (Conditional) ################################### #################################################################################################### ## FUTURE HARDCODE TO 2GB FOR ALL SERVERS SHOULD BE FINE... ## ensure at least 3GB free disk space remains ## DISK_REMAINING_MB=$(df --output=avail -m "$PWD" | tail -n1 | tr -d ' ' 2> /dev/null) TOTAL_RAM_MB=$(echo $(($(getconf _PHYS_PAGES) * $(getconf PAGE_SIZE) / (1024 * 1024))) 2> /dev/null) # echo -e "${COLOR_WARN}Exiting ss-install-ubuntu-swapfile: Swapfile already exists and active so exiting the script now... ${NOCOLOR}" # echo -e "${COLOR_WARN}Exiting ss-install-ubuntu-swapfile: Less than 5GB free disk space so exiting the script now... ${NOCOLOR}" ## install swapfile if not exists ## if [[ -z $(grep "swapfile" /etc/fstab 2> /dev/null) ]] && [[ $DISK_REMAINING_MB -gt 5000 ]]; then 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 ss_fallocate 2G /var/www/cache/system/swapfile 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 swapfile ## ss_mkswap /var/www/cache/system/swapfile ## enable swapfile ## ss_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 ## endif fi #################################################################################################### #### SS-Install-Ubuntu-Swapfile: Delete + Resize Swapfile ############################################### #################################################################################################### ## because of our new auto swapfile size feature we need a "walk back" function in case people downgrade their VMs ## add this later ## remove swapfile if size of swapfile exceeds the amount of installed RAM (then reinstall it) #################################################################################################### #### D. SS-Install-Ubuntu-Swapfile: Reset Permissions (Ubuntu Swapfile) ############################ #################################################################################################### ## run ss-perms-ubuntu-swapfile ## source "${PATH_SS_PERMS_UBUNTU_SWAPFILE}" #################################################################################################### #### G. SS-Install-Ubuntu-Swapfile: Display Active Swapfile ######################################## #################################################################################################### ## show swapfile ## # swapon --show ## move this to stack-overview #################################################################################################### #### PLACEHOLDER: Reset Permissions (SlickStack Scripts) ########################################### #################################################################################################### ## we include this permissions reset in all cron jobs and bash scripts for redundancy ## ## chmod 0700 means only the root/sudo users can execute any SlickStack scripts ## ## THIS SNIPPET DOES NOT RELY ON SS-CONFIG OR SS-FUNCTIONS ## SNIPPET: ss bash scripts, ss cron jobs ## UPDATED: 02JUL2022 chown root:root /var/www/ss* ## must be root:root chown root:root /var/www/crons/*cron* ## must be root:root chown root:root /var/www/crons/custom/*cron* ## must be root:root chmod 0700 /var/www/ss* ## 0700 means only root/sudo can execute chmod 0700 /var/www/crons/*cron* ## 0700 means only root/sudo can execute chmod 0700 /var/www/crons/custom/*cron* ## 0700 means only root/sudo can execute #################################################################################################### #### 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 ## Ref: https://askubuntu.com/questions/898941/how-to-check-ram-size ## Ref: https://kapeli.com/cheat_sheets/Bash_Test_Operators.docset/Contents/Resources/Documents/index ## Ref: https://stackoverflow.com/questions/19703621/get-free-disk-space-with-df-to-just-display-free-space-in-kb ## Ref: https://unix.stackexchange.com/questions/23072/how-can-i-check-if-swap-is-active-from-the-command-line ## SS_EOF