Thinking outside the box

Patron Saint of Lost Yaks
posts - 203, comments - 734, trackbacks - 4

My Links

Advertisement

News

Archives

Post Categories

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.

Print | posted on Thursday, November 12, 2009 3:27 PM | Filed Under [ SQL Server 2008 Administration SQL Server 2005 ]

Feedback

Gravatar

# 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
11/12/2009 5:35 PM | Buck Woody
Gravatar

# 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,
11/12/2009 8:02 PM | Peso
Gravatar

# 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.
11/13/2009 5:02 PM | Scott R.
Gravatar

# 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'.
11/13/2009 6:53 PM | Peso
Gravatar

# re: How to tell if you are running on a virtual environment

And yes, this is for SQL Server 2008 and later.
11/13/2009 6:59 PM | Peso
Gravatar

# 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)
11/15/2009 4:52 AM | eyechart
Gravatar

# 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 .
11/18/2009 5:33 PM | MP
Gravatar

# 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
11/26/2009 4:37 PM | Andreas Terlinden
Gravatar

# re: How to tell if you are running on a virtual environment

thanks
1/27/2010 10:12 AM | Andy
Gravatar

# 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
2/22/2011 4:58 AM | BrAun CoRporatIon
Comments have been closed on this topic.

Powered by:
Powered By Subtext Powered By ASP.NET