Setting up qemu on debian etch

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

Notes

  1. /etc/qemu-ifup is part of the debian qemu package. Here's all it is:
     $ cat /etc/qemu-ifup
     #!/bin/sh
     sudo -p "Password for $0:" /sbin/ifconfig $1 172.20.0.1
    
  2. The /etc/qemu-ifup script sets up the tap0 interface to have the IP address 172.20.0.1. Our qemu machine is set to use 172.20.0.2 for it's IP address. Here's our qemu machine's /etc/network/interfaces:
    # 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
    
  3. qemuDebian.img is our virtual machine. I think I originally got it from the freeoszoo page, but have since modified it and upgraded it to etch.
  4. If you want to allow your qemu machine to talk to the internet (e.g., to apt-get upgrade it), try these before you set up the tun/tap0 interface:
    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)
  5. The -kernel-kqemu option does not work for me (it causes my qemu machine to kernel panic while booting). However, the qemu machine seems pretty fast, and the kqemu module is being used (to see this, do lsmod | grep kqemu when running a qemu machine).
  6. We give our students sudo access to the tunctl and ifconfig commands in /etc/sudoers:
    $ 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
    

Useful links


back to helps
Last Modified: Mon 03 Jan 2011 04:08:53 PM EST