~gandelman-a/ubuntu/precise/facter/merge922788

« back to all changes in this revision

Viewing changes to lib/facter/virtual.rb

  • Committer: Package Import Robot
  • Author(s): Adam Gandelman
  • Date: 2011-10-18 10:32:42 UTC
  • mfrom: (1.3.3 upstream)
  • mto: This revision was merged to the branch mainline in revision 16.
  • Revision ID: package-import@ubuntu.com-20111018103242-ag8i8vejfp8v7b1b
Tags: upstream-1.6.1
ImportĀ upstreamĀ versionĀ 1.6.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Fact: virtual
 
2
#
 
3
# Purpose: Determine if the system's hardware is real or virtualised.
 
4
#
 
5
# Resolution:
 
6
#   Assumes physical unless proven otherwise.
 
7
#
 
8
#   On Darwin, use the macosx util module to acquire the SPDisplaysDataType,
 
9
#   from that parse it to see if it's VMWare or Parallels pretending to be the
 
10
#   display.
 
11
#
 
12
#   On Linux, BSD, Solaris and HPUX:
 
13
#     Much of the logic here is obscured behind util/virtual.rb, which rather
 
14
#     than document here, which would encourage drift, just refer to it.
 
15
#   The Xen tests in here rely on /sys and /proc, and check for the presence and
 
16
#   contents of files in there.
 
17
#   If after all the other tests, it's still seen as physical, then it tries to
 
18
#   parse the output of the "lspci", "dmidecode" and "prtdiag" and parses them
 
19
#   for obvious signs of being under VMWare or Parallels.
 
20
#   Finally it checks for the existence of vmware-vmx, which would hint it's
 
21
#   VMWare.
 
22
#
 
23
# Caveats:
 
24
#   Virtualbox detection isn't implemented. 
 
25
#   Many checks rely purely on existence of files.
 
26
#
 
27
 
1
28
require 'facter/util/virtual'
2
29
 
3
30
Facter.add("virtual") do
25
52
 
26
53
    setcode do
27
54
 
28
 
        if Facter::Util::Virtual.zone? and Facter.value(:operatingsystem) == "Solaris"
 
55
        if Facter.value(:operatingsystem) == "Solaris" and Facter::Util::Virtual.zone?
29
56
            result = "zone"
30
57
        end
31
58
 
77
104
                    # --- look for the vmware video card to determine if it is virtual => vmware.
78
105
                    # ---     00:0f.0 VGA compatible controller: VMware Inc [VMware SVGA II] PCI Display Adapter
79
106
                    result = "vmware" if p =~ /VM[wW]are/
 
107
                    # --- look for virtualbox video card to determine if it is virtual => virtualbox.
 
108
                    # ---     00:02.0 VGA compatible controller: InnoTek Systemberatung GmbH VirtualBox Graphics Adapter
 
109
                    result = "virtualbox" if p =~ /VirtualBox/
80
110
                    # --- look for pci vendor id used by Parallels video card
81
111
                    # ---   01:00.0 VGA compatible controller: Unknown device 1ab8:4005
82
112
                    result = "parallels" if p =~ /1ab8:|[Pp]arallels/
 
113
                    # --- look for pci vendor id used by Xen HVM device
 
114
                    # ---   00:03.0 Unassigned class [ff80]: XenSource, Inc. Xen Platform Device (rev 01)
 
115
                    result = "xenhvm" if p =~ /XenSource/
83
116
                end
84
117
            else
85
118
                output = Facter::Util::Resolution.exec('dmidecode')
87
120
                    output.each_line do |pd|
88
121
                        result = "parallels" if pd =~ /Parallels/
89
122
                        result = "vmware" if pd =~ /VMware/
 
123
                        result = "virtualbox" if pd =~ /VirtualBox/
 
124
                        result = "xenhvm" if pd =~ /HVM domU/
90
125
                    end
91
 
                else
92
 
                    output = Facter::Util::Resolution.exec('prtdiag')
 
126
                elsif Facter.value(:kernel) == 'SunOS'
 
127
                    res = Facter::Util::Resolution.new('prtdiag')
 
128
                    res.timeout = 6
 
129
                    res.setcode('prtdiag')
 
130
                    output = res.value
93
131
                    if not output.nil?
94
132
                        output.each_line do |pd|
95
133
                            result = "parallels" if pd =~ /Parallels/
96
134
                            result = "vmware" if pd =~ /VMware/
 
135
                                                        result = "virtualbox" if pd =~ /VirtualBox/
 
136
                            result = "xenhvm" if pd =~ /HVM domU/
97
137
                        end
98
138
                    end
99
139
                end
100
140
            end
101
141
            
102
 
            if FileTest.exists?("/usr/lib/vmware/bin/vmware-vmx")
103
 
                result = "vmware_server"
 
142
            if output = Facter::Util::Resolution.exec("vmware -v")
 
143
                result = output.sub(/(\S+)\s+(\S+).*/) { | text | "#{$1}_#{$2}"}.downcase
104
144
            end
105
145
        end
106
146
 
108
148
    end
109
149
end
110
150
 
 
151
# Fact: is_virtual
 
152
#
 
153
# Purpose: returning true or false for if a machine is virtualised or not.
 
154
#
 
155
# Resolution: Hypervisors and the like may be detected as a virtual type, but
 
156
# are not actual virtual machines, or should not be treated as such. This 
 
157
# determines if the host is actually virtualized.
 
158
#
 
159
# Caveats:
 
160
#
 
161
 
111
162
Facter.add("is_virtual") do
112
163
    confine :kernel => %w{Linux FreeBSD OpenBSD SunOS HP-UX Darwin GNU/kFreeBSD}
113
164
 
114
165
    setcode do
115
 
        if Facter.value(:virtual) != "physical" && Facter.value(:virtual) != "xen0"
 
166
        physical_types = %w{physical xen0 vmware_server vmware_workstation openvzhn}
 
167
 
 
168
        if physical_types.include? Facter.value(:virtual)
 
169
            "false"
 
170
        else
116
171
            "true"
117
 
        else
118
 
            "false"
119
172
        end
120
173
    end
121
174
end