185 lines
5.1 KiB
Bash
185 lines
5.1 KiB
Bash
#!/bin/bash
|
|
|
|
## addswap.sh
|
|
## version 1.0
|
|
## Quickly add and activate a SWAP file.
|
|
|
|
## Author: Ze'ev Schurmann
|
|
## License: GPL3.0 or later
|
|
## Git Repo: https://git.zaks.web.za/thisiszeev/linux-server-tools
|
|
|
|
## Usage:
|
|
##
|
|
## (as root)
|
|
## ./addswap.sh {size-in-gigaytes}
|
|
##
|
|
## (with sudo)
|
|
## sudo ./addswap.sh {size-in-gigaytes}
|
|
##
|
|
## {size-in-gigabytes} is optional. If you leave it out then the size will default
|
|
## to 4GB.
|
|
##
|
|
## The script will quickly create a SWAP file, activate it and add it to /etc/fstab
|
|
##
|
|
## It is important that you have addswap.sh and remswap.sh in the same folder and
|
|
## set the permissions to 700 and the owner and group to root. Do not try and run
|
|
## either script with out root or sudo privelages.
|
|
##
|
|
## A log file can be found at /var/log/swapmanagement.log
|
|
|
|
## Why did I make these two scripts?
|
|
##
|
|
## I use Debian as my daily driver (I've been using Linux in some for or the other
|
|
## as my daily driver since 2006). I also us Debian for all my servers. I also
|
|
## running a webhosting business (https://www.zaks.web.za) and in my 20+ years in
|
|
## the game, I have learned that... 1. VPS's do not come with SWAP, but sometimes
|
|
## you need a bit of SWAP to handle heavy load. 2. SWAP files can be added and
|
|
## removed on the fly (no reboot required), however a SWAP partition, not so simple.
|
|
##
|
|
## On my daily driver machine, I also create a lot of digital artwork. I recently
|
|
## found myself rendering a finished artfile that was 9.5GB (24000px by 13700px)
|
|
## in Gimp, and half way through I started running out of RAM and SWAP. So I
|
|
## quickly created the needed SWAP file, and Gimp was able to finish the render
|
|
## without crashing.
|
|
|
|
## Plans for the future:
|
|
##
|
|
## I am planning to make a configurable service file that can be run in the
|
|
## background to automatically create more SWAP files when your server/computer is
|
|
## under load, and remove excess SWAP files when the server/computer is no longer
|
|
## under load.
|
|
|
|
## Do you have a script idea?
|
|
|
|
## If you have an idea for a useful server script for selfhost/homelab environments,
|
|
## please start a chat with me on Reddit /u/thisiszeev and share your idea. I might
|
|
## just make it a reality, and give you credit in the relevant README.md file.
|
|
|
|
## Donations
|
|
##
|
|
## Please consider making me small donation. Even though my scripts are open source
|
|
## and free to use, I still need to eat. And the occasional bottle of wine also goes
|
|
## down well.
|
|
##
|
|
## $5 buys me a cup of coffee
|
|
## $10 buys me a nice burger
|
|
## $20 buys me a bottle of wine
|
|
## Anything above that will be awesome as well.
|
|
##
|
|
## You can send me a donation via Paypal https://www.paypal.com/paypalme/thisiszeev
|
|
##
|
|
## Drop me a message on Reddit if you do make a donation. u/thisiszeev
|
|
##
|
|
## Support is only offered freely to those who donate $20 or more.
|
|
##
|
|
## Your donation contributes to further development.
|
|
##
|
|
## If you need a custom script, contact me on Reddit for pricing.
|
|
|
|
function echolog {
|
|
echo "$1"
|
|
echo "$(date) > addswap > $1" >> /var/log/swapmanagement.log
|
|
|
|
if [[ $(wc -l < /var/log/swapmanagement.log) -gt 300 ]]; then
|
|
|
|
cat /var/log/swapmanagement.log | tail -n 250 > /var/log/swapmanagement.temp && mv /var/log/swapmanagement.temp /var/log/swapmanagement.log
|
|
|
|
fi
|
|
}
|
|
|
|
if [[ -z $1 ]] || [[ $1 = "" ]] || [[ ! $1 =~ ^[0-9]+$ ]] || [[ $1 -le 0 ]]; then
|
|
|
|
swapsize=4
|
|
echolog "Swap size not defined. Defaulting to 4GB."
|
|
|
|
else
|
|
|
|
swapsize=$1
|
|
echolog "Swap size manually defined as ${swapsize}GB."
|
|
|
|
fi
|
|
|
|
partitioncount=$(swapon --show | grep "partition" | wc -l)
|
|
filecount=$(swapon --show | grep "file" | wc -l)
|
|
totalcount=$((partitioncount+filecount))
|
|
|
|
echolog "You currently have $partitioncount active swap partitions and $filecount active swap files."
|
|
|
|
if [[ $totalcount -gt 23 ]]; then
|
|
|
|
echolog "You already have 24 or more active swap partitions and files."
|
|
exit 1
|
|
|
|
fi
|
|
|
|
swaplimit=$((24-$partitioncount))
|
|
array=($(df | grep "/$"))
|
|
freespace=$((${array[3]}/1048576))
|
|
|
|
if [[ $freespace -gt $swapsize ]]; then
|
|
|
|
checking=true
|
|
n=1
|
|
|
|
while [[ $checking == true ]]; do
|
|
|
|
if [[ $n -gt $swaplimit ]]; then
|
|
|
|
echolog "You already have 24 or more active swap partitions and files."
|
|
exit 1
|
|
|
|
fi
|
|
|
|
if [[ $n -lt 10 ]]; then
|
|
|
|
ext="00$n"
|
|
|
|
elif [[ $n -lt 25 ]] && [[ $n -gt 9 ]]; then
|
|
|
|
ext="0$n"
|
|
|
|
else
|
|
|
|
echolog "You already have 24 or more active swap partitions and files."
|
|
exit 1
|
|
|
|
fi
|
|
|
|
if [[ -f /swapfile.$ext ]]; then
|
|
|
|
if swapon --show=NAME | grep -qF "/swapfile.$ext"; then
|
|
|
|
echolog "/swapfile.$ext exists...[Active]"
|
|
|
|
else
|
|
|
|
echolog "/swapfile.$ext exists...[Inactive] > Removing"
|
|
bash remswap.sh "/swapfile.$ext"
|
|
|
|
fi
|
|
|
|
((n++))
|
|
|
|
else
|
|
|
|
echolog "Creating /swapfile.$ext..."
|
|
fallocate -l ${swapsize}G /swapfile.$ext
|
|
chmod 600 /swapfile.$ext
|
|
mkswap /swapfile.$ext
|
|
swapon /swapfile.$ext
|
|
echo "/swapfile.$ext none swap sw 0 0" >> /etc/fstab
|
|
checking=false
|
|
echolog "You now have $partitioncount active swap partitions and $filecount active swap files."
|
|
exit 0
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
else
|
|
|
|
echolog "Not enough harddrive space to make a ${swapsize}GB swap file."
|
|
exit 2
|
|
|
|
fi
|