QEMU With a Bridged tap0 Interface on a FreeBSD Host
QEMU is a quick-and-easy suite to set up guest virtual machines on various platforms. Today I'll be using FreeBSD as a host operating system to set up a bridged tap0 interface for guest VMs, then supplying a quick script to automate the creation of the interface and boot the guest OS in one fell swoop.
Begin by downloading the setup image for the OS you want to install as a guest. In this case, I'll be creating a NetBSD 8.0 guest. All files will reside in ~/VM/ for sanity's sake.
$ cd ~/VM/ $ curl -O https://cdn.netbsd.org/pub/NetBSD/NetBSD-8.0/images/NetBSD-8.0-amd64.iso
Now we'll be creating the disk image for the guest OS to reside on
$ truncate -s 20G ~/VM/netbsd0.img
Once you've verified the integrity of the installation image (which I will skip as this is for random home use and not a production VM) we can begin creating the bridge interface and booting the install media. Be sure to replace wlan0 in the fourth command with the name of the interface you want tap0 to bridge.
$ sudo ifconfig tap0 create $ sudo sysctl net.link.tap.up_on_open=1 net.link.tap.up_on_open: 0 -> 1 $ sudo ifconfig bridge0 create $ sudo ifconfig bridge0 addm wlan0 addm tap0 $ sudo ifconfig bridge0 up
OK, now that the interface has been created, time to boot the install media and instruct it to use the netbsd0.img file as the disk image, as well as instruct QEMU to use the tap0 interface for networking. The reason I'm showing you how to enable the tap0 bridge while loading the install media is in case you're using a network-dependent installation image (like in the case of Arch Linux).
The backslash means a line break in a single-line command. You may omit the backslash if you type the entire thing as a single line.
$ qemu-system-x86_64 -hda ~/VM/netbsd0.img -boot d -cdrom ~/VM/NetBSD-8.0-amd64.iso \ -m 1024 -net nic -net tap,ifname=tap0,script=no
As you can probably guess, the -m 1024 flag allots 1GB of RAM to the virtual machine. This should be plenty for an installation procedure. I will allot more RAM to the actual image later.
Once the installation procedure finishes, you can kill the VM. Here's the command to start up the guest for "Normal Use"
$ qemu-system-x86_64 -m 2048 -hda ~/VM/netbsd0.img -net nic -net tap,ifname=tap0,script=no
Soon I'll write a shell script to test if the tap0 interface exists, if it doesn't then create it, then boot the guest OS using qemu.