From 0c74876b911595bc08eb10959f16e98bb3c32c1a Mon Sep 17 00:00:00 2001 From: Actaeus Curabitur Date: Mon, 25 Dec 2023 23:34:07 -0600 Subject: [PATCH] Added auto-ballooning --- states/roles/maintain/host/balloon.service | 10 +++++++ states/roles/maintain/host/balloon.sh | 21 ++++++++++++++ states/roles/maintain/host/balloon.timer | 9 ++++++ states/roles/maintain/host/init.sls | 32 ++++++++++++++++++++++ 4 files changed, 72 insertions(+) create mode 100644 states/roles/maintain/host/balloon.service create mode 100644 states/roles/maintain/host/balloon.sh create mode 100644 states/roles/maintain/host/balloon.timer diff --git a/states/roles/maintain/host/balloon.service b/states/roles/maintain/host/balloon.service new file mode 100644 index 0000000..7c1d861 --- /dev/null +++ b/states/roles/maintain/host/balloon.service @@ -0,0 +1,10 @@ +[Unit] +Description=Automatically adjust balloon size to free up unused memory + +[Service] +Type=oneshot +RemainAfterExit=no +ExecStart=/bin/bash /root/scripts/balloon.sh + +[Install] +WantedBy=multi-user.target diff --git a/states/roles/maintain/host/balloon.sh b/states/roles/maintain/host/balloon.sh new file mode 100644 index 0000000..6d4ee1a --- /dev/null +++ b/states/roles/maintain/host/balloon.sh @@ -0,0 +1,21 @@ +for domain in `virsh list --name` +do + virsh dommemstat --period 5 $domain + max=`virsh dominfo $domain | grep Max | grep -Po "\d+"` + current=`virsh dominfo $domain | grep Used | grep -Po "\d+"` + unused=`virsh dommemstat $domain | grep unused | grep -Po "\d+"` + used=$(($current - $unused)) + newfree=$((($max - $used) / 5)) + if test $newfree -gt 524288 + then + target=$(($newfree + $used)) + else + target=$((524288 + $used)) + fi + if test $target -gt $max + then + target=$max + fi + echo "$domain: $target" + virsh setmem $domain --size $target +done diff --git a/states/roles/maintain/host/balloon.timer b/states/roles/maintain/host/balloon.timer new file mode 100644 index 0000000..c8ac022 --- /dev/null +++ b/states/roles/maintain/host/balloon.timer @@ -0,0 +1,9 @@ +[Unit] +Description=Update balloon sizes every 10 minutes + +[Timer] +OnCalendar=*:0/10 +Unit=balloon.service + +[Install] +WantedBy=multi-user.target diff --git a/states/roles/maintain/host/init.sls b/states/roles/maintain/host/init.sls index e959f0a..a42b3ae 100644 --- a/states/roles/maintain/host/init.sls +++ b/states/roles/maintain/host/init.sls @@ -99,3 +99,35 @@ libvirtd: # - file: /etc/systemd/network/br1.netdev # - file: /etc/systemd/network/br1.network # - file: /etc/systemd/network/uplink.network + + +/root/scripts/balloon.sh: + file.managed: + - source: salt://roles/maintain/host/balloon.sh + - user: root + - group: root + - mode: 644 + +/lib/systemd/system/balloon.service: + file.managed: + - source: salt://roles/maintain/host/balloon.service + - user: root + - group: root + - mode: 644 + +/lib/systemd/system/balloon.timer: + file.managed: + - source: salt://roles/maintain/host/balloon.timer + - user: root + - group: root + - mode: 644 + +balloon-reload: + module.run: + - name: service.systemctl_reload + - onchanges: + - file: /lib/systemd/system/* + +balloon.timer: + service.running: + - enable: true