6

I installed zram with sudo apt-get install zram-config. After that it started automatically and when I verified with cat /proc/swaps it's running properly

But when I used sudo echo 128M > /sys/block/zram0/disksize to change the zram size like in the document I always get bash: /sys/block/zram0/disksize: permission denied

Even when I turned it off with sudo swapoff /dev/zram0 then changed I still got permission denied

So how can I change the zram size? And is disksize the space it consumes on RAM or just the maximum zram disk size?

2
  • Your command is wrong, because it's your shell doing the redirect, not sudo echo. You need to do echo 128M > sudo tee /sys/block/zram0/disksize to have permission to gain permission to overwrite the file in /sys. – ketil Jan 11 '17 at 10:35
  • @ketil Yes Pilot6 said that before and I already tried that – phuclv Jan 11 '17 at 10:52
8

According to http://manpages.ubuntu.com/manpages/xenial/man8/zramctl.8.html, you can remove and recreate a zram swap like this:

# swapoff /dev/zram0
# zramctl --reset /dev/zram0
# zramctl --find --size 1024M
/dev/zram0
# mkswap /dev/zram0
# swapon /dev/zram0

To permanently change the size, you have to adapt the init script, where the swap files are created. Be aware, that this file may be overridden by future system updates.

To increase the size of the swapfile in Ubunutu 16.04 from 50% to 200% of your total memory size, change this line in /usr/bin/init-zram-swapping

mem=$(((totalmem / 2 / ${NRDEVICES}) * 1024))

to

mem=$(((totalmem * 2 / ${NRDEVICES}) * 1024))
1
  • indeed what's important is the mkswap /dev/zram0 before enabling swap – phuclv Jun 10 '17 at 16:06
4

I'm a Fedora 33 user, but I came across this answer when looking for how to configure my zram. This is my /usr/lib/systemd/zram-generator.conf:

# This config file enables a /dev/zram0 device with the default settings:
# — size — half of available RAM or 4GB, whichever is less
# — compression — most likely lzo-rle
#
# To disable, uninstall zram-generator-defaults or create empty
# /etc/systemd/zram-generator.conf file.
[zram0]
zram-fraction = 2
max-zram-size=none

This is persistent across reboots and seems to be the recommended way of controlling zram.

See https://fedoraproject.org/wiki/Changes/SwapOnZRAM, espeically man 5 zram-generator.conf

1

You need to change to "root" to do this.

sudo -i
# echo 128M > /sys/block/zram0/disksize
2
  • 3
    Or echo 128M | sudo tee /sys/block/zram0/disksize – Pilot6 Feb 7 '16 at 10:58
  • 1
    it still gives me "device or resource is busy" :( – phuclv Mar 26 '16 at 2:56
1

There is no file at /usr/bin/init-zram-swapping. It seems since Xenial the file is now located at /sbin/zram-config-start. You can see this by yourself looking at $ cat /etc/init/zram-config.conf.

description "Initializes zram swaping and /tmp"
author      "Adam Conrad <adconrad@canonical.com>"

start on runlevel [2345]

pre-start exec /sbin/zram-config-start

pre-stop exec /sbin/zram-config-stop

The file /sbin/zram-config-start is much more complex than before. I wonder what to do in order to increase the ram size?

1
  • Great! A question in an answer. Make sure to update the answer once you find out. – Konrad Gajewski Oct 23 '19 at 13:52
1

As an old-school technocrat, I prefer doing things at as low a level as possible. I therefore created a small script to enable or change the size of my zram swap file.

#!/bin/bash
# Based on https://github.com/ric96/zram
# ver: 30 dec 2018
# Installed in /usr/local/bin/my-zram.bash
#  And made executable " chmod a+x /usr/local/bin/my-zram.bash "
# I use an /etc/crontab entry of:
#  " @reboot root /usr/local/bin/my-zram.bash 256M ; # [ optionally use w/ "'size'M" for Mega ]
# Or for command-line: " sudo /usr/local/bin/my-zram.bash 256M "
# Note that you may want to change the '256M' parameter I am using.

logger -- $0 $$ zram $1 Start

## If $1 exists
test -n $1 && \
 {
  ZRAMSIZE=$1
  DOLONE=$1
 }
## And yes, invalid option input will create interesting, but apparently not hazardous results.

## If no $1, fallback and calculate a reasonable (to me) size.
test -z $1 && \
 {
  totalmem=`free | grep -e "^Mem:" | awk '{print $2}'`
  mem=$(( ($totalmem) * 1024 / 2 ))
  #echo $mem > /sys/block/zram0/disksize
  ZRAMSIZE=$mem
  DOLONE=NULL
  logger -- $0 $$ Using totalmem $totalmem \* 1024 /2 = $mem ZRAMSIZE=$ZRAMSIZE
 }

## Do we already have a /dev/zram0 ?? if so, swapoff and reset it.
test -b /dev/zram0 && swapoff -v /dev/zram0
test -b /dev/zram0 && echo 1 > /sys/block/zram0/reset

## If /dev/zram0 does NOT exist, but the 'zram' kernel module IS loaded, then remove it.
test -b /dev/zram0 || ( lsmod|grep -q zram && rmmod -v zram )

## (Re)Install the 'zram' kernel module. FYI: It doesn't hurt to 'reinstall'...
modprobe -v zram num_devices=1

## Build the zram swapfile.
echo $ZRAMSIZE > /sys/block/zram0/disksize
mkswap /dev/zram0
swapon -v -p 5 /dev/zram0

logger -- $0 $$ zram Done.  ZRAMSIZE=$ZRAMSIZE  \$1=$DOLONE

# The_End

Enjoy! It works for me. And you may want to review, understand, and edit to suit your situation. :-)

And be aware that if you run this script from cron, as written, it produces output which will (should) be mailed to you ( or root ).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.