# modprobe vzethdev
</pre>
{{Note|in vzctl < 3.0.11, vzethdev is not autoloaded by <code>/etc/init.d/vz</code> script, so you have to edit it to load this module.}}
=== MAC addresses ===
=== Adding veth to a CT ===
==== syntax vzctl version > 3.0.22 ====
vzctl set <CTID> --netif_add <ifname>[,<mac>,<host_ifname>,<host_mac>,<bridge>]
vzctl set 101 --netif_add eth0,,,,vmbr1 --save
==== syntax vzctl version >= 3.0.14 ====
Syntax is the same as above, but without a <bridge> parameter.
==== syntax vzctl version < 3.0.14 ====
vzctl set <CTID> --veth_add <dev_name>,<dev_addr>,<ve_dev_name>,<ve_dev_addr>
Here
* <tt>dev_name</tt> is the Ethernet device name that you are creating on the [[CT0|host system]]
* <tt>dev_addr</tt> is its MAC address
* <tt>ve_dev_name</tt> is the corresponding Ethernet device name you are creating on the CT
* <tt>ve_dev_addr</tt> is its MAC address
{{Note|this option is incremental, so devices are added to already existing ones.}}
NB there should no spaces after the commas.
Example:
<pre>
[host-node] ifconfig eth0
...
HWaddress 00:12:34:56:78:9B
...
</pre>
[host-node] easymac.sh -R
00:12:34:56:78:9A
vzctl set 101 --veth_add veth101.0,00:12:34:56:78:9A,eth0,00:12:34:56:78:9B --save
After executing this command <tt>veth</tt> device will be created for CT 101 and veth configuration will be saved to a CT configuration file.
Host-side Ethernet device will have <tt>veth101.0</tt> name and <tt>00:12:34:56:78:9A</tt> MAC address.
CT-side Ethernet device will have <tt>eth0</tt> name and <tt>00:12:34:56:78:9B</tt> MAC address.
=== Removing veth from a CT ===
==== syntax vzctl version >= 3.0.14 ====
vzctl set <CTID> --netif_del <dev_name>|all
vzctl set 101 --netif_del eth0 --save
==== syntax vzctl version < 3.0.14 ====
vzctl set <CTID> --veth_del <dev_name>
Here <tt>dev_name</tt> is the Ethernet device name in the [[CT0|host system]].
Example:
vzctl set 101 --veth_del veth101.0 --save
After executing this command veth device with host-side Ethernet name
<code>veth101.0</code> will be removed from CT101 and veth configuration
will be updated in CT config file.
== Common configurations with virtual Ethernet devices ==
Like the above example, here it is how to add the veth device to a bridge in a persistent way.
==== method for vzctl version > 3.0.22 ==== Newer versions of vzctl includes include a 'vznetaddbr' script, which makes use of the new <''bridge> '' parameter of the --netif_add switch.
Just create /etc/vz/vznet.conf containing the following.
The script uses 'vmbr0' as default bridge name when no bridge is specified.
==== method for vzctl version <= 3.0.22 ====
Older vzctl doesn't offer an automatic function to do this.
1. First, edit the CT's configuration to specify what is the host bridge , and to indicate that a custom script should be run when starting up a CT.
* Open up /etc/vz/conf/CTID.conf
* Comment out any IP_ADDRESS entries to prevent a CTNET-device from being created in the CT
* Add or change the entry CONFIG_CUSTOMIZED="yes"
* Add an entry VZHOSTBR="<bridge if>" which is the bridge interface (already configured and up), you want to extend.
2. Now to create that "custom script". The following helper script will check the configuration file for the bridge interface name and for the veth interface, and add the interface to the bridge. Create the script /usr/sbin/vznetaddbr to have the following, and then <code>chmod 0500 /usr/sbin/vznetaddbr</code> to make it executable.
<pre>
#!/bin/bash
# /usr/sbin/vznetaddbr
# a script to add virtual network interfaces (veth's) in a CT to a bridge on CT0
CONFIGFILE=/etc/vz/conf/$VEID.conf
. $CONFIGFILE
VZHOSTIF=`echo $NETIF |sed 's/^.*host_ifname=\(.*\),.*$/\1/g'`
if [ ! -n "$VZHOSTIF" ]; then
echo "According to $CONFIGFILE CT$VEID has no veth interface configured."
exit 1
fi
if [ ! -n "$VZHOSTBR" ]; then
echo "According to $CONFIGFILE CT$VEID has no bridge interface configured."
exit 1
fi
echo "Adding interface $VZHOSTIF to bridge $VZHOSTBR on CT0 for CT$VEID"
/sbin/ifconfig $VZHOSTIF 0
/usr/sbin/brctl addif $VZHOSTBR $VZHOSTIF
exit 0
</pre>
3. Now create /etc/vz/vznet.conf containing the following. This is what defines the "custom script" as being the vznetaddbr which you just created.
<pre>
#!/bin/bash
EXTERNAL_SCRIPT="/usr/sbin/vznetaddbr"
</pre>
This may not work for particularily old versions of vzctl, e.g., the version 3.0.11 that ships with Debian Etch. For those versions, you can try a hack: Use the custom script <code>/etc/vz/conf/$VID.mount</code> which is available, even in these old versions. But it gets called too early, before the networking has been set up. But it can start some background process, which waits and occasionally polls until $VZHOSTIF has become available. Here is one way to go about it:
<pre>
#!/bin/bash
CONFIGFILE="/etc/vz/conf/$VEID.conf"
if [ -f "$CONFIGFILE" ]
then
. "$CONFIGFILE"
VZHOSTIF=`echo $NETIF |sed 's/^.*host_ifname=\(.*\),.*$/\1/g'`
export VZHOSTIF
export VZHOSTBR
# Fork into the background and try a few times,
# until the host side of the interface appears:
/bin/bash -c 'for i in 5 10 20 40 80 160
do
if ifconfig -a | grep -q "$VZHOSTIF"
then
exec /usr/sbin/vznetaddbr
else
sleep "$i"
fi
done
' &
# In the meantime, let the CT's start process continue,
# or else the interface will never appear:
exit 0
else
$0: Config file "$CONFIGFILE" does not exist.
exit 1
fi
</pre>
4. Of course, the CT's operating system will need to have . Consult the manual for your CT's OS for details.
When the CT is started, the veth specified in the NETIF value is added to the bridge specified. You can check this by doing <code>brctl show</code>
Inside the CT you can configure the interface statically or using dhcp, as a real interface attached to a switch on the lan.
=== Virtual Ethernet devices + VLAN ===