Commit 36f2d9f3 authored by Joe Holden's avatar Joe Holden

Split detectvirt out into containr and hypervisor, detect kvm properly under non-systemd

parent 44cef3ab
......@@ -59,6 +59,7 @@ Notes:
* Values for **VIRT** and **CONT** use the naming convention from **[systemd-detect-virt](https://www.freedesktop.org/software/systemd/man/systemd-detect-virt.html "systemd-detect-virt")** with the exception of FreeBSD jails.
* Where we can identify the container, it is presented colon seperated (in the case of pipe), e.g **kvm:jail**.
* If the DMI strings or virtual cpu names have been changed, or running on a particularly old OS there is a likely chance that the detected virtualisation will be wrong.
* At present, the virt detection is pretty limited (for example under Linux, if the system doesn't not have systemd and it's running under KVM, it will still return QEMU)
Output:
* Default output is pipe deliminated: `OS|KERNEL|ARCH|DISTRO|VERSION|VIRT[:CONT]`
......
......@@ -301,7 +301,6 @@ getversion() {
}
detectvirt() {
local virt
local type
type=none
case $1 in
......@@ -326,8 +325,8 @@ detectvirt() {
[Vv][Uu][Ll][Tt][Rr]|[Dd][Rr][Oo][Pp][Ll][Ee][Tt]*)
type=kvm
;;
Standard*PC*Q*ICH*)
type=kvm
Standard*PC*Q*ICH*|Standard*PC*i440FX*PIIX*)
type=qemu
;;
zvm|oracle|bochs|uml|parallels)
type=${1}
......@@ -340,6 +339,38 @@ detectvirt() {
echo $type
}
detectcont() {
local type
type=none
case $1 in
*)
type=${1}
;;
esac
echo $type
}
getcont() {
CONT="none"
if [ "${OS}" = "Linux" ]; then
if [ -f /usr/bin/systemd-detect-virt ]; then
CONT=`/usr/bin/systemd-detect-virt -c`
fi
elif [ "${OS}" = "FreeBSD" -o "${OS}" = "OpenBSD" -o "${OS}" = "NetBSD" ]; then
if [ "${OS}" = "FreeBSD" ]; then
tmp=`sysctl -n security.jail.jailed 2>/dev/null`
if [ "${tmp}" = "1" ]; then
CONT=jail
fi
fi
else
CONT=""
fi
export CONT
return 0
}
getvirt() {
VIRT="none"
if [ "${OS}" = "Linux" ]; then
......@@ -347,6 +378,12 @@ getvirt() {
tmp=`/usr/bin/systemd-detect-virt -v`
VIRT=`detectvirt "${tmp}"`
fi
if [ "${VIRT}" = "none" ]; then
if [ -f /sys/devices/virtual/dmi/id/product_name ]; then
tmp=`cat /sys/devices/virtual/dmi/id/product_name`
VIRT=`detectvirt "${tmp}"`
fi
fi
if [ "${VIRT}" = "none" ]; then
if [ -f `which dmidecode 2>/dev/null` ]; then
tmp=`dmidecode -s system-product-name 2>/dev/null`
......@@ -380,16 +417,6 @@ getvirt() {
tmp=`sysctl -n hw.model 2>/dev/null`
VIRT=`detectvirt "${tmp}"`
fi
if [ "${OS}" = "FreeBSD" ]; then
tmp=`sysctl -n security.jail.jailed 2>/dev/null`
if [ "${tmp}" = "1" ]; then
if [ "${VIRT}" = "none" ]; then
VIRT=jail
else
VIRT=${VIRT}:jail
fi
fi
fi
else
VIRT=""
fi
......@@ -403,13 +430,18 @@ getarch
getdistro
getversion
getvirt
getcont
if [ -z ${DISTROEXEC+x} ]; then
if [ "${AGENT_LIBDIR}" -o "${MK_LIBDIR}" ]; then
echo "<<<distro>>>"
fi
if [ "${DISTROFORMAT}" = "twopipe" ]; then
echo "${OS}||${KERNEL}||${ARCH}||${DISTRO}||${VERSION}||${VIRT}"
if [ "${CONT}" = "none" ]; then
echo "${OS}||${KERNEL}||${ARCH}||${DISTRO}||${VERSION}||${VIRT}"
else
echo "${OS}||${KERNEL}||${ARCH}||${DISTRO}||${VERSION}||${VIRT}:${CONT}"
fi
elif [ "${DISTROFORMAT}" = "ini" ]; then
echo "[distroscript]"
echo " OS = ${OS}"
......@@ -418,6 +450,7 @@ if [ -z ${DISTROEXEC+x} ]; then
echo " DISTRO = ${DISTRO}"
echo " VERSION = ${VERSION}"
echo " VIRT = ${VIRT}"
echo " CONT = ${CONT}"
echo " SCRIPTVER = ${DISTROSCRIPT}"
elif [ "${DISTROFORMAT}" = "export" ]; then
echo "OS=${OS}"
......@@ -426,11 +459,16 @@ if [ -z ${DISTROEXEC+x} ]; then
echo "DISTRO=${DISTRO}"
echo "VERSION=${VERSION}"
echo "VIRT=${VIRT}"
echo "CONT=${CONT}"
echo "SCRIPTVER=${DISTROSCRIPT}"
elif [ "${DISTROFORMAT}" = "json" ]; then
echo "{\"os\":\"${OS}\",\"kernel\":\"${KERNEL}\",\"arch\":\"${ARCH}\",\"distro\":\"${DISTRO}\",\"version\":\"${VERSION}\",\"virt\":\"${VIRT}\",\"scriptver\":\"${DISTROSCRIPT}\"}"
echo "{\"os\":\"${OS}\",\"kernel\":\"${KERNEL}\",\"arch\":\"${ARCH}\",\"distro\":\"${DISTRO}\",\"version\":\"${VERSION}\",\"virt\":\"${VIRT}\",\"cont\":\"${CONT}\",\"scriptver\":\"${DISTROSCRIPT}\"}"
else
echo "${OS}|${KERNEL}|${ARCH}|${DISTRO}|${VERSION}|${VIRT}"
if [ "${CONT}" = "none" ]; then
echo "${OS}|${KERNEL}|${ARCH}|${DISTRO}|${VERSION}|${VIRT}"
else
echo "${OS}|${KERNEL}|${ARCH}|${DISTRO}|${VERSION}|${VIRT}:${CONT}"
fi
fi
exit 0
fi
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment