From ec743728c21ef577f6360b1577cf698b5926c5f1 Mon Sep 17 00:00:00 2001 From: Simon Cornet Date: Sat, 25 Oct 2025 14:57:21 +0200 Subject: [PATCH] feat: improve motd memory usage for lxc --- templates/motd/alpine-motd.sh.j2 | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/templates/motd/alpine-motd.sh.j2 b/templates/motd/alpine-motd.sh.j2 index 7616106..3c1f4e7 100644 --- a/templates/motd/alpine-motd.sh.j2 +++ b/templates/motd/alpine-motd.sh.j2 @@ -1,5 +1,4 @@ #!/bin/sh - # distribution information if [ -f /etc/os-release ]; then . /etc/os-release @@ -11,8 +10,17 @@ fi # disk usage disk_usage=$(df -h / | awk 'NR==2 {print $3 " / " $2 " (" $5 ")"}') -# memory usage -memory_usage=$(free -m | awk 'NR==2 {print $3 " MB / " $2 " MB (" int($3/$2*100) "%)"}') +# memory usage (cgroup aware for LXC) +if [ -f /sys/fs/cgroup/memory.current ]; then + mem_used_kb=$(awk '{print int($1/1024)}' /sys/fs/cgroup/memory.current) + mem_total_kb=$(awk '/MemTotal/ {print $2}' /proc/meminfo) + mem_used_mb=$((mem_used_kb / 1024)) + mem_total_mb=$((mem_total_kb / 1024)) + mem_pct=$((mem_used_kb * 100 / mem_total_kb)) + memory_usage="$mem_used_mb MB / $mem_total_mb MB ($mem_pct%)" +else + memory_usage=$(free -m | awk 'NR==2 {print $3 " MB / " $2 " MB (" int($3/$2*100) "%)"}') +fi # display motd echo "Welcome to $(hostname)!" @@ -20,4 +28,3 @@ echo "" echo -e "$linux_distribution" echo "Disk Usage: $disk_usage" echo "Memory Usage: $memory_usage" -echo ""