diff --git a/libvirt_hooks/hooks/alloc_hugepages.sh b/libvirt_hooks/hooks/alloc_hugepages.sh deleted file mode 100755 index 7f2028a..0000000 --- a/libvirt_hooks/hooks/alloc_hugepages.sh +++ /dev/null @@ -1,39 +0,0 @@ -#! /bin/bash -# -# Author: -# -# This hook automatically (un-)allocates static HugePages when starting/stopping a VM. -# This file depends on the Passthrough POST hook helper script found in this repo. -# Place this script in BOTH these directories (or symlink it): -# $SYSCONFDIR/libvirt/hooks/qemu.d/your_vm/prepare/begin/ -# $SYSCONFDIR/libvirt/hooks/qemu.d/your_vm/release/end/ -# $SYSCONFDIR usually is /etc/libvirt. -# Set the files as executable through `chmod +x`. -# -# Also make sure to configure your VM's XML file and /etc/fstab or this script won't work. - -# Get size of VM-Memory and HugePages -XML_PATH=/etc/libvirt/qemu/$1.xml -MEM_HOST=$(grep 'MemAvailable' /proc/meminfo | grep -ohE '[[:digit:]]+') # Available host memory -MEM_GUEST=$(grep ' $HPG_PATH -} - -function prepMemory { - # Prepare memory for allocation - echo 1 > /proc/sys/vm/compact_memory -} - -# If VM fits into memory, then allocate HugePages -if (($MEM_GUEST < $MEM_HOST)); then - allocPages -fi diff --git a/libvirt_hooks/hooks/hugepages.sh b/libvirt_hooks/hooks/hugepages.sh new file mode 100755 index 0000000..0e1c4e6 --- /dev/null +++ b/libvirt_hooks/hooks/hugepages.sh @@ -0,0 +1,67 @@ +#! /bin/bash +# +# Author: Stefsinn (https://github.com/Stefsinn) +# +# This hook automatically un-allocates static HugePages when stopping a VM. +# This file depends on the PassthroughPOST hook helper script found here: +# https://github.com/PassthroughPOST/VFIO-Tools/tree/master/libvirt_hooks +# Place this script in BOTH these directories (or symlink it): +# $SYSCONFDIR/libvirt/hooks/qemu.d/your_vm/prepare/begin/ +# $SYSCONFDIR/libvirt/hooks/qemu.d/your_vm/release/end/ +# $SYSCONFDIR usually is /etc/libvirt. +# Get inputs from libvirt +GUEST_NAME="$1" +GUEST_ACTION="$2/$3" +# Get path to guest XML +XML_PATH="/etc/libvirt/qemu/$GUEST_NAME.xml" +# Get guest HugePage size +HPG_SIZE=$(grep ' "$HPG_PATH" + kmessageNotify "Allocating ${GUEST_MEM}kB of HugePages for VM ${GUEST_NAME}" +} + +function release/end { + # Unallocate HugePages + ((HPG_NEW = HPG_CURRENT - GUEST_MEM / HPG_SIZE )) + echo "$HPG_NEW" > "$HPG_PATH" + kmessageNotify "Releasing ${GUEST_MEM}kB of HugePages for VM ${GUEST_NAME}" +} + +function kmessageNotify { + MESSAGE="$1" + while read -r line; do + echo "libvirt_qemu hugepages: ${line}" > /dev/kmsg 2>&1 + done < <(echo "${MESSAGE}") +} + +# Do some checks before continuing +if [[ $HPG_SIZE -eq 0 ]]; then + # Break if HugePage size is 0 + echo "ERROR: HugePage size cannot be 0." >&2 + exit 1 +elif [[ -z $GUEST_MEM ]]; then + echo "ERROR: Can't determine guest's memory allocation" >&2 + exit 1 +elif [[ ! -d "$HPG_PATH" ]]; then + # Break if HugePages path doesn't exist + echo "ERROR: ${HPG_PATH} does not exist. (HugePages disabled in kernel?)" >&2 + exit 1 +elif [[ -z $HPG_SIZE ]]; then + # This exits silently if HugePages appear disabled for a guest + exit 0 +fi + +# All checks passed, continue +${GUEST_ACTION} diff --git a/libvirt_hooks/hooks/unalloc_hugepages.sh b/libvirt_hooks/hooks/unalloc_hugepages.sh deleted file mode 100755 index 836cfe8..0000000 --- a/libvirt_hooks/hooks/unalloc_hugepages.sh +++ /dev/null @@ -1,33 +0,0 @@ -#! /bin/bash -# -# Author: Stefsinn (https://github.com/Stefsinn) -# -# This hook automatically un-allocates static HugePages when stopping a VM. -# This file depends on the PassthroughPOST hook helper script found here: -# https://github.com/PassthroughPOST/VFIO-Tools/tree/master/libvirt_hooks -# Place this script in BOTH these directories (or symlink it): -# $SYSCONFDIR/libvirt/hooks/qemu.d/your_vm/prepare/begin/ -# $SYSCONFDIR/libvirt/hooks/qemu.d/your_vm/release/end/ -# $SYSCONFDIR usually is /etc/libvirt. -# Get path to guest XML -XML_PATH="/etc/libvirt/qemu/${1}.xml" -# Get guest HugePage size -HPG_SIZE=$(grep ' "$HPG_PATH" -} - -# Call function to unallocate HugePages if HugePage count is greater than 0 -if [[ HPG_SIZE -gt 0 ]]; then - unallocPages -fi