Open Source Virtualization Hacks


Published on

Published in: Technology, Art & Photos
1 Comment
  • nice introduction for new comers of virtualization~
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Open Source Virtualization Hacks

  1. <ul><ul><li>Open Source Virtualization Hacks </li></ul></ul><ul><ul><li>Niel M. Bornstein </li></ul></ul><ul><ul><li>[email_address] </li></ul></ul><ul><ul><li>O'Reilly Open Source Convention 2008 </li></ul></ul>
  2. Agenda <ul><li>The Abstract </li></ul><ul><li>The Technology </li></ul><ul><li>The Hacks </li></ul>
  3. The Abstract <ul><li>Open source virtualization systems use the usual suite of tools and languages—can anyone out there say Python and XML? </li></ul><ul><li>In this session, you’ll see some ways to build a management system, using the tools you already know, to do some wicked things with virtual machines. </li></ul><ul><li>Along the way you’ll learn about the Open Virtual Machine Format (OVF) and some of the other emerging standards that are helping modern data centers run. </li></ul>
  4. The Technology <ul><li>This presentation will make use of the Xen hypervisor on openSUSE 11.0, using libvirt's python bindings. </li></ul><ul><li>The hacks and methods presented should be equally applicable to other operating systems and hypervisors supported by libvirt. </li></ul>
  5. The Technology – openSUSE 11.0 <ul><li>“The openSUSE project is a community program sponsored by Novell. </li></ul><ul><li>“Promoting the use of Linux everywhere, provides free, easy access to the world's most usable Linux distribution, openSUSE. </li></ul><ul><li>“The openSUSE project gives Linux developers and enthusiasts everything they need to get started with Linux.” </li></ul><ul><ul><li> </li></ul></ul>
  6. The Technology – Xen 3.2.1 <ul><li>“The Xen ® hypervisor, the powerful open source industry standard for virtualization, offers a powerful, efficient, and secure feature set for virtualization of x86, x86_64, IA64, PowerPC, and other CPU architectures. </li></ul><ul><li>“It supports a wide range of guest operating systems including Windows ® , Linux ® , Solaris ® , and various versions of the BSD operating systems.” </li></ul><ul><ul><li> </li></ul></ul>
  7. The Technology – libvirt 0.4.0 <ul><li>“A toolkit to interact with the virtualization capabilities of recent versions of Linux (and other OSes) </li></ul><ul><li>“Free software available under the GNU Lesser General Public License </li></ul><ul><li>“A long term stable C API </li></ul><ul><li>“A set of bindings for common languages </li></ul><ul><li>“A CIM provider for the DMTF virtualization schema” </li></ul><ul><ul><li> </li></ul></ul>
  8. The Technology – Python 2.5.2 <ul><li>“Python is a dynamic object-oriented programming language that can be used for many kinds of software development.” </li></ul><ul><ul><li> </li></ul></ul>
  9. The Hacks <ul><li>What’s a Hack? </li></ul><ul><ul><li>“Hacks are tools, tips, and tricks that help users solve problems. They are aimed at intermediate-level power users and scripters.” </li></ul></ul><ul><ul><ul><li> </li></ul></ul></ul><ul><li>Eleven Hacks in Three Categories </li></ul><ul><ul><li>Exploration </li></ul></ul><ul><ul><li>Basic Tasks </li></ul></ul><ul><ul><li>Advanced Tasks </li></ul></ul>
  10. Hack #1 – The xm Command <ul><li>Most Linux geeks like to use the command line. The Xen hypervisor provides a command line management user interface. The xm command allows you to do most common single-host management tasks. </li></ul>NAME xm - Xen management user interface SYNOPSIS xm <subcommand> [args] DESCRIPTION The xm program is the main interface for managing Xen guest domains. The program can be used to create, pause, and shutdown domains. It can also be used to list current domains, enable or pin VCPUs, and attach or detach virtual block devices. The basic structure of every xm command is almost always: xm <subcommand> <domain-id> [OPTIONS] ...
  11. Hack #1 – The xm Command (cont'd) <ul><li>The command line is very useful, but can be tricky to do some tasks. </li></ul><ul><ul><li>For example, to install a new VM from scratch, you first need to define it using configuration files. Only then can you use the xm create command to start the install process. </li></ul></ul><ul><ul><ul><li>More about the configuration file format, and ways to edit it, later. </li></ul></ul></ul><ul><ul><li>Because it's a complex process, virt-manager makes it much easier to build a new VM. </li></ul></ul>
  12. Hack #2 – The virsh Command <ul><li>virsh is another command line tool, using libvirt to provide access to a variety of hypervisors in a generic manner. </li></ul><ul><ul><li>More about libvirt later! </li></ul></ul>NAME virsh - management user interface SYNOPSIS virsh <subcommand> [args] DESCRIPTION The virsh program is the main interface for managing virsh guest domains. The program can be used to create, pause, and shutdown domains. It can also be used to list current domains. Libvirt is a C toolkit to interact with the virtualization capabilities of recent ver‐ sions of Linux (and other OSes). It is free software available under the GNU Lesser General Public License. Virtualization of the Linux Operating System means the ability to run multiple instances of Operat‐ ing Systems concurrently on a single hardware system where the basic resources are driven by a Linux instance. The library aim at providing long term stable C API initially for the Xen paravirtualization but should be able to integrate other virtualization mechanisms, it cur‐ rently also support QEmu and KVM. ...
  13. Hack #2 – The virsh Command (cont'd) <ul><li>The entire libvirt API is available from the virsh command. </li></ul>chonju:~ # virsh Welcome to virsh, the virtualization interactive terminal. Type: 'help' for help with commands 'quit' to quit virsh # dominfo 1 Id: 1 Name: opensuse11 UUID: 60c451d0-9009-2bd6-1cfb-f71e9ec6926d OS Type: linux State: blocked CPU(s): 1 CPU time: 3.3s Max memory: 393216 kB Used memory: 393216 kB virsh # shutdown 1 Domain 1 is being shutdown virsh #
  14. Hack #3 – virt-manager <ul><li>virt-manager is a project hosted by Red Hat designed to provide a graphical UI for managing virtual machines. </li></ul><ul><li>It is written in Python with Glade and GTK+ and uses libvirt to access a variety of virtualization systems. </li></ul><ul><li>It is included in most modern Linux distributions. </li></ul>
  15. Hack #3 – virt-manager (cont'd) <ul><li>You can even view multiple virtualization technologies through a single interface. </li></ul><ul><ul><li>Select the File -> Open Connection menu command and you will see that you can also connect to a QEMU hypervisor in the same interface, as well as making remote connections to other machines. </li></ul></ul>
  16. Hack #3 – virt-manager (cont'd) <ul><li>Building a new VM is also simple. </li></ul><ul><ul><li>Click on the New button to bring up the vm-install process, which can also be launched directly from the command line. </li></ul></ul><ul><ul><li>The vm-install wizard will present you with all the options you need to configure a new VM, and it's much easier than manually editing configuration files. </li></ul></ul><ul><ul><li>Tip: To build a VM with an ISO install source, loop mount the iso first: </li></ul></ul><ul><ul><ul><li>losetup -f /path/to/iso </li></ul></ul></ul>
  17. Hack #4 – libvirt + Python <ul><li>After the command line and the GUI tool, the next step is to build your own tools programmatically. </li></ul><ul><li>Using libvirt , you can manage a variety of virtualization systems from a variety of programming languages. </li></ul><ul><ul><li>For these examples, we'll use Xen and Python, because, well, it's really easy. </li></ul></ul><ul><ul><li>After the first example, I will omit error checking for brevity! </li></ul></ul>
  18. Hack #4 – libvirt + Python (cont'd) <ul><li>The following sample code comes from the libvirt website: </li></ul>#!/usr/bin/python import libvirt import sys conn = libvirt.openReadOnly(None) if conn == None: print 'Failed to open connection to the hypervisor' sys.exit(1) try: dom0 = conn.lookupByName(&quot;Domain-0&quot;) except: print 'Failed to find the main domain' sys.exit(1) print &quot;Domain 0: id %d running %s&quot; % (dom0.ID(), dom0.OSType()) print
  19. Hack #5 – Query the Hypervisor's Capabilities <ul><li>Before taking any action using libvirt , it's a good idea to get an idea of what the hypervisor is capable of doing. </li></ul><ul><li>libvirt provides a couple of convenient methods for doing this: </li></ul><ul><ul><li>virConnect.getCapabilities() returns information in XML format about the hypervisor. </li></ul></ul><ul><ul><li>virConnect.getInfo() returns a python dict of information about the physical node. </li></ul></ul>
  20. Hack #5 – Query the Hypervisor's Capabilities (cont'd) #!/usr/bin/python import libvirt conn = print conn.getCapabilities() info = conn.getInfo() print &quot;CPU model: %s&quot; % info[0] print &quot;memory: %d kB&quot; % info[1] print &quot;# of CPUs: %d&quot; % info[2] print &quot;CPU freq: %d MHz&quot; % info[3] print &quot;# of NUMA cell: %d&quot; % info[4] print &quot;# of CPU sockets: %d&quot; % info[5] print &quot;# of cores per socket: %d&quot; % info[6] print &quot;# of threads per core: %d&quot; % info[7]
  21. Hack #6 – Start/Stop a VM <ul><li>Now that we know what the hypervisor is capable of, we can start a VM instance. </li></ul><ul><li>We've already seen how to do this with the xm command and virt-manager ; now we'll do it from a Python script. </li></ul>#!/usr/bin/python import sys, libvirt vmname = sys.argv[1] conn = dom = conn.lookupByName(vmname) dom.create() dom = conn.lookupByName(vmname) print &quot;%d started&quot; % dom.ID()
  22. Hack #6 – Start/Stop a VM (cont'd) <ul><li>Similarly, we can stop a VM, no matter whether it was started through libvirt , the xm command, or virt-manager . </li></ul>#!/usr/bin/python import sys, libvirt vmid = int(sys.argv[1]) conn = dom = conn.lookupByID(vmid) dom.shutdown()
  23. Hack #7 – Migrate a VM <ul><li>In real life, you may occasionally need to move a running VM from one physical host to another. This is referred to as “migrating” the VM. </li></ul><ul><li>I can't demonstrate doing this on a single laptop, but I can show you some code that should work! </li></ul>
  24. Hack #7 – Migrate a VM (cont'd) #!/usr/bin/python import sys, getpass, libvirt vmname = sys.argv[1] uri = sys.argv[2] mydata = &quot;&quot; def getCredentials(credentials, data): for credential in credentials: print credential[1] + &quot;:&quot;, if credential[0] == libvirt.VIR_CRED_AUTHNAME: data = sys.stdin.readline() data = data[0:len(data)-1] credential[4] = data elif credential[0] == libvirt.VIR_CRED_PASSPHRASE: credential[4] = getpass(&quot;&quot;) else: return -1 return 0 flags = [libvirt.VIR_CRED_AUTHNAME,libvirt.VIR_CRED_PASSPHRASE] auth = [flags,getCredentials,mydata] localconn = dom = localconn.lookupByName(vmname) remoteconn = libvirt.openAuth(None,auth,0) dom.migrate(remoteconn,libvirt.VIR_MIGRATE_LIVE,None,uri,0)
  25. Hack #7 – Migrate a VM (cont'd) <ul><li>In reality, it ends up being much easier to use the xm migrate command to do this. If I had to make a recommendation, I'd say that unless you're building a complete management system, you should just use the xm or virsh tools to migrate your VMs: </li></ul><ul><ul><li>xm migrate --live domain newhost </li></ul></ul><ul><ul><li>virsh migrate --live domain newhost </li></ul></ul><ul><li>In these commands, the domain parameter is either the name or id of the domain, and the newhost parameter is the URI of the new host. </li></ul>
  26. Hack #7 – Migrate a VM (cont'd) <ul><li>The /etc/xen/xend-config.sxp file that ships with Xen does not allow migrations. Make the following changes to turn migration on: </li></ul><ul><ul><li>Uncomment this line and change no to yes: </li></ul></ul><ul><ul><ul><li>#(xend-relocation-server no) </li></ul></ul></ul><ul><ul><li>Uncomment this line: </li></ul></ul><ul><ul><ul><li>#(xend-relocation-port 8002) </li></ul></ul></ul><ul><ul><li>Uncomment this line: </li></ul></ul><ul><ul><ul><li>#(xend-relocation-address '') </li></ul></ul></ul><ul><ul><li>Customize this line for your security needs: </li></ul></ul><ul><ul><ul><li>(xend-relocation-hosts-allow '^localhost$ ^localhostlocaldomain$') </li></ul></ul></ul><ul><li>Then restart xend with the command rcxend restart </li></ul>
  27. Hack #8 – Edit a VM Configuration <ul><li>A VM can be thought of as a combination of configuration information and a disk image. </li></ul><ul><li>You can change the VM configuration when a VM instance is not running by editing the configuration file (in Xen, that's the file in /etc/xen/vm/ vmname ). </li></ul>
  28. Hack #8 – Edit a VM Configuration (cont'd) <ul><li>It's easy enough to modify a config by hand, once you understand the format. </li></ul>name=&quot;opensuse11&quot; uuid=&quot;60c451d0-9009-2bd6-1cfb-f71e9ec6926d&quot; memory=384 vcpus=1 on_poweroff=&quot;destroy&quot; on_reboot=&quot;restart&quot; on_crash=&quot;destroy&quot; localtime=0 keymap=&quot;en-us&quot; builder=&quot;linux&quot; bootloader=&quot;/usr/lib/xen/boot/; bootargs=&quot;--entry=xvda2:/boot/vmlinuz-xen,/boot/initrd-xen&quot; extra=&quot; &quot; disk=[ 'file:/var/lib/xen/images/opensuse11/disk0,xvda,w', ] vif=[ 'mac=00:16:3e:49:b8:b2', ] vfb=['type=vnc,vncunused=1']
  29. Hack #8 – Edit a VM Configuration (cont'd) <ul><li>But why do it by hand when we've got Python? </li></ul>#!/usr/bin/python import sys, os original = sys.argv[1] new = sys.argv[2] f = file(original, &quot;rb&quot;) lines = f.readlines() dict = {} for line in lines: pieces = line.partition(&quot;=&quot;) dict[pieces[0]] = eval(pieces[2]) for key in dict.keys(): if key == &quot;name&quot;: dict[key] = new elif key == &quot;disk&quot;: disks = dict[key] dict[key] = [] for disk in disks: dict[key].append(disk.replace(os.path.basename(original),new)) elif key == &quot;vif&quot;: dict[key] = None elif key == &quot;uuid&quot;: dict[key] = None if dict[key] != None: print &quot;%s=%s&quot; % ( key, repr(dict[key]) )
  30. Hack #8 – Edit a VM Configuration (cont'd) <ul><li>You can change the configuration of a running VM instance using the xm or virsh commands, or using libvirt . </li></ul>#!/usr/bin/python import sys, libvirt vmname = sys.argv[1] maxMemory = int(sys.argv[2]) conn = dom = conn.lookupByName(vmname) print dom.maxMemory() dom.create() dom.setMaxMemory(maxMemory) dom = conn.lookupByName(vmname) print dom.maxMemory()
  31. Hack #9 – Edit a VM Image <ul><li>Editing a VM image file is called “cracking it open”. The image file needs to be mounted as a loopback filesystem. </li></ul><ul><li>The easiest way to crack open a Xen image is to loop mount it using lomount . </li></ul><ul><ul><li>lomount -diskimage disk0 -partition 2 /mnt </li></ul></ul><ul><ul><li>Bear in mind that the image file will almost certainly have more than one partition. </li></ul></ul><ul><ul><li>This method will not work with LVM disks! </li></ul></ul>
  32. Hack #10 – Clone a VM <ul><li>Now we have edited a VM configuration and image. </li></ul><ul><li>Cloning a VM is as simple as copying the configuration and image to a new location, then editing the configuration and “personalizing” the image. </li></ul><ul><li>You just saw how our friend Python can easily read in a configuration file and make the necessary changes. </li></ul>
  33. Hack #10 – Clone a VM (cont'd) <ul><li>Just one little problem: my disk image is 8 gigs. That takes a while to copy </li></ul><ul><li>The solution: Copy-on-Write. </li></ul><ul><li>Use the QEMU CoW (qcow) format </li></ul><ul><li>Convert your raw image to qcow using img2qcow </li></ul><ul><ul><li>img2qcow destination source </li></ul></ul><ul><li>There is also a corresponding qcow2raw </li></ul>
  34. Hack #11 – Create an Appliance <ul><li>Appliances can be defined using Open Virtual Machine Format (OVF), a standard proposed by VMware under the auspices of the Distributed Management Task Force (DMTF). </li></ul><ul><li>OVF is: </li></ul><ul><ul><li>A packaging format for software applications </li></ul></ul><ul><ul><ul><li>Bundle together your VMs for a self-contained application </li></ul></ul></ul><ul><ul><li>A transport mechanism for virtual machine templates </li></ul></ul><ul><ul><ul><li>Distribute your OVFs as images which must be installed before they can be run </li></ul></ul></ul>
  35. Hack #11 – Create an Appliance (cont'd) <ul><li>OVF is not : </li></ul><ul><ul><li>An efficient execution environment </li></ul></ul><ul><ul><ul><li>VMs must be installed before they can be run </li></ul></ul></ul><ul><ul><li>A format requiring a hypervisor </li></ul></ul><ul><ul><ul><li>By itself, OVF does not have any dependence on a hypervisor </li></ul></ul></ul>
  36. Hack #11 – Create an Appliance (cont'd) <ul><li>The OVF file is a zip file containing </li></ul><ul><ul><li>An XML file ( .ovf ) with schema </li></ul></ul><ul><ul><ul><li>May refer to external URIs for actual VM images </li></ul></ul></ul><ul><ul><ul><li>May contain virtual hardware resource descriptions </li></ul></ul></ul><ul><ul><li>May contain a manifest file ( .mf ) </li></ul></ul><ul><ul><li>May contain a certificate file ( .cert ) </li></ul></ul>
  37. Hack #11 – Create an Appliance (cont'd) <ul><li>Today, there is no OVF tool for Xen, only VMware! </li></ul><ul><li>IBM is sponsoring an Open OVF project </li></ul><ul><ul><li> </li></ul></ul>
  38. Hack #11 – Create an Appliance (cont'd) <ul><li>For another take on appliances, check out Nat Friedman's talk: </li></ul><ul><ul><li>The Future of Linux is Software Appliances </li></ul></ul><ul><ul><ul><li>Friday 10:45 in Portland 255 </li></ul></ul></ul>
  39. <ul><ul><li>Q&A </li></ul></ul>