Class: Cangallo::Qcow2

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = nil) ⇒ Qcow2

Returns a new instance of Qcow2.



48
49
50
# File 'lib/cangallo/qcow2.rb', line 48

def initialize(path=nil)
  @path=path
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



26
27
28
# File 'lib/cangallo/qcow2.rb', line 26

def path
  @path
end

Class Method Details

.create(image, parent = nil, size = nil) ⇒ Object



161
162
163
164
165
166
167
168
# File 'lib/cangallo/qcow2.rb', line 161

def self.create(image, parent=nil, size=nil)
  cmd = [:create, '-f qcow2']
  cmd << "-o backing_file=#{parent}" if parent
  cmd << image
  cmd << size if size

  execute(*cmd)
end

.create_from_base(origin, destination, size = nil) ⇒ Object



154
155
156
157
158
159
# File 'lib/cangallo/qcow2.rb', line 154

def self.create_from_base(origin, destination, size=nil)
  cmd = [:create, '-f qcow2', "-o backing_file=#{origin}", destination]
  cmd << size if size

  execute(*cmd)
end

.execute(command, *params) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/cangallo/qcow2.rb', line 141

def self.execute(command, *params)
  command = "#{qemu_img} #{command} #{params.join(' ')}"
  STDERR.puts command

  status, stdout, stderr = systemu command

  if status.success?
    stdout
  else
    raise stderr
  end
end

.qemu_imgObject



32
33
34
# File 'lib/cangallo/qcow2.rb', line 32

def self.qemu_img
  @@qemu_img ||= "qemu-img"
end

.qemu_img=(path) ⇒ Object



28
29
30
# File 'lib/cangallo/qcow2.rb', line 28

def self.qemu_img=(path)
  @@qemu_img = path
end

.qemu_img_versionObject



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/cangallo/qcow2.rb', line 36

def self.qemu_img_version
  text = execute("--version")

  m = text.match(/^qemu-img version (\d+\.\d+\.\d+)/)

  if m
    m[1]
  else
    nil
  end
end

Instance Method Details

#compress(destination = nil, parent = nil) ⇒ Object



58
59
60
# File 'lib/cangallo/qcow2.rb', line 58

def compress(destination = nil, parent = nil)
  copy(destination, :parent => parent, :compress => true)
end

#convert(destination, options = {}) ⇒ Object



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

def convert(destination, options = {})
  command = [:convert]
  command << '-c' if options[:compress]
  command << "-O #{options[:format]}" if options[:format]
  command += [@path, destination]

  execute(*command)
end

#copy(destination = nil, options = {}) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/cangallo/qcow2.rb', line 71

def copy(destination = nil, options = {})
  ops = {
    :parent => nil,
    :compress => true,
    :only_copy => false
  }.merge(options)

  parent = ops[:parent]

  new_path = destination || @path + '.compressed'

  command = [:convert, "-p", "-O qcow2"]
  #command = ["convert", "-p", "-O qcow2"]
  command << '-c' if ops[:compress]
  command << "-o backing_file=#{parent}" if parent
  command += [@path, new_path]

  if ops[:only_copy]
    FileUtils.cp(@path, new_path)
  else
    execute *command
  end

  # pp command
  # system(*command)

  if !destination
    begin
      File.rm @path
      File.mv new_path, @path
    ensure
      File.rm new_path if File.exist? new_path
    end
  else
    @path = new_path
  end
end

#execute(command, *params) ⇒ Object



137
138
139
# File 'lib/cangallo/qcow2.rb', line 137

def execute(command, *params)
  self.class.execute(command, params)
end

#infoObject



52
53
54
55
56
# File 'lib/cangallo/qcow2.rb', line 52

def info
  res = execute :info, '--output=json', @path

  JSON.parse res
end

#rebase(new_base) ⇒ Object



133
134
135
# File 'lib/cangallo/qcow2.rb', line 133

def rebase(new_base)
  execute :rebase, '-u', "-b #{new_base}", @path
end

#sha(ver = 256) ⇒ Object



119
120
121
122
123
# File 'lib/cangallo/qcow2.rb', line 119

def sha(ver = 256)
  command = "guestfish --progress-bars --ro -a #{@path} " <<
            "run : checksum-device sha#{ver} /dev/sda"
  %x{#{command}}.strip
end

#sha1Object



125
126
127
# File 'lib/cangallo/qcow2.rb', line 125

def sha1
  sha(1)
end

#sha256Object



129
130
131
# File 'lib/cangallo/qcow2.rb', line 129

def sha256
  sha(256)
end

#sparsify(destination) ⇒ Object



109
110
111
112
113
114
115
116
117
# File 'lib/cangallo/qcow2.rb', line 109

def sparsify(destination)
  parent = info['backing_file']
  parent_options = ''

  parent_options = "-o backing_file=#{parent}" if parent

  command = "TMPDIR=#{File.dirname(destination)} virt-sparsify #{parent_options} #{@path} #{destination}"
  status, stdout, stderr = systemu command
end