Class: Libvirt::Spec::Device::Disk

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/libvirt/spec/device/disk.rb

Overview

Any device that looks like a disk, be it a floppy, harddisk, cdrom, or paravirtualized driver is specified via the disk element.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

#raise_if_unparseables, #try

Constructor Details

#initialize(xml = nil) ⇒ Disk

Initializes a new disk element. If an XML string is passed then that will be used to initialize the attributes of the device.



24
25
26
27
28
# File 'lib/libvirt/spec/device/disk.rb', line 24

def initialize(xml=nil)
  @shareable = false

  load!(xml) if xml
end

Instance Attribute Details

#deviceObject

Returns the value of attribute device.



11
12
13
# File 'lib/libvirt/spec/device/disk.rb', line 11

def device
  @device
end

#driverObject

Returns the value of attribute driver.



15
16
17
# File 'lib/libvirt/spec/device/disk.rb', line 15

def driver
  @driver
end

#driver_cacheObject

Returns the value of attribute driver_cache.



17
18
19
# File 'lib/libvirt/spec/device/disk.rb', line 17

def driver_cache
  @driver_cache
end

#driver_typeObject

Returns the value of attribute driver_type.



16
17
18
# File 'lib/libvirt/spec/device/disk.rb', line 16

def driver_type
  @driver_type
end

#serialObject

Returns the value of attribute serial.



19
20
21
# File 'lib/libvirt/spec/device/disk.rb', line 19

def serial
  @serial
end

#shareableObject

Returns the value of attribute shareable.



18
19
20
# File 'lib/libvirt/spec/device/disk.rb', line 18

def shareable
  @shareable
end

#sourceObject

Returns the value of attribute source.



12
13
14
# File 'lib/libvirt/spec/device/disk.rb', line 12

def source
  @source
end

#target_busObject

Returns the value of attribute target_bus.



14
15
16
# File 'lib/libvirt/spec/device/disk.rb', line 14

def target_bus
  @target_bus
end

#target_devObject

Returns the value of attribute target_dev.



13
14
15
# File 'lib/libvirt/spec/device/disk.rb', line 13

def target_dev
  @target_dev
end

#typeObject

Returns the value of attribute type.



10
11
12
# File 'lib/libvirt/spec/device/disk.rb', line 10

def type
  @type
end

Instance Method Details

#load!(xml) ⇒ Object

Loads data from XML.



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/libvirt/spec/device/disk.rb', line 31

def load!(xml)
  xml = Nokogiri::XML(xml).root if !xml.is_a?(Nokogiri::XML::Element)
  try(xml.xpath("//disk[@type]"), :preserve => true) { |result| self.type = result["type"].to_sym }
  try(xml.xpath("//disk[@device]"), :preserve => true) { |result| self.device = result["device"].to_sym }
  try(xml.xpath("//disk/source")) { |result| self.source = result["dev"] || result["file"] }

  try(xml.xpath("//disk/target")) do |result|
    self.target_dev = result["dev"]
    self.target_bus = result["bus"]
  end

  raise_if_unparseables(xml.xpath("//disk/*"))
end

#to_xml(xml = Nokogiri::XML::Builder.new) ⇒ Object

Returns the XML representation of this device.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/libvirt/spec/device/disk.rb', line 46

def to_xml(xml=Nokogiri::XML::Builder.new)
  xml.disk(:type => type, :device => device) do |d|
    if source
      # Source tag, the attribute depends on the type.
      attribute = type == :block ? :dev : :file
      d.source(attribute => source)
    end

    if target_dev
      # Target tag has optional "bus" parameter
      options = { :dev => target_dev }
      options[:bus] = target_bus if target_bus
      d.target(options)
    end

    if driver
      # Driver tag has a couple optional parameters
      options = { :name => driver }
      options[:type] = driver_type if driver_type
      options[:cache] = driver_cache if driver_cache
      d.driver(options)
    end

    d.shareable if shareable
    d.serial serial if serial
  end

  xml.to_xml
end