From owner-freebsd-doc@FreeBSD.ORG Wed Dec 17 16:22:07 2008 Return-Path: Delivered-To: freebsd-doc@FreeBSD.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 56EE11065676; Wed, 17 Dec 2008 16:22:07 +0000 (UTC) (envelope-from loader@FreeBSD.org) Received: from freefall.freebsd.org (freefall.freebsd.org [IPv6:2001:4f8:fff6::28]) by mx1.freebsd.org (Postfix) with ESMTP id 3DBD88FC13; Wed, 17 Dec 2008 16:22:07 +0000 (UTC) (envelope-from loader@FreeBSD.org) Received: from freefall.freebsd.org (loader@localhost [127.0.0.1]) by freefall.freebsd.org (8.14.3/8.14.3) with ESMTP id mBHGM793035017; Wed, 17 Dec 2008 16:22:07 GMT (envelope-from loader@freefall.freebsd.org) Received: (from loader@localhost) by freefall.freebsd.org (8.14.3/8.14.3/Submit) id mBHGM6b5035016; Wed, 17 Dec 2008 16:22:06 GMT (envelope-from loader) Date: Wed, 17 Dec 2008 16:22:06 GMT Message-Id: <200812171622.mBHGM6b5035016@freefall.freebsd.org> To: murray@FreeBSD.org References: <200812150128.mBF1Sehd094548@freefall.freebsd.org> <20081215130236.B5E051D703EF@mail.freebsdmall.com> From: loader In-Reply-To: <20081215130236.B5E051D703EF@mail.freebsdmall.com> (loader@freebsdmall.com's message of "Mon\, 15 Dec 2008 05\:02\:36 -0800 \(PST\)") MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Cc: freebsd-doc@FreeBSD.org Subject: Re: docs/127923: Please mention qemu in the FreeBSD Handbook X-BeenThere: freebsd-doc@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Documentation project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 17 Dec 2008 16:22:07 -0000 --=-=-= loader wrote: > murray@FreeBSD.org wrote: > >> Synopsis: Please mention qemu in the FreeBSD Handbook >> >> Responsible-Changed-From-To: freebsd-doc->loader >> Responsible-Changed-By: murray >> Responsible-Changed-When: Mon Dec 15 01:28:02 UTC 2008 >> Responsible-Changed-Why: >> Loader, didn't you have some patchecs to add qemu here? Can you add >> something about it to the virtualization chapter? >> >> >> http://www.freebsd.org/cgi/query-pr.cgi?pr=127923 > > Okay, I will try to add a section about Qemu these days. Juergen Lock sent me a patch. --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=virtualization.diff Index: virtualization/chapter.sgml =================================================================== RCS file: /home/dcvs/doc/en_US.ISO8859-1/books/handbook/virtualization/chapter.sgml,v retrieving revision 1.17 diff -u -p -r1.17 chapter.sgml --- virtualization/chapter.sgml 28 Jul 2008 14:33:51 -0000 1.17 +++ virtualization/chapter.sgml 18 Aug 2008 17:36:31 -0000 @@ -54,6 +54,11 @@ virtualization. + + How to run FreeBSD as guest in qemu + and as a qemu host. + + Before reading this chapter, you should: @@ -958,6 +963,211 @@ xenbr1 8000.feffffffffff no + + + What you should know about qemu + qemu's homepage is + http://bellard.org/qemu/, documentation gets + installed (on &os;) as + /usr/local/share/doc/qemu/qemu-doc.html and + /usr/local/share/doc/qemu/qemu-tech.html, and + there also is a manpage. + Places to check other than the official documentation: + + + The pkg-message of the relevant &os; + port (can also be displayed via + &prompt.user; pkg_info -D qemu\* |less + if a qemu port is installed.) + + + The + unofficial #qemu FAQ (many of the Linux-related notes + there also apply to &os; hosts.). + + + The + qemu forum that also has howtos/patches/svn snapshots. + (actually at the time of writing the snapshots are apparently + still checked out from cvs and are old as such, you can go + to a + git mirror of the qemu svn to get snapshots, note + however that upstream qemu sources don't build on &os; without + changes.) + + + The + qemu-devel mailing list archive. + + + The + freebsd-emulation mailing list archive. + + + If you still have a question after checking the above you can + join the official qemu irc channel + + #qemu on freenode irc (see maybe the + + Wikipedia article on irc and + + http://freenode.net/using_the_network.shtml + if you are new to irc), or post a question on the + freebsd-emulation mailing list if the question is + &os;-specific, or on the + qemu-devel mailing list if it concerns qemu in general. + Or you can join the mentioned + qemu webforum and post the question there. + General things you should know: + + + For best results always try the latest version + (0.9.1 as of this writing) + or even an svn checkout or on &os; the + emulators/qemu-devel + port. + + + qemu's userland networking (-net user, + also called slirp, + which is the default) is not really stable on 64 bit hosts, + so you probably need to use tuntap there (see also + + if this is for a &os; host.) + + + You need to use qemu-system-x86_64 + if you want to emulate + amd64 guests, or if you want to use kqemu on amd64 hosts. + (actually the latter is no longer true for qemu svn and the + emulators/qemu-devel + port at the time of writing.) + + + You can try pcnet, + i82557b or e1000 + (the latter not yet + in 0.9.1), especially with BSD/Linux guests as alternate + emulated nics in place of the default + ne2kpci, they should be a little faster/use less guest cpu. + + + You can also try -drive if=scsi,file=.. + instead of -hdb ... + to specify additional image files (qemu's bios can't boot + from SCSI yet at the time of writing), SCSI should perform + better than IDE (especially with Linux guests, less + with &os;.) + + + And you can try -vnc ... if the host + or qemu build lacks X and + -nographic or -curses + (the latter not yet in 0.9.1) isn't enough + (-nographic needs the guest configured for + serial console.) + -vnc works best with an en-us keymap on the host. + You can + use e.g. the &os; net/tightvnc + port with qemu. The X keymap on the host can be changed + temporarily using + setxkbmap, see its manpage for details. + + + iso images are faster than physical CD/DVD drives. + + + + + + &os; as a qemu guest + + First a few things you should know: + + + &os;/amd64 guests may need a patch to qemu: + i386-hard-interrupt-generation-bug-fix + (unfortunately not yet committed to mainline qemu, or + I missed it... the &os; + emulators/qemu-devel port + has it tho.) + + + Debian/Ubuntu (and their offsprings) have a record of + shipping partially broken + qemu bioses, so if you have problems using a packaged qemu on + these distros also try using the bios found in that qemu + version's(!) source tarball (pc-bios/bios.bin + in there) instead of the one installed by your distro + package. + + + + + An example how to install &os; 7.0 as a qemu guest + First, we create an empty raw 5 GB image: + &prompt.user; qemu-img create 7.0.img 5G + (You may want -f qcow2 for qcow2 format + if you have to/want to avoid sparse files on the host - + some backup apps like &man.bsdtar.1; + don't support them properly, others like gtar or star do if passed + appropriate flags - qcow2 might be slower than raw.) + Next, we boot sysinstall with the image: + &prompt.user; qemu -m 256 -cdrom 7.0-RELEASE-i386-disc1.iso -hda 7.0.img -boot d -monitor stdio + Now follow the instructions in like + as if you would install a real machine using CD media... + Click into the guest window to access the guest mouse pointer + and hit Alt + Ctrl to leave the guest window + mouse grab. If you use the default usermode networking (aka slirp) + like in the above example the guest should have network if you + configure it to use DHCP, so if you want you could probably + also use a bootonly iso instead of disc1 and do a network install. + (&man.ping.8 doesn't work with slirp tho.) + When finished, select `exit install' in sysinstall to reboot, + and when you see the guest booting the iso again, type + q in qemu's monitor (or + killall qemu on another host shell if you are + using -curses, like + when you have no X on the host.) + Now, we should be able to boot the installed image: + &prompt.user; qemu -m 256 -cdrom 7.0-RELEASE-i386-disc1.iso -hda 7.0.img -boot c -monitor stdio + (You can omit -cdrom if you don't plan to + access the install iso from the installed guest.) + Now you should see a normal boot like on a freshly installed + box, all the way up to your first login: prompt. If you plan to + use this guest with -nographic later on, + now is the time to configure it for a serial console: + (both in the guest:) + &prompt.root; echo console=\"comconsole\" >>/boot/loader.conf + &prompt.root; sed -i -e '/^ttyd0/s/off/on/' /etc/ttys + As always with virtualization, lowering + helps performance also with qemu, so you might want to do that too: + (in the guest:) + &prompt.root; echo kern.hz=100 >>/boot/loader.conf + When you are finished with the guest, shut it down, this should + make qemu quit gracefully (in the guest:) + &prompt.root; shutdown -p now + If you just configured for serial console, you can now boot the + guest with -nographic: + &prompt.user; qemu -m 256 -hda 7.0.img -boot c -nographic + With -nographic, the guest console and qemu's + monitor are multiplexed on qemu's tty, hit + Ctrl + a and then h + to show a small help. + + @@ -968,7 +1178,149 @@ xenbr1 8000.feffffffffff no use older versions of VMware in this capacity. Work is also ongoing in getting &xen; to work as a host environment on FreeBSD. + qemu also often works, although some guests + work better than others, and the kqemu accellerator kernel module + still works better (i.e. with more guests) on i386 hosts than + on amd64 - although the latter has improved with qemu svn at the time of + writing, a snapshot of which is available as the + emulators/qemu-devel port. + (But it is still slower than VMware.) + Also kqemu was broken for a long time on amd64 SMP, so + be sure to use recent qemu and kqemu ports if your host is such. + + &os; as a qemu Host + + Notes about the current state of affairs re. qemu on &os; hosts + can always be found in the pkg-message + of the relevant qemu port (there are two at the moment, + emulators/qemu and + emulators/qemu-devel, + the first is usually the latest release and the latter is an + svn snapshot). If you already have a qemu port installed you can + also view those notes via: + &prompt.user; pkg_info -D qemu\* |less + Here are a few facts that are noted there which are unlikely to + change: + + + If you want to use qemu with -m 512 + or larger on i386 &os; 6.x hosts you need to increase the + kern.maxdsiz + tunable in the host's &man.loader.conf.5 since the default + is 512 MB, and qemu needs memory for itself also. + (&os; 7.0 and up use jemalloc which uses &man.mmap.2 and + isn't affected by kern.maxdsiz anymore.) + + + If you use kqemu make sure your kqemu.ko + is always in sync + with the kernel (like with any kld installed outside of base), + i.e. rebuild the + emulators/kqemu-kmod + port (or + emulators/kqemu-kmod-devel + depending on which qemu port you use) whenever you update + the kernel - especially + if you are switching branches or are following a -stable or even + -current branch! + + + qemu now uses &man.aio.4 at least for ide dma, so if you get + `Invalid system call' crashes that is because aio is not + (kld)loaded. + + + You can enable autoloading of kqemu (and aio) at boot by + adding a line + kqemu_enable=YES + to /etc/rc.conf (assuming you have built + the qemu port with the kqemu knob enabled of course). + + + + Simple Linux guest examples + As an example, we run the well known Knoppix Linux live-CD/DVD in qemu: + &prompt.user; qemu -m 256 -cdrom KNOPPIX_V5.3.1DVD-2008-03-26-EN.iso -boot d -monitor stdio -soundhw es1370 + (Omit -soundhw es1370 if you don't have sound + configured on the host.) + If you want a smaller download you can also try Damn Small Linux + (around 50 MB): + &prompt.user; qemu -m 256 -cdrom dsl-4.3.iso -boot d -monitor stdio -soundhw es1370 + Again, click into the guest window to access the guest + mouse pointer and + hit Alt + Ctrl to leave the guest window + mouse grab. + + + Simple Example for using qemu tuntap networking on + &os; Hosts + In cases where you want a qemu guest to appear as a seperate + (virtual) host on your network, or when you are on an amd64 host + where the default usermode networking doesn't quite work, or + when you are simply concerned about virtual network performance, + you may want to use tuntap networking. + The idea here is to have qemu talk to a &man.tap.4; interface, + which you (usually) bridge with your host's physical interface, + and that qemu then passes the traffic of the emulated nic that + the guest uses on. Since you (usually) don't want to run qemu + as root you first need to setup permissions on the tap device in + &man.devfs.conf.5; so that qemu running as your user can acess it. + For example add: + own tap0 <username or uid> + Next you figure out two IPs in an unused sub-subnet of your + host's network, one for the tap interface and the other for the + guest (this is important so the host itself can talk to the guest's + emulated nic properly), and then edit qemu's tuntap setup script + that gets executed after qemu opens a tap device, + /usr/local/etc/qemu-ifup to read something + like this: + #!/bin/sh +sudo /sbin/ifconfig $1 <IP for the tap interface> netmask <sub-subnet's netmask> +case "`/sbin/ifconfig bridge0`" in + *" $1 "*) ;; # already in the bridge + *) sudo /sbin/ifconfig bridge0 addm $1 ;; +esac + (sudo is in ports as + security/sudo, if your user + is in the wheel group you can add a line + %wheel ALL=/sbin/ifconfig + to /usr/local/etc/sudoers for this to work.) + Then you load the if_tap and if_bridge klds, restart devfs, set the + net.link.tap.user_open sysctl, create + the bridge and add your host's physical interface to it: + &prompt.root; kldload if_tap if_bridge + &prompt.root; /etc/rc.d/devfs restart + &prompt.root; sysctl net.link.tap.user_open=1 + &prompt.root; ifconfig bridge0 create + &prompt.root; ifconfig bridge0 addm <host's physical interface> up + Now you should be ready to start qemu: + &prompt.user; qemu -m 256 -hda guest.img -boot c -net nic -net tap,ifname=tap0 + In the guest configure the emulated nic with the IP you selected + above (different than the tap interface's IP) and the netmask of + your host's physical interface, and the rest (gateway, dns) like you + would configure any other host on your physical network. + (Note that if you have more than one qemu guest on a network + you need to make sure they each use a different macaddress, + the macaddress can be set via + -net nic,macaddr=....) + If you would like to make these settings permanent, add + the following to + &man.loader.conf.5; to load if_tap.ko and + if_bridge.ko at boot: + if_tap_load=YES +if_bridge_load=YES + (or put them in your kernel config), add + net.link.tap.user_open=1 + to &man.sysctl.conf.5;, and add the bridge configuration to + &man.rc.conf.5;: + cloned_interfaces="bridge0" +ifconfig_bridge0="addm <host's physical interface> up" + + --=-=-=--