linux-server-tools/swap-management/README.md

142 lines
4.7 KiB
Markdown
Raw Normal View History

# SWAP Management
Two Bash scripts...
- one to add more SWAP without rebooting.
- one to remove unneeded SWAP without rebooting.
## addswap.sh
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
## autoswap.sh
Usage:
1. Copy the file autoswap.conf to /etc/autoswap/autoswap.conf
2. Set owner and group to root and edit the contents of the file to fit your use case.
3. Copy the file autoswap.service to /etc/systemd/system/autoswap.service
4. Set owner and group to root
5. Copy autoswap.sh, addswap.sh and remswap.sh to the / folder.
6. Set owner and group to root and make executable for root only (744)
### as root
```
systemctl daemon-reload
systemctl start autoswap.service
systemctl enable autoswap.service
```
### with sudo
```
sudo systemctl daemon-reload
sudo systemctl start autoswap.service
sudo systemctl enable autoswap.service
```
The script will monitor your available memory and SWAP resources and use addswap.sh and remswap.sh to add and remove SWAP fles depending on your requirements and settings in /etc/autoswap/autoswap.conf.
If you make changes to /etc/autoswap/autoswap.conf then you need to restart the autoswap.service using:
### as root
```
systemctl restart autoswap.service
```
### with sudo
```
sudo systemctl restart autoswap.service
```
## remswap.sh
Usage:
### as root
```
./remswap.sh {/swapfile.0xx}
```
### with sudo
```
2024-11-14 01:07:27 +02:00
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
## Notes:
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.
If you are using the autoswap service then all three Bash scripts must be in the the / (root) folder.
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. (completed 2024/Nov/16)
I am next planning on making a binary version of this tool that can be installed using a package manager.
## 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.
# Get notified when there are updates to this repo...
https://nca.zaks.web.za/index.php/apps/forms/s/8fDpCRzkKRMaBRakNtfyigcQ
Your email address will not be used for any other purpose than to be notifed, not more than once a week, of updates to this or any of the other repos I have on this server. You can also ask me to remove you from the list at anytime, and I will comply.