Class: DiskID::Inspector

Inherits:
Object
  • Object
show all
Defined in:
lib/diskid.rb

Instance Method Summary collapse

Constructor Details

#initialize(file, qemu_img_bin = 'qemu-img-bleeding') ⇒ Inspector

Returns a new instance of Inspector.

Raises:

  • (ArgumentError)


62
63
64
65
66
67
68
69
70
# File 'lib/diskid.rb', line 62

def initialize(file, qemu_img_bin='qemu-img-bleeding')
  @qemu_img_bin = (qemu_img_bin || '').strip.chomp
  if not @qemu_img_bin =~ /^\//
    @qemu_img_bin = `sh -c 'command -v #{@qemu_img_bin}'`.strip.chomp
  end
  raise ArgumentError.new("qemu-img command not found: #{@qemu_img_bin}") if @qemu_img_bin.empty?

  @file = file
end

Instance Method Details

#identifyObject

Raises:

  • (Exception)


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/diskid.rb', line 72

def identify 
  out = `#{@qemu_img_bin} info #{@file}`
  raise Exception.new("Error getting info from #{@file}") if $? != 0

  md = /image:(.*)$\nfile format:(.*)$\nvirtual size:(.*)$\ndisk size:(.*)\n/.match out
  @file_name,@format,@virtual_size,@disk_size = [md[1], md[2], md[3], md[4]].map { |e| e.strip.chomp }
  @virtual_size = @virtual_size.split()[0]
  variant = ''
  if @format == 'vmdk'
    variant = read_variant @file
  end
  DiskInfo.new({
    'file_name' =>  @file_name,
    'format' => @format,
    'virtual_size' => @virtual_size,
    'disk_size' => @disk_size,
    'variant' => variant
  })
end