QEmu

From Gentoo Linux Wiki
Jump to: navigation, search
Fix me: Information in this article was not verified. Please actualise this information by checking its integrity.

Virtualization TOC


Virtual pc.png

Qemu allows the emulation of multiple architectures (ARM systems, IA-32 (x86) PCs, AMD64 PCs, MIPS R4000, Sun's SPARC sun3, and PowerPC (PReP and Power Macintosh)) on your computer. You can use it to install Windows or many other OSes inside your Linux-based environment.

Contents

[edit] Ebuild starting from version 0.10.3

Starting from Qemu 0.10.3, the following packages:

  • app-emulation/qemu
  • app-emulation/qemu-softmmu
  • app-emulation/qemu-user

have been unified into a single app-emulation/qemu package. The following documentation does not yet reflect that change, so make sure you apply all the USE flags to the correct package.


Two new USE_EXPAND variables are used, and they include all possible flags by default. Add them in your Portage configuration file to tweak them:

File: /etc/make.conf
QEMU_SOFTMMU_TARGETS="i386 ppc ppc64 x86_64"
QEMU_USER_TARGETS="arm i386 x86_64"
  • QEMU_SOFTMMU_TARGETS defines the targets for the full system emulation. This mode allows you to emulate a complete machine and to install a different OS on another architecture.
  • QEMU_USER_TARGETS defines the targets for the user space emulation. This mode allows you, for instance to run a program compiled for Linux ARM in your Linux x86_64 environment.

[edit] Installation

If you want to have any graphical output, you should use the sdl library by adding its flag to build :

File: /etc/portage/package.use
app-emulation/qemu sdl

[edit] Kernel module accelerator

You can use the kqemu kernel module accelerator by doing the following:

  • Add kqemu after app-emulation/qemu sdl in etc/portage/package.use;
  • Emerge kqemu using GCC 3
  • Load the kernel module using the following command:
modprobe kqemu major=0

To use kqemu with your user account, you must add it to the qemu group :

gpasswd -a [your_user] qemu

For a 64 bit architecture, you must use the following command in order to get Qemu to work:

qemu-system-x86_64 -hda /path/to/image -boot X -m xxx
Note: To test if kqemu is correctly installed, run info kqemu. If it returns kqemu support: enabled for user and kernel code, your installation is correct.

[edit] Using the emulated OS with ethernet and a valid IP

Qemu provides basic networking functionality by default (-net user), but this does not work with things that need root access, such as ping. To use your host's ethernet connection over the emulated OS you'll need to use TUN/TAP.

You must enable TUN/TAP in your kernel by using the following configuration option:

Linux Kernel Configuration: Enable TUN/TAP support
Device Drivers  ---> 
    Networking support  --->
        <M> Universal TUN/TAP device driver support
Networking  --->
    Networking options  --->
        <*> 802.1d Ethernet Bridging #NOTE : at least for 2.6.20 series

Load TUN/TAP module:

modprobe tun

[edit] Making a bridge

Setting up a bridge on the host machine for two guests (Example)

emerge net-misc/bridge-utils sys-apps/usermode-utilities

Create a tun device as follows (2.4 kernel)

mknod /dev/net/tun c 10 200

Create br0

brctl addbr br0

Manually set up IP information for br0

Static IP:

File: /etc/conf.d/net
bridge_br0="eth0"
config_eth0=( "null" )
config_br0=( "192.168.0.1/24" )
RC_NEED_br0="net.eth0"
brctl_br0=( "setfd 0" "sethello 1" "stp off" )
routes_br0=( "default gw 192.168.0.254" )

Dynamic IP:

File: /etc/conf.d/net
bridge_br0="eth0"
config_eth0=( "null" )
config_br0=( "dhcp" )
dhcpcd_br0="-t 10"
RC_NEED_br0="net.eth0"
brctl_br0=( "setfd 0" "sethello 1" "stp off" )
Note: It is wise to check that other options do not conflict with the new ones. For example, options like config_eth0="dhcp" should be removed

Add net.br0 to the default runlevel

cd /etc/init.d && ln -s net.lo net.br0 && rc-update add net.br0 default

Add this iptables rule to your firewall

/sbin/iptables -A INPUT -i br0 -j ACCEPT

Restart the network

/etc/init.d/net.eth0 restart && /etc/init.d/net.br0 start && ifconfig br0 && ifconfig eth0

[edit] Using TUN/TAP interface as the root user

Create a guest-ifup script

File: guest-ifup
#!/bin/bash
#
# Argument $1 will be the name of the interface (tun0, tun1, ...)
if test $(/sbin/ifconfig | grep -c $1) -gt 0; then
        /sbin/brctl delif br0 $1
        ifconfig $1 down
fi

/sbin/ifconfig $1 0.0.0.0 promisc up
/sbin/brctl addif br0 $1
/usr/bin/tunctl -u root -t $1
exit 0
Note: This will create the tap devices under root so you'll need root access to run qemu
/usr/local/bin/qemu -localtime -m 512m -cdrom /dev/cdrom \ -net nic,macaddr=52:54:00:12:34:56 -net tap,ifname=tap0,script=guest-ifup \ -boot c -hda img=guest01.img
Note: Use different MAC addresses for each guest
Note: The above command assumes that a guest01.img file has been created

[edit] Alternative TUN/TAP using Gentoo's /etc/conf.d/net

Configuration of interface can be offloaded to Gentoo's universal network configuration scripts.

File: /etc/conf.d/net
config_vm0="192.168.0.1/24"
Note: Don't write tuntap_vm0="tap", because tunctl or openvpn will complain about devices already in use.
File: ifup.sh
#!/bin/sh

exec /etc/init.d/net."$1" start
File: ifdown.sh
#!/bin/sh

exec /etc/init.d/net."$1" stop

Don't forget to create net.vm0 symlink to net.lo.

Call your machine like that:

/usr/local/bin/qemu -net nic,macaddr=52:54:00:12:34:56 \ -net tap,ifname=tap0,script=ifup.sh,downscipt=ifdown.sh

[edit] Using TUN/TAP interface as a normal user

The above example creates the TUN/TAP interface as the root user. If you want to run qemu as a normal user then the tap interface needs to be owned by a normal user. To achieve this you need to:

  • Run tunctl -u <user> to create a tap device owned by <user>
  • The user running qemu needs to have read/write permissions to /dev/net/tun

[edit] Creating a group to simplify things

To simplify the process of running qemu as a user we'll create a group vmnet that will have permissions to create the tap network interface, configure the interface and have access to /dev/net/tun.

groupadd vmnet

Then add all the users that should be able to run qemu to the group

usermod -a - G vmnet user1

Finally we'll allow the group to call the specific commands without passwords (assuming we'll use sudo for this)

File: /etc/sudoers
%vmnet	ALL=(ALL)	NOPASSWD: /sbin/ifconfig, /sbin/brctl, /usr/bin/tunctl

Now change the guest-ifup script to call the commands using sudo.

[edit] Setting group permissions on /dev/net/tun

Now to set read/write permission for the group vmnet on /dev/net/tun. The permissions on /dev/net/tun are controlled by a rule in /etc/udev/rules.d/50-udev.rules that looks like this:

File: /etc/udev/rules.d/50-udev.rules
KERNEL=="tun",		NAME="net/%k",	MODE="0660", OPTIONS+="ignore_remove"

Change that rule so that it sets the group of the tun device to be that of our new group:

File: /etc/udev/rules.d/50-udev.rules
KERNEL=="tun",		NAME="net/%k", GROUP="vmnet", MODE="0660", OPTIONS+="ignore_remove"

The change won't take effect until the next boot, so in the meantime you can do

chown root:vmnet /dev/net/tun

to get things working immediately.

Note: That rules file will be overwritten the next time udev is updated, so pay attention when emerging udev.

[edit] Using of QEmu (f.e. Windows XP)

[edit] GUI to setup vm (optional)

This step is optional! If you prefer a gui to setup the virtual machine you can install aqemu.

emerge -av aqemu

[edit] Create a virtual drive

qemu-img create -f qcow windows_xp.img 4G

[edit] Install Windows XP

Use the location of your cd drive.

qemu -localtime -cdrom /dev/cdrom -m 384 -boot d windows_xp.img

This starts QEmu using the time from the linux box, windows_xp.img as the hard disk file, /dev/cdrom as the CD, 384 MB of memory (a good minimum for XP) and will boot from the CD.

You can also install from an iso image file.

qemu -localtime -cdrom cdimagefile.iso -m 384 -boot d windows_xp.img
Note: Use 'qemu-system-x86_64' instead of 'qemu' if you are an 64bit user. Otherwise the "-kernel-kqemu" option will not work.
Note: To install Windows 2000, add the option '-win2k-hack'. Otherwise the install may fail with "Not enough disk space" even when that isn't the case.

[edit] References

Personal tools