util: Enable KVM on VMs and ensure working in Runners

This patch:

1. Adds setup scripting to "provision_root.sh" to setup and enable KVM,
   for the 'vagrant' user, for VMs which are capable of this.
2. Runs a check on each VM to see if KVM can be run sucessfully within a
   docker container. If so, the GitHub Actions runner is given a 'kvm'
   label. It is unknown at this time if GitHub Runners can utlized KVM
   but it is open to their processes.

Change-Id: Idfcbb7bfa3e5b7cc47d29aea50fb1ebcafdb7acc
This commit is contained in:
Bobby R. Bruce
2023-10-16 21:21:31 -07:00
parent d18087af96
commit 3783afff5d
2 changed files with 44 additions and 3 deletions

View File

@@ -58,8 +58,32 @@ Vagrant.configure("2") do |config|
# Copy the "action-run.sh" script from the host to the VM.
runner.vm.provision "file", source: "./action-run.sh", destination: "/tmp/action-run.sh"
runner.vm.provision :shell, privileged: false, inline: "cp /tmp/action-run.sh ."
# Execute the actions-run.sh script on every boot. This configures the and starts the runner.
runner.vm.provision :shell, privileged: false, run: 'always', inline: "./action-run.sh #{PERSONAL_ACCESS_TOKEN} #{GITHUB_ORG} >> action-run.log 2>&1 &"
# The following attempts to see if KVM can be used inside the docker
# container.
#
# Almost all github action jobs run within a docker container. Therefore
# to be compatible with KVM, KVM must be enabled inside the docker.
#
# We used existence of "kvm-works" in the VM home directory is how we
# indicate that KVM is working. It is created if the 'kvm-ok' command is
# successful. This is then passed to the action-run.sh script to indicate
# that the runner can be used for KVM via the `kvm` label.
runner.vm.provision :shell, privileged: false, run: 'always', inline: <<-SHELL
rm -f kvm-works
docker run --device /dev/kvm -v$(pwd):/work -w /work --rm ubuntu:22.04 bash -c "apt update -y && apt install -y cpu-checker && kvm-ok"
status=$?
if [[ ${status} == 0 ]]; then
echo >&1 "Success. KVM enabled."
echo "success" > kvm-works
else
echo >&2 "Failure. KVM not enabled."
fi
exit 0
SHELL
# Execute the actions-run.sh script on every boot. This configures and starts the runner.
# Note the 'kvm' label is applied to this runner if the "kvm-works" file eixsts. See above.
runner.vm.provision :shell, privileged: false, run: 'always', inline: "./action-run.sh #{PERSONAL_ACCESS_TOKEN} #{GITHUB_ORG} $(if [ -f 'kvm-works' ]; then echo 'kvm'; fi) >> action-run.log 2>&1 &"
end
end
end

View File

@@ -56,7 +56,8 @@ apt-get install -y \
apt-transport-https ca-certificates \
curl \
gnupg \
lsb-release
lsb-release \
cpu-checker
# Install docker
apt-get install -y apt-transport-https ca-certificates curl gnupg lsb-release
@@ -70,6 +71,22 @@ apt-get install -y docker-ce docker-ce-cli containerd.io
# work.
usermod -aG docker vagrant
kvm-ok
kvm_ok_status=$?
# `kvm-ok` will return a exit zero if the machine supports KVM, and non-zero
# otherwise. If the machine support KVM, let's enable it.
if [[ ${kvm_ok_status} == 0 ]]; then
apt install -y qemu-kvm \
virt-manager \
libvirt-daemon-system virtinst \
libvirt-clients bridge-utils && \
sudo systemctl enable --now libvirtd && \
sudo systemctl start libvirtd && \
usermod -aG kvm vagrant && \
usermod -aG libvirt vagrant
fi
# Cleanup
apt-get autoremove -y