[#vnetwalk]:

Walkthroughs For Virtual Machine Networking Configuration

(Choose the information related to the software being used. If the software being used is not found, check to see if similar software is being used.)

[#qmhownet]: Setting up networking in Qemu
Overview about this guide

This guide is meant to cover details to get a flexible setup working, without delving into a lot of unnecessary steps. Other options do exist. If the desired results differ from what this guide produces, or troubleshooting is needed, know that the topic of Qemu networking is more thoroughly documented by the lengthier documentation about Qemu's network implementations.

This guide was written for how to use Using a standard-looking virtual NIC on the host machine to communicate with the virtual machine's NIC, and more specifically Having/Creating a TUN/TAP device. That has been known to work well on Unix platforms (or similar/compatible), and perhaps with some versions of Microsoft Windows. This guide might be primarily focused on implementing this in a Unix type of environment. Note that there may be other options (such as the Virtual Distributed Ethernet, discussed in the guide to Using a standard-looking virtual NIC on the host machine to communicate with the virtual machine's NIC), but this guide does not currently have much details on other options that may exist).

This guide starts out by modifying an existing configuration. People who have set up a Qemu machine might be able to follow along with no troubles. If early details are not making sense, review the section on making a Qemu configuration first.

Modifying the command line

In the example shown by making a Qemu configuration, there may be a couple of lines that look like this:

 -net nic,vlan=1,macaddr=52:54:00:12:11:01 \
 -net user,vlan=1 \

If those lines do exist, comment them out. This can be done by making the lines look like the following:

 $(#-net nic,vlan=1,macaddr=52:54:00:12:11:01) \
 $(#-net user,vlan=1) \

Add the following lines, which also should cause some new NICs to be created. (If some lines just got commented out, these new lines can very sensibly come right after the lines that just got commented out.)

 -net nic,vlan=4,macaddr=52:54:00:12:04:02 \
 -net tap,vlan=4,ifname=tun0,script=${VMDir}/bin/nic/upif0,downscript=${VMDir}/bin/nic/dnif0 \
 -net nic,vlan=5,macaddr=52:54:00:12:05:01 \
 -net socket,vlan=5,listen=127.0.0.1:43000 \
 -net nic,vlan=6,macaddr=52:54:00:12:06:01 \
 -net tap,vlan=6,ifname=tun1,script=${VMDir}/bin/nic/upif2,downscript=${VMDir}/bin/nic/dnif2 \

(Note: the lines that say “ -net tap,vlan=” are fairly long... there is no white space, including any word wrapping, intended to be between the “ -net tap,vlan=” and the backslash that follows that text. The lines could be broken up using escaped newline characters, but then the following text would need to be at the very start of the next line, without any indenting. Though technically possible, those end results might make things look more cumbersome, and so less pleasant to read.)

Naturally, the reference to some (NIC configuration) script files does mean that some new script files are now required. Run:

echo ${VMDir}

If the variable is unset, then set an environment variable approrpiately, or modify these instructions appropriately. (An early line, perhaps the first line, of the example script file may show a sensible value to use.)

Ensure all referenced directories exist

Check that any directory in the script file exists. Quite possibly, the reference to ${VMDir} might be an empty reference, or the directory does might not exist.

Setting a variable that points to a directory

In the example shown by making a Qemu configuration, the top line may look something like this:

export VMDir=/srv/bigspace/myvm

So, that command may be found at the top of the main (example) script that starts to run the virtual machine.

That same command line can also be used in the script files that are used for NIC configuration. Using that line is the NIC configuration files is probably not absolutely required, but it can be good and helpful. So, when creating the NIC configuration files, start by looking for such a line at the top of the main script file.

(Such a line might very commonly not exist if the main script file was not created using this documentation/example, or something similar. If the line does exist, though, feel free to use it.)

If there is such a line at the top of the script configuration file, its value is likely to be customized for various machines. So, don't just use the example on this web page. Look to see what that line is set to, and make a note of its value. (If there is an easy known way to copy and paste that line of text to a temporary/scratch unsaved text file, then that may be worthwhile to do.)

Make any needed directories

Here, put to use some skills for making a directory. In Unix, the following may work well:

mkdir -p ${VMDir}/bin/nic
[#qmnicscr]: Additional network configuration script files
Qemu Configuration Files in Unix

Then, the following scripts were used. (Feel free to modify these examples as desired. Such modifications could break things, but modifications may also be necessary to make things work as desired.)

(In case copy-and-paste is not convenient but downloading and/or file transfering is, the following example files have been made available: virtual machine configuration files (version 1) provides the files without extension (which is the common filespec for a shell script), and as text files with a standard .txt filename extension (in case a web browser has difficulties with a file with no extension), and as zip files (for those who can comfortably unzip files).

The ${VMDir}/bin/nic/upif0 file
#! /bin/sh

export VMDir=/srv/bigspace/myvm

DESIPv4=10.0.4.1
DEFAULTNIC=tun0
SCRIPTNAME=upif0
PASSEDVAR=$1

if [ "$1" = "" ]; then
# This code will set PASSEDVAR if $1 is blank.
echo No detected passed parameter to $SCRIPTNAME
echo Will use ${DEFAULTNIC}
PASSEDVAR=${DEFAULTNIC}
else
PASSEDVAR=$1
fi

echo "${SCRIPTNAME} Initializing PASSEDVAR..." \(\$PASSEDVAR=${PASSEDVAR} \$1=$1 \$*=$*\)

ifconfig $PASSEDVAR link0
ifconfig $PASSEDVAR ${DESIPv4} netmask 255.255.255.0
ifconfig $PASSEDVAR up

# The following may have come from some example config # with an older version of Qemu bundled with OpenBSD: # The following two block carp packets from wasting cpu cycles inside the # qemu sessions, remove if testing carp inside qemu
#$SUDO brconfig $BRIDGE rule block in on $ETHER dst 33:33:0:0:0:12
#$SUDO brconfig $BRIDGE rule block in on $ETHER dst 01:00:5e:00:00:12

# Routing commands, if any are needed, may go here.

# No routing commands are used at this time for this NIC

# dhcpd -c ${VMDir}/bin/cfg/dhcpd${PASSEDVAR} {$PASSEDVAR} # echo dhcpd -c ${VMDir}/bin/cfg/dhcpd${PASSEDVAR} ${PASSEDVAR} returned $?
echo Ended ${SCRIPTNAME}

The ${VMDir}/bin/nic/dnif0 file

Note: Qemu may end up not passing the NIC as a parameter. Either test whether that happens okay, or make sure that DEFAULTNIC is getting set sufficiently.

#! /bin/sh

export VMDir=/srv/bigspace/myvm

DEFAULTNIC=tun0
SCRIPTNAME=dnif0
PASSEDVAR=$1

if [ "$1" = "" ]; then
# This code will set PASSEDVAR if $1 is blank.
echo No detected passed parameter to $SCRIPTNAME
echo Will use ${DEFAULTNIC}
PASSEDVAR=${DEFAULTNIC}
else
PASSEDVAR=$1
fi

echo Going to remove dhcpd on ${PASSEDVAR}
echo finding dhcpd...
echo pgrep -f ${VMDir}/bin/cfg/dhcpd${PASSEDVAR}.cfg
echo -n Found:
pgrep -f ${VMDir}/bin/cfg/dhcpd${PASSEDVAR}.cfg
echo Going to try removing what was just found.
sudo kill $( pgrep -f ${VMDir}/bin/cfg/dhcpd${PASSEDVAR}.cfg )
echo Killing attempt completed.

The ${VMDir}/bin/nic/upif2 file
#! /bin/sh

export VMDir=/srv/bigspace/myvm

DESIPv4=10.0.6.2
DEFAULTNIC=tun1
SCRIPTNAME=upif2
PASSEDVAR=$1

if [ "$1" = "" ]; then
# This code will set PASSEDVAR if $1 is blank.
echo No detected passed parameter to $SCRIPTNAME
echo Will use ${DEFAULTNIC}
PASSEDVAR=${DEFAULTNIC}
else
PASSEDVAR=$1
fi

echo "${SCRIPTNAME} Initializing PASSEDVAR..." \(\$PASSEDVAR=${PASSEDVAR} \$1=$1 \$*=$*\)

ifconfig $PASSEDVAR link0
ifconfig $PASSEDVAR ${DESIPv4} netmask 255.255.255.0
ifconfig $PASSEDVAR up

# The following may have come from some example config # with an older version of Qemu bundled with OpenBSD: # The following two block carp packets from wasting cpu cycles inside the # qemu sessions, remove if testing carp inside qemu
#$SUDO brconfig $BRIDGE rule block in on $ETHER dst 33:33:0:0:0:12
#$SUDO brconfig $BRIDGE rule block in on $ETHER dst 01:00:5e:00:00:12

# Routing commands, if any are needed, may go here.

route add 10.0.5.0/24 ${DESIPv4}/24

# dhcpd -c ${VMDir}/bin/cfg/dhcpd${PASSEDVAR} {$PASSEDVAR} # echo dhcpd -c ${VMDir}/bin/cfg/dhcpd${PASSEDVAR} ${PASSEDVAR} returned $?
echo Ended ${SCRIPTNAME}

The ${VMDir}/bin/nic/dnif2 file
#! /bin/sh

export VMDir=/srv/bigspace/myvm

DEFAULTNIC=tun1
SCRIPTNAME=dnif2
PASSEDVAR=$1

if [ "$1" = "" ]; then
# This code will set PASSEDVAR if $1 is blank.
echo No detected passed parameter to $SCRIPTNAME
echo Will use ${DEFAULTNIC}
PASSEDVAR=${DEFAULTNIC}
else
PASSEDVAR=$1
fi

echo Going to remove dhcpd on ${PASSEDVAR}
echo finding dhcpd...
echo pgrep -f ${VMDir}/bin/cfg/dhcpd${PASSEDVAR}.cfg
echo -n Found:
pgrep -f ${VMDir}/bin/cfg/dhcpd${PASSEDVAR}.cfg
echo Going to try removing what was just found.
sudo kill $( pgrep -f ${VMDir}/bin/cfg/dhcpd${PASSEDVAR}.cfg )
echo Killing attempt completed.

route delete 10.0.5.0/24

The above files could probably be enhanced, perhaps in multiple ways. The IPv4 configuration uses both a subnet mask and a prefix length, and /30 would probably work just as well as the /24 used by the example. The output of pgrep could be compared to an empty string to see if the kill command should be run.

Other operating systems

Full clear easy details are not provided at this time. The recommended course of action is to review the examples for Unix, and modify if needed.

After making each of the script files, the next step is going to be to make sure they can be run. Make any required changes to file attributes (like ownerships and permissions).

echo ${VMDir}
chmod ug+x ${VMDir}/bin/exc* ${VMDir}/bin/nic/*
chgrp wheel ${VMDir}/bin/exc* ${VMDir}/bin/nic/*
Other information on specific implemenations

Guide(s) in this section had a design goal of being a walk-through. If there is no such guide, check out Virtual Networking: Specific Implementations to see if there are details for the software being used. (If so, that may be a good sign: the program might be un-complicated enough that straightforward documentation seemed to cover the topic quickly enough that a specialized walkthrough wasn't needed.)

If one of these more specific guides does not suitably provide the desired details, see one of the more general guides that is not quite as “focused on”/“limited to” a specific virtualization software implementation.