QemuOnFreeBSD

From shankerbalan.net
Jump to: navigation, search

Install qemu and the kmod from ports. The tap(4) and bridge(4) devices need to be configured to allow the guest OS full network access.

Contents

Installation

$ sudo portinstall emulators/qemu emulators/kqemu-kmod
> pkg_info -x qemu
Information for qemu-0.8.2_1:

Comment:
QEMU CPU Emulator


Description:
QEMU is a FAST! processor emulator using dynamic translation to achieve
good emulation speed.
QEMU has two operating modes:

   * Full system emulation. In this mode, QEMU emulates a full system
(for example a PC), including a processor and various peripherials.
It can be used to launch different Operating Systems without rebooting
the PC or to debug system code.
   * User mode emulation (Linux host only). In this mode, QEMU can launch
Linux processes compiled for one CPU on another CPU. It can be used to
launch the Wine Windows API emulator or to ease cross-compilation and
cross-debugging. 

As QEMU requires no host kernel patches to run, it is very safe and easy to use.
(but kqemu is now also supported for the i386 on i386 and amd64 case)

See also the preconfigured system images on http://oszoo.org/
Many live cd isos also work.

WWW: http://qemu.org/
> pkg_info -x kqemu-kmod
Information for kqemu-kmod-1.3.0.p9_1:

Comment:
Kernel Acceralator for QEMU CPU Emulator (development version)


Description:
KQEMU is a qemu accelerator kernel module on x86/amd64.

WWW: http://qemu.org/qemu-accel.html

Kernel

loader.conf.local

kqemu_load="YES"
if_tap_load="YES"

devfs.conf

perm    tap?    0660
  • portupgrade -f kqemu-kmod after a kernel upgrade

Networking

A tap device is created and added to the bridge to allow packets into the virtual tap interface. Bridging is already compiled into the kernel. The network configuration is as below:

main-lan: 192.168.44.0/24
qemu-lan: 192.168.45.0/24
tap0: 192.168.45.1  # the default g/w for the guest OS
iwi0: 192.168.44.10 # laptop wireless interface

sysctl

net.inet.ip.forwarding=1
net.link.tap.user_open=1

pf.conf

# nat on iwi0 [qemu]
nat on iwi0 from 192.168.45.0/24 to any -> (iwi0)
# trust 192.168.45.0/24: Thats our qemu subnet
pass in on tap0 from 192.168.45.0/24 to any keep state
  • Set up firewall and NAT correctly

qemu-ifup

> cat ~/bin/qemu-ifup 
#!/bin/sh

[ -z $1 ] && exit

ifconfig=/sbin/ifconfig
echo "Executing $0 ..."
echo "Bringing up $1 for bridged mode..."
sudo $ifconfig $1 192.168.45.1 up
echo "Adding $1 to bridge0..."
sudo $ifconfig bridge0 create
sudo $ifconfig bridge0 addm tap0 up
sudo $ifconfig bridge0 addm iwi0 up
sleep 2

Guest OS Configuration

  1. On the guest OS (Linux/BSD/etc), assign an IP address of 192.168.45.2 and above to the network device
  2. The default gateway is set to 192.168.45.1 which is the IP of the tap(4) device
  3. Update resolver settings with the host OS settings. This is 192.168.44.1 in my case
  • Check firewall settings if pings dont work

Running Qemu

> qemu -hda dfly.img -net nic -net tap,script=~/bin/qemu-ifup
Executing /home/shanu/bin/qemu-ifup ...
Bringing up tap0 for bridged mode...
Adding tap0 to bridge0...
ifconfig: SIOCIFCREATE2: File exists
ifconfig: BRDGADD tap0: File exists
ifconfig: BRDGADD iwi0: File exists
  • The "File exists" warnings are harmless. These devices got created by the previous run

ifconfig

Below is the ifconfig output with a qemu session running

> ifconfig tap0 
tap0: flags=8943<UP,BROADCAST,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500
       inet6 fe80::2bd:a8ff:fe31:1500%tap0 prefixlen 64 scopeid 0x4 
       inet 192.168.45.1 netmask 0xffffff00 broadcast 192.168.45.255
       ether 00:bd:a8:31:15:00
       Opened by PID 63473
> ifconfig bridge0
bridge0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 1500
       ether 76:63:f6:17:56:44
       priority 32768 hellotime 2 fwddelay 15 maxage 20
       member: iwi0 flags=3<LEARNING,DISCOVER>
       member: tap0 flags=3<LEARNING,DISCOVER>

Running Qemu As non-root

  • It should be possible to minimize sudo usage.
  1. /dev/tap? interfaces is root:wheel 0660.
  2. net.link.tap.user_open is 1

Resources

  1. See Qemu On Linux for running Qemu on Linux as the host OS
Personal tools