forked from colin/disk-space-report
Update swap.sh
This commit is contained in:
parent
8301229173
commit
69140e1644
78
swap.sh
78
swap.sh
|
@ -1,40 +1,60 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
# Detect the main disk by finding the partition mounted on root ('/')
|
# Function to create swap file
|
||||||
main_disk=$(df / | tail -1 | awk '{print $1}')
|
create_swapfile() {
|
||||||
mount_point=$(df / | tail -1 | awk '{print $6}')
|
# Detect the main disk by finding the partition mounted on root ('/')
|
||||||
|
mount_point=$(df / | tail -1 | awk '{print $6}')
|
||||||
|
|
||||||
# Get available space on the partition in GB
|
# Get available space on the partition in GB
|
||||||
available_space=$(df -BG $mount_point | tail -1 | awk '{print $4}' | sed 's/G//')
|
available_space=$(df -BG $mount_point | tail -1 | awk '{print $4}' | sed 's/G//')
|
||||||
|
|
||||||
# Calculate 5% of the available space, and ensure it's within the limits (max 30GB)
|
# Calculate 5% of the available space, and ensure it's within the limits (max 30GB)
|
||||||
swap_size=$((available_space * 5 / 100))
|
swap_size=$((available_space * 5 / 100))
|
||||||
if [ $swap_size -gt 30 ]; then
|
if [ $swap_size -gt 30 ]; then
|
||||||
swap_size=30
|
swap_size=30
|
||||||
elif [ $swap_size -lt 1 ]; then
|
elif [ $swap_size -lt 1 ]; then
|
||||||
echo "Not enough disk space to create a swap file."
|
echo "Not enough disk space to create a swap file."
|
||||||
exit 1
|
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
|
fi
|
||||||
|
|
||||||
echo "Creating a ${swap_size}GB swap file at /swapfile..."
|
# Main script execution
|
||||||
|
create_swapfile
|
||||||
|
|
||||||
# Create the swap file
|
if [ "$persist" = true ]; then
|
||||||
sudo fallocate -l ${swap_size}G /swapfile
|
persist_swapfile
|
||||||
sudo chmod 600 /swapfile
|
|
||||||
sudo mkswap /swapfile
|
|
||||||
sudo swapon /swapfile
|
|
||||||
|
|
||||||
echo "Swap file of ${swap_size}GB created and activated."
|
|
||||||
|
|
||||||
# Ask if the user wants to persist the swap after reboot
|
|
||||||
read -p "Do you want to persist the swap file after reboot? (y/n): " persist_swap
|
|
||||||
|
|
||||||
if [[ "$persist_swap" == "y" || "$persist_swap" == "Y" ]]; then
|
|
||||||
echo "/swapfile none swap sw 0 0" | sudo tee -a /etc/fstab
|
|
||||||
echo "Swap file added to /etc/fstab."
|
|
||||||
else
|
else
|
||||||
echo "Swap file will not persist after reboot."
|
echo "Swap file will not persist after reboot."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Show swap summary
|
# Show current swap status
|
||||||
sudo swapon --show
|
sudo swapon --show
|
||||||
|
|
Loading…
Reference in New Issue