This post was inspired by my previous post outlining how to bypass a bug in installing Ubuntu 7.10 within QEMU. You may be interested in reading that post as well, Installing Gutsy in QEMU bugfix.
There are many ways to achieve virtualization these days. I’m sure many of you have played with VMware or perhaps Parrallels, etc. It seems we’ve moved (or are moving) toward more and more virtualization on our machines allowing us to do more and more with the hardware. If you haven’t done virtualization yet and you have a machine that is up to the task you’ll definitely want to read on.
Even if you have done virtualization previously you’ll want to check out QEMU and KQEMU. The real open source virtualization projects.
For this tutorial we’ll look at setting up KQEMU, which does have a few requirements. If you don’t meet these requirements you might want to look at the Ubuntu QEMU Wiki for less accelerated virtualization, but still a usable solution.
Can you handle it?
First things first you’ll need to make sure you’re running Ubuntu 7.04 “Feisty” or similar kernel. As far as I understand KVM wasn’t supported until the Feisty release so this may not be an option in earlier Ubuntu versions.
Second you’ll need to make sure your hardware supports KVM. You can get that information from your machine by checking for some flags in /proc/cpuinfo. Use the command as follows. If you see output you’re in good shape. ..if you don’t see anything returned check out the non-KVM solution linked above.
grep -E '^flags.*(vmx|svm)' /proc/cpuinfo
Note: Even if you get no output from the above command, your computer may actually support hardware virtualization. Some machines are delivered with the BIOS option for support switched off. Some other machines require updating to the latest BIOS. This tutorial won’t cover those requirements.
Install the software
If you’ve got the hardware it takes as found above we’re now ready to install the required packages. Get started by installing kvm and qemu, using a command such as:
sudo aptitude install kvm qemu
When that is finished installing you’ll need to insert the appropriate kernel modules for your hardware. If you’re using AMD hardware use the following:
sudo modprobe kvm-amd
or for intel hardware
sudo modprobe kvm-intel
You may need to grant yourself access to the new kvm ‘device’ in /dev. You can use one of the following methods:
sudo chmod 777 /dev/kvm
sudo usermod -a -G kvm $user
sudo useradd $user kvm
(the first of the above options opens full permissions to the file. The second two add your $user to the kvm group, allowing it group-based permissions to the device.)
Create the environment
Now that you’ve got kvm and qemu installed and the proper modules inserted let’s take a second to create our virtual hard drive. One cool thing about virtualization is that you can create, install and run an entire OS from within a single file.
qemu-img create distro.img -f qcow 5G
This command will create a file called distro.img (distro being whatever you’d like to call it) with an available (but not yet used) size of 5G. Replace the distro.img name and file size to your own preference. For basic testing of Ubuntu and other distributions 5G should be plenty.
Now let’s launch up a virtual installer and get things running!
qemu -hda distro.img -cdrom /dev/cdrom | cdrom-image.iso -m 512 -localtime -boot d -soundhw all
Let me break down this command for you as well.
- -hda tells it the hard drive to use. We’ll want it to use the newly created virtual hard drive, distro.img.
- -cdrom tells it to use the following device or file location as a cdrom. In this case you can use a physical cdrom in your /dev/cdrom drive itself, or specify a distribution cdrom .iso file.
- -m 512 tells the virtual machine to be assigned 512M of RAM. This can change based on your needs.
- -localtime will assign the virtual machine time to your host machines localtime. I have found this optional.
- -boot d will tell the virtual machine to boot from the cdrom drive. Generally use this for installation only.
- -soundhw creates and assigns the virtual sound hardware. If you want sound on your virtual machine, use this as well.
This should launch up your virtual machine in a new window, start the installer and go through as normal. If you’ve never installed virtual machines before don’t worry it really is pretty painless. Also, I know some people worry about the partitioning section of the installer. It won’t harm your host machine so create whatever partitions you’d like. It’s completely contained in the virtual image we created previously.
Running the new system
Now that you’ve got your system installed let’s launch the new machine and play around. Did I say play around? I meant do some serious bug testing, of course.
qemu -hda distro.img -m 512 -soundhw all
This more simple line should be adequate to launch your virtual machine. I know the command above was much more complicated, but we do need to tell it to do a bit more for an installation.
You may see an error with this or the above installation command about acceleration layer not activated. If so keep reading, it should be solved with the next step.
Using kqemu for acceleration
Now that we’ve got a basic machine setup and have a basic understanding of using QEMU for virtualization let’s do it right with kqemu, the kvm improved QEMU system.
For this we’ll need to hop through a few more hoops but when you see the performance difference you’ll be glad you did!
We’ll be using a second kernel module for the kqemu system, which has a bit of a workaround. I’m sure it’ll be fixed in gutsy, but currently we have to do some minor tweaking. Again, it’ll be pretty painless and it’s very much worth it!
Insert the kqemu kernel module using:
sudo modprobe kqemu
This should create a device at /dev/kqemu, but it doesn’t, so we’ll take care of that in the next step.
Create a new file at /etc/udev/rules.d/60-kqemu.rules and insert the following:
KERNEL=="kqemu", NAME="%k", MODE="0660", GROUP="kqemu"
Next, change the contents of /etc/modprobe.d/kqemu to the following:
options kqemu major=0
Lastly, add a new group to your system called kqemu and add yourself to it using:
sudo addgroup --system kqemu
sudo adduser $user kqemu
If you didn’t quite follow everything we just did here’s a recap. We created a udev rule that will create the device we need when the kqemu module is loaded. We also customized some of the details of that device, and then added our $user to the group so they’d have permissions to use the new device.
At this point you’ll need to login and log out again to refresh your user permissions and group containment. After you’ve done this try to launch your virtual machine again and see if you notice a difference in performance. On my machine it is very much noticeable and I’m sure you’ll find the same. Again, this section is for those machines that can support the KVM system tested for in the first part of the tutorial.
If you want to automate the sections on loading the kernel modules we need you’ll want to add them to the list of modules automagically loaded at boot. Add new lines for the modules you need in the /etc/modules file, such as:
kvm-intel or kvm-amd
Enjoy. By the way, if you read this entire thing I’m very impressed. It turned out much longer than I expected it would, but once I get on a roll I just can’t stop. I hope it was helpful and I hope you’ll take some time to install Ubuntu 7.04 “Gutsy” and help us do some testing. Be sure to report your bugs on launchpad so they can be fixed prior to the final release.