Fall 2011 CS380L 

Setting up QEMU

This document is about how to setup the environment for booting Linux on qemu, an open source machine emulator. This document just sets up qemu. Downloading, building and booting Linux is explained elsewhere.

THESE INSTRUCTIONS ARE NOT GUARANTEED TO WORK. They are intended to give you a good head start so that you can figure the rest out for yourself. Surmounting obstacles is vital to doing systems work and for appreciating systems work.

If you discover that these instructions need to be modified, let me know. Systems work is highly collaborative.

I will be indicating variables for you to fill in with <>, as in "<you decide this>". A repeated variable name will indicate that you want to use the same value as before.

I recommend that you look up any command (e.g. dd) or command-line option you aren't familiar with using man (e.g. execute "man dd").

We will be using a number of command line options for QEMU here. I suggest you make shell scripts to organize them.

Diskspace

5GB disk space quota allowed to graduate students(or 1GB for undergrads) on CS machines  might not be sufficient for programming assignments. You can use local disk partition at /var/local/ on CS machines instead. This partition typically has 30+ GB and also is not  part of NFS system which will make I/O faster. 

Check QEMU

Ensure QEMU (or qemu-kvm) is installed (or build it yourself).

The qemu binary will be . You want "full system emulation", which creates an entire emulated machine including simulated hardware. The binary you are searching for will be qemu-system-x86_64 on a 64-bit system (and I believe qemu-system-i386 otherwise). I suggest a 64-bit system for forward compatibility with the way the world is headed, if you can get one.

Create an Ubuntu VM image

Download Ubuntu

http://www.ubuntu.com/download/ubuntu/download

I recommend the 64-bit server version (if you have a 64-bit system; I'm not sure that you can emulate a 64-bit VM on a 32-bit system). You will get a file <ubuntu image.iso> .

Create a place on disk for your VM disk image

This makes a 5 GB file of zeros. dd if=/dev/zero of= bs=1M count=5120

Execute QEMU to boot your VM

There are a number of options you will want for QEMU here. Look up what these do in the QEMU man page (and perhaps in the QEMU source code, it's pretty interesting stuff).

Add ISO image

You want the Ubuntu image as a CD in a CD drive and to tell the system to boot from it. QEMU command line options:
-cdrom <ubuntu image.iso> -boot d

Add hard drive image

QEMU command line options:
<HD image>

Memory

QEMU command line options:
-m <VM mem amount> (I used -m 512 = 512 MB on a CS machine, but you may be able to get away with more.)

Networking

You may need to use:
-net nic,model=rtl8139 -net user
though should actually be the QEMU default. (I needed this on a CS machine which I tried this on.)

Ensure hardware virtualization is off if you have qemu-kvm

I wish you could use it, but you need root or access to the device /dev/kvm. QEMU command line options:
-no-kvm

Add how you intend to connect to the VM

Use VNC to start

QEMU command line options:
-vnc :0

Run QEMU

So in summary here you have:
<qemu> -cdrom <ubuntu image.iso> -boot d -m <VM mem amount> -no-kvm -vnc :0 <HD image>

Note that this isn't secure; someone else can log into your VNC server. "-nographic" will help with that, and there's also a VNC password option if you are paranoid (that I have not played with).

For future QEMU running, I won't collect all the options in one command; you can do that yourself.

Connect to your VM

Use a VNC client. For Linux, you can use vinagre. The VNC server with display at ":<x>" is generally available at port <5900+x>.

Install Ubuntu

Most options are pretty self explanatory. I have not messed with LVM, so I don't know how turning that on will affect your image. You should have net access (though you can't connect to the VM from the outside, only from in the VM outward).

When you come to the end of the install process, you don't need to "eject" your CD - when you reset the VM, the VM will crash. That's fine and expected. Kill the QEMU process.

Prepare your Ubuntu image

There are a few things you need to do to put your system in the final state you will want:

Enable a serial console (option a - preferred)

You won't want to use VNC forever with a text-only interface. You need to ensure that a serial console is loaded to use the non-graphical option.

Copy /etc/init/tty.conf to /etc/ttyS0.conf.

Change the text inside so that tty<x> becomes ttyS0 everywhere, and change the "getty" line to:

exec /sbin/getty -L 115200 ttyS0 vt100

(this by experiment works, other variants may as well)

When you have done this, you will now replace the "-vnc :0" option to qemu with "-nographic", this causes your terminal to instead connect to qemu directly. (You may want to have another terminal handy to kill the qemu process, or use a terminal manager like "screen".) If you don't do that, your kernel will appear to hang when it is done booting. That's just because you're not seeing the action.

You may still see plymouth messages (see below), but it seems in non-graphical boot this is not a problem (you will still reach a terminal). You can remove these by removing Plymouth from startup as below.

Remove plymouth, blacklist framebuffer, continue to use VNC (option b)

For reasons I admit I don't fully understand yet, Plymouth (a graphical bootloader for Ubuntu) will mess up your (graphical only it seems) boot process when you don't have a ramdisk. So if you want to keep using VNC with your custom kernel, you have to remove /etc/init/plymouth*.conf .

I also suggest opening /etc/modprobe.d/blacklist-framebuffer and adding "blacklist vga16fb" to the list, or else listing long directories will be painfully slow.