4

Ubuntu 18, trying to make zram to use lz4 compression.

Tried modifying /usr/bin/init-zram-swapping file

#!/bin/sh
...........
...........
for i in $(seq ${NRDEVICES}); do
  DEVNUMBER=$((i - 1))
  echo $mem > /sys/block/zram${DEVNUMBER}/disksize
  echo lz4 > /sys/block/zram${DEVNUMBER}/comp_algorithm # <= added this line
  mkswap /dev/zram${DEVNUMBER}
  swapon -p 5 /dev/zram${DEVNUMBER}
done

But, after rebootzramctl still shows I'm using lzo algortihm

NAME       ALGORITHM DISKSIZE  DATA  COMPR TOTAL STREAMS MOUNTPOINT
/dev/zram3 lzo           466M  2.9M 614.2K    1M       4 [SWAP]
/dev/zram2 lzo           466M  2.9M 631.2K    1M       4 [SWAP]
/dev/zram1 lzo           466M  2.9M 536.1K 1020K       4 [SWAP]
/dev/zram0 lzo           466M    3M   610K    1M       4 [SWAP]

I also tried adding adding it inrc.local, but it throws device already in use warning, because as far as I know, the change needs to be done before mkswap and swapon runs.

muru
  • 193,181
  • 53
  • 473
  • 722
Sirajus Salekin
  • 1,677
  • 12
  • 28

1 Answers1

7

This works:

# initialize the devices
for i in $(seq ${NRDEVICES}); do
  DEVNUMBER=$((i - 1))
  echo lz4 > /sys/block/zram${DEVNUMBER}/comp_algorithm
  echo $mem > /sys/block/zram${DEVNUMBER}/disksize
  mkswap /dev/zram${DEVNUMBER}
  swapon -p 100 /dev/zram${DEVNUMBER}
done

Note: it seems you need to define the compression algorithm before zram size. Set swapon -p to priority level 100 to use zram actively as it improves performance.

matigo
  • 20,403
  • 7
  • 43
  • 70
Modestas
  • 108
  • 1
  • 6
  • 2
    Yes, it seems you need to define the compression algorithm before zram size. Set swapon -p to 100 swapiness to use zram actively as it improves performance. – Modestas Oct 10 '18 at 19:30
  • thanks, i will check after going home. Too excited if this works :D – Sirajus Salekin Oct 11 '18 at 03:43
  • 2
    IMHO I don't understand why you use `swapon -p 100` here, rather than `sysctl vm.swappiness=100`? – Tomofumi Mar 11 '20 at 06:57
  • Tomofumi I don't know. Please tell if you find out. – Modestas Mar 21 '20 at 21:03
  • swapon -p 100 indicates the priority amongst swap partitions. You may have for example, a priority 10 zram swap and a priority 20 hdd swap partition. swappiness is instead the threshold of free memory at which point the system starts to use the swap. – nrc Dec 17 '21 at 16:49
  • `swapon -p 100` will have no effect here, that's just a relative priority amongst all the swap partition (if there is for example still a disk swap), you probably indeed meant `swappiness` – Stanislav Modrák Mar 04 '23 at 13:00