From owner-freebsd-doc@FreeBSD.ORG Wed Dec 17 16:26:24 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 246E81065676; Wed, 17 Dec 2008 16:26:24 +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 0BCD48FC17; Wed, 17 Dec 2008 16:26:24 +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 mBHGQO7W035214; Wed, 17 Dec 2008 16:26:24 GMT (envelope-from loader@freefall.freebsd.org) Received: (from loader@localhost) by freefall.freebsd.org (8.14.3/8.14.3/Submit) id mBHGQL0K035212; Wed, 17 Dec 2008 16:26:21 GMT (envelope-from loader) Date: Wed, 17 Dec 2008 16:26:21 GMT Message-Id: <200812171626.mBHGQL0K035212@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:26:24 -0000 --=-=-= loader wrote: >> http://www.freebsd.org/cgi/query-pr.cgi?pr=127923 And I reworded some parts of Juergen's patch with the help of murray@. --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=virtualization.diff Index: chapter.sgml =================================================================== RCS file: /home/ncvs/doc/en_US.ISO8859-1/books/handbook/virtualization/chapter.sgml,v retrieving revision 1.17 diff -u -r1.17 chapter.sgml --- chapter.sgml 28 Jul 2008 14:33:51 -0000 1.17 +++ chapter.sgml 17 Dec 2008 16:13:40 -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: @@ -969,6 +974,327 @@ Work is also ongoing in getting &xen; to work as a host environment on FreeBSD. + + + + + Juergen + Lock + Contributed by + + + + <application>QEMU</application> on &os; as Host + + QEMU + is a generic processor emulator using dynamic binary translation + to achieve a reasonable emulation speed. + + + Installation + + There are two QEMU ports in the &os; + Ports tree at the moment: + emulators/qemu + is usually the latest release, and + emulators/qemu-devel. + is an svn snapshot. For best results try the latest version + available as a &os; package, or alternatively try the + emulators/qemu-devel. + + Install emulators/qemu-devel + with the following commands: + + &prompt.root; cd /usr/ports/emulators/qemu-devel +&prompt.root; make install clean + + If the QEMU port is built with the + kqemu knob enabled, load the kqemu kernel module: + + &prompt.root; kldload kqemu + + And kqemu can be loaded automatically at boot by adding + the following line to /etc/rc.conf: + + kqemu_enable="YES" + + Please make sure the kqemu.ko is always in sync + with the kernel like with any kld installed outside of the base. Rebuild + the port emulators/kqemu-kmod or + emulators/kqemu-kmod-devel + whenever you update the kernel. + + QEMU now uses &man.aio.4; at least + for IDE DMA, otherwise QEMU crashes + with Invalid system call: + + &prompt.root; kldload aio + + + If you want to run QEMU with + 512 or larger on &os; 6.3 i386 hosts, + kern.maxdsiz needs to be increased in + /boot/loader.conf since the default + value 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. + + + + + An example of installing &os; 7.0 as a + <application>QEMU</application> guest + + First, create an empty raw 5 GB image: + + &prompt.user; qemu-img create 7.0.img 5G + + + qcow2 is the QEMU image format, + use the qcow2 option to avoid sparse files + on the host. Some backup applications like &man.bsdtar.1; don't + support them properly, others like gtar + or star do if passed appropriate flags, + qcow2 format might be slower than raw format. + + + Next, 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 + + + You need to run qemu-system-x86_64 + if you want to emulate a amd64 guest. + + + 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 though.) + + When finished, select exit install in sysinstall to reboot, + and when the guest booting the ISO again, type + q in QEMU's monitor or + killall qemu on another host shell if you are + using , like when you have no X11 on the + host. + + Now, boot the installed image: + + &prompt.user; qemu -m 256 -cdrom 7.0-RELEASE-i386-disc1.iso -hda 7.0.img -boot c -monitor stdio + + 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 later on, + now is the time to configure it for a serial console in the guest: + + &prompt.root; echo console=\"comconsole\" >>/boot/loader.conf +&prompt.root; sed -i -e '/^ttyu0/s/off/on/' /etc/ttys + + As always with virtualization, lowering kern.hz + helps performance also with QEMU: + + &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: + + &prompt.root; shutdown -p now + + If the serial console is configured, now you can boot the + guest with : + + &prompt.user; qemu -m 256 -hda 7.0.img -boot c -nographic + + With , the guest console and + QEMU's monitor are multiplexed on + QEMU's tty, hit + Ctrl + a and then + h to show a small help. + + + Qemu can emulate several different mode of network card, + try , + or , they should be faster + and use less guest CPU than the + default . + + With the display + option, QEMU will listen on + VNC display display + and redirect the VGA display over the + VNC session. If the host or + QEMU build lacks X11 and + or + is not an available option. works best with + an en-us keymap on the host. + Use the port net/tightvnc + with QEMU. The X keymap on the host + can be changed temporarily using setxkbmap, + see the manual pages for more information. + + + + + &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 + + In this example we launched QEMU + with an ES1370 sound card enabled for the guest OS. + + Click into the guest window to access the guest + mouse pointer and hit Alt + Ctrl to leave the guest window + mouse grab. + + + Debian/Ubuntu (and their offsprings) have a record of + shipping partially broken QEMU + bioses, so if you have problems using a packaged + QEMU 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. + + + + + Simple Example for using <application>QEMU</application> + 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 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 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 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 the following line to + /usr/local/etc/sudoers: + + %wheel ALL=/sbin/ifconfig + + Then load the kernel modules if_tap.ko and + if_bridge.ko, 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 start the QEMU: + + &prompt.user; qemu -m 256 -hda guest.img -boot c -net nic -net tap,ifname=tap0 + + Then configure the NIC in the guest like + other hosts on your physical network. + + + If you have more than one QEMU + guest on a network you need to make sure they use different + MAC addresses, the MAC address can be set via + . + + + To make the settings permanent, add the following lines + in /boot/loader.conf: + + if_tap_load="YES" +if_bridge_load="YES" + + Add the following line in /etc/sysctl.conf: + + net.link.tap.user_open=1 + + And add the bridge configuration in /etc/rc.conf: + + cloned_interfaces="bridge0" +ifconfig_bridge0="addm <host's physical interface> up" + + + + User Documentation and Resources + + The QEMU package + includes qemu(1) man page and the following locally installed html + documents: + + + /usr/local/share/doc/qemu/qemu-doc.html + /usr/local/share/doc/qemu/qemu-tech.html + + + Places to check other than the official documentation: + + + + emulators/qemu-devel + + pkg-message + + + The + freebsd-emulation mailing list archive. + + + The + qemu forum + has howtos, patches, and code snapshots. Note, however + that the qemu sources won't build on &os; without some changes. + + + The + qemu-devel mailing list archive. + + + --=-=-=--