Xen Summit 2008 Tokyo - Operating Xen domains through LL(Perl/Python) with libvirt

18
Xen Summit 2008 Tokyo 2008/11/21 paperboy&co. Gosuke Miyashita

Transcript of Xen Summit 2008 Tokyo - Operating Xen domains through LL(Perl/Python) with libvirt

Page 1: Xen Summit 2008 Tokyo - Operating Xen domains through LL(Perl/Python) with libvirt

Xen Summit 2008 Tokyo

2008/11/21

paperboy&co.

Gosuke Miyashita

Page 2: Xen Summit 2008 Tokyo - Operating Xen domains through LL(Perl/Python) with libvirt

Agenda

About me Purpose of this talk What is libvirt libvirt Xen driver architecture libvirt sample codes with Perl/Python Related tools (Avahi and Func)

Page 3: Xen Summit 2008 Tokyo - Operating Xen domains through LL(Perl/Python) with libvirt

About me

Gosuke Miyashitahttp://mizzy.org/

Technical manager at paperboy&co.Web hosting, blog, ec hosting and so on for

indivisuals

Thinking introduce virtualization technologies to our services

Page 4: Xen Summit 2008 Tokyo - Operating Xen domains through LL(Perl/Python) with libvirt

Purpose of this talk I seek how to manage Xen domains on

many host machines I need a simple and customizable tool It may be fast to develop my own tool libvirt is the one for developing my own

Xen management tool Why python and perl?

These languages may be used widly in system administration

Page 5: Xen Summit 2008 Tokyo - Operating Xen domains through LL(Perl/Python) with libvirt
Page 6: Xen Summit 2008 Tokyo - Operating Xen domains through LL(Perl/Python) with libvirt

What is libvirt? The virtualization API A library intracts with virtualization

systemsXen, QEMU, KVM, LXC and OpenVZ

Language bindingsC, Python, Perl, OCaml, Ruby, Java and C#

Support multiple authentication methodsSSH, TLS and X.509, SASL and Kerberos

Avahi multicast DNS support

Page 7: Xen Summit 2008 Tokyo - Operating Xen domains through LL(Perl/Python) with libvirt

What can you do with libvirt? Get Dom0 info

CPU model/threads/cores, memory size and so on List domains Get domain info

id, uuid, name, xml description, os type and state Various domain operations

create, shutdown, reboot, suspend, resumeset/get max memory/memory size, get max vcpus

Network, Storage operations (excluded in this talk)

See http://libvirt.org/html/libvirt-libvirt.html

Page 8: Xen Summit 2008 Tokyo - Operating Xen domains through LL(Perl/Python) with libvirt

libvirt Xen driver(local)

xend xenstored xen hypervisor

libvirt

Perl Python OCaml RubyJava C#C

Page 9: Xen Summit 2008 Tokyo - Operating Xen domains through LL(Perl/Python) with libvirt

libvirt Xen driver(remote)

xend xenstored xen hypervisor

libvirtd

Perl Python OCaml RubyJava C#C

libvirt

Over a network

Page 10: Xen Summit 2008 Tokyo - Operating Xen domains through LL(Perl/Python) with libvirt
Page 11: Xen Summit 2008 Tokyo - Operating Xen domains through LL(Perl/Python) with libvirt

Perl code exampleuse Sys::Virt;

# connect to the remote hostmy $vmm = Sys::Virt->new( uri => "xen://remote_host/“ );

# get domainsmy @doms = $vmm->list_domains;

# print state of each domainsfor my $dom ( @doms ) { printf "%s: %s\n", $dom->get_name, $dom->get_info->{state};}

Page 12: Xen Summit 2008 Tokyo - Operating Xen domains through LL(Perl/Python) with libvirt

Python code exampleimport libvirt

# connect to the remote hostconn = libvirt.open(‘xen://remote_host/’)

# get domain idsids = conn.listDomainsID()

# print state of each domainsfor id in ids: vm = conn.lookupByID(id) print "%s: %s" % ( vm.name(), vm.info()[0] )

Page 13: Xen Summit 2008 Tokyo - Operating Xen domains through LL(Perl/Python) with libvirt
Page 14: Xen Summit 2008 Tokyo - Operating Xen domains through LL(Perl/Python) with libvirt

Avahi support

libvirt is integrated with Avahi Ahahi is a one of mDNS implementation You can discover hosts with libvirtd

running by mDNS client automatically

Page 15: Xen Summit 2008 Tokyo - Operating Xen domains through LL(Perl/Python) with libvirt

Perl example with libvirt and avahiuse Net::Bonjour;use Sys::Virt;

# discover hosts with libvirtd runningmy $res = Net::Bonjour->new('libvirt');$res->discover;

# access libvirtd on each hostfor my $host ( $res->entries ) { my $vmm = Sys::Virt->new( uri => ‘xen://’ . $host->address ); ....}

Page 16: Xen Summit 2008 Tokyo - Operating Xen domains through LL(Perl/Python) with libvirt

Func Fedora Unified Network Controller

https://fedorahosted.org/func/ Sysadmin application framework made

of Python A programming framework for “Exec

some operations for multiple hosts” Many modules

yum, user, service, command, smart, virt ... Virt module uses libvirt

Page 17: Xen Summit 2008 Tokyo - Operating Xen domains through LL(Perl/Python) with libvirt

Func virt module sample codeimport func.overlord.client as fc

# call state method of virt module on all hostsresults = fc.Client("*").virt.state()

# create domain if domain state is ‘shutdown’for ( host, vms ) in results.iteritems(): if vms[0] == 'REMOTE_ERROR': continue

for vm in vms: ( domain, state ) = vm.split(' ') if state == 'shutdown': fc.Client(host).virt.create(domain)

Page 18: Xen Summit 2008 Tokyo - Operating Xen domains through LL(Perl/Python) with libvirt