Module: VMWareDiskProbe

Defined in:
lib/disk/modules/VMWareDiskProbe.rb

Constant Summary collapse

SPARSE_MAGIC =
"KDMV"
SPARSE_MOD =
"VMWareSparseDisk"
COWD_MAGIC =
"COWD"
COWD_MOD =
"VMWareCowdDisk"
DESC_MOD =
"VMWareDescriptor"

Class Method Summary collapse

Class Method Details

.binary_data?(str) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
73
# File 'lib/disk/modules/VMWareDiskProbe.rb', line 70

def self.binary_data?(str)
  return false if str.nil? || str.empty?
  return (str.count("^ -~", "^\r\n") / str.size > 0.3 || str.count("\x00") > 0) unless str.empty?
end

.getDescriptor(ofs, siz, ostruct) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'lib/disk/modules/VMWareDiskProbe.rb', line 59

def self.getDescriptor(ofs, siz, ostruct)
  f = File.open(ostruct.fileName, "rb")
  f.seek(ofs * 512, IO::SEEK_SET)
  desc = f.read(siz * 512)
  f.close

  pos  = desc.index("\000")
  desc = desc[0...pos] unless pos.nil?
  ostruct.Descriptor = desc
end

.probe(ostruct) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/disk/modules/VMWareDiskProbe.rb', line 10

def self.probe(ostruct)
  return nil unless ostruct.fileName
  # If not .vmdk then not VMWare.
  # Allow .miq also.
  ext = File.extname(ostruct.fileName).downcase
  return nil if ext != ".vmdk" && ext != ".miq"

  size  = File.size(ostruct.fileName)
  f     = File.new(ostruct.fileName, "rb")
  magic = f.read(4)

  if magic == SPARSE_MAGIC
    # get descriptor offset & size.
    f.seek(24, IO::SEEK_CUR)
    ofs = f.read(8).unpack("Q")[0]
    siz = f.read(8).unpack("Q")[0]
  end

  binary = false
  if (magic != SPARSE_MAGIC) && (magic != COWD_MAGIC)
    f.rewind
    loop do
      data   = f.read(4096)
      break if data.nil?
      binary = binary_data?(data)
      break if binary
    end
  end

  f.close

  return COWD_MOD if magic == COWD_MAGIC

  if magic == SPARSE_MAGIC
    # If this ostruct already has a descriptor don't bother checking.
    # NOTE: If it does have a descriptor, we're coming from VMWareDescriptor.rb
    #       trying to open a disk - so don't regress infinitely.
    if ostruct.Descriptor.nil? && ofs > 0
      getDescriptor(ofs, siz, ostruct) if ofs > 0
      return DESC_MOD
    end

    return SPARSE_MOD
  end

  return false if binary
  DESC_MOD
end