It seems like a lot of people have trouble setting up qemu (including myself). Here's how I did it (not necessarily the best way). We use qemu here at Swarthmore in teaching a computer science course on operating systems. I've set up qemu for the students to use on all of our lab machines.
First some details about our computers:
Install the qemu software from debian:
sudo apt-get install uml-utilities qemu kqemu-modules-2.6.18-4-686
dpkg --get-selections | grep qemu
kqemu-modules-2.6-k7 install
kqemu-modules-2.6.18-4-686 install
qemu install
If you want to build kernels for your qemu machine (we do, since our students are using qemu for an operating systems class):
sudo apt-get install devscripts fakeroot kernel-package linux-source-2.6.18
Set up the kqemu device using udev (here useable by all in the vm group):
echo kqemu >> /etc/modules
echo "options kqemu major=0" > /etc/modprobe.d/my_kqemu
echo 'KERNEL=="kqemu", NAME="%k", MODE="0660", GROUP="vm"' > /etc/udev/rules.d/my_kqemu.rules
# comment out the kqemu line in /etc/udev/rules.d/020_permissions.rules
/etc/init.d/udev restart
modprobe kqemu
Set up networking. For our lab machines, we just want a private network between the host and guest os. Use ifconfig -a after this command to see the results.
sudo tunctl -b -u username
Run the qemu machine:
qemu -nographic -m 256 -net nic -net tap,ifname=tap0,script=/etc/qemu-ifup qemuDebian.img
After shutting down the qemu machine, remove the tap0 interface:
sudo tunctl -d tap0
$ cat /etc/qemu-ifup #!/bin/sh sudo -p "Password for $0:" /sbin/ifconfig $1 172.20.0.1
# The loopback network interface auto lo iface lo inet loopback # The primary network interface auto eth0 iface eth0 inet static address 172.20.0.2 netmask 255.255.255.0 broadcast 172.20.0.255 gateway 172.20.0.1
sudo sh -c "echo "1" > /proc/sys/net/ipv4/ip_forward" sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE(and don't forget to set up a correct /etc/resolv.conf on your qemu machine)
$ cat /etc/sudoers # sudoers file. # # This file MUST be edited with the 'visudo' command as root. # # See the man page for details on how to write a sudoers file. # # Host alias specification Host_Alias LAB = machine1, machine2, machine3 # User alias specification User_Alias CS45 = user1, user2, user3 # Cmnd alias specification Cmnd_Alias TUNCTLUP = /usr/sbin/tunctl -b -u [A-z]*, !/usr/sbin/tunctl -b -u root Cmnd_Alias IFUP = /sbin/ifconfig tap0 172.20.0.1 Cmnd_Alias TUNCTLDOWN = /usr/sbin/tunctl -d tap0 # override built-in defaults Defaults env_reset # User privilege specification root ALL=(ALL) ALL # allow cs45-ers to bring up the tun interface on lab machines CS45 LAB = TUNCTLUP, IFUP, TUNCTLDOWN