class Cangallo::Qcow2

Attributes

path[R]

Public Class Methods

create(image, parent=nil, size=nil) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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
new(path=nil) click to toggle source
# File lib/cangallo/qcow2.rb, line 48
def initialize(path=nil)
  @path=path
end
qemu_img() click to toggle source
# File lib/cangallo/qcow2.rb, line 32
def self.qemu_img
  @@qemu_img ||= "qemu-img"
end
qemu_img=(path) click to toggle source
# File lib/cangallo/qcow2.rb, line 28
def self.qemu_img=(path)
  @@qemu_img = path
end
qemu_img_version() click to toggle source
# 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

Public Instance Methods

compress(destination = nil, parent = nil) click to toggle source
# File lib/cangallo/qcow2.rb, line 58
def compress(destination = nil, parent = nil)
  copy(destination, :parent => parent, :compress => true)
end
convert(destination, options = {}) click to toggle source
# 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 = {}) click to toggle source
# 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) click to toggle source
# File lib/cangallo/qcow2.rb, line 137
def execute(command, *params)
  self.class.execute(command, params)
end
info() click to toggle source
# File lib/cangallo/qcow2.rb, line 52
def info
  res = execute :info, '--output=json', @path

  JSON.parse res
end
rebase(new_base) click to toggle source
# File lib/cangallo/qcow2.rb, line 133
def rebase(new_base)
  execute :rebase, '-u', "-b #{new_base}", @path
end
sha(ver = 256) click to toggle source
# 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
sha1() click to toggle source
# File lib/cangallo/qcow2.rb, line 125
def sha1
  sha(1)
end
sha256() click to toggle source
# File lib/cangallo/qcow2.rb, line 129
def sha256
  sha(256)
end
sparsify(destination) click to toggle source
# 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