git.diff

Joe McDonagh, 06/18/2009 01:41 am

Download (4.6 kB)

b/lib/facter/virtual.rb
1
require 'facter/util/virtual'
2

  
3 1
Facter.add("virtual") do
4 2
    confine :kernel => %w{Linux FreeBSD OpenBSD SunOS}
5 3

  
6 4
    result = "physical"
7 5

  
8 6
    setcode do
7
    require 'thread'
9 8

  
10
        result = "zone" if Facter::Util::Virtual.zone?
9
        if FileTest.exists?("/sbin/zonename")
10
            z = %x{"/sbin/zonename"}.chomp
11
            if z != 'global'
12
                result = zone 
13
            end
14
        end
11 15

  
12
        if Facter::Util::Virtual.openvz?
13
            result = Facter::Util::Virtual.openvz_type()
16
        if FileTest.exists?("/proc/vz/veinfo")
17
            if FileTest.exists?("/proc/vz/version")
18
                result = "openvzhn"
19
            else
20
                result = "openvzve"
21
            end
14 22
        end
15 23

  
16
        result = "vserver" if Facter::Util::Virtual.vserver?
24
        if FileTest.exists?("/proc/self/status")
25
            txt = File.read("/proc/self/status")
26
            if txt =~ /^(s_context|VxID):[[:blank:]]*[1-9]/
27
                result = "vserver"
28
            end
29
        end
17 30

  
18 31
        if FileTest.exists?("/proc/virtual")
19 32
            result = "vserver_host"
......
21 34

  
22 35
        # new Xen domains have this in dom0 not domu :(
23 36
        if FileTest.exists?("/proc/sys/xen/independent_wallclock")
24
            result = "xenu"
37
            result = "xenu" 
25 38
        end
26 39
        if FileTest.exists?("/sys/bus/xen")
27
            result = "xenu"
40
            result = "xenu" 
28 41
        end
29 42
        
30 43
        if FileTest.exists?("/proc/xen/capabilities")
31 44
            txt = File.read("/proc/xen/capabilities")
32 45
            if txt =~ /control_d/i
33
                result = "xen0"
46
                result = "xen0" 
34 47
            end
35 48
        end
36 49

  
37
        if result == "physical"
38
            output = Facter::Util::Resolution.exec('lspci')
39
            if not output.nil?
40
                output.each_line do |p|
41
                    # --- look for the vmware video card to determine if it is virtual => vmware.
42
                    # ---     00:0f.0 VGA compatible controller: VMware Inc [VMware SVGA II] PCI Display Adapter
43
                    result = "vmware" if p =~ /VM[wW]are/
44
                end
45
            else
46
                output = Facter::Util::Resolution.exec('dmidecode')
47
                if not output.nil?
48
                    output.each_line do |pd|
49
                        result = "vmware" if pd =~ /VMware|Parallels/
50
                    end
51
                else
52
                    output = Facter::Util::Resolution.exec('prtdiag')
53
                    if not output.nil?
54
                        output.each_line do |pd|
55
                            result = "vmware" if pd =~ /VMware|Parallels/
56
                        end
57
                    end
58
                end
59
            end
60
        end
61

  
62
        # VMware server 1.0.3 rpm places vmware-vmx in this place, other versions or platforms may not.
63
        if FileTest.exists?("/usr/lib/vmware/bin/vmware-vmx")
64
            result = "vmware_server"
65
        end
66

  
67
        result
68
    end
50
        if Facter.value(:manufacturer) =~ /vmware/i 
51
            result = "vmware" 
52
        end 
53
 
54
        if result == "physical" 
55
            commands = [ 
56
                'lspci', 
57
                'dmidecode', 
58
                'prtdiag', 
59
                'vmware -v | awk \'{ print $1"_"$2 }\' | tr [:upper:] [:lower:]' 
60
            ] 
61
 
62
            virt_platforms = [ 
63
                'vmware', 
64
                'vmware_server', 
65
                'vmware_workstation' 
66
            ] 
67
 
68
            catch "ResultFound" do 
69
                commands.each do |command| 
70
                    output = Facter::Util::Resolution.exec(command) 
71
                    if not output.nil? 
72
                        output.each_line do |p| 
73
                            virt_platforms.each do |vp| 
74
                                if vp =~ /#{p}/i 
75
                                    result = vp  
76
                                    throw "ResultFound" 
77
                                end 
78
                            end 
79
                        end 
80
                    end 
81
                end 
82
            end 
83
        end 
84
 
85
        result 
86
    end 
69 87
end
70
  
88

  
71 89
Facter.add("is_virtual") do
72 90
    confine :kernel => %w{Linux FreeBSD OpenBSD SunOS}
73 91

  
74 92
    setcode do
75 93
        case Facter.value(:virtual)
76
        when "xenu", "openvzve", "vmware"
94
        when "xenu", "openvzve", "vmware" 
77 95
            true
78 96
        else 
79 97
            false