From 2f712d05dec8d4fd812821a6968ac009279d6f6a Mon Sep 17 00:00:00 2001 From: colin Date: Fri, 13 Sep 2024 17:03:51 -0400 Subject: [PATCH] Update swap.sh --- swap.sh | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/swap.sh b/swap.sh index 68d81f7..9afeae4 100644 --- a/swap.sh +++ b/swap.sh @@ -2,17 +2,25 @@ # Function to create swap file create_swapfile() { - # Detect the main disk by finding the partition mounted on root ('/') + # Detect the partition mounted on root ('/') mount_point=$(df / | tail -1 | awk '{print $6}') - # Get available space on the partition in MB + # 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, and ensure it's within the limits (max 30GB) - swap_size=$((available_space * 5 / 100 / 1024)) # Convert MB to GB + # 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 - elif [ $swap_size -lt 1 ]; then + 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