Skip to content

Instantly share code, notes, and snippets.

@btm
Forked from b/minimal_virt.rb
Created May 20, 2009 18:09
Show Gist options
  • Save btm/114987 to your computer and use it in GitHub Desktop.
Save btm/114987 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'nokogiri'
require 'json'
minimal = {
'name' => 'minimal_vm',
'memory' => 512,
'hd' => '/var/lib/libvirt/images/fc10-x86-64-xen.img',
'cdrom' => '/home/bb/os_images/Fedora-10-x86_64-DVD.iso',
'boot' => ['hd', 'cdrom']
}
defaults = {
'type' => 'kvm',
'name' => 'default_vm',
'memory' => 256,
'vcpu' => 1,
'boot' => ['hd'],
'network' => {
'source' => 'default'
},
'events' => {
'poweroff' => 'destroy',
'reboot' => 'restart',
'crash' => 'restart'
},
'features' => [
'acpi',
'apic',
'pae'
]
}
def get_next_vnet
0
end
def hash_to_libvirt_domain(hash)
root = Nokogiri::XML::Node.new 'domain', Nokogiri::XML::Document.new
root["type"] = hash["type"]
name_node = Nokogiri::XML::Node.new 'name', Nokogiri::XML::Document.new
name_node.content = hash["name"]
root << name_node
memory_node = Nokogiri::XML::Node.new 'memory', Nokogiri::XML::Document.new
memory_node.content = (hash["memory"] * 1024).to_s
root << memory_node
vcpu_node = Nokogiri::XML::Node.new 'vcpu', Nokogiri::XML::Document.new
vcpu_node.content = hash["vcpu"].to_s
root << vcpu_node
os_node = Nokogiri::XML::Node.new 'os', Nokogiri::XML::Document.new
type_node = Nokogiri::XML::Node.new 'type', Nokogiri::XML::Document.new
type_node.content = "hvm"
os_node << type_node
hash["boot"].each do |boot|
boot_node = Nokogiri::XML::Node.new 'boot', Nokogiri::XML::Document.new
boot_node["dev"] = boot
os_node << boot_node
end
root << os_node
features_node = Nokogiri::XML::Node.new 'features', Nokogiri::XML::Document.new
hash["features"].each do |feature|
features_node << Nokogiri::XML::Node.new(feature, Nokogiri::XML::Document.new)
end
root << features_node
dev_node = Nokogiri::XML::Node.new 'devices', Nokogiri::XML::Document.new
hd_node = Nokogiri::XML::Node.new 'disk', Nokogiri::XML::Document.new
hd_node["device"] = "disk"
if hash["hd"].match(/^\/dev\//)
hd_node["type"] = "block"
target_node = Nokogiri::XML::Node.new 'target', Nokogiri::XML::Document.new
target_node["dev"] = 'hda'; target_node["bus"] = 'ide'
hd_node << target_node
else
hd_node["type"] = "file"
source_node = Nokogiri::XML::Node.new 'source', Nokogiri::XML::Document.new
source_node["file"] = hash["hd"]
hd_node << source_node
target_node = Nokogiri::XML::Node.new 'target', Nokogiri::XML::Document.new
target_node["dev"] = 'vda'; target_node["bus"] = 'virtio'
hd_node << target_node
end
dev_node << hd_node
if hash.has_key?("cdrom")
cd_node = Nokogiri::XML::Node.new 'disk', Nokogiri::XML::Document.new
cd_node["device"] = "cdrom"
if hash["cdrom"].match(/^\/dev\//)
cd_node["type"] = "block"
target_node = Nokogiri::XML::Node.new 'target', Nokogiri::XML::Document.new
target_node["dev"] = 'hdc'; target_node["bus"] = 'ide'
cd_node << target_node
else
cd_node["type"] = "file"
source_node = Nokogiri::XML::Node.new 'source', Nokogiri::XML::Document.new
source_node["file"] = hash["cdrom"]
cd_node << source_node
target_node = Nokogiri::XML::Node.new 'target', Nokogiri::XML::Document.new
target_node["dev"] = 'vdc'; target_node["bus"] = 'virtio'
cd_node << target_node
end
dev_node << cd_node
end
net_node = Nokogiri::XML::Node.new 'interface', Nokogiri::XML::Document.new
net_node["type"] = 'network'
source_node = Nokogiri::XML::Node.new 'source', Nokogiri::XML::Document.new
source_node["network"] = hash["network"]["source"]
net_node << source_node
#target_node = Nokogiri::XML::Node.new 'target', Nokogiri::XML::Document.new
#target_node["dev"] = (hash["network"].has_key?("target") ? hash["network"]["target"] : "vnet {get_next_vnet}")
#net_node << target_node
dev_node << net_node
#<serial type='pty'>
# <source path='/dev/pts/3'/>
# <target port='0'/>
#</serial>
serial_node = Nokogiri::XML::Node.new 'serial', Nokogiri::XML::Document.new
serial_node["type"] = 'pty'
#source_node = Nokogiri::XML::Node.new 'source', Nokogiri::XML::Document.new
#source_node["path"] = '/dev/pts/3'
#serial_node << source_node
dev_node << serial_node
#<console type='pty' tty='/dev/pts/3'>
# <source path='/dev/pts/3'/>
# <target port='0'/>
#</console>
#<input type='mouse' bus='ps2'/>
input_node = Nokogiri::XML::Node.new 'input', Nokogiri::XML::Document.new
input_node["type"] = 'mouse'; input_node["bus"] = 'ps2'
dev_node << input_node
#<graphics type='vnc' port='5908' autoport='yes' keymap='en-us'/>
graphics_node = Nokogiri::XML::Node.new 'graphics', Nokogiri::XML::Document.new
graphics_node["type"] = 'vnc'; graphics_node["autoport"] = 'yes'
dev_node << graphics_node
root << dev_node
Nokogiri::XML::Document.new << root
end
def hash_to_libvirt(type, hash)
case type
when 'domain', 'network', 'pool', 'volume'
send("hash_to_libvirt_#{type}", hash)
else
raise "Unknown type #{type}"
end
end
puts hash_to_libvirt("domain", defaults.merge(minimal))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment