Zram on Centos 7 / Nethserver 7

Seen @thorsten script and rpodgorny zramswap script on AUR, I decided to make a python script which allows you to customize ZRAM parameters, allowing you to create a service that fit your needs.

First lets start with requirements, I decided to do the script using python 3, so first thing first:

  1. Install python3, pip, and virtualenv:
    yum install python34 python34-devel python34-virtualenv python34-pip

  2. Create a virtual enviroment, I decided to set it on /opt/zram:
    virtualenv-3 /opt/zram/env/

  3. Activate virtual enviroment and install sh and psutil using pip
    source /opt/zram/env/bin/activate
    pip install psutil sh
    deactivate

  4. Disable partition swap:
    Run:
    swapoff -a
    Comment the swap line on /etc/fstab

  5. Create folder and script files:
    mkdir /opt/zram/src/
    Using an editor create zramctrl.py and procsmem.py inside /opt/zram/src/. Please notice that procsmem.py is mostly a copy from giampaolo and is used to know which process is been swapped. You can check if the script works by running this:
    /opt/zram/env/bin/python /opt/zram/src/zramctrl.py start -A lzo
    This will create a ZRAM swap using all CPU cores and 20% of total RAM.
    To see how to use the script to adapt configuration to your needs run:
    /opt/zram/env/bin/python /opt/zram/src/zramctrl.py -h

  6. Create, install, load, enable and start a service:
    Using an editor create zramswap.service inside /opt/zram/src/ and install the service by running:
    install -Dm644 /opt/zram/src/zramswap.service /lib/systemd/system/zramswap.service
    Load the service by running:
    systemctl daemon-reload
    Enable the service by running:
    systemctl enable zramswap.service
    Start the service by running:
    systemctl start zramswap.service

That’s it, you now have a ZRAM swap online, here is some stuff you can do:
For those who use Proxmox ballooning, it’s advisable to set ZRAM to the minimum RAM assigned, to accomplish that, change line 8 from this:

ExecStart=/opt/zram/env/bin/python /opt/zram/src/zramctrl.py start -A lzo

To this:

ExecStart=/opt/zram/env/bin/python /opt/zram/src/zramctrl.py start -A lzo -P 40 -M 2147483648

Where 2147483648 = 2 *1024^3 = 2G, so this new setup would create a ZRAM of 819M (40% of 2G). Any question or suggestions are welcomed.

5 Likes