Created
December 22, 2025 20:49
-
-
Save cnmoro/32a898436388e8245fbb67cd7b6bb1e6 to your computer and use it in GitHub Desktop.
Activate and Configure ZRAM 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 | |
| # zRAM Setup Script | |
| # Example: curl -sL https://gist.githubusercontent.com/cnmoro/32a898436388e8245fbb67cd7b6bb1e6/raw/98de143fdfe1a9c2d8649a5b5c572835c32b45e1/activate_zram.sh | sudo bash -s 4096M | |
| 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 zRAM size as parameter" | |
| echo "Usage: $0 <size>" | |
| echo "Example: $0 512M" | |
| exit 1 | |
| fi | |
| ZRAM_SIZE="$1" | |
| echo "===================================" | |
| echo "zRAM Setup Script" | |
| echo "===================================" | |
| echo "zRAM Size: $ZRAM_SIZE" | |
| echo "===================================" | |
| echo "" | |
| # Create zram.conf in modules-load.d | |
| echo "Creating /etc/modules-load.d/zram.conf..." | |
| echo "zram" > /etc/modules-load.d/zram.conf | |
| # Create zram.conf in modprobe.d | |
| echo "Creating /etc/modprobe.d/zram.conf..." | |
| echo "options zram num_devices=1" > /etc/modprobe.d/zram.conf | |
| # Create udev rule | |
| echo "Creating /etc/udev/rules.d/99-zram.rules..." | |
| echo "KERNEL==\"zram0\", ATTR{disksize}=\"$ZRAM_SIZE\",TAG+=\"systemd\"" > /etc/udev/rules.d/99-zram.rules | |
| # Disable traditional swap in fstab | |
| echo "Disabling traditional swap in /etc/fstab..." | |
| if grep -q "^/swap.img" /etc/fstab; then | |
| sed -i 's|^/swap.img|#/swap.img|' /etc/fstab | |
| echo "Traditional swap disabled in fstab" | |
| elif grep -q "^[^#].*swap" /etc/fstab; then | |
| sed -i '/^[^#].*swap/s/^/#/' /etc/fstab | |
| echo "Swap entries commented out in fstab" | |
| else | |
| echo "No active swap found in fstab" | |
| fi | |
| # Create systemd unit file | |
| echo "Creating /etc/systemd/system/zram.service..." | |
| cat > /etc/systemd/system/zram.service << 'EOF' | |
| [Unit] | |
| Description=Swap with zram | |
| After=multi-user.target | |
| [Service] | |
| Type=oneshot | |
| RemainAfterExit=true | |
| ExecStartPre=/sbin/mkswap /dev/zram0 | |
| ExecStart=/sbin/swapon /dev/zram0 | |
| ExecStop=/sbin/swapoff /dev/zram0 | |
| [Install] | |
| WantedBy=multi-user.target | |
| EOF | |
| # Enable the service | |
| echo "Enabling zram service..." | |
| systemctl enable zram.service | |
| echo "" | |
| echo "===================================" | |
| echo "zRAM setup completed successfully!" | |
| echo "===================================" | |
| echo "" | |
| echo "Please reboot your system for changes to take effect:" | |
| echo " sudo reboot" | |
| echo "" | |
| echo "After reboot, verify zRAM is active with:" | |
| echo " swapon --show" | |
| echo " cat /proc/swaps" | |
| echo "===================================" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment