Package: freedombox-setup
Severity: wishlist

This may help for systems with not much RAM, such as the Beaglebone Black.

---
 first-run.d/03_zram | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++++
  1 file changed, 92 insertions(+)
   create mode 100755 first-run.d/03_zram

diff --git a/first-run.d/03_zram b/first-run.d/03_zram
new file mode 100755
index 0000000..656a067
--- /dev/null
+++ b/first-run.d/03_zram
@@ -0,0 +1,92 @@
+#!/bin/bash
+
+# Because the typical deployment will be on hardware with limited
+# memory resources enabling zram can help to avoid processes being
+# oom killed
+
+# Adapted from systemd scripts at https://github.com/mystilleef/FedoraZram
+
+if [ ! -f /etc/modprobe.d/zram.conf ]; then
+    echo 'options zram num_devices=1' > /etc/modprobe.d/zram.conf
+fi
+
+cat > /etc/systemd/system/zram.service <<EOF
+[Unit]
+Description=Enable compressed swap in memory using zram
+After=multi-user.target
+
+[Service]
+RemainAfterExit=yes
+ExecStart=/usr/sbin/zramstart
+ExecStop=/usr/sbin/zramstop
+Type=oneshot
+
+[Install]
+WantedBy=multi-user.target
+EOF
+
+echo '#!/bin/bash -
+
+# get the number of CPUs
+num_cpus=$(grep -c processor /proc/cpuinfo)
+
+# if something goes wrong, assume we have 1
+[ "$num_cpus" != 0 ] || num_cpus=1
+
+# set decremented number of CPUs
+decr_num_cpus=$((num_cpus - 1))
+
+# get the amount of memory in the machine
+mem_total_kb=$(grep MemTotal /proc/meminfo | grep -E --only-matching 
"[[:digit:]]+")
+mem_total=$((mem_total_kb * 1024))
+
+# load dependency modules
+modprobe zram num_devices=$num_cpus
+
+# initialize the devices
+for i in $(seq 0 $decr_num_cpus); do
+    echo $((mem_total / num_cpus)) > /sys/block/zram$i/disksize
+done
+
+# Creating swap filesystems
+for i in $(seq 0 $decr_num_cpus); do
+    mkswap /dev/zram$i
+done
+
+# Switch the swaps on
+for i in $(seq 0 $decr_num_cpus); do
+    swapon -p 100 /dev/zram$i
+done
+exit 0' > /usr/sbin/zramstart
+
+cat > /usr/sbin/zramstop <<EOF
+#!/bin/bash -
+
+# get the number of CPUs
+num_cpus=$(grep -c processor /proc/cpuinfo)
+
+# set decremented number of CPUs
+decr_num_cpus=$((num_cpus - 1))
+
+# Switching off swap
+for i in $(seq 0 $decr_num_cpus); do
+    if [ "$(grep /dev/zram$i /proc/swaps)" != "" ]; then
+        swapoff /dev/zram$i
+        sleep 1
+    fi
+done
+
+sleep 1
+rmmod zram
+exit 0
+EOF
+
+chmod +x /usr/sbin/zramstart
+chmod +x /usr/sbin/zramstop
+
+# start the daemon
+systemctl enable zram.service
+systemctl daemon-reload
+systemctl start zram.service
+
+exit 0
--
2.4.1

-- System Information:
Debian Release: 8.2
  APT prefers stable-updates
  APT policy: (500, 'stable-updates'), (500, 'stable')
Architecture: amd64 (x86_64)

Kernel: Linux 3.16.0-4-amd64 (SMP w/4 CPU cores)
Locale: LANG=en_GB.UTF-8, LC_CTYPE=en_GB.UTF-8 (charmap=UTF-8)
Shell: /bin/sh linked to /bin/dash
Init: systemd (via /run/systemd/system)

Reply via email to