Class: QEMU::Image

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, options = {}) ⇒ Image

Returns a new instance of Image.



6
7
8
9
10
# File 'lib/qemu/image.rb', line 6

def initialize(file, options = {})
  self.file = file
  options = { :format => self.class.file_format(file) }.merge options
  options.each { |k,v| send "#{k}=", v }
end

Instance Attribute Details

#fileObject

Returns the value of attribute file.



4
5
6
# File 'lib/qemu/image.rb', line 4

def file
  @file
end

#formatObject

Returns the value of attribute format.



4
5
6
# File 'lib/qemu/image.rb', line 4

def format
  @format
end

#optionsObject

Returns the value of attribute options.



4
5
6
# File 'lib/qemu/image.rb', line 4

def options
  @options
end

#sizeObject

Returns the value of attribute size.



4
5
6
# File 'lib/qemu/image.rb', line 4

def size
  @size
end

Class Method Details

.file_format(file) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/qemu/image.rb', line 12

def self.file_format(file)
  case `file #{file}`
  when /qcow/
    "qcow2"
  when /x86 boot sector/
    "raw"
  else
    QEMU.logger.info "Unknown file format : #{file}"
    "raw"
  end
end

.output_file(file, format) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/qemu/image.rb', line 39

def self.output_file(file, format)
  unless file.to_s.end_with? ".#{format}"
    "#{file}.#{format}"
  else
    file
  end
end

Instance Method Details

#convert(format, output_file = nil) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/qemu/image.rb', line 47

def convert(format, output_file = nil)
  output_file ||= self.class.output_file file, format
  unless self.format.to_s == format.to_s
    QEMU.sh! "qemu-img convert -O #{format} #{file} #{output_file}"
  end
  output_file
end

#createObject



31
32
33
# File 'lib/qemu/image.rb', line 31

def create
  QEMU.sh! "qemu-img create -f #{format} #{options_arguments} #{file} #{size}"
end

#exists?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/qemu/image.rb', line 35

def exists?
  File.exists? file
end

#options_argumentsObject



24
25
26
27
28
29
# File 'lib/qemu/image.rb', line 24

def options_arguments
  if options
    options_value = options.map { |k,v| "#{k}=#{v}" }.join(",")
    "-o #{options_value}"
  end
end