Skip to content

Instantly share code, notes, and snippets.

@MohamedElashri
Created November 17, 2024 08:44
Show Gist options
  • Save MohamedElashri/6b03d8305ad72bedc391e9a1a55a5d27 to your computer and use it in GitHub Desktop.
Save MohamedElashri/6b03d8305ad72bedc391e9a1a55a5d27 to your computer and use it in GitHub Desktop.
Script to make creating swap easier and quicker
#!/bin/bash
# Color Codes
RED='\033[0;31m' # Red for errors
GREEN='\033[0;32m' # Green for info
YELLOW='\033[1;33m' # Yellow for warnings
NC='\033[0m' # No Color
# Functions for colored output
info() {
echo -e "${GREEN}[INFO]${NC} $1"
}
warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Function to display help
show_help() {
echo "Usage: $0 [-h] [-s SWAPPINESS] [-p CACHE_PRESSURE] SIZE"
echo ""
echo "Create a swap file with the specified size and optionally set kernel parameters for swappiness and cache pressure."
echo ""
echo "Options:"
echo " -h Show this help message and exit"
echo " -s SWAPPINESS Set the vm.swappiness kernel parameter (0-100)"
echo " -p CACHE_PRESSURE Set the vm.vfs_cache_pressure kernel parameter (e.g., 50, 100)"
echo ""
echo "Arguments:"
echo " SIZE Size of the swap file (e.g., 1G, 512M, etc.)"
echo ""
echo "Examples:"
echo " $0 -s 60 -p 50 2G # Creates a 2GB swap file and sets swappiness to 60 and cache pressure to 50"
echo " $0 1G # Creates a 1GB swap file without changing kernel parameters"
}
# Function to create swap
create_swap() {
local size=$1
local swapfile="/swapfile"
# Check if swapfile already exists
if [ -f "$swapfile" ]; then
warning "Swap file $swapfile already exists."
read -r -p "Do you want to overwrite it? [y/N]: " choice
case "$choice" in
y|Y )
info "Disabling existing swap..."
sudo swapoff "$swapfile"
info "Removing existing swap file..."
sudo rm "$swapfile"
;;
* )
info "Exiting without making changes."
exit 0
;;
esac
fi
info "Creating a swap file of size $size at $swapfile..."
# Create the swap file
sudo fallocate -l "$size" "$swapfile"
if [ $? -ne 0 ]; then
error "Failed to allocate swap file. Ensure you have enough disk space and permissions."
exit 1
fi
# Set correct permissions
sudo chmod 600 "$swapfile"
# Set up the swap file
sudo mkswap "$swapfile"
# Enable the swap file
sudo swapon "$swapfile"
# Add to /etc/fstab for persistence
if ! grep -q "^/swapfile" /etc/fstab; then
info "Adding swap file to /etc/fstab..."
echo "/swapfile none swap sw 0 0" | sudo tee -a /etc/fstab > /dev/null
fi
# Verify swap is active
info "Swap file created and activated."
info "Swap details:"
sudo swapon --show
}
# Function to set swappiness
set_swappiness() {
local swappiness=$1
info "Setting vm.swappiness to $swappiness..."
sudo sysctl vm.swappiness="$swappiness"
if [ $? -ne 0 ]; then
error "Failed to set vm.swappiness. Ensure you have sufficient permissions."
exit 1
fi
# Make it persistent
sudo sed -i '/vm.swappiness/d' /etc/sysctl.conf
echo "vm.swappiness=$swappiness" | sudo tee -a /etc/sysctl.conf > /dev/null
info "vm.swappiness set to $swappiness."
}
# Function to set vfs_cache_pressure
set_cache_pressure() {
local cache_pressure=$1
info "Setting vm.vfs_cache_pressure to $cache_pressure..."
sudo sysctl vm.vfs_cache_pressure="$cache_pressure"
if [ $? -ne 0 ]; then
error "Failed to set vm.vfs_cache_pressure. Ensure you have sufficient permissions."
exit 1
fi
# Make it persistent
sudo sed -i '/vm.vfs_cache_pressure/d' /etc/sysctl.conf
echo "vm.vfs_cache_pressure=$cache_pressure" | sudo tee -a /etc/sysctl.conf > /dev/null
info "vm.vfs_cache_pressure set to $cache_pressure."
}
# Parse command-line arguments
swappiness=""
cache_pressure=""
while getopts "hs:p:" opt; do
case "$opt" in
h)
show_help
exit 0
;;
s)
swappiness="$OPTARG"
;;
p)
cache_pressure="$OPTARG"
;;
*)
show_help
exit 1
;;
esac
done
# Shift positional arguments
shift $((OPTIND-1))
# Check if size is provided
if [ -z "$1" ]; then
error "SIZE argument is required."
show_help
exit 1
fi
# Validate swappiness if provided
if [ -n "$swappiness" ]; then
if ! [[ "$swappiness" =~ ^[0-9]+$ ]] || [ "$swappiness" -lt 0 ] || [ "$swappiness" -gt 100 ]; then
error "SWAPPINESS must be an integer between 0 and 100."
show_help
exit 1
fi
fi
# Validate cache pressure if provided
if [ -n "$cache_pressure" ]; then
if ! [[ "$cache_pressure" =~ ^[0-9]+$ ]]; then
error "CACHE_PRESSURE must be a positive integer."
show_help
exit 1
fi
fi
# Create swap with specified size
create_swap "$1"
# Set swappiness if specified
if [ -n "$swappiness" ]; then
set_swappiness "$swappiness"
fi
# Set vfs_cache_pressure if specified
if [ -n "$cache_pressure" ]; then
set_cache_pressure "$cache_pressure"
fi

What is this?

This is a simple Bash script designed to help easily create and manage swap files on a Linux system. It also allows easily configure kernel parameters vm.swappiness and vm.vfs_cache_pressure.


How to Use

  1. Basic Usage
    Create a swap file with the specified size:

    ./create_swap.sh SIZE

    Example:

    ./create_swap.sh 2G
  2. Set vm.swappiness
    Adjust the kernel parameter for how aggressively the system uses swap:

    ./create_swap.sh -s SWAPPINESS SIZE

    Example:

    ./create_swap.sh -s 60 2G
  3. Set vm.vfs_cache_pressure
    Adjust the kernel parameter for how the system prioritizes cache over swap:

    ./create_swap.sh -p CACHE_PRESSURE SIZE

    Example:

    ./create_swap.sh -p 50 2G
  4. Set Both Parameters
    Configure both vm.swappiness and vm.vfs_cache_pressure:

    ./create_swap.sh -s SWAPPINESS -p CACHE_PRESSURE SIZE

    Example:

    ./create_swap.sh -s 60 -p 50 2G
  5. Check for Existing Swap
    If a swap file already exists, the script will prompt you to overwrite it.

  6. Display Help
    To see usage instructions:

    ./create_swap.sh -h

Important Notes

  • Permissions: The script requires sudo privileges to create swap files and modify kernel parameters.
  • Persistence: Swap settings and kernel parameters are automatically made persistent across reboots.
  • Validation: After running the script, verify the swap and kernel settings using:
    swapon --show
    sysctl vm.swappiness
    sysctl vm.vfs_cache_pressure
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment