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?
3 Answers
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

- 4,894
- 1
- 26
- 35
-
@user2650277 - the web site mentions a stand-alone package, with all Perl dependencies included. That should run on CentOS. – Peter Westlake Mar 12 '14 at 15:36
-
I did `yum install imvirt` and unfortunately the imvirt command show errors on my system.It seems unable to detect that i am on KVM – user2650277 Mar 12 '14 at 16:34
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

- 6,289
- 17
- 63
- 132
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...

- 20,270
- 5
- 40
- 73
-
So just to confirm, you want to detect if the dom0 is under `OpenVz`. Right? – anishsane Mar 12 '14 at 15:28