152 lines
4.4 KiB
Bash
152 lines
4.4 KiB
Bash
#!/bin/bash
|
|
|
|
## remswap.sh
|
|
## version 1.0
|
|
## Quickly deactivate and remove 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)
|
|
## ./remswap.sh {/swapfile.0xx}
|
|
##
|
|
## (with sudo)
|
|
## sudo remswap.sh {/swapfile.0xx}
|
|
##
|
|
## {/swapfile.0xx} is optional. The 0xx extension is a range of numbers from 001 to
|
|
## 024. If you leave it out then the last file in the sequence will be removed.
|
|
##
|
|
## The script will quickly deactivate a SWAP file, removed it and remove it from
|
|
## /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) > remswap > $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 == "" ]]; then
|
|
|
|
swapfile=$(ls -1 /swapfile.* 2>/dev/null | tail -1)
|
|
|
|
if [[ -z "$swapfile" ]]; then
|
|
|
|
echolog "No swap files found to remove."
|
|
exit 1
|
|
|
|
fi
|
|
|
|
elif [[ $1 =~ ^/swapfile\.0(0[0-9]|1[0-9]|2[0-4])$ ]] && [[ -f $1 ]]; then
|
|
|
|
swapfile="$1"
|
|
|
|
else
|
|
|
|
echolog "Invalid swap file name given or swap file does not exist."
|
|
exit 2
|
|
|
|
fi
|
|
|
|
if swapon --show=NAME | grep -qF "$swapfile"; then
|
|
|
|
echolog "Turning off $swapfile..."
|
|
swapoff "$swapfile"
|
|
|
|
else
|
|
|
|
echolog "$swapfile is not active."
|
|
|
|
fi
|
|
|
|
if grep -Fq "$swapfile" /etc/fstab; then
|
|
|
|
echolog "Removing $swapfile entry from /etc/fstab..."
|
|
sed -i "\|$swapfile|d" /etc/fstab
|
|
|
|
else
|
|
|
|
echolog "$swapfile entry not found in /etc/fstab."
|
|
|
|
fi
|
|
|
|
echolog "Deleting $swapfile..."
|
|
rm -f "$swapfile"
|
|
|
|
if [[ $? == 0 ]]; then
|
|
|
|
echolog "$swapfile successfully removed."
|
|
partitioncount=$(swapon --show | grep "partition" | wc -l)
|
|
filecount=$(swapon --show | grep "file" | wc -l)
|
|
echolog "You now have $partitioncount active swap partitions and $filecount active swap files."
|
|
exit 0
|
|
|
|
else
|
|
|
|
echolog "$swapfile failed to be removed."
|
|
exit 3
|
|
|
|
fi
|