Skip to content

Instantly share code, notes, and snippets.

@Manouchehri
Forked from rizalp/zram-ubuntu-1804.md
Created December 6, 2020 19:12
Show Gist options
  • Save Manouchehri/4a25aa3552d8b519d56981ed4c4fbaf2 to your computer and use it in GitHub Desktop.
Save Manouchehri/4a25aa3552d8b519d56981ed4c4fbaf2 to your computer and use it in GitHub Desktop.
Zram Ubuntu 1804
  • sudo apt install zram-config, this will install several files, including systemd startup /etc/systemd/system/multi-user.target.wants/zram-config.service
  • sudo nano /usr/bin/init-zram-swapping and edit:
#!/bin/sh

# load dependency modules
NRDEVICES=1
if modinfo zram | grep -q ' zram_num_devices:' 2>/dev/null; then
  MODPROBE_ARGS="zram_num_devices=${NRDEVICES}"
elif modinfo zram | grep -q ' num_devices:' 2>/dev/null; then
  MODPROBE_ARGS="num_devices=${NRDEVICES}"
else
  exit 1
fi
modprobe zram $MODPROBE_ARGS

# Calculate memory to use for zram (1/2 of ram)
totalmem=`LC_ALL=C free | grep -e "^Mem:" | sed -e 's/^Mem: *//' -e 's/  *.*//'`
mem=$(((totalmem / 2 / ${NRDEVICES}) * 1024))

# 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment