我希望在 QEMU 中运行的某个 Linux 来宾释放内存前主机面临内存压力。如何手动触发virtio 气球驱动程序以便它释放尽可能多的内存?
答案1
这样的事情似乎有效:首先减少来宾可用的内存量,然后(可选)将其恢复到原始设置。
virsh --connect qemu:///system qemu-monitor-command --domain NAME --hmp 'balloon 512'
# Wait for the balloon driver to release some memory
sleep 2
virsh --connect qemu:///system qemu-monitor-command --domain NAME --hmp 'balloon 4096'
或者,作为一个正确的脚本:
#!/bin/bash
set -eu
domain=$1
# How many MB to temporarily reduce the guest memory to; don't set this too
# low or the OOM killer will start killing.
reduced_memory=$2
hmp() {
virsh --connect qemu:///system qemu-monitor-command --domain "$domain" --hmp "$1"
}
original_memory=$(hmp "info balloon" | grep = | cut -d '=' -f 2)
hmp "balloon $reduced_memory" > /dev/null
# Wait for the balloon driver to release some memory before setting it back
sleep 2
hmp "balloon $original_memory" > /dev/null