Skip to content

Commit

Permalink
check for KVM virtualization
Browse files Browse the repository at this point in the history
  • Loading branch information
bra-fsn committed Jan 10, 2025
1 parent 26f1397 commit f1361a1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
8 changes: 8 additions & 0 deletions images/virtualization/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM ubuntu:24.04
RUN --mount=type=tmpfs,target=/tmp,rw \
--mount=id=var_cache_apt,type=cache,target=/var/cache/apt,sharing=locked \
--mount=id=var_lib_apt,type=cache,target=/var/lib/apt,sharing=locked \
apt-get update --error-on=any && \
apt-get upgrade -y && \
apt-get install -y cpu-checker
ADD check_virt.sh /usr/local/bin/check_virt.sh
19 changes: 19 additions & 0 deletions images/virtualization/check_virt.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/sh

# Check if kvm-ok is installed
if ! command -v kvm-ok > /dev/null 2>&1; then
echo '{"kvm":false,"kvm_error":"kvm-ok command not found"}'
exit 1
fi

# Run kvm-ok and capture both stdout and stderr
output=$(kvm-ok 2>&1)
exit_code=$?

if [ "$exit_code" -eq 0 ]; then
echo '{"kvm":true}'
else
# Replace newlines with spaces and escape quotes
sanitized_output=$(printf '%s' "$output" | tr '\n' ' ' | sed 's/"/\\"/g')
echo "{\"kvm\":false,\"kvm_error\":\"$sanitized_output\"}"
fi

0 comments on commit f1361a1

Please sign in to comment.