Dev Week -- Developing and Testing in KVM --kirkland -- Wed, Jan 27


   1 [19:03] <cjohnston> kirkland, your next
   2 [19:03] <kirkland> cjohnston: hi, thanks.
   3 [19:03] <kirkland> alright, so I'm here to talk about KVM
   4 [19:03] <kirkland> which is the recommended virtualization hypervisor for Ubuntu
   5 [19:04] <kirkland> it's similar, at least in principle to other hypervisors like VMWare, Xen, or VirtualBox (among many others)
   6 [19:04] <kirkland> this talk is structured as follows ...
   7 [19:04] <kirkland>  a) a brief description/explanation about Virtualization, and how KVM works
   8 [19:05] <kirkland>  b) a few simple examples for launching virtual machines with KVM (that you can try at home now)
   9 [19:05] <kirkland>  c) and finally, some "KVM hacks", to do some fun, or more interesting things with your VMs
  10 [19:06] <kirkland> so for starters, check to see if your CPU supports KVM
  11 [19:06] <kirkland> egrep "flags.*:.*(svm|vmx)" /proc/cpuinfo
  12 [19:06] <kirkland> you need to see if your CPU supports either the svm or the vmx flag
  13 [19:06] <kirkland> these are the AMD and Intel designations for hardware supported virtualization
  14 [19:07] <kirkland> quick poll in #ubuntu-classroom-chat, hands up o/ if you have cpu support for kvm
  15 [19:07] <kirkland> if you don't have hw support for KVM, you'll probably need to use something else (like Xen or VirtualBox) to do virtualization
  16 [19:08] <kirkland> unfortunately, I'm not going to cover those in this talk
  17 [19:08] <kirkland> okay so KVM ...
  18 [19:08] <kirkland> kvm is actually supported through a driver in the linux kernel
  19 [19:08] <kirkland> so if you have cpu support, you can "sudo apt-get install qemu-kvm"
  20 [19:09] <kirkland> once that installs, you should have a device, /dev/kvm
  21 [19:09] <kirkland> this is an interface that the kernel provides to the userspace kvm program
  22 [19:10] <kirkland> basically, this allows virtual machines to runs some instructions *directly* on your real cpu
  23 [19:10] <kirkland> rather than running those instructions on an emulated CPU
  24 [19:10] <kirkland> which is what qemu alone (ie, without kvm) does
  25 [19:10] <kirkland> needless to say, emulation is slow
  26 [19:10] <kirkland> running on real hardware is much faster!
  27 [19:10] <kirkland> for this reason, KVM is really smoking fast virtualization
  28 [19:11] <kirkland> however, we still need parts of QEMU
  29 [19:11] <kirkland> QEMU = "quick emulator"
  30 [19:11] <kirkland> it provides what we call "the driver model" for KVM
  31 [19:11] <kirkland> it emulates the rest of the things that aren't accelerated (yet)
  32 [19:11] <kirkland> like the sound card, and the video card
  33 [19:12] <kirkland> so there are a lot of ways to launch virtual machines with kvm
  34 [19:12] <kirkland> you can launch them through the command line
  35 [19:12] <kirkland> which is what I generally do, as a developer
  36 [19:12] <kirkland> or through one of the graphical front ends
  37 [19:13] <kirkland> virt-manager is probably the most popular front end
  38 [19:13] <kirkland> i encourage you to try virt-manager out, if gui's are your thing
  39 [19:13] <kirkland> it's pretty straight-forward, wizard/menu driven
  40 [19:13] <kirkland> it prompts you for various options about your vm creation
  41 [19:14] <kirkland> and then, ultimately, just runs a really long kvm line :-)
  42 [19:14] <kirkland> it does all of this using the libvirt library
  43 [19:14] <kirkland> libvirt provides a common library interface to multiple different virtualization backends (like kvm, xen, and others)
  44 [19:14] <kirkland> to get a quick example up and running, let's use "testdrive"
  45 [19:15] <kirkland> anyone here already running Lucid on the machine they're going to use to try out these examples?  (o/ hands up in -chat)
  46 [19:15] <kirkland> if so, just "sudo apt-get install testdrive"
  47 [19:16] <kirkland> otherwise, you'll need to install testdrive from the PPA linked to from http://launchpad.net/testdrive
  48 [19:16] <kirkland> testdrive is just a wrapper that rsync's or zsync's an Ubuntu ISO and launches it in a KVM with a good set of options
  49 [19:17] <kirkland> it's a nice way of "testdriving" the daily Ubuntu desktop or server ISOs in a virtual machine
  50 [19:18] <kirkland> hopefully you have an Ubuntu *.iso file lying around somewhere
  51 [19:18] <kirkland> otherwise, you're going to need to download one to do any of the following examples
  52 [19:18] <kirkland> fortunately, I do ...
  53 [19:18] <kirkland> and testdrive can either run against a http/ftp/rsync able ISO
  54 [19:18] <kirkland> or against a local file
  55 [19:19] <kirkland> $ testdrive -u ./lucid-desktop-amd64.iso
  56 [19:19] <kirkland> so that's what I'm running right now
  57 [19:19] <kirkland> and so the desktop live cd installer then pops up in a new window
  58 [19:20] <kirkland> let's look at what KVM was launched with
  59 [19:20] <kirkland> $ ps -ef | grep kvm
  60 [19:20] <kirkland> kirkland 11395 11364 99 13:19 pts/12   00:00:35 kvm -m 512 -cdrom /local/virt/iso/lucid-desktop-amd64.iso -drive file=/home/kirkland/.cache/testdrive/img/testdrive-disk-nvqebh.img,if=virtio,index=0,boot=on -usb -usbdevice tablet -net nic,model=virtio -net user -soundhw es1370
  61 [19:20] <kirkland> the options are mostly straightforward
  62 [19:20] <kirkland> but testdrive launched with a decent set of options without you having to go figure all of these out
  63 [19:21] <kirkland> but since you're here to learn about KVM, let's look at them  :-)
  64 [19:21] <kirkland> -m 512 says give this virtual machine 512MB of memory
  65 [19:21] <kirkland> which is enough to use the LiveCD
  66 [19:21] <kirkland> my VM is now up and running the liveCD
  67 [19:21] <kirkland> i can launch a terminal in the VM and run "free"
  68 [19:21] <kirkland> and see that yes, this VM has ~512MB of memory
  69 [19:22] <kirkland> -cdrom tells KVM where to find the ISO to boot
  70 [19:22] <kirkland> -drive tells KVM where to find the hard disk
  71 [19:22] <kirkland> now there's a bit more to this
  72 [19:22] <kirkland> in particular, take note of if=virtio
  73 [19:22] <kirkland> this VM is using "virtio" for the hard disk driver
  74 [19:23] <kirkland> this is *much* faster for KVM to use, than say, scsi
  75 [19:23] <kirkland> virtio is a "paravirtual" disk driver
  76 [19:23] <kirkland> kvm is able to write to the backing disk image more directly, through a special, virtio feature in the host's kernel
  77 [19:23] <kirkland> you can also see where the .img file is stored (in ~/.cache/testdrive/*img in this case
  78 [19:24] <kirkland> it's index=0 which puts this disk at /dev/vda
  79 [19:24] <kirkland> (note that the disk would be /dev/sda if the if=scsi
  80 [19:24] <kirkland> )
  81 [19:24] <kirkland> and boot=on makes kvm able to boot from this disk
  82 [19:25] <kirkland> -usb -usbdevice tablet is just a neat little hack that allows you to move your mouse cursor in and out of the KVM sdl window more smoothly
  83 [19:25] <kirkland> totally optional, but I really like it
  84 [19:25] <kirkland> -net nic,model=virtio is the network adapter
  85 [19:25] <kirkland> again, we're using virtio for networking
  86 [19:26] <kirkland> which, like virtio disk, is a paravirtual networking driver, allowing the vm to talk through the host's network adapter *much* faster
  87 [19:26] <kirkland> like 10x faster
  88 [19:26] <kirkland> -net user is the type of networking, there are a lot of different options here, but this is the simplest for quick outgoing network access
  89 [19:26] <kirkland> and -soundhw is the emulated sound device
  90 [19:27] <kirkland> okay ... was anyone able to get a desktop LiveCD VM launched (either through testdrive or on your own)?  hands up in -chat o/
  91 [19:27] <kirkland> ah, okay, I see a question about KVM in the BIOS
  92 [19:27] <kirkland> let me address that ....
  93 [19:28] <kirkland> first, run "kvm-ok" on your host
  94 [19:28] <kirkland> that should help detect *most* issues why KVM might not work
  95 [19:28] <kirkland> first, it does that grep I asked you to do earlier
  96 [19:28] <kirkland> next, it does a few sanity checks, including grepping for a really annoying message in BIOS
  97 [19:29] <kirkland> some desktop/laptop manufacturers ship machines that *have* CPUs that support virtualization (VT)
  98 [19:29] <kirkland> *but* they disable it in BIOS
  99 [19:29] <kirkland> the widespread rumour is that M$ pressures them to do so
 100 [19:29] <kirkland> in any case, we all suffer with this
 101 [19:29] <kirkland> so if you enter your bios, look in CPU options, and you should see something about "enabling VT, Virtualization Technology"
 102 [19:30] <kirkland> turn that on, physically power off, then back on
 103 [19:30] <kirkland> and then re-run kvm-ok, and you should be good to go
 104 [19:30] <kirkland> cool, looks like we have a few people with running VMs
 105 [19:30] <kirkland> now, go ahead and perform an installation (if you like)
 106 [19:30] <kirkland> and your desktop image should be installed into that single disk image
 107 [19:31] <kirkland> it'll take a little while to complete
 108 [19:32] <kirkland> okay, mine's going in the background
 109 [19:32] <kirkland> the fun thing is that you can do *anything* in this VM without affecting your stable desktop
 110 [19:32] <kirkland> for that reason, KVM is wonderful for developers and testers
 111 [19:33] <kirkland> i did all of my development of Ubuntu's Encrypted Home Directory feature, for instance, in hundreds of VMs that I created and destroyed
 112 [19:33] <kirkland> have you ever wanted to cd / && sudo rm -rf / ?
 113 [19:33] <kirkland> be my guest, in your VM :-)
 114 [19:33] <kirkland> anyway, you can do some really constructive (or destructive) things in VMs
 115 [19:34] <kirkland> let's pause for a few questions, now, while our installations are going, and before we get into some advanced features
 116 [19:34] <kirkland> please post your questions in #ubuntu-classroom-chat, prepended with: QUESTION
 117 [19:34] <kirkland> <rmunn|lernid> QUESTION: If I have an existing VM created in VirtualBox and/or VMWare, is it possible to convert it to run under qemu-kvm instead?
 118 [19:35] <kirkland> rmunn|lernid: possibly ...  or at least you can get very, very close
 119 [19:35] <kirkland> rmunn|lernid: mainly, you need to track down the disk image, where it installed into
 120 [19:35] <kirkland> rmunn|lernid: assuming it's a standard disk format, you should be able to launch
 121 [19:36] <kirkland> rmunn|lernid: you might have to recreate some of the meta data, though
 122 [19:36] <kirkland> rmunn|lernid: like how much memory the VM gets
 123 [19:36] <kirkland> rmunn|lernid: but if you find the disk image itself, make a backup copy of the disk image (while the VM is *not* running), cp it to /tmp/foo.img, for instance
 124 [19:36] <kirkland> rmunn|lernid: and then just run "kvm -m 512 -hda foo.img"
 125 [19:37] <kirkland> rmunn|lernid: if that works (or mostly works), you'll then just need to play with getting the rest of the kvm options you want in there (like adding networking, sound, etc)
 126 [19:37] <kirkland> rmunn|lernid: as for something more automated, it might well exist ... though I don't know of it
 127 [19:37] <kirkland> rmunn|lernid: i think that would be a cool idea
 128 [19:37] <kirkland> rmunn|lernid: file a brainstorm request, and/or a wishlist bug
 129 [19:38] <kirkland> <rmunn|lernid> QUESTION: How do you save a "snapshot" of your VM's state under qemu-kvm, then revert to that snapshot later, discarding any changes since the snapshot?
 130 [19:38] <kirkland> rmunn|lernid: *great* question ... i'll try to get to that in the advanced examples section
 131 [19:39] <kirkland> rmunn|lernid: in case I don't get all the way through to it (as I didn't prepare that particular example), see: http://manpages.ubuntu.com/manpages/lucid/en/man1/kvm.1.html
 132 [19:39] <kirkland> rmunn|lernid: search in that manpage for snapshot=
 133 [19:39] <kirkland> rmunn|lernid: and -snapshot
 134 [19:40] <kirkland> <kjele> QUESTION: What benefit does kvm have over virtualbox?
 135 [19:40] <kirkland> kjele: for one thing, KVM is really, really, really fast
 136 [19:40] <kirkland> kjele: though it requires hardware support to be really, really, really fast
 137 [19:40] <kirkland> kjele: KVM is mostly maintained by the Ubuntu Server team
 138 [19:41] <kirkland> kjele: and our target hardware (mostly server class hardware) has such support pretty ubiquitously
 139 [19:41] <kirkland> kjele: personally, I think VirtualBox is the best alternative for users who *don't* have hardware support
 140 [19:41] <kirkland> kjele: and VirtualBox has a pretty GUI
 141 [19:42] <kirkland> kjele: ideally, someone in the Ubuntu Desktop team would perhaps support VirtualBox
 142 [19:42] <kirkland> kjele: but as of now, it's a project in Ubuntu Universe that mostly works pretty well
 143 [19:42] <kirkland> <yltsrc> QUESTION: is kvm faster than virtualbox and vmware?
 144 [19:42] <kirkland> yltsrc: in my experience, and on hardware that has VT, yes, and yes.
 145 [19:43] <kirkland> yltsrc: in particular, if KVM is using virtio networking and disk, yes, and yes ;-)
 146 [19:43] <kirkland> <kamalmostafa> QUESTION: Does testdrive create a blank .img of a particular size before booting the specified iso?  Or does the .img file somehow "grow" as needed?
 147 [19:43] <kirkland> kamalmostafa: good question
 148 [19:43] <kirkland> kamalmostafa: yeah, it totally does, and I'm glad you reminded me to mention the command
 149 [19:43] <kirkland> kvm-img create -f qcow2 $DISK_FILE $DISK_SIZE
 150 [19:43] <kirkland> that's the command testdrive uses
 151 [19:44] <kirkland> if you're creating/launching custom VMs outside of testdrive, you'll need to use "kvm-img create" to create a backing disk image
 152 [19:44] <kirkland> there are several supported -f formats
 153 [19:44] <kirkland> usually, I use either "raw" or "qcow2"
 154 [19:45] <kirkland> I strongly recommend "qcow2" whenever possible
 155 [19:45] <kirkland> qcow = quick copy on write format 2
 156 [19:45] <kirkland> which allows the backing disk image to be *much* smaller than the total allocation
 157 [19:45] <kirkland> testdrive randomly generates the filename
 158 [19:45] <kirkland> and uses 6G for the backing disk size, by default
 159 [19:46] <kirkland> (both of which can be overriden by the user (see the testdrive manpages)
 160 [19:46] <kirkland> okay, my install has completed
 161 [19:46] <kirkland> has anyone else's installation completed?  (hands up, or % done in -chat)
 162 [19:46] <kirkland> <lernid_luislopez> QUESTION: Is it possible to have live migration in KVM?
 163 [19:47] <kirkland> lernid_luislopez: yes!  we'll try that out shortly, actually :-)
 164 [19:47] <kirkland> <yltsrc> QUESTION: is kvm ready for production?
 165 [19:47] <kirkland> yltsrc: another good question ...
 166 [19:47] <kirkland> yltsrc: i would say "if you're on a modern version, yes" :-)
 167 [19:47] <kirkland> yltsrc: let me qualify that ...
 168 [19:48] <kirkland> yltsrc: Ubuntu 8.04 LTS (Hardy) was the first of the enterprise distros to ship with KVM as the hypervisor
 169 [19:48] <kirkland> yltsrc: however, the kvm version back then (and more importantly the kernel support) was very young
 170 [19:48] <kirkland> yltsrc: for this reason, I backported kvm-84 to hardy-backports
 171 [19:48] <kirkland> yltsrc: I'd say that version (which is Jaunty's KVM) is pretty good
 172 [19:48] <kirkland> yltsrc: however, Karmic's (and Lucid's) is far more stable
 173 [19:49] <kirkland> yltsrc: and KVM is ultimately what UEC (Ubuntu Enterprise Cloud and Eucalyptus) are using for cloud VMs
 174 [19:49] <kirkland> yltsrc: which is production ready ;-)
 175 [19:49] <kirkland> <statik-lernid> QUESTION: I used testdrive-select-iso last night and ran through a lucid desktop installation, but then i turned it off this morning. is there a way for me to get back to that machine without having to do a whole new install?
 176 [19:50] <kirkland> statik-lernid: if you want to launch the backing image, yeah, sure ... you just need to find the disk image
 177 [19:50] <kirkland> statik-lernid: cd ~/.cache/testdrive
 178 [19:50] <kirkland> statik-lernid: cd ~/.cache/testdrive/img
 179 [19:50] <kirkland> statik-lernid: ls -thalF *.img
 180 [19:50] <kirkland> statik-lernid: look at the timestamps, and the file sizes, and you should be able to find the one you want
 181 [19:51] <kirkland> statik-lernid: and then just launch KVM
 182 [19:51] <kirkland> okay, let me tear through the rest of this
 183 [19:51] <kirkland> I have a few fun hacks I'm going to just paste and tell you about, as I'm running low on time
 184 [19:51] <kirkland> kvm -m 1024 -cdrom http://cdimage.ubuntu.com/daily/current/lucid-alternate-amd64.iso
 185 [19:51] <kirkland> this one is fun ...
 186 [19:51] <kirkland> you can actually stream the ISO over http, without downloading it :-)
 187 [19:51] <kirkland> i have a pretty good ISO mirror here local on my gigabit network
 188 [19:52] <kirkland> which is obviously faster than going over the internet
 189 [19:52] <kirkland> so I can launch ISOs without having them locally on my small SSD hard drive in my laptop
 190 [19:52] <kirkland> for *really* fast VMs, put your ISO and/or hard disk image in a tmpfs in memory
 191 [19:52] <kirkland> i use tmpfs for my /tmp
 192 [19:53] <kirkland> (you have to have a lot of RAM, really, to do this well)
 193 [19:53] <kirkland>     /etc/fstab:tmpfs /tmp tmpfs rw
 194 [19:53] <kirkland> that's the line you add to /etc/fstab
 195 [19:53] <kirkland> kvm -m 2048 -cdrom /tmp/lucid-desktop-amd64.iso
 196 [19:53] <kirkland> that launches an ISO for me, with a *lot* of memory
 197 [19:53] <kirkland> where the backing ISO is itself in my RAM :-)
 198 [19:53] <kirkland> needless to say, it's pretty quick :-)
 199 [19:54] <kirkland> kvm-img can create disks of *any* size
 200 [19:54] <kirkland> and qcow2 will make them sparse
 201 [19:54] <kirkland> so you can create, let's say, a 2PB (petabyte) disk
 202 [19:54] <kirkland> kvm-img create -f qcow2 /tmp/petabyte.img 1024T
 203 [19:54] <kirkland> launch a KVM with that as the backing disk and do a "sudo fdisk -l" in the VM
 204 [19:54] <kirkland> it's fun to see a 1PB backing disk
 205 [19:55] <kirkland> long before they're on the market :-D
 206 [19:55] <kirkland> you can dynamically add a disk to a running VM
 207 [19:55] <kirkland> in the vm, "sudo modprobe acpiphp"
 208 [19:55] <kirkland> (note that we're enabling this by default in Lucid, so that you don't have to modprobe it on the server)
 209 [19:56] <kirkland> now, in your running KVM, drop to the QEMU console, where there are all sorts of fun, interesting commands
 210 [19:56] <kirkland> ctrl-alt-2
 211 [19:56] <kirkland> that'll put you at a QEMU prompt
 212 [19:56] <kirkland> pci_add auto storage file=/tmp/petabyte.img,if=virtio
 213 [19:56] <kirkland> then go back to your VM
 214 [19:56] <kirkland> ctrl-alt-1
 215 [19:56] <kirkland> sudo fdisk -l
 216 [19:57] <kirkland> that should show you your very big disk :-)
 217 [19:57] <kirkland> finally, let's do a live migration ...
 218 [19:57] <kirkland> now we'll do the live migration from one VM to another VM, running on the same system (your localhost)
 219 [19:58] <kirkland> but if you have two machines running the same KVM version, you should be able to do this across your network too
 220 [19:58] <kirkland> so first, launch a vm
 221 [19:58] <kirkland> kvm -m 512 -hda /local/virt/img/lucid-desktop.img
 222 [19:58] <kirkland> or use your running one
 223 [19:59] <kirkland> next, launch a second kvm process WITH THE EXACT SAME PARAMETERS
 224 [19:59] <kirkland> plus the -incoming option
 225 [19:59] <kirkland> kvm -m 512 -hda /local/virt/img/lucid-desktop.img -incoming tcp:
 226 [19:59] <kirkland> and the IP address and Port
 227 [19:59] <kirkland> i'm using local, so I have
 228 [19:59] <kirkland> and I arbitrarily chose 1025
 229 [19:59] <kirkland> In the source, drop to the QEMU console
 230 [19:59] <kirkland> ctrl-alt-2
 231 [19:59] <kirkland> and run:
 232 [19:59] <kirkland>     migrate tcp:localhost:1025
 233 [20:00] <kirkland> and in a few seconds, you should see your vm move from one process to the other
 234 [20:00] <kirkland> to do this across separate hosts, you need to have persistent storage across both (like NFS)
 235 [20:00] <kirkland> and it requires a slightly more complicated networking setup
 236 [20:00] <kirkland> but it's quite doable
 237 [20:00] <kirkland> okay, I think my time is up
 238 [20:00] <kirkland> hope this was useful
 239 [20:01] <kirkland> i'll be around in #ubuntu-server if you have more questions
 240 [20:01] <kirkland> cheers

MeetingLogs/devweek1001/KVM (last edited 2010-01-29 10:07:12 by dholbach)