XMLTagsEditHistoryDiscussion

También en Español.

Abstract: This page explains how to run QEMU as a normal user. Although it focuses in Debian GNU/Linux, it can be useful for other distributions. QEMU is ...

  1. Install the software we will need:
  2. Creating the partition image
  3. Running QEMU
  4. Running and networking QEMU
    1. tun/tap
      1. using a bridge
  5. Issues
    1. Out of memory
  6. Links

Install the software we will need:

QEMU
 apt-get install qemu
tunctl
 apt-get install uml-utilities
bridge-utils
 apt-get install bridge-utils

You should install the free (as in freedom) kqemu, QEMU Accelerator Module, and for that you will need the Linux's kernel headers. kqemu used to be proprietary, but now it is licensed under GNU GPL.

linux-kernel-headers
 apt-get install linux-kernel-headers
kqemu
 apt-get install kqemu-source
 cd /usr/src
 m-a prepare
 m-a a-i kqemu

Note that you might find pre-built modules. Do a aptitude search kqemu-modules to see if your debian installation provides them.

Creating the partition image

This command will create a sparse file that QEMU will use.

 qemu-img create image.img 4300M

The size of this file-system will be up to you. We liked the idea of being able to make backups on DVD.

Note that if you wish to make backups of a newly created image (let's say, just after you installed the OS), you could take advantage of the fact that it's a sparse file and make a tar with the {-S} option, that will make smaller files in this way.

tar cjSf backup-image.tar.bz2 image.img

For instance, a backup of a 4.4G file-system required just 679M. We made the backup just after installing Win2k SP4 and the latest patches.

Running QEMU

Before running QEMU, you might want to run as the root user:

 modprobe tun # to network QEMU
 chmod 666 /dev/net/tun #  make it accessible to all users
   
 modprobe kqemu 
 mknod /dev/kqemu c 250 0
 /bin/chmod 666 /dev/kqemu  # make it accessible to all users

Note that you can also use sudo, to allow user joe to run modprobe. We don't like this approach. If we are going to use QEMU in this machine, we'd better add the lines above to the init scripts.

We will need to let the user joe run ifconfig (let's not talk about security now), so we will need to have this in {/etc/sudoers}:

root    ALL=(ALL) ALL
Cmnd_Alias      QEMU=/sbin/ifconfig
# User privilege specification
root    ALL=(ALL) ALL
joe     ALL=NOPASSWD: QEMU

Running and networking QEMU

tun/tap

We need the user joe to be able to use tap0. You might want to add the following lines to the init scripts.

# tunctl -d tap0
 Set 'tap0' nonpersistent
# tunctl -u joe -t tap0
 Set 'tap0' persistent and owned by uid 1000

make a file scripts/up.sh:

 #!/bin/sh
 sudo /sbin/ifconfig $1 192.168.2.1

And now we can run QEMU:

 qemu -m 350 -net nic -net nic,vlan=0 \
                      -net tap,vlan=0,ifname=tap0,script=scripts/up.sh \
                      -hda image.img -localtime -cdrom /dev/cdrom -boot d

We used -cdrom /dev/cdrom so we can install an O.S. from CD/DVD.

To give QEMU access to the local network or to the Internet:

iptables -t nat -A POSTROUTING -o eth0 -j SNAT --to-source 10.0.0.2  # Use your external IP here
echo "1" > /proc/sys/net/ipv4/ip_forward

using a bridge

We liked the method described in this page to hook QEMU to a bridge. In this way, it will be in the local network and can be accessed by other machines.

In the /etc/network/interfaces file, We don't use eth0 as the external interface. Instead, I use a bridge.

 auto br0
 iface br0 inet static
 address 192.168.1.10
 network 192.168.1.0
 netmask 255.255.255.0
 broadcast 192.168.1.255
 gateway 192.168.1.1
 bridge_ports eth0
 bridge_fd 9
 bridge_hello 2
 bridge_maxage 12
 bridge_stp off

In the /etc/sudoers file we have these lines (let's not talk about security now):

Cmnd_Alias  QEMU=/sbin/ifconfig,/usr/sbin/brctl
  
# User privilege specification
root    ALL=(ALL) ALL
joe     ALL=NOPASSWD: QEMU

And We run QEMU with this script:

 #!/bin/sh

 # You need to use the option -win2k-hack if you need to install win2000
 # After the installation, you should remove it.
 # Note that We're using 900MB of RAM for the virtual machine. Change to suit your needs.

 ARGS="-hda image.img -boot c -cdrom /dev/cdrom  -net nic,vlan=0 -net tap,vlan=0,ifname=tap0,script=./qemu-ifup -m 900 -localtime"
 
 echo "starting QEMU with ..."
 echo $ARGS
 echo "...."
 exec qemu $ARGS

The script qemu-ifup we used:

#!/bin/sh

echo "executing qemu-ifup"
echo "bringing up $1 for bridged mode..."
sudo /sbin/ifconfig $1 0.0.0.0 promisc up
echo "adding $1 to br0..."
sudo /usr/sbin/brctl addif br0 $1
sleep 2

You might want to add the following lines to your init scripts.

 echo "Loading kqemu kernel module..."
 modprobe kqemu

 sudo mknod /dev/kqemu c 250 0
 sudo chmod 766 /dev/kqemu
 /usr/sbin/tunctl -u joe

Issues

Out of memory

We still don't know if we have to increase the default value of max_map_count. It is necessary in User Mode Linux. If you don't increase this value, processes inside of the virtual machine (UML) will fail if they try to allocate more than 256MB of RAM.

echo 262144 > /proc/sys/vm/max_map_count

TODO: Is this true also for QEMU?

Links

Last update: 2007-12-30 (Rev 13389)

svnwiki $Rev: 12966 $