-
-
Save hedgehog/1058037 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source 'http://rubygems.org' | |
gem 'ruby-libvirt' | |
gem 'ruby-vnc' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'libvirt' | |
require 'vmcrud' | |
require 'vncsend' | |
require 'pp' | |
uri = "qemu+ssh://root@mykvm/system?socket=/opt/libvirt/var/run/libvirt/libvirt-sock" | |
def send_seq(host) | |
Net::VNC.open host do |vnc| | |
vnc.key_press :escape | |
sleep 2 | |
vnc.key_press :escape | |
sleep 2 | |
vnc.key_press :return | |
sleep 2 | |
vnc.type '/install/vmlinuz' | |
sleep 2 | |
vnc.type ' noapic ' | |
sleep 2 | |
vnc.type 'preseed/url=http' | |
sleep 2 | |
vnc.key_down :shift | |
vnc.key_down ":" | |
vnc.key_up :shift | |
sleep 2 | |
vnc.type '//192.168.122.1' | |
vnc.key_down :shift | |
vnc.key_down ":" | |
vnc.key_up :shift | |
vnc.type '80/preseed.cfg ' | |
sleep 2 | |
vnc.type 'debian-installer=en' | |
vnc.key_down :shift | |
vnc.key_down "-" | |
vnc.key_up :shift | |
vnc.type 'US auto locale=en' | |
vnc.key_down :shift | |
vnc.key_down "-" | |
vnc.key_up :shift | |
vnc.type 'US kbd-chooser/method=us ' | |
vnc.type 'hostname=maverick ' | |
sleep 2 | |
vnc.type 'fb=false debconf/frontend=noninteractive ' | |
sleep 2 | |
vnc.type 'console-setup/ask_detect=false ' | |
sleep 2 | |
vnc.type 'console-setup/modelcode=pc105 ' | |
sleep 2 | |
vnc.type 'console-setup/layoutcode=us ' | |
sleep 2 | |
vnc.type 'initrd=/install/initrd.gz --' | |
vnc.key_press :return | |
end | |
end | |
conn = Libvirt::open(uri) | |
name="ubuntu" | |
vm_destroy(conn,name) | |
vm_create(conn,name) | |
vm_start(conn,name) | |
vm_info(conn,name) | |
sleep 5 | |
send_seq("localhost") | |
conn.close |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# this program demonstrates various methods that can be used to create and | |
# remove libvirt domains | |
require 'libvirt' | |
GUEST_DISK = '/opt/libvirt/var/lib/libvirt/images/ubuntu.qcow2' | |
UUID = "93a5c045-6457-2c09-e5ff-927cdf34e17c" | |
def vm_create(conn,name) | |
# create the guest disk | |
#`rm -f #{GUEST_DISK} ; qemu-img create -f qcow2 #{GUEST_DISK} 5G` | |
new_volume_xml = <<EOF | |
<volume> | |
<name>ubuntu.qcow2</name> | |
<capacity unit="G">10</capacity> | |
<target> | |
<path>#{GUEST_DISK}</path> | |
<format type="qcow2"/> | |
</target> | |
</volume> | |
EOF | |
# the XML that describes our guest; note that this is a KVM guest. For | |
# additional information about the guest XML, please see the libvirt | |
# documentation | |
new_dom_xml = <<EOF | |
<domain type='kvm'> | |
<name>ubuntu</name> | |
<uuid>#{UUID}</uuid> | |
<memory>1048576</memory> | |
<currentMemory>1048576</currentMemory> | |
<vcpu>1</vcpu> | |
<os> | |
<type arch='x86_64'>hvm</type> | |
<boot dev='hd'/> | |
<boot dev='cdrom'/> | |
</os> | |
<features> | |
<acpi/> | |
<apic/> | |
<pae/> | |
</features> | |
<clock offset='utc'/> | |
<on_poweroff>destroy</on_poweroff> | |
<on_reboot>restart</on_reboot> | |
<on_crash>restart</on_crash> | |
<devices> | |
<disk type='file' device='cdrom'> | |
<driver name='qemu' type='raw'/> | |
<source file='/opt/libvirt/var/lib/libvirt/cdroms/ubuntu-10.10-server-amd64.iso'/> | |
<target dev='hdc' bus='ide'/> | |
<readonly/> | |
<address type='drive' controller='0' bus='1' unit='0'/> | |
</disk> | |
<disk type='file' device='disk'> | |
<driver name='qemu' type='qcow2'/> | |
<source file='#{GUEST_DISK}'/> | |
<target dev='vda' bus='virtio'/> | |
</disk> | |
<interface type='bridge'> | |
<source bridge='virbr0'/> | |
<model type='virtio'/> | |
<target dev='rl556'/> | |
</interface> | |
<serial type='pty'> | |
<target port='0'/> | |
</serial> | |
<console type='pty'> | |
<target port='0'/> | |
</console> | |
<input type='mouse' bus='ps2'/> | |
<graphics type='vnc' port='-1' autoport='yes' keymap='en-us'/> | |
<video> | |
<model type='cirrus' vram='9216' heads='1'/> | |
</video> | |
</devices> | |
</domain> | |
EOF | |
#<mac address='52:54:01:60:3c:95'/> | |
pool = conn.lookup_storage_pool_by_name("virtimages") | |
volume=pool.create_volume_xml(new_volume_xml) | |
# Create = transient | |
# dom = conn.create_xml(new_dom_xml) | |
dom = conn.define_domain_xml(new_dom_xml) | |
puts "#{name} - creating" | |
dom.create | |
end | |
def vm_destroy(conn, name) | |
puts "#{name} - destroying" | |
begin | |
dom = conn.lookup_domain_by_name(name) | |
if dom.active? | |
dom.destroy | |
end | |
dom.undefine | |
rescue => e | |
puts "ERROR"+e | |
end | |
begin | |
pool = conn.lookup_storage_pool_by_name("virtimages") | |
volume= pool.lookup_volume_by_name("ubuntu.qcow2") | |
volume.delete | |
rescue => e | |
puts "ERROR"+e | |
end | |
end | |
def vm_start(conn, name) | |
begin | |
dom = conn.lookup_domain_by_name(name) | |
unless dom.active? | |
dom.start | |
else | |
puts "#{name} - already running" | |
end | |
rescue => e | |
puts "ERROR"+e | |
end | |
end | |
def vm_info(conn,name) | |
dom = conn.lookup_domain_by_name(name) | |
puts dom.xml_desc | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment