Skip to content

Instantly share code, notes, and snippets.

@pdf
Created March 24, 2011 03:00
Show Gist options
  • Select an option

  • Save pdf/884469 to your computer and use it in GitHub Desktop.

Select an option

Save pdf/884469 to your computer and use it in GitHub Desktop.
Convert hash into libvirt domain XML
require 'rubygems'
# xml-simple has a bug that needs to be fixed before this works, see:
# https://github.com/pdf/xml-simple/commit/b4da34165c88fe912b45af614b326ed512a666c7
require 'xmlsimple'
# domain1 omits the __value__ key except where required
domain1 = {
"@type"=>"kvm",
"name"=>"demo1",
"uuid"=>"4dea24b3-1d52-d8f3-2516-782e98a23fa1",
"devices"=>
{
"graphics"=>
{
"@keymap"=>"de",
"@port"=>"-1",
"@type"=>"vnc"
},
"interface"=>
{
"mac"=>{"@address"=>"24:42:53:21:52:45"},
"@type"=>"network",
"source"=>{"@network"=>"default"}
},
"disk"=>
{
"@device"=>"disk",
"@type"=>"file",
"target"=>{"@dev"=>"hda"},
"source"=>{"@file"=>"/var/lib/libvirt/images/demo2.img"}
},
"emulator"=>"/usr/bin/qemu-kvm"
},
"clock"=>{"@sync"=>"localtime"},
"sysinfo"=>
{
"bios"=>{"entry"=>{"@name"=>"vendor", "__value__"=>"LENOVO"}},
"system"=>{"entry"=>[{"@name"=>"manufacturer", "__value__"=>"Fedora"},
{"@name"=>"vendor", "__value__"=>"Virt-Manager"}]},
"@type"=>"smbios"
},
"memory"=>"131072",
"os"=>
{
"type"=>{"@arch"=>"i686", "__value__"=>"hvm"},
"boot"=>[{"@dev"=>"hd"},
{"@dev"=>"cdrom"}]
},
"vcpu"=>
{
"__value__"=>"2",
"@cpuset"=>"1-4,^3,6",
"@current"=>"1"
}
}
# domain2 uses the __value__ key always
domain2 = {
"@type"=>"kvm",
"name"=>{"__value__"=>"demo2"},
"uuid"=>{"__value__"=>"4dea24b3-1d52-d8f3-2516-782e98a23fa0"},
"devices"=>
{
"graphics"=>
{
"@keymap"=>"de",
"@port"=>"-1",
"@type"=>"vnc"
},
"interface"=>
{
"mac"=>{"@address"=>"24:42:53:21:52:45"},
"@type"=>"network",
"source"=>{"@network"=>"default"}
},
"disk"=>
{
"@device"=>"disk",
"@type"=>"file",
"target"=>{"@dev"=>"hda"},
"source"=>{"@file"=>"/var/lib/libvirt/images/demo2.img"}
},
"emulator"=>{"__value__"=>"/usr/bin/qemu-kvm"}
},
"clock"=>{"@sync"=>"localtime"},
"sysinfo"=>
{
"bios"=> {"entry"=>{"@name"=>"vendor", "__value__"=>"LENOVO"}},
"system"=> {"entry"=>[{"@name"=>"manufacturer", "__value__"=>"Fedora"},
{"@name"=>"vendor", "__value__"=>"Virt-Manager"}]},
"@type"=>"smbios"
},
"memory"=>{"__value__"=>"131072"},
"os"=>
{
"type"=>{"@arch"=>"i686", "__value__"=>"hvm"},
"boot"=>[{"@dev"=>"hd"},
{"@dev"=>"cdrom"}]
},
"vcpu"=>
{
"__value__"=>"2",
"@cpuset"=>"1-4,^3,6",
"@current"=>"1"
}
}
# Either way works, just a matter of preference
puts XmlSimple.xml_out(domain1, {'RootName' => 'domain', 'AttrPrefix' => true, 'ContentKey' => '__value__'})
puts XmlSimple.xml_out(domain2, {'RootName' => 'domain', 'AttrPrefix' => true, 'ContentKey' => '__value__'})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment