Peter Larsson Blog

Patron Saint of Lost Yaks

How to tell if you are running on a virtual environment

This is a piece of code I often use to see if the database server is running in a virtual environment. That is not always obvious or known by the developers.

DECLARE @Result TABLE
        (
            LogDate DATETIME,
            ProcessInfo NVARCHAR(MAX),
            Text NVARCHAR(MAX)
        )
 
INSERT  @Result
EXEC    sys.xp_readerrorlog 0, 1, 'System Manufacturer', 'VMware'
 
IF EXISTS (SELECT * FROM @Result)
    SELECT 'It seems you are running on VMware.' AS Msg
ELSE
    SELECT 'It seems you are not running on VMware.' AS Msg


If you test this in your environment and found other virtual manufacturers, please let me know so I can add them in the code above.

Legacy Comments


Buck Woody
2009-11-12
re: How to tell if you are running on a virtual environment
This works for Virtual PC/Hyper-V - and I'm totally borrowing this code if that's acceptable with you

DECLARE @Result TABLE
(
LogDate DATETIME,
ProcessInfo NVARCHAR(MAX),
Text NVARCHAR(MAX)
)

INSERT @Result
EXEC sys.xp_readerrorlog 0, 1, 'System Manufacturer', 'Virtual'

IF EXISTS (SELECT * FROM @Result)
SELECT 'Virtual' AS Msg
ELSE
SELECT 'Physical' AS Msg

Peso
2009-11-12
re: How to tell if you are running on a virtual environment
Of course!
It is always good to have more identifiers.

This is a script I use a consultant when dealing with customer database server.
I like to get the whole picture first when performance monitoring,

Scott R.
2009-11-13
re: How to tell if you are running on a virtual environment
I am not able to get successful results using the original script above on VMware VMs. No rows are returned from the xp_readerrorlog within the script, so it thinks is is not running VMware when it really is.

Can you provide an example of the message you are able to find in the SQL Server error log, given the provided text search criteria?

I tested against a VMware VM using SQL 2005. Are there any SQL version dependencies?


Thanks,

Scott R.

Peso
2009-11-13
re: How to tell if you are running on a virtual environment
I get this record when omitting second search criteria, on my laptop.

EXEC sys.xp_readerrorlog 0, 1, 'System Manufacturer'


System Manufacturer: 'Hewlett-Packard', System Model: 'HP Compaq 6735b'.

Peso
2009-11-13
re: How to tell if you are running on a virtual environment
And yes, this is for SQL Server 2008 and later.

eyechart
2009-11-15
re: How to tell if you are running on a virtual environment
@@VERSION now also indicates if a system is a VM. This also only works on SQL 2008.

on a physical host:
Microsoft SQL Server 2008 (SP1) - 10.0.2734.0 (X64) Sep 11 2009 14:30:58 Copyright (c) 1988-2008 Microsoft Corporation Enterprise Edition (64-bit) on Windows NT 6.0 <X64> (Build 6002: Service Pack 2)

on a VM:
Microsoft SQL Server 2008 (SP1) - 10.0.2734.0 (X64) Sep 11 2009 14:30:58 Copyright (c) 1988-2008 Microsoft Corporation Enterprise Edition (64-bit) on Windows NT 6.0 <X64> (Build 6002: Service Pack 2) (VM)

MP
2009-11-18
re: How to tell if you are running on a virtual environment
I agree w/ <Scott R.>'s comment, and since xp_readerrorlog 0,1 is reading from the SQL error log, whose initial lines seem to be the same as the output of @@VERSION (thanks, <eyechart>), <peso>'s comment seems applicable to <peterl>'s post, and <eyechart>'s comment implies that the EXEC call's 3rd parameter should be "VM" :).

For more on the EXEC call, see http://www.sqlteam.com/article/using-xp_readerrorlog-in-sql-server-2005 .

Andreas Terlinden
2009-11-26
re: How to tell if you are running on a virtual environment
Virtual Box String:
System Manufacturer: 'innotek GmbH', System Model: 'VirtualBox'.

SQL 2008 R2 CTP in a Virtual Box @@Version:
Microsoft SQL Server 2008 R2 (CTP) - 10.50.1352.12 (X64) Oct 30 2009 18:06:48 Copyright (c) Microsoft Corporation Enterprise Evaluation Edition (64-bit) on Windows NT 6.0 <X64> (Build 6001: Service Pack 1)

^^^ no VM inside

Andy
2010-01-27
re: How to tell if you are running on a virtual environment
thanks

BrAun CoRporatIon
2011-02-22
re: How to tell if you are running on a virtual environment
If you got the result "System Manufacturer: 'Hewlett-Packard', System Model: 'HP Compaq 6735b'" then I would say that server is not running on a virtual environment. When I ran this snippit of code I found that vmware always positively identified itself. This sounds like a real server