class Fog::Libvirt::Compute::Volume

Attributes

xml[R]

Public Class Methods

new(attributes={ }) click to toggle source

Can be created by passing in :xml => “<xml to create volume>” A volume always belongs to a pool, :pool_name => “<name of pool>”

Calls superclass method
# File lib/fog/libvirt/models/compute/volume.rb, line 27
def initialize(attributes={ })
  @xml = attributes.delete(:xml)
  super(defaults.merge(attributes))

  # We need a connection to calculate the pool_name
  # This is why we do this after super
  self.pool_name ||= default_pool_name
end

Public Instance Methods

clone(name) click to toggle source

Clones this volume to the name provided

# File lib/fog/libvirt/models/compute/volume.rb, line 56
def clone(name)
  new_volume      = self.dup
  new_volume.key  = nil
  new_volume.name = name
  new_volume.save

  new_volume.reload
end
clone_volume(new_name) click to toggle source
# File lib/fog/libvirt/models/compute/volume.rb, line 65
def clone_volume(new_name)
  requires :pool_name

  new_volume      = self.dup
  new_volume.key  = nil
  new_volume.name = new_name
  new_volume.path = service.clone_volume(pool_name, new_volume.to_xml, self.name).path

  new_volume.reload
end
destroy() click to toggle source

Destroy a volume

# File lib/fog/libvirt/models/compute/volume.rb, line 46
def destroy
  service.volume_action key, :delete
end
save() click to toggle source

Takes a pool and either :xml or other settings

# File lib/fog/libvirt/models/compute/volume.rb, line 37
def save
  requires :pool_name

  raise Fog::Errors::Error.new('Reserving an existing volume may create a duplicate') if key
  @xml ||= to_xml
  self.path = service.create_volume(pool_name, xml).path
end
upload_image(file_path) click to toggle source
# File lib/fog/libvirt/models/compute/volume.rb, line 76
def upload_image(file_path)
  requires :pool_name
  service.upload_volume(pool_name, name, file_path)
end
wipe() click to toggle source

Wipes a volume , zeroes disk

# File lib/fog/libvirt/models/compute/volume.rb, line 51
def wipe
  service.volume_action key, :wipe
end

Private Instance Methods

default_pool_name() click to toggle source

Try to guess the default/first pool of no pool_name was specified

# File lib/fog/libvirt/models/compute/volume.rb, line 93
def default_pool_name
  name = "default"
  return name unless (service.pools.all(:name => name)).empty?

  # we default to the first pool we find.
  first_pool = service.pools.first

  raise Fog::Errors::Error.new('No storage pools are defined') unless first_pool
  first_pool.name
end
defaults() click to toggle source
# File lib/fog/libvirt/models/compute/volume.rb, line 104
def defaults
  {
    :persistent  => true,
    :format_type => "raw",
    :name        => randomized_name,
    :capacity    => "10G",
    :allocation  => "1G",
    :owner       => nil,
    :group       => nil,
  }
end
image_suffix() click to toggle source
# File lib/fog/libvirt/models/compute/volume.rb, line 83
def image_suffix
  return "img" if format_type == "raw"
  format_type
end
randominzed_name() click to toggle source
# File lib/fog/libvirt/models/compute/volume.rb, line 88
def randominzed_name
  "#{super}.#{image_suffix}"
end
split_size_unit(text) click to toggle source
# File lib/fog/libvirt/models/compute/volume.rb, line 116
def split_size_unit(text)
  if (text.kind_of? String) && (matcher = text.match(/(\d+)(.+)/))
    size    = matcher[1]
    unit    = matcher[2]
  else
    size    = text.to_i
    unit    = "G"
  end
  [size, unit]
end