In addition to the following methods, the mac address can also detect virtual machines.
Update: You may be interested in [[the perl module | perl-module-for-virtualization alization-detection] implementing these tricks.
Frequently, our customers want to install our software in a virtual machine. this can be OK, but frequently they hit a CPU, memory, or IO limit caused by running in a constrained virtual environment. when this happens, we really like to know if they're running under implements alization when we try to support them. here's some tricks to detect, from a shell, if the system is already alized.
The first thing to check is dmesg. on a recently-booted system, checking the 'dmesg' command output may be sufficient. otherwise, try "cat/var/log/dmesg" instead of "dmesg"
VMWare:
# Dmesg | grep-I virtual
VMware vmxnet virtual NIC driver
Vendor: VMware Model: Virtual disk Rev: 1.0
Hda: VMware Virtual ide cdrom Drive, atapi cd/DVD-ROM drive
QEmu or KVM: If the "-cpu host" option has not been used, QEmu and KVM will identify themselves:
# Dmesg | grep-I virtual
CPU: amd qemu Virtual CPU version 0.9.1 stepping 03
Otherwise, the host's CPU information will be used both in dmesg, or in/proc/cpuinfo.
However, you shoshould see something like:
$ Dmesg | grep-I virtual
[2, 0.000000] Booting paravirtualized kernel on KVM
On newer kernels that understand that they're running under paravirtualization. (Thanks to René Pfeiffer for pointing this out)
Microsoft VirtualPC:
# Dmesg | grep-I virtual
Hda: Virtual HD, ata disk drive
Hdc: Virtual CD, atapi cd/DVD-ROM drive
Xen (thanks, wardi and others)
# Dmesg | grep-I xen
Xen virtual console successfully installed as tty1
Virtuozzo
# Dmesg
(Returns no output)
# Cat/var/log/dmesg
(Returns no output)
# Ls-al/proc/vz
Veinfo veinfo_redir veredir vestat vzaquota vzdata
On longer-running systems, you may need to grep/var/log/dmesg instead.
If that doesn't produce anything useful, try using dmidecode to look at the BIOS information. Frequently, there will be at least one component identifying itself as receivalized:
VMWare:
# Dmidecode | egrep-I manufacturer | product
Manufacturer: VMware, Inc.
Product Name: VMware Virtual Platform
Microsoft VirtualPC:
# Dmidecode | egrep-I manufacturer | product
Manufacturer: Microsoft Corporation
Product Name: Virtual Machine
QEMU or KVM:
# Dmidecode | egrep-I vendor
Vendor: QEMU
Virtuozzo:
# Dmidecode
/Dev/mem: Permission denied
Xen:
# Dmidecode | grep-I domU
Product Name: HVM domU
You shoshould just examine the output of dmidecode directly rather than trying to grep as above, in case the output changes. QEMU, for example, doesn't report the vendor in all versions.
Next, check disk devices for identification as receivalized:
VMWare:
# Cat/proc/ide/hd */model
VMware Virtual ide cdrom Drive
# Cat/proc/scsi
Attached devices:
Host: scsi0 Channel: 00 Id: 00 Lun: 00
Vendor: VMware Model: Virtual disk Rev: 1.0
Type: Direct-Access ansi scsi revision: 02
Microsoft VirtualPC:
# Cat/proc/ide/hd */model
Virtual HD
Virtual CD
QEMU, KVM, or Xen:
# Cat/proc/ide/hd */model
QEMU HARDDISK
DVD-ROM QEMU
Virtuozzo:
# Ls-al/dev/vzfs
B ----- x --- 1 root 0, 19/dev/vzfs
If you 've got tips for other already alized environments, let me know and I'll add them to the list.