Skip to content

Instantly share code, notes, and snippets.

@Davenchy
Last active November 4, 2021 02:52
Show Gist options
  • Save Davenchy/99df231cfc0034d8fffabda0b7628cef to your computer and use it in GitHub Desktop.
Save Davenchy/99df231cfc0034d8fffabda0b7628cef to your computer and use it in GitHub Desktop.
My zram control script

ZRam Controller Script

Install

The script uses zramctl under the hood

make sure to remove swap partition from /etc/fstab

Script

  • to auto install script

    curl -s https://gist.githubusercontent.com/Davenchy/99df231cfc0034d8fffabda0b7628cef/raw/auto_install.sh | sudo bash

  • to auto uninstall script

    curl -s https://gist.githubusercontent.com/Davenchy/99df231cfc0034d8fffabda0b7628cef/raw/auto_remove.sh | sudo bash

Manual

  • add zram-controller.sh file at /usr/local/bin/ with permissions 0700 with user su

  • add zram.service file at /etc/systemd/system/ with permissions 0600 with user su

  • reload systemd sudo systemctl daemon-reload

  • enable the service and start it

    sudo systemctl enable zram.service

    sudo systemctl start zram.service

  • check zram devices zramctl

To uninstall

  • make sure to stop the script by calling

    sudo systemctl stop zram.service

    or

    sudo zram-controller.sh stop

  • then remove script files

    sudo rm /etc/systemd/system/zram.service

    sudo rm /usr/local/bin/zram-controller.sh

  • reload systemd

    sudo systemctl daemon-reload

#!/bin/bash
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
exit 1
fi
echo "installing zram controller script"
echo "Downloading zram controller script"
cd /usr/local/bin && curl -s -o zram-controller.sh https://gist.githubusercontent.com/Davenchy/99df231cfc0034d8fffabda0b7628cef/raw/zram-controller.sh
sudo chmod 0700 zram-controller.sh
echo "Install systemd service"
cd /etc/systemd/system && curl -s -o zram.service https://gist.githubusercontent.com/Davenchy/99df231cfc0034d8fffabda0b7628cef/raw/zram.service
sudo chmod 0600 zram.service
echo "Enabling service"
sudo systemctl daemon-reload && sudo systemctl enable zram.service && sudo systemctl start zram.service
echo "Script Complete!"
#!/bin/bash
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
exit 1
fi
echo "removing zram controller script"
sudo systemctl stop zram.service && sudo systemctl disable zram.service
sudo zram-controller.sh stop
sudo rm /etc/systemd/system/zram.service
sudo rm /usr/local/bin/zram-controller.sh
sudo systemctl daemon-reload
echo "script complete!"
#!/bin/bash
function init_zram {
# load zram module
echo "load zram module"
modprobe zram
# define zram device
zram_count=2
echo "define ${1-$zram_count} device/s"
for ((n=0;n<${1-$zram_count};n++)); do zramctl -f -a lz4 -s 4G -t 4; done
# mkswap for each device
echo "mkswap for each zram"
for device in $(zramctl -n -o name); do mkswap $device; done
}
function start_zram {
echo "starting zram devices"
for device in $(zramctl -n -o name); do swapon $device; done
zramctl
}
function stop_zram {
echo "stopping running zram devices"
for device in $(zramctl -n -o name); do swapoff $device; done
# reset all zram devices
echo "reset all zram devices"
for device in $(zramctl -n -o name); do zramctl -r $device; done
}
case "$1" in
init) init_zram $2;;
start) start_zram;;
stop) stop_zram;;
*) echo "usage: init [<zram_devices_count>]|start|stop"
exit 1
;;
esac
[Unit]
Description=ZRam controller script
After=multi-user.target
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStartPre=/usr/local/bin/zram-controller.sh init
ExecStart=/usr/local/bin/zram-controller.sh start
ExecStop=/usr/local/bin/zram-controller.sh stop
[Install]
WantedBy=multi-user.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment