QEMU

From ArchWiki
Revision as of 04:28, 24 July 2005 by SleepyDog (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Qemu is a fast cpu emulator using dynamic translation to achieve good emulation speed. Unlike VMWARE and Win4Lin, it emulates the cpu instead of only virtualizing the computer. This means that it is considerably slower, but on the other hand it is much more portable, stable and secure. Plus it is opensource. It is great solution if you want to run some simple Windows applications like MS Office on a fast PC and you want to ensure maximum compatibility. It is also a great tool for operating system development. This HOWTO describes how to set up the emulator and install Windows 9x/2000 under it.

Qemu has the homepage at http://fabrice.bellard.free.fr/qemu/.

Choosing Qemu version

The current qemu version is 0.7.0. However, you should consider installing a cvs version of qemu if the emulated machine is supposed to run under Windows 2000 (or previous NT versions). It is also advised to apply a patch which can be found at http://www.dad-answers.com/qemu/patches/win2k-diskfull-fix/ - otherwise you'll have problems during OS installation.

Choosing Windows version

Qemu can run any version of Windows. However, 98, Me and XP will run at quite a low speed. You should choose either Windows 95 or Windows 2000. Suprisingly, 2000 seems to run faster than 98. The fastest one is 95, which can from time to time make you forget that you are running an emulator :)

If you own both Win95 and win98/winme, then 98lite (from http://www.litepc.com) might be worth trying. It decouples Internet Explorer from operating system and replaces it with original Windows 95 Explorer. It also enables you to do a minimal Windows installion, without all the bloat you normally cannot disable. This might be the best option, because you get the smallest, fastest and most stable Windows this way.

Creating qemu package

As of writing this, Arch linux doesn't have an official qemu package. This means that you will have to build one yourself. To do so, you need ABS installed.

Login as root. Create a directory /var/abs/local/qemu

Create the file /var/abs/local/qemu/PKGBUILD with the following contents (if you want stable version):

pkgname=qemu
pkgver=0.7.0
pkgrel=1
url=\"http://fabrice.bellard.free.fr/qemu/\"
pkgdesc=\"QEMU is a fast portable CPU emulator\"
depends=('sdl' 'xorg')
source=(http://fabrice.bellard.free.fr/$pkgname/$pkgname-$pkgver.tar.gz kqemu-0.6.2-1.tar.gz configure.patch)

build() {
    cd $startdir/src/$pkgname-$pkgver
    mv $startdir/src/kqemu .
    patch -p1 < $startdir/configure.patch

    ./configure --target-list<code>i386-softmmu --prefix</code>/usr
    mv config-host.h config-host.h.tmp

    ./configure --target-list<code>i386-softmmu --prefix</code>$startdir/pkg/usr
    mv -f config-host.h.tmp config-host.h
    make ||| return 1
    make install
    rm -r $startdir/pkg/usr/share/doc
    mv $startdir/pkg/usr/share/man $startdir/pkg/usr/man
}

(or if you want a snapshot (SOMEBODY PLEASE UPDATE!!))

pkgname=qemu
pkgver1=snapshot-2004-12-26_23
pkgver=snapshot2004122623
pkgrel=1
url=\"http://fabrice.bellard.free.fr/qemu/\"
pkgdesc=\"QEMU is a fast portable CPU emulator\"
depends=('sdl' 'xorg')
source=(http://www.dad-answers.com/qemu/qemu-$pkgver1.tar.bz2 http://fabrice.bellard.free.fr/qemu/kqemu-0.6.2-1.tar.gz configure.patch)

build() {
    cd $startdir/src/$pkgname-$pkgver1
    patch -p1 < $startdir/configure.patch

    ./configure --target-list<code>i386-softmmu --prefix</code>/usr
    mv config-host.h config-host.h.tmp

    ./configure --target-list<code>i386-softmmu --prefix</code>$startdir/pkg/usr
    mv -f config-host.h.tmp config-host.h

    for a in *.texi; do echo -n > `basename $a .texi`.html; done
    make ||| return 1
    make install
    rm -r $startdir/pkg/usr/share/doc
    mv $startdir/pkg/usr/share/man $startdir/pkg/usr/man
}

Create the file /var/abs/local/qemu/configure.patch with the following contents:

--- qemu-0.6.1/configure	2004-11-14 21:51:33.000000000 +0100
+++ qemu-0.6.1a/configure	2004-12-27 04:43:18.000000000 +0100
@@ -257,7 +257,7 @@
 if test \"$sdl\" = \"yes\" ; then
 aa=\"no\"
 `$sdl_config --static-libs || grep \\\-laa > /dev/null` && aa=\"yes\"
-sdl''static''libs=`$sdl_config --static-libs`
+sdl''static''libs=`$sdl_config --libs`
 if [[ \"$aa\" = \"yes\" ]] ; then
   sdl''static''libs=\"$sdl''static''libs `aalib-config --static-libs`\"
 fi

Run makepkg. Install the package with \"pacman -A qemu-*.pkg.tar.gz\".

Now you can log out from the root account.

Creating the hard disk image

To run qemu you will probably need a hard disk image. This is a file which stores the contents of the emulated hard disk.

Use the command:

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

to create the image file named \"win.qcow\". The \"4G\" parameter specifies the size of the disk - in this case 4 GB. You can use suffix M for megabytes (for example \"256M\"). You shouldn't worry too much about the size of the disk - the qcow format compresses the image so that the empty space doesn't add up to the size of the file.

Preparing the installation media

The installation CD-ROM/floppy shouldn't be mounted, because Qemu accesses the media directly. It is a good idea to dump CD-ROM and/or floppy to a file, because it both improves performance and doesn't require you to have direct access to the devices (that is, you can run Qemu as a regular user). For example, if the CD-ROM device node is named \"/dev/cdrom\", you can dump it to a file with the command:

dd if<code>/dev/cdrom of</code>win98icd.iso

Installing the operating system

This is the first time you will need to start the emulator. One thing to keep in mind: when you click inside qemu window, the mouse pointer is grabbed. To release it press Ctrl+Alt.

If you need to use a bootable floppy, run Qemu with:

qemu -cdrom [[cdrom''image]] -fda [[floppy''image]] -boot a [[hd_image]]

If your CD-ROM is bootable, run Qemu with:

qemu -cdrom [[cdrom''image]] -boot d [[hd''image]]

Now partition the virtual hard disk, format the partitions and install the OS.

A few hints:

  1. If you are using Windows 95 boot floppy, then choosing SAMSUNG as the type of CD-ROM seems to work
  2. There are problems when installing Windows 2000. Windows setup will generate a lot of edb*.log files, one after the other containing nothing but blank spaces in C:\WINNT\SECURITY which quickly fill the virtual harddisk. A workaround is to open a Windows command prompt as early as possible during setup (by pressing Shift-F10) which will allow you to remove these log files as they appear by typing :
del %windir%\security\*.log

Running the system

To run the system simply type:

qemu [hd_image]

A good idea is to use overlay images. This way you can create hard disk image once and tell Qemu to store changes in external file. You get rid of all the instability, because it is so easy to revert to previous system state :)

To create an overlay image, type:

qemu-img create -b [[base''image]] -f qcow [[overlay''image]]

Substitute the hard disk image for base_image (in our case win.qcow). After that you can run qemu with:

qemu [overlay_image]

and the original image will be left untouched.

Moving data between host and guest OS

If you have servers on your host OS they will be accessible with the ip-address 10.0.2.2 without any further configuration. So you could just FTP or SSH, etc to 10.0.2.2 from windows to share data, or if you would like to use samba:

Samba

Qemu supports SAMBA which allows you to mount host directories during the emulation. It seems that there is an incompatibility with SAMBA 3.x. and some versions of qemu. But at least with a current snapshot of qemu it should be working.

First, you need to have a working samba installation. Then add the following section to your smb.conf:

[qemu]
   comment = Temporary file space
   path = /tmp
   read only = no
   public = yes

Now start qemu with:

qemu [hd_image] -smb qemu

Then you should be able to access your host's smb-server with the ip-address 10.0.2.2. If you're running Win9x as guest OS, you may need to add

10.0.2.2 smbserver

to c:\windows\lmhosts.

Mounting the hard disk image

Fortunately there is a way to mount the hard disk image with a loopback device. Login as root, make a temporary directory and mount the image with the command:

mount -o loop,offset=32256 [[hd''image]] [[tmp''dir]]

Now you can copy data in both directions. When you are done, umount with:

umount [hd_image]

The drawback of this solution is that you cannot use it with qcow images (including overlay images). So you need to create you images without \"-f qcow\" option. Tip: create a second, raw hardrive image. This way you'll be able to transfer data easily and use qcow overlay images for the primary drive.

REMEMBER: never run qemu when the image is mounted!


Optimizing Windows 9x CPU usage

Windows 9x doesn't use hlt instruction, so the emulator always eats up 100% CPU even if no computation is being done. Grab the file http://www.user.cityline.ru/~~maxamn/amnhltm.zip, unpack it, copy it to the image and run the .bat file.

Using the QEmu Accelerator Module

The developers of qemu have created a kernel module to accelerate qemu to sometimes near native levels. To use this module simply add \"kqemu\" to your list of modules in /etc/rc.conf and add this to your /etc/rc.local:

# Create the KQEMU device
mknod /dev/kqemu c 250 0
chmod 666 /dev/kqemu