KVM: selftests: Avoid assuming "sudo" exists in NX hugepage test

Writing various root-only files, omit "sudo" when already running as root
to allow running the NX hugepage test on systems with a minimal rootfs,
i.e. without sudo.

Signed-off-by: Brendan Jackman <jackmanb@google.com>
Reviewed-by: Muhammad Usama Anjum <usama.anjum@collabora.com>
Link: https://lore.kernel.org/r/20240415-kvm-selftests-no-sudo-v1-1-95153ad5f470@google.com
[sean: name the helper do_sudo() instead of maybe_sudo(), massage changelog]
Signed-off-by: Sean Christopherson <seanjc@google.com>
This commit is contained in:
Brendan Jackman
2024-04-15 14:43:54 +00:00
committed by Sean Christopherson
parent 72cd4de01d
commit 0540193614
@@ -13,10 +13,21 @@ NX_HUGE_PAGES_RECOVERY_RATIO=$(cat /sys/module/kvm/parameters/nx_huge_pages_reco
NX_HUGE_PAGES_RECOVERY_PERIOD=$(cat /sys/module/kvm/parameters/nx_huge_pages_recovery_period_ms)
HUGE_PAGES=$(cat /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages)
# If we're already root, the host might not have sudo.
if [ $(whoami) == "root" ]; then
function do_sudo () {
"$@"
}
else
function do_sudo () {
sudo "$@"
}
fi
set +e
function sudo_echo () {
echo "$1" | sudo tee -a "$2" > /dev/null
echo "$1" | do_sudo tee -a "$2" > /dev/null
}
NXECUTABLE="$(dirname $0)/nx_huge_pages_test"