1
0
Fork 1

Update swap.sh

This commit is contained in:
colin 2024-09-13 16:43:10 -04:00
parent 8301229173
commit 69140e1644
1 changed files with 49 additions and 29 deletions

32
swap.sh
View File

@ -1,7 +1,8 @@
#!/bin/bash
# Function to create swap file
create_swapfile() {
# Detect the main disk by finding the partition mounted on root ('/')
main_disk=$(df / | tail -1 | awk '{print $1}')
mount_point=$(df / | tail -1 | awk '{print $6}')
# Get available space on the partition in GB
@ -18,6 +19,13 @@ 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
@ -25,16 +33,28 @@ 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
# 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 swap summary
# Show current swap status
sudo swapon --show