Created
December 22, 2025 20:56
-
-
Save cnmoro/f7b8935736150dbac4d754051dc07e4b to your computer and use it in GitHub Desktop.
Activate and Configure ZSWAP Automatically
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # zswap Setup Script | |
| # Example: curl -sL https://gist.githubusercontent.com/cnmoro/f7b8935736150dbac4d754051dc07e4b/raw/6c3489e2d612756acddb5152a136484a816b6528/activate_zswap.sh | sudo bash -s 4 | |
| set -e | |
| # Check if running as root | |
| if [ "$EUID" -ne 0 ]; then | |
| echo "Error: This script must be run as root (use sudo)" | |
| exit 1 | |
| fi | |
| # Check if size parameter is provided | |
| if [ -z "$1" ]; then | |
| echo "Error: Please provide swap size in GB as parameter" | |
| echo "Usage: $0 <size_in_gb>" | |
| echo "Example: $0 4" | |
| exit 1 | |
| fi | |
| SWAP_SIZE="$1" | |
| echo "===================================" | |
| echo "zswap Setup Script" | |
| echo "===================================" | |
| echo "Swap Size: ${SWAP_SIZE}GB" | |
| echo "===================================" | |
| echo "" | |
| # Step 1: Create or recreate swap file | |
| echo "Step 1: Setting up swap file..." | |
| # Check if swap file exists | |
| if [ -f /swapfile ]; then | |
| echo "Existing /swapfile found. Removing it..." | |
| swapoff /swapfile 2>/dev/null || true | |
| rm -f /swapfile | |
| echo "Old swap file removed." | |
| fi | |
| # Check if swap is already active | |
| if swapon --show | grep -q '/swapfile'; then | |
| echo "Deactivating existing swap..." | |
| swapoff /swapfile | |
| fi | |
| # Create new swap file | |
| echo "Creating ${SWAP_SIZE}GB swap file..." | |
| fallocate -l ${SWAP_SIZE}G /swapfile | |
| chmod 600 /swapfile | |
| mkswap /swapfile | |
| swapon /swapfile | |
| # Add to fstab if not already present | |
| if ! grep -q '/swapfile' /etc/fstab; then | |
| echo '/swapfile none swap sw 0 0' | tee -a /etc/fstab | |
| echo "Swap file added to /etc/fstab" | |
| else | |
| echo "Swap file already in /etc/fstab" | |
| fi | |
| echo "Swap file created and activated." | |
| echo "" | |
| # Step 2: Configure GRUB | |
| echo "Step 2: Configuring GRUB..." | |
| # Backup grub config | |
| cp /etc/default/grub /etc/default/grub.backup | |
| # Update GRUB_CMDLINE_LINUX_DEFAULT | |
| if grep -q "^GRUB_CMDLINE_LINUX_DEFAULT=" /etc/default/grub; then | |
| # Remove any existing zswap parameters | |
| sed -i 's/ zswap\.[^ ]*//g' /etc/default/grub | |
| # Add zswap parameters | |
| sed -i 's/^\(GRUB_CMDLINE_LINUX_DEFAULT="[^"]*\)"/\1 zswap.enabled=1 zswap.compressor=lz4 zswap.max_pool_percent=50 zswap.zpool=z3fold"/' /etc/default/grub | |
| echo "GRUB configuration updated." | |
| else | |
| echo "Warning: GRUB_CMDLINE_LINUX_DEFAULT not found in /etc/default/grub" | |
| echo "You may need to add it manually." | |
| fi | |
| # Update GRUB | |
| echo "Updating GRUB..." | |
| update-grub | |
| echo "" | |
| # Step 3: Configure initramfs modules | |
| echo "Step 3: Configuring initramfs modules..." | |
| # Add modules if not already present | |
| for module in lz4 lz4_compress z3fold; do | |
| if ! grep -q "^${module}$" /etc/initramfs-tools/modules 2>/dev/null; then | |
| echo "$module" >> /etc/initramfs-tools/modules | |
| echo "Added $module to initramfs modules" | |
| else | |
| echo "$module already in initramfs modules" | |
| fi | |
| done | |
| # Update initramfs | |
| echo "Updating initramfs..." | |
| update-initramfs -u | |
| echo "" | |
| echo "===================================" | |
| echo "zswap setup completed successfully!" | |
| echo "===================================" | |
| echo "" | |
| echo "Please reboot your system for changes to take effect:" | |
| echo " sudo reboot" | |
| echo "" | |
| echo "After reboot, verify zswap is active with:" | |
| echo " cat /sys/module/zswap/parameters/enabled" | |
| echo " (Should display 'Y')" | |
| echo "" | |
| echo "Check swap status with:" | |
| echo " free -h" | |
| echo " swapon --show" | |
| echo "===================================" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment