HOWTO: QEMU on FreeBSD

vermaden

Son of Beastie

Reaction score: 1,411
Messages: 2,939

I wrote short qemu howto install/usage on FreeBSD as host os, and in this example with Win2000 as guest os.

changelog:
1. removed useless /etc/qemu-ifup script [it was used in older versions of qemu with tap/bridge interfaces]
2. added info about sound emulation inside qemu OS.
3. removed HACKS_CIRRUS from options [thx to scottro sugestion], reason here: http://forums.bsdnexus.com/viewtopic.php?pid=14036#p14036
4. sometimes OS installation requires unload of kqemu. short: install without kqemu and the use with kqemu

Install qemu with options showed below:
Code:
# cd /usr/ports/emulators/qemu && make install clean

Code:
Options for qemu:
 [X] KQEMU          Build with (alpha!) accelerator module
 [ ] HACKS_CIRRUS   Large display speedup (buggy!)
 [ ] RTL8139_TIMER  allow use of re(4) nic with FreeBSD guests
 [ ] SAMBA          samba dependency (for -smb)
 [X] SDL            SDL/X dependency (graphical output)
 [X] CDROM_DMA      IDE CDROM DMA

install and configure sudo:
Code:
# cd /usr/ports/security/sudo && make install clean

sudo config:
Code:
# cat > /usr/local/etc/sudoers << EOF
root            ALL=(ALL) ALL
username        ALL=(ALL) NOPASSWD: ALL
EOF

username is YOU.

load needed modules:
Code:
# sudo kldload aio
# sudo kldload kqemu


create Windows2000 install CD image, use cat or dd:
Code:
% cat /dev/acd0 > ~/qemu/win2k.iso
% dd bs=2048 if=/dev/acd0 of=~/qemu/win2k.iso

create qemu image for new OS:
Code:
% qemu-img create -f qcow2 ~/qemu/win2000.img 4G

install new os, Windows2000 in our example:
Code:
% qemu -boot d -hda ~/qemu/win2000.img -m 128 -cdrom ~/qemu/win2k.iso -win2k-hack -localtime


if You have any problems while installing the OS then unload kqemu module and install again, then after install, load the kqemu module again:
Code:
% sudo kldunload kqemu
% qemu -boot d -hda ~/qemu/win2000.img -m 128 -cdrom ~/qemu/win2k.iso -win2k-hack -localtime
% sudo kldload kqemu


typical usage of OS under qemu:
Code:
% qemu -hda ~/qemu/win2000.img -m 256 -localtime

typical usage of OS under qemu in snapshot mode [to commit made changes hit left_CTRL+left_ALT+2 and type commit]:
Code:
% qemu -hda ~/qemu/win2000.img -m 256 -localtime -snapshot

1. to switch focus between qemu mouse focus and X11 mouse focus use: left_CTRL+left_ALT
2. to ensure that You use kqemu kernel module in user mode hit left_CTRL+left_ALT+2 and type info kqemu, if everything is ok You will see:
Code:
kqemu support: enabled for user code

if not You will see:
Code:
kqemu support: disabled

if disabled it will work, but terribly slow, You will be running at Pentium 75 speed at host with CPU AthlonXP 1.66GHz.
3. to switch between qemu console and qemu os emualtion use left_CTRL+left_ALT+2 to go to console and left_CTRL+left_ALT+1 to back to emulation.
4. enabling network on emulated os, use DHCP configuration inside emulated os to get automatic IP adress 10.x.x.x

after You quit You os inside emulated os, or by qemu console left_CTRL+left_ALT+2 and type quit use this command to launch again Your emulated OS:
Code:
% qemu -hda ~/qemu/win2000.img -m 256 -localtime



How to have sound in qemu OS, You just need to launch qemu the way I listed above, only with -soundhw sb16 or -soundhw es1370 to emulate Sound Blaster 16, or Sound Blaster 128. You can also enable standart annoying BEEP with -soundhw pcspk. You can also enable all three of them like that: -soundhw sb16,es1370,pcspk, or just BEEP + one of them: -soundhw es1370,pcspk, examples
Code:
% qemu -hda ~/qemu/win2000.img -m 256 -localtime -soundhw pcspk

Code:
% qemu -hda ~/qemu/win2000.img -m 256 -localtime -soundhw es1370,pcspk

Code:
% qemu -hda ~/qemu/win2000.img -m 256 -localtime -soundhw es1370,pcspk,sb16



fluxbox add-on:
in ~/.fluxbox/apps:
Code:
# qemu
[app] (class=SDL_App)
  [Workspace]  {2}
  [Deco]       {BORDER}
[end]


resources:

man qemu-img
man qemu
man aio
man dd
man cat
http://kidsquid.com/cgi-bin/moin.cgi/QuickStartGuide
http://qemu-forum.ipi.fi/qemu-snapshots/
http://qemu-forum.ipi.fi/
http://qemu.org/user-doc.html
http://en.wikipedia.org/wiki/QEMU


The same schema works for Solaris 10 and WindowsXP, You only do not use -win2k-hack.

feel free to point errors, typo and Your hints/suggestions here.
 

SPlissken

Active Member

Reaction score: 6
Messages: 151

Incredible , thanks for this
Here is 2 screenshots



And this one, WinXP Service Pack 3 and video using Flash on french website Allocine (talking about movie)
No problem while watching the video, sound working too

 

lme@

Administrator
Staff member
Administrator
Moderator
Developer

Reaction score: 303
Messages: 776

How fast does XP run with kqemu?
 

SPlissken

Active Member

Reaction score: 6
Messages: 151

Well, it s on my laptop with 2 Go of ram
Here is part on dmesg
CPU: Intel(R) Core(TM)2 CPU T7200 @ 2.00GHz (1995.02-MHz 686-class CPU)
Origin = "GenuineIntel" Id = 0x6f6 Stepping = 6

I allow 512 Mo to winXP , you can browse on internet and as i said you can watch flash movie , like those on youtube.
 

rliegh

Member

Reaction score: 5
Messages: 54

One comment and one question:
comment: It's probably better, and certainly easier to add yourself to the sudoers file using the visudo command.

question: on several platforms (including freebsd 7.1/amd64) I've compiled qemu from source, including kqemu -only to find that the results of the "info kqemu" command is "support not compiled in". I've had this happen on Linux and OpenBSD (solaris too, I think -but I'm not sure). Most recently I had this happen on FreeBSD 7.1/amd64 (dispite explicitly selectiong the kqemu option).

Has anyone else encountered this and found a way around it?
 
OP
vermaden

vermaden

Son of Beastie

Reaction score: 1,411
Messages: 2,939

I have been using QEMU only on i386 systems so I would not help here.
 

rliegh

Member

Reaction score: 5
Messages: 54

vermaden said:
I have been using QEMU only on i386 systems so I would not help here.
It seems pretty hit-and-miss; but I just compiled qemu and it seems like this run does support kqemu.
 

caligula

New Member

Reaction score: 1
Messages: 10

vermaden said:
1. to switch focus between qemu mouse focus and X11 mouse focus use: left_CTRL+left_ALT


Hello,

use the qemu options "-usb -usbdevice tablet" and free your mouse by motion.
 

nox@

Member
Developer

Reaction score: 46
Messages: 78

rliegh said:
...
question: on several platforms (including freebsd 7.1/amd64) I've compiled qemu from source, including kqemu -only to find that the results of the "info kqemu" command is "support not compiled in". I've had this happen on Linux and OpenBSD (solaris too, I think -but I'm not sure). Most recently I had this happen on FreeBSD 7.1/amd64 (dispite explicitly selectiong the kqemu option).

Has anyone else encountered this and found a way around it?

I suspect you are using qemu 0.9.1 (emulators/qemu)? If yes the answer is in the port's pkg-message (can also be read via % pkg_info -D qemu\* |less):
Code:
...
- also remember that on amd64 you need to run the amd64 (x86_64) system
emulation if you want to use kqemu, i.e. run qemu-system-x86_64 instead of
qemu (the latter only emulates a 32 bit system.)  ...


Btw, this has since changed in qemu svn, a snapshot of which is in ports as emulators/qemu-devel.
 

nox@

Member
Developer

Reaction score: 46
Messages: 78

vermaden said:
...
changelog:
...
3. removed HACKS_CIRRUS from options [thx to scottro sugestion], reason here: http://forums.bsdnexus.com/viewtopic.php?pid=14036#p14036

Actually that patch (HACKS_CIRRUS) no longer applied and has been removed from the qemu ports since.

vermaden said:
...
4. sometimes OS installation requires unload of kqemu. short: install without kqemu and the use with kqemu

..or you can do: % qemu -no-kqemu ...

vermaden said:
...
2. to ensure that You use kqemu kernel module in user mode hit left_CTRL+left_ALT+2 and type info kqemu, if everything is ok You will see:
Code:
kqemu support: enabled for user code

If you want kqemu to run (guest) kernel code too do: % qemu -kernel-kqemu ... This is (usually) even faster, but works for less guests than userland kqemu, so you may need to experiment.

vermaden said:

Also, there are FreeBSD host notes in the relevant qemu port's pkg-message(s) which can be read by
% pkg_info -D qemu\* |less if the port is installed (which also answer FAQs like the kqemu not compiled into the 32 bit qemu 0.9.1 on amd64), and there are the qemu-devel@nongnu.org and freebsd-emulation@FreeBSD.org mailing lists, see
http://lists.gnu.org/mailman/listinfo/qemu-devel and http://lists.freebsd.org/mailman/listinfo/freebsd-emulation respectively. (a nice way to search FreeBSD mailinglists is also http://freebsd.markmail.org/search/.)

And finally, if you want to try svn snapshots: a (hopefully, but currently it seems to be) stable one is usually in ports as emulators/qemu-devel, and once in a while I post experimental port updates for more recent snapshots on the above mailing lists - testers are always welcome...
 

cloud

New Member


Messages: 11

A question with Qemu :

I have to log all incoming and outgoing traffic. But in my case, all the traffic use an existing interface, ath0 on my pc, and if I try to sniff, the guest address is nated so I can't know wich packets comes from the guest...

Someone have a solution ?
 
OP
vermaden

vermaden

Son of Beastie

Reaction score: 1,411
Messages: 2,939

@nox

I havent updated it lately

nox@ said:
Also, there are FreeBSD host notes in the relevant qemu port's pkg-message(s) which can be read by ...

FreeBSD Handbook does not provide any info about FreeBSD as a host system:
http://freebsd.org/handbook/virtualization-host.html

I have created myself such page ans submited it for inclusion info the FreeBSD's Handbook, but there was lack of interest on the mailing lists for that unfortunelly :/
http://toya.net.pl/~vermaden/FreeBSD-Handbook-Virtualization.htm
 

nox@

Member
Developer

Reaction score: 46
Messages: 78

cloud said:
A question with Qemu :

I have to log all incoming and outgoing traffic. But in my case, all the traffic use an existing interface, ath0 on my pc, and if I try to sniff, the guest address is nated so I can't know wich packets comes from the guest...

Someone have a solution ?

Well, if you are using slirp (qemu's internal nat) your best bet is probably to simply run tcpdump in the guest and transfer the log out later. Otherwise you could use tuntap instead of slirp and sniff on the tap(4) interface, but since you are using wifi bridging will usually not work so you'd have to nat on the host instead. (using eg pf(4) or ipfw(8)/natd(8).)

Oh, or maybe you could run qemu as an otherwise idle user and use pf(4) to log packets on sockets belonging to that user...
 

nox@

Member
Developer

Reaction score: 46
Messages: 78

vermaden said:
@nox

I havent updated it lately

Well this was more for the benefit of other readers than to criticise you... :)

vermaden said:
...
FreeBSD Handbook does not provide any info about FreeBSD as a host system:
http://freebsd.org/handbook/virtualization-host.html

I have created myself such page ans submited it for inclusion info the FreeBSD's Handbook, but there was lack of interest on the mailing lists for that unfortunelly :/
http://toya.net.pl/~vermaden/FreeBSD-Handbook-Virtualization.htm

Oh, I hadn't seen that before! Actually I had started some docbook myself a while ago that finally ended up in this thread (read the followups too): http://lists.freebsd.org/pipermail/freebsd-doc/2008-December/015224.html

Maybe you can follow up to that...
 
OP
vermaden

vermaden

Son of Beastie

Reaction score: 1,411
Messages: 2,939

nox@ said:
Well this was more for the benefit of other readers than to criticise you... :)

I did not took your comment as something bad, imho good you pointed out updates that I havent checked lately.

nox@ said:
Oh, I hadn't seen that before! Actually I had started some docbook myself a while ago that finally ended up in this thread (read the followups too): http://lists.freebsd.org/pipermail/freebsd-doc/2008-December/015224.html

Maybe you can follow up to that...

Nice work mate, I also havent seen it unfortunelly.

"follow up to that"? You mean include your work in mine part?
 

nox@

Member
Developer

Reaction score: 46
Messages: 78

vermaden said:
...
Nice work mate, I also havent seen it unfortunelly.

"follow up to that"? You mean include your work in mine part?

Follow up to that thread (hmm maybe this is usenet lingo and young ppl don't know it :) - it means post an answer to that thread.)

Anyway, yes you could make a `best-of-both' and post it on there... (preferably as a patch in docbook as I did it so others don't have to convert it.)
 

Maxamoto

Member


Messages: 39

usb_add hard drive?

Is the assumption that it is impossible to add a USB Western Digital hard drive, either when launching qemu or via console (usb_add) correct? I have a 1TB WD hard drive that I have not been able to add at all. qemu doesn't see it with "info usbhost". I don't have it mounted from within FreeBSD. Here's the output of usbdevs -dv

# usbdevs -dv
Controller /dev/usb0:
addr 1: full speed, self powered, config 1, UHCI root hub(0x0000), Intel(0x0000), rev 1.00
uhub0
port 1 powered
port 2 powered
Controller /dev/usb1:
addr 1: full speed, self powered, config 1, UHCI root hub(0x0000), Intel(0x0000), rev 1.00
uhub1
port 1 powered
port 2 powered
Controller /dev/usb2:
addr 1: high speed, self powered, config 1, EHCI root hub(0x0000), Intel(0x0000), rev 1.00
uhub2
port 1 powered
port 2 powered
port 3 addr 2: high speed, power 500 mA, config 1, Zune(0x0710), Microsoft(0x045e), rev 1.00
ugen0
port 4 powered
Controller /dev/usb3:
addr 1: full speed, self powered, config 1, UHCI root hub(0x0000), Intel(0x0000), rev 1.00
uhub3
port 1 powered
port 2 powered
Controller /dev/usb4:
addr 1: full speed, self powered, config 1, UHCI root hub(0x0000), Intel(0x0000), rev 1.00
uhub4
port 1 addr 2: low speed, power 100 mA, config 1, Microsoft 5-Button Mouse with IntelliEye(TM)(0x0039), Microsoft(0x045e), rev 3.00
ums0
port 2 powered
Controller /dev/usb5:
addr 1: full speed, self powered, config 1, UHCI root hub(0x0000), Intel(0x0000), rev 1.00
uhub5
port 1 powered
port 2 powered
Controller /dev/usb6:
addr 1: high speed, self powered, config 1, EHCI root hub(0x0000), Intel(0x0000), rev 1.00
uhub6
port 1 addr 2: high speed, self powered, config 1, My Book(0x1100), Western Digital(0x1058), rev 1.65
umass0
port 2 powered
port 3 powered
port 4 powered
port 5 powered
port 6 addr 3: high speed, power 500 mA, config 1, Laptop Integrated Webcam(0x2640), OmniVision Technologies, Inc. -2640-07.07.20.3(0x05a9), rev 1.00
ugen1


My command to launch the qemu vm is as such:

qemu -boot d -hda winxp.img -m 256 -cdrom -soundhw es1370 -usb -usbdevice tablet -localtime

Modifying this string, or attempting to add the device via qemu's console always ends in "could not add device" error. Thanks in advance for your help :)
 

adamk

Daemon

Reaction score: 266
Messages: 1,622

If the drive is detected under the FreeBSD host, why not just use "-hdb /dev/da0" (or whatever device the drive shows up as) when starting qemu?

Adam
 

Maxamoto

Member


Messages: 39

adamk said:
If the drive is detected under the FreeBSD host, why not just use "-hdb /dev/da0" (or whatever device the drive shows up as) when starting qemu?

Adam

Thanks for the suggestion. I'll give that a shot and see what happens :)
 

da_oli

New Member


Messages: 3

Great HowTo, but how can i get the network working.
On my old qemu installation i had a script that did something like this bevor qemu starts:
sudo ifconfig tap0 create
sudo /sbin/ifconfig tap0 0.0.0.0 promisc up
sudo ifconfig bridge0 create
sudo ifconfig bridge0 addm xl0 up
sudo ifconfig bridge0 addm tap0

So, how is it working in this howto?

greets

Oliver
 

da_oli

New Member


Messages: 3

Sorry for the double post, but i the solution i'm searching for is for the use of TUN/TAP-Network-Interfaces to get a complete access to the vm

greets

oliver
 

da_oli

New Member


Messages: 3

Okay and here is the solution :p

I created a startup script for the first vm
Code:
#!/bin/sh
sudo ifconfig tap0 create
sudo ifconfig tap0 0.0.0.0 promisc up
sudo ifconfig bridge0 create
sudo ifconfig bridge0 addm em0 up
sudo ifconfig bridge0 addm tap0
sudo qemu -k de -boot c -hda test.img -m 512 -localtime -monitor stdio -usb -usbdevice tablet -net nic -net tap,ifname=tap0,script=no


and one for the second

Code:
#!/bin/sh
sudo ifconfig tap1 create
sudo /sbin/ifconfig tap1 0.0.0.0 promisc up
sudo ifconfig bridge0 addm tap1
sudo qemu -k de -boot c -hda test.img -m 512 -localtime -monitor stdio -usb -usbdevice tablet -net nic,macaddr=52:54:00:12:34:57 -net tap,ifname=tap1,script=no


Important in here is the MAC address, a very tricky thing for those who don't like to read manuals like me of course :stud

But okay, all qemu's use the same MAC 52:54:00:12:34:56
That's why its important to change the MAC on the following or all qemu's.

That works fine for me.

And sorry for the triple Post :e
 
Top