## page was renamed from MeetingLogs/devweek1001/devweek1001/KVM == Dev Week -- Developing and Testing in KVM --kirkland -- Wed, Jan 27 == UTC {{{#!IRC [19:03] kirkland, your next [19:03] cjohnston: hi, thanks. [19:03] alright, so I'm here to talk about KVM [19:03] which is the recommended virtualization hypervisor for Ubuntu [19:04] it's similar, at least in principle to other hypervisors like VMWare, Xen, or VirtualBox (among many others) [19:04] this talk is structured as follows ... [19:04] a) a brief description/explanation about Virtualization, and how KVM works [19:05] b) a few simple examples for launching virtual machines with KVM (that you can try at home now) [19:05] c) and finally, some "KVM hacks", to do some fun, or more interesting things with your VMs [19:06] so for starters, check to see if your CPU supports KVM [19:06] egrep "flags.*:.*(svm|vmx)" /proc/cpuinfo [19:06] you need to see if your CPU supports either the svm or the vmx flag [19:06] these are the AMD and Intel designations for hardware supported virtualization [19:07] quick poll in #ubuntu-classroom-chat, hands up o/ if you have cpu support for kvm [19:07] if you don't have hw support for KVM, you'll probably need to use something else (like Xen or VirtualBox) to do virtualization [19:08] unfortunately, I'm not going to cover those in this talk [19:08] okay so KVM ... [19:08] kvm is actually supported through a driver in the linux kernel [19:08] so if you have cpu support, you can "sudo apt-get install qemu-kvm" [19:09] once that installs, you should have a device, /dev/kvm [19:09] this is an interface that the kernel provides to the userspace kvm program [19:10] basically, this allows virtual machines to runs some instructions *directly* on your real cpu [19:10] rather than running those instructions on an emulated CPU [19:10] which is what qemu alone (ie, without kvm) does [19:10] needless to say, emulation is slow [19:10] running on real hardware is much faster! [19:10] for this reason, KVM is really smoking fast virtualization [19:11] however, we still need parts of QEMU [19:11] QEMU = "quick emulator" [19:11] it provides what we call "the driver model" for KVM [19:11] it emulates the rest of the things that aren't accelerated (yet) [19:11] like the sound card, and the video card [19:12] so there are a lot of ways to launch virtual machines with kvm [19:12] you can launch them through the command line [19:12] which is what I generally do, as a developer [19:12] or through one of the graphical front ends [19:13] virt-manager is probably the most popular front end [19:13] i encourage you to try virt-manager out, if gui's are your thing [19:13] it's pretty straight-forward, wizard/menu driven [19:13] it prompts you for various options about your vm creation [19:14] and then, ultimately, just runs a really long kvm line :-) [19:14] it does all of this using the libvirt library [19:14] libvirt provides a common library interface to multiple different virtualization backends (like kvm, xen, and others) [19:14] to get a quick example up and running, let's use "testdrive" [19:15] anyone here already running Lucid on the machine they're going to use to try out these examples? (o/ hands up in -chat) [19:15] if so, just "sudo apt-get install testdrive" [19:16] otherwise, you'll need to install testdrive from the PPA linked to from http://launchpad.net/testdrive [19:16] 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 [19:17] it's a nice way of "testdriving" the daily Ubuntu desktop or server ISOs in a virtual machine [19:18] hopefully you have an Ubuntu *.iso file lying around somewhere [19:18] otherwise, you're going to need to download one to do any of the following examples [19:18] fortunately, I do ... [19:18] and testdrive can either run against a http/ftp/rsync able ISO [19:18] or against a local file [19:19] $ testdrive -u ./lucid-desktop-amd64.iso [19:19] so that's what I'm running right now [19:19] and so the desktop live cd installer then pops up in a new window [19:20] let's look at what KVM was launched with [19:20] $ ps -ef | grep kvm [19:20] 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 [19:20] the options are mostly straightforward [19:20] but testdrive launched with a decent set of options without you having to go figure all of these out [19:21] but since you're here to learn about KVM, let's look at them :-) [19:21] -m 512 says give this virtual machine 512MB of memory [19:21] which is enough to use the LiveCD [19:21] my VM is now up and running the liveCD [19:21] i can launch a terminal in the VM and run "free" [19:21] and see that yes, this VM has ~512MB of memory [19:22] -cdrom tells KVM where to find the ISO to boot [19:22] -drive tells KVM where to find the hard disk [19:22] now there's a bit more to this [19:22] in particular, take note of if=virtio [19:22] this VM is using "virtio" for the hard disk driver [19:23] this is *much* faster for KVM to use, than say, scsi [19:23] virtio is a "paravirtual" disk driver [19:23] kvm is able to write to the backing disk image more directly, through a special, virtio feature in the host's kernel [19:23] you can also see where the .img file is stored (in ~/.cache/testdrive/*img in this case [19:24] it's index=0 which puts this disk at /dev/vda [19:24] (note that the disk would be /dev/sda if the if=scsi [19:24] ) [19:24] and boot=on makes kvm able to boot from this disk [19:25] -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 [19:25] totally optional, but I really like it [19:25] -net nic,model=virtio is the network adapter [19:25] again, we're using virtio for networking [19:26] which, like virtio disk, is a paravirtual networking driver, allowing the vm to talk through the host's network adapter *much* faster [19:26] like 10x faster [19:26] -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 [19:26] and -soundhw is the emulated sound device [19:27] okay ... was anyone able to get a desktop LiveCD VM launched (either through testdrive or on your own)? hands up in -chat o/ [19:27] ah, okay, I see a question about KVM in the BIOS [19:27] let me address that .... [19:28] first, run "kvm-ok" on your host [19:28] that should help detect *most* issues why KVM might not work [19:28] first, it does that grep I asked you to do earlier [19:28] next, it does a few sanity checks, including grepping for a really annoying message in BIOS [19:29] some desktop/laptop manufacturers ship machines that *have* CPUs that support virtualization (VT) [19:29] *but* they disable it in BIOS [19:29] the widespread rumour is that M$ pressures them to do so [19:29] in any case, we all suffer with this [19:29] so if you enter your bios, look in CPU options, and you should see something about "enabling VT, Virtualization Technology" [19:30] turn that on, physically power off, then back on [19:30] and then re-run kvm-ok, and you should be good to go [19:30] cool, looks like we have a few people with running VMs [19:30] now, go ahead and perform an installation (if you like) [19:30] and your desktop image should be installed into that single disk image [19:31] it'll take a little while to complete [19:32] okay, mine's going in the background [19:32] the fun thing is that you can do *anything* in this VM without affecting your stable desktop [19:32] for that reason, KVM is wonderful for developers and testers [19:33] i did all of my development of Ubuntu's Encrypted Home Directory feature, for instance, in hundreds of VMs that I created and destroyed [19:33] have you ever wanted to cd / && sudo rm -rf / ? [19:33] be my guest, in your VM :-) [19:33] anyway, you can do some really constructive (or destructive) things in VMs [19:34] let's pause for a few questions, now, while our installations are going, and before we get into some advanced features [19:34] please post your questions in #ubuntu-classroom-chat, prepended with: QUESTION [19:34] 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? [19:35] rmunn|lernid: possibly ... or at least you can get very, very close [19:35] rmunn|lernid: mainly, you need to track down the disk image, where it installed into [19:35] rmunn|lernid: assuming it's a standard disk format, you should be able to launch [19:36] rmunn|lernid: you might have to recreate some of the meta data, though [19:36] rmunn|lernid: like how much memory the VM gets [19:36] 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 [19:36] rmunn|lernid: and then just run "kvm -m 512 -hda foo.img" [19:37] 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) [19:37] rmunn|lernid: as for something more automated, it might well exist ... though I don't know of it [19:37] rmunn|lernid: i think that would be a cool idea [19:37] rmunn|lernid: file a brainstorm request, and/or a wishlist bug [19:38] 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? [19:38] rmunn|lernid: *great* question ... i'll try to get to that in the advanced examples section [19:39] 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 [19:39] rmunn|lernid: search in that manpage for snapshot= [19:39] rmunn|lernid: and -snapshot [19:40] QUESTION: What benefit does kvm have over virtualbox? [19:40] kjele: for one thing, KVM is really, really, really fast [19:40] kjele: though it requires hardware support to be really, really, really fast [19:40] kjele: KVM is mostly maintained by the Ubuntu Server team [19:41] kjele: and our target hardware (mostly server class hardware) has such support pretty ubiquitously [19:41] kjele: personally, I think VirtualBox is the best alternative for users who *don't* have hardware support [19:41] kjele: and VirtualBox has a pretty GUI [19:42] kjele: ideally, someone in the Ubuntu Desktop team would perhaps support VirtualBox [19:42] kjele: but as of now, it's a project in Ubuntu Universe that mostly works pretty well [19:42] QUESTION: is kvm faster than virtualbox and vmware? [19:42] yltsrc: in my experience, and on hardware that has VT, yes, and yes. [19:43] yltsrc: in particular, if KVM is using virtio networking and disk, yes, and yes ;-) [19:43] 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? [19:43] kamalmostafa: good question [19:43] kamalmostafa: yeah, it totally does, and I'm glad you reminded me to mention the command [19:43] kvm-img create -f qcow2 $DISK_FILE $DISK_SIZE [19:43] that's the command testdrive uses [19:44] if you're creating/launching custom VMs outside of testdrive, you'll need to use "kvm-img create" to create a backing disk image [19:44] there are several supported -f formats [19:44] usually, I use either "raw" or "qcow2" [19:45] I strongly recommend "qcow2" whenever possible [19:45] qcow = quick copy on write format 2 [19:45] which allows the backing disk image to be *much* smaller than the total allocation [19:45] testdrive randomly generates the filename [19:45] and uses 6G for the backing disk size, by default [19:46] (both of which can be overriden by the user (see the testdrive manpages) [19:46] okay, my install has completed [19:46] has anyone else's installation completed? (hands up, or % done in -chat) [19:46] QUESTION: Is it possible to have live migration in KVM? [19:47] lernid_luislopez: yes! we'll try that out shortly, actually :-) [19:47] QUESTION: is kvm ready for production? [19:47] yltsrc: another good question ... [19:47] yltsrc: i would say "if you're on a modern version, yes" :-) [19:47] yltsrc: let me qualify that ... [19:48] yltsrc: Ubuntu 8.04 LTS (Hardy) was the first of the enterprise distros to ship with KVM as the hypervisor [19:48] yltsrc: however, the kvm version back then (and more importantly the kernel support) was very young [19:48] yltsrc: for this reason, I backported kvm-84 to hardy-backports [19:48] yltsrc: I'd say that version (which is Jaunty's KVM) is pretty good [19:48] yltsrc: however, Karmic's (and Lucid's) is far more stable [19:49] yltsrc: and KVM is ultimately what UEC (Ubuntu Enterprise Cloud and Eucalyptus) are using for cloud VMs [19:49] yltsrc: which is production ready ;-) [19:49] 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? [19:50] statik-lernid: if you want to launch the backing image, yeah, sure ... you just need to find the disk image [19:50] statik-lernid: cd ~/.cache/testdrive [19:50] statik-lernid: cd ~/.cache/testdrive/img [19:50] statik-lernid: ls -thalF *.img [19:50] statik-lernid: look at the timestamps, and the file sizes, and you should be able to find the one you want [19:51] statik-lernid: and then just launch KVM [19:51] okay, let me tear through the rest of this [19:51] I have a few fun hacks I'm going to just paste and tell you about, as I'm running low on time [19:51] kvm -m 1024 -cdrom http://cdimage.ubuntu.com/daily/current/lucid-alternate-amd64.iso [19:51] this one is fun ... [19:51] you can actually stream the ISO over http, without downloading it :-) [19:51] i have a pretty good ISO mirror here local on my gigabit network [19:52] which is obviously faster than going over the internet [19:52] so I can launch ISOs without having them locally on my small SSD hard drive in my laptop [19:52] for *really* fast VMs, put your ISO and/or hard disk image in a tmpfs in memory [19:52] i use tmpfs for my /tmp [19:53] (you have to have a lot of RAM, really, to do this well) [19:53] /etc/fstab:tmpfs /tmp tmpfs rw [19:53] that's the line you add to /etc/fstab [19:53] kvm -m 2048 -cdrom /tmp/lucid-desktop-amd64.iso [19:53] that launches an ISO for me, with a *lot* of memory [19:53] where the backing ISO is itself in my RAM :-) [19:53] needless to say, it's pretty quick :-) [19:54] kvm-img can create disks of *any* size [19:54] and qcow2 will make them sparse [19:54] so you can create, let's say, a 2PB (petabyte) disk [19:54] kvm-img create -f qcow2 /tmp/petabyte.img 1024T [19:54] launch a KVM with that as the backing disk and do a "sudo fdisk -l" in the VM [19:54] it's fun to see a 1PB backing disk [19:55] long before they're on the market :-D [19:55] you can dynamically add a disk to a running VM [19:55] in the vm, "sudo modprobe acpiphp" [19:55] (note that we're enabling this by default in Lucid, so that you don't have to modprobe it on the server) [19:56] now, in your running KVM, drop to the QEMU console, where there are all sorts of fun, interesting commands [19:56] ctrl-alt-2 [19:56] that'll put you at a QEMU prompt [19:56] pci_add auto storage file=/tmp/petabyte.img,if=virtio [19:56] then go back to your VM [19:56] ctrl-alt-1 [19:56] sudo fdisk -l [19:57] that should show you your very big disk :-) [19:57] finally, let's do a live migration ... [19:57] now we'll do the live migration from one VM to another VM, running on the same system (your localhost) [19:58] but if you have two machines running the same KVM version, you should be able to do this across your network too [19:58] so first, launch a vm [19:58] kvm -m 512 -hda /local/virt/img/lucid-desktop.img [19:58] or use your running one [19:59] next, launch a second kvm process WITH THE EXACT SAME PARAMETERS [19:59] plus the -incoming option [19:59] kvm -m 512 -hda /local/virt/img/lucid-desktop.img -incoming tcp:0.0.0.0:1025 [19:59] and the IP address and Port [19:59] i'm using local, so I have 0.0.0.0 [19:59] and I arbitrarily chose 1025 [19:59] In the source, drop to the QEMU console [19:59] ctrl-alt-2 [19:59] and run: [19:59] migrate tcp:localhost:1025 [20:00] and in a few seconds, you should see your vm move from one process to the other [20:00] to do this across separate hosts, you need to have persistent storage across both (like NFS) [20:00] and it requires a slightly more complicated networking setup [20:00] but it's quite doable [20:00] okay, I think my time is up [20:00] hope this was useful [20:01] i'll be around in #ubuntu-server if you have more questions [20:01] cheers }}}