Memory

如何手動觸發來賓上的 virtio 氣球驅動程序?

  • June 11, 2019

我希望在 QEMU 中執行的某個 Linux 客戶機在主機處於記憶體壓力下*之前釋放記憶體。*如何手動觸發來賓上的virtio 氣球驅動程序,以便它釋放盡可能多的記憶體?

像這樣的事情似乎有效:首先減少來賓可用的記憶體量,然後(可選)將其恢復到原始設置。

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

引用自:https://unix.stackexchange.com/questions/413457