Compare commits
14 Commits
Author | SHA1 | Date |
---|---|---|
|
c98bd82999 | |
|
821249b0e0 | |
|
2f712d05de | |
|
95c88f7715 | |
|
34f3e59243 | |
|
69140e1644 | |
|
8301229173 | |
|
115a538790 | |
|
a08f30fa56 | |
|
fa251952e2 | |
|
3b9c70433a | |
|
a1790d0cb4 | |
|
a62f9d450f | |
|
74fe320361 |
|
@ -0,0 +1,44 @@
|
|||
# Swap File Creation Script
|
||||
|
||||
This script automatically creates a swap file on your system, allowing you to increase the available virtual memory. It calculates the optimal swap file size based on the available disk space, ensuring that the swap file does not exceed 30GB or 5% of the free disk space, whichever is smaller.
|
||||
|
||||
## Features
|
||||
|
||||
- Detects the main disk partition used by the system.
|
||||
- Calculates the optimal swap file size (up to 30GB or 5% of the free space).
|
||||
- Creates and activates a swap file at `/swapfile`.
|
||||
- Optionally persists the swap file across reboots using the `--persist` flag.
|
||||
- Displays the status of the created swap file.
|
||||
|
||||
## Usage
|
||||
|
||||
To use this script, you can execute it directly from the URL without downloading it.
|
||||
|
||||
### Execute Directly from URL
|
||||
|
||||
Run the script directly from the URL using `curl` and piping to `bash`:
|
||||
|
||||
```bash
|
||||
curl -sSL https://git.nixc.us/colin/swap-increase/raw/branch/main/swap.sh | bash
|
||||
```
|
||||
|
||||
### Persist the Swap File Across Reboots
|
||||
|
||||
To ensure the swap file is persisted across reboots, run the script with the `--persist` flag:
|
||||
|
||||
```bash
|
||||
curl -sSL https://git.nixc.us/colin/swap-increase/raw/branch/main/swap.sh | bash -s -- --persist
|
||||
```
|
||||
|
||||
## Output
|
||||
|
||||
The script will provide the following output:
|
||||
|
||||
- The calculated size of the swap file.
|
||||
- Confirmation of swap file creation and activation.
|
||||
- Whether the swap file will persist after reboot (based on the `--persist` flag).
|
||||
- Information on the current swap status using `swapon --show`.
|
||||
|
||||
## Note
|
||||
|
||||
This script requires `sudo` privileges to create and activate the swap file, as well as to modify the `/etc/fstab` file for persistence across reboots when using the `--persist` flag.
|
|
@ -1,68 +0,0 @@
|
|||
#!/bin/bash
|
||||
|
||||
# Function to estimate the size of the apt cache
|
||||
estimate_apt_cache_size() {
|
||||
echo "Estimating the size of the apt cache:"
|
||||
sudo du -sh /var/cache/apt
|
||||
echo
|
||||
}
|
||||
|
||||
# Function to estimate the size of old installed packages
|
||||
estimate_old_packages_size() {
|
||||
echo "Estimating the size of old installed packages:"
|
||||
sudo du -sh /var/lib/apt/lists /var/lib/apt/lists/partial
|
||||
echo
|
||||
}
|
||||
|
||||
# Function to estimate the size of journal logs
|
||||
estimate_journal_size() {
|
||||
echo "Estimating the size of journal logs:"
|
||||
sudo journalctl --disk-usage
|
||||
echo
|
||||
}
|
||||
|
||||
# Function to estimate the size of temporary files
|
||||
estimate_tmp_size() {
|
||||
echo "Estimating the size of temporary files:"
|
||||
sudo du -sh /tmp /var/tmp
|
||||
echo
|
||||
}
|
||||
|
||||
# Function to estimate the size of unused Docker volumes
|
||||
estimate_docker_volumes_size() {
|
||||
echo "Estimating the size of unused Docker volumes:"
|
||||
docker volume ls -qf dangling=true | xargs -I {} docker inspect {} -f '{{ .Name }}: {{ .Size }}' | awk '{ sum += $3 } END { print sum/1024/1024 " MB" }'
|
||||
echo
|
||||
}
|
||||
|
||||
# Function to list directories consuming more than a specified size
|
||||
list_large_directories() {
|
||||
local directory=$1
|
||||
local size_limit=$2
|
||||
echo "Directories in $directory consuming more than $size_limit:"
|
||||
sudo du -ah $directory 2>/dev/null | awk -v limit=$size_limit '{
|
||||
size=$1; unit=substr(size, length(size));
|
||||
size_val=substr(size, 1, length(size)-1);
|
||||
if ((unit=="G" && size_val+0 > limit) || (unit=="T" && size_val*1024 > limit)) {
|
||||
print
|
||||
}
|
||||
}'
|
||||
echo
|
||||
}
|
||||
|
||||
# Estimate storage savings
|
||||
echo "Estimating potential storage savings..."
|
||||
estimate_apt_cache_size
|
||||
estimate_old_packages_size
|
||||
estimate_journal_size
|
||||
estimate_tmp_size
|
||||
estimate_docker_volumes_size
|
||||
|
||||
# List large directories
|
||||
echo "Listing directories consuming more than 5GB and 10GB:"
|
||||
list_large_directories / 5
|
||||
list_large_directories /home 5
|
||||
list_large_directories / 10
|
||||
list_large_directories /home 10
|
||||
|
||||
echo "Storage savings estimation and large directory listing completed."
|
|
@ -0,0 +1,68 @@
|
|||
#!/bin/bash
|
||||
|
||||
# Function to create swap file
|
||||
create_swapfile() {
|
||||
# Detect the partition mounted on root ('/')
|
||||
mount_point=$(df / | tail -1 | awk '{print $6}')
|
||||
|
||||
# Get available space on the root partition in MB
|
||||
available_space=$(df -BM $mount_point | tail -1 | awk '{print $4}' | sed 's/M//')
|
||||
|
||||
# Calculate 5% of the available space in MB
|
||||
swap_size=$((available_space * 5 / 100)) # 5% of available space in MB
|
||||
|
||||
# Convert swap size from MB to GB
|
||||
swap_size=$((swap_size / 1024)) # Convert MB to GB
|
||||
|
||||
# Set upper limit of 30GB for the swap file
|
||||
if [ $swap_size -gt 30 ]; then
|
||||
swap_size=30
|
||||
fi
|
||||
|
||||
# Abort if the calculated swap size is less than 1GB
|
||||
if [ $swap_size -lt 1 ]; then
|
||||
echo "Not enough disk space to create a swap file."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "Creating a ${swap_size}GB swap file at /swapfile..."
|
||||
|
||||
# Remove existing swapfile if it exists
|
||||
if [ -f /swapfile ]; then
|
||||
sudo swapoff /swapfile
|
||||
sudo rm /swapfile
|
||||
echo "Existing swapfile removed."
|
||||
fi
|
||||
|
||||
# Create the swap file
|
||||
sudo fallocate -l ${swap_size}G /swapfile
|
||||
sudo chmod 600 /swapfile
|
||||
sudo mkswap /swapfile
|
||||
sudo swapon /swapfile
|
||||
|
||||
echo "Swap file of ${swap_size}GB created and activated."
|
||||
}
|
||||
|
||||
# Function to persist the swap file across reboots
|
||||
persist_swapfile() {
|
||||
echo "/swapfile none swap sw 0 0" | sudo tee -a /etc/fstab
|
||||
echo "Swap file added to /etc/fstab."
|
||||
}
|
||||
|
||||
# Check if --persist flag is passed
|
||||
persist=false
|
||||
if [ "$1" == "--persist" ]; then
|
||||
persist=true
|
||||
fi
|
||||
|
||||
# Main script execution
|
||||
create_swapfile
|
||||
|
||||
if [ "$persist" = true ]; then
|
||||
persist_swapfile
|
||||
else
|
||||
echo "Swap file will not persist after reboot."
|
||||
fi
|
||||
|
||||
# Show current swap status
|
||||
sudo swapon --show
|
Loading…
Reference in New Issue