Module: Facter::Util::Virtual

### Home Classes Methods

### In Files

- lib/facter/util/virtual.rb

### Methods

- ::hpvm?
- ::jail?
- ::kvm?
- ::kvm_type
- ::lspci
- ::openvz?
- ::openvz_cloudlinux?
- ::openvz_type
- ::ovirt?
- ::read_sysfs_dmi_entries
- ::rhev?
- ::virt_what
- ::virtualbox?
- ::vserver?
- ::vserver_type
- ::xen?
- ::zlinux?
- ::zone?

### Files

- LICENSE
- Rakefile
- THANKS

### Class/Module Index

Quicksearch

- Facter
- Facter::Application
- Facter::Manufacturer
- Facter::Memory
- Facter::NetMask
- Facter::Util
- Facter::Util::CFPropertyList
- Facter::Util::CFPropertyList::CFArray
- Facter::Util::CFPropertyList::CFBoolean
- Facter::Util::CFPropertyList::CFData
- Facter::Util::CFPropertyList::CFDate
- Facter::Util::CFPropertyList::CFDictionary
- Facter::Util::CFPropertyList::CFInteger
- Facter::Util::CFPropertyList::CFReal
- Facter::Util::CFPropertyList::CFString
- Facter::Util::CFPropertyList::CFType
- Facter::Util::CFPropertyList::XML
- Facter::Util::Collection
- Facter::Util::CompositeLoader
- Facter::Util::Config
- Facter::Util::Confine
- Facter::Util::DirectoryLoader
- Facter::Util::DirectoryLoader::NoSuchDirectoryError
- Facter::Util::EC2
- Facter::Util::Fact
- Facter::Util::FileRead
- Facter::Util::IP
- Facter::Util::IP::Windows
- Facter::Util::IP::Windows::Bindings4
- Facter::Util::IP::Windows::Bindings6
- Facter::Util::Loader
- Facter::Util::Macaddress
- Facter::Util::Macaddress::Darwin
- Facter::Util::Macaddress::Windows
- Facter::Util::Macosx
- Facter::Util::MonkeyPatches
- Facter::Util::MonkeyPatches::Lines
- Facter::Util::NothingLoader
- Facter::Util::Parser
- Facter::Util::Parser::Base
- Facter::Util::Parser::JsonParser
- Facter::Util::Parser::KeyValuePairOutputFormat
- Facter::Util::Parser::NothingParser
- Facter::Util::Parser::PowershellParser
- Facter::Util::Parser::ScriptParser
- Facter::Util::Parser::TextParser
- Facter::Util::Parser::YamlParser
- Facter::Util::Processor
- Facter::Util::Registry
- Facter::Util::Resolution
- Facter::Util::SolarisZones
- Facter::Util::Uptime
- Facter::Util::Values
- Facter::Util::Virtual
- Facter::Util::Vlans
- Facter::Util::WMI
- Facter::Util::Xendomains
- Plist
- Plist::Emit
- Plist::Listener
- Plist::PArray
- Plist::PData
- Plist::PDate
- Plist::PDict
- Plist::PFalse
- Plist::PInteger
- Plist::PKey
- Plist::PList
- Plist::PReal
- Plist::PString
- Plist::PTag
- Plist::PTrue
- Plist::StreamParser
- CFFormatError
- CFPlistError
- CFTypeError
- IO
- Object
- String

No matching classes.

# Facter::Util::Virtual

### Public Class Methods

hpvm?() click to toggle source

    # File lib/facter/util/virtual.rb, line 129
    def self.hpvm?
      Facter::Util::Resolution.exec("/usr/bin/getconf MACHINE_MODEL").chomp =~ /Virtual Machine/
    end

jail?() click to toggle source

    # File lib/facter/util/virtual.rb, line 121
    def self.jail?
      path = case Facter.value(:kernel)
        when "FreeBSD" then "/sbin"
        when "GNU/kFreeBSD" then "/bin"
      end
      Facter::Util::Resolution.exec("#{path}/sysctl -n security.jail.jailed") == "1"
    end

kvm?() click to toggle source

    # File lib/facter/util/virtual.rb, line 91
    def self.kvm?
       txt = if FileTest.exists?("/proc/cpuinfo")
         File.read("/proc/cpuinfo")
       elsif ["FreeBSD", "OpenBSD"].include? Facter.value(:kernel)
         Facter::Util::Resolution.exec("/sbin/sysctl -n hw.model")
       end
       (txt =~ /QEMU Virtual CPU/) ? true : false
    end

kvm_type() click to toggle source

    # File lib/facter/util/virtual.rb, line 104
    def self.kvm_type
      # TODO Tell the difference between kvm and qemu
      # Can't work out a way to do this at the moment that doesn't
      # require a special binary
      if self.kvm?
        "kvm"
      end
    end

lspci(command = "lspci 2>/dev/null") click to toggle source

lspci is a delegating helper method intended to make it easier to stub the system call without affecting other calls to Facter::Util::Resolution.exec

    # File lib/facter/util/virtual.rb, line 30
    def self.lspci(command = "lspci 2>/dev/null")
      Facter::Util::Resolution.exec command
    end

openvz?() click to toggle source

    # File lib/facter/util/virtual.rb, line 34
    def self.openvz?
      FileTest.directory?("/proc/vz") and not self.openvz_cloudlinux?
    end

openvz_cloudlinux?() click to toggle source

Cloudlinux uses OpenVZ to a degree, but always has an empty /proc/vz/ and has /proc/lve/list present

    # File lib/facter/util/virtual.rb, line 53
    def self.openvz_cloudlinux?
      FileTest.file?("/proc/lve/list") or Dir.glob('/proc/vz/*').empty?
    end

openvz_type() click to toggle source

So one can either have #6728 work on OpenVZ or Cloudlinux. Whoo.

    # File lib/facter/util/virtual.rb, line 39
    def self.openvz_type
      return false unless self.openvz?
      return false unless FileTest.exists?( '/proc/self/status' )

      envid = Facter::Util::Resolution.exec( 'grep "envID" /proc/self/status' )
      if envid =~ /^envID:\s+0$/
      return 'openvzhn'
      elsif envid =~ /^envID:\s+(\d+)$/
      return 'openvzve'
      end
    end

ovirt?() click to toggle source

    # File lib/facter/util/virtual.rb, line 117
    def self.ovirt?
      File.read("/sys/devices/virtual/dmi/id/product_name") =~ /oVirt Node/ rescue false
    end

read_sysfs_dmi_entries(path="/sys/firmware/dmi/entries/1-0/raw") click to toggle source

read_sysfs Reads the raw data as per the documentation at [Detecting if You Are Running in Google Compute Engine](developers.google.com/compute/docs/instances#dmi) This method is intended to provide an easy seam to mock.

@api public

@return [String] or nil if the path does not exist

    # File lib/facter/util/virtual.rb, line 146
    def self.read_sysfs_dmi_entries(path="/sys/firmware/dmi/entries/1-0/raw")
      if File.exists?(path)
        Facter::Util::FileRead.read_binary(path)
      end
    end

rhev?() click to toggle source

    # File lib/facter/util/virtual.rb, line 113
    def self.rhev?
      File.read("/sys/devices/virtual/dmi/id/product_name") =~ /RHEV Hypervisor/ rescue false
    end

virt_what(command = "virt-what") click to toggle source

virt_what is a delegating helper method intended to make it easier to stub the system call without affecting other calls to Facter::Util::Resolution.exec

Per bugzilla.redhat.com/show_bug.cgi?id=719611 when run as a non-root user the virt-what command may emit an error message on stdout, and later versions of virt-what may emit this message on stderr. This method ensures stderr is redirected and that error messages are stripped from stdout.

    # File lib/facter/util/virtual.rb, line 14
    def self.virt_what(command = "virt-what")
      command = Facter::Util::Resolution.which(command)
      return unless command

      if Facter.value(:kernel) == 'windows'
        redirected_cmd = "#{command} 2>NUL"
      else
        redirected_cmd = "#{command} 2>/dev/null"
      end
      output = Facter::Util::Resolution.exec redirected_cmd
      output.gsub(/^virt-what: .*$/, '') if output
    end

virtualbox?() click to toggle source

    # File lib/facter/util/virtual.rb, line 100
    def self.virtualbox?
      File.read("/sys/devices/virtual/dmi/id/product_name") =~ /VirtualBox/ rescue false
    end

vserver?() click to toggle source

    # File lib/facter/util/virtual.rb, line 64
    def self.vserver?
      return false unless FileTest.exists?("/proc/self/status")
      txt = File.open("/proc/self/status", "rb").read
      if txt.respond_to?(:encode!)
        txt.encode!('UTF-16', 'UTF-8', :invalid => :replace)
        txt.encode!('UTF-8', 'UTF-16')
      end
      return true if txt =~ /^(s_context|VxID):[[:blank:]]*[0-9]/
      return false
    end

vserver_type() click to toggle source

    # File lib/facter/util/virtual.rb, line 75
    def self.vserver_type
      if self.vserver?
        if FileTest.exists?("/proc/virtual")
          "vserver_host"
        else
          "vserver"
        end
      end
    end

xen?() click to toggle source

    # File lib/facter/util/virtual.rb, line 85
    def self.xen?
      ["/proc/sys/xen", "/sys/bus/xen", "/proc/xen" ].detect do |f|
        FileTest.exists?(f)
      end
    end

zlinux?() click to toggle source

    # File lib/facter/util/virtual.rb, line 133
    def self.zlinux?
      "zlinux"
    end

zone?() click to toggle source

    # File lib/facter/util/virtual.rb, line 57
    def self.zone?
      return true if FileTest.directory?("/.SUNWnative")
      z = Facter::Util::Resolution.exec("/sbin/zonename")
      return false unless z
      return z.chomp != 'global'
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.