Skip to content

Commit

Permalink
compose: increase zram at runtime
Browse files Browse the repository at this point in the history
To alleviate memory pressure, increase zram to 50% of total memory at
runtime. This allows the host to compress inactive pages to reduce
contention.

This service optionally allows for the maximum zram percentage and
compression algorithm to be modified at runtime using variables.

Change-type: patch
Signed-off-by: Joseph Kogut <joseph@balena.io>
  • Loading branch information
jakogut committed Oct 28, 2024
1 parent 4502783 commit dc63ee1
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,48 @@ services:
CLOUDFLARE_DNS_ZONE: product-os.io
ROBOT_API: https://robot-ws.your-server.de

zram:
image: docker:27.3.1-cli-alpine3.20
restart: no
labels:
io.balena.features.balena-socket: '1'
entrypoint:
- /bin/sh
- -c
environment:
ENABLED: true
zram_pct: 50
zram_algo: lz4
command:
- |
set -e
[[ $ENABLED == 'true' ]] || exit
HOST_CONTAINER_NAME=host-chroot
stop_container() { docker stop "$(docker ps -qf "NAME=$HOST_CONTAINER_NAME")"; }
trap stop_container EXIT
host_cmd() {
if [ -z "$(docker ps -qf "NAME=$HOST_CONTAINER_NAME")" ]; then
docker run \
--interactive \
--tty=true \
--detach \
--rm \
--name="$HOST_CONTAINER_NAME" \
--volume=/:/host \
--privileged \
alpine:3.20.3
fi
HOST_CONTAINER_ID="$(docker ps -qf "NAME=$HOST_CONTAINER_NAME")"
docker exec "$HOST_CONTAINER_ID" chroot /host bash -c "$*"
}
host_cmd "swaps=\$(awk 'NR>1 { print \$1 }' /proc/swaps); if [ -n \"\$swaps\" ]; then swapoff \$swaps; fi"
host_cmd "zrams=\$(find /dev -name \"zram*\"); if [ -n \"\$zrams\" ]; then for d in \$zrams; do zramctl -r \$d; done; fi"
zram_dev=$(host_cmd "memtotal=\$(grep MemTotal /proc/meminfo | awk '{ print \$2 }'); zramctl --find --size \$((memtotal * $zram_pct / 100))K --algorithm $zram_algo")
host_cmd "mkswap $zram_dev && swapon $zram_dev"
# https://github.com/balenablocks/cert-manager
# https://certbot.eff.org/docs/using.html
# https://certbot-dns-cloudflare.readthedocs.io/
Expand Down

0 comments on commit dc63ee1

Please sign in to comment.