Question

One of my commands in my bash script will depend on the virtualization of the server (XEN or OpenVZ or KVM ). How can I check which of these is in use in bash?

Was it helpful?

Solution 2

I found a small shell script that is able to detect virtualization and it handles Xen,OpenVZ,KVM,Parallels, Vmware and many more

virt-what

Installation with yum is pretty straight forward

Here is the output on my system

$ virt-what
kvm 

OTHER TIPS

There's a very useful script called imvirt that handles Xen, OpenVZ, VMware, VirtualBox, KVM, and lots of others. It's available as a package in Debian, or from the imvirt web site.

$ imvirt
Xen PV 4.1

If you want to detect host (dom0) for xen, check

[ "$(cat /proc/xen/capabilities)" == "control_d" ]

If you want to detect in VM,
You need to execute cpuid instruction in VM, with original_eax=1.
If the resultant ecx has MSB set ((ecx & 0x80000000) != 0), then you are under VM.
This is assuming that your hypervisor supports viridian interface. Xen does.
cpuid package is easily available for many linux distros. I'm sure windows port would be available too. Else, the code is pretty simple for you to write...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top