<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.openvz.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Mose</id>
	<title>OpenVZ Virtuozzo Containers Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.openvz.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Mose"/>
	<link rel="alternate" type="text/html" href="https://wiki.openvz.org/Special:Contributions/Mose"/>
	<updated>2026-06-10T02:15:58Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.31.1</generator>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Debian_template_creation&amp;diff=13336</id>
		<title>Debian template creation</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Debian_template_creation&amp;diff=13336"/>
		<updated>2013-01-19T15:51:37Z</updated>

		<summary type="html">&lt;p&gt;Mose: /* Final cleanup */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;These are rough instructions of how to manually create basic Debian template cache, which can be used to create OpenVZ [[VE]]s based on Debian. &lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* You shouldn't be running as root, but as a user that is permitted to use sudo instead.  It's a dangerous idea, run as root at your peril.&lt;br /&gt;
* Anywhere you see &amp;lt;tt&amp;gt;/vz&amp;lt;/tt&amp;gt;, you might really need to use &amp;lt;tt&amp;gt;/var/lib/vz&amp;lt;/tt&amp;gt; instead, especially on a Debian Etch host.&lt;br /&gt;
* Anywhere you see &amp;lt;tt&amp;gt;http://http.us.debian.org/debian/&amp;lt;/tt&amp;gt;, you can substitute your favorite Debian mirror.  ([http://www.debian.org/mirror/list List of official Debian Mirrors])&lt;br /&gt;
* See also: &amp;lt;tt&amp;gt;/usr/share/doc/vzctl/README.Debian&amp;lt;/tt&amp;gt; in the ''vzctl'' Debian package&lt;br /&gt;
&lt;br /&gt;
== Prerequisites ==&lt;br /&gt;
&lt;br /&gt;
{{Warning|if you want to use &amp;lt;code&amp;gt;ext4&amp;lt;/code&amp;gt; file system for &amp;lt;code&amp;gt;/vz&amp;lt;/code&amp;gt;, use &amp;lt;code&amp;gt;nodelalloc&amp;lt;/code&amp;gt; option in &amp;lt;code&amp;gt;/etc/fstab&amp;lt;/code&amp;gt;, otherwise it will crash. See {{Bug|1509}} and its duplicates for details.}}&lt;br /&gt;
&lt;br /&gt;
You need to have a working copy of &amp;lt;tt&amp;gt;debootstrap&amp;lt;/tt&amp;gt; running on your hardware node.&lt;br /&gt;
&lt;br /&gt;
For Debian:&lt;br /&gt;
 sudo apt-get install debootstrap&lt;br /&gt;
&lt;br /&gt;
For Gentoo:&lt;br /&gt;
 sudo emerge debootstrap&lt;br /&gt;
&lt;br /&gt;
For Fedora (at least Fedora 8 have it, not sure about earlier versions):&lt;br /&gt;
 sudo yum install debootstrap&lt;br /&gt;
&lt;br /&gt;
For other distros you might need to install it from sources, or search for an appropriate package for your distribution.  An RPM is available on the [http://forum.openvz.org/index.php?t=tree&amp;amp;th=142&amp;amp;mid=584 OpenVZ Forum].&lt;br /&gt;
&lt;br /&gt;
== Bootstrapping Debian ==&lt;br /&gt;
&lt;br /&gt;
You can install different releases of Debian into a VE's private directory using the debootstrap command.&lt;br /&gt;
&lt;br /&gt;
The command parameters are:&lt;br /&gt;
&lt;br /&gt;
  debootstrap --arch ARCH NAME DIRECTORY URL&lt;br /&gt;
&lt;br /&gt;
Specify your architecture instead of &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; if you're using something other than i386/x86.  For example, for AMD64/x86_64, use &amp;lt;tt&amp;gt;amd64&amp;lt;/tt&amp;gt; or for ia64, use &amp;lt;tt&amp;gt;ia64&amp;lt;/tt&amp;gt;. You can use http or ftp in the URL.&lt;br /&gt;
&lt;br /&gt;
We use VE ID of 777 for this example, but it can be any unused ID.&lt;br /&gt;
&lt;br /&gt;
=== Squeeze (current Debian stable) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap --arch i386 squeeze /vz/private/777 http://http.us.debian.org/debian/&lt;br /&gt;
 or&lt;br /&gt;
 debootstrap --arch amd64 squeeze /vz/private/777 ftp://ftp.us.debian.org/debian/&lt;br /&gt;
&lt;br /&gt;
=== Lenny (Debian oldstable) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap --arch i386 lenny /vz/private/777 http://http.us.debian.org/debian/&lt;br /&gt;
 or&lt;br /&gt;
 debootstrap --arch amd64 lenny /vz/private/777 ftp://ftp.us.debian.org/debian/&lt;br /&gt;
&lt;br /&gt;
=== Etch (old release) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap --arch i386 etch /vz/private/777 http://http.us.debian.org/debian/&lt;br /&gt;
&lt;br /&gt;
=== Sarge (very old release) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap sarge /vz/private/777 http://archive.debian.org/debian&lt;br /&gt;
&lt;br /&gt;
== Preparing the HN network ==&lt;br /&gt;
Append the following lines to /etc/sysctl.conf, adjust to taste and then execute &amp;quot;sysctl -p&amp;quot; for them to take effect.&lt;br /&gt;
 ### OpenVZ settings&lt;br /&gt;
 &lt;br /&gt;
 # On Hardware Node enable packet forwarding to forward&lt;br /&gt;
 # packets between the HN network interfaces and venet.&lt;br /&gt;
 # Proxy arp is needed when CT is in a different subnet&lt;br /&gt;
 # or when using veth AND veth is not bridged to a HN&lt;br /&gt;
 # interface. When veth is bridged to a HN interface,&lt;br /&gt;
 # the CT handles its own arps.&lt;br /&gt;
 &lt;br /&gt;
 net.ipv4.conf.default.forwarding=1&lt;br /&gt;
 net.ipv4.conf.default.proxy_arp = 0&lt;br /&gt;
 net.ipv4.ip_forward=1&lt;br /&gt;
 &lt;br /&gt;
 # Enables source route verification&lt;br /&gt;
 net.ipv4.conf.all.rp_filter = 1&lt;br /&gt;
 &lt;br /&gt;
 # Enables the magic-sysrq key&lt;br /&gt;
 kernel.sysrq = 1&lt;br /&gt;
 &lt;br /&gt;
 # TCP Explict Congestion Notification&lt;br /&gt;
 net.ipv4.tcp_ecn = 0&lt;br /&gt;
 &lt;br /&gt;
 # we do not want all our interfaces to send redirects&lt;br /&gt;
 net.ipv4.conf.default.send_redirects = 1&lt;br /&gt;
 net.ipv4.conf.all.send_redirects = 0&lt;br /&gt;
&lt;br /&gt;
== Preparing and starting the VE ==&lt;br /&gt;
&lt;br /&gt;
=== Setting VE config ===&lt;br /&gt;
First, we need a config for the [[VE]]:&lt;br /&gt;
 sudo vzctl set 777 --applyconfig vps.basic --save&lt;br /&gt;
&lt;br /&gt;
On debian squeeze only the following worked for me (''confirmed''), because the standard template names in /etc/vz/conf have changed.&lt;br /&gt;
 sudo vzctl set 777 --applyconfig basic --save&lt;br /&gt;
&lt;br /&gt;
=== Setting VE OSTEMPLATE ===&lt;br /&gt;
Also, we need &amp;lt;tt&amp;gt;OSTEMPLATE&amp;lt;/tt&amp;gt; to be set in VE configuration file, for [[vzctl]] to work properly.&lt;br /&gt;
&lt;br /&gt;
 sudo sh -c 'echo OSTEMPLATE=\&amp;quot;debian-6.0\&amp;quot;' &amp;gt;&amp;gt; /etc/vz/conf/777.conf&lt;br /&gt;
&lt;br /&gt;
=== Setting VE IP address ===&lt;br /&gt;
For the [[VE]] to be able to download updates from the Internet, we need a valid IP address for it:&lt;br /&gt;
 sudo vzctl set 777 --ipadd x.x.x.x --save&lt;br /&gt;
&lt;br /&gt;
{{Note|if you use private IP for the VE, you might have to set up NAT as described in [[Using NAT for VE with private IPs]].}}&lt;br /&gt;
&lt;br /&gt;
=== Setting DNS server for VE ===&lt;br /&gt;
For the [[VE]] to be able to download updates from the Internet, we also need to specify a DNS for it:&lt;br /&gt;
 sudo vzctl set 777 --nameserver x.x.x.x --save&lt;br /&gt;
&lt;br /&gt;
=== Creating /dev/ptmx  ===&lt;br /&gt;
The ptmx character device should normally exist, but if it doesn't, create one.&lt;br /&gt;
 sudo mknod --mode 666 /var/lib/vz/private/777/dev/ptmx c 5 2&lt;br /&gt;
&lt;br /&gt;
=== Starting VE ===&lt;br /&gt;
Now start the VE:&lt;br /&gt;
 sudo vzctl start 777&lt;br /&gt;
&lt;br /&gt;
== Customizing the installation ==&lt;br /&gt;
A few things need to be done inside a newly created VE for it to become suitable for OpenVZ. Enter the VE to begin the configuration. Exporting the path is optional.&lt;br /&gt;
 sudo vzctl enter 777&lt;br /&gt;
 export PATH=/sbin:/usr/sbin:/bin:/usr/bin&lt;br /&gt;
&lt;br /&gt;
{{Warning|Do not run the commands below on the hardware node, they are only to be run within the VE!}}&lt;br /&gt;
&lt;br /&gt;
=== Set Debian repositories ===&lt;br /&gt;
 cat &amp;lt;&amp;lt;EOF &amp;gt; /etc/apt/sources.list&lt;br /&gt;
 deb http://http.us.debian.org/debian squeeze main contrib&lt;br /&gt;
 deb http://security.debian.org squeeze/updates main contrib&lt;br /&gt;
 deb http://http.us.debian.org/debian squeeze-updates main&lt;br /&gt;
 ## backports - ONLY IF YOU KNOW WHAT YOU DO&lt;br /&gt;
 # deb http://http.us.debian.org/debian-backports/ squeeze-backports main&lt;br /&gt;
 EOF&lt;br /&gt;
&lt;br /&gt;
=== Get new security updates ===&lt;br /&gt;
 apt-get update&lt;br /&gt;
 apt-get upgrade&lt;br /&gt;
&lt;br /&gt;
=== Install some more packages ===&lt;br /&gt;
Installing packages could be an interactive process so the system might ask some questions.  You can install more packages if you'd like.  For example:&lt;br /&gt;
 apt-get install ssh quota less&lt;br /&gt;
&lt;br /&gt;
=== Set sane permissions for &amp;lt;tt&amp;gt;/root&amp;lt;/tt&amp;gt; directory ===&lt;br /&gt;
 chmod 700 /root&lt;br /&gt;
&lt;br /&gt;
=== Disable root login ===&lt;br /&gt;
This will disable root login by default.&lt;br /&gt;
 usermod -L root&lt;br /&gt;
&lt;br /&gt;
=== Disable getty ===&lt;br /&gt;
Disable running &amp;lt;tt&amp;gt;getty&amp;lt;/tt&amp;gt;s on terminals as a VE does not have any:&lt;br /&gt;
 sed -i -e '/getty/d' /etc/inittab&lt;br /&gt;
&lt;br /&gt;
=== Disable &amp;lt;tt&amp;gt;sync()&amp;lt;/tt&amp;gt; for syslog ===&lt;br /&gt;
Turn off doing &amp;lt;tt&amp;gt;sync()&amp;lt;/tt&amp;gt; on every write for &amp;lt;tt&amp;gt;syslog&amp;lt;/tt&amp;gt;'s log files, to improve I/O performance:&lt;br /&gt;
 &amp;lt;pre&amp;gt;sed -i -e 's@\([[:space:]]\)\(/var/log/\)@\1-\2@' /etc/*syslog.conf&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Fix &amp;lt;tt&amp;gt;/etc/mtab&amp;lt;/tt&amp;gt; ===&lt;br /&gt;
Link &amp;lt;tt&amp;gt;/etc/mtab&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;/proc/mounts&amp;lt;/tt&amp;gt;, so &amp;lt;tt&amp;gt;df&amp;lt;/tt&amp;gt; and friends will work:&lt;br /&gt;
 rm -f /etc/mtab&lt;br /&gt;
 ln -s /proc/mounts /etc/mtab&lt;br /&gt;
&lt;br /&gt;
=== Remove some unneeded packages ===&lt;br /&gt;
If you have any packages you'd like to remove, now's the time for it.  Here's an example — note that not all of those packages are installed by default in Debian Squeeze (although they were in earlier versions):&lt;br /&gt;
 dpkg --purge modutils ppp pppoeconf pppoe pppconfig module-init-tools&lt;br /&gt;
&lt;br /&gt;
=== Disable services ===&lt;br /&gt;
Do not start some services, stick to bare minimum:&lt;br /&gt;
 update-rc.d -f klogd remove&lt;br /&gt;
 update-rc.d -f quotarpc remove&lt;br /&gt;
 update-rc.d -f exim4 remove&lt;br /&gt;
 update-rc.d -f inetd remove&lt;br /&gt;
&lt;br /&gt;
For dependency-based boot sequence introduced with Squeeze type:&lt;br /&gt;
&lt;br /&gt;
 update-rc.d-insserv -f klogd remove&lt;br /&gt;
 update-rc.d-insserv -f quotarpc remove&lt;br /&gt;
 update-rc.d-insserv -f exim4 remove&lt;br /&gt;
 update-rc.d-insserv -f inetd remove&lt;br /&gt;
&lt;br /&gt;
=== Fix SSH host keys ===&lt;br /&gt;
This is only useful if you installed SSH.  Each individual [[VE]] should have its own pair of SSH host keys.  The code below will wipe out the existing SSH keys and instruct the newly-created [[VE]] to create new SSH keys on first boot.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- please do not remove &amp;lt;source&amp;gt;...&amp;lt;/source&amp;gt; pair of tags below,&lt;br /&gt;
     otherwise quotes after -N (-N '') are not visible --&amp;gt;&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
rm -f /etc/ssh/ssh_host_*&lt;br /&gt;
cat &amp;lt;&amp;lt; EOF &amp;gt; /etc/rc2.d/S15ssh_gen_host_keys&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
ssh-keygen -f /etc/ssh/ssh_host_rsa_key -t rsa -N ''&lt;br /&gt;
ssh-keygen -f /etc/ssh/ssh_host_dsa_key -t dsa -N ''&lt;br /&gt;
rm -f \$0&lt;br /&gt;
EOF&lt;br /&gt;
chmod a+x /etc/rc2.d/S15ssh_gen_host_keys&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Note|This will not work using the dependency-based boot sequence introduced with Squeeze. See the section below. }}&lt;br /&gt;
&lt;br /&gt;
=== Fix SSH host keys in Squeeze when using dependency-based booting ===&lt;br /&gt;
&lt;br /&gt;
 rm -f /etc/ssh/ssh_host_*&lt;br /&gt;
&amp;lt;!-- please do not remove &amp;lt;source&amp;gt;...&amp;lt;/source&amp;gt; pair of tags below,&lt;br /&gt;
     otherwise quotes after -N (-N '') are not visible --&amp;gt;&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
cat &amp;lt;&amp;lt; EOF &amp;gt; /etc/init.d/ssh_gen_host_keys&lt;br /&gt;
#!/bin/sh&lt;br /&gt;
### BEGIN INIT INFO&lt;br /&gt;
# Provides:          Generates new ssh host keys on first boot&lt;br /&gt;
# Required-Start:    $remote_fs $syslog&lt;br /&gt;
# Required-Stop:     $remote_fs $syslog&lt;br /&gt;
# Default-Start:     2 3 4 5&lt;br /&gt;
# Default-Stop:&lt;br /&gt;
# Short-Description: Generates new ssh host keys on first boot&lt;br /&gt;
# Description:       Generates new ssh host keys on first boot&lt;br /&gt;
### END INIT INFO&lt;br /&gt;
ssh-keygen -f /etc/ssh/ssh_host_rsa_key -t rsa -N &amp;quot;&amp;quot;&lt;br /&gt;
ssh-keygen -f /etc/ssh/ssh_host_dsa_key -t dsa -N &amp;quot;&amp;quot;&lt;br /&gt;
insserv -r /etc/init.d/ssh_gen_host_keys&lt;br /&gt;
rm -f \$0&lt;br /&gt;
EOF&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
 chmod a+x /etc/init.d/ssh_gen_host_keys&lt;br /&gt;
 insserv /etc/init.d/ssh_gen_host_keys&lt;br /&gt;
&lt;br /&gt;
=== Change timezone ===&lt;br /&gt;
&lt;br /&gt;
You might want to change timezone if you do not live in $UTC. The following example is for Germany&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ln -sf /usr/share/zoneinfo/Europe/Berlin /etc/localtime&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
or even better&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
dpkg-reconfigure tzdata&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Clean packages ===&lt;br /&gt;
After installing packages, you'll have some junk packages laying around in your cache.  Since you don't want your template to have those, this command will wipe them out.&lt;br /&gt;
 apt-get --purge clean&lt;br /&gt;
&lt;br /&gt;
Now everything is done.  Exit from the template and go back to the hardware node.&lt;br /&gt;
 exit&lt;br /&gt;
&lt;br /&gt;
== Preparing for and packing template cache ==&lt;br /&gt;
&lt;br /&gt;
We don't need an IP for the VE anymore, and we definitely do not need it in template cache, so remove it:&lt;br /&gt;
 sudo vzctl set 777 --ipdel all --save&lt;br /&gt;
&lt;br /&gt;
Also, remove DNS server and search domain information from ''/etc/resolv.conf'' file '''in VE''':&lt;br /&gt;
 sudo nano /vz/private/777/etc/resolv.conf&lt;br /&gt;
&lt;br /&gt;
Also, remove ''/etc/hostname'' file '''in VE''':&lt;br /&gt;
 sudo rm -f /vz/private/777/etc/hostname&lt;br /&gt;
&lt;br /&gt;
Stop the VE:&lt;br /&gt;
 sudo vzctl stop 777&lt;br /&gt;
&lt;br /&gt;
Go to the VE directory:&lt;br /&gt;
 cd /vz/private/777&lt;br /&gt;
&lt;br /&gt;
Now create a cached OS tarball.  In the command below, you'll want to replace &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; with your architecture (i386, amd64, ia64, etc).&lt;br /&gt;
 sudo tar --numeric-owner -zcf /vz/template/cache/debian-5.0-i386-minimal.tar.gz .&lt;br /&gt;
&lt;br /&gt;
Look at the resulting tarball to see its size is sane:&lt;br /&gt;
 # ls -lh /vz/template/cache&lt;br /&gt;
 -rw-r--r--  1 root root  51M Apr 10 03:16 debian-5.0-i386-minimal.tar.gz&lt;br /&gt;
&lt;br /&gt;
== Checking if template cache works ==&lt;br /&gt;
We can now create a VE based on the just-created template cache.  Be sure to change &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; to your architecture just like you did when you named the tarball above.&lt;br /&gt;
 sudo vzctl create 123456 --ostemplate debian-5.0-i386-minimal&lt;br /&gt;
&lt;br /&gt;
Now make sure that it works:&lt;br /&gt;
 sudo vzctl start 123456&lt;br /&gt;
 sudo vzctl exec 123456 ps ax&lt;br /&gt;
&lt;br /&gt;
You should see that a few processes are running.&lt;br /&gt;
&lt;br /&gt;
== Final cleanup ==&lt;br /&gt;
Stop and remove the test VE you just created:&lt;br /&gt;
 sudo vzctl stop 123456&lt;br /&gt;
 sudo vzctl destroy 123456&lt;br /&gt;
 sudo rm /etc/vz/conf/123456.conf.destroyed&lt;br /&gt;
&lt;br /&gt;
Finally, let's remove the VE we used for OS template cache creation:&lt;br /&gt;
 sudo vzctl destroy 777&lt;br /&gt;
 sudo rm /etc/vz/conf/777.conf.destroyed&lt;br /&gt;
&lt;br /&gt;
You might want to edit /etc/vz/vz.conf and change DEF_OSTEMPLATE to the name of the template you use most often so that you don't have to specify the template when creating a VE.&lt;br /&gt;
 DEF_OSTEMPLATE=&amp;quot;debian-6.0-i386-minimal&amp;quot;&lt;br /&gt;
If you use iptables, you might want to include additional modules in the list for IPTABLES in /etc/vz/vz.conf. See ''man vzctl'' for a list of available modules.&lt;br /&gt;
&lt;br /&gt;
[[Category: HOWTO]]&lt;br /&gt;
[[Category: Templates]]&lt;br /&gt;
[[Category: Debian]]&lt;/div&gt;</summary>
		<author><name>Mose</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Debian_template_creation&amp;diff=13335</id>
		<title>Debian template creation</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Debian_template_creation&amp;diff=13335"/>
		<updated>2013-01-19T15:32:02Z</updated>

		<summary type="html">&lt;p&gt;Mose: /* Setting VE OSTEMPLATE */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;These are rough instructions of how to manually create basic Debian template cache, which can be used to create OpenVZ [[VE]]s based on Debian. &lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* You shouldn't be running as root, but as a user that is permitted to use sudo instead.  It's a dangerous idea, run as root at your peril.&lt;br /&gt;
* Anywhere you see &amp;lt;tt&amp;gt;/vz&amp;lt;/tt&amp;gt;, you might really need to use &amp;lt;tt&amp;gt;/var/lib/vz&amp;lt;/tt&amp;gt; instead, especially on a Debian Etch host.&lt;br /&gt;
* Anywhere you see &amp;lt;tt&amp;gt;http://http.us.debian.org/debian/&amp;lt;/tt&amp;gt;, you can substitute your favorite Debian mirror.  ([http://www.debian.org/mirror/list List of official Debian Mirrors])&lt;br /&gt;
* See also: &amp;lt;tt&amp;gt;/usr/share/doc/vzctl/README.Debian&amp;lt;/tt&amp;gt; in the ''vzctl'' Debian package&lt;br /&gt;
&lt;br /&gt;
== Prerequisites ==&lt;br /&gt;
&lt;br /&gt;
{{Warning|if you want to use &amp;lt;code&amp;gt;ext4&amp;lt;/code&amp;gt; file system for &amp;lt;code&amp;gt;/vz&amp;lt;/code&amp;gt;, use &amp;lt;code&amp;gt;nodelalloc&amp;lt;/code&amp;gt; option in &amp;lt;code&amp;gt;/etc/fstab&amp;lt;/code&amp;gt;, otherwise it will crash. See {{Bug|1509}} and its duplicates for details.}}&lt;br /&gt;
&lt;br /&gt;
You need to have a working copy of &amp;lt;tt&amp;gt;debootstrap&amp;lt;/tt&amp;gt; running on your hardware node.&lt;br /&gt;
&lt;br /&gt;
For Debian:&lt;br /&gt;
 sudo apt-get install debootstrap&lt;br /&gt;
&lt;br /&gt;
For Gentoo:&lt;br /&gt;
 sudo emerge debootstrap&lt;br /&gt;
&lt;br /&gt;
For Fedora (at least Fedora 8 have it, not sure about earlier versions):&lt;br /&gt;
 sudo yum install debootstrap&lt;br /&gt;
&lt;br /&gt;
For other distros you might need to install it from sources, or search for an appropriate package for your distribution.  An RPM is available on the [http://forum.openvz.org/index.php?t=tree&amp;amp;th=142&amp;amp;mid=584 OpenVZ Forum].&lt;br /&gt;
&lt;br /&gt;
== Bootstrapping Debian ==&lt;br /&gt;
&lt;br /&gt;
You can install different releases of Debian into a VE's private directory using the debootstrap command.&lt;br /&gt;
&lt;br /&gt;
The command parameters are:&lt;br /&gt;
&lt;br /&gt;
  debootstrap --arch ARCH NAME DIRECTORY URL&lt;br /&gt;
&lt;br /&gt;
Specify your architecture instead of &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; if you're using something other than i386/x86.  For example, for AMD64/x86_64, use &amp;lt;tt&amp;gt;amd64&amp;lt;/tt&amp;gt; or for ia64, use &amp;lt;tt&amp;gt;ia64&amp;lt;/tt&amp;gt;. You can use http or ftp in the URL.&lt;br /&gt;
&lt;br /&gt;
We use VE ID of 777 for this example, but it can be any unused ID.&lt;br /&gt;
&lt;br /&gt;
=== Squeeze (current Debian stable) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap --arch i386 squeeze /vz/private/777 http://http.us.debian.org/debian/&lt;br /&gt;
 or&lt;br /&gt;
 debootstrap --arch amd64 squeeze /vz/private/777 ftp://ftp.us.debian.org/debian/&lt;br /&gt;
&lt;br /&gt;
=== Lenny (Debian oldstable) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap --arch i386 lenny /vz/private/777 http://http.us.debian.org/debian/&lt;br /&gt;
 or&lt;br /&gt;
 debootstrap --arch amd64 lenny /vz/private/777 ftp://ftp.us.debian.org/debian/&lt;br /&gt;
&lt;br /&gt;
=== Etch (old release) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap --arch i386 etch /vz/private/777 http://http.us.debian.org/debian/&lt;br /&gt;
&lt;br /&gt;
=== Sarge (very old release) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap sarge /vz/private/777 http://archive.debian.org/debian&lt;br /&gt;
&lt;br /&gt;
== Preparing the HN network ==&lt;br /&gt;
Append the following lines to /etc/sysctl.conf, adjust to taste and then execute &amp;quot;sysctl -p&amp;quot; for them to take effect.&lt;br /&gt;
 ### OpenVZ settings&lt;br /&gt;
 &lt;br /&gt;
 # On Hardware Node enable packet forwarding to forward&lt;br /&gt;
 # packets between the HN network interfaces and venet.&lt;br /&gt;
 # Proxy arp is needed when CT is in a different subnet&lt;br /&gt;
 # or when using veth AND veth is not bridged to a HN&lt;br /&gt;
 # interface. When veth is bridged to a HN interface,&lt;br /&gt;
 # the CT handles its own arps.&lt;br /&gt;
 &lt;br /&gt;
 net.ipv4.conf.default.forwarding=1&lt;br /&gt;
 net.ipv4.conf.default.proxy_arp = 0&lt;br /&gt;
 net.ipv4.ip_forward=1&lt;br /&gt;
 &lt;br /&gt;
 # Enables source route verification&lt;br /&gt;
 net.ipv4.conf.all.rp_filter = 1&lt;br /&gt;
 &lt;br /&gt;
 # Enables the magic-sysrq key&lt;br /&gt;
 kernel.sysrq = 1&lt;br /&gt;
 &lt;br /&gt;
 # TCP Explict Congestion Notification&lt;br /&gt;
 net.ipv4.tcp_ecn = 0&lt;br /&gt;
 &lt;br /&gt;
 # we do not want all our interfaces to send redirects&lt;br /&gt;
 net.ipv4.conf.default.send_redirects = 1&lt;br /&gt;
 net.ipv4.conf.all.send_redirects = 0&lt;br /&gt;
&lt;br /&gt;
== Preparing and starting the VE ==&lt;br /&gt;
&lt;br /&gt;
=== Setting VE config ===&lt;br /&gt;
First, we need a config for the [[VE]]:&lt;br /&gt;
 sudo vzctl set 777 --applyconfig vps.basic --save&lt;br /&gt;
&lt;br /&gt;
On debian squeeze only the following worked for me (''confirmed''), because the standard template names in /etc/vz/conf have changed.&lt;br /&gt;
 sudo vzctl set 777 --applyconfig basic --save&lt;br /&gt;
&lt;br /&gt;
=== Setting VE OSTEMPLATE ===&lt;br /&gt;
Also, we need &amp;lt;tt&amp;gt;OSTEMPLATE&amp;lt;/tt&amp;gt; to be set in VE configuration file, for [[vzctl]] to work properly.&lt;br /&gt;
&lt;br /&gt;
 sudo sh -c 'echo OSTEMPLATE=\&amp;quot;debian-6.0\&amp;quot;' &amp;gt;&amp;gt; /etc/vz/conf/777.conf&lt;br /&gt;
&lt;br /&gt;
=== Setting VE IP address ===&lt;br /&gt;
For the [[VE]] to be able to download updates from the Internet, we need a valid IP address for it:&lt;br /&gt;
 sudo vzctl set 777 --ipadd x.x.x.x --save&lt;br /&gt;
&lt;br /&gt;
{{Note|if you use private IP for the VE, you might have to set up NAT as described in [[Using NAT for VE with private IPs]].}}&lt;br /&gt;
&lt;br /&gt;
=== Setting DNS server for VE ===&lt;br /&gt;
For the [[VE]] to be able to download updates from the Internet, we also need to specify a DNS for it:&lt;br /&gt;
 sudo vzctl set 777 --nameserver x.x.x.x --save&lt;br /&gt;
&lt;br /&gt;
=== Creating /dev/ptmx  ===&lt;br /&gt;
The ptmx character device should normally exist, but if it doesn't, create one.&lt;br /&gt;
 sudo mknod --mode 666 /var/lib/vz/private/777/dev/ptmx c 5 2&lt;br /&gt;
&lt;br /&gt;
=== Starting VE ===&lt;br /&gt;
Now start the VE:&lt;br /&gt;
 sudo vzctl start 777&lt;br /&gt;
&lt;br /&gt;
== Customizing the installation ==&lt;br /&gt;
A few things need to be done inside a newly created VE for it to become suitable for OpenVZ. Enter the VE to begin the configuration. Exporting the path is optional.&lt;br /&gt;
 sudo vzctl enter 777&lt;br /&gt;
 export PATH=/sbin:/usr/sbin:/bin:/usr/bin&lt;br /&gt;
&lt;br /&gt;
{{Warning|Do not run the commands below on the hardware node, they are only to be run within the VE!}}&lt;br /&gt;
&lt;br /&gt;
=== Set Debian repositories ===&lt;br /&gt;
 cat &amp;lt;&amp;lt;EOF &amp;gt; /etc/apt/sources.list&lt;br /&gt;
 deb http://http.us.debian.org/debian squeeze main contrib&lt;br /&gt;
 deb http://security.debian.org squeeze/updates main contrib&lt;br /&gt;
 deb http://http.us.debian.org/debian squeeze-updates main&lt;br /&gt;
 ## backports - ONLY IF YOU KNOW WHAT YOU DO&lt;br /&gt;
 # deb http://http.us.debian.org/debian-backports/ squeeze-backports main&lt;br /&gt;
 EOF&lt;br /&gt;
&lt;br /&gt;
=== Get new security updates ===&lt;br /&gt;
 apt-get update&lt;br /&gt;
 apt-get upgrade&lt;br /&gt;
&lt;br /&gt;
=== Install some more packages ===&lt;br /&gt;
Installing packages could be an interactive process so the system might ask some questions.  You can install more packages if you'd like.  For example:&lt;br /&gt;
 apt-get install ssh quota less&lt;br /&gt;
&lt;br /&gt;
=== Set sane permissions for &amp;lt;tt&amp;gt;/root&amp;lt;/tt&amp;gt; directory ===&lt;br /&gt;
 chmod 700 /root&lt;br /&gt;
&lt;br /&gt;
=== Disable root login ===&lt;br /&gt;
This will disable root login by default.&lt;br /&gt;
 usermod -L root&lt;br /&gt;
&lt;br /&gt;
=== Disable getty ===&lt;br /&gt;
Disable running &amp;lt;tt&amp;gt;getty&amp;lt;/tt&amp;gt;s on terminals as a VE does not have any:&lt;br /&gt;
 sed -i -e '/getty/d' /etc/inittab&lt;br /&gt;
&lt;br /&gt;
=== Disable &amp;lt;tt&amp;gt;sync()&amp;lt;/tt&amp;gt; for syslog ===&lt;br /&gt;
Turn off doing &amp;lt;tt&amp;gt;sync()&amp;lt;/tt&amp;gt; on every write for &amp;lt;tt&amp;gt;syslog&amp;lt;/tt&amp;gt;'s log files, to improve I/O performance:&lt;br /&gt;
 &amp;lt;pre&amp;gt;sed -i -e 's@\([[:space:]]\)\(/var/log/\)@\1-\2@' /etc/*syslog.conf&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Fix &amp;lt;tt&amp;gt;/etc/mtab&amp;lt;/tt&amp;gt; ===&lt;br /&gt;
Link &amp;lt;tt&amp;gt;/etc/mtab&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;/proc/mounts&amp;lt;/tt&amp;gt;, so &amp;lt;tt&amp;gt;df&amp;lt;/tt&amp;gt; and friends will work:&lt;br /&gt;
 rm -f /etc/mtab&lt;br /&gt;
 ln -s /proc/mounts /etc/mtab&lt;br /&gt;
&lt;br /&gt;
=== Remove some unneeded packages ===&lt;br /&gt;
If you have any packages you'd like to remove, now's the time for it.  Here's an example — note that not all of those packages are installed by default in Debian Squeeze (although they were in earlier versions):&lt;br /&gt;
 dpkg --purge modutils ppp pppoeconf pppoe pppconfig module-init-tools&lt;br /&gt;
&lt;br /&gt;
=== Disable services ===&lt;br /&gt;
Do not start some services, stick to bare minimum:&lt;br /&gt;
 update-rc.d -f klogd remove&lt;br /&gt;
 update-rc.d -f quotarpc remove&lt;br /&gt;
 update-rc.d -f exim4 remove&lt;br /&gt;
 update-rc.d -f inetd remove&lt;br /&gt;
&lt;br /&gt;
For dependency-based boot sequence introduced with Squeeze type:&lt;br /&gt;
&lt;br /&gt;
 update-rc.d-insserv -f klogd remove&lt;br /&gt;
 update-rc.d-insserv -f quotarpc remove&lt;br /&gt;
 update-rc.d-insserv -f exim4 remove&lt;br /&gt;
 update-rc.d-insserv -f inetd remove&lt;br /&gt;
&lt;br /&gt;
=== Fix SSH host keys ===&lt;br /&gt;
This is only useful if you installed SSH.  Each individual [[VE]] should have its own pair of SSH host keys.  The code below will wipe out the existing SSH keys and instruct the newly-created [[VE]] to create new SSH keys on first boot.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- please do not remove &amp;lt;source&amp;gt;...&amp;lt;/source&amp;gt; pair of tags below,&lt;br /&gt;
     otherwise quotes after -N (-N '') are not visible --&amp;gt;&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
rm -f /etc/ssh/ssh_host_*&lt;br /&gt;
cat &amp;lt;&amp;lt; EOF &amp;gt; /etc/rc2.d/S15ssh_gen_host_keys&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
ssh-keygen -f /etc/ssh/ssh_host_rsa_key -t rsa -N ''&lt;br /&gt;
ssh-keygen -f /etc/ssh/ssh_host_dsa_key -t dsa -N ''&lt;br /&gt;
rm -f \$0&lt;br /&gt;
EOF&lt;br /&gt;
chmod a+x /etc/rc2.d/S15ssh_gen_host_keys&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Note|This will not work using the dependency-based boot sequence introduced with Squeeze. See the section below. }}&lt;br /&gt;
&lt;br /&gt;
=== Fix SSH host keys in Squeeze when using dependency-based booting ===&lt;br /&gt;
&lt;br /&gt;
 rm -f /etc/ssh/ssh_host_*&lt;br /&gt;
&amp;lt;!-- please do not remove &amp;lt;source&amp;gt;...&amp;lt;/source&amp;gt; pair of tags below,&lt;br /&gt;
     otherwise quotes after -N (-N '') are not visible --&amp;gt;&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
cat &amp;lt;&amp;lt; EOF &amp;gt; /etc/init.d/ssh_gen_host_keys&lt;br /&gt;
#!/bin/sh&lt;br /&gt;
### BEGIN INIT INFO&lt;br /&gt;
# Provides:          Generates new ssh host keys on first boot&lt;br /&gt;
# Required-Start:    $remote_fs $syslog&lt;br /&gt;
# Required-Stop:     $remote_fs $syslog&lt;br /&gt;
# Default-Start:     2 3 4 5&lt;br /&gt;
# Default-Stop:&lt;br /&gt;
# Short-Description: Generates new ssh host keys on first boot&lt;br /&gt;
# Description:       Generates new ssh host keys on first boot&lt;br /&gt;
### END INIT INFO&lt;br /&gt;
ssh-keygen -f /etc/ssh/ssh_host_rsa_key -t rsa -N &amp;quot;&amp;quot;&lt;br /&gt;
ssh-keygen -f /etc/ssh/ssh_host_dsa_key -t dsa -N &amp;quot;&amp;quot;&lt;br /&gt;
insserv -r /etc/init.d/ssh_gen_host_keys&lt;br /&gt;
rm -f \$0&lt;br /&gt;
EOF&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
 chmod a+x /etc/init.d/ssh_gen_host_keys&lt;br /&gt;
 insserv /etc/init.d/ssh_gen_host_keys&lt;br /&gt;
&lt;br /&gt;
=== Change timezone ===&lt;br /&gt;
&lt;br /&gt;
You might want to change timezone if you do not live in $UTC. The following example is for Germany&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ln -sf /usr/share/zoneinfo/Europe/Berlin /etc/localtime&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
or even better&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
dpkg-reconfigure tzdata&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Clean packages ===&lt;br /&gt;
After installing packages, you'll have some junk packages laying around in your cache.  Since you don't want your template to have those, this command will wipe them out.&lt;br /&gt;
 apt-get --purge clean&lt;br /&gt;
&lt;br /&gt;
Now everything is done.  Exit from the template and go back to the hardware node.&lt;br /&gt;
 exit&lt;br /&gt;
&lt;br /&gt;
== Preparing for and packing template cache ==&lt;br /&gt;
&lt;br /&gt;
We don't need an IP for the VE anymore, and we definitely do not need it in template cache, so remove it:&lt;br /&gt;
 sudo vzctl set 777 --ipdel all --save&lt;br /&gt;
&lt;br /&gt;
Also, remove DNS server and search domain information from ''/etc/resolv.conf'' file '''in VE''':&lt;br /&gt;
 sudo nano /vz/private/777/etc/resolv.conf&lt;br /&gt;
&lt;br /&gt;
Also, remove ''/etc/hostname'' file '''in VE''':&lt;br /&gt;
 sudo rm -f /vz/private/777/etc/hostname&lt;br /&gt;
&lt;br /&gt;
Stop the VE:&lt;br /&gt;
 sudo vzctl stop 777&lt;br /&gt;
&lt;br /&gt;
Go to the VE directory:&lt;br /&gt;
 cd /vz/private/777&lt;br /&gt;
&lt;br /&gt;
Now create a cached OS tarball.  In the command below, you'll want to replace &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; with your architecture (i386, amd64, ia64, etc).&lt;br /&gt;
 sudo tar --numeric-owner -zcf /vz/template/cache/debian-5.0-i386-minimal.tar.gz .&lt;br /&gt;
&lt;br /&gt;
Look at the resulting tarball to see its size is sane:&lt;br /&gt;
 # ls -lh /vz/template/cache&lt;br /&gt;
 -rw-r--r--  1 root root  51M Apr 10 03:16 debian-5.0-i386-minimal.tar.gz&lt;br /&gt;
&lt;br /&gt;
== Checking if template cache works ==&lt;br /&gt;
We can now create a VE based on the just-created template cache.  Be sure to change &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; to your architecture just like you did when you named the tarball above.&lt;br /&gt;
 sudo vzctl create 123456 --ostemplate debian-5.0-i386-minimal&lt;br /&gt;
&lt;br /&gt;
Now make sure that it works:&lt;br /&gt;
 sudo vzctl start 123456&lt;br /&gt;
 sudo vzctl exec 123456 ps ax&lt;br /&gt;
&lt;br /&gt;
You should see that a few processes are running.&lt;br /&gt;
&lt;br /&gt;
== Final cleanup ==&lt;br /&gt;
Stop and remove the test VE you just created:&lt;br /&gt;
 sudo vzctl stop 123456&lt;br /&gt;
 sudo vzctl destroy 123456&lt;br /&gt;
 sudo rm /etc/vz/conf/123456.conf.destroyed&lt;br /&gt;
&lt;br /&gt;
Finally, let's remove the VE we used for OS template cache creation:&lt;br /&gt;
 sudo vzctl destroy 777&lt;br /&gt;
 sudo rm /etc/vz/conf/777.conf.destroyed&lt;br /&gt;
&lt;br /&gt;
You might want to edit /etc/vz/vz.conf and change DEF_OSTEMPLATE to the name of the template you use most often so that you don't have to specify the template when creating a VE.&lt;br /&gt;
 DEF_OSTEMPLATE=&amp;quot;debian-5.0-i386-minimal&amp;quot;&lt;br /&gt;
If you use iptables, you might want to include additional modules in the list for IPTABLES in /etc/vz/vz.conf. See ''man vzctl'' for a list of available modules.&lt;br /&gt;
&lt;br /&gt;
[[Category: HOWTO]]&lt;br /&gt;
[[Category: Templates]]&lt;br /&gt;
[[Category: Debian]]&lt;/div&gt;</summary>
		<author><name>Mose</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Debian_template_creation&amp;diff=12055</id>
		<title>Debian template creation</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Debian_template_creation&amp;diff=12055"/>
		<updated>2012-02-28T20:53:51Z</updated>

		<summary type="html">&lt;p&gt;Mose: /* Preparing the HN network */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;These are rough instructions of how to manually create basic Debian template cache, which can be used to create OpenVZ [[VE]]s based on Debian. &lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* You shouldn't be running as root, but as a user that is permitted to use sudo instead.  It's a dangerous idea, run as root at your peril.&lt;br /&gt;
* Anywhere you see &amp;lt;tt&amp;gt;/vz&amp;lt;/tt&amp;gt;, you might really need to use &amp;lt;tt&amp;gt;/var/lib/vz&amp;lt;/tt&amp;gt; instead, especially on a Debian Etch host.&lt;br /&gt;
* Anywhere you see &amp;lt;tt&amp;gt;http://http.us.debian.org/debian/&amp;lt;/tt&amp;gt;, you can substitute your favorite Debian mirror.  ([http://www.debian.org/mirror/list List of official Debian Mirrors])&lt;br /&gt;
* See also: &amp;lt;tt&amp;gt;/usr/share/doc/vzctl/README.Debian&amp;lt;/tt&amp;gt; in the ''vzctl'' Debian package&lt;br /&gt;
&lt;br /&gt;
== Prerequisites ==&lt;br /&gt;
&lt;br /&gt;
{{Warning|if you want to use &amp;lt;code&amp;gt;ext4&amp;lt;/code&amp;gt; file system for &amp;lt;code&amp;gt;/vz&amp;lt;/code&amp;gt;, use &amp;lt;code&amp;gt;nodelalloc&amp;lt;/code&amp;gt; option in &amp;lt;code&amp;gt;/etc/fstab&amp;lt;/code&amp;gt;, otherwise it will crash. See {{Bug|1509}} and its duplicates for details.}}&lt;br /&gt;
&lt;br /&gt;
You need to have a working copy of &amp;lt;tt&amp;gt;debootstrap&amp;lt;/tt&amp;gt; running on your hardware node.&lt;br /&gt;
&lt;br /&gt;
For Debian:&lt;br /&gt;
 sudo apt-get install debootstrap&lt;br /&gt;
&lt;br /&gt;
For Gentoo:&lt;br /&gt;
 sudo emerge debootstrap&lt;br /&gt;
&lt;br /&gt;
For Fedora (at least Fedora 8 have it, not sure about earlier versions):&lt;br /&gt;
 sudo yum install debootstrap&lt;br /&gt;
&lt;br /&gt;
For other distros you might need to install it from sources, or search for an appropriate package for your distribution.  An RPM is available on the [http://forum.openvz.org/index.php?t=tree&amp;amp;th=142&amp;amp;mid=584 OpenVZ Forum].&lt;br /&gt;
&lt;br /&gt;
== Bootstrapping Debian ==&lt;br /&gt;
&lt;br /&gt;
You can install different releases of Debian into a VE's private directory using the debootstrap command.&lt;br /&gt;
&lt;br /&gt;
The command parameters are:&lt;br /&gt;
&lt;br /&gt;
  debootstrap --arch ARCH NAME DIRECTORY URL&lt;br /&gt;
&lt;br /&gt;
Specify your architecture instead of &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; if you're using something other than i386/x86.  For example, for AMD64/x86_64, use &amp;lt;tt&amp;gt;amd64&amp;lt;/tt&amp;gt; or for ia64, use &amp;lt;tt&amp;gt;ia64&amp;lt;/tt&amp;gt;. You can use http or ftp in the URL.&lt;br /&gt;
&lt;br /&gt;
We use VE ID of 777 for this example, but it can be any unused ID.&lt;br /&gt;
&lt;br /&gt;
=== Squeeze (current Debian stable) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap --arch i386 squeeze /vz/private/777 http://http.us.debian.org/debian/&lt;br /&gt;
 or&lt;br /&gt;
 debootstrap --arch amd64 squeeze /vz/private/777 ftp://ftp.us.debian.org/debian/&lt;br /&gt;
&lt;br /&gt;
=== Lenny (Debian oldstable) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap --arch i386 lenny /vz/private/777 http://http.us.debian.org/debian/&lt;br /&gt;
 or&lt;br /&gt;
 debootstrap --arch amd64 lenny /vz/private/777 ftp://ftp.us.debian.org/debian/&lt;br /&gt;
&lt;br /&gt;
=== Etch (old release) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap --arch i386 etch /vz/private/777 http://http.us.debian.org/debian/&lt;br /&gt;
&lt;br /&gt;
=== Sarge (very old release) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap sarge /vz/private/777 http://archive.debian.org/debian&lt;br /&gt;
&lt;br /&gt;
== Preparing the HN network ==&lt;br /&gt;
Append the following lines to /etc/sysctl.conf, adjust to taste and then execute &amp;quot;sysctl -p&amp;quot; for them to take effect.&lt;br /&gt;
 ### OpenVZ settings&lt;br /&gt;
 &lt;br /&gt;
 # On Hardware Node enable packet forwarding to forward&lt;br /&gt;
 # packets between the HN network interfaces and venet.&lt;br /&gt;
 # Proxy arp is needed when CT is in a different subnet&lt;br /&gt;
 # or when using veth AND veth is not bridged to a HN&lt;br /&gt;
 # interface. When veth is bridged to a HN interface,&lt;br /&gt;
 # the CT handles its own arps.&lt;br /&gt;
 &lt;br /&gt;
 net.ipv4.conf.default.forwarding=1&lt;br /&gt;
 net.ipv4.conf.default.proxy_arp = 0&lt;br /&gt;
 net.ipv4.ip_forward=1&lt;br /&gt;
 &lt;br /&gt;
 # Enables source route verification&lt;br /&gt;
 net.ipv4.conf.all.rp_filter = 1&lt;br /&gt;
 &lt;br /&gt;
 # Enables the magic-sysrq key&lt;br /&gt;
 kernel.sysrq = 1&lt;br /&gt;
 &lt;br /&gt;
 # TCP Explict Congestion Notification&lt;br /&gt;
 net.ipv4.tcp_ecn = 0&lt;br /&gt;
 &lt;br /&gt;
 # we do not want all our interfaces to send redirects&lt;br /&gt;
 net.ipv4.conf.default.send_redirects = 1&lt;br /&gt;
 net.ipv4.conf.all.send_redirects = 0&lt;br /&gt;
&lt;br /&gt;
== Preparing and starting the VE ==&lt;br /&gt;
&lt;br /&gt;
=== Setting VE config ===&lt;br /&gt;
First, we need a config for the [[VE]]:&lt;br /&gt;
 sudo vzctl set 777 --applyconfig vps.basic --save&lt;br /&gt;
&lt;br /&gt;
On debian squeeze only the following worked for me (''confirmed''), because the standard template names in /etc/vz/conf have changed.&lt;br /&gt;
 sudo vzctl set 777 --applyconfig basic --save&lt;br /&gt;
&lt;br /&gt;
=== Setting VE OSTEMPLATE ===&lt;br /&gt;
Also, we need &amp;lt;tt&amp;gt;OSTEMPLATE&amp;lt;/tt&amp;gt; to be set in VE configuration file, for [[vzctl]] to work properly.&lt;br /&gt;
&lt;br /&gt;
 sudo sh -c 'echo OSTEMPLATE=\&amp;quot;debian-5.0\&amp;quot;' &amp;gt;&amp;gt; /etc/vz/conf/777.conf&lt;br /&gt;
&lt;br /&gt;
=== Setting VE IP address ===&lt;br /&gt;
For the [[VE]] to be able to download updates from the Internet, we need a valid IP address for it:&lt;br /&gt;
 sudo vzctl set 777 --ipadd x.x.x.x --save&lt;br /&gt;
&lt;br /&gt;
{{Note|if you use private IP for the VE, you might have to set up NAT as described in [[Using NAT for VE with private IPs]].}}&lt;br /&gt;
&lt;br /&gt;
=== Setting DNS server for VE ===&lt;br /&gt;
For the [[VE]] to be able to download updates from the Internet, we also need to specify a DNS for it:&lt;br /&gt;
 sudo vzctl set 777 --nameserver x.x.x.x --save&lt;br /&gt;
&lt;br /&gt;
=== Creating /dev/ptmx  ===&lt;br /&gt;
The ptmx character device should normally exist, but if it doesn't, create one.&lt;br /&gt;
 sudo mknod --mode 666 /var/lib/vz/private/777/dev/ptmx c 5 2&lt;br /&gt;
&lt;br /&gt;
=== Starting VE ===&lt;br /&gt;
Now start the VE:&lt;br /&gt;
 sudo vzctl start 777&lt;br /&gt;
&lt;br /&gt;
== Customizing the installation ==&lt;br /&gt;
A few things need to be done inside a newly created VE for it to become suitable for OpenVZ. Enter the VE to begin the configuration. Exporting the path is optional.&lt;br /&gt;
 sudo vzctl enter 777&lt;br /&gt;
 export PATH=/sbin:/usr/sbin:/bin:/usr/bin&lt;br /&gt;
&lt;br /&gt;
{{Warning|Do not run the commands below on the hardware node, they are only to be run within the VE!}}&lt;br /&gt;
&lt;br /&gt;
=== Set Debian repositories ===&lt;br /&gt;
 cat &amp;lt;&amp;lt;EOF &amp;gt; /etc/apt/sources.list&lt;br /&gt;
 deb http://http.us.debian.org/debian squeeze main contrib&lt;br /&gt;
 deb http://security.debian.org squeeze/updates main contrib&lt;br /&gt;
 deb http://http.us.debian.org/debian squeeze-updates main&lt;br /&gt;
 ## backports - ONLY IF YOU KNOW WHAT YOU DO&lt;br /&gt;
 # deb http://http.us.debian.org/debian-backports/ squeeze-backports main&lt;br /&gt;
 EOF&lt;br /&gt;
&lt;br /&gt;
=== Get new security updates ===&lt;br /&gt;
 apt-get update&lt;br /&gt;
 apt-get upgrade&lt;br /&gt;
&lt;br /&gt;
=== Install some more packages ===&lt;br /&gt;
Installing packages could be an interactive process so the system might ask some questions.  You can install more packages if you'd like.  For example:&lt;br /&gt;
 apt-get install ssh quota less&lt;br /&gt;
&lt;br /&gt;
=== Set sane permissions for &amp;lt;tt&amp;gt;/root&amp;lt;/tt&amp;gt; directory ===&lt;br /&gt;
 chmod 700 /root&lt;br /&gt;
&lt;br /&gt;
=== Disable root login ===&lt;br /&gt;
This will disable root login by default.&lt;br /&gt;
 usermod -L root&lt;br /&gt;
&lt;br /&gt;
=== Disable getty ===&lt;br /&gt;
Disable running &amp;lt;tt&amp;gt;getty&amp;lt;/tt&amp;gt;s on terminals as a VE does not have any:&lt;br /&gt;
 sed -i -e '/getty/d' /etc/inittab&lt;br /&gt;
&lt;br /&gt;
=== Disable &amp;lt;tt&amp;gt;sync()&amp;lt;/tt&amp;gt; for syslog ===&lt;br /&gt;
Turn off doing &amp;lt;tt&amp;gt;sync()&amp;lt;/tt&amp;gt; on every write for &amp;lt;tt&amp;gt;syslog&amp;lt;/tt&amp;gt;'s log files, to improve I/O performance:&lt;br /&gt;
 &amp;lt;pre&amp;gt;sed -i -e 's@\([[:space:]]\)\(/var/log/\)@\1-\2@' /etc/*syslog.conf&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Fix &amp;lt;tt&amp;gt;/etc/mtab&amp;lt;/tt&amp;gt; ===&lt;br /&gt;
Link &amp;lt;tt&amp;gt;/etc/mtab&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;/proc/mounts&amp;lt;/tt&amp;gt;, so &amp;lt;tt&amp;gt;df&amp;lt;/tt&amp;gt; and friends will work:&lt;br /&gt;
 rm -f /etc/mtab&lt;br /&gt;
 ln -s /proc/mounts /etc/mtab&lt;br /&gt;
&lt;br /&gt;
=== Remove some unneeded packages ===&lt;br /&gt;
If you have any packages you'd like to remove, now's the time for it.  Here's an example — note that not all of those packages are installed by default in Debian Squeeze (although they were in earlier versions):&lt;br /&gt;
 dpkg --purge modutils ppp pppoeconf pppoe pppconfig module-init-tools&lt;br /&gt;
&lt;br /&gt;
=== Disable services ===&lt;br /&gt;
Do not start some services, stick to bare minimum:&lt;br /&gt;
 update-rc.d -f klogd remove&lt;br /&gt;
 update-rc.d -f quotarpc remove&lt;br /&gt;
 update-rc.d -f exim4 remove&lt;br /&gt;
 update-rc.d -f inetd remove&lt;br /&gt;
&lt;br /&gt;
For dependency-based boot sequence introduced with Squeeze type:&lt;br /&gt;
&lt;br /&gt;
 update-rc.d-insserv -f klogd remove&lt;br /&gt;
 update-rc.d-insserv -f quotarpc remove&lt;br /&gt;
 update-rc.d-insserv -f exim4 remove&lt;br /&gt;
 update-rc.d-insserv -f inetd remove&lt;br /&gt;
&lt;br /&gt;
=== Fix SSH host keys ===&lt;br /&gt;
This is only useful if you installed SSH.  Each individual [[VE]] should have its own pair of SSH host keys.  The code below will wipe out the existing SSH keys and instruct the newly-created [[VE]] to create new SSH keys on first boot.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- please do not remove &amp;lt;source&amp;gt;...&amp;lt;/source&amp;gt; pair of tags below,&lt;br /&gt;
     otherwise quotes after -N (-N '') are not visible --&amp;gt;&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
rm -f /etc/ssh/ssh_host_*&lt;br /&gt;
cat &amp;lt;&amp;lt; EOF &amp;gt; /etc/rc2.d/S15ssh_gen_host_keys&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
ssh-keygen -f /etc/ssh/ssh_host_rsa_key -t rsa -N ''&lt;br /&gt;
ssh-keygen -f /etc/ssh/ssh_host_dsa_key -t dsa -N ''&lt;br /&gt;
rm -f \$0&lt;br /&gt;
EOF&lt;br /&gt;
chmod a+x /etc/rc2.d/S15ssh_gen_host_keys&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Note|This will not work using the dependency-based boot sequence introduced with Squeeze. See the section below. }}&lt;br /&gt;
&lt;br /&gt;
=== Fix SSH host keys in Squeeze when using dependency-based booting ===&lt;br /&gt;
&lt;br /&gt;
 rm -f /etc/ssh/ssh_host_*&lt;br /&gt;
&amp;lt;!-- please do not remove &amp;lt;source&amp;gt;...&amp;lt;/source&amp;gt; pair of tags below,&lt;br /&gt;
     otherwise quotes after -N (-N '') are not visible --&amp;gt;&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
cat &amp;lt;&amp;lt; EOF &amp;gt; /etc/init.d/ssh_gen_host_keys&lt;br /&gt;
#!/bin/sh&lt;br /&gt;
### BEGIN INIT INFO&lt;br /&gt;
# Provides:          Generates new ssh host keys on first boot&lt;br /&gt;
# Required-Start:    $remote_fs $syslog&lt;br /&gt;
# Required-Stop:     $remote_fs $syslog&lt;br /&gt;
# Default-Start:     2 3 4 5&lt;br /&gt;
# Default-Stop:&lt;br /&gt;
# Short-Description: Generates new ssh host keys on first boot&lt;br /&gt;
# Description:       Generates new ssh host keys on first boot&lt;br /&gt;
### END INIT INFO&lt;br /&gt;
ssh-keygen -f /etc/ssh/ssh_host_rsa_key -t rsa -N &amp;quot;&amp;quot;&lt;br /&gt;
ssh-keygen -f /etc/ssh/ssh_host_dsa_key -t dsa -N &amp;quot;&amp;quot;&lt;br /&gt;
insserv -r /etc/init.d/ssh_gen_host_keys&lt;br /&gt;
rm -f \$0&lt;br /&gt;
EOF&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
 chmod a+x /etc/init.d/ssh_gen_host_keys&lt;br /&gt;
 insserv /etc/init.d/ssh_gen_host_keys&lt;br /&gt;
&lt;br /&gt;
=== Change timezone ===&lt;br /&gt;
&lt;br /&gt;
You might want to change timezone if you do not live in $UTC. The following example is for Germany&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ln -sf /usr/share/zoneinfo/Europe/Berlin /etc/localtime&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
or even better&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
dpkg-reconfigure tzdata&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Clean packages ===&lt;br /&gt;
After installing packages, you'll have some junk packages laying around in your cache.  Since you don't want your template to have those, this command will wipe them out.&lt;br /&gt;
 apt-get --purge clean&lt;br /&gt;
&lt;br /&gt;
Now everything is done.  Exit from the template and go back to the hardware node.&lt;br /&gt;
 exit&lt;br /&gt;
&lt;br /&gt;
== Preparing for and packing template cache ==&lt;br /&gt;
&lt;br /&gt;
We don't need an IP for the VE anymore, and we definitely do not need it in template cache, so remove it:&lt;br /&gt;
 sudo vzctl set 777 --ipdel all --save&lt;br /&gt;
&lt;br /&gt;
Also, remove DNS server and search domain information from ''/etc/resolv.conf'' file '''in VE''':&lt;br /&gt;
 sudo nano /vz/private/777/etc/resolv.conf&lt;br /&gt;
&lt;br /&gt;
Also, remove ''/etc/hostname'' file '''in VE''':&lt;br /&gt;
 sudo rm -f /vz/private/777/etc/hostname&lt;br /&gt;
&lt;br /&gt;
Stop the VE:&lt;br /&gt;
 sudo vzctl stop 777&lt;br /&gt;
&lt;br /&gt;
Go to the VE directory:&lt;br /&gt;
 cd /vz/private/777&lt;br /&gt;
&lt;br /&gt;
Now create a cached OS tarball.  In the command below, you'll want to replace &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; with your architecture (i386, amd64, ia64, etc).&lt;br /&gt;
 sudo tar --numeric-owner -zcf /vz/template/cache/debian-5.0-i386-minimal.tar.gz .&lt;br /&gt;
&lt;br /&gt;
Look at the resulting tarball to see its size is sane:&lt;br /&gt;
 # ls -lh /vz/template/cache&lt;br /&gt;
 -rw-r--r--  1 root root  51M Apr 10 03:16 debian-5.0-i386-minimal.tar.gz&lt;br /&gt;
&lt;br /&gt;
== Checking if template cache works ==&lt;br /&gt;
We can now create a VE based on the just-created template cache.  Be sure to change &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; to your architecture just like you did when you named the tarball above.&lt;br /&gt;
 sudo vzctl create 123456 --ostemplate debian-5.0-i386-minimal&lt;br /&gt;
&lt;br /&gt;
Now make sure that it works:&lt;br /&gt;
 sudo vzctl start 123456&lt;br /&gt;
 sudo vzctl exec 123456 ps ax&lt;br /&gt;
&lt;br /&gt;
You should see that a few processes are running.&lt;br /&gt;
&lt;br /&gt;
== Final cleanup ==&lt;br /&gt;
Stop and remove the test VE you just created:&lt;br /&gt;
 sudo vzctl stop 123456&lt;br /&gt;
 sudo vzctl destroy 123456&lt;br /&gt;
 sudo rm /etc/vz/conf/123456.conf.destroyed&lt;br /&gt;
&lt;br /&gt;
Finally, let's remove the VE we used for OS template cache creation:&lt;br /&gt;
 sudo vzctl destroy 777&lt;br /&gt;
 sudo rm /etc/vz/conf/777.conf.destroyed&lt;br /&gt;
&lt;br /&gt;
You might want to edit /etc/vz/vz.conf and change DEF_OSTEMPLATE to the name of the template you use most often so that you don't have to specify the template when creating a VE.&lt;br /&gt;
 DEF_OSTEMPLATE=&amp;quot;debian-5.0-i386-minimal&amp;quot;&lt;br /&gt;
If you use iptables, you might want to include additional modules in the list for IPTABLES in /etc/vz/vz.conf. See ''man vzctl'' for a list of available modules.&lt;br /&gt;
&lt;br /&gt;
[[Category: HOWTO]]&lt;br /&gt;
[[Category: Templates]]&lt;br /&gt;
[[Category: Debian]]&lt;/div&gt;</summary>
		<author><name>Mose</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Bridge_doesn%27t_forward_packets&amp;diff=11999</id>
		<title>Bridge doesn't forward packets</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Bridge_doesn%27t_forward_packets&amp;diff=11999"/>
		<updated>2012-02-19T05:02:49Z</updated>

		<summary type="html">&lt;p&gt;Mose: /* Another Problem Case */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Sometimes a bridge can mysteriously drop packets and not forward them.&lt;br /&gt;
e.g. eyck user experienced a problem when some of the broadcasts were not&lt;br /&gt;
delivered to container via the bridge.&lt;br /&gt;
&lt;br /&gt;
Original report and the thread: [http://forum.openvz.org/index.php?t=tree&amp;amp;th=4052&amp;amp; forum thread]&lt;br /&gt;
&lt;br /&gt;
== Simplest configuration ==&lt;br /&gt;
&lt;br /&gt;
Container #101 with veth interface (veth101.0) connected to eth0 physical interface via bridge.&lt;br /&gt;
&lt;br /&gt;
== Problem statement ==&lt;br /&gt;
&lt;br /&gt;
We faced a situation when some of the broadcast packets were not delivered to&lt;br /&gt;
the container. Actually it could happen with any packets, not with the&lt;br /&gt;
broadcasts only. But broadcasts are simpler and obviously should have been&lt;br /&gt;
delivered to all the networking interfaces with no doubt.&lt;br /&gt;
&lt;br /&gt;
Using tcpdump we see that BOOTP/DHCP request is visible on br0 interface in&lt;br /&gt;
the host system ([[CT0]]):&lt;br /&gt;
  15:21:52.258220 00:1b:d5:2c:bf:38 &amp;gt; ff:ff:ff:ff:ff:ff, ethertype IPv4 (0x0800), length 350: 0.0.0.0.68 &amp;gt; 255.255.255.255.67:&lt;br /&gt;
    BOOTP/DHCP, Request from 00:1b:d5:2c:bf:38, length 308&lt;br /&gt;
  15:21:52.287269 00:08:02:ac:36:20 &amp;gt; ff:ff:ff:ff:ff:ff, ethertype IPv4 (0x0800), length 342: 172.17.8.254.67 &amp;gt; 255.255.255.255.68:&lt;br /&gt;
    BOOTP/DHCP, Reply, length 300&lt;br /&gt;
&lt;br /&gt;
However, eth0 inside the container received only 2nd packet with a BOOTP/DHCP reply and doesn't see the 1st one with the request itself:&lt;br /&gt;
  15:21:52.291145 00:08:02:ac:36:20 &amp;gt; ff:ff:ff:ff:ff:ff, ethertype IPv4 (0x0800), length 342: 172.17.8.254.67 &amp;gt; 255.255.255.255.68:&lt;br /&gt;
    BOOTP/DHCP, Reply, length 300&lt;br /&gt;
&lt;br /&gt;
== Resolution ==&lt;br /&gt;
&lt;br /&gt;
It is not obvious at all, but bridges (though they have their own ebtables filters) do also call iptables FORWARD chain when forwarding packets between interfaces.&lt;br /&gt;
Thus your FORWARD iptables rules should allow all the packets which are supposed to go through.&lt;br /&gt;
&lt;br /&gt;
in our case eyck had a default DROP policy on FORWARD and had to add:&lt;br /&gt;
  iptables -A FORWARD -d 255.255.255.255 -j ACCEPT&lt;br /&gt;
to fix the issue.&lt;br /&gt;
&lt;br /&gt;
== Another Problem Case ==&lt;br /&gt;
I had setup a bridge and got the same problem, but iptables was setup well. In my case the problem was lying in /proc/sys/net/bridge/.&lt;br /&gt;
Everything inside had value &amp;quot;1&amp;quot;. Changing them to &amp;quot;0&amp;quot; solved the problem. This stopped ARP and bridge packets from being&lt;br /&gt;
passed through the FORWARD chain. These settings can be placed inside /etc/sysctl.conf (Debian) so that they are persistent.&lt;br /&gt;
&lt;br /&gt;
== Credits ==&lt;br /&gt;
Many credits to Dariush Pietrzak, who patiently helped to debug this.&lt;br /&gt;
&lt;br /&gt;
[[Category:Troubleshooting]]&lt;br /&gt;
[[Category:Networking]]&lt;/div&gt;</summary>
		<author><name>Mose</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Virtual_Ethernet_device&amp;diff=11998</id>
		<title>Virtual Ethernet device</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Virtual_Ethernet_device&amp;diff=11998"/>
		<updated>2012-02-19T04:58:34Z</updated>

		<summary type="html">&lt;p&gt;Mose: /* Automating the bridge */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Virtual Ethernet device''' is an Ethernet-like device that can be used&lt;br /&gt;
inside a [[container]]. Unlike a [[venet]] network device, a [[veth]] device&lt;br /&gt;
has a MAC address. Therefore, it can be used in more configurations. When veth&lt;br /&gt;
is bridged to a [[CT0]] network interface (e.g., eth0), the container can act as an&lt;br /&gt;
independent host on the network. The container's user can set up all of the networking&lt;br /&gt;
himself, including IPs, gateways, etc.&lt;br /&gt;
&lt;br /&gt;
A virtual Ethernet device consist of two Ethernet devices,&lt;br /&gt;
one in [[CT0]] (e.g., vethN.0) and a corresponding one in CT (e.g., eth0) that are&lt;br /&gt;
connected to each other. If a packet is sent to one device it will come out the other device.&lt;br /&gt;
&lt;br /&gt;
== Virtual Ethernet device usage ==&lt;br /&gt;
&lt;br /&gt;
=== Kernel module ===&lt;br /&gt;
The &amp;lt;code&amp;gt;vzethdev&amp;lt;/code&amp;gt; module should be loaded. You can check it with the following commands.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# lsmod | grep vzeth&lt;br /&gt;
vzethdev                8224  0&lt;br /&gt;
vzmon                  35164  5 vzethdev,vznetdev,vzrst,vzcpt&lt;br /&gt;
vzdev                   3080  4 vzethdev,vznetdev,vzmon,vzdquota&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In case it is not loaded, load it:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# modprobe vzethdev&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== MAC addresses ===&lt;br /&gt;
The following steps to generate a MAC address are not necessary, since newer versions&lt;br /&gt;
of vzctl will automatically generate a MAC address for you. These steps are provided&lt;br /&gt;
in case you want to set a MAC address manually.&lt;br /&gt;
&lt;br /&gt;
You should use a random MAC address when adding a network interface to a container. Do not use MAC addresses of real eth devices, because this can lead to collisions.&lt;br /&gt;
&lt;br /&gt;
MAC addresses must be entered in XX:XX:XX:XX:XX:XX format.&lt;br /&gt;
&lt;br /&gt;
There is a utility script available for generating MAC addresses: http://www.easyvmx.com/software/easymac.sh. It is used like this:&lt;br /&gt;
&lt;br /&gt;
 chmod +x easymac.sh&lt;br /&gt;
 ./easymac.sh -R&lt;br /&gt;
&lt;br /&gt;
=== Adding veth to a CT ===&lt;br /&gt;
&lt;br /&gt;
 vzctl set &amp;lt;CTID&amp;gt; --netif_add &amp;lt;ifname&amp;gt;[,&amp;lt;mac&amp;gt;,&amp;lt;host_ifname&amp;gt;,&amp;lt;host_mac&amp;gt;,&amp;lt;bridge&amp;gt;]&lt;br /&gt;
&lt;br /&gt;
Here&lt;br /&gt;
* &amp;lt;tt&amp;gt;ifname&amp;lt;/tt&amp;gt; is the Ethernet device name in the CT&lt;br /&gt;
* &amp;lt;tt&amp;gt;mac&amp;lt;/tt&amp;gt; is its MAC address in the CT&lt;br /&gt;
* &amp;lt;tt&amp;gt;host_ifname&amp;lt;/tt&amp;gt;  is the Ethernet device name on the host ([[CT0]])&lt;br /&gt;
* &amp;lt;tt&amp;gt;host_mac&amp;lt;/tt&amp;gt; is its MAC address on the host ([[CT0]])&lt;br /&gt;
* &amp;lt;tt&amp;gt;bridge&amp;lt;/tt&amp;gt; is an optional parameter which can be used in custom network start scripts to automatically add  the interface to a bridge. (See the reference to the vznetaddbr script below and persistent bridge configurations.)&lt;br /&gt;
&lt;br /&gt;
{{Note|All parameters except &amp;lt;code&amp;gt;ifname&amp;lt;/code&amp;gt; are optional. Missing parameters, except for bridge, are automatically generated, if not specified. This is the preferred method.}}&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 vzctl set 101 --netif_add eth0 --save&lt;br /&gt;
&lt;br /&gt;
If you want to specify everything:&lt;br /&gt;
&lt;br /&gt;
 vzctl set 101 --netif_add eth0,00:12:34:56:78:9A,veth101.0,00:12:34:56:78:9B --save&lt;br /&gt;
&lt;br /&gt;
If you want to specify the bridge and autogenerate the other values:&lt;br /&gt;
&lt;br /&gt;
 vzctl set 101 --netif_add eth0,,,,vzbr0 --save&lt;br /&gt;
&lt;br /&gt;
=== Removing veth from a CT ===&lt;br /&gt;
&lt;br /&gt;
 vzctl set &amp;lt;CTID&amp;gt; --netif_del &amp;lt;dev_name&amp;gt;|all&lt;br /&gt;
&lt;br /&gt;
Here&lt;br /&gt;
* &amp;lt;code&amp;gt;dev_name&amp;lt;/code&amp;gt; is the Ethernet device name in the [[CT]].&lt;br /&gt;
&lt;br /&gt;
{{Note|If you want to remove all Ethernet devices in CT, use &amp;lt;code&amp;gt;all&amp;lt;/code&amp;gt;.}}&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 vzctl set 101 --netif_del eth0 --save&lt;br /&gt;
&lt;br /&gt;
== Common configurations with virtual Ethernet devices ==&lt;br /&gt;
Module &amp;lt;tt&amp;gt;vzethdev&amp;lt;/tt&amp;gt; must be loaded to operate with veth devices.&lt;br /&gt;
&lt;br /&gt;
=== Simple configuration with virtual Ethernet device ===&lt;br /&gt;
&lt;br /&gt;
Assuming that 192.168.0.0/24 is being used on your LAN, the following sections show how to configure a container for the LAN using veth.&lt;br /&gt;
&lt;br /&gt;
==== Start a CT ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# vzctl start 101&lt;br /&gt;
&lt;br /&gt;
==== Add veth device to CT ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# vzctl set 101 --netif_add eth0 --save&lt;br /&gt;
&lt;br /&gt;
This allocates a MAC address and associates it with the host eth0 port.&lt;br /&gt;
&lt;br /&gt;
==== Configure devices in CT0 ====&lt;br /&gt;
The following steps are needed when the [[CT]] is '''not''' bridged to a [[CT0]] network interface. That is because the [[CT]] is connected to a virtual network that is &amp;quot;behind&amp;quot; [[CT0]]. [[CT0]] must forward packets between its physical network interface and the virtual network interface where [[CT]] is located. The first step below to configure the interface is not necessary if the container has been started, since the device will have been initialized.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# ifconfig veth101.0 0&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/veth101.0/forwarding&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/veth101.0/proxy_arp&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/eth0/forwarding&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/eth0/proxy_arp&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Configure device in CT ====&lt;br /&gt;
The following steps show an example of a quick manual configuration of the [[CT]] network interface. Typically, you would configure the network settings in /etc/network/interfaces (Debian, see below) or however it is normally configured on your distribution. You can also comment or remove the configuration for venet0, if it exists, because that device will not be used.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# vzctl enter 101&lt;br /&gt;
[ve-101]# /sbin/ifconfig eth0 0&lt;br /&gt;
[ve-101]# /sbin/ip addr add 192.168.0.101 dev eth0&lt;br /&gt;
[ve-101]# /sbin/ip route add default dev eth0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
* Until you ifconfig eth0 it won't appear. When you do it will use the mac address netif_add added earlier&lt;br /&gt;
* 192.168.0.101 is chosen to be an [[unrouteable private ip address]]. Where 101 reminds you that it is node 101.&lt;br /&gt;
* The &amp;quot;ip route&amp;quot; tells all traffic to head to &amp;quot;device eth0&amp;quot;&lt;br /&gt;
* In theory you could [[use dhcpd with OpenVZ]] and dhclient to pick up an DHCP address from your router instead of hardwiring it&lt;br /&gt;
** http://openvz.org/pipermail/users/2005-November/000020.html&lt;br /&gt;
&lt;br /&gt;
==== Add route in [[CT0]] ====&lt;br /&gt;
Since [[CT0]] is acting as a router between its physical network interface and the virtual network interface of the [[CT]], we need to add a route to the [[CT]] to direct traffic to the right destination.&lt;br /&gt;
 [host-node]# ip route add 192.168.0.101 dev veth101.0&lt;br /&gt;
&lt;br /&gt;
=== Using a directly routed IPv4 with virtual Ethernet device ===&lt;br /&gt;
&lt;br /&gt;
==== Situation ====&lt;br /&gt;
Hardware Node (HN/CT0) has 192.168.0.1/24 with router 192.168.0.254.&lt;br /&gt;
&lt;br /&gt;
We also know that IPv4 10.0.0.1/32 is directly routed to 192.168.0.1 (this is called a ''fail-over IP'').&lt;br /&gt;
&lt;br /&gt;
We want to give this directly routed IPv4 address to a container (CT).&lt;br /&gt;
&lt;br /&gt;
==== Start container ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# vzctl start 101&lt;br /&gt;
&lt;br /&gt;
==== Add veth device to CT ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# vzctl set 101 --netif_add eth0 --save&lt;br /&gt;
&lt;br /&gt;
This allocates a MAC address and associates it with the host eth0 port.&lt;br /&gt;
&lt;br /&gt;
==== Configure device and add route in CT0 ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# ifconfig veth101.0 0&lt;br /&gt;
[host-node]# ip route add 10.0.0.1 dev veth101.0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can automatize this at VPS creation by using a mount script &amp;lt;tt&amp;gt;$VEID.mount&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The problem here is that the ''veth'' interface appears in CT0 '''after''' VPS has started, therefore we cannot directly use the commands in the mount script. We launch a shell script (enclosed by { }) in background (operator '''&amp;amp;''') that waits for the interface to be ready and then adds the IP route.&lt;br /&gt;
&lt;br /&gt;
Contents of the mount script &amp;lt;tt&amp;gt;/etc/vz/conf/101.mount&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
# This script source VPS configuration files in the same order as vzctl does&lt;br /&gt;
&lt;br /&gt;
# if one of these files does not exist then something is really broken&lt;br /&gt;
[ -f /etc/vz/vz.conf ] || exit 1&lt;br /&gt;
[ -f $VE_CONFFILE ] || exit 1&lt;br /&gt;
&lt;br /&gt;
# source both files. Note the order, it is important&lt;br /&gt;
. /etc/vz/vz.conf&lt;br /&gt;
. $VE_CONFFILE&lt;br /&gt;
&lt;br /&gt;
# Configure veth with IP after VPS has started&lt;br /&gt;
{&lt;br /&gt;
  IP=X.Y.Z.T&lt;br /&gt;
  DEV=veth101.0&lt;br /&gt;
  while sleep 1; do&lt;br /&gt;
    /sbin/ifconfig $DEV 0 &amp;gt;/dev/null 2&amp;gt;&amp;amp;1&lt;br /&gt;
    if [ $? -eq 0 ]; then&lt;br /&gt;
      /sbin/ip route add $IP dev $DEV&lt;br /&gt;
      break&lt;br /&gt;
    fi&lt;br /&gt;
  done&lt;br /&gt;
} &amp;amp;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Make sure IPv4 forwarding is enabled in CT0 ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/ip_forward&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/eth0/forwarding&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/veth101.0/forwarding&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
You can permanently set this by using &amp;lt;tt&amp;gt;/etc/sysctl.conf&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==== Configure device in CT ====&lt;br /&gt;
&lt;br /&gt;
1. Configure IP address&lt;br /&gt;
&lt;br /&gt;
2. Add gateway&lt;br /&gt;
&lt;br /&gt;
3. Add default route&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[ve-101]# /sbin/ifconfig eth0 10.0.0.1 netmask 255.255.255.255&lt;br /&gt;
[ve-101]# /sbin/ip route add 192.168.0.1 dev eth0&lt;br /&gt;
[ve-101]# /sbin/ip route add default via 192.168.0.1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In a Debian container, you can configure this permanently by using &amp;lt;tt&amp;gt;/etc/network/interfaces&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
auto eth0&lt;br /&gt;
iface eth0 inet static&lt;br /&gt;
        address 10.0.0.1&lt;br /&gt;
        netmask 255.255.255.255&lt;br /&gt;
        up /sbin/ip route add 192.168.0.1 dev eth0&lt;br /&gt;
        up /sbin/ip route add default via 192.168.0.1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Virtual Ethernet device with IPv6 ===&lt;br /&gt;
&lt;br /&gt;
See the [[VEs and HNs in same subnets]] article.&lt;br /&gt;
&lt;br /&gt;
=== Virtual Ethernet devices can be joined in one bridge ===&lt;br /&gt;
Bridging a [[CT]] interface to a [[CT0]] interface is the magic that allows the [[CT]] to be an independent host on the network with its own IP address, gateway, etc. [[CT0]] does not need any configuration for forwarding packets to the [[CT]] or performing proxy arp for the [[CT]].&lt;br /&gt;
&lt;br /&gt;
To manually configure a bridge and add devices to it, perform steps 1 - 4 from Simple configuration chapter for several containers and/or veth devices and then follow these steps.&lt;br /&gt;
&lt;br /&gt;
==== Create bridge device ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# brctl addbr vzbr0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Add veth devices to bridge ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# brctl addif vzbr0 veth101.0&lt;br /&gt;
...&lt;br /&gt;
[host-node]# brctl addif vzbr0 veth101.n&lt;br /&gt;
[host-node]# brctl addif vzbr0 veth102.0&lt;br /&gt;
...&lt;br /&gt;
...&lt;br /&gt;
[host-node]# brctl addif vzbr0 vethXXX.N&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Configure bridge device ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# ifconfig vzbr0 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Add routes in [[CT0]] ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# ip route add 192.168.101.1 dev vzbr0&lt;br /&gt;
...&lt;br /&gt;
[host-node]# ip route add 192.168.101.n dev vzbr0&lt;br /&gt;
[host-node]# ip route add 192.168.102.1 dev vzbr0&lt;br /&gt;
...&lt;br /&gt;
...&lt;br /&gt;
[host-node]# ip route add 192.168.XXX.N dev vzbr0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All routes to containers will be through this bridge and containers can communicate with each other even without these routes.&lt;br /&gt;
&lt;br /&gt;
=== Automating the bridge ===&lt;br /&gt;
The most convenient method is to automatically create the bridge at boot as a network interface, add the physical interface from [[CT0]] and then add the interface from each [[CT]] as it starts. All devices are connected to a virtual switch, and containers directly access the network just as any other host without additional configuration on [[CT0]].&lt;br /&gt;
&lt;br /&gt;
In Debian, configure the network interface on [[CT0]] to plug into a bridge in /etc/network/interfaces. The [[CT0]] physical device is added to the bridge as the &amp;quot;uplink&amp;quot; port to the physical network.&lt;br /&gt;
&lt;br /&gt;
The bridge forwarding delay is set to 0 seconds so that forwarding begins immediately when a new interface is added to a bridge. The default delay is 30 seconds, during which the bridge pauses all traffic to listen and figure out where devices are. This can interrupt services when a container is added to the bridge. If you aren't running the spanning tree protocol (off by default) and the bridge does not create a loop in your network, then there is no need for a forwarding delay.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
iface eth0 inet manual&lt;br /&gt;
&lt;br /&gt;
auto vzbr0&lt;br /&gt;
iface vzbr0 inet static&lt;br /&gt;
        bridge_ports eth0&lt;br /&gt;
        bridge_fd 0&lt;br /&gt;
        address 192.168.1.100&lt;br /&gt;
        netmask 255.255.255.0&lt;br /&gt;
        gateway 192.168.1.254&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Follow the steps below for making a veth bridge persistent with the included script. That will automatically add each container to the bridge when it is started. Finally, specify vzbr0 as the bridge when adding the network interface to a container, as describe above. No configuration is needed on [[CT0]] for forwarding packets, proxy arp or additional routes. The interface in each [[CT]] can be configured as desired. Everything &amp;quot;just works&amp;quot; according to normal network interface configuration and default routing rules. Note that as discussed in the troubleshooting section below, bridged packets by default pass through the FORWARD iptables chain. Take care when adding rules to that table that bridged packets are not mistakenly blocked. This behavior can be disabled, if desired.&lt;br /&gt;
&lt;br /&gt;
=== Making a veth-device persistent ===&lt;br /&gt;
These steps are no longer necessary, as the veth device is automatically created when the container is started. They remain here as a reference.&lt;br /&gt;
&lt;br /&gt;
According to http://bugzilla.openvz.org/show_bug.cgi?id=301 , a bug that stopped the veth device persistent was &amp;quot;Obsoleted now when --veth_add/--veth_del are introduced&amp;quot;&lt;br /&gt;
&lt;br /&gt;
See http://wiki.openvz.org/w/index.php?title=Virtual_Ethernet_device&amp;amp;diff=5990&amp;amp;oldid=5989#Making_a_veth-device_persistent for a workaround that used to be described in this section.&lt;br /&gt;
&lt;br /&gt;
That's it! At this point, when you restart the CT you should see a new line in the output, indicating that the interface is being configured and a new route being added. And you should be able to ping the host, and to enter the CT and use the network.&lt;br /&gt;
&lt;br /&gt;
=== Making a bridged veth-device persistent ===&lt;br /&gt;
&lt;br /&gt;
Like the above example, here it is how to add the veth device to a bridge in a persistent way. &lt;br /&gt;
&lt;br /&gt;
vzctl includes a 'vznetaddbr' script, which makes use of the ''bridge'' parameter of the --netif_add switch.&lt;br /&gt;
&lt;br /&gt;
Just create /etc/vz/vznet.conf containing the following.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
EXTERNAL_SCRIPT=&amp;quot;/usr/sbin/vznetaddbr&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The script uses 'vmbr0' as default bridge name when no bridge is specified.&lt;br /&gt;
&lt;br /&gt;
=== Virtual Ethernet devices + VLAN ===&lt;br /&gt;
This configuration can be done by adding vlan device to the previous configuration.&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
* [[Virtual network device]]&lt;br /&gt;
* [[Differences between venet and veth]]&lt;br /&gt;
* [[Using private IPs for Hardware Nodes]]&lt;br /&gt;
* Patch: [[Disable venet interface]]&lt;br /&gt;
* Troubleshooting: [[Bridge doesn't forward packets]]&lt;br /&gt;
&lt;br /&gt;
== External links ==&lt;br /&gt;
* [http://www.tldp.org/HOWTO/Linux+IPv6-HOWTO/hints-daemons-radvd.html Linux IPv6 HOWTO, a chapter about radvd]&lt;br /&gt;
* [http://sysadmin-ivanov.blogspot.com/2008/02/2-veth-with-2-bridges-on-openvz-at.html 2 veth with 2 bridges setup]&lt;br /&gt;
&lt;br /&gt;
[[Category: Networking]]&lt;br /&gt;
[[Category: HOWTO]]&lt;/div&gt;</summary>
		<author><name>Mose</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Virtual_Ethernet_device&amp;diff=11997</id>
		<title>Virtual Ethernet device</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Virtual_Ethernet_device&amp;diff=11997"/>
		<updated>2012-02-19T04:51:32Z</updated>

		<summary type="html">&lt;p&gt;Mose: /* Automating the bridge */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Virtual Ethernet device''' is an Ethernet-like device that can be used&lt;br /&gt;
inside a [[container]]. Unlike a [[venet]] network device, a [[veth]] device&lt;br /&gt;
has a MAC address. Therefore, it can be used in more configurations. When veth&lt;br /&gt;
is bridged to a [[CT0]] network interface (e.g., eth0), the container can act as an&lt;br /&gt;
independent host on the network. The container's user can set up all of the networking&lt;br /&gt;
himself, including IPs, gateways, etc.&lt;br /&gt;
&lt;br /&gt;
A virtual Ethernet device consist of two Ethernet devices,&lt;br /&gt;
one in [[CT0]] (e.g., vethN.0) and a corresponding one in CT (e.g., eth0) that are&lt;br /&gt;
connected to each other. If a packet is sent to one device it will come out the other device.&lt;br /&gt;
&lt;br /&gt;
== Virtual Ethernet device usage ==&lt;br /&gt;
&lt;br /&gt;
=== Kernel module ===&lt;br /&gt;
The &amp;lt;code&amp;gt;vzethdev&amp;lt;/code&amp;gt; module should be loaded. You can check it with the following commands.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# lsmod | grep vzeth&lt;br /&gt;
vzethdev                8224  0&lt;br /&gt;
vzmon                  35164  5 vzethdev,vznetdev,vzrst,vzcpt&lt;br /&gt;
vzdev                   3080  4 vzethdev,vznetdev,vzmon,vzdquota&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In case it is not loaded, load it:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# modprobe vzethdev&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== MAC addresses ===&lt;br /&gt;
The following steps to generate a MAC address are not necessary, since newer versions&lt;br /&gt;
of vzctl will automatically generate a MAC address for you. These steps are provided&lt;br /&gt;
in case you want to set a MAC address manually.&lt;br /&gt;
&lt;br /&gt;
You should use a random MAC address when adding a network interface to a container. Do not use MAC addresses of real eth devices, because this can lead to collisions.&lt;br /&gt;
&lt;br /&gt;
MAC addresses must be entered in XX:XX:XX:XX:XX:XX format.&lt;br /&gt;
&lt;br /&gt;
There is a utility script available for generating MAC addresses: http://www.easyvmx.com/software/easymac.sh. It is used like this:&lt;br /&gt;
&lt;br /&gt;
 chmod +x easymac.sh&lt;br /&gt;
 ./easymac.sh -R&lt;br /&gt;
&lt;br /&gt;
=== Adding veth to a CT ===&lt;br /&gt;
&lt;br /&gt;
 vzctl set &amp;lt;CTID&amp;gt; --netif_add &amp;lt;ifname&amp;gt;[,&amp;lt;mac&amp;gt;,&amp;lt;host_ifname&amp;gt;,&amp;lt;host_mac&amp;gt;,&amp;lt;bridge&amp;gt;]&lt;br /&gt;
&lt;br /&gt;
Here&lt;br /&gt;
* &amp;lt;tt&amp;gt;ifname&amp;lt;/tt&amp;gt; is the Ethernet device name in the CT&lt;br /&gt;
* &amp;lt;tt&amp;gt;mac&amp;lt;/tt&amp;gt; is its MAC address in the CT&lt;br /&gt;
* &amp;lt;tt&amp;gt;host_ifname&amp;lt;/tt&amp;gt;  is the Ethernet device name on the host ([[CT0]])&lt;br /&gt;
* &amp;lt;tt&amp;gt;host_mac&amp;lt;/tt&amp;gt; is its MAC address on the host ([[CT0]])&lt;br /&gt;
* &amp;lt;tt&amp;gt;bridge&amp;lt;/tt&amp;gt; is an optional parameter which can be used in custom network start scripts to automatically add  the interface to a bridge. (See the reference to the vznetaddbr script below and persistent bridge configurations.)&lt;br /&gt;
&lt;br /&gt;
{{Note|All parameters except &amp;lt;code&amp;gt;ifname&amp;lt;/code&amp;gt; are optional. Missing parameters, except for bridge, are automatically generated, if not specified. This is the preferred method.}}&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 vzctl set 101 --netif_add eth0 --save&lt;br /&gt;
&lt;br /&gt;
If you want to specify everything:&lt;br /&gt;
&lt;br /&gt;
 vzctl set 101 --netif_add eth0,00:12:34:56:78:9A,veth101.0,00:12:34:56:78:9B --save&lt;br /&gt;
&lt;br /&gt;
If you want to specify the bridge and autogenerate the other values:&lt;br /&gt;
&lt;br /&gt;
 vzctl set 101 --netif_add eth0,,,,vzbr0 --save&lt;br /&gt;
&lt;br /&gt;
=== Removing veth from a CT ===&lt;br /&gt;
&lt;br /&gt;
 vzctl set &amp;lt;CTID&amp;gt; --netif_del &amp;lt;dev_name&amp;gt;|all&lt;br /&gt;
&lt;br /&gt;
Here&lt;br /&gt;
* &amp;lt;code&amp;gt;dev_name&amp;lt;/code&amp;gt; is the Ethernet device name in the [[CT]].&lt;br /&gt;
&lt;br /&gt;
{{Note|If you want to remove all Ethernet devices in CT, use &amp;lt;code&amp;gt;all&amp;lt;/code&amp;gt;.}}&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 vzctl set 101 --netif_del eth0 --save&lt;br /&gt;
&lt;br /&gt;
== Common configurations with virtual Ethernet devices ==&lt;br /&gt;
Module &amp;lt;tt&amp;gt;vzethdev&amp;lt;/tt&amp;gt; must be loaded to operate with veth devices.&lt;br /&gt;
&lt;br /&gt;
=== Simple configuration with virtual Ethernet device ===&lt;br /&gt;
&lt;br /&gt;
Assuming that 192.168.0.0/24 is being used on your LAN, the following sections show how to configure a container for the LAN using veth.&lt;br /&gt;
&lt;br /&gt;
==== Start a CT ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# vzctl start 101&lt;br /&gt;
&lt;br /&gt;
==== Add veth device to CT ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# vzctl set 101 --netif_add eth0 --save&lt;br /&gt;
&lt;br /&gt;
This allocates a MAC address and associates it with the host eth0 port.&lt;br /&gt;
&lt;br /&gt;
==== Configure devices in CT0 ====&lt;br /&gt;
The following steps are needed when the [[CT]] is '''not''' bridged to a [[CT0]] network interface. That is because the [[CT]] is connected to a virtual network that is &amp;quot;behind&amp;quot; [[CT0]]. [[CT0]] must forward packets between its physical network interface and the virtual network interface where [[CT]] is located. The first step below to configure the interface is not necessary if the container has been started, since the device will have been initialized.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# ifconfig veth101.0 0&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/veth101.0/forwarding&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/veth101.0/proxy_arp&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/eth0/forwarding&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/eth0/proxy_arp&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Configure device in CT ====&lt;br /&gt;
The following steps show an example of a quick manual configuration of the [[CT]] network interface. Typically, you would configure the network settings in /etc/network/interfaces (Debian, see below) or however it is normally configured on your distribution. You can also comment or remove the configuration for venet0, if it exists, because that device will not be used.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# vzctl enter 101&lt;br /&gt;
[ve-101]# /sbin/ifconfig eth0 0&lt;br /&gt;
[ve-101]# /sbin/ip addr add 192.168.0.101 dev eth0&lt;br /&gt;
[ve-101]# /sbin/ip route add default dev eth0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
* Until you ifconfig eth0 it won't appear. When you do it will use the mac address netif_add added earlier&lt;br /&gt;
* 192.168.0.101 is chosen to be an [[unrouteable private ip address]]. Where 101 reminds you that it is node 101.&lt;br /&gt;
* The &amp;quot;ip route&amp;quot; tells all traffic to head to &amp;quot;device eth0&amp;quot;&lt;br /&gt;
* In theory you could [[use dhcpd with OpenVZ]] and dhclient to pick up an DHCP address from your router instead of hardwiring it&lt;br /&gt;
** http://openvz.org/pipermail/users/2005-November/000020.html&lt;br /&gt;
&lt;br /&gt;
==== Add route in [[CT0]] ====&lt;br /&gt;
Since [[CT0]] is acting as a router between its physical network interface and the virtual network interface of the [[CT]], we need to add a route to the [[CT]] to direct traffic to the right destination.&lt;br /&gt;
 [host-node]# ip route add 192.168.0.101 dev veth101.0&lt;br /&gt;
&lt;br /&gt;
=== Using a directly routed IPv4 with virtual Ethernet device ===&lt;br /&gt;
&lt;br /&gt;
==== Situation ====&lt;br /&gt;
Hardware Node (HN/CT0) has 192.168.0.1/24 with router 192.168.0.254.&lt;br /&gt;
&lt;br /&gt;
We also know that IPv4 10.0.0.1/32 is directly routed to 192.168.0.1 (this is called a ''fail-over IP'').&lt;br /&gt;
&lt;br /&gt;
We want to give this directly routed IPv4 address to a container (CT).&lt;br /&gt;
&lt;br /&gt;
==== Start container ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# vzctl start 101&lt;br /&gt;
&lt;br /&gt;
==== Add veth device to CT ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# vzctl set 101 --netif_add eth0 --save&lt;br /&gt;
&lt;br /&gt;
This allocates a MAC address and associates it with the host eth0 port.&lt;br /&gt;
&lt;br /&gt;
==== Configure device and add route in CT0 ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# ifconfig veth101.0 0&lt;br /&gt;
[host-node]# ip route add 10.0.0.1 dev veth101.0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can automatize this at VPS creation by using a mount script &amp;lt;tt&amp;gt;$VEID.mount&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The problem here is that the ''veth'' interface appears in CT0 '''after''' VPS has started, therefore we cannot directly use the commands in the mount script. We launch a shell script (enclosed by { }) in background (operator '''&amp;amp;''') that waits for the interface to be ready and then adds the IP route.&lt;br /&gt;
&lt;br /&gt;
Contents of the mount script &amp;lt;tt&amp;gt;/etc/vz/conf/101.mount&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
# This script source VPS configuration files in the same order as vzctl does&lt;br /&gt;
&lt;br /&gt;
# if one of these files does not exist then something is really broken&lt;br /&gt;
[ -f /etc/vz/vz.conf ] || exit 1&lt;br /&gt;
[ -f $VE_CONFFILE ] || exit 1&lt;br /&gt;
&lt;br /&gt;
# source both files. Note the order, it is important&lt;br /&gt;
. /etc/vz/vz.conf&lt;br /&gt;
. $VE_CONFFILE&lt;br /&gt;
&lt;br /&gt;
# Configure veth with IP after VPS has started&lt;br /&gt;
{&lt;br /&gt;
  IP=X.Y.Z.T&lt;br /&gt;
  DEV=veth101.0&lt;br /&gt;
  while sleep 1; do&lt;br /&gt;
    /sbin/ifconfig $DEV 0 &amp;gt;/dev/null 2&amp;gt;&amp;amp;1&lt;br /&gt;
    if [ $? -eq 0 ]; then&lt;br /&gt;
      /sbin/ip route add $IP dev $DEV&lt;br /&gt;
      break&lt;br /&gt;
    fi&lt;br /&gt;
  done&lt;br /&gt;
} &amp;amp;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Make sure IPv4 forwarding is enabled in CT0 ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/ip_forward&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/eth0/forwarding&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/veth101.0/forwarding&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
You can permanently set this by using &amp;lt;tt&amp;gt;/etc/sysctl.conf&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==== Configure device in CT ====&lt;br /&gt;
&lt;br /&gt;
1. Configure IP address&lt;br /&gt;
&lt;br /&gt;
2. Add gateway&lt;br /&gt;
&lt;br /&gt;
3. Add default route&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[ve-101]# /sbin/ifconfig eth0 10.0.0.1 netmask 255.255.255.255&lt;br /&gt;
[ve-101]# /sbin/ip route add 192.168.0.1 dev eth0&lt;br /&gt;
[ve-101]# /sbin/ip route add default via 192.168.0.1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In a Debian container, you can configure this permanently by using &amp;lt;tt&amp;gt;/etc/network/interfaces&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
auto eth0&lt;br /&gt;
iface eth0 inet static&lt;br /&gt;
        address 10.0.0.1&lt;br /&gt;
        netmask 255.255.255.255&lt;br /&gt;
        up /sbin/ip route add 192.168.0.1 dev eth0&lt;br /&gt;
        up /sbin/ip route add default via 192.168.0.1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Virtual Ethernet device with IPv6 ===&lt;br /&gt;
&lt;br /&gt;
See the [[VEs and HNs in same subnets]] article.&lt;br /&gt;
&lt;br /&gt;
=== Virtual Ethernet devices can be joined in one bridge ===&lt;br /&gt;
Bridging a [[CT]] interface to a [[CT0]] interface is the magic that allows the [[CT]] to be an independent host on the network with its own IP address, gateway, etc. [[CT0]] does not need any configuration for forwarding packets to the [[CT]] or performing proxy arp for the [[CT]].&lt;br /&gt;
&lt;br /&gt;
To manually configure a bridge and add devices to it, perform steps 1 - 4 from Simple configuration chapter for several containers and/or veth devices and then follow these steps.&lt;br /&gt;
&lt;br /&gt;
==== Create bridge device ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# brctl addbr vzbr0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Add veth devices to bridge ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# brctl addif vzbr0 veth101.0&lt;br /&gt;
...&lt;br /&gt;
[host-node]# brctl addif vzbr0 veth101.n&lt;br /&gt;
[host-node]# brctl addif vzbr0 veth102.0&lt;br /&gt;
...&lt;br /&gt;
...&lt;br /&gt;
[host-node]# brctl addif vzbr0 vethXXX.N&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Configure bridge device ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# ifconfig vzbr0 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Add routes in [[CT0]] ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# ip route add 192.168.101.1 dev vzbr0&lt;br /&gt;
...&lt;br /&gt;
[host-node]# ip route add 192.168.101.n dev vzbr0&lt;br /&gt;
[host-node]# ip route add 192.168.102.1 dev vzbr0&lt;br /&gt;
...&lt;br /&gt;
...&lt;br /&gt;
[host-node]# ip route add 192.168.XXX.N dev vzbr0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All routes to containers will be through this bridge and containers can communicate with each other even without these routes.&lt;br /&gt;
&lt;br /&gt;
=== Automating the bridge ===&lt;br /&gt;
The most convenient method is to automatically create the bridge at boot as a network interface, add the physical interface from [[CT0]] and then add the interface from each [[CT]] as it starts. All devices are connected to a virtual switch, and containers directly access the network just as any other host without additional configuration on [[CT0]].&lt;br /&gt;
&lt;br /&gt;
In Debian, configure the network interface on [[CT0]] to plug into a bridge in /etc/network/interfaces. The [[CT0]] physical device is added to the bridge as the &amp;quot;uplink&amp;quot; port to the physical network.&lt;br /&gt;
&lt;br /&gt;
The bridge forwarding delay is set to 0 seconds so that forwarding begins immediately when a new interface is added to a bridge. The default delay is 30 seconds, during which the bridge pauses all traffic to listen and figure out where devices are. This can interrupt services when a container is added to the bridge. If you aren't running the spanning tree protocol (off by default) and the bridge does not create a loop in your network, then there is no need for a forwarding delay.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
iface eth0 inet manual&lt;br /&gt;
&lt;br /&gt;
auto vzbr0&lt;br /&gt;
iface vzbr0 inet static&lt;br /&gt;
        bridge_ports eth0&lt;br /&gt;
        bridge_fd 0&lt;br /&gt;
        address 192.168.1.100&lt;br /&gt;
        netmask 255.255.255.0&lt;br /&gt;
        gateway 192.168.1.254&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Follow the steps below for making a veth bridge persistent with the included script. That will automatically add each container to the bridge when it is started. Finally, specify vzbr0 as the bridge when adding the network interface to a container, as describe above. No configuration is needed on [[CT0]] for forwarding packets, proxy arp or additional routes. The interface in each [[CT]] can be configured as desired. Everything &amp;quot;just works&amp;quot; according to normal network interface configuration and default routing rules. Note that as discussed in the troubleshooting section below, bridged packets pass through the FORWARD iptables chain. Take care when adding rules to that table that bridged packets are not mistakenly blocked.&lt;br /&gt;
&lt;br /&gt;
=== Making a veth-device persistent ===&lt;br /&gt;
These steps are no longer necessary, as the veth device is automatically created when the container is started. They remain here as a reference.&lt;br /&gt;
&lt;br /&gt;
According to http://bugzilla.openvz.org/show_bug.cgi?id=301 , a bug that stopped the veth device persistent was &amp;quot;Obsoleted now when --veth_add/--veth_del are introduced&amp;quot;&lt;br /&gt;
&lt;br /&gt;
See http://wiki.openvz.org/w/index.php?title=Virtual_Ethernet_device&amp;amp;diff=5990&amp;amp;oldid=5989#Making_a_veth-device_persistent for a workaround that used to be described in this section.&lt;br /&gt;
&lt;br /&gt;
That's it! At this point, when you restart the CT you should see a new line in the output, indicating that the interface is being configured and a new route being added. And you should be able to ping the host, and to enter the CT and use the network.&lt;br /&gt;
&lt;br /&gt;
=== Making a bridged veth-device persistent ===&lt;br /&gt;
&lt;br /&gt;
Like the above example, here it is how to add the veth device to a bridge in a persistent way. &lt;br /&gt;
&lt;br /&gt;
vzctl includes a 'vznetaddbr' script, which makes use of the ''bridge'' parameter of the --netif_add switch.&lt;br /&gt;
&lt;br /&gt;
Just create /etc/vz/vznet.conf containing the following.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
EXTERNAL_SCRIPT=&amp;quot;/usr/sbin/vznetaddbr&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The script uses 'vmbr0' as default bridge name when no bridge is specified.&lt;br /&gt;
&lt;br /&gt;
=== Virtual Ethernet devices + VLAN ===&lt;br /&gt;
This configuration can be done by adding vlan device to the previous configuration.&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
* [[Virtual network device]]&lt;br /&gt;
* [[Differences between venet and veth]]&lt;br /&gt;
* [[Using private IPs for Hardware Nodes]]&lt;br /&gt;
* Patch: [[Disable venet interface]]&lt;br /&gt;
* Troubleshooting: [[Bridge doesn't forward packets]]&lt;br /&gt;
&lt;br /&gt;
== External links ==&lt;br /&gt;
* [http://www.tldp.org/HOWTO/Linux+IPv6-HOWTO/hints-daemons-radvd.html Linux IPv6 HOWTO, a chapter about radvd]&lt;br /&gt;
* [http://sysadmin-ivanov.blogspot.com/2008/02/2-veth-with-2-bridges-on-openvz-at.html 2 veth with 2 bridges setup]&lt;br /&gt;
&lt;br /&gt;
[[Category: Networking]]&lt;br /&gt;
[[Category: HOWTO]]&lt;/div&gt;</summary>
		<author><name>Mose</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Virtual_Ethernet_device&amp;diff=11986</id>
		<title>Virtual Ethernet device</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Virtual_Ethernet_device&amp;diff=11986"/>
		<updated>2012-02-16T19:48:55Z</updated>

		<summary type="html">&lt;p&gt;Mose: /* Simple configuration with virtual Ethernet device */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Virtual Ethernet device''' is an Ethernet-like device that can be used&lt;br /&gt;
inside a [[container]]. Unlike a [[venet]] network device, a [[veth]] device&lt;br /&gt;
has a MAC address. Therefore, it can be used in more configurations. When veth&lt;br /&gt;
is bridged to a [[CT0]] network interface (e.g., eth0), the container can act as an&lt;br /&gt;
independent host on the network. The container's user can set up all of the networking&lt;br /&gt;
himself, including IPs, gateways, etc.&lt;br /&gt;
&lt;br /&gt;
A virtual Ethernet device consist of two Ethernet devices,&lt;br /&gt;
one in [[CT0]] (e.g., vethN.0) and a corresponding one in CT (e.g., eth0) that are&lt;br /&gt;
connected to each other. If a packet is sent to one device it will come out the other device.&lt;br /&gt;
&lt;br /&gt;
== Virtual Ethernet device usage ==&lt;br /&gt;
&lt;br /&gt;
=== Kernel module ===&lt;br /&gt;
The &amp;lt;code&amp;gt;vzethdev&amp;lt;/code&amp;gt; module should be loaded. You can check it with the following commands.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# lsmod | grep vzeth&lt;br /&gt;
vzethdev                8224  0&lt;br /&gt;
vzmon                  35164  5 vzethdev,vznetdev,vzrst,vzcpt&lt;br /&gt;
vzdev                   3080  4 vzethdev,vznetdev,vzmon,vzdquota&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In case it is not loaded, load it:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# modprobe vzethdev&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== MAC addresses ===&lt;br /&gt;
The following steps to generate a MAC address are not necessary, since newer versions&lt;br /&gt;
of vzctl will automatically generate a MAC address for you. These steps are provided&lt;br /&gt;
in case you want to set a MAC address manually.&lt;br /&gt;
&lt;br /&gt;
You should use a random MAC address when adding a network interface to a container. Do not use MAC addresses of real eth devices, because this can lead to collisions.&lt;br /&gt;
&lt;br /&gt;
MAC addresses must be entered in XX:XX:XX:XX:XX:XX format.&lt;br /&gt;
&lt;br /&gt;
There is a utility script available for generating MAC addresses: http://www.easyvmx.com/software/easymac.sh. It is used like this:&lt;br /&gt;
&lt;br /&gt;
 chmod +x easymac.sh&lt;br /&gt;
 ./easymac.sh -R&lt;br /&gt;
&lt;br /&gt;
=== Adding veth to a CT ===&lt;br /&gt;
&lt;br /&gt;
 vzctl set &amp;lt;CTID&amp;gt; --netif_add &amp;lt;ifname&amp;gt;[,&amp;lt;mac&amp;gt;,&amp;lt;host_ifname&amp;gt;,&amp;lt;host_mac&amp;gt;,&amp;lt;bridge&amp;gt;]&lt;br /&gt;
&lt;br /&gt;
Here&lt;br /&gt;
* &amp;lt;tt&amp;gt;ifname&amp;lt;/tt&amp;gt; is the Ethernet device name in the CT&lt;br /&gt;
* &amp;lt;tt&amp;gt;mac&amp;lt;/tt&amp;gt; is its MAC address in the CT&lt;br /&gt;
* &amp;lt;tt&amp;gt;host_ifname&amp;lt;/tt&amp;gt;  is the Ethernet device name on the host ([[CT0]])&lt;br /&gt;
* &amp;lt;tt&amp;gt;host_mac&amp;lt;/tt&amp;gt; is its MAC address on the host ([[CT0]])&lt;br /&gt;
* &amp;lt;tt&amp;gt;bridge&amp;lt;/tt&amp;gt; is an optional parameter which can be used in custom network start scripts to automatically add  the interface to a bridge. (See the reference to the vznetaddbr script below and persistent bridge configurations.)&lt;br /&gt;
&lt;br /&gt;
{{Note|All parameters except &amp;lt;code&amp;gt;ifname&amp;lt;/code&amp;gt; are optional. Missing parameters, except for bridge, are automatically generated, if not specified. This is the preferred method.}}&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 vzctl set 101 --netif_add eth0 --save&lt;br /&gt;
&lt;br /&gt;
If you want to specify everything:&lt;br /&gt;
&lt;br /&gt;
 vzctl set 101 --netif_add eth0,00:12:34:56:78:9A,veth101.0,00:12:34:56:78:9B --save&lt;br /&gt;
&lt;br /&gt;
If you want to specify the bridge and autogenerate the other values:&lt;br /&gt;
&lt;br /&gt;
 vzctl set 101 --netif_add eth0,,,,vzbr0 --save&lt;br /&gt;
&lt;br /&gt;
=== Removing veth from a CT ===&lt;br /&gt;
&lt;br /&gt;
 vzctl set &amp;lt;CTID&amp;gt; --netif_del &amp;lt;dev_name&amp;gt;|all&lt;br /&gt;
&lt;br /&gt;
Here&lt;br /&gt;
* &amp;lt;code&amp;gt;dev_name&amp;lt;/code&amp;gt; is the Ethernet device name in the [[CT]].&lt;br /&gt;
&lt;br /&gt;
{{Note|If you want to remove all Ethernet devices in CT, use &amp;lt;code&amp;gt;all&amp;lt;/code&amp;gt;.}}&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 vzctl set 101 --netif_del eth0 --save&lt;br /&gt;
&lt;br /&gt;
== Common configurations with virtual Ethernet devices ==&lt;br /&gt;
Module &amp;lt;tt&amp;gt;vzethdev&amp;lt;/tt&amp;gt; must be loaded to operate with veth devices.&lt;br /&gt;
&lt;br /&gt;
=== Simple configuration with virtual Ethernet device ===&lt;br /&gt;
&lt;br /&gt;
Assuming that 192.168.0.0/24 is being used on your LAN, the following sections show how to configure a container for the LAN using veth.&lt;br /&gt;
&lt;br /&gt;
==== Start a CT ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# vzctl start 101&lt;br /&gt;
&lt;br /&gt;
==== Add veth device to CT ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# vzctl set 101 --netif_add eth0 --save&lt;br /&gt;
&lt;br /&gt;
This allocates a MAC address and associates it with the host eth0 port.&lt;br /&gt;
&lt;br /&gt;
==== Configure devices in CT0 ====&lt;br /&gt;
The following steps are needed when the [[CT]] is '''not''' bridged to a [[CT0]] network interface. That is because the [[CT]] is connected to a virtual network that is &amp;quot;behind&amp;quot; [[CT0]]. [[CT0]] must forward packets between its physical network interface and the virtual network interface where [[CT]] is located. The first step below to configure the interface is not necessary if the container has been started, since the device will have been initialized.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# ifconfig veth101.0 0&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/veth101.0/forwarding&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/veth101.0/proxy_arp&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/eth0/forwarding&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/eth0/proxy_arp&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Configure device in CT ====&lt;br /&gt;
The following steps show an example of a quick manual configuration of the [[CT]] network interface. Typically, you would configure the network settings in /etc/network/interfaces (Debian, see below) or however it is normally configured on your distribution. You can also comment or remove the configuration for venet0, if it exists, because that device will not be used.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# vzctl enter 101&lt;br /&gt;
[ve-101]# /sbin/ifconfig eth0 0&lt;br /&gt;
[ve-101]# /sbin/ip addr add 192.168.0.101 dev eth0&lt;br /&gt;
[ve-101]# /sbin/ip route add default dev eth0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
* Until you ifconfig eth0 it won't appear. When you do it will use the mac address netif_add added earlier&lt;br /&gt;
* 192.168.0.101 is chosen to be an [[unrouteable private ip address]]. Where 101 reminds you that it is node 101.&lt;br /&gt;
* The &amp;quot;ip route&amp;quot; tells all traffic to head to &amp;quot;device eth0&amp;quot;&lt;br /&gt;
* In theory you could [[use dhcpd with OpenVZ]] and dhclient to pick up an DHCP address from your router instead of hardwiring it&lt;br /&gt;
** http://openvz.org/pipermail/users/2005-November/000020.html&lt;br /&gt;
&lt;br /&gt;
==== Add route in [[CT0]] ====&lt;br /&gt;
Since [[CT0]] is acting as a router between its physical network interface and the virtual network interface of the [[CT]], we need to add a route to the [[CT]] to direct traffic to the right destination.&lt;br /&gt;
 [host-node]# ip route add 192.168.0.101 dev veth101.0&lt;br /&gt;
&lt;br /&gt;
=== Using a directly routed IPv4 with virtual Ethernet device ===&lt;br /&gt;
&lt;br /&gt;
==== Situation ====&lt;br /&gt;
Hardware Node (HN/CT0) has 192.168.0.1/24 with router 192.168.0.254.&lt;br /&gt;
&lt;br /&gt;
We also know that IPv4 10.0.0.1/32 is directly routed to 192.168.0.1 (this is called a ''fail-over IP'').&lt;br /&gt;
&lt;br /&gt;
We want to give this directly routed IPv4 address to a container (CT).&lt;br /&gt;
&lt;br /&gt;
==== Start container ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# vzctl start 101&lt;br /&gt;
&lt;br /&gt;
==== Add veth device to CT ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# vzctl set 101 --netif_add eth0 --save&lt;br /&gt;
&lt;br /&gt;
This allocates a MAC address and associates it with the host eth0 port.&lt;br /&gt;
&lt;br /&gt;
==== Configure device and add route in CT0 ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# ifconfig veth101.0 0&lt;br /&gt;
[host-node]# ip route add 10.0.0.1 dev veth101.0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can automatize this at VPS creation by using a mount script &amp;lt;tt&amp;gt;$VEID.mount&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The problem here is that the ''veth'' interface appears in CT0 '''after''' VPS has started, therefore we cannot directly use the commands in the mount script. We launch a shell script (enclosed by { }) in background (operator '''&amp;amp;''') that waits for the interface to be ready and then adds the IP route.&lt;br /&gt;
&lt;br /&gt;
Contents of the mount script &amp;lt;tt&amp;gt;/etc/vz/conf/101.mount&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
# This script source VPS configuration files in the same order as vzctl does&lt;br /&gt;
&lt;br /&gt;
# if one of these files does not exist then something is really broken&lt;br /&gt;
[ -f /etc/vz/vz.conf ] || exit 1&lt;br /&gt;
[ -f $VE_CONFFILE ] || exit 1&lt;br /&gt;
&lt;br /&gt;
# source both files. Note the order, it is important&lt;br /&gt;
. /etc/vz/vz.conf&lt;br /&gt;
. $VE_CONFFILE&lt;br /&gt;
&lt;br /&gt;
# Configure veth with IP after VPS has started&lt;br /&gt;
{&lt;br /&gt;
  IP=X.Y.Z.T&lt;br /&gt;
  DEV=veth101.0&lt;br /&gt;
  while sleep 1; do&lt;br /&gt;
    /sbin/ifconfig $DEV 0 &amp;gt;/dev/null 2&amp;gt;&amp;amp;1&lt;br /&gt;
    if [ $? -eq 0 ]; then&lt;br /&gt;
      /sbin/ip route add $IP dev $DEV&lt;br /&gt;
      break&lt;br /&gt;
    fi&lt;br /&gt;
  done&lt;br /&gt;
} &amp;amp;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Make sure IPv4 forwarding is enabled in CT0 ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/ip_forward&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/eth0/forwarding&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/veth101.0/forwarding&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
You can permanently set this by using &amp;lt;tt&amp;gt;/etc/sysctl.conf&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==== Configure device in CT ====&lt;br /&gt;
&lt;br /&gt;
1. Configure IP address&lt;br /&gt;
&lt;br /&gt;
2. Add gateway&lt;br /&gt;
&lt;br /&gt;
3. Add default route&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[ve-101]# /sbin/ifconfig eth0 10.0.0.1 netmask 255.255.255.255&lt;br /&gt;
[ve-101]# /sbin/ip route add 192.168.0.1 dev eth0&lt;br /&gt;
[ve-101]# /sbin/ip route add default via 192.168.0.1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In a Debian container, you can configure this permanently by using &amp;lt;tt&amp;gt;/etc/network/interfaces&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
auto eth0&lt;br /&gt;
iface eth0 inet static&lt;br /&gt;
        address 10.0.0.1&lt;br /&gt;
        netmask 255.255.255.255&lt;br /&gt;
        up /sbin/ip route add 192.168.0.1 dev eth0&lt;br /&gt;
        up /sbin/ip route add default via 192.168.0.1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Virtual Ethernet device with IPv6 ===&lt;br /&gt;
&lt;br /&gt;
See the [[VEs and HNs in same subnets]] article.&lt;br /&gt;
&lt;br /&gt;
=== Virtual Ethernet devices can be joined in one bridge ===&lt;br /&gt;
Bridging a [[CT]] interface to a [[CT0]] interface is the magic that allows the [[CT]] to be an independent host on the network with its own IP address, gateway, etc. [[CT0]] does not need any configuration for forwarding packets to the [[CT]] or performing proxy arp for the [[CT]].&lt;br /&gt;
&lt;br /&gt;
To manually configure a bridge and add devices to it, perform steps 1 - 4 from Simple configuration chapter for several containers and/or veth devices and then follow these steps.&lt;br /&gt;
&lt;br /&gt;
==== Create bridge device ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# brctl addbr vzbr0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Add veth devices to bridge ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# brctl addif vzbr0 veth101.0&lt;br /&gt;
...&lt;br /&gt;
[host-node]# brctl addif vzbr0 veth101.n&lt;br /&gt;
[host-node]# brctl addif vzbr0 veth102.0&lt;br /&gt;
...&lt;br /&gt;
...&lt;br /&gt;
[host-node]# brctl addif vzbr0 vethXXX.N&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Configure bridge device ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# ifconfig vzbr0 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Add routes in [[CT0]] ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# ip route add 192.168.101.1 dev vzbr0&lt;br /&gt;
...&lt;br /&gt;
[host-node]# ip route add 192.168.101.n dev vzbr0&lt;br /&gt;
[host-node]# ip route add 192.168.102.1 dev vzbr0&lt;br /&gt;
...&lt;br /&gt;
...&lt;br /&gt;
[host-node]# ip route add 192.168.XXX.N dev vzbr0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All routes to containers will be through this bridge and containers can communicate with each other even without these routes.&lt;br /&gt;
&lt;br /&gt;
=== Automating the bridge ===&lt;br /&gt;
The most convenient method is to automatically create the bridge at boot as a network interface, add the physical interface from [[CT0]] and then add the interface from each [[CT]] as it starts. All devices are connected to a virtual switch, and containers directly access the network just as any other host without additional configuration on [[CT0]].&lt;br /&gt;
&lt;br /&gt;
In Debian, configure the network interface on [[CT0]] to be a bridge in /etc/network/interfaces.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
iface eth0 inet manual&lt;br /&gt;
&lt;br /&gt;
auto vzbr0&lt;br /&gt;
iface vzbr0 inet static&lt;br /&gt;
        bridge-ports eth0&lt;br /&gt;
        address 192.168.1.100&lt;br /&gt;
        netmask 255.255.255.0&lt;br /&gt;
        network 192.168.1.0&lt;br /&gt;
        broadcast 192.168.1.255&lt;br /&gt;
        gateway 192.168.1.254&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Follow the steps below for making a veth bridge persistent with the included script. That will automatically add each container to the bridge when it is started. Finally, specify vzbr0 as the bridge when adding the network interface to a container, as describe above. No configuration is needed on [[CT0]] for forwarding packets, proxy arp or additional routes. The interface in each [[CT]] can be configured as desired. Everything &amp;quot;just works&amp;quot; according to normal network interface configuration and default routing rules.&lt;br /&gt;
&lt;br /&gt;
=== Making a veth-device persistent ===&lt;br /&gt;
These steps are no longer necessary, as the veth device is automatically created when the container is started. They remain here as a reference.&lt;br /&gt;
&lt;br /&gt;
According to http://bugzilla.openvz.org/show_bug.cgi?id=301 , a bug that stopped the veth device persistent was &amp;quot;Obsoleted now when --veth_add/--veth_del are introduced&amp;quot;&lt;br /&gt;
&lt;br /&gt;
See http://wiki.openvz.org/w/index.php?title=Virtual_Ethernet_device&amp;amp;diff=5990&amp;amp;oldid=5989#Making_a_veth-device_persistent for a workaround that used to be described in this section.&lt;br /&gt;
&lt;br /&gt;
That's it! At this point, when you restart the CT you should see a new line in the output, indicating that the interface is being configured and a new route being added. And you should be able to ping the host, and to enter the CT and use the network.&lt;br /&gt;
&lt;br /&gt;
=== Making a bridged veth-device persistent ===&lt;br /&gt;
&lt;br /&gt;
Like the above example, here it is how to add the veth device to a bridge in a persistent way. &lt;br /&gt;
&lt;br /&gt;
vzctl includes a 'vznetaddbr' script, which makes use of the ''bridge'' parameter of the --netif_add switch.&lt;br /&gt;
&lt;br /&gt;
Just create /etc/vz/vznet.conf containing the following.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
EXTERNAL_SCRIPT=&amp;quot;/usr/sbin/vznetaddbr&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The script uses 'vmbr0' as default bridge name when no bridge is specified.&lt;br /&gt;
&lt;br /&gt;
=== Virtual Ethernet devices + VLAN ===&lt;br /&gt;
This configuration can be done by adding vlan device to the previous configuration.&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
* [[Virtual network device]]&lt;br /&gt;
* [[Differences between venet and veth]]&lt;br /&gt;
* [[Using private IPs for Hardware Nodes]]&lt;br /&gt;
* Patch: [[Disable venet interface]]&lt;br /&gt;
* Troubleshooting: [[Bridge doesn't forward packets]]&lt;br /&gt;
&lt;br /&gt;
== External links ==&lt;br /&gt;
* [http://www.tldp.org/HOWTO/Linux+IPv6-HOWTO/hints-daemons-radvd.html Linux IPv6 HOWTO, a chapter about radvd]&lt;br /&gt;
* [http://sysadmin-ivanov.blogspot.com/2008/02/2-veth-with-2-bridges-on-openvz-at.html 2 veth with 2 bridges setup]&lt;br /&gt;
&lt;br /&gt;
[[Category: Networking]]&lt;br /&gt;
[[Category: HOWTO]]&lt;/div&gt;</summary>
		<author><name>Mose</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Virtual_Ethernet_device&amp;diff=11985</id>
		<title>Virtual Ethernet device</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Virtual_Ethernet_device&amp;diff=11985"/>
		<updated>2012-02-16T19:46:19Z</updated>

		<summary type="html">&lt;p&gt;Mose: /* Adding veth to a CT */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Virtual Ethernet device''' is an Ethernet-like device that can be used&lt;br /&gt;
inside a [[container]]. Unlike a [[venet]] network device, a [[veth]] device&lt;br /&gt;
has a MAC address. Therefore, it can be used in more configurations. When veth&lt;br /&gt;
is bridged to a [[CT0]] network interface (e.g., eth0), the container can act as an&lt;br /&gt;
independent host on the network. The container's user can set up all of the networking&lt;br /&gt;
himself, including IPs, gateways, etc.&lt;br /&gt;
&lt;br /&gt;
A virtual Ethernet device consist of two Ethernet devices,&lt;br /&gt;
one in [[CT0]] (e.g., vethN.0) and a corresponding one in CT (e.g., eth0) that are&lt;br /&gt;
connected to each other. If a packet is sent to one device it will come out the other device.&lt;br /&gt;
&lt;br /&gt;
== Virtual Ethernet device usage ==&lt;br /&gt;
&lt;br /&gt;
=== Kernel module ===&lt;br /&gt;
The &amp;lt;code&amp;gt;vzethdev&amp;lt;/code&amp;gt; module should be loaded. You can check it with the following commands.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# lsmod | grep vzeth&lt;br /&gt;
vzethdev                8224  0&lt;br /&gt;
vzmon                  35164  5 vzethdev,vznetdev,vzrst,vzcpt&lt;br /&gt;
vzdev                   3080  4 vzethdev,vznetdev,vzmon,vzdquota&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In case it is not loaded, load it:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# modprobe vzethdev&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== MAC addresses ===&lt;br /&gt;
The following steps to generate a MAC address are not necessary, since newer versions&lt;br /&gt;
of vzctl will automatically generate a MAC address for you. These steps are provided&lt;br /&gt;
in case you want to set a MAC address manually.&lt;br /&gt;
&lt;br /&gt;
You should use a random MAC address when adding a network interface to a container. Do not use MAC addresses of real eth devices, because this can lead to collisions.&lt;br /&gt;
&lt;br /&gt;
MAC addresses must be entered in XX:XX:XX:XX:XX:XX format.&lt;br /&gt;
&lt;br /&gt;
There is a utility script available for generating MAC addresses: http://www.easyvmx.com/software/easymac.sh. It is used like this:&lt;br /&gt;
&lt;br /&gt;
 chmod +x easymac.sh&lt;br /&gt;
 ./easymac.sh -R&lt;br /&gt;
&lt;br /&gt;
=== Adding veth to a CT ===&lt;br /&gt;
&lt;br /&gt;
 vzctl set &amp;lt;CTID&amp;gt; --netif_add &amp;lt;ifname&amp;gt;[,&amp;lt;mac&amp;gt;,&amp;lt;host_ifname&amp;gt;,&amp;lt;host_mac&amp;gt;,&amp;lt;bridge&amp;gt;]&lt;br /&gt;
&lt;br /&gt;
Here&lt;br /&gt;
* &amp;lt;tt&amp;gt;ifname&amp;lt;/tt&amp;gt; is the Ethernet device name in the CT&lt;br /&gt;
* &amp;lt;tt&amp;gt;mac&amp;lt;/tt&amp;gt; is its MAC address in the CT&lt;br /&gt;
* &amp;lt;tt&amp;gt;host_ifname&amp;lt;/tt&amp;gt;  is the Ethernet device name on the host ([[CT0]])&lt;br /&gt;
* &amp;lt;tt&amp;gt;host_mac&amp;lt;/tt&amp;gt; is its MAC address on the host ([[CT0]])&lt;br /&gt;
* &amp;lt;tt&amp;gt;bridge&amp;lt;/tt&amp;gt; is an optional parameter which can be used in custom network start scripts to automatically add  the interface to a bridge. (See the reference to the vznetaddbr script below and persistent bridge configurations.)&lt;br /&gt;
&lt;br /&gt;
{{Note|All parameters except &amp;lt;code&amp;gt;ifname&amp;lt;/code&amp;gt; are optional. Missing parameters, except for bridge, are automatically generated, if not specified. This is the preferred method.}}&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 vzctl set 101 --netif_add eth0 --save&lt;br /&gt;
&lt;br /&gt;
If you want to specify everything:&lt;br /&gt;
&lt;br /&gt;
 vzctl set 101 --netif_add eth0,00:12:34:56:78:9A,veth101.0,00:12:34:56:78:9B --save&lt;br /&gt;
&lt;br /&gt;
If you want to specify the bridge and autogenerate the other values:&lt;br /&gt;
&lt;br /&gt;
 vzctl set 101 --netif_add eth0,,,,vzbr0 --save&lt;br /&gt;
&lt;br /&gt;
=== Removing veth from a CT ===&lt;br /&gt;
&lt;br /&gt;
 vzctl set &amp;lt;CTID&amp;gt; --netif_del &amp;lt;dev_name&amp;gt;|all&lt;br /&gt;
&lt;br /&gt;
Here&lt;br /&gt;
* &amp;lt;code&amp;gt;dev_name&amp;lt;/code&amp;gt; is the Ethernet device name in the [[CT]].&lt;br /&gt;
&lt;br /&gt;
{{Note|If you want to remove all Ethernet devices in CT, use &amp;lt;code&amp;gt;all&amp;lt;/code&amp;gt;.}}&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 vzctl set 101 --netif_del eth0 --save&lt;br /&gt;
&lt;br /&gt;
== Common configurations with virtual Ethernet devices ==&lt;br /&gt;
Module &amp;lt;tt&amp;gt;vzethdev&amp;lt;/tt&amp;gt; must be loaded to operate with veth devices.&lt;br /&gt;
&lt;br /&gt;
=== Simple configuration with virtual Ethernet device ===&lt;br /&gt;
&lt;br /&gt;
Assuming you have 192.168.0.0/24 on your LAN, you will learn how to integrate a container in this LAN using veth.&lt;br /&gt;
&lt;br /&gt;
==== Start a CT ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# vzctl start 101&lt;br /&gt;
&lt;br /&gt;
==== Add veth device to CT ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# vzctl set 101 --netif_add eth0 --save&lt;br /&gt;
&lt;br /&gt;
This allocates a MAC address and associates it with the host eth0 port.&lt;br /&gt;
&lt;br /&gt;
==== Configure devices in CT0 ====&lt;br /&gt;
The following steps are needed when the [[CT]] is '''not''' bridged to a [[CT0]] network interface. That is because the [[CT]] is connected to a virtual network that is &amp;quot;behind&amp;quot; [[CT0]]. [[CT0]] must forward packets between its physical network interface and the virtual network interface where [[CT]] is located. The first step below to configure the interface is not necessary if the container has been started, since the device will have been initialized.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# ifconfig veth101.0 0&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/veth101.0/forwarding&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/veth101.0/proxy_arp&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/eth0/forwarding&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/eth0/proxy_arp&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Configure device in CT ====&lt;br /&gt;
The following steps show an example of a quick manual configuration of the [[CT]] network interface. Typically, you would configure the network settings in /etc/network/interfaces (Debian, see below) or however it is normally configured on your distribution. You can also comment or remove the configuration for venet0, if it exists, because that device will not be used.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# vzctl enter 101&lt;br /&gt;
[ve-101]# /sbin/ifconfig eth0 0&lt;br /&gt;
[ve-101]# /sbin/ip addr add 192.168.0.101 dev eth0&lt;br /&gt;
[ve-101]# /sbin/ip route add default dev eth0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
* Until you ifconfig eth0 it won't appear. When you do it will use the mac address netif_add added earlier&lt;br /&gt;
* 192.168.0.101 is chosen to be an [[unrouteable private ip address]]. Where 101 reminds you that it is node 101.&lt;br /&gt;
* The &amp;quot;ip route&amp;quot; tells all traffic to head to &amp;quot;device eth0&amp;quot;&lt;br /&gt;
* In theory you could [[use dhcpd with OpenVZ]] and dhclient to pick up an DHCP address from your router instead of hardwiring it&lt;br /&gt;
** http://openvz.org/pipermail/users/2005-November/000020.html&lt;br /&gt;
&lt;br /&gt;
==== Add route in [[CT0]] ====&lt;br /&gt;
Since [[CT0]] is acting as a router between its physical network interface and the virtual network interface of the [[CT]], we need to add a route to the [[CT]] to direct traffic to the right destination.&lt;br /&gt;
 [host-node]# ip route add 192.168.0.101 dev veth101.0&lt;br /&gt;
&lt;br /&gt;
=== Using a directly routed IPv4 with virtual Ethernet device ===&lt;br /&gt;
&lt;br /&gt;
==== Situation ====&lt;br /&gt;
Hardware Node (HN/CT0) has 192.168.0.1/24 with router 192.168.0.254.&lt;br /&gt;
&lt;br /&gt;
We also know that IPv4 10.0.0.1/32 is directly routed to 192.168.0.1 (this is called a ''fail-over IP'').&lt;br /&gt;
&lt;br /&gt;
We want to give this directly routed IPv4 address to a container (CT).&lt;br /&gt;
&lt;br /&gt;
==== Start container ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# vzctl start 101&lt;br /&gt;
&lt;br /&gt;
==== Add veth device to CT ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# vzctl set 101 --netif_add eth0 --save&lt;br /&gt;
&lt;br /&gt;
This allocates a MAC address and associates it with the host eth0 port.&lt;br /&gt;
&lt;br /&gt;
==== Configure device and add route in CT0 ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# ifconfig veth101.0 0&lt;br /&gt;
[host-node]# ip route add 10.0.0.1 dev veth101.0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can automatize this at VPS creation by using a mount script &amp;lt;tt&amp;gt;$VEID.mount&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The problem here is that the ''veth'' interface appears in CT0 '''after''' VPS has started, therefore we cannot directly use the commands in the mount script. We launch a shell script (enclosed by { }) in background (operator '''&amp;amp;''') that waits for the interface to be ready and then adds the IP route.&lt;br /&gt;
&lt;br /&gt;
Contents of the mount script &amp;lt;tt&amp;gt;/etc/vz/conf/101.mount&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
# This script source VPS configuration files in the same order as vzctl does&lt;br /&gt;
&lt;br /&gt;
# if one of these files does not exist then something is really broken&lt;br /&gt;
[ -f /etc/vz/vz.conf ] || exit 1&lt;br /&gt;
[ -f $VE_CONFFILE ] || exit 1&lt;br /&gt;
&lt;br /&gt;
# source both files. Note the order, it is important&lt;br /&gt;
. /etc/vz/vz.conf&lt;br /&gt;
. $VE_CONFFILE&lt;br /&gt;
&lt;br /&gt;
# Configure veth with IP after VPS has started&lt;br /&gt;
{&lt;br /&gt;
  IP=X.Y.Z.T&lt;br /&gt;
  DEV=veth101.0&lt;br /&gt;
  while sleep 1; do&lt;br /&gt;
    /sbin/ifconfig $DEV 0 &amp;gt;/dev/null 2&amp;gt;&amp;amp;1&lt;br /&gt;
    if [ $? -eq 0 ]; then&lt;br /&gt;
      /sbin/ip route add $IP dev $DEV&lt;br /&gt;
      break&lt;br /&gt;
    fi&lt;br /&gt;
  done&lt;br /&gt;
} &amp;amp;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Make sure IPv4 forwarding is enabled in CT0 ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/ip_forward&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/eth0/forwarding&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/veth101.0/forwarding&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
You can permanently set this by using &amp;lt;tt&amp;gt;/etc/sysctl.conf&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==== Configure device in CT ====&lt;br /&gt;
&lt;br /&gt;
1. Configure IP address&lt;br /&gt;
&lt;br /&gt;
2. Add gateway&lt;br /&gt;
&lt;br /&gt;
3. Add default route&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[ve-101]# /sbin/ifconfig eth0 10.0.0.1 netmask 255.255.255.255&lt;br /&gt;
[ve-101]# /sbin/ip route add 192.168.0.1 dev eth0&lt;br /&gt;
[ve-101]# /sbin/ip route add default via 192.168.0.1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In a Debian container, you can configure this permanently by using &amp;lt;tt&amp;gt;/etc/network/interfaces&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
auto eth0&lt;br /&gt;
iface eth0 inet static&lt;br /&gt;
        address 10.0.0.1&lt;br /&gt;
        netmask 255.255.255.255&lt;br /&gt;
        up /sbin/ip route add 192.168.0.1 dev eth0&lt;br /&gt;
        up /sbin/ip route add default via 192.168.0.1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Virtual Ethernet device with IPv6 ===&lt;br /&gt;
&lt;br /&gt;
See the [[VEs and HNs in same subnets]] article.&lt;br /&gt;
&lt;br /&gt;
=== Virtual Ethernet devices can be joined in one bridge ===&lt;br /&gt;
Bridging a [[CT]] interface to a [[CT0]] interface is the magic that allows the [[CT]] to be an independent host on the network with its own IP address, gateway, etc. [[CT0]] does not need any configuration for forwarding packets to the [[CT]] or performing proxy arp for the [[CT]].&lt;br /&gt;
&lt;br /&gt;
To manually configure a bridge and add devices to it, perform steps 1 - 4 from Simple configuration chapter for several containers and/or veth devices and then follow these steps.&lt;br /&gt;
&lt;br /&gt;
==== Create bridge device ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# brctl addbr vzbr0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Add veth devices to bridge ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# brctl addif vzbr0 veth101.0&lt;br /&gt;
...&lt;br /&gt;
[host-node]# brctl addif vzbr0 veth101.n&lt;br /&gt;
[host-node]# brctl addif vzbr0 veth102.0&lt;br /&gt;
...&lt;br /&gt;
...&lt;br /&gt;
[host-node]# brctl addif vzbr0 vethXXX.N&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Configure bridge device ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# ifconfig vzbr0 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Add routes in [[CT0]] ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# ip route add 192.168.101.1 dev vzbr0&lt;br /&gt;
...&lt;br /&gt;
[host-node]# ip route add 192.168.101.n dev vzbr0&lt;br /&gt;
[host-node]# ip route add 192.168.102.1 dev vzbr0&lt;br /&gt;
...&lt;br /&gt;
...&lt;br /&gt;
[host-node]# ip route add 192.168.XXX.N dev vzbr0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All routes to containers will be through this bridge and containers can communicate with each other even without these routes.&lt;br /&gt;
&lt;br /&gt;
=== Automating the bridge ===&lt;br /&gt;
The most convenient method is to automatically create the bridge at boot as a network interface, add the physical interface from [[CT0]] and then add the interface from each [[CT]] as it starts. All devices are connected to a virtual switch, and containers directly access the network just as any other host without additional configuration on [[CT0]].&lt;br /&gt;
&lt;br /&gt;
In Debian, configure the network interface on [[CT0]] to be a bridge in /etc/network/interfaces.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
iface eth0 inet manual&lt;br /&gt;
&lt;br /&gt;
auto vzbr0&lt;br /&gt;
iface vzbr0 inet static&lt;br /&gt;
        bridge-ports eth0&lt;br /&gt;
        address 192.168.1.100&lt;br /&gt;
        netmask 255.255.255.0&lt;br /&gt;
        network 192.168.1.0&lt;br /&gt;
        broadcast 192.168.1.255&lt;br /&gt;
        gateway 192.168.1.254&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Follow the steps below for making a veth bridge persistent with the included script. That will automatically add each container to the bridge when it is started. Finally, specify vzbr0 as the bridge when adding the network interface to a container, as describe above. No configuration is needed on [[CT0]] for forwarding packets, proxy arp or additional routes. The interface in each [[CT]] can be configured as desired. Everything &amp;quot;just works&amp;quot; according to normal network interface configuration and default routing rules.&lt;br /&gt;
&lt;br /&gt;
=== Making a veth-device persistent ===&lt;br /&gt;
These steps are no longer necessary, as the veth device is automatically created when the container is started. They remain here as a reference.&lt;br /&gt;
&lt;br /&gt;
According to http://bugzilla.openvz.org/show_bug.cgi?id=301 , a bug that stopped the veth device persistent was &amp;quot;Obsoleted now when --veth_add/--veth_del are introduced&amp;quot;&lt;br /&gt;
&lt;br /&gt;
See http://wiki.openvz.org/w/index.php?title=Virtual_Ethernet_device&amp;amp;diff=5990&amp;amp;oldid=5989#Making_a_veth-device_persistent for a workaround that used to be described in this section.&lt;br /&gt;
&lt;br /&gt;
That's it! At this point, when you restart the CT you should see a new line in the output, indicating that the interface is being configured and a new route being added. And you should be able to ping the host, and to enter the CT and use the network.&lt;br /&gt;
&lt;br /&gt;
=== Making a bridged veth-device persistent ===&lt;br /&gt;
&lt;br /&gt;
Like the above example, here it is how to add the veth device to a bridge in a persistent way. &lt;br /&gt;
&lt;br /&gt;
vzctl includes a 'vznetaddbr' script, which makes use of the ''bridge'' parameter of the --netif_add switch.&lt;br /&gt;
&lt;br /&gt;
Just create /etc/vz/vznet.conf containing the following.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
EXTERNAL_SCRIPT=&amp;quot;/usr/sbin/vznetaddbr&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The script uses 'vmbr0' as default bridge name when no bridge is specified.&lt;br /&gt;
&lt;br /&gt;
=== Virtual Ethernet devices + VLAN ===&lt;br /&gt;
This configuration can be done by adding vlan device to the previous configuration.&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
* [[Virtual network device]]&lt;br /&gt;
* [[Differences between venet and veth]]&lt;br /&gt;
* [[Using private IPs for Hardware Nodes]]&lt;br /&gt;
* Patch: [[Disable venet interface]]&lt;br /&gt;
* Troubleshooting: [[Bridge doesn't forward packets]]&lt;br /&gt;
&lt;br /&gt;
== External links ==&lt;br /&gt;
* [http://www.tldp.org/HOWTO/Linux+IPv6-HOWTO/hints-daemons-radvd.html Linux IPv6 HOWTO, a chapter about radvd]&lt;br /&gt;
* [http://sysadmin-ivanov.blogspot.com/2008/02/2-veth-with-2-bridges-on-openvz-at.html 2 veth with 2 bridges setup]&lt;br /&gt;
&lt;br /&gt;
[[Category: Networking]]&lt;br /&gt;
[[Category: HOWTO]]&lt;/div&gt;</summary>
		<author><name>Mose</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Virtual_Ethernet_device&amp;diff=11984</id>
		<title>Virtual Ethernet device</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Virtual_Ethernet_device&amp;diff=11984"/>
		<updated>2012-02-16T19:44:28Z</updated>

		<summary type="html">&lt;p&gt;Mose: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Virtual Ethernet device''' is an Ethernet-like device that can be used&lt;br /&gt;
inside a [[container]]. Unlike a [[venet]] network device, a [[veth]] device&lt;br /&gt;
has a MAC address. Therefore, it can be used in more configurations. When veth&lt;br /&gt;
is bridged to a [[CT0]] network interface (e.g., eth0), the container can act as an&lt;br /&gt;
independent host on the network. The container's user can set up all of the networking&lt;br /&gt;
himself, including IPs, gateways, etc.&lt;br /&gt;
&lt;br /&gt;
A virtual Ethernet device consist of two Ethernet devices,&lt;br /&gt;
one in [[CT0]] (e.g., vethN.0) and a corresponding one in CT (e.g., eth0) that are&lt;br /&gt;
connected to each other. If a packet is sent to one device it will come out the other device.&lt;br /&gt;
&lt;br /&gt;
== Virtual Ethernet device usage ==&lt;br /&gt;
&lt;br /&gt;
=== Kernel module ===&lt;br /&gt;
The &amp;lt;code&amp;gt;vzethdev&amp;lt;/code&amp;gt; module should be loaded. You can check it with the following commands.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# lsmod | grep vzeth&lt;br /&gt;
vzethdev                8224  0&lt;br /&gt;
vzmon                  35164  5 vzethdev,vznetdev,vzrst,vzcpt&lt;br /&gt;
vzdev                   3080  4 vzethdev,vznetdev,vzmon,vzdquota&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In case it is not loaded, load it:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# modprobe vzethdev&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== MAC addresses ===&lt;br /&gt;
The following steps to generate a MAC address are not necessary, since newer versions&lt;br /&gt;
of vzctl will automatically generate a MAC address for you. These steps are provided&lt;br /&gt;
in case you want to set a MAC address manually.&lt;br /&gt;
&lt;br /&gt;
You should use a random MAC address when adding a network interface to a container. Do not use MAC addresses of real eth devices, because this can lead to collisions.&lt;br /&gt;
&lt;br /&gt;
MAC addresses must be entered in XX:XX:XX:XX:XX:XX format.&lt;br /&gt;
&lt;br /&gt;
There is a utility script available for generating MAC addresses: http://www.easyvmx.com/software/easymac.sh. It is used like this:&lt;br /&gt;
&lt;br /&gt;
 chmod +x easymac.sh&lt;br /&gt;
 ./easymac.sh -R&lt;br /&gt;
&lt;br /&gt;
=== Adding veth to a CT ===&lt;br /&gt;
&lt;br /&gt;
 vzctl set &amp;lt;CTID&amp;gt; --netif_add &amp;lt;ifname&amp;gt;[,&amp;lt;mac&amp;gt;,&amp;lt;host_ifname&amp;gt;,&amp;lt;host_mac&amp;gt;,&amp;lt;bridge&amp;gt;]&lt;br /&gt;
&lt;br /&gt;
Here&lt;br /&gt;
* &amp;lt;tt&amp;gt;ifname&amp;lt;/tt&amp;gt; is the Ethernet device name in the CT&lt;br /&gt;
* &amp;lt;tt&amp;gt;mac&amp;lt;/tt&amp;gt; is its MAC address in the CT&lt;br /&gt;
* &amp;lt;tt&amp;gt;host_ifname&amp;lt;/tt&amp;gt;  is the Ethernet device name on the host ([[CT0]])&lt;br /&gt;
* &amp;lt;tt&amp;gt;host_mac&amp;lt;/tt&amp;gt; is its MAC address on the host ([[CT0]])&lt;br /&gt;
* &amp;lt;tt&amp;gt;bridge&amp;lt;/tt&amp;gt; is an optional parameter which can be used in custom network start scripts to automatically add  the interface to a bridge. (See the reference to the vznetaddbr script below and persistent bridge configurations.)&lt;br /&gt;
&lt;br /&gt;
{{Note|All parameters except &amp;lt;code&amp;gt;ifname&amp;lt;/code&amp;gt; are optional. Missing parameters, except for bridge, are automatically generated, if not specified. This is the preferred method.}}&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 vzctl set 101 --netif_add eth0 --save&lt;br /&gt;
&lt;br /&gt;
If you want to specify everything:&lt;br /&gt;
&lt;br /&gt;
 vzctl set 101 --netif_add eth0,00:12:34:56:78:9A,veth101.0,00:12:34:56:78:9B --save&lt;br /&gt;
&lt;br /&gt;
If you want to specify the bridge and autogenerate the other values:&lt;br /&gt;
&lt;br /&gt;
 vzctl set 101 --netif_add eth0,,,,vmbr1 --save&lt;br /&gt;
&lt;br /&gt;
=== Removing veth from a CT ===&lt;br /&gt;
&lt;br /&gt;
 vzctl set &amp;lt;CTID&amp;gt; --netif_del &amp;lt;dev_name&amp;gt;|all&lt;br /&gt;
&lt;br /&gt;
Here&lt;br /&gt;
* &amp;lt;code&amp;gt;dev_name&amp;lt;/code&amp;gt; is the Ethernet device name in the [[CT]].&lt;br /&gt;
&lt;br /&gt;
{{Note|If you want to remove all Ethernet devices in CT, use &amp;lt;code&amp;gt;all&amp;lt;/code&amp;gt;.}}&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 vzctl set 101 --netif_del eth0 --save&lt;br /&gt;
&lt;br /&gt;
== Common configurations with virtual Ethernet devices ==&lt;br /&gt;
Module &amp;lt;tt&amp;gt;vzethdev&amp;lt;/tt&amp;gt; must be loaded to operate with veth devices.&lt;br /&gt;
&lt;br /&gt;
=== Simple configuration with virtual Ethernet device ===&lt;br /&gt;
&lt;br /&gt;
Assuming you have 192.168.0.0/24 on your LAN, you will learn how to integrate a container in this LAN using veth.&lt;br /&gt;
&lt;br /&gt;
==== Start a CT ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# vzctl start 101&lt;br /&gt;
&lt;br /&gt;
==== Add veth device to CT ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# vzctl set 101 --netif_add eth0 --save&lt;br /&gt;
&lt;br /&gt;
This allocates a MAC address and associates it with the host eth0 port.&lt;br /&gt;
&lt;br /&gt;
==== Configure devices in CT0 ====&lt;br /&gt;
The following steps are needed when the [[CT]] is '''not''' bridged to a [[CT0]] network interface. That is because the [[CT]] is connected to a virtual network that is &amp;quot;behind&amp;quot; [[CT0]]. [[CT0]] must forward packets between its physical network interface and the virtual network interface where [[CT]] is located. The first step below to configure the interface is not necessary if the container has been started, since the device will have been initialized.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# ifconfig veth101.0 0&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/veth101.0/forwarding&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/veth101.0/proxy_arp&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/eth0/forwarding&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/eth0/proxy_arp&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Configure device in CT ====&lt;br /&gt;
The following steps show an example of a quick manual configuration of the [[CT]] network interface. Typically, you would configure the network settings in /etc/network/interfaces (Debian, see below) or however it is normally configured on your distribution. You can also comment or remove the configuration for venet0, if it exists, because that device will not be used.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# vzctl enter 101&lt;br /&gt;
[ve-101]# /sbin/ifconfig eth0 0&lt;br /&gt;
[ve-101]# /sbin/ip addr add 192.168.0.101 dev eth0&lt;br /&gt;
[ve-101]# /sbin/ip route add default dev eth0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
* Until you ifconfig eth0 it won't appear. When you do it will use the mac address netif_add added earlier&lt;br /&gt;
* 192.168.0.101 is chosen to be an [[unrouteable private ip address]]. Where 101 reminds you that it is node 101.&lt;br /&gt;
* The &amp;quot;ip route&amp;quot; tells all traffic to head to &amp;quot;device eth0&amp;quot;&lt;br /&gt;
* In theory you could [[use dhcpd with OpenVZ]] and dhclient to pick up an DHCP address from your router instead of hardwiring it&lt;br /&gt;
** http://openvz.org/pipermail/users/2005-November/000020.html&lt;br /&gt;
&lt;br /&gt;
==== Add route in [[CT0]] ====&lt;br /&gt;
Since [[CT0]] is acting as a router between its physical network interface and the virtual network interface of the [[CT]], we need to add a route to the [[CT]] to direct traffic to the right destination.&lt;br /&gt;
 [host-node]# ip route add 192.168.0.101 dev veth101.0&lt;br /&gt;
&lt;br /&gt;
=== Using a directly routed IPv4 with virtual Ethernet device ===&lt;br /&gt;
&lt;br /&gt;
==== Situation ====&lt;br /&gt;
Hardware Node (HN/CT0) has 192.168.0.1/24 with router 192.168.0.254.&lt;br /&gt;
&lt;br /&gt;
We also know that IPv4 10.0.0.1/32 is directly routed to 192.168.0.1 (this is called a ''fail-over IP'').&lt;br /&gt;
&lt;br /&gt;
We want to give this directly routed IPv4 address to a container (CT).&lt;br /&gt;
&lt;br /&gt;
==== Start container ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# vzctl start 101&lt;br /&gt;
&lt;br /&gt;
==== Add veth device to CT ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# vzctl set 101 --netif_add eth0 --save&lt;br /&gt;
&lt;br /&gt;
This allocates a MAC address and associates it with the host eth0 port.&lt;br /&gt;
&lt;br /&gt;
==== Configure device and add route in CT0 ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# ifconfig veth101.0 0&lt;br /&gt;
[host-node]# ip route add 10.0.0.1 dev veth101.0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can automatize this at VPS creation by using a mount script &amp;lt;tt&amp;gt;$VEID.mount&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The problem here is that the ''veth'' interface appears in CT0 '''after''' VPS has started, therefore we cannot directly use the commands in the mount script. We launch a shell script (enclosed by { }) in background (operator '''&amp;amp;''') that waits for the interface to be ready and then adds the IP route.&lt;br /&gt;
&lt;br /&gt;
Contents of the mount script &amp;lt;tt&amp;gt;/etc/vz/conf/101.mount&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
# This script source VPS configuration files in the same order as vzctl does&lt;br /&gt;
&lt;br /&gt;
# if one of these files does not exist then something is really broken&lt;br /&gt;
[ -f /etc/vz/vz.conf ] || exit 1&lt;br /&gt;
[ -f $VE_CONFFILE ] || exit 1&lt;br /&gt;
&lt;br /&gt;
# source both files. Note the order, it is important&lt;br /&gt;
. /etc/vz/vz.conf&lt;br /&gt;
. $VE_CONFFILE&lt;br /&gt;
&lt;br /&gt;
# Configure veth with IP after VPS has started&lt;br /&gt;
{&lt;br /&gt;
  IP=X.Y.Z.T&lt;br /&gt;
  DEV=veth101.0&lt;br /&gt;
  while sleep 1; do&lt;br /&gt;
    /sbin/ifconfig $DEV 0 &amp;gt;/dev/null 2&amp;gt;&amp;amp;1&lt;br /&gt;
    if [ $? -eq 0 ]; then&lt;br /&gt;
      /sbin/ip route add $IP dev $DEV&lt;br /&gt;
      break&lt;br /&gt;
    fi&lt;br /&gt;
  done&lt;br /&gt;
} &amp;amp;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Make sure IPv4 forwarding is enabled in CT0 ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/ip_forward&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/eth0/forwarding&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/veth101.0/forwarding&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
You can permanently set this by using &amp;lt;tt&amp;gt;/etc/sysctl.conf&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==== Configure device in CT ====&lt;br /&gt;
&lt;br /&gt;
1. Configure IP address&lt;br /&gt;
&lt;br /&gt;
2. Add gateway&lt;br /&gt;
&lt;br /&gt;
3. Add default route&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[ve-101]# /sbin/ifconfig eth0 10.0.0.1 netmask 255.255.255.255&lt;br /&gt;
[ve-101]# /sbin/ip route add 192.168.0.1 dev eth0&lt;br /&gt;
[ve-101]# /sbin/ip route add default via 192.168.0.1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In a Debian container, you can configure this permanently by using &amp;lt;tt&amp;gt;/etc/network/interfaces&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
auto eth0&lt;br /&gt;
iface eth0 inet static&lt;br /&gt;
        address 10.0.0.1&lt;br /&gt;
        netmask 255.255.255.255&lt;br /&gt;
        up /sbin/ip route add 192.168.0.1 dev eth0&lt;br /&gt;
        up /sbin/ip route add default via 192.168.0.1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Virtual Ethernet device with IPv6 ===&lt;br /&gt;
&lt;br /&gt;
See the [[VEs and HNs in same subnets]] article.&lt;br /&gt;
&lt;br /&gt;
=== Virtual Ethernet devices can be joined in one bridge ===&lt;br /&gt;
Bridging a [[CT]] interface to a [[CT0]] interface is the magic that allows the [[CT]] to be an independent host on the network with its own IP address, gateway, etc. [[CT0]] does not need any configuration for forwarding packets to the [[CT]] or performing proxy arp for the [[CT]].&lt;br /&gt;
&lt;br /&gt;
To manually configure a bridge and add devices to it, perform steps 1 - 4 from Simple configuration chapter for several containers and/or veth devices and then follow these steps.&lt;br /&gt;
&lt;br /&gt;
==== Create bridge device ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# brctl addbr vzbr0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Add veth devices to bridge ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# brctl addif vzbr0 veth101.0&lt;br /&gt;
...&lt;br /&gt;
[host-node]# brctl addif vzbr0 veth101.n&lt;br /&gt;
[host-node]# brctl addif vzbr0 veth102.0&lt;br /&gt;
...&lt;br /&gt;
...&lt;br /&gt;
[host-node]# brctl addif vzbr0 vethXXX.N&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Configure bridge device ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# ifconfig vzbr0 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Add routes in [[CT0]] ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# ip route add 192.168.101.1 dev vzbr0&lt;br /&gt;
...&lt;br /&gt;
[host-node]# ip route add 192.168.101.n dev vzbr0&lt;br /&gt;
[host-node]# ip route add 192.168.102.1 dev vzbr0&lt;br /&gt;
...&lt;br /&gt;
...&lt;br /&gt;
[host-node]# ip route add 192.168.XXX.N dev vzbr0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All routes to containers will be through this bridge and containers can communicate with each other even without these routes.&lt;br /&gt;
&lt;br /&gt;
=== Automating the bridge ===&lt;br /&gt;
The most convenient method is to automatically create the bridge at boot as a network interface, add the physical interface from [[CT0]] and then add the interface from each [[CT]] as it starts. All devices are connected to a virtual switch, and containers directly access the network just as any other host without additional configuration on [[CT0]].&lt;br /&gt;
&lt;br /&gt;
In Debian, configure the network interface on [[CT0]] to be a bridge in /etc/network/interfaces.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
iface eth0 inet manual&lt;br /&gt;
&lt;br /&gt;
auto vzbr0&lt;br /&gt;
iface vzbr0 inet static&lt;br /&gt;
        bridge-ports eth0&lt;br /&gt;
        address 192.168.1.100&lt;br /&gt;
        netmask 255.255.255.0&lt;br /&gt;
        network 192.168.1.0&lt;br /&gt;
        broadcast 192.168.1.255&lt;br /&gt;
        gateway 192.168.1.254&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Follow the steps below for making a veth bridge persistent with the included script. That will automatically add each container to the bridge when it is started. Finally, specify vzbr0 as the bridge when adding the network interface to a container, as describe above. No configuration is needed on [[CT0]] for forwarding packets, proxy arp or additional routes. The interface in each [[CT]] can be configured as desired. Everything &amp;quot;just works&amp;quot; according to normal network interface configuration and default routing rules.&lt;br /&gt;
&lt;br /&gt;
=== Making a veth-device persistent ===&lt;br /&gt;
These steps are no longer necessary, as the veth device is automatically created when the container is started. They remain here as a reference.&lt;br /&gt;
&lt;br /&gt;
According to http://bugzilla.openvz.org/show_bug.cgi?id=301 , a bug that stopped the veth device persistent was &amp;quot;Obsoleted now when --veth_add/--veth_del are introduced&amp;quot;&lt;br /&gt;
&lt;br /&gt;
See http://wiki.openvz.org/w/index.php?title=Virtual_Ethernet_device&amp;amp;diff=5990&amp;amp;oldid=5989#Making_a_veth-device_persistent for a workaround that used to be described in this section.&lt;br /&gt;
&lt;br /&gt;
That's it! At this point, when you restart the CT you should see a new line in the output, indicating that the interface is being configured and a new route being added. And you should be able to ping the host, and to enter the CT and use the network.&lt;br /&gt;
&lt;br /&gt;
=== Making a bridged veth-device persistent ===&lt;br /&gt;
&lt;br /&gt;
Like the above example, here it is how to add the veth device to a bridge in a persistent way. &lt;br /&gt;
&lt;br /&gt;
vzctl includes a 'vznetaddbr' script, which makes use of the ''bridge'' parameter of the --netif_add switch.&lt;br /&gt;
&lt;br /&gt;
Just create /etc/vz/vznet.conf containing the following.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
EXTERNAL_SCRIPT=&amp;quot;/usr/sbin/vznetaddbr&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The script uses 'vmbr0' as default bridge name when no bridge is specified.&lt;br /&gt;
&lt;br /&gt;
=== Virtual Ethernet devices + VLAN ===&lt;br /&gt;
This configuration can be done by adding vlan device to the previous configuration.&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
* [[Virtual network device]]&lt;br /&gt;
* [[Differences between venet and veth]]&lt;br /&gt;
* [[Using private IPs for Hardware Nodes]]&lt;br /&gt;
* Patch: [[Disable venet interface]]&lt;br /&gt;
* Troubleshooting: [[Bridge doesn't forward packets]]&lt;br /&gt;
&lt;br /&gt;
== External links ==&lt;br /&gt;
* [http://www.tldp.org/HOWTO/Linux+IPv6-HOWTO/hints-daemons-radvd.html Linux IPv6 HOWTO, a chapter about radvd]&lt;br /&gt;
* [http://sysadmin-ivanov.blogspot.com/2008/02/2-veth-with-2-bridges-on-openvz-at.html 2 veth with 2 bridges setup]&lt;br /&gt;
&lt;br /&gt;
[[Category: Networking]]&lt;br /&gt;
[[Category: HOWTO]]&lt;/div&gt;</summary>
		<author><name>Mose</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Virtual_Ethernet_device&amp;diff=11981</id>
		<title>Virtual Ethernet device</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Virtual_Ethernet_device&amp;diff=11981"/>
		<updated>2012-02-16T18:51:31Z</updated>

		<summary type="html">&lt;p&gt;Mose: /* Virtual Ethernet devices can be joined in one bridge */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Virtual Ethernet device''' is an Ethernet-like device that can be used&lt;br /&gt;
inside a [[container]]. Unlike a [[venet]] network device, a [[veth]] device&lt;br /&gt;
has a MAC address. Therefore, it can be used in more configurations. When veth&lt;br /&gt;
is bridged to a [[CT0]] network interface (e.g., eth0), the container can act as an&lt;br /&gt;
independent host on the network. The container's user can set up all of the networking&lt;br /&gt;
himself, including IPs, gateways, etc.&lt;br /&gt;
&lt;br /&gt;
A virtual Ethernet device consist of two Ethernet devices,&lt;br /&gt;
one in [[CT0]] (e.g., vethN.0) and a corresponding one in CT (e.g., eth0) that are&lt;br /&gt;
connected to each other. If a packet is sent to one device it will come out the other device.&lt;br /&gt;
&lt;br /&gt;
== Virtual Ethernet device usage ==&lt;br /&gt;
&lt;br /&gt;
=== Kernel module ===&lt;br /&gt;
The &amp;lt;code&amp;gt;vzethdev&amp;lt;/code&amp;gt; module should be loaded. You can check it with the following commands.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# lsmod | grep vzeth&lt;br /&gt;
vzethdev                8224  0&lt;br /&gt;
vzmon                  35164  5 vzethdev,vznetdev,vzrst,vzcpt&lt;br /&gt;
vzdev                   3080  4 vzethdev,vznetdev,vzmon,vzdquota&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In case it is not loaded, load it:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# modprobe vzethdev&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== MAC addresses ===&lt;br /&gt;
The following steps to generate a MAC address are not necessary, since newer versions&lt;br /&gt;
of vzctl will automatically generate a MAC address for you. These steps are provided&lt;br /&gt;
in case you want to set a MAC address manually.&lt;br /&gt;
&lt;br /&gt;
You should use a random MAC address when adding a network interface to a container. Do not use MAC addresses of real eth devices, because this can lead to collisions.&lt;br /&gt;
&lt;br /&gt;
MAC addresses must be entered in XX:XX:XX:XX:XX:XX format.&lt;br /&gt;
&lt;br /&gt;
There is a utility script available for generating MAC addresses: http://www.easyvmx.com/software/easymac.sh. It is used like this:&lt;br /&gt;
&lt;br /&gt;
 chmod +x easymac.sh&lt;br /&gt;
 ./easymac.sh -R&lt;br /&gt;
&lt;br /&gt;
=== Adding veth to a CT ===&lt;br /&gt;
&lt;br /&gt;
 vzctl set &amp;lt;CTID&amp;gt; --netif_add &amp;lt;ifname&amp;gt;[,&amp;lt;mac&amp;gt;,&amp;lt;host_ifname&amp;gt;,&amp;lt;host_mac&amp;gt;,&amp;lt;bridge&amp;gt;]&lt;br /&gt;
&lt;br /&gt;
Here&lt;br /&gt;
* &amp;lt;tt&amp;gt;ifname&amp;lt;/tt&amp;gt; is the Ethernet device name in the CT&lt;br /&gt;
* &amp;lt;tt&amp;gt;mac&amp;lt;/tt&amp;gt; is its MAC address in the CT&lt;br /&gt;
* &amp;lt;tt&amp;gt;host_ifname&amp;lt;/tt&amp;gt;  is the Ethernet device name on the host ([[CT0]])&lt;br /&gt;
* &amp;lt;tt&amp;gt;host_mac&amp;lt;/tt&amp;gt; is its MAC address on the host ([[CT0]])&lt;br /&gt;
* &amp;lt;tt&amp;gt;bridge&amp;lt;/tt&amp;gt; is an optional parameter which can be used in custom network start scripts to automatically add  the interface to a bridge. (See the reference to the vznetaddbr script below and persistent bridge configurations.)&lt;br /&gt;
&lt;br /&gt;
{{Note|All parameters except &amp;lt;code&amp;gt;ifname&amp;lt;/code&amp;gt; are optional. Missing parameters, except for bridge, are automatically generated, if not specified. This is the preferred method.}}&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 vzctl set 101 --netif_add eth0 --save&lt;br /&gt;
&lt;br /&gt;
If you want to specify everything:&lt;br /&gt;
&lt;br /&gt;
 vzctl set 101 --netif_add eth0,00:12:34:56:78:9A,veth101.0,00:12:34:56:78:9B --save&lt;br /&gt;
&lt;br /&gt;
If you want to specify the bridge and autogenerate the other values:&lt;br /&gt;
&lt;br /&gt;
 vzctl set 101 --netif_add eth0,,,,vmbr1 --save&lt;br /&gt;
&lt;br /&gt;
=== Removing veth from a CT ===&lt;br /&gt;
&lt;br /&gt;
 vzctl set &amp;lt;CTID&amp;gt; --netif_del &amp;lt;dev_name&amp;gt;|all&lt;br /&gt;
&lt;br /&gt;
Here&lt;br /&gt;
* &amp;lt;code&amp;gt;dev_name&amp;lt;/code&amp;gt; is the Ethernet device name in the [[CT]].&lt;br /&gt;
&lt;br /&gt;
{{Note|If you want to remove all Ethernet devices in CT, use &amp;lt;code&amp;gt;all&amp;lt;/code&amp;gt;.}}&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 vzctl set 101 --netif_del eth0 --save&lt;br /&gt;
&lt;br /&gt;
== Common configurations with virtual Ethernet devices ==&lt;br /&gt;
Module &amp;lt;tt&amp;gt;vzethdev&amp;lt;/tt&amp;gt; must be loaded to operate with veth devices.&lt;br /&gt;
&lt;br /&gt;
=== Simple configuration with virtual Ethernet device ===&lt;br /&gt;
&lt;br /&gt;
Assuming you have 192.168.0.0/24 on your LAN, you will learn how to integrate a container in this LAN using veth.&lt;br /&gt;
&lt;br /&gt;
==== Start a CT ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# vzctl start 101&lt;br /&gt;
&lt;br /&gt;
==== Add veth device to CT ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# vzctl set 101 --netif_add eth0 --save&lt;br /&gt;
&lt;br /&gt;
This allocates a MAC address and associates it with the host eth0 port.&lt;br /&gt;
&lt;br /&gt;
==== Configure devices in CT0 ====&lt;br /&gt;
The following steps are needed when the [[CT]] is '''not''' bridged to a [[CT0]] network interface. That is because the [[CT]] is connected to a virtual network that is &amp;quot;behind&amp;quot; [[CT0]]. [[CT0]] must forward packets between its physical network interface and the virtual network interface where [[CT]] is located. The first step below to configure the interface is not necessary if the container has been started, since the device will have been initialized.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# ifconfig veth101.0 0&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/veth101.0/forwarding&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/veth101.0/proxy_arp&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/eth0/forwarding&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/eth0/proxy_arp&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Configure device in CT ====&lt;br /&gt;
The following steps show an example of a quick manual configuration of the [[CT]] network interface. Typically, you would configure the network settings in /etc/network/interfaces (Debian, see below) or however it is normally configured on your distribution. You can also comment or remove the configuration for venet0, if it exists, because that device will not be used.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# vzctl enter 101&lt;br /&gt;
[ve-101]# /sbin/ifconfig eth0 0&lt;br /&gt;
[ve-101]# /sbin/ip addr add 192.168.0.101 dev eth0&lt;br /&gt;
[ve-101]# /sbin/ip route add default dev eth0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
* Until you ifconfig eth0 it won't appear. When you do it will use the mac address netif_add added earlier&lt;br /&gt;
* 192.168.0.101 is chosen to be an [[unrouteable private ip address]]. Where 101 reminds you that it is node 101.&lt;br /&gt;
* The &amp;quot;ip route&amp;quot; tells all traffic to head to &amp;quot;device eth0&amp;quot;&lt;br /&gt;
* In theory you could [[use dhcpd with OpenVZ]] and dhclient to pick up an DHCP address from your router instead of hardwiring it&lt;br /&gt;
** http://openvz.org/pipermail/users/2005-November/000020.html&lt;br /&gt;
&lt;br /&gt;
==== Add route in [[CT0]] ====&lt;br /&gt;
Since [[CT0]] is acting as a router between its physical network interface and the virtual network interface of the [[CT]], we need to add a route to the [[CT]] to direct traffic to the right destination.&lt;br /&gt;
 [host-node]# ip route add 192.168.0.101 dev veth101.0&lt;br /&gt;
&lt;br /&gt;
=== Using a directly routed IPv4 with virtual Ethernet device ===&lt;br /&gt;
&lt;br /&gt;
==== Situation ====&lt;br /&gt;
Hardware Node (HN/CT0) has 192.168.0.1/24 with router 192.168.0.254.&lt;br /&gt;
&lt;br /&gt;
We also know that IPv4 10.0.0.1/32 is directly routed to 192.168.0.1 (this is called a ''fail-over IP'').&lt;br /&gt;
&lt;br /&gt;
We want to give this directly routed IPv4 address to a container (CT).&lt;br /&gt;
&lt;br /&gt;
==== Start container ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# vzctl start 101&lt;br /&gt;
&lt;br /&gt;
==== Add veth device to CT ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# vzctl set 101 --netif_add eth0 --save&lt;br /&gt;
&lt;br /&gt;
This allocates a MAC address and associates it with the host eth0 port.&lt;br /&gt;
&lt;br /&gt;
==== Configure device and add route in CT0 ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# ifconfig veth101.0 0&lt;br /&gt;
[host-node]# ip route add 10.0.0.1 dev veth101.0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can automatize this at VPS creation by using a mount script &amp;lt;tt&amp;gt;$VEID.mount&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The problem here is that the ''veth'' interface appears in CT0 '''after''' VPS has started, therefore we cannot directly use the commands in the mount script. We launch a shell script (enclosed by { }) in background (operator '''&amp;amp;''') that waits for the interface to be ready and then adds the IP route.&lt;br /&gt;
&lt;br /&gt;
Contents of the mount script &amp;lt;tt&amp;gt;/etc/vz/conf/101.mount&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
# This script source VPS configuration files in the same order as vzctl does&lt;br /&gt;
&lt;br /&gt;
# if one of these files does not exist then something is really broken&lt;br /&gt;
[ -f /etc/vz/vz.conf ] || exit 1&lt;br /&gt;
[ -f $VE_CONFFILE ] || exit 1&lt;br /&gt;
&lt;br /&gt;
# source both files. Note the order, it is important&lt;br /&gt;
. /etc/vz/vz.conf&lt;br /&gt;
. $VE_CONFFILE&lt;br /&gt;
&lt;br /&gt;
# Configure veth with IP after VPS has started&lt;br /&gt;
{&lt;br /&gt;
  IP=X.Y.Z.T&lt;br /&gt;
  DEV=veth101.0&lt;br /&gt;
  while sleep 1; do&lt;br /&gt;
    /sbin/ifconfig $DEV 0 &amp;gt;/dev/null 2&amp;gt;&amp;amp;1&lt;br /&gt;
    if [ $? -eq 0 ]; then&lt;br /&gt;
      /sbin/ip route add $IP dev $DEV&lt;br /&gt;
      break&lt;br /&gt;
    fi&lt;br /&gt;
  done&lt;br /&gt;
} &amp;amp;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Make sure IPv4 forwarding is enabled in CT0 ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/ip_forward&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/eth0/forwarding&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/veth101.0/forwarding&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
You can permanently set this by using &amp;lt;tt&amp;gt;/etc/sysctl.conf&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==== Configure device in CT ====&lt;br /&gt;
&lt;br /&gt;
1. Configure IP address&lt;br /&gt;
&lt;br /&gt;
2. Add gateway&lt;br /&gt;
&lt;br /&gt;
3. Add default route&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[ve-101]# /sbin/ifconfig eth0 10.0.0.1 netmask 255.255.255.255&lt;br /&gt;
[ve-101]# /sbin/ip route add 192.168.0.1 dev eth0&lt;br /&gt;
[ve-101]# /sbin/ip route add default via 192.168.0.1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In a Debian container, you can configure this permanently by using &amp;lt;tt&amp;gt;/etc/network/interfaces&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
auto eth0&lt;br /&gt;
iface eth0 inet static&lt;br /&gt;
        address 10.0.0.1&lt;br /&gt;
        netmask 255.255.255.255&lt;br /&gt;
        up /sbin/ip route add 192.168.0.1 dev eth0&lt;br /&gt;
        up /sbin/ip route add default via 192.168.0.1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Virtual Ethernet device with IPv6 ===&lt;br /&gt;
&lt;br /&gt;
See the [[VEs and HNs in same subnets]] article.&lt;br /&gt;
&lt;br /&gt;
=== Virtual Ethernet devices can be joined in one bridge ===&lt;br /&gt;
Bridging a [[CT]] interface to a [[CT0]] interface is the magic that allows the [[CT]] to be an independent host on the network with its own IP address, gateway, etc. [[CT0]] does not need any configuration for forwarding packets to the [[CT]] or performing proxy arp for the [[CT]].&lt;br /&gt;
&lt;br /&gt;
To manually configure a bridge and add devices to it, perform steps 1 - 4 from Simple configuration chapter for several containers and/or veth devices and then follow these steps.&lt;br /&gt;
&lt;br /&gt;
==== Create bridge device ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# brctl addbr vzbr0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Add veth devices to bridge ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# brctl addif vzbr0 veth101.0&lt;br /&gt;
...&lt;br /&gt;
[host-node]# brctl addif vzbr0 veth101.n&lt;br /&gt;
[host-node]# brctl addif vzbr0 veth102.0&lt;br /&gt;
...&lt;br /&gt;
...&lt;br /&gt;
[host-node]# brctl addif vzbr0 vethXXX.N&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Configure bridge device ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# ifconfig vzbr0 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Add routes in [[CT0]] ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# ip route add 192.168.101.1 dev vzbr0&lt;br /&gt;
...&lt;br /&gt;
[host-node]# ip route add 192.168.101.n dev vzbr0&lt;br /&gt;
[host-node]# ip route add 192.168.102.1 dev vzbr0&lt;br /&gt;
...&lt;br /&gt;
...&lt;br /&gt;
[host-node]# ip route add 192.168.XXX.N dev vzbr0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All routes to containers will be through this bridge and containers can communicate with each other even without these routes.&lt;br /&gt;
&lt;br /&gt;
=== Making a veth-device persistent ===&lt;br /&gt;
According to http://bugzilla.openvz.org/show_bug.cgi?id=301 , a bug that stopped the veth device persistent was &amp;quot;Obsoleted now when --veth_add/--veth_del are introduced&amp;quot;&lt;br /&gt;
&lt;br /&gt;
See http://wiki.openvz.org/w/index.php?title=Virtual_Ethernet_device&amp;amp;diff=5990&amp;amp;oldid=5989#Making_a_veth-device_persistent for a workaround that used to be described in this section.&lt;br /&gt;
&lt;br /&gt;
That's it! At this point, when you restart the CT you should see a new line in the output, indicating that the interface is being configured and a new route being added. And you should be able to ping the host, and to enter the CT and use the network.&lt;br /&gt;
&lt;br /&gt;
=== Making a bridged veth-device persistent ===&lt;br /&gt;
&lt;br /&gt;
Like the above example, here it is how to add the veth device to a bridge in a persistent way. &lt;br /&gt;
&lt;br /&gt;
vzctl include a 'vznetaddbr' script, which makes use of the ''bridge'' parameter of the --netif_add switch.&lt;br /&gt;
&lt;br /&gt;
Just create /etc/vz/vznet.conf containing the following.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
EXTERNAL_SCRIPT=&amp;quot;/usr/sbin/vznetaddbr&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The script uses 'vmbr0' as default bridge name when no bridge is specified.&lt;br /&gt;
&lt;br /&gt;
=== Virtual Ethernet devices + VLAN ===&lt;br /&gt;
This configuration can be done by adding vlan device to the previous configuration.&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
* [[Virtual network device]]&lt;br /&gt;
* [[Differences between venet and veth]]&lt;br /&gt;
* [[Using private IPs for Hardware Nodes]]&lt;br /&gt;
* Patch: [[Disable venet interface]]&lt;br /&gt;
* Troubleshooting: [[Bridge doesn't forward packets]]&lt;br /&gt;
&lt;br /&gt;
== External links ==&lt;br /&gt;
* [http://www.tldp.org/HOWTO/Linux+IPv6-HOWTO/hints-daemons-radvd.html Linux IPv6 HOWTO, a chapter about radvd]&lt;br /&gt;
* [http://sysadmin-ivanov.blogspot.com/2008/02/2-veth-with-2-bridges-on-openvz-at.html 2 veth with 2 bridges setup]&lt;br /&gt;
&lt;br /&gt;
[[Category: Networking]]&lt;br /&gt;
[[Category: HOWTO]]&lt;/div&gt;</summary>
		<author><name>Mose</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Virtual_Ethernet_device&amp;diff=11980</id>
		<title>Virtual Ethernet device</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Virtual_Ethernet_device&amp;diff=11980"/>
		<updated>2012-02-16T18:29:10Z</updated>

		<summary type="html">&lt;p&gt;Mose: /* Add route in CT0 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Virtual Ethernet device''' is an Ethernet-like device that can be used&lt;br /&gt;
inside a [[container]]. Unlike a [[venet]] network device, a [[veth]] device&lt;br /&gt;
has a MAC address. Therefore, it can be used in more configurations. When veth&lt;br /&gt;
is bridged to a [[CT0]] network interface (e.g., eth0), the container can act as an&lt;br /&gt;
independent host on the network. The container's user can set up all of the networking&lt;br /&gt;
himself, including IPs, gateways, etc.&lt;br /&gt;
&lt;br /&gt;
A virtual Ethernet device consist of two Ethernet devices,&lt;br /&gt;
one in [[CT0]] (e.g., vethN.0) and a corresponding one in CT (e.g., eth0) that are&lt;br /&gt;
connected to each other. If a packet is sent to one device it will come out the other device.&lt;br /&gt;
&lt;br /&gt;
== Virtual Ethernet device usage ==&lt;br /&gt;
&lt;br /&gt;
=== Kernel module ===&lt;br /&gt;
The &amp;lt;code&amp;gt;vzethdev&amp;lt;/code&amp;gt; module should be loaded. You can check it with the following commands.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# lsmod | grep vzeth&lt;br /&gt;
vzethdev                8224  0&lt;br /&gt;
vzmon                  35164  5 vzethdev,vznetdev,vzrst,vzcpt&lt;br /&gt;
vzdev                   3080  4 vzethdev,vznetdev,vzmon,vzdquota&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In case it is not loaded, load it:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# modprobe vzethdev&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== MAC addresses ===&lt;br /&gt;
The following steps to generate a MAC address are not necessary, since newer versions&lt;br /&gt;
of vzctl will automatically generate a MAC address for you. These steps are provided&lt;br /&gt;
in case you want to set a MAC address manually.&lt;br /&gt;
&lt;br /&gt;
You should use a random MAC address when adding a network interface to a container. Do not use MAC addresses of real eth devices, because this can lead to collisions.&lt;br /&gt;
&lt;br /&gt;
MAC addresses must be entered in XX:XX:XX:XX:XX:XX format.&lt;br /&gt;
&lt;br /&gt;
There is a utility script available for generating MAC addresses: http://www.easyvmx.com/software/easymac.sh. It is used like this:&lt;br /&gt;
&lt;br /&gt;
 chmod +x easymac.sh&lt;br /&gt;
 ./easymac.sh -R&lt;br /&gt;
&lt;br /&gt;
=== Adding veth to a CT ===&lt;br /&gt;
&lt;br /&gt;
 vzctl set &amp;lt;CTID&amp;gt; --netif_add &amp;lt;ifname&amp;gt;[,&amp;lt;mac&amp;gt;,&amp;lt;host_ifname&amp;gt;,&amp;lt;host_mac&amp;gt;,&amp;lt;bridge&amp;gt;]&lt;br /&gt;
&lt;br /&gt;
Here&lt;br /&gt;
* &amp;lt;tt&amp;gt;ifname&amp;lt;/tt&amp;gt; is the Ethernet device name in the CT&lt;br /&gt;
* &amp;lt;tt&amp;gt;mac&amp;lt;/tt&amp;gt; is its MAC address in the CT&lt;br /&gt;
* &amp;lt;tt&amp;gt;host_ifname&amp;lt;/tt&amp;gt;  is the Ethernet device name on the host ([[CT0]])&lt;br /&gt;
* &amp;lt;tt&amp;gt;host_mac&amp;lt;/tt&amp;gt; is its MAC address on the host ([[CT0]])&lt;br /&gt;
* &amp;lt;tt&amp;gt;bridge&amp;lt;/tt&amp;gt; is an optional parameter which can be used in custom network start scripts to automatically add  the interface to a bridge. (See the reference to the vznetaddbr script below and persistent bridge configurations.)&lt;br /&gt;
&lt;br /&gt;
{{Note|All parameters except &amp;lt;code&amp;gt;ifname&amp;lt;/code&amp;gt; are optional. Missing parameters, except for bridge, are automatically generated, if not specified. This is the preferred method.}}&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 vzctl set 101 --netif_add eth0 --save&lt;br /&gt;
&lt;br /&gt;
If you want to specify everything:&lt;br /&gt;
&lt;br /&gt;
 vzctl set 101 --netif_add eth0,00:12:34:56:78:9A,veth101.0,00:12:34:56:78:9B --save&lt;br /&gt;
&lt;br /&gt;
If you want to specify the bridge and autogenerate the other values:&lt;br /&gt;
&lt;br /&gt;
 vzctl set 101 --netif_add eth0,,,,vmbr1 --save&lt;br /&gt;
&lt;br /&gt;
=== Removing veth from a CT ===&lt;br /&gt;
&lt;br /&gt;
 vzctl set &amp;lt;CTID&amp;gt; --netif_del &amp;lt;dev_name&amp;gt;|all&lt;br /&gt;
&lt;br /&gt;
Here&lt;br /&gt;
* &amp;lt;code&amp;gt;dev_name&amp;lt;/code&amp;gt; is the Ethernet device name in the [[CT]].&lt;br /&gt;
&lt;br /&gt;
{{Note|If you want to remove all Ethernet devices in CT, use &amp;lt;code&amp;gt;all&amp;lt;/code&amp;gt;.}}&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 vzctl set 101 --netif_del eth0 --save&lt;br /&gt;
&lt;br /&gt;
== Common configurations with virtual Ethernet devices ==&lt;br /&gt;
Module &amp;lt;tt&amp;gt;vzethdev&amp;lt;/tt&amp;gt; must be loaded to operate with veth devices.&lt;br /&gt;
&lt;br /&gt;
=== Simple configuration with virtual Ethernet device ===&lt;br /&gt;
&lt;br /&gt;
Assuming you have 192.168.0.0/24 on your LAN, you will learn how to integrate a container in this LAN using veth.&lt;br /&gt;
&lt;br /&gt;
==== Start a CT ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# vzctl start 101&lt;br /&gt;
&lt;br /&gt;
==== Add veth device to CT ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# vzctl set 101 --netif_add eth0 --save&lt;br /&gt;
&lt;br /&gt;
This allocates a MAC address and associates it with the host eth0 port.&lt;br /&gt;
&lt;br /&gt;
==== Configure devices in CT0 ====&lt;br /&gt;
The following steps are needed when the [[CT]] is '''not''' bridged to a [[CT0]] network interface. That is because the [[CT]] is connected to a virtual network that is &amp;quot;behind&amp;quot; [[CT0]]. [[CT0]] must forward packets between its physical network interface and the virtual network interface where [[CT]] is located. The first step below to configure the interface is not necessary if the container has been started, since the device will have been initialized.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# ifconfig veth101.0 0&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/veth101.0/forwarding&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/veth101.0/proxy_arp&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/eth0/forwarding&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/eth0/proxy_arp&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Configure device in CT ====&lt;br /&gt;
The following steps show an example of a quick manual configuration of the [[CT]] network interface. Typically, you would configure the network settings in /etc/network/interfaces (Debian, see below) or however it is normally configured on your distribution. You can also comment or remove the configuration for venet0, if it exists, because that device will not be used.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# vzctl enter 101&lt;br /&gt;
[ve-101]# /sbin/ifconfig eth0 0&lt;br /&gt;
[ve-101]# /sbin/ip addr add 192.168.0.101 dev eth0&lt;br /&gt;
[ve-101]# /sbin/ip route add default dev eth0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
* Until you ifconfig eth0 it won't appear. When you do it will use the mac address netif_add added earlier&lt;br /&gt;
* 192.168.0.101 is chosen to be an [[unrouteable private ip address]]. Where 101 reminds you that it is node 101.&lt;br /&gt;
* The &amp;quot;ip route&amp;quot; tells all traffic to head to &amp;quot;device eth0&amp;quot;&lt;br /&gt;
* In theory you could [[use dhcpd with OpenVZ]] and dhclient to pick up an DHCP address from your router instead of hardwiring it&lt;br /&gt;
** http://openvz.org/pipermail/users/2005-November/000020.html&lt;br /&gt;
&lt;br /&gt;
==== Add route in [[CT0]] ====&lt;br /&gt;
Since [[CT0]] is acting as a router between its physical network interface and the virtual network interface of the [[CT]], we need to add a route to the [[CT]] to direct traffic to the right destination.&lt;br /&gt;
 [host-node]# ip route add 192.168.0.101 dev veth101.0&lt;br /&gt;
&lt;br /&gt;
=== Using a directly routed IPv4 with virtual Ethernet device ===&lt;br /&gt;
&lt;br /&gt;
==== Situation ====&lt;br /&gt;
Hardware Node (HN/CT0) has 192.168.0.1/24 with router 192.168.0.254.&lt;br /&gt;
&lt;br /&gt;
We also know that IPv4 10.0.0.1/32 is directly routed to 192.168.0.1 (this is called a ''fail-over IP'').&lt;br /&gt;
&lt;br /&gt;
We want to give this directly routed IPv4 address to a container (CT).&lt;br /&gt;
&lt;br /&gt;
==== Start container ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# vzctl start 101&lt;br /&gt;
&lt;br /&gt;
==== Add veth device to CT ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# vzctl set 101 --netif_add eth0 --save&lt;br /&gt;
&lt;br /&gt;
This allocates a MAC address and associates it with the host eth0 port.&lt;br /&gt;
&lt;br /&gt;
==== Configure device and add route in CT0 ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# ifconfig veth101.0 0&lt;br /&gt;
[host-node]# ip route add 10.0.0.1 dev veth101.0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can automatize this at VPS creation by using a mount script &amp;lt;tt&amp;gt;$VEID.mount&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The problem here is that the ''veth'' interface appears in CT0 '''after''' VPS has started, therefore we cannot directly use the commands in the mount script. We launch a shell script (enclosed by { }) in background (operator '''&amp;amp;''') that waits for the interface to be ready and then adds the IP route.&lt;br /&gt;
&lt;br /&gt;
Contents of the mount script &amp;lt;tt&amp;gt;/etc/vz/conf/101.mount&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
# This script source VPS configuration files in the same order as vzctl does&lt;br /&gt;
&lt;br /&gt;
# if one of these files does not exist then something is really broken&lt;br /&gt;
[ -f /etc/vz/vz.conf ] || exit 1&lt;br /&gt;
[ -f $VE_CONFFILE ] || exit 1&lt;br /&gt;
&lt;br /&gt;
# source both files. Note the order, it is important&lt;br /&gt;
. /etc/vz/vz.conf&lt;br /&gt;
. $VE_CONFFILE&lt;br /&gt;
&lt;br /&gt;
# Configure veth with IP after VPS has started&lt;br /&gt;
{&lt;br /&gt;
  IP=X.Y.Z.T&lt;br /&gt;
  DEV=veth101.0&lt;br /&gt;
  while sleep 1; do&lt;br /&gt;
    /sbin/ifconfig $DEV 0 &amp;gt;/dev/null 2&amp;gt;&amp;amp;1&lt;br /&gt;
    if [ $? -eq 0 ]; then&lt;br /&gt;
      /sbin/ip route add $IP dev $DEV&lt;br /&gt;
      break&lt;br /&gt;
    fi&lt;br /&gt;
  done&lt;br /&gt;
} &amp;amp;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Make sure IPv4 forwarding is enabled in CT0 ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/ip_forward&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/eth0/forwarding&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/veth101.0/forwarding&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
You can permanently set this by using &amp;lt;tt&amp;gt;/etc/sysctl.conf&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==== Configure device in CT ====&lt;br /&gt;
&lt;br /&gt;
1. Configure IP address&lt;br /&gt;
&lt;br /&gt;
2. Add gateway&lt;br /&gt;
&lt;br /&gt;
3. Add default route&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[ve-101]# /sbin/ifconfig eth0 10.0.0.1 netmask 255.255.255.255&lt;br /&gt;
[ve-101]# /sbin/ip route add 192.168.0.1 dev eth0&lt;br /&gt;
[ve-101]# /sbin/ip route add default via 192.168.0.1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In a Debian container, you can configure this permanently by using &amp;lt;tt&amp;gt;/etc/network/interfaces&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
auto eth0&lt;br /&gt;
iface eth0 inet static&lt;br /&gt;
        address 10.0.0.1&lt;br /&gt;
        netmask 255.255.255.255&lt;br /&gt;
        up /sbin/ip route add 192.168.0.1 dev eth0&lt;br /&gt;
        up /sbin/ip route add default via 192.168.0.1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Virtual Ethernet device with IPv6 ===&lt;br /&gt;
&lt;br /&gt;
See the [[VEs and HNs in same subnets]] article.&lt;br /&gt;
&lt;br /&gt;
=== Virtual Ethernet devices can be joined in one bridge ===&lt;br /&gt;
Perform steps 1 - 4 from Simple configuration chapter for several containers and/or veth devices&lt;br /&gt;
&lt;br /&gt;
==== Create bridge device ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# brctl addbr vzbr0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Add veth devices to bridge ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# brctl addif vzbr0 veth101.0&lt;br /&gt;
...&lt;br /&gt;
[host-node]# brctl addif vzbr0 veth101.n&lt;br /&gt;
[host-node]# brctl addif vzbr0 veth102.0&lt;br /&gt;
...&lt;br /&gt;
...&lt;br /&gt;
[host-node]# brctl addif vzbr0 vethXXX.N&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Configure bridge device ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# ifconfig vzbr0 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Add routes in [[CT0]] ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# ip route add 192.168.101.1 dev vzbr0&lt;br /&gt;
...&lt;br /&gt;
[host-node]# ip route add 192.168.101.n dev vzbr0&lt;br /&gt;
[host-node]# ip route add 192.168.102.1 dev vzbr0&lt;br /&gt;
...&lt;br /&gt;
...&lt;br /&gt;
[host-node]# ip route add 192.168.XXX.N dev vzbr0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Thus you'll have more convinient configuration, i.e. all routes to containers will be through this bridge and containers can communicate with each other even without these routes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Making a veth-device persistent ===&lt;br /&gt;
According to http://bugzilla.openvz.org/show_bug.cgi?id=301 , a bug that stopped the veth device persistent was &amp;quot;Obsoleted now when --veth_add/--veth_del are introduced&amp;quot;&lt;br /&gt;
&lt;br /&gt;
See http://wiki.openvz.org/w/index.php?title=Virtual_Ethernet_device&amp;amp;diff=5990&amp;amp;oldid=5989#Making_a_veth-device_persistent for a workaround that used to be described in this section.&lt;br /&gt;
&lt;br /&gt;
That's it! At this point, when you restart the CT you should see a new line in the output, indicating that the interface is being configured and a new route being added. And you should be able to ping the host, and to enter the CT and use the network.&lt;br /&gt;
&lt;br /&gt;
=== Making a bridged veth-device persistent ===&lt;br /&gt;
&lt;br /&gt;
Like the above example, here it is how to add the veth device to a bridge in a persistent way. &lt;br /&gt;
&lt;br /&gt;
vzctl include a 'vznetaddbr' script, which makes use of the ''bridge'' parameter of the --netif_add switch.&lt;br /&gt;
&lt;br /&gt;
Just create /etc/vz/vznet.conf containing the following.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
EXTERNAL_SCRIPT=&amp;quot;/usr/sbin/vznetaddbr&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The script uses 'vmbr0' as default bridge name when no bridge is specified.&lt;br /&gt;
&lt;br /&gt;
=== Virtual Ethernet devices + VLAN ===&lt;br /&gt;
This configuration can be done by adding vlan device to the previous configuration.&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
* [[Virtual network device]]&lt;br /&gt;
* [[Differences between venet and veth]]&lt;br /&gt;
* [[Using private IPs for Hardware Nodes]]&lt;br /&gt;
* Patch: [[Disable venet interface]]&lt;br /&gt;
* Troubleshooting: [[Bridge doesn't forward packets]]&lt;br /&gt;
&lt;br /&gt;
== External links ==&lt;br /&gt;
* [http://www.tldp.org/HOWTO/Linux+IPv6-HOWTO/hints-daemons-radvd.html Linux IPv6 HOWTO, a chapter about radvd]&lt;br /&gt;
* [http://sysadmin-ivanov.blogspot.com/2008/02/2-veth-with-2-bridges-on-openvz-at.html 2 veth with 2 bridges setup]&lt;br /&gt;
&lt;br /&gt;
[[Category: Networking]]&lt;br /&gt;
[[Category: HOWTO]]&lt;/div&gt;</summary>
		<author><name>Mose</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Virtual_Ethernet_device&amp;diff=11979</id>
		<title>Virtual Ethernet device</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Virtual_Ethernet_device&amp;diff=11979"/>
		<updated>2012-02-16T18:27:39Z</updated>

		<summary type="html">&lt;p&gt;Mose: /* Configure device in CT */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Virtual Ethernet device''' is an Ethernet-like device that can be used&lt;br /&gt;
inside a [[container]]. Unlike a [[venet]] network device, a [[veth]] device&lt;br /&gt;
has a MAC address. Therefore, it can be used in more configurations. When veth&lt;br /&gt;
is bridged to a [[CT0]] network interface (e.g., eth0), the container can act as an&lt;br /&gt;
independent host on the network. The container's user can set up all of the networking&lt;br /&gt;
himself, including IPs, gateways, etc.&lt;br /&gt;
&lt;br /&gt;
A virtual Ethernet device consist of two Ethernet devices,&lt;br /&gt;
one in [[CT0]] (e.g., vethN.0) and a corresponding one in CT (e.g., eth0) that are&lt;br /&gt;
connected to each other. If a packet is sent to one device it will come out the other device.&lt;br /&gt;
&lt;br /&gt;
== Virtual Ethernet device usage ==&lt;br /&gt;
&lt;br /&gt;
=== Kernel module ===&lt;br /&gt;
The &amp;lt;code&amp;gt;vzethdev&amp;lt;/code&amp;gt; module should be loaded. You can check it with the following commands.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# lsmod | grep vzeth&lt;br /&gt;
vzethdev                8224  0&lt;br /&gt;
vzmon                  35164  5 vzethdev,vznetdev,vzrst,vzcpt&lt;br /&gt;
vzdev                   3080  4 vzethdev,vznetdev,vzmon,vzdquota&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In case it is not loaded, load it:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# modprobe vzethdev&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== MAC addresses ===&lt;br /&gt;
The following steps to generate a MAC address are not necessary, since newer versions&lt;br /&gt;
of vzctl will automatically generate a MAC address for you. These steps are provided&lt;br /&gt;
in case you want to set a MAC address manually.&lt;br /&gt;
&lt;br /&gt;
You should use a random MAC address when adding a network interface to a container. Do not use MAC addresses of real eth devices, because this can lead to collisions.&lt;br /&gt;
&lt;br /&gt;
MAC addresses must be entered in XX:XX:XX:XX:XX:XX format.&lt;br /&gt;
&lt;br /&gt;
There is a utility script available for generating MAC addresses: http://www.easyvmx.com/software/easymac.sh. It is used like this:&lt;br /&gt;
&lt;br /&gt;
 chmod +x easymac.sh&lt;br /&gt;
 ./easymac.sh -R&lt;br /&gt;
&lt;br /&gt;
=== Adding veth to a CT ===&lt;br /&gt;
&lt;br /&gt;
 vzctl set &amp;lt;CTID&amp;gt; --netif_add &amp;lt;ifname&amp;gt;[,&amp;lt;mac&amp;gt;,&amp;lt;host_ifname&amp;gt;,&amp;lt;host_mac&amp;gt;,&amp;lt;bridge&amp;gt;]&lt;br /&gt;
&lt;br /&gt;
Here&lt;br /&gt;
* &amp;lt;tt&amp;gt;ifname&amp;lt;/tt&amp;gt; is the Ethernet device name in the CT&lt;br /&gt;
* &amp;lt;tt&amp;gt;mac&amp;lt;/tt&amp;gt; is its MAC address in the CT&lt;br /&gt;
* &amp;lt;tt&amp;gt;host_ifname&amp;lt;/tt&amp;gt;  is the Ethernet device name on the host ([[CT0]])&lt;br /&gt;
* &amp;lt;tt&amp;gt;host_mac&amp;lt;/tt&amp;gt; is its MAC address on the host ([[CT0]])&lt;br /&gt;
* &amp;lt;tt&amp;gt;bridge&amp;lt;/tt&amp;gt; is an optional parameter which can be used in custom network start scripts to automatically add  the interface to a bridge. (See the reference to the vznetaddbr script below and persistent bridge configurations.)&lt;br /&gt;
&lt;br /&gt;
{{Note|All parameters except &amp;lt;code&amp;gt;ifname&amp;lt;/code&amp;gt; are optional. Missing parameters, except for bridge, are automatically generated, if not specified. This is the preferred method.}}&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 vzctl set 101 --netif_add eth0 --save&lt;br /&gt;
&lt;br /&gt;
If you want to specify everything:&lt;br /&gt;
&lt;br /&gt;
 vzctl set 101 --netif_add eth0,00:12:34:56:78:9A,veth101.0,00:12:34:56:78:9B --save&lt;br /&gt;
&lt;br /&gt;
If you want to specify the bridge and autogenerate the other values:&lt;br /&gt;
&lt;br /&gt;
 vzctl set 101 --netif_add eth0,,,,vmbr1 --save&lt;br /&gt;
&lt;br /&gt;
=== Removing veth from a CT ===&lt;br /&gt;
&lt;br /&gt;
 vzctl set &amp;lt;CTID&amp;gt; --netif_del &amp;lt;dev_name&amp;gt;|all&lt;br /&gt;
&lt;br /&gt;
Here&lt;br /&gt;
* &amp;lt;code&amp;gt;dev_name&amp;lt;/code&amp;gt; is the Ethernet device name in the [[CT]].&lt;br /&gt;
&lt;br /&gt;
{{Note|If you want to remove all Ethernet devices in CT, use &amp;lt;code&amp;gt;all&amp;lt;/code&amp;gt;.}}&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 vzctl set 101 --netif_del eth0 --save&lt;br /&gt;
&lt;br /&gt;
== Common configurations with virtual Ethernet devices ==&lt;br /&gt;
Module &amp;lt;tt&amp;gt;vzethdev&amp;lt;/tt&amp;gt; must be loaded to operate with veth devices.&lt;br /&gt;
&lt;br /&gt;
=== Simple configuration with virtual Ethernet device ===&lt;br /&gt;
&lt;br /&gt;
Assuming you have 192.168.0.0/24 on your LAN, you will learn how to integrate a container in this LAN using veth.&lt;br /&gt;
&lt;br /&gt;
==== Start a CT ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# vzctl start 101&lt;br /&gt;
&lt;br /&gt;
==== Add veth device to CT ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# vzctl set 101 --netif_add eth0 --save&lt;br /&gt;
&lt;br /&gt;
This allocates a MAC address and associates it with the host eth0 port.&lt;br /&gt;
&lt;br /&gt;
==== Configure devices in CT0 ====&lt;br /&gt;
The following steps are needed when the [[CT]] is '''not''' bridged to a [[CT0]] network interface. That is because the [[CT]] is connected to a virtual network that is &amp;quot;behind&amp;quot; [[CT0]]. [[CT0]] must forward packets between its physical network interface and the virtual network interface where [[CT]] is located. The first step below to configure the interface is not necessary if the container has been started, since the device will have been initialized.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# ifconfig veth101.0 0&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/veth101.0/forwarding&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/veth101.0/proxy_arp&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/eth0/forwarding&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/eth0/proxy_arp&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Configure device in CT ====&lt;br /&gt;
The following steps show an example of a quick manual configuration of the [[CT]] network interface. Typically, you would configure the network settings in /etc/network/interfaces (Debian, see below) or however it is normally configured on your distribution. You can also comment or remove the configuration for venet0, if it exists, because that device will not be used.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# vzctl enter 101&lt;br /&gt;
[ve-101]# /sbin/ifconfig eth0 0&lt;br /&gt;
[ve-101]# /sbin/ip addr add 192.168.0.101 dev eth0&lt;br /&gt;
[ve-101]# /sbin/ip route add default dev eth0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
* Until you ifconfig eth0 it won't appear. When you do it will use the mac address netif_add added earlier&lt;br /&gt;
* 192.168.0.101 is chosen to be an [[unrouteable private ip address]]. Where 101 reminds you that it is node 101.&lt;br /&gt;
* The &amp;quot;ip route&amp;quot; tells all traffic to head to &amp;quot;device eth0&amp;quot;&lt;br /&gt;
* In theory you could [[use dhcpd with OpenVZ]] and dhclient to pick up an DHCP address from your router instead of hardwiring it&lt;br /&gt;
** http://openvz.org/pipermail/users/2005-November/000020.html&lt;br /&gt;
&lt;br /&gt;
==== Add route in [[CT0]] ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# ip route add 192.168.0.101 dev veth101.0&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Using a directly routed IPv4 with virtual Ethernet device ===&lt;br /&gt;
&lt;br /&gt;
==== Situation ====&lt;br /&gt;
Hardware Node (HN/CT0) has 192.168.0.1/24 with router 192.168.0.254.&lt;br /&gt;
&lt;br /&gt;
We also know that IPv4 10.0.0.1/32 is directly routed to 192.168.0.1 (this is called a ''fail-over IP'').&lt;br /&gt;
&lt;br /&gt;
We want to give this directly routed IPv4 address to a container (CT).&lt;br /&gt;
&lt;br /&gt;
==== Start container ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# vzctl start 101&lt;br /&gt;
&lt;br /&gt;
==== Add veth device to CT ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# vzctl set 101 --netif_add eth0 --save&lt;br /&gt;
&lt;br /&gt;
This allocates a MAC address and associates it with the host eth0 port.&lt;br /&gt;
&lt;br /&gt;
==== Configure device and add route in CT0 ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# ifconfig veth101.0 0&lt;br /&gt;
[host-node]# ip route add 10.0.0.1 dev veth101.0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can automatize this at VPS creation by using a mount script &amp;lt;tt&amp;gt;$VEID.mount&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The problem here is that the ''veth'' interface appears in CT0 '''after''' VPS has started, therefore we cannot directly use the commands in the mount script. We launch a shell script (enclosed by { }) in background (operator '''&amp;amp;''') that waits for the interface to be ready and then adds the IP route.&lt;br /&gt;
&lt;br /&gt;
Contents of the mount script &amp;lt;tt&amp;gt;/etc/vz/conf/101.mount&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
# This script source VPS configuration files in the same order as vzctl does&lt;br /&gt;
&lt;br /&gt;
# if one of these files does not exist then something is really broken&lt;br /&gt;
[ -f /etc/vz/vz.conf ] || exit 1&lt;br /&gt;
[ -f $VE_CONFFILE ] || exit 1&lt;br /&gt;
&lt;br /&gt;
# source both files. Note the order, it is important&lt;br /&gt;
. /etc/vz/vz.conf&lt;br /&gt;
. $VE_CONFFILE&lt;br /&gt;
&lt;br /&gt;
# Configure veth with IP after VPS has started&lt;br /&gt;
{&lt;br /&gt;
  IP=X.Y.Z.T&lt;br /&gt;
  DEV=veth101.0&lt;br /&gt;
  while sleep 1; do&lt;br /&gt;
    /sbin/ifconfig $DEV 0 &amp;gt;/dev/null 2&amp;gt;&amp;amp;1&lt;br /&gt;
    if [ $? -eq 0 ]; then&lt;br /&gt;
      /sbin/ip route add $IP dev $DEV&lt;br /&gt;
      break&lt;br /&gt;
    fi&lt;br /&gt;
  done&lt;br /&gt;
} &amp;amp;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Make sure IPv4 forwarding is enabled in CT0 ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/ip_forward&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/eth0/forwarding&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/veth101.0/forwarding&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
You can permanently set this by using &amp;lt;tt&amp;gt;/etc/sysctl.conf&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==== Configure device in CT ====&lt;br /&gt;
&lt;br /&gt;
1. Configure IP address&lt;br /&gt;
&lt;br /&gt;
2. Add gateway&lt;br /&gt;
&lt;br /&gt;
3. Add default route&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[ve-101]# /sbin/ifconfig eth0 10.0.0.1 netmask 255.255.255.255&lt;br /&gt;
[ve-101]# /sbin/ip route add 192.168.0.1 dev eth0&lt;br /&gt;
[ve-101]# /sbin/ip route add default via 192.168.0.1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In a Debian container, you can configure this permanently by using &amp;lt;tt&amp;gt;/etc/network/interfaces&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
auto eth0&lt;br /&gt;
iface eth0 inet static&lt;br /&gt;
        address 10.0.0.1&lt;br /&gt;
        netmask 255.255.255.255&lt;br /&gt;
        up /sbin/ip route add 192.168.0.1 dev eth0&lt;br /&gt;
        up /sbin/ip route add default via 192.168.0.1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Virtual Ethernet device with IPv6 ===&lt;br /&gt;
&lt;br /&gt;
See the [[VEs and HNs in same subnets]] article.&lt;br /&gt;
&lt;br /&gt;
=== Virtual Ethernet devices can be joined in one bridge ===&lt;br /&gt;
Perform steps 1 - 4 from Simple configuration chapter for several containers and/or veth devices&lt;br /&gt;
&lt;br /&gt;
==== Create bridge device ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# brctl addbr vzbr0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Add veth devices to bridge ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# brctl addif vzbr0 veth101.0&lt;br /&gt;
...&lt;br /&gt;
[host-node]# brctl addif vzbr0 veth101.n&lt;br /&gt;
[host-node]# brctl addif vzbr0 veth102.0&lt;br /&gt;
...&lt;br /&gt;
...&lt;br /&gt;
[host-node]# brctl addif vzbr0 vethXXX.N&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Configure bridge device ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# ifconfig vzbr0 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Add routes in [[CT0]] ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# ip route add 192.168.101.1 dev vzbr0&lt;br /&gt;
...&lt;br /&gt;
[host-node]# ip route add 192.168.101.n dev vzbr0&lt;br /&gt;
[host-node]# ip route add 192.168.102.1 dev vzbr0&lt;br /&gt;
...&lt;br /&gt;
...&lt;br /&gt;
[host-node]# ip route add 192.168.XXX.N dev vzbr0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Thus you'll have more convinient configuration, i.e. all routes to containers will be through this bridge and containers can communicate with each other even without these routes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Making a veth-device persistent ===&lt;br /&gt;
According to http://bugzilla.openvz.org/show_bug.cgi?id=301 , a bug that stopped the veth device persistent was &amp;quot;Obsoleted now when --veth_add/--veth_del are introduced&amp;quot;&lt;br /&gt;
&lt;br /&gt;
See http://wiki.openvz.org/w/index.php?title=Virtual_Ethernet_device&amp;amp;diff=5990&amp;amp;oldid=5989#Making_a_veth-device_persistent for a workaround that used to be described in this section.&lt;br /&gt;
&lt;br /&gt;
That's it! At this point, when you restart the CT you should see a new line in the output, indicating that the interface is being configured and a new route being added. And you should be able to ping the host, and to enter the CT and use the network.&lt;br /&gt;
&lt;br /&gt;
=== Making a bridged veth-device persistent ===&lt;br /&gt;
&lt;br /&gt;
Like the above example, here it is how to add the veth device to a bridge in a persistent way. &lt;br /&gt;
&lt;br /&gt;
vzctl include a 'vznetaddbr' script, which makes use of the ''bridge'' parameter of the --netif_add switch.&lt;br /&gt;
&lt;br /&gt;
Just create /etc/vz/vznet.conf containing the following.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
EXTERNAL_SCRIPT=&amp;quot;/usr/sbin/vznetaddbr&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The script uses 'vmbr0' as default bridge name when no bridge is specified.&lt;br /&gt;
&lt;br /&gt;
=== Virtual Ethernet devices + VLAN ===&lt;br /&gt;
This configuration can be done by adding vlan device to the previous configuration.&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
* [[Virtual network device]]&lt;br /&gt;
* [[Differences between venet and veth]]&lt;br /&gt;
* [[Using private IPs for Hardware Nodes]]&lt;br /&gt;
* Patch: [[Disable venet interface]]&lt;br /&gt;
* Troubleshooting: [[Bridge doesn't forward packets]]&lt;br /&gt;
&lt;br /&gt;
== External links ==&lt;br /&gt;
* [http://www.tldp.org/HOWTO/Linux+IPv6-HOWTO/hints-daemons-radvd.html Linux IPv6 HOWTO, a chapter about radvd]&lt;br /&gt;
* [http://sysadmin-ivanov.blogspot.com/2008/02/2-veth-with-2-bridges-on-openvz-at.html 2 veth with 2 bridges setup]&lt;br /&gt;
&lt;br /&gt;
[[Category: Networking]]&lt;br /&gt;
[[Category: HOWTO]]&lt;/div&gt;</summary>
		<author><name>Mose</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Virtual_Ethernet_device&amp;diff=11978</id>
		<title>Virtual Ethernet device</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Virtual_Ethernet_device&amp;diff=11978"/>
		<updated>2012-02-16T18:25:06Z</updated>

		<summary type="html">&lt;p&gt;Mose: /* Configure device in CT */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Virtual Ethernet device''' is an Ethernet-like device that can be used&lt;br /&gt;
inside a [[container]]. Unlike a [[venet]] network device, a [[veth]] device&lt;br /&gt;
has a MAC address. Therefore, it can be used in more configurations. When veth&lt;br /&gt;
is bridged to a [[CT0]] network interface (e.g., eth0), the container can act as an&lt;br /&gt;
independent host on the network. The container's user can set up all of the networking&lt;br /&gt;
himself, including IPs, gateways, etc.&lt;br /&gt;
&lt;br /&gt;
A virtual Ethernet device consist of two Ethernet devices,&lt;br /&gt;
one in [[CT0]] (e.g., vethN.0) and a corresponding one in CT (e.g., eth0) that are&lt;br /&gt;
connected to each other. If a packet is sent to one device it will come out the other device.&lt;br /&gt;
&lt;br /&gt;
== Virtual Ethernet device usage ==&lt;br /&gt;
&lt;br /&gt;
=== Kernel module ===&lt;br /&gt;
The &amp;lt;code&amp;gt;vzethdev&amp;lt;/code&amp;gt; module should be loaded. You can check it with the following commands.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# lsmod | grep vzeth&lt;br /&gt;
vzethdev                8224  0&lt;br /&gt;
vzmon                  35164  5 vzethdev,vznetdev,vzrst,vzcpt&lt;br /&gt;
vzdev                   3080  4 vzethdev,vznetdev,vzmon,vzdquota&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In case it is not loaded, load it:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# modprobe vzethdev&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== MAC addresses ===&lt;br /&gt;
The following steps to generate a MAC address are not necessary, since newer versions&lt;br /&gt;
of vzctl will automatically generate a MAC address for you. These steps are provided&lt;br /&gt;
in case you want to set a MAC address manually.&lt;br /&gt;
&lt;br /&gt;
You should use a random MAC address when adding a network interface to a container. Do not use MAC addresses of real eth devices, because this can lead to collisions.&lt;br /&gt;
&lt;br /&gt;
MAC addresses must be entered in XX:XX:XX:XX:XX:XX format.&lt;br /&gt;
&lt;br /&gt;
There is a utility script available for generating MAC addresses: http://www.easyvmx.com/software/easymac.sh. It is used like this:&lt;br /&gt;
&lt;br /&gt;
 chmod +x easymac.sh&lt;br /&gt;
 ./easymac.sh -R&lt;br /&gt;
&lt;br /&gt;
=== Adding veth to a CT ===&lt;br /&gt;
&lt;br /&gt;
 vzctl set &amp;lt;CTID&amp;gt; --netif_add &amp;lt;ifname&amp;gt;[,&amp;lt;mac&amp;gt;,&amp;lt;host_ifname&amp;gt;,&amp;lt;host_mac&amp;gt;,&amp;lt;bridge&amp;gt;]&lt;br /&gt;
&lt;br /&gt;
Here&lt;br /&gt;
* &amp;lt;tt&amp;gt;ifname&amp;lt;/tt&amp;gt; is the Ethernet device name in the CT&lt;br /&gt;
* &amp;lt;tt&amp;gt;mac&amp;lt;/tt&amp;gt; is its MAC address in the CT&lt;br /&gt;
* &amp;lt;tt&amp;gt;host_ifname&amp;lt;/tt&amp;gt;  is the Ethernet device name on the host ([[CT0]])&lt;br /&gt;
* &amp;lt;tt&amp;gt;host_mac&amp;lt;/tt&amp;gt; is its MAC address on the host ([[CT0]])&lt;br /&gt;
* &amp;lt;tt&amp;gt;bridge&amp;lt;/tt&amp;gt; is an optional parameter which can be used in custom network start scripts to automatically add  the interface to a bridge. (See the reference to the vznetaddbr script below and persistent bridge configurations.)&lt;br /&gt;
&lt;br /&gt;
{{Note|All parameters except &amp;lt;code&amp;gt;ifname&amp;lt;/code&amp;gt; are optional. Missing parameters, except for bridge, are automatically generated, if not specified. This is the preferred method.}}&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 vzctl set 101 --netif_add eth0 --save&lt;br /&gt;
&lt;br /&gt;
If you want to specify everything:&lt;br /&gt;
&lt;br /&gt;
 vzctl set 101 --netif_add eth0,00:12:34:56:78:9A,veth101.0,00:12:34:56:78:9B --save&lt;br /&gt;
&lt;br /&gt;
If you want to specify the bridge and autogenerate the other values:&lt;br /&gt;
&lt;br /&gt;
 vzctl set 101 --netif_add eth0,,,,vmbr1 --save&lt;br /&gt;
&lt;br /&gt;
=== Removing veth from a CT ===&lt;br /&gt;
&lt;br /&gt;
 vzctl set &amp;lt;CTID&amp;gt; --netif_del &amp;lt;dev_name&amp;gt;|all&lt;br /&gt;
&lt;br /&gt;
Here&lt;br /&gt;
* &amp;lt;code&amp;gt;dev_name&amp;lt;/code&amp;gt; is the Ethernet device name in the [[CT]].&lt;br /&gt;
&lt;br /&gt;
{{Note|If you want to remove all Ethernet devices in CT, use &amp;lt;code&amp;gt;all&amp;lt;/code&amp;gt;.}}&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 vzctl set 101 --netif_del eth0 --save&lt;br /&gt;
&lt;br /&gt;
== Common configurations with virtual Ethernet devices ==&lt;br /&gt;
Module &amp;lt;tt&amp;gt;vzethdev&amp;lt;/tt&amp;gt; must be loaded to operate with veth devices.&lt;br /&gt;
&lt;br /&gt;
=== Simple configuration with virtual Ethernet device ===&lt;br /&gt;
&lt;br /&gt;
Assuming you have 192.168.0.0/24 on your LAN, you will learn how to integrate a container in this LAN using veth.&lt;br /&gt;
&lt;br /&gt;
==== Start a CT ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# vzctl start 101&lt;br /&gt;
&lt;br /&gt;
==== Add veth device to CT ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# vzctl set 101 --netif_add eth0 --save&lt;br /&gt;
&lt;br /&gt;
This allocates a MAC address and associates it with the host eth0 port.&lt;br /&gt;
&lt;br /&gt;
==== Configure devices in CT0 ====&lt;br /&gt;
The following steps are needed when the [[CT]] is '''not''' bridged to a [[CT0]] network interface. That is because the [[CT]] is connected to a virtual network that is &amp;quot;behind&amp;quot; [[CT0]]. [[CT0]] must forward packets between its physical network interface and the virtual network interface where [[CT]] is located. The first step below to configure the interface is not necessary if the container has been started, since the device will have been initialized.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# ifconfig veth101.0 0&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/veth101.0/forwarding&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/veth101.0/proxy_arp&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/eth0/forwarding&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/eth0/proxy_arp&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Configure device in CT ====&lt;br /&gt;
The following steps show an example of a quick manual configuration of the [[CT]] network interface. Typically, you would configure the network settings in /etc/network/interfaces or however it is normally configured on your distribution. You can also comment or remove the configuration for venet0, if it exists, because that device will not be used.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# vzctl enter 101&lt;br /&gt;
[ve-101]# /sbin/ifconfig eth0 0&lt;br /&gt;
[ve-101]# /sbin/ip addr add 192.168.0.101 dev eth0&lt;br /&gt;
[ve-101]# /sbin/ip route add default dev eth0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
* Until you ifconfig eth0 it won't appear. When you do it will use the mac address netif_add added earlier&lt;br /&gt;
* 192.168.0.101 is chosen to be an [[unrouteable private ip address]]. Where 101 reminds you that it is node 101.&lt;br /&gt;
* The &amp;quot;ip route&amp;quot; tells all traffic to head to &amp;quot;device eth0&amp;quot;&lt;br /&gt;
* In theory you could [[use dhcpd with OpenVZ]] and dhclient to pick up an DHCP address from your router instead of hardwiring it&lt;br /&gt;
** http://openvz.org/pipermail/users/2005-November/000020.html&lt;br /&gt;
&lt;br /&gt;
==== Add route in [[CT0]] ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# ip route add 192.168.0.101 dev veth101.0&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Using a directly routed IPv4 with virtual Ethernet device ===&lt;br /&gt;
&lt;br /&gt;
==== Situation ====&lt;br /&gt;
Hardware Node (HN/CT0) has 192.168.0.1/24 with router 192.168.0.254.&lt;br /&gt;
&lt;br /&gt;
We also know that IPv4 10.0.0.1/32 is directly routed to 192.168.0.1 (this is called a ''fail-over IP'').&lt;br /&gt;
&lt;br /&gt;
We want to give this directly routed IPv4 address to a container (CT).&lt;br /&gt;
&lt;br /&gt;
==== Start container ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# vzctl start 101&lt;br /&gt;
&lt;br /&gt;
==== Add veth device to CT ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# vzctl set 101 --netif_add eth0 --save&lt;br /&gt;
&lt;br /&gt;
This allocates a MAC address and associates it with the host eth0 port.&lt;br /&gt;
&lt;br /&gt;
==== Configure device and add route in CT0 ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# ifconfig veth101.0 0&lt;br /&gt;
[host-node]# ip route add 10.0.0.1 dev veth101.0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can automatize this at VPS creation by using a mount script &amp;lt;tt&amp;gt;$VEID.mount&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The problem here is that the ''veth'' interface appears in CT0 '''after''' VPS has started, therefore we cannot directly use the commands in the mount script. We launch a shell script (enclosed by { }) in background (operator '''&amp;amp;''') that waits for the interface to be ready and then adds the IP route.&lt;br /&gt;
&lt;br /&gt;
Contents of the mount script &amp;lt;tt&amp;gt;/etc/vz/conf/101.mount&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
# This script source VPS configuration files in the same order as vzctl does&lt;br /&gt;
&lt;br /&gt;
# if one of these files does not exist then something is really broken&lt;br /&gt;
[ -f /etc/vz/vz.conf ] || exit 1&lt;br /&gt;
[ -f $VE_CONFFILE ] || exit 1&lt;br /&gt;
&lt;br /&gt;
# source both files. Note the order, it is important&lt;br /&gt;
. /etc/vz/vz.conf&lt;br /&gt;
. $VE_CONFFILE&lt;br /&gt;
&lt;br /&gt;
# Configure veth with IP after VPS has started&lt;br /&gt;
{&lt;br /&gt;
  IP=X.Y.Z.T&lt;br /&gt;
  DEV=veth101.0&lt;br /&gt;
  while sleep 1; do&lt;br /&gt;
    /sbin/ifconfig $DEV 0 &amp;gt;/dev/null 2&amp;gt;&amp;amp;1&lt;br /&gt;
    if [ $? -eq 0 ]; then&lt;br /&gt;
      /sbin/ip route add $IP dev $DEV&lt;br /&gt;
      break&lt;br /&gt;
    fi&lt;br /&gt;
  done&lt;br /&gt;
} &amp;amp;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Make sure IPv4 forwarding is enabled in CT0 ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/ip_forward&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/eth0/forwarding&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/veth101.0/forwarding&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
You can permanently set this by using &amp;lt;tt&amp;gt;/etc/sysctl.conf&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==== Configure device in CT ====&lt;br /&gt;
&lt;br /&gt;
1. Configure IP address&lt;br /&gt;
&lt;br /&gt;
2. Add gateway&lt;br /&gt;
&lt;br /&gt;
3. Add default route&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[ve-101]# /sbin/ifconfig eth0 10.0.0.1 netmask 255.255.255.255&lt;br /&gt;
[ve-101]# /sbin/ip route add 192.168.0.1 dev eth0&lt;br /&gt;
[ve-101]# /sbin/ip route add default via 192.168.0.1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In a Debian container, you can configure this permanently by using &amp;lt;tt&amp;gt;/etc/network/interfaces&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
auto eth0&lt;br /&gt;
iface eth0 inet static&lt;br /&gt;
        address 10.0.0.1&lt;br /&gt;
        netmask 255.255.255.255&lt;br /&gt;
        up /sbin/ip route add 192.168.0.1 dev eth0&lt;br /&gt;
        up /sbin/ip route add default via 192.168.0.1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Virtual Ethernet device with IPv6 ===&lt;br /&gt;
&lt;br /&gt;
See the [[VEs and HNs in same subnets]] article.&lt;br /&gt;
&lt;br /&gt;
=== Virtual Ethernet devices can be joined in one bridge ===&lt;br /&gt;
Perform steps 1 - 4 from Simple configuration chapter for several containers and/or veth devices&lt;br /&gt;
&lt;br /&gt;
==== Create bridge device ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# brctl addbr vzbr0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Add veth devices to bridge ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# brctl addif vzbr0 veth101.0&lt;br /&gt;
...&lt;br /&gt;
[host-node]# brctl addif vzbr0 veth101.n&lt;br /&gt;
[host-node]# brctl addif vzbr0 veth102.0&lt;br /&gt;
...&lt;br /&gt;
...&lt;br /&gt;
[host-node]# brctl addif vzbr0 vethXXX.N&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Configure bridge device ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# ifconfig vzbr0 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Add routes in [[CT0]] ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# ip route add 192.168.101.1 dev vzbr0&lt;br /&gt;
...&lt;br /&gt;
[host-node]# ip route add 192.168.101.n dev vzbr0&lt;br /&gt;
[host-node]# ip route add 192.168.102.1 dev vzbr0&lt;br /&gt;
...&lt;br /&gt;
...&lt;br /&gt;
[host-node]# ip route add 192.168.XXX.N dev vzbr0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Thus you'll have more convinient configuration, i.e. all routes to containers will be through this bridge and containers can communicate with each other even without these routes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Making a veth-device persistent ===&lt;br /&gt;
According to http://bugzilla.openvz.org/show_bug.cgi?id=301 , a bug that stopped the veth device persistent was &amp;quot;Obsoleted now when --veth_add/--veth_del are introduced&amp;quot;&lt;br /&gt;
&lt;br /&gt;
See http://wiki.openvz.org/w/index.php?title=Virtual_Ethernet_device&amp;amp;diff=5990&amp;amp;oldid=5989#Making_a_veth-device_persistent for a workaround that used to be described in this section.&lt;br /&gt;
&lt;br /&gt;
That's it! At this point, when you restart the CT you should see a new line in the output, indicating that the interface is being configured and a new route being added. And you should be able to ping the host, and to enter the CT and use the network.&lt;br /&gt;
&lt;br /&gt;
=== Making a bridged veth-device persistent ===&lt;br /&gt;
&lt;br /&gt;
Like the above example, here it is how to add the veth device to a bridge in a persistent way. &lt;br /&gt;
&lt;br /&gt;
vzctl include a 'vznetaddbr' script, which makes use of the ''bridge'' parameter of the --netif_add switch.&lt;br /&gt;
&lt;br /&gt;
Just create /etc/vz/vznet.conf containing the following.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
EXTERNAL_SCRIPT=&amp;quot;/usr/sbin/vznetaddbr&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The script uses 'vmbr0' as default bridge name when no bridge is specified.&lt;br /&gt;
&lt;br /&gt;
=== Virtual Ethernet devices + VLAN ===&lt;br /&gt;
This configuration can be done by adding vlan device to the previous configuration.&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
* [[Virtual network device]]&lt;br /&gt;
* [[Differences between venet and veth]]&lt;br /&gt;
* [[Using private IPs for Hardware Nodes]]&lt;br /&gt;
* Patch: [[Disable venet interface]]&lt;br /&gt;
* Troubleshooting: [[Bridge doesn't forward packets]]&lt;br /&gt;
&lt;br /&gt;
== External links ==&lt;br /&gt;
* [http://www.tldp.org/HOWTO/Linux+IPv6-HOWTO/hints-daemons-radvd.html Linux IPv6 HOWTO, a chapter about radvd]&lt;br /&gt;
* [http://sysadmin-ivanov.blogspot.com/2008/02/2-veth-with-2-bridges-on-openvz-at.html 2 veth with 2 bridges setup]&lt;br /&gt;
&lt;br /&gt;
[[Category: Networking]]&lt;br /&gt;
[[Category: HOWTO]]&lt;/div&gt;</summary>
		<author><name>Mose</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Virtual_Ethernet_device&amp;diff=11977</id>
		<title>Virtual Ethernet device</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Virtual_Ethernet_device&amp;diff=11977"/>
		<updated>2012-02-16T18:16:13Z</updated>

		<summary type="html">&lt;p&gt;Mose: /* Configure devices in CT0 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Virtual Ethernet device''' is an Ethernet-like device that can be used&lt;br /&gt;
inside a [[container]]. Unlike a [[venet]] network device, a [[veth]] device&lt;br /&gt;
has a MAC address. Therefore, it can be used in more configurations. When veth&lt;br /&gt;
is bridged to a [[CT0]] network interface (e.g., eth0), the container can act as an&lt;br /&gt;
independent host on the network. The container's user can set up all of the networking&lt;br /&gt;
himself, including IPs, gateways, etc.&lt;br /&gt;
&lt;br /&gt;
A virtual Ethernet device consist of two Ethernet devices,&lt;br /&gt;
one in [[CT0]] (e.g., vethN.0) and a corresponding one in CT (e.g., eth0) that are&lt;br /&gt;
connected to each other. If a packet is sent to one device it will come out the other device.&lt;br /&gt;
&lt;br /&gt;
== Virtual Ethernet device usage ==&lt;br /&gt;
&lt;br /&gt;
=== Kernel module ===&lt;br /&gt;
The &amp;lt;code&amp;gt;vzethdev&amp;lt;/code&amp;gt; module should be loaded. You can check it with the following commands.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# lsmod | grep vzeth&lt;br /&gt;
vzethdev                8224  0&lt;br /&gt;
vzmon                  35164  5 vzethdev,vznetdev,vzrst,vzcpt&lt;br /&gt;
vzdev                   3080  4 vzethdev,vznetdev,vzmon,vzdquota&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In case it is not loaded, load it:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# modprobe vzethdev&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== MAC addresses ===&lt;br /&gt;
The following steps to generate a MAC address are not necessary, since newer versions&lt;br /&gt;
of vzctl will automatically generate a MAC address for you. These steps are provided&lt;br /&gt;
in case you want to set a MAC address manually.&lt;br /&gt;
&lt;br /&gt;
You should use a random MAC address when adding a network interface to a container. Do not use MAC addresses of real eth devices, because this can lead to collisions.&lt;br /&gt;
&lt;br /&gt;
MAC addresses must be entered in XX:XX:XX:XX:XX:XX format.&lt;br /&gt;
&lt;br /&gt;
There is a utility script available for generating MAC addresses: http://www.easyvmx.com/software/easymac.sh. It is used like this:&lt;br /&gt;
&lt;br /&gt;
 chmod +x easymac.sh&lt;br /&gt;
 ./easymac.sh -R&lt;br /&gt;
&lt;br /&gt;
=== Adding veth to a CT ===&lt;br /&gt;
&lt;br /&gt;
 vzctl set &amp;lt;CTID&amp;gt; --netif_add &amp;lt;ifname&amp;gt;[,&amp;lt;mac&amp;gt;,&amp;lt;host_ifname&amp;gt;,&amp;lt;host_mac&amp;gt;,&amp;lt;bridge&amp;gt;]&lt;br /&gt;
&lt;br /&gt;
Here&lt;br /&gt;
* &amp;lt;tt&amp;gt;ifname&amp;lt;/tt&amp;gt; is the Ethernet device name in the CT&lt;br /&gt;
* &amp;lt;tt&amp;gt;mac&amp;lt;/tt&amp;gt; is its MAC address in the CT&lt;br /&gt;
* &amp;lt;tt&amp;gt;host_ifname&amp;lt;/tt&amp;gt;  is the Ethernet device name on the host ([[CT0]])&lt;br /&gt;
* &amp;lt;tt&amp;gt;host_mac&amp;lt;/tt&amp;gt; is its MAC address on the host ([[CT0]])&lt;br /&gt;
* &amp;lt;tt&amp;gt;bridge&amp;lt;/tt&amp;gt; is an optional parameter which can be used in custom network start scripts to automatically add  the interface to a bridge. (See the reference to the vznetaddbr script below and persistent bridge configurations.)&lt;br /&gt;
&lt;br /&gt;
{{Note|All parameters except &amp;lt;code&amp;gt;ifname&amp;lt;/code&amp;gt; are optional. Missing parameters, except for bridge, are automatically generated, if not specified. This is the preferred method.}}&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 vzctl set 101 --netif_add eth0 --save&lt;br /&gt;
&lt;br /&gt;
If you want to specify everything:&lt;br /&gt;
&lt;br /&gt;
 vzctl set 101 --netif_add eth0,00:12:34:56:78:9A,veth101.0,00:12:34:56:78:9B --save&lt;br /&gt;
&lt;br /&gt;
If you want to specify the bridge and autogenerate the other values:&lt;br /&gt;
&lt;br /&gt;
 vzctl set 101 --netif_add eth0,,,,vmbr1 --save&lt;br /&gt;
&lt;br /&gt;
=== Removing veth from a CT ===&lt;br /&gt;
&lt;br /&gt;
 vzctl set &amp;lt;CTID&amp;gt; --netif_del &amp;lt;dev_name&amp;gt;|all&lt;br /&gt;
&lt;br /&gt;
Here&lt;br /&gt;
* &amp;lt;code&amp;gt;dev_name&amp;lt;/code&amp;gt; is the Ethernet device name in the [[CT]].&lt;br /&gt;
&lt;br /&gt;
{{Note|If you want to remove all Ethernet devices in CT, use &amp;lt;code&amp;gt;all&amp;lt;/code&amp;gt;.}}&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 vzctl set 101 --netif_del eth0 --save&lt;br /&gt;
&lt;br /&gt;
== Common configurations with virtual Ethernet devices ==&lt;br /&gt;
Module &amp;lt;tt&amp;gt;vzethdev&amp;lt;/tt&amp;gt; must be loaded to operate with veth devices.&lt;br /&gt;
&lt;br /&gt;
=== Simple configuration with virtual Ethernet device ===&lt;br /&gt;
&lt;br /&gt;
Assuming you have 192.168.0.0/24 on your LAN, you will learn how to integrate a container in this LAN using veth.&lt;br /&gt;
&lt;br /&gt;
==== Start a CT ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# vzctl start 101&lt;br /&gt;
&lt;br /&gt;
==== Add veth device to CT ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# vzctl set 101 --netif_add eth0 --save&lt;br /&gt;
&lt;br /&gt;
This allocates a MAC address and associates it with the host eth0 port.&lt;br /&gt;
&lt;br /&gt;
==== Configure devices in CT0 ====&lt;br /&gt;
The following steps are needed when the [[CT]] is '''not''' bridged to a [[CT0]] network interface. That is because the [[CT]] is connected to a virtual network that is &amp;quot;behind&amp;quot; [[CT0]]. [[CT0]] must forward packets between its physical network interface and the virtual network interface where [[CT]] is located. The first step below to configure the interface is not necessary if the container has been started, since the device will have been initialized.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# ifconfig veth101.0 0&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/veth101.0/forwarding&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/veth101.0/proxy_arp&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/eth0/forwarding&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/eth0/proxy_arp&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Configure device in CT ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# vzctl enter 101&lt;br /&gt;
[ve-101]# /sbin/ifconfig eth0 0&lt;br /&gt;
[ve-101]# /sbin/ip addr add 192.168.0.101 dev eth0&lt;br /&gt;
[ve-101]# /sbin/ip route add default dev eth0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
* Until you ifconfig eth0 it won't appear. When you do it will use the mac address netif_add added earlier&lt;br /&gt;
* 192.168.0.101 is chosen to be an [[unrouteable private ip address]]. Where 101 reminds you that it is node 101.&lt;br /&gt;
* The &amp;quot;ip route&amp;quot; tells all traffic to head to &amp;quot;device eth0&amp;quot;&lt;br /&gt;
* In theory you could [[use dhcpd with OpenVZ]] and dhclient to pick up an DHCP address from your router instead of hardwiring it&lt;br /&gt;
** http://openvz.org/pipermail/users/2005-November/000020.html&lt;br /&gt;
&lt;br /&gt;
==== Add route in [[CT0]] ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# ip route add 192.168.0.101 dev veth101.0&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Using a directly routed IPv4 with virtual Ethernet device ===&lt;br /&gt;
&lt;br /&gt;
==== Situation ====&lt;br /&gt;
Hardware Node (HN/CT0) has 192.168.0.1/24 with router 192.168.0.254.&lt;br /&gt;
&lt;br /&gt;
We also know that IPv4 10.0.0.1/32 is directly routed to 192.168.0.1 (this is called a ''fail-over IP'').&lt;br /&gt;
&lt;br /&gt;
We want to give this directly routed IPv4 address to a container (CT).&lt;br /&gt;
&lt;br /&gt;
==== Start container ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# vzctl start 101&lt;br /&gt;
&lt;br /&gt;
==== Add veth device to CT ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# vzctl set 101 --netif_add eth0 --save&lt;br /&gt;
&lt;br /&gt;
This allocates a MAC address and associates it with the host eth0 port.&lt;br /&gt;
&lt;br /&gt;
==== Configure device and add route in CT0 ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# ifconfig veth101.0 0&lt;br /&gt;
[host-node]# ip route add 10.0.0.1 dev veth101.0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can automatize this at VPS creation by using a mount script &amp;lt;tt&amp;gt;$VEID.mount&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The problem here is that the ''veth'' interface appears in CT0 '''after''' VPS has started, therefore we cannot directly use the commands in the mount script. We launch a shell script (enclosed by { }) in background (operator '''&amp;amp;''') that waits for the interface to be ready and then adds the IP route.&lt;br /&gt;
&lt;br /&gt;
Contents of the mount script &amp;lt;tt&amp;gt;/etc/vz/conf/101.mount&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
# This script source VPS configuration files in the same order as vzctl does&lt;br /&gt;
&lt;br /&gt;
# if one of these files does not exist then something is really broken&lt;br /&gt;
[ -f /etc/vz/vz.conf ] || exit 1&lt;br /&gt;
[ -f $VE_CONFFILE ] || exit 1&lt;br /&gt;
&lt;br /&gt;
# source both files. Note the order, it is important&lt;br /&gt;
. /etc/vz/vz.conf&lt;br /&gt;
. $VE_CONFFILE&lt;br /&gt;
&lt;br /&gt;
# Configure veth with IP after VPS has started&lt;br /&gt;
{&lt;br /&gt;
  IP=X.Y.Z.T&lt;br /&gt;
  DEV=veth101.0&lt;br /&gt;
  while sleep 1; do&lt;br /&gt;
    /sbin/ifconfig $DEV 0 &amp;gt;/dev/null 2&amp;gt;&amp;amp;1&lt;br /&gt;
    if [ $? -eq 0 ]; then&lt;br /&gt;
      /sbin/ip route add $IP dev $DEV&lt;br /&gt;
      break&lt;br /&gt;
    fi&lt;br /&gt;
  done&lt;br /&gt;
} &amp;amp;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Make sure IPv4 forwarding is enabled in CT0 ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/ip_forward&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/eth0/forwarding&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/veth101.0/forwarding&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
You can permanently set this by using &amp;lt;tt&amp;gt;/etc/sysctl.conf&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==== Configure device in CT ====&lt;br /&gt;
&lt;br /&gt;
1. Configure IP address&lt;br /&gt;
&lt;br /&gt;
2. Add gateway&lt;br /&gt;
&lt;br /&gt;
3. Add default route&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[ve-101]# /sbin/ifconfig eth0 10.0.0.1 netmask 255.255.255.255&lt;br /&gt;
[ve-101]# /sbin/ip route add 192.168.0.1 dev eth0&lt;br /&gt;
[ve-101]# /sbin/ip route add default via 192.168.0.1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In a Debian container, you can configure this permanently by using &amp;lt;tt&amp;gt;/etc/network/interfaces&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
auto eth0&lt;br /&gt;
iface eth0 inet static&lt;br /&gt;
        address 10.0.0.1&lt;br /&gt;
        netmask 255.255.255.255&lt;br /&gt;
        up /sbin/ip route add 192.168.0.1 dev eth0&lt;br /&gt;
        up /sbin/ip route add default via 192.168.0.1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Virtual Ethernet device with IPv6 ===&lt;br /&gt;
&lt;br /&gt;
See the [[VEs and HNs in same subnets]] article.&lt;br /&gt;
&lt;br /&gt;
=== Virtual Ethernet devices can be joined in one bridge ===&lt;br /&gt;
Perform steps 1 - 4 from Simple configuration chapter for several containers and/or veth devices&lt;br /&gt;
&lt;br /&gt;
==== Create bridge device ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# brctl addbr vzbr0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Add veth devices to bridge ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# brctl addif vzbr0 veth101.0&lt;br /&gt;
...&lt;br /&gt;
[host-node]# brctl addif vzbr0 veth101.n&lt;br /&gt;
[host-node]# brctl addif vzbr0 veth102.0&lt;br /&gt;
...&lt;br /&gt;
...&lt;br /&gt;
[host-node]# brctl addif vzbr0 vethXXX.N&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Configure bridge device ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# ifconfig vzbr0 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Add routes in [[CT0]] ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# ip route add 192.168.101.1 dev vzbr0&lt;br /&gt;
...&lt;br /&gt;
[host-node]# ip route add 192.168.101.n dev vzbr0&lt;br /&gt;
[host-node]# ip route add 192.168.102.1 dev vzbr0&lt;br /&gt;
...&lt;br /&gt;
...&lt;br /&gt;
[host-node]# ip route add 192.168.XXX.N dev vzbr0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Thus you'll have more convinient configuration, i.e. all routes to containers will be through this bridge and containers can communicate with each other even without these routes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Making a veth-device persistent ===&lt;br /&gt;
According to http://bugzilla.openvz.org/show_bug.cgi?id=301 , a bug that stopped the veth device persistent was &amp;quot;Obsoleted now when --veth_add/--veth_del are introduced&amp;quot;&lt;br /&gt;
&lt;br /&gt;
See http://wiki.openvz.org/w/index.php?title=Virtual_Ethernet_device&amp;amp;diff=5990&amp;amp;oldid=5989#Making_a_veth-device_persistent for a workaround that used to be described in this section.&lt;br /&gt;
&lt;br /&gt;
That's it! At this point, when you restart the CT you should see a new line in the output, indicating that the interface is being configured and a new route being added. And you should be able to ping the host, and to enter the CT and use the network.&lt;br /&gt;
&lt;br /&gt;
=== Making a bridged veth-device persistent ===&lt;br /&gt;
&lt;br /&gt;
Like the above example, here it is how to add the veth device to a bridge in a persistent way. &lt;br /&gt;
&lt;br /&gt;
vzctl include a 'vznetaddbr' script, which makes use of the ''bridge'' parameter of the --netif_add switch.&lt;br /&gt;
&lt;br /&gt;
Just create /etc/vz/vznet.conf containing the following.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
EXTERNAL_SCRIPT=&amp;quot;/usr/sbin/vznetaddbr&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The script uses 'vmbr0' as default bridge name when no bridge is specified.&lt;br /&gt;
&lt;br /&gt;
=== Virtual Ethernet devices + VLAN ===&lt;br /&gt;
This configuration can be done by adding vlan device to the previous configuration.&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
* [[Virtual network device]]&lt;br /&gt;
* [[Differences between venet and veth]]&lt;br /&gt;
* [[Using private IPs for Hardware Nodes]]&lt;br /&gt;
* Patch: [[Disable venet interface]]&lt;br /&gt;
* Troubleshooting: [[Bridge doesn't forward packets]]&lt;br /&gt;
&lt;br /&gt;
== External links ==&lt;br /&gt;
* [http://www.tldp.org/HOWTO/Linux+IPv6-HOWTO/hints-daemons-radvd.html Linux IPv6 HOWTO, a chapter about radvd]&lt;br /&gt;
* [http://sysadmin-ivanov.blogspot.com/2008/02/2-veth-with-2-bridges-on-openvz-at.html 2 veth with 2 bridges setup]&lt;br /&gt;
&lt;br /&gt;
[[Category: Networking]]&lt;br /&gt;
[[Category: HOWTO]]&lt;/div&gt;</summary>
		<author><name>Mose</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Virtual_Ethernet_device&amp;diff=11976</id>
		<title>Virtual Ethernet device</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Virtual_Ethernet_device&amp;diff=11976"/>
		<updated>2012-02-16T18:13:47Z</updated>

		<summary type="html">&lt;p&gt;Mose: /* Configure devices in CT0 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Virtual Ethernet device''' is an Ethernet-like device that can be used&lt;br /&gt;
inside a [[container]]. Unlike a [[venet]] network device, a [[veth]] device&lt;br /&gt;
has a MAC address. Therefore, it can be used in more configurations. When veth&lt;br /&gt;
is bridged to a [[CT0]] network interface (e.g., eth0), the container can act as an&lt;br /&gt;
independent host on the network. The container's user can set up all of the networking&lt;br /&gt;
himself, including IPs, gateways, etc.&lt;br /&gt;
&lt;br /&gt;
A virtual Ethernet device consist of two Ethernet devices,&lt;br /&gt;
one in [[CT0]] (e.g., vethN.0) and a corresponding one in CT (e.g., eth0) that are&lt;br /&gt;
connected to each other. If a packet is sent to one device it will come out the other device.&lt;br /&gt;
&lt;br /&gt;
== Virtual Ethernet device usage ==&lt;br /&gt;
&lt;br /&gt;
=== Kernel module ===&lt;br /&gt;
The &amp;lt;code&amp;gt;vzethdev&amp;lt;/code&amp;gt; module should be loaded. You can check it with the following commands.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# lsmod | grep vzeth&lt;br /&gt;
vzethdev                8224  0&lt;br /&gt;
vzmon                  35164  5 vzethdev,vznetdev,vzrst,vzcpt&lt;br /&gt;
vzdev                   3080  4 vzethdev,vznetdev,vzmon,vzdquota&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In case it is not loaded, load it:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# modprobe vzethdev&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== MAC addresses ===&lt;br /&gt;
The following steps to generate a MAC address are not necessary, since newer versions&lt;br /&gt;
of vzctl will automatically generate a MAC address for you. These steps are provided&lt;br /&gt;
in case you want to set a MAC address manually.&lt;br /&gt;
&lt;br /&gt;
You should use a random MAC address when adding a network interface to a container. Do not use MAC addresses of real eth devices, because this can lead to collisions.&lt;br /&gt;
&lt;br /&gt;
MAC addresses must be entered in XX:XX:XX:XX:XX:XX format.&lt;br /&gt;
&lt;br /&gt;
There is a utility script available for generating MAC addresses: http://www.easyvmx.com/software/easymac.sh. It is used like this:&lt;br /&gt;
&lt;br /&gt;
 chmod +x easymac.sh&lt;br /&gt;
 ./easymac.sh -R&lt;br /&gt;
&lt;br /&gt;
=== Adding veth to a CT ===&lt;br /&gt;
&lt;br /&gt;
 vzctl set &amp;lt;CTID&amp;gt; --netif_add &amp;lt;ifname&amp;gt;[,&amp;lt;mac&amp;gt;,&amp;lt;host_ifname&amp;gt;,&amp;lt;host_mac&amp;gt;,&amp;lt;bridge&amp;gt;]&lt;br /&gt;
&lt;br /&gt;
Here&lt;br /&gt;
* &amp;lt;tt&amp;gt;ifname&amp;lt;/tt&amp;gt; is the Ethernet device name in the CT&lt;br /&gt;
* &amp;lt;tt&amp;gt;mac&amp;lt;/tt&amp;gt; is its MAC address in the CT&lt;br /&gt;
* &amp;lt;tt&amp;gt;host_ifname&amp;lt;/tt&amp;gt;  is the Ethernet device name on the host ([[CT0]])&lt;br /&gt;
* &amp;lt;tt&amp;gt;host_mac&amp;lt;/tt&amp;gt; is its MAC address on the host ([[CT0]])&lt;br /&gt;
* &amp;lt;tt&amp;gt;bridge&amp;lt;/tt&amp;gt; is an optional parameter which can be used in custom network start scripts to automatically add  the interface to a bridge. (See the reference to the vznetaddbr script below and persistent bridge configurations.)&lt;br /&gt;
&lt;br /&gt;
{{Note|All parameters except &amp;lt;code&amp;gt;ifname&amp;lt;/code&amp;gt; are optional. Missing parameters, except for bridge, are automatically generated, if not specified. This is the preferred method.}}&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 vzctl set 101 --netif_add eth0 --save&lt;br /&gt;
&lt;br /&gt;
If you want to specify everything:&lt;br /&gt;
&lt;br /&gt;
 vzctl set 101 --netif_add eth0,00:12:34:56:78:9A,veth101.0,00:12:34:56:78:9B --save&lt;br /&gt;
&lt;br /&gt;
If you want to specify the bridge and autogenerate the other values:&lt;br /&gt;
&lt;br /&gt;
 vzctl set 101 --netif_add eth0,,,,vmbr1 --save&lt;br /&gt;
&lt;br /&gt;
=== Removing veth from a CT ===&lt;br /&gt;
&lt;br /&gt;
 vzctl set &amp;lt;CTID&amp;gt; --netif_del &amp;lt;dev_name&amp;gt;|all&lt;br /&gt;
&lt;br /&gt;
Here&lt;br /&gt;
* &amp;lt;code&amp;gt;dev_name&amp;lt;/code&amp;gt; is the Ethernet device name in the [[CT]].&lt;br /&gt;
&lt;br /&gt;
{{Note|If you want to remove all Ethernet devices in CT, use &amp;lt;code&amp;gt;all&amp;lt;/code&amp;gt;.}}&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 vzctl set 101 --netif_del eth0 --save&lt;br /&gt;
&lt;br /&gt;
== Common configurations with virtual Ethernet devices ==&lt;br /&gt;
Module &amp;lt;tt&amp;gt;vzethdev&amp;lt;/tt&amp;gt; must be loaded to operate with veth devices.&lt;br /&gt;
&lt;br /&gt;
=== Simple configuration with virtual Ethernet device ===&lt;br /&gt;
&lt;br /&gt;
Assuming you have 192.168.0.0/24 on your LAN, you will learn how to integrate a container in this LAN using veth.&lt;br /&gt;
&lt;br /&gt;
==== Start a CT ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# vzctl start 101&lt;br /&gt;
&lt;br /&gt;
==== Add veth device to CT ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# vzctl set 101 --netif_add eth0 --save&lt;br /&gt;
&lt;br /&gt;
This allocates a MAC address and associates it with the host eth0 port.&lt;br /&gt;
&lt;br /&gt;
==== Configure devices in CT0 ====&lt;br /&gt;
The following steps are needed when the [[CT]] is '''not''' bridged to a [[CT0]] interface. That is because the [[CT]] is connected to a virtual network that is &amp;quot;behind&amp;quot; [[CT0]]. [[CT0]] must forward packets between its physical network interface and the virtual network interface where [[CT]] is located. The first step below to configure the interface is not necessary if the container has been started, since the device will have been initialized.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# ifconfig veth101.0 0&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/veth101.0/forwarding&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/veth101.0/proxy_arp&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/eth0/forwarding&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/eth0/proxy_arp&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Configure device in CT ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# vzctl enter 101&lt;br /&gt;
[ve-101]# /sbin/ifconfig eth0 0&lt;br /&gt;
[ve-101]# /sbin/ip addr add 192.168.0.101 dev eth0&lt;br /&gt;
[ve-101]# /sbin/ip route add default dev eth0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
* Until you ifconfig eth0 it won't appear. When you do it will use the mac address netif_add added earlier&lt;br /&gt;
* 192.168.0.101 is chosen to be an [[unrouteable private ip address]]. Where 101 reminds you that it is node 101.&lt;br /&gt;
* The &amp;quot;ip route&amp;quot; tells all traffic to head to &amp;quot;device eth0&amp;quot;&lt;br /&gt;
* In theory you could [[use dhcpd with OpenVZ]] and dhclient to pick up an DHCP address from your router instead of hardwiring it&lt;br /&gt;
** http://openvz.org/pipermail/users/2005-November/000020.html&lt;br /&gt;
&lt;br /&gt;
==== Add route in [[CT0]] ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# ip route add 192.168.0.101 dev veth101.0&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Using a directly routed IPv4 with virtual Ethernet device ===&lt;br /&gt;
&lt;br /&gt;
==== Situation ====&lt;br /&gt;
Hardware Node (HN/CT0) has 192.168.0.1/24 with router 192.168.0.254.&lt;br /&gt;
&lt;br /&gt;
We also know that IPv4 10.0.0.1/32 is directly routed to 192.168.0.1 (this is called a ''fail-over IP'').&lt;br /&gt;
&lt;br /&gt;
We want to give this directly routed IPv4 address to a container (CT).&lt;br /&gt;
&lt;br /&gt;
==== Start container ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# vzctl start 101&lt;br /&gt;
&lt;br /&gt;
==== Add veth device to CT ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# vzctl set 101 --netif_add eth0 --save&lt;br /&gt;
&lt;br /&gt;
This allocates a MAC address and associates it with the host eth0 port.&lt;br /&gt;
&lt;br /&gt;
==== Configure device and add route in CT0 ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# ifconfig veth101.0 0&lt;br /&gt;
[host-node]# ip route add 10.0.0.1 dev veth101.0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can automatize this at VPS creation by using a mount script &amp;lt;tt&amp;gt;$VEID.mount&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The problem here is that the ''veth'' interface appears in CT0 '''after''' VPS has started, therefore we cannot directly use the commands in the mount script. We launch a shell script (enclosed by { }) in background (operator '''&amp;amp;''') that waits for the interface to be ready and then adds the IP route.&lt;br /&gt;
&lt;br /&gt;
Contents of the mount script &amp;lt;tt&amp;gt;/etc/vz/conf/101.mount&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
# This script source VPS configuration files in the same order as vzctl does&lt;br /&gt;
&lt;br /&gt;
# if one of these files does not exist then something is really broken&lt;br /&gt;
[ -f /etc/vz/vz.conf ] || exit 1&lt;br /&gt;
[ -f $VE_CONFFILE ] || exit 1&lt;br /&gt;
&lt;br /&gt;
# source both files. Note the order, it is important&lt;br /&gt;
. /etc/vz/vz.conf&lt;br /&gt;
. $VE_CONFFILE&lt;br /&gt;
&lt;br /&gt;
# Configure veth with IP after VPS has started&lt;br /&gt;
{&lt;br /&gt;
  IP=X.Y.Z.T&lt;br /&gt;
  DEV=veth101.0&lt;br /&gt;
  while sleep 1; do&lt;br /&gt;
    /sbin/ifconfig $DEV 0 &amp;gt;/dev/null 2&amp;gt;&amp;amp;1&lt;br /&gt;
    if [ $? -eq 0 ]; then&lt;br /&gt;
      /sbin/ip route add $IP dev $DEV&lt;br /&gt;
      break&lt;br /&gt;
    fi&lt;br /&gt;
  done&lt;br /&gt;
} &amp;amp;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Make sure IPv4 forwarding is enabled in CT0 ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/ip_forward&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/eth0/forwarding&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/veth101.0/forwarding&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
You can permanently set this by using &amp;lt;tt&amp;gt;/etc/sysctl.conf&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==== Configure device in CT ====&lt;br /&gt;
&lt;br /&gt;
1. Configure IP address&lt;br /&gt;
&lt;br /&gt;
2. Add gateway&lt;br /&gt;
&lt;br /&gt;
3. Add default route&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[ve-101]# /sbin/ifconfig eth0 10.0.0.1 netmask 255.255.255.255&lt;br /&gt;
[ve-101]# /sbin/ip route add 192.168.0.1 dev eth0&lt;br /&gt;
[ve-101]# /sbin/ip route add default via 192.168.0.1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In a Debian container, you can configure this permanently by using &amp;lt;tt&amp;gt;/etc/network/interfaces&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
auto eth0&lt;br /&gt;
iface eth0 inet static&lt;br /&gt;
        address 10.0.0.1&lt;br /&gt;
        netmask 255.255.255.255&lt;br /&gt;
        up /sbin/ip route add 192.168.0.1 dev eth0&lt;br /&gt;
        up /sbin/ip route add default via 192.168.0.1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Virtual Ethernet device with IPv6 ===&lt;br /&gt;
&lt;br /&gt;
See the [[VEs and HNs in same subnets]] article.&lt;br /&gt;
&lt;br /&gt;
=== Virtual Ethernet devices can be joined in one bridge ===&lt;br /&gt;
Perform steps 1 - 4 from Simple configuration chapter for several containers and/or veth devices&lt;br /&gt;
&lt;br /&gt;
==== Create bridge device ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# brctl addbr vzbr0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Add veth devices to bridge ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# brctl addif vzbr0 veth101.0&lt;br /&gt;
...&lt;br /&gt;
[host-node]# brctl addif vzbr0 veth101.n&lt;br /&gt;
[host-node]# brctl addif vzbr0 veth102.0&lt;br /&gt;
...&lt;br /&gt;
...&lt;br /&gt;
[host-node]# brctl addif vzbr0 vethXXX.N&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Configure bridge device ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# ifconfig vzbr0 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Add routes in [[CT0]] ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# ip route add 192.168.101.1 dev vzbr0&lt;br /&gt;
...&lt;br /&gt;
[host-node]# ip route add 192.168.101.n dev vzbr0&lt;br /&gt;
[host-node]# ip route add 192.168.102.1 dev vzbr0&lt;br /&gt;
...&lt;br /&gt;
...&lt;br /&gt;
[host-node]# ip route add 192.168.XXX.N dev vzbr0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Thus you'll have more convinient configuration, i.e. all routes to containers will be through this bridge and containers can communicate with each other even without these routes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Making a veth-device persistent ===&lt;br /&gt;
According to http://bugzilla.openvz.org/show_bug.cgi?id=301 , a bug that stopped the veth device persistent was &amp;quot;Obsoleted now when --veth_add/--veth_del are introduced&amp;quot;&lt;br /&gt;
&lt;br /&gt;
See http://wiki.openvz.org/w/index.php?title=Virtual_Ethernet_device&amp;amp;diff=5990&amp;amp;oldid=5989#Making_a_veth-device_persistent for a workaround that used to be described in this section.&lt;br /&gt;
&lt;br /&gt;
That's it! At this point, when you restart the CT you should see a new line in the output, indicating that the interface is being configured and a new route being added. And you should be able to ping the host, and to enter the CT and use the network.&lt;br /&gt;
&lt;br /&gt;
=== Making a bridged veth-device persistent ===&lt;br /&gt;
&lt;br /&gt;
Like the above example, here it is how to add the veth device to a bridge in a persistent way. &lt;br /&gt;
&lt;br /&gt;
vzctl include a 'vznetaddbr' script, which makes use of the ''bridge'' parameter of the --netif_add switch.&lt;br /&gt;
&lt;br /&gt;
Just create /etc/vz/vznet.conf containing the following.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
EXTERNAL_SCRIPT=&amp;quot;/usr/sbin/vznetaddbr&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The script uses 'vmbr0' as default bridge name when no bridge is specified.&lt;br /&gt;
&lt;br /&gt;
=== Virtual Ethernet devices + VLAN ===&lt;br /&gt;
This configuration can be done by adding vlan device to the previous configuration.&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
* [[Virtual network device]]&lt;br /&gt;
* [[Differences between venet and veth]]&lt;br /&gt;
* [[Using private IPs for Hardware Nodes]]&lt;br /&gt;
* Patch: [[Disable venet interface]]&lt;br /&gt;
* Troubleshooting: [[Bridge doesn't forward packets]]&lt;br /&gt;
&lt;br /&gt;
== External links ==&lt;br /&gt;
* [http://www.tldp.org/HOWTO/Linux+IPv6-HOWTO/hints-daemons-radvd.html Linux IPv6 HOWTO, a chapter about radvd]&lt;br /&gt;
* [http://sysadmin-ivanov.blogspot.com/2008/02/2-veth-with-2-bridges-on-openvz-at.html 2 veth with 2 bridges setup]&lt;br /&gt;
&lt;br /&gt;
[[Category: Networking]]&lt;br /&gt;
[[Category: HOWTO]]&lt;/div&gt;</summary>
		<author><name>Mose</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Virtual_Ethernet_device&amp;diff=11975</id>
		<title>Virtual Ethernet device</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Virtual_Ethernet_device&amp;diff=11975"/>
		<updated>2012-02-16T17:52:08Z</updated>

		<summary type="html">&lt;p&gt;Mose: /* Removing veth from a CT */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Virtual Ethernet device''' is an Ethernet-like device that can be used&lt;br /&gt;
inside a [[container]]. Unlike a [[venet]] network device, a [[veth]] device&lt;br /&gt;
has a MAC address. Therefore, it can be used in more configurations. When veth&lt;br /&gt;
is bridged to a [[CT0]] network interface (e.g., eth0), the container can act as an&lt;br /&gt;
independent host on the network. The container's user can set up all of the networking&lt;br /&gt;
himself, including IPs, gateways, etc.&lt;br /&gt;
&lt;br /&gt;
A virtual Ethernet device consist of two Ethernet devices,&lt;br /&gt;
one in [[CT0]] (e.g., vethN.0) and a corresponding one in CT (e.g., eth0) that are&lt;br /&gt;
connected to each other. If a packet is sent to one device it will come out the other device.&lt;br /&gt;
&lt;br /&gt;
== Virtual Ethernet device usage ==&lt;br /&gt;
&lt;br /&gt;
=== Kernel module ===&lt;br /&gt;
The &amp;lt;code&amp;gt;vzethdev&amp;lt;/code&amp;gt; module should be loaded. You can check it with the following commands.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# lsmod | grep vzeth&lt;br /&gt;
vzethdev                8224  0&lt;br /&gt;
vzmon                  35164  5 vzethdev,vznetdev,vzrst,vzcpt&lt;br /&gt;
vzdev                   3080  4 vzethdev,vznetdev,vzmon,vzdquota&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In case it is not loaded, load it:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# modprobe vzethdev&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== MAC addresses ===&lt;br /&gt;
The following steps to generate a MAC address are not necessary, since newer versions&lt;br /&gt;
of vzctl will automatically generate a MAC address for you. These steps are provided&lt;br /&gt;
in case you want to set a MAC address manually.&lt;br /&gt;
&lt;br /&gt;
You should use a random MAC address when adding a network interface to a container. Do not use MAC addresses of real eth devices, because this can lead to collisions.&lt;br /&gt;
&lt;br /&gt;
MAC addresses must be entered in XX:XX:XX:XX:XX:XX format.&lt;br /&gt;
&lt;br /&gt;
There is a utility script available for generating MAC addresses: http://www.easyvmx.com/software/easymac.sh. It is used like this:&lt;br /&gt;
&lt;br /&gt;
 chmod +x easymac.sh&lt;br /&gt;
 ./easymac.sh -R&lt;br /&gt;
&lt;br /&gt;
=== Adding veth to a CT ===&lt;br /&gt;
&lt;br /&gt;
 vzctl set &amp;lt;CTID&amp;gt; --netif_add &amp;lt;ifname&amp;gt;[,&amp;lt;mac&amp;gt;,&amp;lt;host_ifname&amp;gt;,&amp;lt;host_mac&amp;gt;,&amp;lt;bridge&amp;gt;]&lt;br /&gt;
&lt;br /&gt;
Here&lt;br /&gt;
* &amp;lt;tt&amp;gt;ifname&amp;lt;/tt&amp;gt; is the Ethernet device name in the CT&lt;br /&gt;
* &amp;lt;tt&amp;gt;mac&amp;lt;/tt&amp;gt; is its MAC address in the CT&lt;br /&gt;
* &amp;lt;tt&amp;gt;host_ifname&amp;lt;/tt&amp;gt;  is the Ethernet device name on the host ([[CT0]])&lt;br /&gt;
* &amp;lt;tt&amp;gt;host_mac&amp;lt;/tt&amp;gt; is its MAC address on the host ([[CT0]])&lt;br /&gt;
* &amp;lt;tt&amp;gt;bridge&amp;lt;/tt&amp;gt; is an optional parameter which can be used in custom network start scripts to automatically add  the interface to a bridge. (See the reference to the vznetaddbr script below and persistent bridge configurations.)&lt;br /&gt;
&lt;br /&gt;
{{Note|All parameters except &amp;lt;code&amp;gt;ifname&amp;lt;/code&amp;gt; are optional. Missing parameters, except for bridge, are automatically generated, if not specified. This is the preferred method.}}&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 vzctl set 101 --netif_add eth0 --save&lt;br /&gt;
&lt;br /&gt;
If you want to specify everything:&lt;br /&gt;
&lt;br /&gt;
 vzctl set 101 --netif_add eth0,00:12:34:56:78:9A,veth101.0,00:12:34:56:78:9B --save&lt;br /&gt;
&lt;br /&gt;
If you want to specify the bridge and autogenerate the other values:&lt;br /&gt;
&lt;br /&gt;
 vzctl set 101 --netif_add eth0,,,,vmbr1 --save&lt;br /&gt;
&lt;br /&gt;
=== Removing veth from a CT ===&lt;br /&gt;
&lt;br /&gt;
 vzctl set &amp;lt;CTID&amp;gt; --netif_del &amp;lt;dev_name&amp;gt;|all&lt;br /&gt;
&lt;br /&gt;
Here&lt;br /&gt;
* &amp;lt;code&amp;gt;dev_name&amp;lt;/code&amp;gt; is the Ethernet device name in the [[CT]].&lt;br /&gt;
&lt;br /&gt;
{{Note|If you want to remove all Ethernet devices in CT, use &amp;lt;code&amp;gt;all&amp;lt;/code&amp;gt;.}}&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 vzctl set 101 --netif_del eth0 --save&lt;br /&gt;
&lt;br /&gt;
== Common configurations with virtual Ethernet devices ==&lt;br /&gt;
Module &amp;lt;tt&amp;gt;vzethdev&amp;lt;/tt&amp;gt; must be loaded to operate with veth devices.&lt;br /&gt;
&lt;br /&gt;
=== Simple configuration with virtual Ethernet device ===&lt;br /&gt;
&lt;br /&gt;
Assuming you have 192.168.0.0/24 on your LAN, you will learn how to integrate a container in this LAN using veth.&lt;br /&gt;
&lt;br /&gt;
==== Start a CT ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# vzctl start 101&lt;br /&gt;
&lt;br /&gt;
==== Add veth device to CT ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# vzctl set 101 --netif_add eth0 --save&lt;br /&gt;
&lt;br /&gt;
This allocates a MAC address and associates it with the host eth0 port.&lt;br /&gt;
&lt;br /&gt;
==== Configure devices in CT0 ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# ifconfig veth101.0 0&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/veth101.0/forwarding&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/veth101.0/proxy_arp&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/eth0/forwarding&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/eth0/proxy_arp&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Configure device in CT ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# vzctl enter 101&lt;br /&gt;
[ve-101]# /sbin/ifconfig eth0 0&lt;br /&gt;
[ve-101]# /sbin/ip addr add 192.168.0.101 dev eth0&lt;br /&gt;
[ve-101]# /sbin/ip route add default dev eth0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
* Until you ifconfig eth0 it won't appear. When you do it will use the mac address netif_add added earlier&lt;br /&gt;
* 192.168.0.101 is chosen to be an [[unrouteable private ip address]]. Where 101 reminds you that it is node 101.&lt;br /&gt;
* The &amp;quot;ip route&amp;quot; tells all traffic to head to &amp;quot;device eth0&amp;quot;&lt;br /&gt;
* In theory you could [[use dhcpd with OpenVZ]] and dhclient to pick up an DHCP address from your router instead of hardwiring it&lt;br /&gt;
** http://openvz.org/pipermail/users/2005-November/000020.html&lt;br /&gt;
&lt;br /&gt;
==== Add route in [[CT0]] ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# ip route add 192.168.0.101 dev veth101.0&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Using a directly routed IPv4 with virtual Ethernet device ===&lt;br /&gt;
&lt;br /&gt;
==== Situation ====&lt;br /&gt;
Hardware Node (HN/CT0) has 192.168.0.1/24 with router 192.168.0.254.&lt;br /&gt;
&lt;br /&gt;
We also know that IPv4 10.0.0.1/32 is directly routed to 192.168.0.1 (this is called a ''fail-over IP'').&lt;br /&gt;
&lt;br /&gt;
We want to give this directly routed IPv4 address to a container (CT).&lt;br /&gt;
&lt;br /&gt;
==== Start container ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# vzctl start 101&lt;br /&gt;
&lt;br /&gt;
==== Add veth device to CT ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# vzctl set 101 --netif_add eth0 --save&lt;br /&gt;
&lt;br /&gt;
This allocates a MAC address and associates it with the host eth0 port.&lt;br /&gt;
&lt;br /&gt;
==== Configure device and add route in CT0 ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# ifconfig veth101.0 0&lt;br /&gt;
[host-node]# ip route add 10.0.0.1 dev veth101.0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can automatize this at VPS creation by using a mount script &amp;lt;tt&amp;gt;$VEID.mount&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The problem here is that the ''veth'' interface appears in CT0 '''after''' VPS has started, therefore we cannot directly use the commands in the mount script. We launch a shell script (enclosed by { }) in background (operator '''&amp;amp;''') that waits for the interface to be ready and then adds the IP route.&lt;br /&gt;
&lt;br /&gt;
Contents of the mount script &amp;lt;tt&amp;gt;/etc/vz/conf/101.mount&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
# This script source VPS configuration files in the same order as vzctl does&lt;br /&gt;
&lt;br /&gt;
# if one of these files does not exist then something is really broken&lt;br /&gt;
[ -f /etc/vz/vz.conf ] || exit 1&lt;br /&gt;
[ -f $VE_CONFFILE ] || exit 1&lt;br /&gt;
&lt;br /&gt;
# source both files. Note the order, it is important&lt;br /&gt;
. /etc/vz/vz.conf&lt;br /&gt;
. $VE_CONFFILE&lt;br /&gt;
&lt;br /&gt;
# Configure veth with IP after VPS has started&lt;br /&gt;
{&lt;br /&gt;
  IP=X.Y.Z.T&lt;br /&gt;
  DEV=veth101.0&lt;br /&gt;
  while sleep 1; do&lt;br /&gt;
    /sbin/ifconfig $DEV 0 &amp;gt;/dev/null 2&amp;gt;&amp;amp;1&lt;br /&gt;
    if [ $? -eq 0 ]; then&lt;br /&gt;
      /sbin/ip route add $IP dev $DEV&lt;br /&gt;
      break&lt;br /&gt;
    fi&lt;br /&gt;
  done&lt;br /&gt;
} &amp;amp;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Make sure IPv4 forwarding is enabled in CT0 ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/ip_forward&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/eth0/forwarding&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/veth101.0/forwarding&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
You can permanently set this by using &amp;lt;tt&amp;gt;/etc/sysctl.conf&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==== Configure device in CT ====&lt;br /&gt;
&lt;br /&gt;
1. Configure IP address&lt;br /&gt;
&lt;br /&gt;
2. Add gateway&lt;br /&gt;
&lt;br /&gt;
3. Add default route&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[ve-101]# /sbin/ifconfig eth0 10.0.0.1 netmask 255.255.255.255&lt;br /&gt;
[ve-101]# /sbin/ip route add 192.168.0.1 dev eth0&lt;br /&gt;
[ve-101]# /sbin/ip route add default via 192.168.0.1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In a Debian container, you can configure this permanently by using &amp;lt;tt&amp;gt;/etc/network/interfaces&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
auto eth0&lt;br /&gt;
iface eth0 inet static&lt;br /&gt;
        address 10.0.0.1&lt;br /&gt;
        netmask 255.255.255.255&lt;br /&gt;
        up /sbin/ip route add 192.168.0.1 dev eth0&lt;br /&gt;
        up /sbin/ip route add default via 192.168.0.1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Virtual Ethernet device with IPv6 ===&lt;br /&gt;
&lt;br /&gt;
See the [[VEs and HNs in same subnets]] article.&lt;br /&gt;
&lt;br /&gt;
=== Virtual Ethernet devices can be joined in one bridge ===&lt;br /&gt;
Perform steps 1 - 4 from Simple configuration chapter for several containers and/or veth devices&lt;br /&gt;
&lt;br /&gt;
==== Create bridge device ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# brctl addbr vzbr0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Add veth devices to bridge ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# brctl addif vzbr0 veth101.0&lt;br /&gt;
...&lt;br /&gt;
[host-node]# brctl addif vzbr0 veth101.n&lt;br /&gt;
[host-node]# brctl addif vzbr0 veth102.0&lt;br /&gt;
...&lt;br /&gt;
...&lt;br /&gt;
[host-node]# brctl addif vzbr0 vethXXX.N&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Configure bridge device ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# ifconfig vzbr0 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Add routes in [[CT0]] ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# ip route add 192.168.101.1 dev vzbr0&lt;br /&gt;
...&lt;br /&gt;
[host-node]# ip route add 192.168.101.n dev vzbr0&lt;br /&gt;
[host-node]# ip route add 192.168.102.1 dev vzbr0&lt;br /&gt;
...&lt;br /&gt;
...&lt;br /&gt;
[host-node]# ip route add 192.168.XXX.N dev vzbr0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Thus you'll have more convinient configuration, i.e. all routes to containers will be through this bridge and containers can communicate with each other even without these routes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Making a veth-device persistent ===&lt;br /&gt;
According to http://bugzilla.openvz.org/show_bug.cgi?id=301 , a bug that stopped the veth device persistent was &amp;quot;Obsoleted now when --veth_add/--veth_del are introduced&amp;quot;&lt;br /&gt;
&lt;br /&gt;
See http://wiki.openvz.org/w/index.php?title=Virtual_Ethernet_device&amp;amp;diff=5990&amp;amp;oldid=5989#Making_a_veth-device_persistent for a workaround that used to be described in this section.&lt;br /&gt;
&lt;br /&gt;
That's it! At this point, when you restart the CT you should see a new line in the output, indicating that the interface is being configured and a new route being added. And you should be able to ping the host, and to enter the CT and use the network.&lt;br /&gt;
&lt;br /&gt;
=== Making a bridged veth-device persistent ===&lt;br /&gt;
&lt;br /&gt;
Like the above example, here it is how to add the veth device to a bridge in a persistent way. &lt;br /&gt;
&lt;br /&gt;
vzctl include a 'vznetaddbr' script, which makes use of the ''bridge'' parameter of the --netif_add switch.&lt;br /&gt;
&lt;br /&gt;
Just create /etc/vz/vznet.conf containing the following.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
EXTERNAL_SCRIPT=&amp;quot;/usr/sbin/vznetaddbr&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The script uses 'vmbr0' as default bridge name when no bridge is specified.&lt;br /&gt;
&lt;br /&gt;
=== Virtual Ethernet devices + VLAN ===&lt;br /&gt;
This configuration can be done by adding vlan device to the previous configuration.&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
* [[Virtual network device]]&lt;br /&gt;
* [[Differences between venet and veth]]&lt;br /&gt;
* [[Using private IPs for Hardware Nodes]]&lt;br /&gt;
* Patch: [[Disable venet interface]]&lt;br /&gt;
* Troubleshooting: [[Bridge doesn't forward packets]]&lt;br /&gt;
&lt;br /&gt;
== External links ==&lt;br /&gt;
* [http://www.tldp.org/HOWTO/Linux+IPv6-HOWTO/hints-daemons-radvd.html Linux IPv6 HOWTO, a chapter about radvd]&lt;br /&gt;
* [http://sysadmin-ivanov.blogspot.com/2008/02/2-veth-with-2-bridges-on-openvz-at.html 2 veth with 2 bridges setup]&lt;br /&gt;
&lt;br /&gt;
[[Category: Networking]]&lt;br /&gt;
[[Category: HOWTO]]&lt;/div&gt;</summary>
		<author><name>Mose</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Virtual_Ethernet_device&amp;diff=11974</id>
		<title>Virtual Ethernet device</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Virtual_Ethernet_device&amp;diff=11974"/>
		<updated>2012-02-16T17:51:17Z</updated>

		<summary type="html">&lt;p&gt;Mose: /* Removing veth from a CT */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Virtual Ethernet device''' is an Ethernet-like device that can be used&lt;br /&gt;
inside a [[container]]. Unlike a [[venet]] network device, a [[veth]] device&lt;br /&gt;
has a MAC address. Therefore, it can be used in more configurations. When veth&lt;br /&gt;
is bridged to a [[CT0]] network interface (e.g., eth0), the container can act as an&lt;br /&gt;
independent host on the network. The container's user can set up all of the networking&lt;br /&gt;
himself, including IPs, gateways, etc.&lt;br /&gt;
&lt;br /&gt;
A virtual Ethernet device consist of two Ethernet devices,&lt;br /&gt;
one in [[CT0]] (e.g., vethN.0) and a corresponding one in CT (e.g., eth0) that are&lt;br /&gt;
connected to each other. If a packet is sent to one device it will come out the other device.&lt;br /&gt;
&lt;br /&gt;
== Virtual Ethernet device usage ==&lt;br /&gt;
&lt;br /&gt;
=== Kernel module ===&lt;br /&gt;
The &amp;lt;code&amp;gt;vzethdev&amp;lt;/code&amp;gt; module should be loaded. You can check it with the following commands.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# lsmod | grep vzeth&lt;br /&gt;
vzethdev                8224  0&lt;br /&gt;
vzmon                  35164  5 vzethdev,vznetdev,vzrst,vzcpt&lt;br /&gt;
vzdev                   3080  4 vzethdev,vznetdev,vzmon,vzdquota&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In case it is not loaded, load it:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# modprobe vzethdev&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== MAC addresses ===&lt;br /&gt;
The following steps to generate a MAC address are not necessary, since newer versions&lt;br /&gt;
of vzctl will automatically generate a MAC address for you. These steps are provided&lt;br /&gt;
in case you want to set a MAC address manually.&lt;br /&gt;
&lt;br /&gt;
You should use a random MAC address when adding a network interface to a container. Do not use MAC addresses of real eth devices, because this can lead to collisions.&lt;br /&gt;
&lt;br /&gt;
MAC addresses must be entered in XX:XX:XX:XX:XX:XX format.&lt;br /&gt;
&lt;br /&gt;
There is a utility script available for generating MAC addresses: http://www.easyvmx.com/software/easymac.sh. It is used like this:&lt;br /&gt;
&lt;br /&gt;
 chmod +x easymac.sh&lt;br /&gt;
 ./easymac.sh -R&lt;br /&gt;
&lt;br /&gt;
=== Adding veth to a CT ===&lt;br /&gt;
&lt;br /&gt;
 vzctl set &amp;lt;CTID&amp;gt; --netif_add &amp;lt;ifname&amp;gt;[,&amp;lt;mac&amp;gt;,&amp;lt;host_ifname&amp;gt;,&amp;lt;host_mac&amp;gt;,&amp;lt;bridge&amp;gt;]&lt;br /&gt;
&lt;br /&gt;
Here&lt;br /&gt;
* &amp;lt;tt&amp;gt;ifname&amp;lt;/tt&amp;gt; is the Ethernet device name in the CT&lt;br /&gt;
* &amp;lt;tt&amp;gt;mac&amp;lt;/tt&amp;gt; is its MAC address in the CT&lt;br /&gt;
* &amp;lt;tt&amp;gt;host_ifname&amp;lt;/tt&amp;gt;  is the Ethernet device name on the host ([[CT0]])&lt;br /&gt;
* &amp;lt;tt&amp;gt;host_mac&amp;lt;/tt&amp;gt; is its MAC address on the host ([[CT0]])&lt;br /&gt;
* &amp;lt;tt&amp;gt;bridge&amp;lt;/tt&amp;gt; is an optional parameter which can be used in custom network start scripts to automatically add  the interface to a bridge. (See the reference to the vznetaddbr script below and persistent bridge configurations.)&lt;br /&gt;
&lt;br /&gt;
{{Note|All parameters except &amp;lt;code&amp;gt;ifname&amp;lt;/code&amp;gt; are optional. Missing parameters, except for bridge, are automatically generated, if not specified. This is the preferred method.}}&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 vzctl set 101 --netif_add eth0 --save&lt;br /&gt;
&lt;br /&gt;
If you want to specify everything:&lt;br /&gt;
&lt;br /&gt;
 vzctl set 101 --netif_add eth0,00:12:34:56:78:9A,veth101.0,00:12:34:56:78:9B --save&lt;br /&gt;
&lt;br /&gt;
If you want to specify the bridge and autogenerate the other values:&lt;br /&gt;
&lt;br /&gt;
 vzctl set 101 --netif_add eth0,,,,vmbr1 --save&lt;br /&gt;
&lt;br /&gt;
=== Removing veth from a CT ===&lt;br /&gt;
&lt;br /&gt;
 vzctl set &amp;lt;CTID&amp;gt; --netif_del &amp;lt;dev_name&amp;gt;|all&lt;br /&gt;
&lt;br /&gt;
Here&lt;br /&gt;
* &amp;lt;code&amp;gt;dev_name&amp;lt;/code&amp;gt; is the Ethernet device name in the [[CT]], and it is typically eth0.&lt;br /&gt;
&lt;br /&gt;
{{Note|If you want to remove all Ethernet devices in CT, use &amp;lt;code&amp;gt;all&amp;lt;/code&amp;gt;.}}&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 vzctl set 101 --netif_del eth0 --save&lt;br /&gt;
&lt;br /&gt;
== Common configurations with virtual Ethernet devices ==&lt;br /&gt;
Module &amp;lt;tt&amp;gt;vzethdev&amp;lt;/tt&amp;gt; must be loaded to operate with veth devices.&lt;br /&gt;
&lt;br /&gt;
=== Simple configuration with virtual Ethernet device ===&lt;br /&gt;
&lt;br /&gt;
Assuming you have 192.168.0.0/24 on your LAN, you will learn how to integrate a container in this LAN using veth.&lt;br /&gt;
&lt;br /&gt;
==== Start a CT ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# vzctl start 101&lt;br /&gt;
&lt;br /&gt;
==== Add veth device to CT ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# vzctl set 101 --netif_add eth0 --save&lt;br /&gt;
&lt;br /&gt;
This allocates a MAC address and associates it with the host eth0 port.&lt;br /&gt;
&lt;br /&gt;
==== Configure devices in CT0 ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# ifconfig veth101.0 0&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/veth101.0/forwarding&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/veth101.0/proxy_arp&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/eth0/forwarding&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/eth0/proxy_arp&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Configure device in CT ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# vzctl enter 101&lt;br /&gt;
[ve-101]# /sbin/ifconfig eth0 0&lt;br /&gt;
[ve-101]# /sbin/ip addr add 192.168.0.101 dev eth0&lt;br /&gt;
[ve-101]# /sbin/ip route add default dev eth0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
* Until you ifconfig eth0 it won't appear. When you do it will use the mac address netif_add added earlier&lt;br /&gt;
* 192.168.0.101 is chosen to be an [[unrouteable private ip address]]. Where 101 reminds you that it is node 101.&lt;br /&gt;
* The &amp;quot;ip route&amp;quot; tells all traffic to head to &amp;quot;device eth0&amp;quot;&lt;br /&gt;
* In theory you could [[use dhcpd with OpenVZ]] and dhclient to pick up an DHCP address from your router instead of hardwiring it&lt;br /&gt;
** http://openvz.org/pipermail/users/2005-November/000020.html&lt;br /&gt;
&lt;br /&gt;
==== Add route in [[CT0]] ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# ip route add 192.168.0.101 dev veth101.0&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Using a directly routed IPv4 with virtual Ethernet device ===&lt;br /&gt;
&lt;br /&gt;
==== Situation ====&lt;br /&gt;
Hardware Node (HN/CT0) has 192.168.0.1/24 with router 192.168.0.254.&lt;br /&gt;
&lt;br /&gt;
We also know that IPv4 10.0.0.1/32 is directly routed to 192.168.0.1 (this is called a ''fail-over IP'').&lt;br /&gt;
&lt;br /&gt;
We want to give this directly routed IPv4 address to a container (CT).&lt;br /&gt;
&lt;br /&gt;
==== Start container ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# vzctl start 101&lt;br /&gt;
&lt;br /&gt;
==== Add veth device to CT ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# vzctl set 101 --netif_add eth0 --save&lt;br /&gt;
&lt;br /&gt;
This allocates a MAC address and associates it with the host eth0 port.&lt;br /&gt;
&lt;br /&gt;
==== Configure device and add route in CT0 ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# ifconfig veth101.0 0&lt;br /&gt;
[host-node]# ip route add 10.0.0.1 dev veth101.0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can automatize this at VPS creation by using a mount script &amp;lt;tt&amp;gt;$VEID.mount&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The problem here is that the ''veth'' interface appears in CT0 '''after''' VPS has started, therefore we cannot directly use the commands in the mount script. We launch a shell script (enclosed by { }) in background (operator '''&amp;amp;''') that waits for the interface to be ready and then adds the IP route.&lt;br /&gt;
&lt;br /&gt;
Contents of the mount script &amp;lt;tt&amp;gt;/etc/vz/conf/101.mount&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
# This script source VPS configuration files in the same order as vzctl does&lt;br /&gt;
&lt;br /&gt;
# if one of these files does not exist then something is really broken&lt;br /&gt;
[ -f /etc/vz/vz.conf ] || exit 1&lt;br /&gt;
[ -f $VE_CONFFILE ] || exit 1&lt;br /&gt;
&lt;br /&gt;
# source both files. Note the order, it is important&lt;br /&gt;
. /etc/vz/vz.conf&lt;br /&gt;
. $VE_CONFFILE&lt;br /&gt;
&lt;br /&gt;
# Configure veth with IP after VPS has started&lt;br /&gt;
{&lt;br /&gt;
  IP=X.Y.Z.T&lt;br /&gt;
  DEV=veth101.0&lt;br /&gt;
  while sleep 1; do&lt;br /&gt;
    /sbin/ifconfig $DEV 0 &amp;gt;/dev/null 2&amp;gt;&amp;amp;1&lt;br /&gt;
    if [ $? -eq 0 ]; then&lt;br /&gt;
      /sbin/ip route add $IP dev $DEV&lt;br /&gt;
      break&lt;br /&gt;
    fi&lt;br /&gt;
  done&lt;br /&gt;
} &amp;amp;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Make sure IPv4 forwarding is enabled in CT0 ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/ip_forward&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/eth0/forwarding&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/veth101.0/forwarding&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
You can permanently set this by using &amp;lt;tt&amp;gt;/etc/sysctl.conf&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==== Configure device in CT ====&lt;br /&gt;
&lt;br /&gt;
1. Configure IP address&lt;br /&gt;
&lt;br /&gt;
2. Add gateway&lt;br /&gt;
&lt;br /&gt;
3. Add default route&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[ve-101]# /sbin/ifconfig eth0 10.0.0.1 netmask 255.255.255.255&lt;br /&gt;
[ve-101]# /sbin/ip route add 192.168.0.1 dev eth0&lt;br /&gt;
[ve-101]# /sbin/ip route add default via 192.168.0.1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In a Debian container, you can configure this permanently by using &amp;lt;tt&amp;gt;/etc/network/interfaces&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
auto eth0&lt;br /&gt;
iface eth0 inet static&lt;br /&gt;
        address 10.0.0.1&lt;br /&gt;
        netmask 255.255.255.255&lt;br /&gt;
        up /sbin/ip route add 192.168.0.1 dev eth0&lt;br /&gt;
        up /sbin/ip route add default via 192.168.0.1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Virtual Ethernet device with IPv6 ===&lt;br /&gt;
&lt;br /&gt;
See the [[VEs and HNs in same subnets]] article.&lt;br /&gt;
&lt;br /&gt;
=== Virtual Ethernet devices can be joined in one bridge ===&lt;br /&gt;
Perform steps 1 - 4 from Simple configuration chapter for several containers and/or veth devices&lt;br /&gt;
&lt;br /&gt;
==== Create bridge device ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# brctl addbr vzbr0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Add veth devices to bridge ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# brctl addif vzbr0 veth101.0&lt;br /&gt;
...&lt;br /&gt;
[host-node]# brctl addif vzbr0 veth101.n&lt;br /&gt;
[host-node]# brctl addif vzbr0 veth102.0&lt;br /&gt;
...&lt;br /&gt;
...&lt;br /&gt;
[host-node]# brctl addif vzbr0 vethXXX.N&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Configure bridge device ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# ifconfig vzbr0 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Add routes in [[CT0]] ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# ip route add 192.168.101.1 dev vzbr0&lt;br /&gt;
...&lt;br /&gt;
[host-node]# ip route add 192.168.101.n dev vzbr0&lt;br /&gt;
[host-node]# ip route add 192.168.102.1 dev vzbr0&lt;br /&gt;
...&lt;br /&gt;
...&lt;br /&gt;
[host-node]# ip route add 192.168.XXX.N dev vzbr0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Thus you'll have more convinient configuration, i.e. all routes to containers will be through this bridge and containers can communicate with each other even without these routes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Making a veth-device persistent ===&lt;br /&gt;
According to http://bugzilla.openvz.org/show_bug.cgi?id=301 , a bug that stopped the veth device persistent was &amp;quot;Obsoleted now when --veth_add/--veth_del are introduced&amp;quot;&lt;br /&gt;
&lt;br /&gt;
See http://wiki.openvz.org/w/index.php?title=Virtual_Ethernet_device&amp;amp;diff=5990&amp;amp;oldid=5989#Making_a_veth-device_persistent for a workaround that used to be described in this section.&lt;br /&gt;
&lt;br /&gt;
That's it! At this point, when you restart the CT you should see a new line in the output, indicating that the interface is being configured and a new route being added. And you should be able to ping the host, and to enter the CT and use the network.&lt;br /&gt;
&lt;br /&gt;
=== Making a bridged veth-device persistent ===&lt;br /&gt;
&lt;br /&gt;
Like the above example, here it is how to add the veth device to a bridge in a persistent way. &lt;br /&gt;
&lt;br /&gt;
vzctl include a 'vznetaddbr' script, which makes use of the ''bridge'' parameter of the --netif_add switch.&lt;br /&gt;
&lt;br /&gt;
Just create /etc/vz/vznet.conf containing the following.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
EXTERNAL_SCRIPT=&amp;quot;/usr/sbin/vznetaddbr&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The script uses 'vmbr0' as default bridge name when no bridge is specified.&lt;br /&gt;
&lt;br /&gt;
=== Virtual Ethernet devices + VLAN ===&lt;br /&gt;
This configuration can be done by adding vlan device to the previous configuration.&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
* [[Virtual network device]]&lt;br /&gt;
* [[Differences between venet and veth]]&lt;br /&gt;
* [[Using private IPs for Hardware Nodes]]&lt;br /&gt;
* Patch: [[Disable venet interface]]&lt;br /&gt;
* Troubleshooting: [[Bridge doesn't forward packets]]&lt;br /&gt;
&lt;br /&gt;
== External links ==&lt;br /&gt;
* [http://www.tldp.org/HOWTO/Linux+IPv6-HOWTO/hints-daemons-radvd.html Linux IPv6 HOWTO, a chapter about radvd]&lt;br /&gt;
* [http://sysadmin-ivanov.blogspot.com/2008/02/2-veth-with-2-bridges-on-openvz-at.html 2 veth with 2 bridges setup]&lt;br /&gt;
&lt;br /&gt;
[[Category: Networking]]&lt;br /&gt;
[[Category: HOWTO]]&lt;/div&gt;</summary>
		<author><name>Mose</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Virtual_Ethernet_device&amp;diff=11973</id>
		<title>Virtual Ethernet device</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Virtual_Ethernet_device&amp;diff=11973"/>
		<updated>2012-02-16T17:50:05Z</updated>

		<summary type="html">&lt;p&gt;Mose: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Virtual Ethernet device''' is an Ethernet-like device that can be used&lt;br /&gt;
inside a [[container]]. Unlike a [[venet]] network device, a [[veth]] device&lt;br /&gt;
has a MAC address. Therefore, it can be used in more configurations. When veth&lt;br /&gt;
is bridged to a [[CT0]] network interface (e.g., eth0), the container can act as an&lt;br /&gt;
independent host on the network. The container's user can set up all of the networking&lt;br /&gt;
himself, including IPs, gateways, etc.&lt;br /&gt;
&lt;br /&gt;
A virtual Ethernet device consist of two Ethernet devices,&lt;br /&gt;
one in [[CT0]] (e.g., vethN.0) and a corresponding one in CT (e.g., eth0) that are&lt;br /&gt;
connected to each other. If a packet is sent to one device it will come out the other device.&lt;br /&gt;
&lt;br /&gt;
== Virtual Ethernet device usage ==&lt;br /&gt;
&lt;br /&gt;
=== Kernel module ===&lt;br /&gt;
The &amp;lt;code&amp;gt;vzethdev&amp;lt;/code&amp;gt; module should be loaded. You can check it with the following commands.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# lsmod | grep vzeth&lt;br /&gt;
vzethdev                8224  0&lt;br /&gt;
vzmon                  35164  5 vzethdev,vznetdev,vzrst,vzcpt&lt;br /&gt;
vzdev                   3080  4 vzethdev,vznetdev,vzmon,vzdquota&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In case it is not loaded, load it:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# modprobe vzethdev&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== MAC addresses ===&lt;br /&gt;
The following steps to generate a MAC address are not necessary, since newer versions&lt;br /&gt;
of vzctl will automatically generate a MAC address for you. These steps are provided&lt;br /&gt;
in case you want to set a MAC address manually.&lt;br /&gt;
&lt;br /&gt;
You should use a random MAC address when adding a network interface to a container. Do not use MAC addresses of real eth devices, because this can lead to collisions.&lt;br /&gt;
&lt;br /&gt;
MAC addresses must be entered in XX:XX:XX:XX:XX:XX format.&lt;br /&gt;
&lt;br /&gt;
There is a utility script available for generating MAC addresses: http://www.easyvmx.com/software/easymac.sh. It is used like this:&lt;br /&gt;
&lt;br /&gt;
 chmod +x easymac.sh&lt;br /&gt;
 ./easymac.sh -R&lt;br /&gt;
&lt;br /&gt;
=== Adding veth to a CT ===&lt;br /&gt;
&lt;br /&gt;
 vzctl set &amp;lt;CTID&amp;gt; --netif_add &amp;lt;ifname&amp;gt;[,&amp;lt;mac&amp;gt;,&amp;lt;host_ifname&amp;gt;,&amp;lt;host_mac&amp;gt;,&amp;lt;bridge&amp;gt;]&lt;br /&gt;
&lt;br /&gt;
Here&lt;br /&gt;
* &amp;lt;tt&amp;gt;ifname&amp;lt;/tt&amp;gt; is the Ethernet device name in the CT&lt;br /&gt;
* &amp;lt;tt&amp;gt;mac&amp;lt;/tt&amp;gt; is its MAC address in the CT&lt;br /&gt;
* &amp;lt;tt&amp;gt;host_ifname&amp;lt;/tt&amp;gt;  is the Ethernet device name on the host ([[CT0]])&lt;br /&gt;
* &amp;lt;tt&amp;gt;host_mac&amp;lt;/tt&amp;gt; is its MAC address on the host ([[CT0]])&lt;br /&gt;
* &amp;lt;tt&amp;gt;bridge&amp;lt;/tt&amp;gt; is an optional parameter which can be used in custom network start scripts to automatically add  the interface to a bridge. (See the reference to the vznetaddbr script below and persistent bridge configurations.)&lt;br /&gt;
&lt;br /&gt;
{{Note|All parameters except &amp;lt;code&amp;gt;ifname&amp;lt;/code&amp;gt; are optional. Missing parameters, except for bridge, are automatically generated, if not specified. This is the preferred method.}}&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 vzctl set 101 --netif_add eth0 --save&lt;br /&gt;
&lt;br /&gt;
If you want to specify everything:&lt;br /&gt;
&lt;br /&gt;
 vzctl set 101 --netif_add eth0,00:12:34:56:78:9A,veth101.0,00:12:34:56:78:9B --save&lt;br /&gt;
&lt;br /&gt;
If you want to specify the bridge and autogenerate the other values:&lt;br /&gt;
&lt;br /&gt;
 vzctl set 101 --netif_add eth0,,,,vmbr1 --save&lt;br /&gt;
&lt;br /&gt;
=== Removing veth from a CT ===&lt;br /&gt;
&lt;br /&gt;
 vzctl set &amp;lt;CTID&amp;gt; --netif_del &amp;lt;dev_name&amp;gt;|all&lt;br /&gt;
&lt;br /&gt;
Here&lt;br /&gt;
* &amp;lt;code&amp;gt;dev_name&amp;lt;/code&amp;gt; is the Ethernet device name in the [[CT]].&lt;br /&gt;
&lt;br /&gt;
{{Note|If you want to remove all Ethernet devices in CT, use &amp;lt;code&amp;gt;all&amp;lt;/code&amp;gt;.}}&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 vzctl set 101 --netif_del eth0 --save&lt;br /&gt;
&lt;br /&gt;
== Common configurations with virtual Ethernet devices ==&lt;br /&gt;
Module &amp;lt;tt&amp;gt;vzethdev&amp;lt;/tt&amp;gt; must be loaded to operate with veth devices.&lt;br /&gt;
&lt;br /&gt;
=== Simple configuration with virtual Ethernet device ===&lt;br /&gt;
&lt;br /&gt;
Assuming you have 192.168.0.0/24 on your LAN, you will learn how to integrate a container in this LAN using veth.&lt;br /&gt;
&lt;br /&gt;
==== Start a CT ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# vzctl start 101&lt;br /&gt;
&lt;br /&gt;
==== Add veth device to CT ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# vzctl set 101 --netif_add eth0 --save&lt;br /&gt;
&lt;br /&gt;
This allocates a MAC address and associates it with the host eth0 port.&lt;br /&gt;
&lt;br /&gt;
==== Configure devices in CT0 ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# ifconfig veth101.0 0&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/veth101.0/forwarding&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/veth101.0/proxy_arp&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/eth0/forwarding&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/eth0/proxy_arp&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Configure device in CT ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# vzctl enter 101&lt;br /&gt;
[ve-101]# /sbin/ifconfig eth0 0&lt;br /&gt;
[ve-101]# /sbin/ip addr add 192.168.0.101 dev eth0&lt;br /&gt;
[ve-101]# /sbin/ip route add default dev eth0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
* Until you ifconfig eth0 it won't appear. When you do it will use the mac address netif_add added earlier&lt;br /&gt;
* 192.168.0.101 is chosen to be an [[unrouteable private ip address]]. Where 101 reminds you that it is node 101.&lt;br /&gt;
* The &amp;quot;ip route&amp;quot; tells all traffic to head to &amp;quot;device eth0&amp;quot;&lt;br /&gt;
* In theory you could [[use dhcpd with OpenVZ]] and dhclient to pick up an DHCP address from your router instead of hardwiring it&lt;br /&gt;
** http://openvz.org/pipermail/users/2005-November/000020.html&lt;br /&gt;
&lt;br /&gt;
==== Add route in [[CT0]] ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# ip route add 192.168.0.101 dev veth101.0&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Using a directly routed IPv4 with virtual Ethernet device ===&lt;br /&gt;
&lt;br /&gt;
==== Situation ====&lt;br /&gt;
Hardware Node (HN/CT0) has 192.168.0.1/24 with router 192.168.0.254.&lt;br /&gt;
&lt;br /&gt;
We also know that IPv4 10.0.0.1/32 is directly routed to 192.168.0.1 (this is called a ''fail-over IP'').&lt;br /&gt;
&lt;br /&gt;
We want to give this directly routed IPv4 address to a container (CT).&lt;br /&gt;
&lt;br /&gt;
==== Start container ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# vzctl start 101&lt;br /&gt;
&lt;br /&gt;
==== Add veth device to CT ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# vzctl set 101 --netif_add eth0 --save&lt;br /&gt;
&lt;br /&gt;
This allocates a MAC address and associates it with the host eth0 port.&lt;br /&gt;
&lt;br /&gt;
==== Configure device and add route in CT0 ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# ifconfig veth101.0 0&lt;br /&gt;
[host-node]# ip route add 10.0.0.1 dev veth101.0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can automatize this at VPS creation by using a mount script &amp;lt;tt&amp;gt;$VEID.mount&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The problem here is that the ''veth'' interface appears in CT0 '''after''' VPS has started, therefore we cannot directly use the commands in the mount script. We launch a shell script (enclosed by { }) in background (operator '''&amp;amp;''') that waits for the interface to be ready and then adds the IP route.&lt;br /&gt;
&lt;br /&gt;
Contents of the mount script &amp;lt;tt&amp;gt;/etc/vz/conf/101.mount&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
# This script source VPS configuration files in the same order as vzctl does&lt;br /&gt;
&lt;br /&gt;
# if one of these files does not exist then something is really broken&lt;br /&gt;
[ -f /etc/vz/vz.conf ] || exit 1&lt;br /&gt;
[ -f $VE_CONFFILE ] || exit 1&lt;br /&gt;
&lt;br /&gt;
# source both files. Note the order, it is important&lt;br /&gt;
. /etc/vz/vz.conf&lt;br /&gt;
. $VE_CONFFILE&lt;br /&gt;
&lt;br /&gt;
# Configure veth with IP after VPS has started&lt;br /&gt;
{&lt;br /&gt;
  IP=X.Y.Z.T&lt;br /&gt;
  DEV=veth101.0&lt;br /&gt;
  while sleep 1; do&lt;br /&gt;
    /sbin/ifconfig $DEV 0 &amp;gt;/dev/null 2&amp;gt;&amp;amp;1&lt;br /&gt;
    if [ $? -eq 0 ]; then&lt;br /&gt;
      /sbin/ip route add $IP dev $DEV&lt;br /&gt;
      break&lt;br /&gt;
    fi&lt;br /&gt;
  done&lt;br /&gt;
} &amp;amp;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Make sure IPv4 forwarding is enabled in CT0 ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/ip_forward&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/eth0/forwarding&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/veth101.0/forwarding&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
You can permanently set this by using &amp;lt;tt&amp;gt;/etc/sysctl.conf&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==== Configure device in CT ====&lt;br /&gt;
&lt;br /&gt;
1. Configure IP address&lt;br /&gt;
&lt;br /&gt;
2. Add gateway&lt;br /&gt;
&lt;br /&gt;
3. Add default route&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[ve-101]# /sbin/ifconfig eth0 10.0.0.1 netmask 255.255.255.255&lt;br /&gt;
[ve-101]# /sbin/ip route add 192.168.0.1 dev eth0&lt;br /&gt;
[ve-101]# /sbin/ip route add default via 192.168.0.1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In a Debian container, you can configure this permanently by using &amp;lt;tt&amp;gt;/etc/network/interfaces&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
auto eth0&lt;br /&gt;
iface eth0 inet static&lt;br /&gt;
        address 10.0.0.1&lt;br /&gt;
        netmask 255.255.255.255&lt;br /&gt;
        up /sbin/ip route add 192.168.0.1 dev eth0&lt;br /&gt;
        up /sbin/ip route add default via 192.168.0.1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Virtual Ethernet device with IPv6 ===&lt;br /&gt;
&lt;br /&gt;
See the [[VEs and HNs in same subnets]] article.&lt;br /&gt;
&lt;br /&gt;
=== Virtual Ethernet devices can be joined in one bridge ===&lt;br /&gt;
Perform steps 1 - 4 from Simple configuration chapter for several containers and/or veth devices&lt;br /&gt;
&lt;br /&gt;
==== Create bridge device ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# brctl addbr vzbr0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Add veth devices to bridge ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# brctl addif vzbr0 veth101.0&lt;br /&gt;
...&lt;br /&gt;
[host-node]# brctl addif vzbr0 veth101.n&lt;br /&gt;
[host-node]# brctl addif vzbr0 veth102.0&lt;br /&gt;
...&lt;br /&gt;
...&lt;br /&gt;
[host-node]# brctl addif vzbr0 vethXXX.N&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Configure bridge device ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# ifconfig vzbr0 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Add routes in [[CT0]] ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# ip route add 192.168.101.1 dev vzbr0&lt;br /&gt;
...&lt;br /&gt;
[host-node]# ip route add 192.168.101.n dev vzbr0&lt;br /&gt;
[host-node]# ip route add 192.168.102.1 dev vzbr0&lt;br /&gt;
...&lt;br /&gt;
...&lt;br /&gt;
[host-node]# ip route add 192.168.XXX.N dev vzbr0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Thus you'll have more convinient configuration, i.e. all routes to containers will be through this bridge and containers can communicate with each other even without these routes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Making a veth-device persistent ===&lt;br /&gt;
According to http://bugzilla.openvz.org/show_bug.cgi?id=301 , a bug that stopped the veth device persistent was &amp;quot;Obsoleted now when --veth_add/--veth_del are introduced&amp;quot;&lt;br /&gt;
&lt;br /&gt;
See http://wiki.openvz.org/w/index.php?title=Virtual_Ethernet_device&amp;amp;diff=5990&amp;amp;oldid=5989#Making_a_veth-device_persistent for a workaround that used to be described in this section.&lt;br /&gt;
&lt;br /&gt;
That's it! At this point, when you restart the CT you should see a new line in the output, indicating that the interface is being configured and a new route being added. And you should be able to ping the host, and to enter the CT and use the network.&lt;br /&gt;
&lt;br /&gt;
=== Making a bridged veth-device persistent ===&lt;br /&gt;
&lt;br /&gt;
Like the above example, here it is how to add the veth device to a bridge in a persistent way. &lt;br /&gt;
&lt;br /&gt;
vzctl include a 'vznetaddbr' script, which makes use of the ''bridge'' parameter of the --netif_add switch.&lt;br /&gt;
&lt;br /&gt;
Just create /etc/vz/vznet.conf containing the following.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
EXTERNAL_SCRIPT=&amp;quot;/usr/sbin/vznetaddbr&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The script uses 'vmbr0' as default bridge name when no bridge is specified.&lt;br /&gt;
&lt;br /&gt;
=== Virtual Ethernet devices + VLAN ===&lt;br /&gt;
This configuration can be done by adding vlan device to the previous configuration.&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
* [[Virtual network device]]&lt;br /&gt;
* [[Differences between venet and veth]]&lt;br /&gt;
* [[Using private IPs for Hardware Nodes]]&lt;br /&gt;
* Patch: [[Disable venet interface]]&lt;br /&gt;
* Troubleshooting: [[Bridge doesn't forward packets]]&lt;br /&gt;
&lt;br /&gt;
== External links ==&lt;br /&gt;
* [http://www.tldp.org/HOWTO/Linux+IPv6-HOWTO/hints-daemons-radvd.html Linux IPv6 HOWTO, a chapter about radvd]&lt;br /&gt;
* [http://sysadmin-ivanov.blogspot.com/2008/02/2-veth-with-2-bridges-on-openvz-at.html 2 veth with 2 bridges setup]&lt;br /&gt;
&lt;br /&gt;
[[Category: Networking]]&lt;br /&gt;
[[Category: HOWTO]]&lt;/div&gt;</summary>
		<author><name>Mose</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Virtual_Ethernet_device&amp;diff=11972</id>
		<title>Virtual Ethernet device</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Virtual_Ethernet_device&amp;diff=11972"/>
		<updated>2012-02-16T17:32:56Z</updated>

		<summary type="html">&lt;p&gt;Mose: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Virtual Ethernet device''' is an Ethernet-like device that can be used&lt;br /&gt;
inside a [[container]]. Unlike a [[venet]] network device, a [[veth]] device&lt;br /&gt;
has a MAC address. Therefore, it can be used in more configurations. When veth&lt;br /&gt;
is bridged to a [[CT0]] network interface (e.g., eth0), the container can act as an&lt;br /&gt;
independent host on the network. The container's user can set up all of the networking&lt;br /&gt;
himself, including IPs, gateways, etc.&lt;br /&gt;
&lt;br /&gt;
Virtual Ethernet device consist of two Ethernet devices --&lt;br /&gt;
one in [[CT0]] and another one in CT. These devices are connected to each&lt;br /&gt;
other. If a packet is sent to one device it will come out the other device.&lt;br /&gt;
&lt;br /&gt;
== Virtual Ethernet device usage ==&lt;br /&gt;
&lt;br /&gt;
=== Kernel module ===&lt;br /&gt;
First of all, make sure the &amp;lt;code&amp;gt;vzethdev&amp;lt;/code&amp;gt; module is loaded:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# lsmod | grep vzeth&lt;br /&gt;
vzethdev                8224  0&lt;br /&gt;
vzmon                  35164  5 vzethdev,vznetdev,vzrst,vzcpt&lt;br /&gt;
vzdev                   3080  4 vzethdev,vznetdev,vzmon,vzdquota&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In case it is not loaded, load it:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# modprobe vzethdev&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== MAC addresses ===&lt;br /&gt;
In the below commands, you should use random MAC addresses. Do not use MAC addresses of real eth devices, because this can lead to collisions.&lt;br /&gt;
&lt;br /&gt;
MAC addresses must be entered in XX:XX:XX:XX:XX:XX format.&lt;br /&gt;
&lt;br /&gt;
YOU MAY NOT NEED TO GENERATE MAC ADDRESSES BY HAND BECAUSE vzctl --veth_add&lt;br /&gt;
MAY GENERATE THEM AUTOMATICALLY AS NECESSARY.&lt;br /&gt;
&lt;br /&gt;
Nevertheless, there is a utility script available for generating MAC addresses: http://www.easyvmx.com/software/easymac.sh. It is to be used like this:&lt;br /&gt;
&lt;br /&gt;
 chmod +x easymac.sh&lt;br /&gt;
 ./easymac.sh -R&lt;br /&gt;
&lt;br /&gt;
=== Adding veth to a CT ===&lt;br /&gt;
&lt;br /&gt;
 vzctl set &amp;lt;CTID&amp;gt; --netif_add &amp;lt;ifname&amp;gt;[,&amp;lt;mac&amp;gt;,&amp;lt;host_ifname&amp;gt;,&amp;lt;host_mac&amp;gt;,&amp;lt;bridge&amp;gt;]&lt;br /&gt;
&lt;br /&gt;
Here&lt;br /&gt;
* &amp;lt;tt&amp;gt;ifname&amp;lt;/tt&amp;gt; is the Ethernet device name in the CT&lt;br /&gt;
* &amp;lt;tt&amp;gt;mac&amp;lt;/tt&amp;gt; is its MAC address in the CT&lt;br /&gt;
* &amp;lt;tt&amp;gt;host_ifname&amp;lt;/tt&amp;gt;  is the Ethernet device name on the host ([[CT0]])&lt;br /&gt;
* &amp;lt;tt&amp;gt;host_mac&amp;lt;/tt&amp;gt; is its MAC address on the host ([[CT0]])&lt;br /&gt;
* &amp;lt;tt&amp;gt;bridge&amp;lt;/tt&amp;gt; is an optional parameter which can be used in custom network start scripts to automatically add  the interface to a bridge.&lt;br /&gt;
&lt;br /&gt;
{{Note|All parameters except &amp;lt;code&amp;gt;ifname&amp;lt;/code&amp;gt; are optional and are automatically generated if not specified.}}&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 vzctl set 101 --netif_add eth0 --save&lt;br /&gt;
&lt;br /&gt;
Or, if you want to specify everything:&lt;br /&gt;
&lt;br /&gt;
 vzctl set 101 --netif_add eth0,00:12:34:56:78:9A,veth101.0,00:12:34:56:78:9B --save&lt;br /&gt;
&lt;br /&gt;
Or, if you want to specify the bridge and leave the other values autogenerated:&lt;br /&gt;
&lt;br /&gt;
 vzctl set 101 --netif_add eth0,,,,vmbr1 --save&lt;br /&gt;
&lt;br /&gt;
=== Removing veth from a CT ===&lt;br /&gt;
&lt;br /&gt;
 vzctl set &amp;lt;CTID&amp;gt; --netif_del &amp;lt;dev_name&amp;gt;|all&lt;br /&gt;
&lt;br /&gt;
Here&lt;br /&gt;
* &amp;lt;code&amp;gt;dev_name&amp;lt;/code&amp;gt; is the Ethernet device name in the [[CT]].&lt;br /&gt;
&lt;br /&gt;
{{Note|If you want to remove all Ethernet devices in CT, use &amp;lt;code&amp;gt;all&amp;lt;/code&amp;gt;.}}&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 vzctl set 101 --netif_del eth0 --save&lt;br /&gt;
&lt;br /&gt;
== Common configurations with virtual Ethernet devices ==&lt;br /&gt;
Module &amp;lt;tt&amp;gt;vzethdev&amp;lt;/tt&amp;gt; must be loaded to operate with veth devices.&lt;br /&gt;
&lt;br /&gt;
=== Simple configuration with virtual Ethernet device ===&lt;br /&gt;
&lt;br /&gt;
Assuming you have 192.168.0.0/24 on your LAN, you will learn how to integrate a container in this LAN using veth.&lt;br /&gt;
&lt;br /&gt;
==== Start a CT ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# vzctl start 101&lt;br /&gt;
&lt;br /&gt;
==== Add veth device to CT ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# vzctl set 101 --netif_add eth0 --save&lt;br /&gt;
&lt;br /&gt;
This allocates a MAC address and associates it with the host eth0 port.&lt;br /&gt;
&lt;br /&gt;
==== Configure devices in CT0 ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# ifconfig veth101.0 0&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/veth101.0/forwarding&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/veth101.0/proxy_arp&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/eth0/forwarding&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/eth0/proxy_arp&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Configure device in CT ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# vzctl enter 101&lt;br /&gt;
[ve-101]# /sbin/ifconfig eth0 0&lt;br /&gt;
[ve-101]# /sbin/ip addr add 192.168.0.101 dev eth0&lt;br /&gt;
[ve-101]# /sbin/ip route add default dev eth0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
* Until you ifconfig eth0 it won't appear. When you do it will use the mac address netif_add added earlier&lt;br /&gt;
* 192.168.0.101 is chosen to be an [[unrouteable private ip address]]. Where 101 reminds you that it is node 101.&lt;br /&gt;
* The &amp;quot;ip route&amp;quot; tells all traffic to head to &amp;quot;device eth0&amp;quot;&lt;br /&gt;
* In theory you could [[use dhcpd with OpenVZ]] and dhclient to pick up an DHCP address from your router instead of hardwiring it&lt;br /&gt;
** http://openvz.org/pipermail/users/2005-November/000020.html&lt;br /&gt;
&lt;br /&gt;
==== Add route in [[CT0]] ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# ip route add 192.168.0.101 dev veth101.0&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Using a directly routed IPv4 with virtual Ethernet device ===&lt;br /&gt;
&lt;br /&gt;
==== Situation ====&lt;br /&gt;
Hardware Node (HN/CT0) has 192.168.0.1/24 with router 192.168.0.254.&lt;br /&gt;
&lt;br /&gt;
We also know that IPv4 10.0.0.1/32 is directly routed to 192.168.0.1 (this is called a ''fail-over IP'').&lt;br /&gt;
&lt;br /&gt;
We want to give this directly routed IPv4 address to a container (CT).&lt;br /&gt;
&lt;br /&gt;
==== Start container ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# vzctl start 101&lt;br /&gt;
&lt;br /&gt;
==== Add veth device to CT ====&lt;br /&gt;
&lt;br /&gt;
 [host-node]# vzctl set 101 --netif_add eth0 --save&lt;br /&gt;
&lt;br /&gt;
This allocates a MAC address and associates it with the host eth0 port.&lt;br /&gt;
&lt;br /&gt;
==== Configure device and add route in CT0 ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# ifconfig veth101.0 0&lt;br /&gt;
[host-node]# ip route add 10.0.0.1 dev veth101.0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can automatize this at VPS creation by using a mount script &amp;lt;tt&amp;gt;$VEID.mount&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The problem here is that the ''veth'' interface appears in CT0 '''after''' VPS has started, therefore we cannot directly use the commands in the mount script. We launch a shell script (enclosed by { }) in background (operator '''&amp;amp;''') that waits for the interface to be ready and then adds the IP route.&lt;br /&gt;
&lt;br /&gt;
Contents of the mount script &amp;lt;tt&amp;gt;/etc/vz/conf/101.mount&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
# This script source VPS configuration files in the same order as vzctl does&lt;br /&gt;
&lt;br /&gt;
# if one of these files does not exist then something is really broken&lt;br /&gt;
[ -f /etc/vz/vz.conf ] || exit 1&lt;br /&gt;
[ -f $VE_CONFFILE ] || exit 1&lt;br /&gt;
&lt;br /&gt;
# source both files. Note the order, it is important&lt;br /&gt;
. /etc/vz/vz.conf&lt;br /&gt;
. $VE_CONFFILE&lt;br /&gt;
&lt;br /&gt;
# Configure veth with IP after VPS has started&lt;br /&gt;
{&lt;br /&gt;
  IP=X.Y.Z.T&lt;br /&gt;
  DEV=veth101.0&lt;br /&gt;
  while sleep 1; do&lt;br /&gt;
    /sbin/ifconfig $DEV 0 &amp;gt;/dev/null 2&amp;gt;&amp;amp;1&lt;br /&gt;
    if [ $? -eq 0 ]; then&lt;br /&gt;
      /sbin/ip route add $IP dev $DEV&lt;br /&gt;
      break&lt;br /&gt;
    fi&lt;br /&gt;
  done&lt;br /&gt;
} &amp;amp;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Make sure IPv4 forwarding is enabled in CT0 ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/ip_forward&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/eth0/forwarding&lt;br /&gt;
[host-node]# echo 1 &amp;gt; /proc/sys/net/ipv4/conf/veth101.0/forwarding&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
You can permanently set this by using &amp;lt;tt&amp;gt;/etc/sysctl.conf&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==== Configure device in CT ====&lt;br /&gt;
&lt;br /&gt;
1. Configure IP address&lt;br /&gt;
&lt;br /&gt;
2. Add gateway&lt;br /&gt;
&lt;br /&gt;
3. Add default route&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[ve-101]# /sbin/ifconfig eth0 10.0.0.1 netmask 255.255.255.255&lt;br /&gt;
[ve-101]# /sbin/ip route add 192.168.0.1 dev eth0&lt;br /&gt;
[ve-101]# /sbin/ip route add default via 192.168.0.1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In a Debian container, you can configure this permanently by using &amp;lt;tt&amp;gt;/etc/network/interfaces&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
auto eth0&lt;br /&gt;
iface eth0 inet static&lt;br /&gt;
        address 10.0.0.1&lt;br /&gt;
        netmask 255.255.255.255&lt;br /&gt;
        up /sbin/ip route add 192.168.0.1 dev eth0&lt;br /&gt;
        up /sbin/ip route add default via 192.168.0.1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Virtual Ethernet device with IPv6 ===&lt;br /&gt;
&lt;br /&gt;
See the [[VEs and HNs in same subnets]] article.&lt;br /&gt;
&lt;br /&gt;
=== Virtual Ethernet devices can be joined in one bridge ===&lt;br /&gt;
Perform steps 1 - 4 from Simple configuration chapter for several containers and/or veth devices&lt;br /&gt;
&lt;br /&gt;
==== Create bridge device ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# brctl addbr vzbr0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Add veth devices to bridge ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# brctl addif vzbr0 veth101.0&lt;br /&gt;
...&lt;br /&gt;
[host-node]# brctl addif vzbr0 veth101.n&lt;br /&gt;
[host-node]# brctl addif vzbr0 veth102.0&lt;br /&gt;
...&lt;br /&gt;
...&lt;br /&gt;
[host-node]# brctl addif vzbr0 vethXXX.N&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Configure bridge device ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# ifconfig vzbr0 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Add routes in [[CT0]] ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[host-node]# ip route add 192.168.101.1 dev vzbr0&lt;br /&gt;
...&lt;br /&gt;
[host-node]# ip route add 192.168.101.n dev vzbr0&lt;br /&gt;
[host-node]# ip route add 192.168.102.1 dev vzbr0&lt;br /&gt;
...&lt;br /&gt;
...&lt;br /&gt;
[host-node]# ip route add 192.168.XXX.N dev vzbr0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Thus you'll have more convinient configuration, i.e. all routes to containers will be through this bridge and containers can communicate with each other even without these routes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Making a veth-device persistent ===&lt;br /&gt;
According to http://bugzilla.openvz.org/show_bug.cgi?id=301 , a bug that stopped the veth device persistent was &amp;quot;Obsoleted now when --veth_add/--veth_del are introduced&amp;quot;&lt;br /&gt;
&lt;br /&gt;
See http://wiki.openvz.org/w/index.php?title=Virtual_Ethernet_device&amp;amp;diff=5990&amp;amp;oldid=5989#Making_a_veth-device_persistent for a workaround that used to be described in this section.&lt;br /&gt;
&lt;br /&gt;
That's it! At this point, when you restart the CT you should see a new line in the output, indicating that the interface is being configured and a new route being added. And you should be able to ping the host, and to enter the CT and use the network.&lt;br /&gt;
&lt;br /&gt;
=== Making a bridged veth-device persistent ===&lt;br /&gt;
&lt;br /&gt;
Like the above example, here it is how to add the veth device to a bridge in a persistent way. &lt;br /&gt;
&lt;br /&gt;
vzctl include a 'vznetaddbr' script, which makes use of the ''bridge'' parameter of the --netif_add switch.&lt;br /&gt;
&lt;br /&gt;
Just create /etc/vz/vznet.conf containing the following.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
EXTERNAL_SCRIPT=&amp;quot;/usr/sbin/vznetaddbr&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The script uses 'vmbr0' as default bridge name when no bridge is specified.&lt;br /&gt;
&lt;br /&gt;
=== Virtual Ethernet devices + VLAN ===&lt;br /&gt;
This configuration can be done by adding vlan device to the previous configuration.&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
* [[Virtual network device]]&lt;br /&gt;
* [[Differences between venet and veth]]&lt;br /&gt;
* [[Using private IPs for Hardware Nodes]]&lt;br /&gt;
* Patch: [[Disable venet interface]]&lt;br /&gt;
* Troubleshooting: [[Bridge doesn't forward packets]]&lt;br /&gt;
&lt;br /&gt;
== External links ==&lt;br /&gt;
* [http://www.tldp.org/HOWTO/Linux+IPv6-HOWTO/hints-daemons-radvd.html Linux IPv6 HOWTO, a chapter about radvd]&lt;br /&gt;
* [http://sysadmin-ivanov.blogspot.com/2008/02/2-veth-with-2-bridges-on-openvz-at.html 2 veth with 2 bridges setup]&lt;br /&gt;
&lt;br /&gt;
[[Category: Networking]]&lt;br /&gt;
[[Category: HOWTO]]&lt;/div&gt;</summary>
		<author><name>Mose</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Debian_template_creation&amp;diff=11967</id>
		<title>Debian template creation</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Debian_template_creation&amp;diff=11967"/>
		<updated>2012-02-14T18:38:50Z</updated>

		<summary type="html">&lt;p&gt;Mose: /* Preparing the HN network */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;These are rough instructions of how to manually create basic Debian template cache, which can be used to create OpenVZ [[VE]]s based on Debian. &lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* You shouldn't be running as root, but as a user that is permitted to use sudo instead.  It's a dangerous idea, run as root at your peril.&lt;br /&gt;
* Anywhere you see &amp;lt;tt&amp;gt;/vz&amp;lt;/tt&amp;gt;, you might really need to use &amp;lt;tt&amp;gt;/var/lib/vz&amp;lt;/tt&amp;gt; instead, especially on a Debian Etch host.&lt;br /&gt;
* Anywhere you see &amp;lt;tt&amp;gt;http://http.us.debian.org/debian/&amp;lt;/tt&amp;gt;, you can substitute your favorite Debian mirror.  ([http://www.debian.org/mirror/list List of official Debian Mirrors])&lt;br /&gt;
* See also: &amp;lt;tt&amp;gt;/usr/share/doc/vzctl/README.Debian&amp;lt;/tt&amp;gt; in the ''vzctl'' Debian package&lt;br /&gt;
&lt;br /&gt;
== Prerequisites ==&lt;br /&gt;
&lt;br /&gt;
{{Warning|if you want to use &amp;lt;code&amp;gt;ext4&amp;lt;/code&amp;gt; file system for &amp;lt;code&amp;gt;/vz&amp;lt;/code&amp;gt;, use &amp;lt;code&amp;gt;nodelalloc&amp;lt;/code&amp;gt; option in &amp;lt;code&amp;gt;/etc/fstab&amp;lt;/code&amp;gt;, otherwise it will crash. See {{Bug|1509}} and its duplicates for details.}}&lt;br /&gt;
&lt;br /&gt;
You need to have a working copy of &amp;lt;tt&amp;gt;debootstrap&amp;lt;/tt&amp;gt; running on your hardware node.&lt;br /&gt;
&lt;br /&gt;
For Debian:&lt;br /&gt;
 sudo apt-get install debootstrap&lt;br /&gt;
&lt;br /&gt;
For Gentoo:&lt;br /&gt;
 sudo emerge debootstrap&lt;br /&gt;
&lt;br /&gt;
For Fedora (at least Fedora 8 have it, not sure about earlier versions):&lt;br /&gt;
 sudo yum install debootstrap&lt;br /&gt;
&lt;br /&gt;
For other distros you might need to install it from sources, or search for an appropriate package for your distribution.  An RPM is available on the [http://forum.openvz.org/index.php?t=tree&amp;amp;th=142&amp;amp;mid=584 OpenVZ Forum].&lt;br /&gt;
&lt;br /&gt;
== Bootstrapping Debian ==&lt;br /&gt;
&lt;br /&gt;
You can install different releases of Debian into a VE's private directory using the debootstrap command.&lt;br /&gt;
&lt;br /&gt;
The command parameters are:&lt;br /&gt;
&lt;br /&gt;
  debootstrap --arch ARCH NAME DIRECTORY URL&lt;br /&gt;
&lt;br /&gt;
Specify your architecture instead of &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; if you're using something other than i386/x86.  For example, for AMD64/x86_64, use &amp;lt;tt&amp;gt;amd64&amp;lt;/tt&amp;gt; or for ia64, use &amp;lt;tt&amp;gt;ia64&amp;lt;/tt&amp;gt;. You can use http or ftp in the URL.&lt;br /&gt;
&lt;br /&gt;
We use VE ID of 777 for this example, but it can be any unused ID.&lt;br /&gt;
&lt;br /&gt;
=== Squeeze (current Debian stable) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap --arch i386 squeeze /vz/private/777 http://http.us.debian.org/debian/&lt;br /&gt;
 or&lt;br /&gt;
 debootstrap --arch amd64 squeeze /vz/private/777 ftp://ftp.us.debian.org/debian/&lt;br /&gt;
&lt;br /&gt;
=== Lenny (Debian oldstable) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap --arch i386 lenny /vz/private/777 http://http.us.debian.org/debian/&lt;br /&gt;
 or&lt;br /&gt;
 debootstrap --arch amd64 lenny /vz/private/777 ftp://ftp.us.debian.org/debian/&lt;br /&gt;
&lt;br /&gt;
=== Etch (old release) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap --arch i386 etch /vz/private/777 http://http.us.debian.org/debian/&lt;br /&gt;
&lt;br /&gt;
=== Sarge (very old release) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap sarge /vz/private/777 http://archive.debian.org/debian&lt;br /&gt;
&lt;br /&gt;
== Preparing the HN network ==&lt;br /&gt;
Append the following lines to /etc/sysctl.conf, adjust to taste and then execute &amp;quot;sysctl -p&amp;quot; for them to take effect.&lt;br /&gt;
 ### OpenVZ settings&lt;br /&gt;
 &lt;br /&gt;
 # On Hardware Node enable packet forwarding to forward&lt;br /&gt;
 # packets between the HN network interfaces and venet.&lt;br /&gt;
 # Proxy arp is only needed when using veth AND veth is&lt;br /&gt;
 # not bridged to a HN interface. When veth is bridged&lt;br /&gt;
 # to a HN interface, the CT handles its own arps.&lt;br /&gt;
 &lt;br /&gt;
 net.ipv4.conf.default.forwarding=1&lt;br /&gt;
 net.ipv4.conf.default.proxy_arp = 0&lt;br /&gt;
 net.ipv4.ip_forward=1&lt;br /&gt;
 &lt;br /&gt;
 # Enables source route verification&lt;br /&gt;
 net.ipv4.conf.all.rp_filter = 1&lt;br /&gt;
 &lt;br /&gt;
 # Enables the magic-sysrq key&lt;br /&gt;
 kernel.sysrq = 1&lt;br /&gt;
 &lt;br /&gt;
 # TCP Explict Congestion Notification&lt;br /&gt;
 net.ipv4.tcp_ecn = 0&lt;br /&gt;
 &lt;br /&gt;
 # we do not want all our interfaces to send redirects&lt;br /&gt;
 net.ipv4.conf.default.send_redirects = 1&lt;br /&gt;
 net.ipv4.conf.all.send_redirects = 0&lt;br /&gt;
&lt;br /&gt;
== Preparing and starting the VE ==&lt;br /&gt;
&lt;br /&gt;
=== Setting VE config ===&lt;br /&gt;
First, we need a config for the [[VE]]:&lt;br /&gt;
 sudo vzctl set 777 --applyconfig vps.basic --save&lt;br /&gt;
&lt;br /&gt;
On debian squeeze only the following worked for me (''confirmed''), because the standard template names in /etc/vz/conf have changed.&lt;br /&gt;
 sudo vzctl set 777 --applyconfig basic --save&lt;br /&gt;
&lt;br /&gt;
=== Setting VE OSTEMPLATE ===&lt;br /&gt;
Also, we need &amp;lt;tt&amp;gt;OSTEMPLATE&amp;lt;/tt&amp;gt; to be set in VE configuration file, for [[vzctl]] to work properly.&lt;br /&gt;
&lt;br /&gt;
 sudo sh -c 'echo OSTEMPLATE=\&amp;quot;debian-5.0\&amp;quot;' &amp;gt;&amp;gt; /etc/vz/conf/777.conf&lt;br /&gt;
&lt;br /&gt;
=== Setting VE IP address ===&lt;br /&gt;
For the [[VE]] to be able to download updates from the Internet, we need a valid IP address for it:&lt;br /&gt;
 sudo vzctl set 777 --ipadd x.x.x.x --save&lt;br /&gt;
&lt;br /&gt;
{{Note|if you use private IP for the VE, you might have to set up NAT as described in [[Using NAT for VE with private IPs]].}}&lt;br /&gt;
&lt;br /&gt;
=== Setting DNS server for VE ===&lt;br /&gt;
For the [[VE]] to be able to download updates from the Internet, we also need to specify a DNS for it:&lt;br /&gt;
 sudo vzctl set 777 --nameserver x.x.x.x --save&lt;br /&gt;
&lt;br /&gt;
=== Creating /dev/ptmx  ===&lt;br /&gt;
The ptmx character device should normally exist, but if it doesn't, create one.&lt;br /&gt;
 sudo mknod --mode 666 /var/lib/vz/private/777/dev/ptmx c 5 2&lt;br /&gt;
&lt;br /&gt;
=== Starting VE ===&lt;br /&gt;
Now start the VE:&lt;br /&gt;
 sudo vzctl start 777&lt;br /&gt;
&lt;br /&gt;
== Customizing the installation ==&lt;br /&gt;
A few things need to be done inside a newly created VE for it to become suitable for OpenVZ. Enter the VE to begin the configuration. Exporting the path is optional.&lt;br /&gt;
 sudo vzctl enter 777&lt;br /&gt;
 export PATH=/sbin:/usr/sbin:/bin:/usr/bin&lt;br /&gt;
&lt;br /&gt;
{{Warning|Do not run the commands below on the hardware node, they are only to be run within the VE!}}&lt;br /&gt;
&lt;br /&gt;
=== Set Debian repositories ===&lt;br /&gt;
 cat &amp;lt;&amp;lt;EOF &amp;gt; /etc/apt/sources.list&lt;br /&gt;
 deb http://http.us.debian.org/debian squeeze main contrib&lt;br /&gt;
 deb http://security.debian.org squeeze/updates main contrib&lt;br /&gt;
 deb http://http.us.debian.org/debian squeeze-updates main&lt;br /&gt;
 ## backports - ONLY IF YOU KNOW WHAT YOU DO&lt;br /&gt;
 # deb http://http.us.debian.org/debian-backports/ squeeze-backports main&lt;br /&gt;
 EOF&lt;br /&gt;
&lt;br /&gt;
=== Get new security updates ===&lt;br /&gt;
 apt-get update&lt;br /&gt;
 apt-get upgrade&lt;br /&gt;
&lt;br /&gt;
=== Install some more packages ===&lt;br /&gt;
Installing packages could be an interactive process so the system might ask some questions.  You can install more packages if you'd like.  For example:&lt;br /&gt;
 apt-get install ssh quota less&lt;br /&gt;
&lt;br /&gt;
=== Set sane permissions for &amp;lt;tt&amp;gt;/root&amp;lt;/tt&amp;gt; directory ===&lt;br /&gt;
 chmod 700 /root&lt;br /&gt;
&lt;br /&gt;
=== Disable root login ===&lt;br /&gt;
This will disable root login by default.&lt;br /&gt;
 usermod -L root&lt;br /&gt;
&lt;br /&gt;
=== Disable getty ===&lt;br /&gt;
Disable running &amp;lt;tt&amp;gt;getty&amp;lt;/tt&amp;gt;s on terminals as a VE does not have any:&lt;br /&gt;
 sed -i -e '/getty/d' /etc/inittab&lt;br /&gt;
&lt;br /&gt;
=== Disable &amp;lt;tt&amp;gt;sync()&amp;lt;/tt&amp;gt; for syslog ===&lt;br /&gt;
Turn off doing &amp;lt;tt&amp;gt;sync()&amp;lt;/tt&amp;gt; on every write for &amp;lt;tt&amp;gt;syslog&amp;lt;/tt&amp;gt;'s log files, to improve I/O performance:&lt;br /&gt;
 &amp;lt;pre&amp;gt;sed -i -e 's@\([[:space:]]\)\(/var/log/\)@\1-\2@' /etc/*syslog.conf&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Fix &amp;lt;tt&amp;gt;/etc/mtab&amp;lt;/tt&amp;gt; ===&lt;br /&gt;
Link &amp;lt;tt&amp;gt;/etc/mtab&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;/proc/mounts&amp;lt;/tt&amp;gt;, so &amp;lt;tt&amp;gt;df&amp;lt;/tt&amp;gt; and friends will work:&lt;br /&gt;
 rm -f /etc/mtab&lt;br /&gt;
 ln -s /proc/mounts /etc/mtab&lt;br /&gt;
&lt;br /&gt;
=== Remove some unneeded packages ===&lt;br /&gt;
If you have any packages you'd like to remove, now's the time for it.  Here's an example — note that not all of those packages are installed by default in Debian Squeeze (although they were in earlier versions):&lt;br /&gt;
 dpkg --purge modutils ppp pppoeconf pppoe pppconfig module-init-tools&lt;br /&gt;
&lt;br /&gt;
=== Disable services ===&lt;br /&gt;
Do not start some services, stick to bare minimum:&lt;br /&gt;
 update-rc.d -f klogd remove&lt;br /&gt;
 update-rc.d -f quotarpc remove&lt;br /&gt;
 update-rc.d -f exim4 remove&lt;br /&gt;
 update-rc.d -f inetd remove&lt;br /&gt;
&lt;br /&gt;
For dependency-based boot sequence introduced with Squeeze type:&lt;br /&gt;
&lt;br /&gt;
 update-rc.d-insserv -f klogd remove&lt;br /&gt;
 update-rc.d-insserv -f quotarpc remove&lt;br /&gt;
 update-rc.d-insserv -f exim4 remove&lt;br /&gt;
 update-rc.d-insserv -f inetd remove&lt;br /&gt;
&lt;br /&gt;
=== Fix SSH host keys ===&lt;br /&gt;
This is only useful if you installed SSH.  Each individual [[VE]] should have its own pair of SSH host keys.  The code below will wipe out the existing SSH keys and instruct the newly-created [[VE]] to create new SSH keys on first boot.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- please do not remove &amp;lt;source&amp;gt;...&amp;lt;/source&amp;gt; pair of tags below,&lt;br /&gt;
     otherwise quotes after -N (-N '') are not visible --&amp;gt;&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
rm -f /etc/ssh/ssh_host_*&lt;br /&gt;
cat &amp;lt;&amp;lt; EOF &amp;gt; /etc/rc2.d/S15ssh_gen_host_keys&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
ssh-keygen -f /etc/ssh/ssh_host_rsa_key -t rsa -N ''&lt;br /&gt;
ssh-keygen -f /etc/ssh/ssh_host_dsa_key -t dsa -N ''&lt;br /&gt;
rm -f \$0&lt;br /&gt;
EOF&lt;br /&gt;
chmod a+x /etc/rc2.d/S15ssh_gen_host_keys&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Note|This will not work using the dependency-based boot sequence introduced with Squeeze. See the section below. }}&lt;br /&gt;
&lt;br /&gt;
=== Fix SSH host keys in Squeeze when using dependency-based booting ===&lt;br /&gt;
&lt;br /&gt;
 rm -f /etc/ssh/ssh_host_*&lt;br /&gt;
&amp;lt;!-- please do not remove &amp;lt;source&amp;gt;...&amp;lt;/source&amp;gt; pair of tags below,&lt;br /&gt;
     otherwise quotes after -N (-N '') are not visible --&amp;gt;&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
cat &amp;lt;&amp;lt; EOF &amp;gt; /etc/init.d/ssh_gen_host_keys&lt;br /&gt;
#!/bin/sh&lt;br /&gt;
### BEGIN INIT INFO&lt;br /&gt;
# Provides:          Generates new ssh host keys on first boot&lt;br /&gt;
# Required-Start:    $remote_fs $syslog&lt;br /&gt;
# Required-Stop:     $remote_fs $syslog&lt;br /&gt;
# Default-Start:     2 3 4 5&lt;br /&gt;
# Default-Stop:&lt;br /&gt;
# Short-Description: Generates new ssh host keys on first boot&lt;br /&gt;
# Description:       Generates new ssh host keys on first boot&lt;br /&gt;
### END INIT INFO&lt;br /&gt;
ssh-keygen -f /etc/ssh/ssh_host_rsa_key -t rsa -N &amp;quot;&amp;quot;&lt;br /&gt;
ssh-keygen -f /etc/ssh/ssh_host_dsa_key -t dsa -N &amp;quot;&amp;quot;&lt;br /&gt;
insserv -r /etc/init.d/ssh_gen_host_keys&lt;br /&gt;
rm -f \$0&lt;br /&gt;
EOF&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
 chmod a+x /etc/init.d/ssh_gen_host_keys&lt;br /&gt;
 insserv /etc/init.d/ssh_gen_host_keys&lt;br /&gt;
&lt;br /&gt;
=== Change timezone ===&lt;br /&gt;
&lt;br /&gt;
You might want to change timezone if you do not live in $UTC. The following example is for Germany&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ln -sf /usr/share/zoneinfo/Europe/Berlin /etc/localtime&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
or even better&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
dpkg-reconfigure tzdata&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Clean packages ===&lt;br /&gt;
After installing packages, you'll have some junk packages laying around in your cache.  Since you don't want your template to have those, this command will wipe them out.&lt;br /&gt;
 apt-get --purge clean&lt;br /&gt;
&lt;br /&gt;
Now everything is done.  Exit from the template and go back to the hardware node.&lt;br /&gt;
 exit&lt;br /&gt;
&lt;br /&gt;
== Preparing for and packing template cache ==&lt;br /&gt;
&lt;br /&gt;
We don't need an IP for the VE anymore, and we definitely do not need it in template cache, so remove it:&lt;br /&gt;
 sudo vzctl set 777 --ipdel all --save&lt;br /&gt;
&lt;br /&gt;
Also, remove DNS server and search domain information from ''/etc/resolv.conf'' file '''in VE''':&lt;br /&gt;
 sudo nano /vz/private/777/etc/resolv.conf&lt;br /&gt;
&lt;br /&gt;
Also, remove ''/etc/hostname'' file '''in VE''':&lt;br /&gt;
 sudo rm -f /vz/private/777/etc/hostname&lt;br /&gt;
&lt;br /&gt;
Stop the VE:&lt;br /&gt;
 sudo vzctl stop 777&lt;br /&gt;
&lt;br /&gt;
Go to the VE directory:&lt;br /&gt;
 cd /vz/private/777&lt;br /&gt;
&lt;br /&gt;
Now create a cached OS tarball.  In the command below, you'll want to replace &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; with your architecture (i386, amd64, ia64, etc).&lt;br /&gt;
 sudo tar --numeric-owner -zcf /vz/template/cache/debian-5.0-i386-minimal.tar.gz .&lt;br /&gt;
&lt;br /&gt;
Look at the resulting tarball to see its size is sane:&lt;br /&gt;
 # ls -lh /vz/template/cache&lt;br /&gt;
 -rw-r--r--  1 root root  51M Apr 10 03:16 debian-5.0-i386-minimal.tar.gz&lt;br /&gt;
&lt;br /&gt;
== Checking if template cache works ==&lt;br /&gt;
We can now create a VE based on the just-created template cache.  Be sure to change &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; to your architecture just like you did when you named the tarball above.&lt;br /&gt;
 sudo vzctl create 123456 --ostemplate debian-5.0-i386-minimal&lt;br /&gt;
&lt;br /&gt;
Now make sure that it works:&lt;br /&gt;
 sudo vzctl start 123456&lt;br /&gt;
 sudo vzctl exec 123456 ps ax&lt;br /&gt;
&lt;br /&gt;
You should see that a few processes are running.&lt;br /&gt;
&lt;br /&gt;
== Final cleanup ==&lt;br /&gt;
Stop and remove the test VE you just created:&lt;br /&gt;
 sudo vzctl stop 123456&lt;br /&gt;
 sudo vzctl destroy 123456&lt;br /&gt;
 sudo rm /etc/vz/conf/123456.conf.destroyed&lt;br /&gt;
&lt;br /&gt;
Finally, let's remove the VE we used for OS template cache creation:&lt;br /&gt;
 sudo vzctl destroy 777&lt;br /&gt;
 sudo rm /etc/vz/conf/777.conf.destroyed&lt;br /&gt;
&lt;br /&gt;
You might want to edit /etc/vz/vz.conf and change DEF_OSTEMPLATE to the name of the template you use most often so that you don't have to specify the template when creating a VE.&lt;br /&gt;
 DEF_OSTEMPLATE=&amp;quot;debian-5.0-i386-minimal&amp;quot;&lt;br /&gt;
If you use iptables, you might want to include additional modules in the list for IPTABLES in /etc/vz/vz.conf. See ''man vzctl'' for a list of available modules.&lt;br /&gt;
&lt;br /&gt;
[[Category: HOWTO]]&lt;br /&gt;
[[Category: Templates]]&lt;br /&gt;
[[Category: Debian]]&lt;/div&gt;</summary>
		<author><name>Mose</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Debian_template_creation&amp;diff=10704</id>
		<title>Debian template creation</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Debian_template_creation&amp;diff=10704"/>
		<updated>2011-07-23T11:18:01Z</updated>

		<summary type="html">&lt;p&gt;Mose: /* Setting VE OSTEMPLATE */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;These are rough instructions of how to manually create basic Debian Etch (4.0) template cache, which can be used to create OpenVZ [[VE]]s based on Debian Etch (4.0). (see also &amp;lt;tt&amp;gt;/usr/share/doc/vzctl/README.Debian&amp;lt;/tt&amp;gt; in the ''vzctl'' Debian package)&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* You shouldn't be running as root, but as a user that is permitted to use sudo instead.  It's a dangerous idea, run as root at your peril.&lt;br /&gt;
* Anywhere you see &amp;lt;tt&amp;gt;/vz&amp;lt;/tt&amp;gt;, you might really need to use &amp;lt;tt&amp;gt;/var/lib/vz&amp;lt;/tt&amp;gt; instead, especially on a Debian Etch host.&lt;br /&gt;
* Anywhere you see &amp;lt;tt&amp;gt;http://http.us.debian.org/debian/&amp;lt;/tt&amp;gt;, you can substitute your favorite Debian mirror.  ([http://www.debian.org/mirror/list List of official Debian Mirrors])&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Prerequisites ==&lt;br /&gt;
&lt;br /&gt;
{{Warning|if you want to use &amp;lt;code&amp;gt;ext4&amp;lt;/code&amp;gt; file system for &amp;lt;code&amp;gt;/vz&amp;lt;/code&amp;gt;, use &amp;lt;code&amp;gt;nodelalloc&amp;lt;/code&amp;gt; option in &amp;lt;code&amp;gt;/etc/fstab&amp;lt;/code&amp;gt;, otherwise it will crash. See {{Bug|1509}} and its duplicates for details.}}&lt;br /&gt;
&lt;br /&gt;
You need to have a working copy of &amp;lt;tt&amp;gt;debootstrap&amp;lt;/tt&amp;gt; running on your hardware node.&lt;br /&gt;
&lt;br /&gt;
For Debian:&lt;br /&gt;
 sudo apt-get install debootstrap&lt;br /&gt;
&lt;br /&gt;
For Gentoo:&lt;br /&gt;
 sudo emerge debootstrap&lt;br /&gt;
&lt;br /&gt;
For Fedora (at least Fedora 8 have it, not sure about earlier versions):&lt;br /&gt;
 sudo yum install debootstrap&lt;br /&gt;
&lt;br /&gt;
For other distros you might need to install it from sources, or search for an appropriate package for your distribution.  An RPM is available on the [http://forum.openvz.org/index.php?t=tree&amp;amp;th=142&amp;amp;mid=584 OpenVZ Forum].&lt;br /&gt;
&lt;br /&gt;
== Bootstrapping Debian ==&lt;br /&gt;
&lt;br /&gt;
You can install different releases of Debian into a VE's private directory using the debootstrap command.&lt;br /&gt;
&lt;br /&gt;
The command parameters are:&lt;br /&gt;
&lt;br /&gt;
  debootstrap --arch ARCH NAME DIRECTORY URL&lt;br /&gt;
&lt;br /&gt;
Specify your architecture instead of &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; if you're using something other than i386/x86.  For example, for AMD64/x86_64, use &amp;lt;tt&amp;gt;amd64&amp;lt;/tt&amp;gt; or for ia64, use &amp;lt;tt&amp;gt;ia64&amp;lt;/tt&amp;gt;. You can use http or ftp in the URL.&lt;br /&gt;
&lt;br /&gt;
We use VE ID of 777 for this example, but it can be any unused ID.&lt;br /&gt;
&lt;br /&gt;
=== Squeeze (current Debian stable) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap --arch i386 squeeze /vz/private/777 http://http.us.debian.org/debian/&lt;br /&gt;
 or&lt;br /&gt;
 debootstrap --arch amd64 squeeze /vz/private/777 ftp://ftp.us.debian.org/debian/&lt;br /&gt;
&lt;br /&gt;
=== Lenny (Debian oldstable) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap --arch i386 lenny /vz/private/777 http://http.us.debian.org/debian/&lt;br /&gt;
 or&lt;br /&gt;
 debootstrap --arch amd64 lenny /vz/private/777 ftp://ftp.us.debian.org/debian/&lt;br /&gt;
&lt;br /&gt;
=== Etch (old release) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap --arch i386 etch /vz/private/777 http://http.us.debian.org/debian/&lt;br /&gt;
&lt;br /&gt;
=== Sarge (very old release) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap sarge /vz/private/777 http://archive.debian.org/debian&lt;br /&gt;
&lt;br /&gt;
== Preparing the HN network ==&lt;br /&gt;
Append the following lines to /etc/sysctl.conf, adjust to taste and then execute &amp;quot;sysctl -p&amp;quot; for them to take effect.&lt;br /&gt;
 ### OpenVZ settings&lt;br /&gt;
 &lt;br /&gt;
 # On Hardware Node we generally need packet&lt;br /&gt;
 # forwarding enabled and proxy arp disabled&lt;br /&gt;
 &lt;br /&gt;
 net.ipv4.conf.default.forwarding=1&lt;br /&gt;
 net.ipv4.conf.default.proxy_arp = 0&lt;br /&gt;
 net.ipv4.ip_forward=1&lt;br /&gt;
 &lt;br /&gt;
 # Enables source route verification&lt;br /&gt;
 net.ipv4.conf.all.rp_filter = 1&lt;br /&gt;
 &lt;br /&gt;
 # Enables the magic-sysrq key&lt;br /&gt;
 kernel.sysrq = 1&lt;br /&gt;
 &lt;br /&gt;
 # TCP Explict Congestion Notification&lt;br /&gt;
 net.ipv4.tcp_ecn = 0&lt;br /&gt;
 &lt;br /&gt;
 # we do not want all our interfaces to send redirects&lt;br /&gt;
 net.ipv4.conf.default.send_redirects = 1&lt;br /&gt;
 net.ipv4.conf.all.send_redirects = 0&lt;br /&gt;
&lt;br /&gt;
== Preparing and starting the VE ==&lt;br /&gt;
&lt;br /&gt;
=== Setting VE config ===&lt;br /&gt;
First, we need a config for the [[VE]]:&lt;br /&gt;
 sudo vzctl set 777 --applyconfig vps.basic --save&lt;br /&gt;
&lt;br /&gt;
On debian squeeze only the following worked for me (''confirmed''), because the standard template names in /etc/vz/conf have changed.&lt;br /&gt;
 sudo vzctl set 777 --applyconfig basic --save&lt;br /&gt;
&lt;br /&gt;
=== Setting VE OSTEMPLATE ===&lt;br /&gt;
Also, we need &amp;lt;tt&amp;gt;OSTEMPLATE&amp;lt;/tt&amp;gt; to be set in VE configuration file, for [[vzctl]] to work properly.&lt;br /&gt;
&lt;br /&gt;
 sudo sh -c 'echo OSTEMPLATE=\&amp;quot;debian-5.0\&amp;quot;' &amp;gt;&amp;gt; /etc/vz/conf/777.conf&lt;br /&gt;
&lt;br /&gt;
=== Setting VE IP address ===&lt;br /&gt;
For the [[VE]] to be able to download updates from the Internet, we need a valid IP address for it:&lt;br /&gt;
 sudo vzctl set 777 --ipadd x.x.x.x --save&lt;br /&gt;
&lt;br /&gt;
{{Note|if you use private IP for the VE, you might have to set up NAT as described in [[Using NAT for VE with private IPs]].}}&lt;br /&gt;
&lt;br /&gt;
=== Setting DNS server for VE ===&lt;br /&gt;
For the [[VE]] to be able to download updates from the Internet, we also need to specify a DNS for it:&lt;br /&gt;
 sudo vzctl set 777 --nameserver x.x.x.x --save&lt;br /&gt;
&lt;br /&gt;
=== Creating /dev/ptmx  ===&lt;br /&gt;
The ptmx character device should normally exist, but if it doesn't, create one.&lt;br /&gt;
 sudo mknod --mode 666 /var/lib/vz/private/777/dev/ptmx c 5 2&lt;br /&gt;
&lt;br /&gt;
=== Starting VE ===&lt;br /&gt;
Now start the VE:&lt;br /&gt;
 sudo vzctl start 777&lt;br /&gt;
&lt;br /&gt;
== Customizing the installation ==&lt;br /&gt;
A few things need to be done inside a newly created VE for it to become suitable for OpenVZ. Enter the VE to begin the configuration. Exporting the path is optional.&lt;br /&gt;
 sudo vzctl enter 777&lt;br /&gt;
 export PATH=/sbin:/usr/sbin:/bin:/usr/bin&lt;br /&gt;
&lt;br /&gt;
{{Warning|Do not run the commands below on the hardware node, they are only to be run within the VE!}}&lt;br /&gt;
&lt;br /&gt;
=== Set Debian repositories ===&lt;br /&gt;
 cat &amp;lt;&amp;lt;EOF &amp;gt; /etc/apt/sources.list&lt;br /&gt;
 deb http://http.us.debian.org/debian squeeze main contrib&lt;br /&gt;
 deb http://security.debian.org squeeze/updates main contrib&lt;br /&gt;
 deb http://http.us.debian.org/debian squeeze-updates main&lt;br /&gt;
 ## backports - ONLY IF YOU KNOW WHAT YOU DO&lt;br /&gt;
 # deb http://http.us.debian.org/debian-backports/ squeeze-backports main&lt;br /&gt;
 EOF&lt;br /&gt;
&lt;br /&gt;
=== Get new security updates ===&lt;br /&gt;
 apt-get update&lt;br /&gt;
 apt-get upgrade&lt;br /&gt;
&lt;br /&gt;
=== Install some more packages ===&lt;br /&gt;
Installing packages could be an interactive process so the system might ask some questions.  You can install more packages if you'd like.  For example:&lt;br /&gt;
 apt-get install ssh quota less&lt;br /&gt;
&lt;br /&gt;
=== Set sane permissions for &amp;lt;tt&amp;gt;/root&amp;lt;/tt&amp;gt; directory ===&lt;br /&gt;
 chmod 700 /root&lt;br /&gt;
&lt;br /&gt;
=== Disable root login ===&lt;br /&gt;
This will disable root login by default.&lt;br /&gt;
 usermod -L root&lt;br /&gt;
&lt;br /&gt;
=== Disable getty ===&lt;br /&gt;
Disable running &amp;lt;tt&amp;gt;getty&amp;lt;/tt&amp;gt;s on terminals as a VE does not have any:&lt;br /&gt;
 sed -i -e '/getty/d' /etc/inittab&lt;br /&gt;
&lt;br /&gt;
=== Disable &amp;lt;tt&amp;gt;sync()&amp;lt;/tt&amp;gt; for syslog ===&lt;br /&gt;
Turn off doing &amp;lt;tt&amp;gt;sync()&amp;lt;/tt&amp;gt; on every write for &amp;lt;tt&amp;gt;syslog&amp;lt;/tt&amp;gt;'s log files, to improve I/O performance:&lt;br /&gt;
 &amp;lt;pre&amp;gt;sed -i -e 's@\([[:space:]]\)\(/var/log/\)@\1-\2@' /etc/*syslog.conf&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Fix &amp;lt;tt&amp;gt;/etc/mtab&amp;lt;/tt&amp;gt; ===&lt;br /&gt;
Link &amp;lt;tt&amp;gt;/etc/mtab&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;/proc/mounts&amp;lt;/tt&amp;gt;, so &amp;lt;tt&amp;gt;df&amp;lt;/tt&amp;gt; and friends will work:&lt;br /&gt;
 rm -f /etc/mtab&lt;br /&gt;
 ln -s /proc/mounts /etc/mtab&lt;br /&gt;
&lt;br /&gt;
=== Remove some unneeded packages ===&lt;br /&gt;
If you have any packages you'd like to remove, now's the time for it.  Here's an example — note that not all of those packages are installed by default in Debian Squeeze (although they were in earlier versions):&lt;br /&gt;
 dpkg --purge modutils ppp pppoeconf pppoe pppconfig module-init-tools&lt;br /&gt;
&lt;br /&gt;
=== Disable services ===&lt;br /&gt;
Do not start some services, stick to bare minimum:&lt;br /&gt;
 update-rc.d -f klogd remove&lt;br /&gt;
 update-rc.d -f quotarpc remove&lt;br /&gt;
 update-rc.d -f exim4 remove&lt;br /&gt;
 update-rc.d -f inetd remove&lt;br /&gt;
&lt;br /&gt;
For dependency-based boot sequence introduced with Squeeze type:&lt;br /&gt;
&lt;br /&gt;
 update-rc.d-insserv -f klogd remove&lt;br /&gt;
 update-rc.d-insserv -f quotarpc remove&lt;br /&gt;
 update-rc.d-insserv -f exim4 remove&lt;br /&gt;
 update-rc.d-insserv -f inetd remove&lt;br /&gt;
&lt;br /&gt;
=== Fix SSH host keys ===&lt;br /&gt;
This is only useful if you installed SSH.  Each individual [[VE]] should have its own pair of SSH host keys.  The code below will wipe out the existing SSH keys and instruct the newly-created [[VE]] to create new SSH keys on first boot.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- please do not remove &amp;lt;source&amp;gt;...&amp;lt;/source&amp;gt; pair of tags below,&lt;br /&gt;
     otherwise quotes after -N (-N '') are not visible --&amp;gt;&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
rm -f /etc/ssh/ssh_host_*&lt;br /&gt;
cat &amp;lt;&amp;lt; EOF &amp;gt; /etc/rc2.d/S15ssh_gen_host_keys&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
ssh-keygen -f /etc/ssh/ssh_host_rsa_key -t rsa -N ''&lt;br /&gt;
ssh-keygen -f /etc/ssh/ssh_host_dsa_key -t dsa -N ''&lt;br /&gt;
rm -f \$0&lt;br /&gt;
EOF&lt;br /&gt;
chmod a+x /etc/rc2.d/S15ssh_gen_host_keys&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Note|This will not work using the dependency-based boot sequence introduced with Squeeze. See the section below. }}&lt;br /&gt;
&lt;br /&gt;
=== Fix SSH host keys in Squeeze when using dependency-based booting ===&lt;br /&gt;
&lt;br /&gt;
 rm -f /etc/ssh/ssh_host_*&lt;br /&gt;
&amp;lt;!-- please do not remove &amp;lt;source&amp;gt;...&amp;lt;/source&amp;gt; pair of tags below,&lt;br /&gt;
     otherwise quotes after -N (-N '') are not visible --&amp;gt;&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
cat &amp;lt;&amp;lt; EOF &amp;gt; /etc/init.d/ssh_gen_host_keys&lt;br /&gt;
#!/bin/sh&lt;br /&gt;
### BEGIN INIT INFO&lt;br /&gt;
# Provides:          Generates new ssh host keys on first boot&lt;br /&gt;
# Required-Start:    $remote_fs $syslog&lt;br /&gt;
# Required-Stop:     $remote_fs $syslog&lt;br /&gt;
# Default-Start:     2 3 4 5&lt;br /&gt;
# Default-Stop:&lt;br /&gt;
# Short-Description: Generates new ssh host keys on first boot&lt;br /&gt;
# Description:       Generates new ssh host keys on first boot&lt;br /&gt;
### END INIT INFO&lt;br /&gt;
ssh-keygen -f /etc/ssh/ssh_host_rsa_key -t rsa -N ''&lt;br /&gt;
ssh-keygen -f /etc/ssh/ssh_host_dsa_key -t dsa -N ''&lt;br /&gt;
insserv -r /etc/init.d/ssh_gen_host_keys&lt;br /&gt;
rm -f \$0&lt;br /&gt;
EOF&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
 chmod a+x /etc/init.d/ssh_gen_host_keys&lt;br /&gt;
 insserv /etc/init.d/ssh_gen_host_keys&lt;br /&gt;
&lt;br /&gt;
=== Change timezone ===&lt;br /&gt;
&lt;br /&gt;
You might want to change timezone if you do not live in $UTC. The following example is for Germany&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ln -sf /usr/share/zoneinfo/Europe/Berlin /etc/localtime&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
or even better&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
dpkg-reconfigure tzdata&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Clean packages ===&lt;br /&gt;
After installing packages, you'll have some junk packages laying around in your cache.  Since you don't want your template to have those, this command will wipe them out.&lt;br /&gt;
 apt-get --purge clean&lt;br /&gt;
&lt;br /&gt;
Now everything is done.  Exit from the template and go back to the hardware node.&lt;br /&gt;
 exit&lt;br /&gt;
&lt;br /&gt;
== Preparing for and packing template cache ==&lt;br /&gt;
&lt;br /&gt;
We don't need an IP for the VE anymore, and we definitely do not need it in template cache, so remove it:&lt;br /&gt;
 sudo vzctl set 777 --ipdel all --save&lt;br /&gt;
&lt;br /&gt;
Also, remove DNS server and search domain information from ''/etc/resolv.conf'' file '''in VE''':&lt;br /&gt;
 sudo nano /vz/private/777/etc/resolv.conf&lt;br /&gt;
&lt;br /&gt;
Also, remove ''/etc/hostname'' file '''in VE''':&lt;br /&gt;
 sudo rm -f /vz/private/777/etc/hostname&lt;br /&gt;
&lt;br /&gt;
Stop the VE:&lt;br /&gt;
 sudo vzctl stop 777&lt;br /&gt;
&lt;br /&gt;
Go to the VE directory:&lt;br /&gt;
 cd /vz/private/777&lt;br /&gt;
&lt;br /&gt;
Now create a cached OS tarball.  In the command below, you'll want to replace &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; with your architecture (i386, amd64, ia64, etc).&lt;br /&gt;
 sudo tar --numeric-owner -zcf /vz/template/cache/debian-5.0-i386-minimal.tar.gz .&lt;br /&gt;
&lt;br /&gt;
Look at the resulting tarball to see its size is sane:&lt;br /&gt;
 # ls -lh /vz/template/cache&lt;br /&gt;
 -rw-r--r--  1 root root  51M Apr 10 03:16 debian-5.0-i386-minimal.tar.gz&lt;br /&gt;
&lt;br /&gt;
== Checking if template cache works ==&lt;br /&gt;
We can now create a VE based on the just-created template cache.  Be sure to change &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; to your architecture just like you did when you named the tarball above.&lt;br /&gt;
 sudo vzctl create 123456 --ostemplate debian-5.0-i386-minimal&lt;br /&gt;
&lt;br /&gt;
Now make sure that it works:&lt;br /&gt;
 sudo vzctl start 123456&lt;br /&gt;
 sudo vzctl exec 123456 ps ax&lt;br /&gt;
&lt;br /&gt;
You should see that a few processes are running.&lt;br /&gt;
&lt;br /&gt;
== Final cleanup ==&lt;br /&gt;
Stop and remove the test VE you just created:&lt;br /&gt;
 sudo vzctl stop 123456&lt;br /&gt;
 sudo vzctl destroy 123456&lt;br /&gt;
 sudo rm /etc/vz/conf/123456.conf.destroyed&lt;br /&gt;
&lt;br /&gt;
Finally, let's remove the VE we used for OS template cache creation:&lt;br /&gt;
 sudo vzctl destroy 777&lt;br /&gt;
 sudo rm /etc/vz/conf/777.conf.destroyed&lt;br /&gt;
&lt;br /&gt;
You might want to edit /etc/vz/vz.conf and change DEF_OSTEMPLATE to the name of the template you use most often so that you don't have to specify the template when creating a VE.&lt;br /&gt;
 DEF_OSTEMPLATE=&amp;quot;debian-5.0-i386-minimal&amp;quot;&lt;br /&gt;
If you use iptables, you might want to include additional modules in the list for IPTABLES in /etc/vz/vz.conf. See ''man vzctl'' for a list of available modules.&lt;br /&gt;
&lt;br /&gt;
[[Category: HOWTO]]&lt;br /&gt;
[[Category: Templates]]&lt;br /&gt;
[[Category: Debian]]&lt;/div&gt;</summary>
		<author><name>Mose</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Debian_template_creation&amp;diff=10703</id>
		<title>Debian template creation</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Debian_template_creation&amp;diff=10703"/>
		<updated>2011-07-23T11:17:30Z</updated>

		<summary type="html">&lt;p&gt;Mose: /* Setting VE config */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;These are rough instructions of how to manually create basic Debian Etch (4.0) template cache, which can be used to create OpenVZ [[VE]]s based on Debian Etch (4.0). (see also &amp;lt;tt&amp;gt;/usr/share/doc/vzctl/README.Debian&amp;lt;/tt&amp;gt; in the ''vzctl'' Debian package)&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* You shouldn't be running as root, but as a user that is permitted to use sudo instead.  It's a dangerous idea, run as root at your peril.&lt;br /&gt;
* Anywhere you see &amp;lt;tt&amp;gt;/vz&amp;lt;/tt&amp;gt;, you might really need to use &amp;lt;tt&amp;gt;/var/lib/vz&amp;lt;/tt&amp;gt; instead, especially on a Debian Etch host.&lt;br /&gt;
* Anywhere you see &amp;lt;tt&amp;gt;http://http.us.debian.org/debian/&amp;lt;/tt&amp;gt;, you can substitute your favorite Debian mirror.  ([http://www.debian.org/mirror/list List of official Debian Mirrors])&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Prerequisites ==&lt;br /&gt;
&lt;br /&gt;
{{Warning|if you want to use &amp;lt;code&amp;gt;ext4&amp;lt;/code&amp;gt; file system for &amp;lt;code&amp;gt;/vz&amp;lt;/code&amp;gt;, use &amp;lt;code&amp;gt;nodelalloc&amp;lt;/code&amp;gt; option in &amp;lt;code&amp;gt;/etc/fstab&amp;lt;/code&amp;gt;, otherwise it will crash. See {{Bug|1509}} and its duplicates for details.}}&lt;br /&gt;
&lt;br /&gt;
You need to have a working copy of &amp;lt;tt&amp;gt;debootstrap&amp;lt;/tt&amp;gt; running on your hardware node.&lt;br /&gt;
&lt;br /&gt;
For Debian:&lt;br /&gt;
 sudo apt-get install debootstrap&lt;br /&gt;
&lt;br /&gt;
For Gentoo:&lt;br /&gt;
 sudo emerge debootstrap&lt;br /&gt;
&lt;br /&gt;
For Fedora (at least Fedora 8 have it, not sure about earlier versions):&lt;br /&gt;
 sudo yum install debootstrap&lt;br /&gt;
&lt;br /&gt;
For other distros you might need to install it from sources, or search for an appropriate package for your distribution.  An RPM is available on the [http://forum.openvz.org/index.php?t=tree&amp;amp;th=142&amp;amp;mid=584 OpenVZ Forum].&lt;br /&gt;
&lt;br /&gt;
== Bootstrapping Debian ==&lt;br /&gt;
&lt;br /&gt;
You can install different releases of Debian into a VE's private directory using the debootstrap command.&lt;br /&gt;
&lt;br /&gt;
The command parameters are:&lt;br /&gt;
&lt;br /&gt;
  debootstrap --arch ARCH NAME DIRECTORY URL&lt;br /&gt;
&lt;br /&gt;
Specify your architecture instead of &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; if you're using something other than i386/x86.  For example, for AMD64/x86_64, use &amp;lt;tt&amp;gt;amd64&amp;lt;/tt&amp;gt; or for ia64, use &amp;lt;tt&amp;gt;ia64&amp;lt;/tt&amp;gt;. You can use http or ftp in the URL.&lt;br /&gt;
&lt;br /&gt;
We use VE ID of 777 for this example, but it can be any unused ID.&lt;br /&gt;
&lt;br /&gt;
=== Squeeze (current Debian stable) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap --arch i386 squeeze /vz/private/777 http://http.us.debian.org/debian/&lt;br /&gt;
 or&lt;br /&gt;
 debootstrap --arch amd64 squeeze /vz/private/777 ftp://ftp.us.debian.org/debian/&lt;br /&gt;
&lt;br /&gt;
=== Lenny (Debian oldstable) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap --arch i386 lenny /vz/private/777 http://http.us.debian.org/debian/&lt;br /&gt;
 or&lt;br /&gt;
 debootstrap --arch amd64 lenny /vz/private/777 ftp://ftp.us.debian.org/debian/&lt;br /&gt;
&lt;br /&gt;
=== Etch (old release) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap --arch i386 etch /vz/private/777 http://http.us.debian.org/debian/&lt;br /&gt;
&lt;br /&gt;
=== Sarge (very old release) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap sarge /vz/private/777 http://archive.debian.org/debian&lt;br /&gt;
&lt;br /&gt;
== Preparing the HN network ==&lt;br /&gt;
Append the following lines to /etc/sysctl.conf, adjust to taste and then execute &amp;quot;sysctl -p&amp;quot; for them to take effect.&lt;br /&gt;
 ### OpenVZ settings&lt;br /&gt;
 &lt;br /&gt;
 # On Hardware Node we generally need packet&lt;br /&gt;
 # forwarding enabled and proxy arp disabled&lt;br /&gt;
 &lt;br /&gt;
 net.ipv4.conf.default.forwarding=1&lt;br /&gt;
 net.ipv4.conf.default.proxy_arp = 0&lt;br /&gt;
 net.ipv4.ip_forward=1&lt;br /&gt;
 &lt;br /&gt;
 # Enables source route verification&lt;br /&gt;
 net.ipv4.conf.all.rp_filter = 1&lt;br /&gt;
 &lt;br /&gt;
 # Enables the magic-sysrq key&lt;br /&gt;
 kernel.sysrq = 1&lt;br /&gt;
 &lt;br /&gt;
 # TCP Explict Congestion Notification&lt;br /&gt;
 net.ipv4.tcp_ecn = 0&lt;br /&gt;
 &lt;br /&gt;
 # we do not want all our interfaces to send redirects&lt;br /&gt;
 net.ipv4.conf.default.send_redirects = 1&lt;br /&gt;
 net.ipv4.conf.all.send_redirects = 0&lt;br /&gt;
&lt;br /&gt;
== Preparing and starting the VE ==&lt;br /&gt;
&lt;br /&gt;
=== Setting VE config ===&lt;br /&gt;
First, we need a config for the [[VE]]:&lt;br /&gt;
 sudo vzctl set 777 --applyconfig vps.basic --save&lt;br /&gt;
&lt;br /&gt;
On debian squeeze only the following worked for me (''confirmed''), because the standard template names in /etc/vz/conf have changed.&lt;br /&gt;
 sudo vzctl set 777 --applyconfig basic --save&lt;br /&gt;
&lt;br /&gt;
=== Setting VE OSTEMPLATE ===&lt;br /&gt;
Also, we need &amp;lt;tt&amp;gt;OSTEMPLATE&amp;lt;/tt&amp;gt; to be set in VE configuration file, for the [[vzctl]] to work properly.&lt;br /&gt;
&lt;br /&gt;
 sudo sh -c 'echo OSTEMPLATE=\&amp;quot;debian-5.0\&amp;quot;' &amp;gt;&amp;gt; /etc/vz/conf/777.conf&lt;br /&gt;
&lt;br /&gt;
=== Setting VE IP address ===&lt;br /&gt;
For the [[VE]] to be able to download updates from the Internet, we need a valid IP address for it:&lt;br /&gt;
 sudo vzctl set 777 --ipadd x.x.x.x --save&lt;br /&gt;
&lt;br /&gt;
{{Note|if you use private IP for the VE, you might have to set up NAT as described in [[Using NAT for VE with private IPs]].}}&lt;br /&gt;
&lt;br /&gt;
=== Setting DNS server for VE ===&lt;br /&gt;
For the [[VE]] to be able to download updates from the Internet, we also need to specify a DNS for it:&lt;br /&gt;
 sudo vzctl set 777 --nameserver x.x.x.x --save&lt;br /&gt;
&lt;br /&gt;
=== Creating /dev/ptmx  ===&lt;br /&gt;
The ptmx character device should normally exist, but if it doesn't, create one.&lt;br /&gt;
 sudo mknod --mode 666 /var/lib/vz/private/777/dev/ptmx c 5 2&lt;br /&gt;
&lt;br /&gt;
=== Starting VE ===&lt;br /&gt;
Now start the VE:&lt;br /&gt;
 sudo vzctl start 777&lt;br /&gt;
&lt;br /&gt;
== Customizing the installation ==&lt;br /&gt;
A few things need to be done inside a newly created VE for it to become suitable for OpenVZ. Enter the VE to begin the configuration. Exporting the path is optional.&lt;br /&gt;
 sudo vzctl enter 777&lt;br /&gt;
 export PATH=/sbin:/usr/sbin:/bin:/usr/bin&lt;br /&gt;
&lt;br /&gt;
{{Warning|Do not run the commands below on the hardware node, they are only to be run within the VE!}}&lt;br /&gt;
&lt;br /&gt;
=== Set Debian repositories ===&lt;br /&gt;
 cat &amp;lt;&amp;lt;EOF &amp;gt; /etc/apt/sources.list&lt;br /&gt;
 deb http://http.us.debian.org/debian squeeze main contrib&lt;br /&gt;
 deb http://security.debian.org squeeze/updates main contrib&lt;br /&gt;
 deb http://http.us.debian.org/debian squeeze-updates main&lt;br /&gt;
 ## backports - ONLY IF YOU KNOW WHAT YOU DO&lt;br /&gt;
 # deb http://http.us.debian.org/debian-backports/ squeeze-backports main&lt;br /&gt;
 EOF&lt;br /&gt;
&lt;br /&gt;
=== Get new security updates ===&lt;br /&gt;
 apt-get update&lt;br /&gt;
 apt-get upgrade&lt;br /&gt;
&lt;br /&gt;
=== Install some more packages ===&lt;br /&gt;
Installing packages could be an interactive process so the system might ask some questions.  You can install more packages if you'd like.  For example:&lt;br /&gt;
 apt-get install ssh quota less&lt;br /&gt;
&lt;br /&gt;
=== Set sane permissions for &amp;lt;tt&amp;gt;/root&amp;lt;/tt&amp;gt; directory ===&lt;br /&gt;
 chmod 700 /root&lt;br /&gt;
&lt;br /&gt;
=== Disable root login ===&lt;br /&gt;
This will disable root login by default.&lt;br /&gt;
 usermod -L root&lt;br /&gt;
&lt;br /&gt;
=== Disable getty ===&lt;br /&gt;
Disable running &amp;lt;tt&amp;gt;getty&amp;lt;/tt&amp;gt;s on terminals as a VE does not have any:&lt;br /&gt;
 sed -i -e '/getty/d' /etc/inittab&lt;br /&gt;
&lt;br /&gt;
=== Disable &amp;lt;tt&amp;gt;sync()&amp;lt;/tt&amp;gt; for syslog ===&lt;br /&gt;
Turn off doing &amp;lt;tt&amp;gt;sync()&amp;lt;/tt&amp;gt; on every write for &amp;lt;tt&amp;gt;syslog&amp;lt;/tt&amp;gt;'s log files, to improve I/O performance:&lt;br /&gt;
 &amp;lt;pre&amp;gt;sed -i -e 's@\([[:space:]]\)\(/var/log/\)@\1-\2@' /etc/*syslog.conf&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Fix &amp;lt;tt&amp;gt;/etc/mtab&amp;lt;/tt&amp;gt; ===&lt;br /&gt;
Link &amp;lt;tt&amp;gt;/etc/mtab&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;/proc/mounts&amp;lt;/tt&amp;gt;, so &amp;lt;tt&amp;gt;df&amp;lt;/tt&amp;gt; and friends will work:&lt;br /&gt;
 rm -f /etc/mtab&lt;br /&gt;
 ln -s /proc/mounts /etc/mtab&lt;br /&gt;
&lt;br /&gt;
=== Remove some unneeded packages ===&lt;br /&gt;
If you have any packages you'd like to remove, now's the time for it.  Here's an example — note that not all of those packages are installed by default in Debian Squeeze (although they were in earlier versions):&lt;br /&gt;
 dpkg --purge modutils ppp pppoeconf pppoe pppconfig module-init-tools&lt;br /&gt;
&lt;br /&gt;
=== Disable services ===&lt;br /&gt;
Do not start some services, stick to bare minimum:&lt;br /&gt;
 update-rc.d -f klogd remove&lt;br /&gt;
 update-rc.d -f quotarpc remove&lt;br /&gt;
 update-rc.d -f exim4 remove&lt;br /&gt;
 update-rc.d -f inetd remove&lt;br /&gt;
&lt;br /&gt;
For dependency-based boot sequence introduced with Squeeze type:&lt;br /&gt;
&lt;br /&gt;
 update-rc.d-insserv -f klogd remove&lt;br /&gt;
 update-rc.d-insserv -f quotarpc remove&lt;br /&gt;
 update-rc.d-insserv -f exim4 remove&lt;br /&gt;
 update-rc.d-insserv -f inetd remove&lt;br /&gt;
&lt;br /&gt;
=== Fix SSH host keys ===&lt;br /&gt;
This is only useful if you installed SSH.  Each individual [[VE]] should have its own pair of SSH host keys.  The code below will wipe out the existing SSH keys and instruct the newly-created [[VE]] to create new SSH keys on first boot.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- please do not remove &amp;lt;source&amp;gt;...&amp;lt;/source&amp;gt; pair of tags below,&lt;br /&gt;
     otherwise quotes after -N (-N '') are not visible --&amp;gt;&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
rm -f /etc/ssh/ssh_host_*&lt;br /&gt;
cat &amp;lt;&amp;lt; EOF &amp;gt; /etc/rc2.d/S15ssh_gen_host_keys&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
ssh-keygen -f /etc/ssh/ssh_host_rsa_key -t rsa -N ''&lt;br /&gt;
ssh-keygen -f /etc/ssh/ssh_host_dsa_key -t dsa -N ''&lt;br /&gt;
rm -f \$0&lt;br /&gt;
EOF&lt;br /&gt;
chmod a+x /etc/rc2.d/S15ssh_gen_host_keys&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Note|This will not work using the dependency-based boot sequence introduced with Squeeze. See the section below. }}&lt;br /&gt;
&lt;br /&gt;
=== Fix SSH host keys in Squeeze when using dependency-based booting ===&lt;br /&gt;
&lt;br /&gt;
 rm -f /etc/ssh/ssh_host_*&lt;br /&gt;
&amp;lt;!-- please do not remove &amp;lt;source&amp;gt;...&amp;lt;/source&amp;gt; pair of tags below,&lt;br /&gt;
     otherwise quotes after -N (-N '') are not visible --&amp;gt;&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
cat &amp;lt;&amp;lt; EOF &amp;gt; /etc/init.d/ssh_gen_host_keys&lt;br /&gt;
#!/bin/sh&lt;br /&gt;
### BEGIN INIT INFO&lt;br /&gt;
# Provides:          Generates new ssh host keys on first boot&lt;br /&gt;
# Required-Start:    $remote_fs $syslog&lt;br /&gt;
# Required-Stop:     $remote_fs $syslog&lt;br /&gt;
# Default-Start:     2 3 4 5&lt;br /&gt;
# Default-Stop:&lt;br /&gt;
# Short-Description: Generates new ssh host keys on first boot&lt;br /&gt;
# Description:       Generates new ssh host keys on first boot&lt;br /&gt;
### END INIT INFO&lt;br /&gt;
ssh-keygen -f /etc/ssh/ssh_host_rsa_key -t rsa -N ''&lt;br /&gt;
ssh-keygen -f /etc/ssh/ssh_host_dsa_key -t dsa -N ''&lt;br /&gt;
insserv -r /etc/init.d/ssh_gen_host_keys&lt;br /&gt;
rm -f \$0&lt;br /&gt;
EOF&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
 chmod a+x /etc/init.d/ssh_gen_host_keys&lt;br /&gt;
 insserv /etc/init.d/ssh_gen_host_keys&lt;br /&gt;
&lt;br /&gt;
=== Change timezone ===&lt;br /&gt;
&lt;br /&gt;
You might want to change timezone if you do not live in $UTC. The following example is for Germany&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ln -sf /usr/share/zoneinfo/Europe/Berlin /etc/localtime&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
or even better&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
dpkg-reconfigure tzdata&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Clean packages ===&lt;br /&gt;
After installing packages, you'll have some junk packages laying around in your cache.  Since you don't want your template to have those, this command will wipe them out.&lt;br /&gt;
 apt-get --purge clean&lt;br /&gt;
&lt;br /&gt;
Now everything is done.  Exit from the template and go back to the hardware node.&lt;br /&gt;
 exit&lt;br /&gt;
&lt;br /&gt;
== Preparing for and packing template cache ==&lt;br /&gt;
&lt;br /&gt;
We don't need an IP for the VE anymore, and we definitely do not need it in template cache, so remove it:&lt;br /&gt;
 sudo vzctl set 777 --ipdel all --save&lt;br /&gt;
&lt;br /&gt;
Also, remove DNS server and search domain information from ''/etc/resolv.conf'' file '''in VE''':&lt;br /&gt;
 sudo nano /vz/private/777/etc/resolv.conf&lt;br /&gt;
&lt;br /&gt;
Also, remove ''/etc/hostname'' file '''in VE''':&lt;br /&gt;
 sudo rm -f /vz/private/777/etc/hostname&lt;br /&gt;
&lt;br /&gt;
Stop the VE:&lt;br /&gt;
 sudo vzctl stop 777&lt;br /&gt;
&lt;br /&gt;
Go to the VE directory:&lt;br /&gt;
 cd /vz/private/777&lt;br /&gt;
&lt;br /&gt;
Now create a cached OS tarball.  In the command below, you'll want to replace &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; with your architecture (i386, amd64, ia64, etc).&lt;br /&gt;
 sudo tar --numeric-owner -zcf /vz/template/cache/debian-5.0-i386-minimal.tar.gz .&lt;br /&gt;
&lt;br /&gt;
Look at the resulting tarball to see its size is sane:&lt;br /&gt;
 # ls -lh /vz/template/cache&lt;br /&gt;
 -rw-r--r--  1 root root  51M Apr 10 03:16 debian-5.0-i386-minimal.tar.gz&lt;br /&gt;
&lt;br /&gt;
== Checking if template cache works ==&lt;br /&gt;
We can now create a VE based on the just-created template cache.  Be sure to change &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; to your architecture just like you did when you named the tarball above.&lt;br /&gt;
 sudo vzctl create 123456 --ostemplate debian-5.0-i386-minimal&lt;br /&gt;
&lt;br /&gt;
Now make sure that it works:&lt;br /&gt;
 sudo vzctl start 123456&lt;br /&gt;
 sudo vzctl exec 123456 ps ax&lt;br /&gt;
&lt;br /&gt;
You should see that a few processes are running.&lt;br /&gt;
&lt;br /&gt;
== Final cleanup ==&lt;br /&gt;
Stop and remove the test VE you just created:&lt;br /&gt;
 sudo vzctl stop 123456&lt;br /&gt;
 sudo vzctl destroy 123456&lt;br /&gt;
 sudo rm /etc/vz/conf/123456.conf.destroyed&lt;br /&gt;
&lt;br /&gt;
Finally, let's remove the VE we used for OS template cache creation:&lt;br /&gt;
 sudo vzctl destroy 777&lt;br /&gt;
 sudo rm /etc/vz/conf/777.conf.destroyed&lt;br /&gt;
&lt;br /&gt;
You might want to edit /etc/vz/vz.conf and change DEF_OSTEMPLATE to the name of the template you use most often so that you don't have to specify the template when creating a VE.&lt;br /&gt;
 DEF_OSTEMPLATE=&amp;quot;debian-5.0-i386-minimal&amp;quot;&lt;br /&gt;
If you use iptables, you might want to include additional modules in the list for IPTABLES in /etc/vz/vz.conf. See ''man vzctl'' for a list of available modules.&lt;br /&gt;
&lt;br /&gt;
[[Category: HOWTO]]&lt;br /&gt;
[[Category: Templates]]&lt;br /&gt;
[[Category: Debian]]&lt;/div&gt;</summary>
		<author><name>Mose</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Debian_template_creation&amp;diff=9195</id>
		<title>Debian template creation</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Debian_template_creation&amp;diff=9195"/>
		<updated>2010-09-30T14:39:30Z</updated>

		<summary type="html">&lt;p&gt;Mose: /* Final cleanup */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;These are rough instructions of how to manually create basic Debian Etch (4.0) template cache, which can be used to create OpenVZ [[VE]]s based on Debian Etch (4.0). (see also &amp;lt;tt&amp;gt;/usr/share/doc/vzctl/README.Debian&amp;lt;/tt&amp;gt; in the ''vzctl'' Debian package)&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* You shouldn't be running as root, but as a user that is permitted to use sudo instead.  It's a dangerous idea, run as root at your peril.&lt;br /&gt;
* Anywhere you see &amp;lt;tt&amp;gt;/vz&amp;lt;/tt&amp;gt;, you might really need to use &amp;lt;tt&amp;gt;/var/lib/vz&amp;lt;/tt&amp;gt; instead, especially on a Debian Etch host.&lt;br /&gt;
* Anywhere you see &amp;lt;tt&amp;gt;http://http.us.debian.org/debian/&amp;lt;/tt&amp;gt;, you can substitute your favorite Debian mirror.  ([http://www.debian.org/mirror/list List of official Debian Mirrors])&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Prerequisites ==&lt;br /&gt;
You need to have a working copy of &amp;lt;tt&amp;gt;debootstrap&amp;lt;/tt&amp;gt; running on your hardware node.&lt;br /&gt;
&lt;br /&gt;
For Debian:&lt;br /&gt;
 sudo apt-get install debootstrap&lt;br /&gt;
&lt;br /&gt;
For Gentoo:&lt;br /&gt;
 sudo emerge debootstrap&lt;br /&gt;
&lt;br /&gt;
For Fedora (at least Fedora 8 have it, not sure about earlier versions):&lt;br /&gt;
 sudo yum install debootstrap&lt;br /&gt;
&lt;br /&gt;
For other distros you might need to install it from sources, or search for an appropriate package for your distribution.  An RPM is available on the [http://forum.openvz.org/index.php?t=tree&amp;amp;th=142&amp;amp;mid=584 OpenVZ Forum].&lt;br /&gt;
&lt;br /&gt;
== Bootstrapping Debian ==&lt;br /&gt;
&lt;br /&gt;
You can install different releases of Debian into a VE's private directory using the debootstrap command.&lt;br /&gt;
&lt;br /&gt;
The command parameters are:&lt;br /&gt;
&lt;br /&gt;
  debootstrap --arch ARCH NAME DIRECTORY URL&lt;br /&gt;
&lt;br /&gt;
Specify your architecture instead of &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; if you're using something other than i386/x86.  For example, for AMD64/x86_64, use &amp;lt;tt&amp;gt;amd64&amp;lt;/tt&amp;gt; or for ia64, use &amp;lt;tt&amp;gt;ia64&amp;lt;/tt&amp;gt;. You can use http or ftp in the URL.&lt;br /&gt;
&lt;br /&gt;
We use VE ID of 777 for this example, but it can be any unused ID.&lt;br /&gt;
&lt;br /&gt;
=== Lenny (current Debian stable) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap --arch i386 lenny /vz/private/777 http://http.us.debian.org/debian/&lt;br /&gt;
 or&lt;br /&gt;
 debootstrap --arch amd64 lenny /vz/private/777 ftp://ftp.us.debian.org/debian/&lt;br /&gt;
&lt;br /&gt;
=== Etch (old release) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap --arch i386 etch /vz/private/777 http://http.us.debian.org/debian/&lt;br /&gt;
&lt;br /&gt;
=== Sarge (very old release) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap sarge /vz/private/777 http://archive.debian.org/debian&lt;br /&gt;
&lt;br /&gt;
== Preparing the HN network ==&lt;br /&gt;
Append the following lines to /etc/sysctl.conf, adjust to taste and then execute &amp;quot;sysctl -p&amp;quot; for them to take effect.&lt;br /&gt;
 ### OpenVZ settings&lt;br /&gt;
 &lt;br /&gt;
 # On Hardware Node we generally need packet&lt;br /&gt;
 # forwarding enabled and proxy arp disabled&lt;br /&gt;
 &lt;br /&gt;
 net.ipv4.conf.default.forwarding=1&lt;br /&gt;
 net.ipv4.conf.default.proxy_arp = 0&lt;br /&gt;
 net.ipv4.ip_forward=1&lt;br /&gt;
 &lt;br /&gt;
 # Enables source route verification&lt;br /&gt;
 net.ipv4.conf.all.rp_filter = 1&lt;br /&gt;
 &lt;br /&gt;
 # Enables the magic-sysrq key&lt;br /&gt;
 kernel.sysrq = 1&lt;br /&gt;
 &lt;br /&gt;
 # TCP Explict Congestion Notification&lt;br /&gt;
 net.ipv4.tcp_ecn = 0&lt;br /&gt;
 &lt;br /&gt;
 # we do not want all our interfaces to send redirects&lt;br /&gt;
 net.ipv4.conf.default.send_redirects = 1&lt;br /&gt;
 net.ipv4.conf.all.send_redirects = 0&lt;br /&gt;
&lt;br /&gt;
== Preparing and starting the VE ==&lt;br /&gt;
&lt;br /&gt;
=== Setting VE config ===&lt;br /&gt;
First, we need a config for the [[VE]]:&lt;br /&gt;
 sudo vzctl set 777 --applyconfig vps.basic --save&lt;br /&gt;
&lt;br /&gt;
=== Setting VE OSTEMPLATE ===&lt;br /&gt;
Also, we need &amp;lt;tt&amp;gt;OSTEMPLATE&amp;lt;/tt&amp;gt; to be set in VE configuration file, for the [[vzctl]] to work properly.&lt;br /&gt;
&lt;br /&gt;
 sudo sh -c 'echo &amp;quot;OSTEMPLATE=debian-5.0&amp;quot; &amp;gt;&amp;gt; /etc/vz/conf/777.conf'&lt;br /&gt;
&lt;br /&gt;
=== Setting VE IP address ===&lt;br /&gt;
For the [[VE]] to be able to download updates from the Internet, we need a valid IP address for it:&lt;br /&gt;
 sudo vzctl set 777 --ipadd x.x.x.x --save&lt;br /&gt;
&lt;br /&gt;
{{Note|if you use private IP for the VE, you might have to set up NAT as described in [[Using NAT for VE with private IPs]].}}&lt;br /&gt;
&lt;br /&gt;
=== Setting DNS server for VE ===&lt;br /&gt;
For the [[VE]] to be able to download updates from the Internet, we also need to specify a DNS for it:&lt;br /&gt;
 sudo vzctl set 777 --nameserver x.x.x.x --save&lt;br /&gt;
&lt;br /&gt;
=== Creating /dev/ptmx  ===&lt;br /&gt;
The ptmx character device should normally exist, but if it doesn't, create one.&lt;br /&gt;
 sudo mknod --mode 666 /var/lib/vz/private/777/dev/ptmx c 5 2&lt;br /&gt;
&lt;br /&gt;
=== Starting VE ===&lt;br /&gt;
Now start the VE:&lt;br /&gt;
 sudo vzctl start 777&lt;br /&gt;
&lt;br /&gt;
== Customizing the installation ==&lt;br /&gt;
A few things need to be done inside a newly created VE for it to become suitable for OpenVZ. Enter the VE to begin the configuration. Exporting the path is optional.&lt;br /&gt;
 sudo vzctl enter 777&lt;br /&gt;
 export PATH=/sbin:/usr/sbin:/bin:/usr/bin&lt;br /&gt;
&lt;br /&gt;
{{Warning|Do not run the commands below on the hardware node, they are only to be run within the VE!}}&lt;br /&gt;
&lt;br /&gt;
=== Set Debian repositories ===&lt;br /&gt;
 cat &amp;lt;&amp;lt;EOF &amp;gt; /etc/apt/sources.list&lt;br /&gt;
 deb http://http.us.debian.org/debian lenny main contrib&lt;br /&gt;
 deb http://security.debian.org lenny/updates main contrib&lt;br /&gt;
 deb http://security.debian.org lenny/volatile main contrib&lt;br /&gt;
 EOF&lt;br /&gt;
&lt;br /&gt;
=== Get new security updates ===&lt;br /&gt;
 apt-get update&lt;br /&gt;
 apt-get upgrade&lt;br /&gt;
&lt;br /&gt;
=== Install some more packages ===&lt;br /&gt;
Installing packages could be an interactive process so the system might ask some questions.  You can install more packages if you'd like.  For example:&lt;br /&gt;
 apt-get install ssh quota less&lt;br /&gt;
&lt;br /&gt;
=== Set sane permissions for &amp;lt;tt&amp;gt;/root&amp;lt;/tt&amp;gt; directory ===&lt;br /&gt;
 chmod 700 /root&lt;br /&gt;
&lt;br /&gt;
=== Disable root login ===&lt;br /&gt;
This will disable root login by default.&lt;br /&gt;
 usermod -L root&lt;br /&gt;
&lt;br /&gt;
=== Disable getty ===&lt;br /&gt;
Disable running &amp;lt;tt&amp;gt;getty&amp;lt;/tt&amp;gt;s on terminals as a VE does not have any:&lt;br /&gt;
 sed -i -e '/getty/d' /etc/inittab&lt;br /&gt;
&lt;br /&gt;
=== Disable &amp;lt;tt&amp;gt;sync()&amp;lt;/tt&amp;gt; for syslog ===&lt;br /&gt;
Turn off doing &amp;lt;tt&amp;gt;sync()&amp;lt;/tt&amp;gt; on every write for &amp;lt;tt&amp;gt;syslog&amp;lt;/tt&amp;gt;'s log files, to improve I/O performance:&lt;br /&gt;
 &amp;lt;pre&amp;gt;sed -i -e 's@\([[:space:]]\)\(/var/log/\)@\1-\2@' /etc/*syslog.conf&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Fix &amp;lt;tt&amp;gt;/etc/mtab&amp;lt;/tt&amp;gt; ===&lt;br /&gt;
Link &amp;lt;tt&amp;gt;/etc/mtab&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;/proc/mounts&amp;lt;/tt&amp;gt;, so &amp;lt;tt&amp;gt;df&amp;lt;/tt&amp;gt; and friends will work:&lt;br /&gt;
 rm -f /etc/mtab&lt;br /&gt;
 ln -s /proc/mounts /etc/mtab&lt;br /&gt;
&lt;br /&gt;
=== Remove some unneeded packages ===&lt;br /&gt;
If you have any packages you'd like to remove, now's the time for it.  Here's an example:&lt;br /&gt;
 dpkg --purge modutils ppp pppoeconf pppoe pppconfig&lt;br /&gt;
&lt;br /&gt;
=== Disable services ===&lt;br /&gt;
Do not start some services, stick to bare minimum:&lt;br /&gt;
 update-rc.d -f klogd remove&lt;br /&gt;
 update-rc.d -f quotarpc remove&lt;br /&gt;
 update-rc.d -f exim4 remove&lt;br /&gt;
 update-rc.d -f inetd remove&lt;br /&gt;
&lt;br /&gt;
=== Fix SSH host keys ===&lt;br /&gt;
This is only useful if you installed SSH.  Each individual [[VE]] should have its own pair of SSH host keys.  The code below will wipe out the existing SSH keys and instruct the newly-created [[VE]] to create new SSH keys on first boot.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- please do not remove &amp;lt;source&amp;gt;...&amp;lt;/source&amp;gt; pair of tags below,&lt;br /&gt;
     otherwise quotes after -N (-N '') are not visible --&amp;gt;&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
rm -f /etc/ssh/ssh_host_*&lt;br /&gt;
cat &amp;lt;&amp;lt; EOF &amp;gt; /etc/rc2.d/S15ssh_gen_host_keys&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
ssh-keygen -f /etc/ssh/ssh_host_rsa_key -t rsa -N ''&lt;br /&gt;
ssh-keygen -f /etc/ssh/ssh_host_dsa_key -t dsa -N ''&lt;br /&gt;
rm -f \$0&lt;br /&gt;
EOF&lt;br /&gt;
chmod a+x /etc/rc2.d/S15ssh_gen_host_keys&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Note|This will not work using the dependency-based boot sequence introduced with Squeeze...}}&lt;br /&gt;
&lt;br /&gt;
=== Change timezone ===&lt;br /&gt;
&lt;br /&gt;
You might want to change timezone if you do not live in $UTC. The following example is for Germany&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ln -sf /usr/share/zoneinfo/Europe/Berlin /etc/localtime&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
or even better&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
dpkg-reconfigure tzdata&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Clean packages ===&lt;br /&gt;
After installing packages, you'll have some junk packages laying around in your cache.  Since you don't want your template to have those, this command will wipe them out.&lt;br /&gt;
 apt-get --purge clean&lt;br /&gt;
&lt;br /&gt;
Now everything is done.  Exit from the template and go back to the hardware node.&lt;br /&gt;
 exit&lt;br /&gt;
&lt;br /&gt;
== Preparing for and packing template cache ==&lt;br /&gt;
&lt;br /&gt;
We don't need an IP for the VE anymore, and we definitely do not need it in template cache, so remove it:&lt;br /&gt;
 sudo vzctl set 777 --ipdel all --save&lt;br /&gt;
&lt;br /&gt;
Also, remove DNS server and search domain information from ''/etc/resolv.conf'' file '''in VE''':&lt;br /&gt;
 sudo nano /vz/private/777/etc/resolv.conf&lt;br /&gt;
&lt;br /&gt;
Also, remove ''/etc/hostname'' file '''in VE''':&lt;br /&gt;
 sudo rm -f /vz/private/777/etc/hostname&lt;br /&gt;
&lt;br /&gt;
Stop the VE:&lt;br /&gt;
 sudo vzctl stop 777&lt;br /&gt;
&lt;br /&gt;
Go to the VE directory:&lt;br /&gt;
 cd /vz/private/777&lt;br /&gt;
&lt;br /&gt;
Now create a cached OS tarball.  In the command below, you'll want to replace &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; with your architecture (i386, amd64, ia64, etc).&lt;br /&gt;
 sudo tar --numeric-owner -zcf /vz/template/cache/debian-5.0-i386-minimal.tar.gz .&lt;br /&gt;
&lt;br /&gt;
Look at the resulting tarball to see its size is sane:&lt;br /&gt;
 # ls -lh /vz/template/cache&lt;br /&gt;
 -rw-r--r--  1 root root  51M Apr 10 03:16 debian-5.0-i386-minimal.tar.gz&lt;br /&gt;
&lt;br /&gt;
== Checking if template cache works ==&lt;br /&gt;
We can now create a VE based on the just-created template cache.  Be sure to change &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; to your architecture just like you did when you named the tarball above.&lt;br /&gt;
 sudo vzctl create 123456 --ostemplate debian-5.0-i386-minimal&lt;br /&gt;
&lt;br /&gt;
Now make sure that it works:&lt;br /&gt;
 sudo vzctl start 123456&lt;br /&gt;
 sudo vzctl exec 123456 ps ax&lt;br /&gt;
&lt;br /&gt;
You should see that a few processes are running.&lt;br /&gt;
&lt;br /&gt;
== Final cleanup ==&lt;br /&gt;
Stop and remove the test VE you just created:&lt;br /&gt;
 sudo vzctl stop 123456&lt;br /&gt;
 sudo vzctl destroy 123456&lt;br /&gt;
 sudo rm /etc/vz/conf/123456.conf.destroyed&lt;br /&gt;
&lt;br /&gt;
Finally, let's remove the VE we used for OS template cache creation:&lt;br /&gt;
 sudo vzctl destroy 777&lt;br /&gt;
 sudo rm /etc/vz/conf/777.conf.destroyed&lt;br /&gt;
&lt;br /&gt;
You might want to edit /etc/vz/vz.conf and change DEF_OSTEMPLATE to the name of the template you use most often so that you don't have to specify the template when creating a VE.&lt;br /&gt;
 DEF_OSTEMPLATE=&amp;quot;debian-5.0-i386-minimal&amp;quot;&lt;br /&gt;
If you use iptables, you might want to include additional modules in the list for IPTABLES in /etc/vz/vz.conf. See ''man vzctl'' for a list of available modules.&lt;br /&gt;
&lt;br /&gt;
[[Category: HOWTO]]&lt;br /&gt;
[[Category: Templates]]&lt;br /&gt;
[[Category: Debian]]&lt;/div&gt;</summary>
		<author><name>Mose</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Debian_template_creation&amp;diff=9194</id>
		<title>Debian template creation</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Debian_template_creation&amp;diff=9194"/>
		<updated>2010-09-30T14:39:01Z</updated>

		<summary type="html">&lt;p&gt;Mose: /* Checking if template cache works */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;These are rough instructions of how to manually create basic Debian Etch (4.0) template cache, which can be used to create OpenVZ [[VE]]s based on Debian Etch (4.0). (see also &amp;lt;tt&amp;gt;/usr/share/doc/vzctl/README.Debian&amp;lt;/tt&amp;gt; in the ''vzctl'' Debian package)&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* You shouldn't be running as root, but as a user that is permitted to use sudo instead.  It's a dangerous idea, run as root at your peril.&lt;br /&gt;
* Anywhere you see &amp;lt;tt&amp;gt;/vz&amp;lt;/tt&amp;gt;, you might really need to use &amp;lt;tt&amp;gt;/var/lib/vz&amp;lt;/tt&amp;gt; instead, especially on a Debian Etch host.&lt;br /&gt;
* Anywhere you see &amp;lt;tt&amp;gt;http://http.us.debian.org/debian/&amp;lt;/tt&amp;gt;, you can substitute your favorite Debian mirror.  ([http://www.debian.org/mirror/list List of official Debian Mirrors])&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Prerequisites ==&lt;br /&gt;
You need to have a working copy of &amp;lt;tt&amp;gt;debootstrap&amp;lt;/tt&amp;gt; running on your hardware node.&lt;br /&gt;
&lt;br /&gt;
For Debian:&lt;br /&gt;
 sudo apt-get install debootstrap&lt;br /&gt;
&lt;br /&gt;
For Gentoo:&lt;br /&gt;
 sudo emerge debootstrap&lt;br /&gt;
&lt;br /&gt;
For Fedora (at least Fedora 8 have it, not sure about earlier versions):&lt;br /&gt;
 sudo yum install debootstrap&lt;br /&gt;
&lt;br /&gt;
For other distros you might need to install it from sources, or search for an appropriate package for your distribution.  An RPM is available on the [http://forum.openvz.org/index.php?t=tree&amp;amp;th=142&amp;amp;mid=584 OpenVZ Forum].&lt;br /&gt;
&lt;br /&gt;
== Bootstrapping Debian ==&lt;br /&gt;
&lt;br /&gt;
You can install different releases of Debian into a VE's private directory using the debootstrap command.&lt;br /&gt;
&lt;br /&gt;
The command parameters are:&lt;br /&gt;
&lt;br /&gt;
  debootstrap --arch ARCH NAME DIRECTORY URL&lt;br /&gt;
&lt;br /&gt;
Specify your architecture instead of &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; if you're using something other than i386/x86.  For example, for AMD64/x86_64, use &amp;lt;tt&amp;gt;amd64&amp;lt;/tt&amp;gt; or for ia64, use &amp;lt;tt&amp;gt;ia64&amp;lt;/tt&amp;gt;. You can use http or ftp in the URL.&lt;br /&gt;
&lt;br /&gt;
We use VE ID of 777 for this example, but it can be any unused ID.&lt;br /&gt;
&lt;br /&gt;
=== Lenny (current Debian stable) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap --arch i386 lenny /vz/private/777 http://http.us.debian.org/debian/&lt;br /&gt;
 or&lt;br /&gt;
 debootstrap --arch amd64 lenny /vz/private/777 ftp://ftp.us.debian.org/debian/&lt;br /&gt;
&lt;br /&gt;
=== Etch (old release) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap --arch i386 etch /vz/private/777 http://http.us.debian.org/debian/&lt;br /&gt;
&lt;br /&gt;
=== Sarge (very old release) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap sarge /vz/private/777 http://archive.debian.org/debian&lt;br /&gt;
&lt;br /&gt;
== Preparing the HN network ==&lt;br /&gt;
Append the following lines to /etc/sysctl.conf, adjust to taste and then execute &amp;quot;sysctl -p&amp;quot; for them to take effect.&lt;br /&gt;
 ### OpenVZ settings&lt;br /&gt;
 &lt;br /&gt;
 # On Hardware Node we generally need packet&lt;br /&gt;
 # forwarding enabled and proxy arp disabled&lt;br /&gt;
 &lt;br /&gt;
 net.ipv4.conf.default.forwarding=1&lt;br /&gt;
 net.ipv4.conf.default.proxy_arp = 0&lt;br /&gt;
 net.ipv4.ip_forward=1&lt;br /&gt;
 &lt;br /&gt;
 # Enables source route verification&lt;br /&gt;
 net.ipv4.conf.all.rp_filter = 1&lt;br /&gt;
 &lt;br /&gt;
 # Enables the magic-sysrq key&lt;br /&gt;
 kernel.sysrq = 1&lt;br /&gt;
 &lt;br /&gt;
 # TCP Explict Congestion Notification&lt;br /&gt;
 net.ipv4.tcp_ecn = 0&lt;br /&gt;
 &lt;br /&gt;
 # we do not want all our interfaces to send redirects&lt;br /&gt;
 net.ipv4.conf.default.send_redirects = 1&lt;br /&gt;
 net.ipv4.conf.all.send_redirects = 0&lt;br /&gt;
&lt;br /&gt;
== Preparing and starting the VE ==&lt;br /&gt;
&lt;br /&gt;
=== Setting VE config ===&lt;br /&gt;
First, we need a config for the [[VE]]:&lt;br /&gt;
 sudo vzctl set 777 --applyconfig vps.basic --save&lt;br /&gt;
&lt;br /&gt;
=== Setting VE OSTEMPLATE ===&lt;br /&gt;
Also, we need &amp;lt;tt&amp;gt;OSTEMPLATE&amp;lt;/tt&amp;gt; to be set in VE configuration file, for the [[vzctl]] to work properly.&lt;br /&gt;
&lt;br /&gt;
 sudo sh -c 'echo &amp;quot;OSTEMPLATE=debian-5.0&amp;quot; &amp;gt;&amp;gt; /etc/vz/conf/777.conf'&lt;br /&gt;
&lt;br /&gt;
=== Setting VE IP address ===&lt;br /&gt;
For the [[VE]] to be able to download updates from the Internet, we need a valid IP address for it:&lt;br /&gt;
 sudo vzctl set 777 --ipadd x.x.x.x --save&lt;br /&gt;
&lt;br /&gt;
{{Note|if you use private IP for the VE, you might have to set up NAT as described in [[Using NAT for VE with private IPs]].}}&lt;br /&gt;
&lt;br /&gt;
=== Setting DNS server for VE ===&lt;br /&gt;
For the [[VE]] to be able to download updates from the Internet, we also need to specify a DNS for it:&lt;br /&gt;
 sudo vzctl set 777 --nameserver x.x.x.x --save&lt;br /&gt;
&lt;br /&gt;
=== Creating /dev/ptmx  ===&lt;br /&gt;
The ptmx character device should normally exist, but if it doesn't, create one.&lt;br /&gt;
 sudo mknod --mode 666 /var/lib/vz/private/777/dev/ptmx c 5 2&lt;br /&gt;
&lt;br /&gt;
=== Starting VE ===&lt;br /&gt;
Now start the VE:&lt;br /&gt;
 sudo vzctl start 777&lt;br /&gt;
&lt;br /&gt;
== Customizing the installation ==&lt;br /&gt;
A few things need to be done inside a newly created VE for it to become suitable for OpenVZ. Enter the VE to begin the configuration. Exporting the path is optional.&lt;br /&gt;
 sudo vzctl enter 777&lt;br /&gt;
 export PATH=/sbin:/usr/sbin:/bin:/usr/bin&lt;br /&gt;
&lt;br /&gt;
{{Warning|Do not run the commands below on the hardware node, they are only to be run within the VE!}}&lt;br /&gt;
&lt;br /&gt;
=== Set Debian repositories ===&lt;br /&gt;
 cat &amp;lt;&amp;lt;EOF &amp;gt; /etc/apt/sources.list&lt;br /&gt;
 deb http://http.us.debian.org/debian lenny main contrib&lt;br /&gt;
 deb http://security.debian.org lenny/updates main contrib&lt;br /&gt;
 deb http://security.debian.org lenny/volatile main contrib&lt;br /&gt;
 EOF&lt;br /&gt;
&lt;br /&gt;
=== Get new security updates ===&lt;br /&gt;
 apt-get update&lt;br /&gt;
 apt-get upgrade&lt;br /&gt;
&lt;br /&gt;
=== Install some more packages ===&lt;br /&gt;
Installing packages could be an interactive process so the system might ask some questions.  You can install more packages if you'd like.  For example:&lt;br /&gt;
 apt-get install ssh quota less&lt;br /&gt;
&lt;br /&gt;
=== Set sane permissions for &amp;lt;tt&amp;gt;/root&amp;lt;/tt&amp;gt; directory ===&lt;br /&gt;
 chmod 700 /root&lt;br /&gt;
&lt;br /&gt;
=== Disable root login ===&lt;br /&gt;
This will disable root login by default.&lt;br /&gt;
 usermod -L root&lt;br /&gt;
&lt;br /&gt;
=== Disable getty ===&lt;br /&gt;
Disable running &amp;lt;tt&amp;gt;getty&amp;lt;/tt&amp;gt;s on terminals as a VE does not have any:&lt;br /&gt;
 sed -i -e '/getty/d' /etc/inittab&lt;br /&gt;
&lt;br /&gt;
=== Disable &amp;lt;tt&amp;gt;sync()&amp;lt;/tt&amp;gt; for syslog ===&lt;br /&gt;
Turn off doing &amp;lt;tt&amp;gt;sync()&amp;lt;/tt&amp;gt; on every write for &amp;lt;tt&amp;gt;syslog&amp;lt;/tt&amp;gt;'s log files, to improve I/O performance:&lt;br /&gt;
 &amp;lt;pre&amp;gt;sed -i -e 's@\([[:space:]]\)\(/var/log/\)@\1-\2@' /etc/*syslog.conf&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Fix &amp;lt;tt&amp;gt;/etc/mtab&amp;lt;/tt&amp;gt; ===&lt;br /&gt;
Link &amp;lt;tt&amp;gt;/etc/mtab&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;/proc/mounts&amp;lt;/tt&amp;gt;, so &amp;lt;tt&amp;gt;df&amp;lt;/tt&amp;gt; and friends will work:&lt;br /&gt;
 rm -f /etc/mtab&lt;br /&gt;
 ln -s /proc/mounts /etc/mtab&lt;br /&gt;
&lt;br /&gt;
=== Remove some unneeded packages ===&lt;br /&gt;
If you have any packages you'd like to remove, now's the time for it.  Here's an example:&lt;br /&gt;
 dpkg --purge modutils ppp pppoeconf pppoe pppconfig&lt;br /&gt;
&lt;br /&gt;
=== Disable services ===&lt;br /&gt;
Do not start some services, stick to bare minimum:&lt;br /&gt;
 update-rc.d -f klogd remove&lt;br /&gt;
 update-rc.d -f quotarpc remove&lt;br /&gt;
 update-rc.d -f exim4 remove&lt;br /&gt;
 update-rc.d -f inetd remove&lt;br /&gt;
&lt;br /&gt;
=== Fix SSH host keys ===&lt;br /&gt;
This is only useful if you installed SSH.  Each individual [[VE]] should have its own pair of SSH host keys.  The code below will wipe out the existing SSH keys and instruct the newly-created [[VE]] to create new SSH keys on first boot.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- please do not remove &amp;lt;source&amp;gt;...&amp;lt;/source&amp;gt; pair of tags below,&lt;br /&gt;
     otherwise quotes after -N (-N '') are not visible --&amp;gt;&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
rm -f /etc/ssh/ssh_host_*&lt;br /&gt;
cat &amp;lt;&amp;lt; EOF &amp;gt; /etc/rc2.d/S15ssh_gen_host_keys&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
ssh-keygen -f /etc/ssh/ssh_host_rsa_key -t rsa -N ''&lt;br /&gt;
ssh-keygen -f /etc/ssh/ssh_host_dsa_key -t dsa -N ''&lt;br /&gt;
rm -f \$0&lt;br /&gt;
EOF&lt;br /&gt;
chmod a+x /etc/rc2.d/S15ssh_gen_host_keys&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Note|This will not work using the dependency-based boot sequence introduced with Squeeze...}}&lt;br /&gt;
&lt;br /&gt;
=== Change timezone ===&lt;br /&gt;
&lt;br /&gt;
You might want to change timezone if you do not live in $UTC. The following example is for Germany&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ln -sf /usr/share/zoneinfo/Europe/Berlin /etc/localtime&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
or even better&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
dpkg-reconfigure tzdata&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Clean packages ===&lt;br /&gt;
After installing packages, you'll have some junk packages laying around in your cache.  Since you don't want your template to have those, this command will wipe them out.&lt;br /&gt;
 apt-get --purge clean&lt;br /&gt;
&lt;br /&gt;
Now everything is done.  Exit from the template and go back to the hardware node.&lt;br /&gt;
 exit&lt;br /&gt;
&lt;br /&gt;
== Preparing for and packing template cache ==&lt;br /&gt;
&lt;br /&gt;
We don't need an IP for the VE anymore, and we definitely do not need it in template cache, so remove it:&lt;br /&gt;
 sudo vzctl set 777 --ipdel all --save&lt;br /&gt;
&lt;br /&gt;
Also, remove DNS server and search domain information from ''/etc/resolv.conf'' file '''in VE''':&lt;br /&gt;
 sudo nano /vz/private/777/etc/resolv.conf&lt;br /&gt;
&lt;br /&gt;
Also, remove ''/etc/hostname'' file '''in VE''':&lt;br /&gt;
 sudo rm -f /vz/private/777/etc/hostname&lt;br /&gt;
&lt;br /&gt;
Stop the VE:&lt;br /&gt;
 sudo vzctl stop 777&lt;br /&gt;
&lt;br /&gt;
Go to the VE directory:&lt;br /&gt;
 cd /vz/private/777&lt;br /&gt;
&lt;br /&gt;
Now create a cached OS tarball.  In the command below, you'll want to replace &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; with your architecture (i386, amd64, ia64, etc).&lt;br /&gt;
 sudo tar --numeric-owner -zcf /vz/template/cache/debian-5.0-i386-minimal.tar.gz .&lt;br /&gt;
&lt;br /&gt;
Look at the resulting tarball to see its size is sane:&lt;br /&gt;
 # ls -lh /vz/template/cache&lt;br /&gt;
 -rw-r--r--  1 root root  51M Apr 10 03:16 debian-5.0-i386-minimal.tar.gz&lt;br /&gt;
&lt;br /&gt;
== Checking if template cache works ==&lt;br /&gt;
We can now create a VE based on the just-created template cache.  Be sure to change &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; to your architecture just like you did when you named the tarball above.&lt;br /&gt;
 sudo vzctl create 123456 --ostemplate debian-5.0-i386-minimal&lt;br /&gt;
&lt;br /&gt;
Now make sure that it works:&lt;br /&gt;
 sudo vzctl start 123456&lt;br /&gt;
 sudo vzctl exec 123456 ps ax&lt;br /&gt;
&lt;br /&gt;
You should see that a few processes are running.&lt;br /&gt;
&lt;br /&gt;
== Final cleanup ==&lt;br /&gt;
Stop and remove the test VE you just created:&lt;br /&gt;
 sudo vzctl stop 123456&lt;br /&gt;
 sudo vzctl destroy 123456&lt;br /&gt;
 sudo rm /etc/vz/conf/123456.conf.destroyed&lt;br /&gt;
&lt;br /&gt;
Finally, let's remove the VE we used for OS template cache creation:&lt;br /&gt;
 sudo vzctl destroy 777&lt;br /&gt;
 sudo rm /etc/vz/conf/777.conf.destroyed&lt;br /&gt;
&lt;br /&gt;
You might want to edit /etc/vz/vz.conf and change DEF_OSTEMPLATE to the name of the template you use most often so that you don't have to specify the template when creating a VE.&lt;br /&gt;
 DEF_OSTEMPLATE=&amp;quot;debian-5.0-amd65-minimal&amp;quot;&lt;br /&gt;
If you use iptables, you might want to include additional modules in the list for IPTABLES in /etc/vz/vz.conf. See ''man vzctl'' for a list of available modules.&lt;br /&gt;
&lt;br /&gt;
[[Category: HOWTO]]&lt;br /&gt;
[[Category: Templates]]&lt;br /&gt;
[[Category: Debian]]&lt;/div&gt;</summary>
		<author><name>Mose</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Debian_template_creation&amp;diff=9193</id>
		<title>Debian template creation</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Debian_template_creation&amp;diff=9193"/>
		<updated>2010-09-30T14:38:43Z</updated>

		<summary type="html">&lt;p&gt;Mose: /* Preparing for and packing template cache */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;These are rough instructions of how to manually create basic Debian Etch (4.0) template cache, which can be used to create OpenVZ [[VE]]s based on Debian Etch (4.0). (see also &amp;lt;tt&amp;gt;/usr/share/doc/vzctl/README.Debian&amp;lt;/tt&amp;gt; in the ''vzctl'' Debian package)&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* You shouldn't be running as root, but as a user that is permitted to use sudo instead.  It's a dangerous idea, run as root at your peril.&lt;br /&gt;
* Anywhere you see &amp;lt;tt&amp;gt;/vz&amp;lt;/tt&amp;gt;, you might really need to use &amp;lt;tt&amp;gt;/var/lib/vz&amp;lt;/tt&amp;gt; instead, especially on a Debian Etch host.&lt;br /&gt;
* Anywhere you see &amp;lt;tt&amp;gt;http://http.us.debian.org/debian/&amp;lt;/tt&amp;gt;, you can substitute your favorite Debian mirror.  ([http://www.debian.org/mirror/list List of official Debian Mirrors])&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Prerequisites ==&lt;br /&gt;
You need to have a working copy of &amp;lt;tt&amp;gt;debootstrap&amp;lt;/tt&amp;gt; running on your hardware node.&lt;br /&gt;
&lt;br /&gt;
For Debian:&lt;br /&gt;
 sudo apt-get install debootstrap&lt;br /&gt;
&lt;br /&gt;
For Gentoo:&lt;br /&gt;
 sudo emerge debootstrap&lt;br /&gt;
&lt;br /&gt;
For Fedora (at least Fedora 8 have it, not sure about earlier versions):&lt;br /&gt;
 sudo yum install debootstrap&lt;br /&gt;
&lt;br /&gt;
For other distros you might need to install it from sources, or search for an appropriate package for your distribution.  An RPM is available on the [http://forum.openvz.org/index.php?t=tree&amp;amp;th=142&amp;amp;mid=584 OpenVZ Forum].&lt;br /&gt;
&lt;br /&gt;
== Bootstrapping Debian ==&lt;br /&gt;
&lt;br /&gt;
You can install different releases of Debian into a VE's private directory using the debootstrap command.&lt;br /&gt;
&lt;br /&gt;
The command parameters are:&lt;br /&gt;
&lt;br /&gt;
  debootstrap --arch ARCH NAME DIRECTORY URL&lt;br /&gt;
&lt;br /&gt;
Specify your architecture instead of &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; if you're using something other than i386/x86.  For example, for AMD64/x86_64, use &amp;lt;tt&amp;gt;amd64&amp;lt;/tt&amp;gt; or for ia64, use &amp;lt;tt&amp;gt;ia64&amp;lt;/tt&amp;gt;. You can use http or ftp in the URL.&lt;br /&gt;
&lt;br /&gt;
We use VE ID of 777 for this example, but it can be any unused ID.&lt;br /&gt;
&lt;br /&gt;
=== Lenny (current Debian stable) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap --arch i386 lenny /vz/private/777 http://http.us.debian.org/debian/&lt;br /&gt;
 or&lt;br /&gt;
 debootstrap --arch amd64 lenny /vz/private/777 ftp://ftp.us.debian.org/debian/&lt;br /&gt;
&lt;br /&gt;
=== Etch (old release) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap --arch i386 etch /vz/private/777 http://http.us.debian.org/debian/&lt;br /&gt;
&lt;br /&gt;
=== Sarge (very old release) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap sarge /vz/private/777 http://archive.debian.org/debian&lt;br /&gt;
&lt;br /&gt;
== Preparing the HN network ==&lt;br /&gt;
Append the following lines to /etc/sysctl.conf, adjust to taste and then execute &amp;quot;sysctl -p&amp;quot; for them to take effect.&lt;br /&gt;
 ### OpenVZ settings&lt;br /&gt;
 &lt;br /&gt;
 # On Hardware Node we generally need packet&lt;br /&gt;
 # forwarding enabled and proxy arp disabled&lt;br /&gt;
 &lt;br /&gt;
 net.ipv4.conf.default.forwarding=1&lt;br /&gt;
 net.ipv4.conf.default.proxy_arp = 0&lt;br /&gt;
 net.ipv4.ip_forward=1&lt;br /&gt;
 &lt;br /&gt;
 # Enables source route verification&lt;br /&gt;
 net.ipv4.conf.all.rp_filter = 1&lt;br /&gt;
 &lt;br /&gt;
 # Enables the magic-sysrq key&lt;br /&gt;
 kernel.sysrq = 1&lt;br /&gt;
 &lt;br /&gt;
 # TCP Explict Congestion Notification&lt;br /&gt;
 net.ipv4.tcp_ecn = 0&lt;br /&gt;
 &lt;br /&gt;
 # we do not want all our interfaces to send redirects&lt;br /&gt;
 net.ipv4.conf.default.send_redirects = 1&lt;br /&gt;
 net.ipv4.conf.all.send_redirects = 0&lt;br /&gt;
&lt;br /&gt;
== Preparing and starting the VE ==&lt;br /&gt;
&lt;br /&gt;
=== Setting VE config ===&lt;br /&gt;
First, we need a config for the [[VE]]:&lt;br /&gt;
 sudo vzctl set 777 --applyconfig vps.basic --save&lt;br /&gt;
&lt;br /&gt;
=== Setting VE OSTEMPLATE ===&lt;br /&gt;
Also, we need &amp;lt;tt&amp;gt;OSTEMPLATE&amp;lt;/tt&amp;gt; to be set in VE configuration file, for the [[vzctl]] to work properly.&lt;br /&gt;
&lt;br /&gt;
 sudo sh -c 'echo &amp;quot;OSTEMPLATE=debian-5.0&amp;quot; &amp;gt;&amp;gt; /etc/vz/conf/777.conf'&lt;br /&gt;
&lt;br /&gt;
=== Setting VE IP address ===&lt;br /&gt;
For the [[VE]] to be able to download updates from the Internet, we need a valid IP address for it:&lt;br /&gt;
 sudo vzctl set 777 --ipadd x.x.x.x --save&lt;br /&gt;
&lt;br /&gt;
{{Note|if you use private IP for the VE, you might have to set up NAT as described in [[Using NAT for VE with private IPs]].}}&lt;br /&gt;
&lt;br /&gt;
=== Setting DNS server for VE ===&lt;br /&gt;
For the [[VE]] to be able to download updates from the Internet, we also need to specify a DNS for it:&lt;br /&gt;
 sudo vzctl set 777 --nameserver x.x.x.x --save&lt;br /&gt;
&lt;br /&gt;
=== Creating /dev/ptmx  ===&lt;br /&gt;
The ptmx character device should normally exist, but if it doesn't, create one.&lt;br /&gt;
 sudo mknod --mode 666 /var/lib/vz/private/777/dev/ptmx c 5 2&lt;br /&gt;
&lt;br /&gt;
=== Starting VE ===&lt;br /&gt;
Now start the VE:&lt;br /&gt;
 sudo vzctl start 777&lt;br /&gt;
&lt;br /&gt;
== Customizing the installation ==&lt;br /&gt;
A few things need to be done inside a newly created VE for it to become suitable for OpenVZ. Enter the VE to begin the configuration. Exporting the path is optional.&lt;br /&gt;
 sudo vzctl enter 777&lt;br /&gt;
 export PATH=/sbin:/usr/sbin:/bin:/usr/bin&lt;br /&gt;
&lt;br /&gt;
{{Warning|Do not run the commands below on the hardware node, they are only to be run within the VE!}}&lt;br /&gt;
&lt;br /&gt;
=== Set Debian repositories ===&lt;br /&gt;
 cat &amp;lt;&amp;lt;EOF &amp;gt; /etc/apt/sources.list&lt;br /&gt;
 deb http://http.us.debian.org/debian lenny main contrib&lt;br /&gt;
 deb http://security.debian.org lenny/updates main contrib&lt;br /&gt;
 deb http://security.debian.org lenny/volatile main contrib&lt;br /&gt;
 EOF&lt;br /&gt;
&lt;br /&gt;
=== Get new security updates ===&lt;br /&gt;
 apt-get update&lt;br /&gt;
 apt-get upgrade&lt;br /&gt;
&lt;br /&gt;
=== Install some more packages ===&lt;br /&gt;
Installing packages could be an interactive process so the system might ask some questions.  You can install more packages if you'd like.  For example:&lt;br /&gt;
 apt-get install ssh quota less&lt;br /&gt;
&lt;br /&gt;
=== Set sane permissions for &amp;lt;tt&amp;gt;/root&amp;lt;/tt&amp;gt; directory ===&lt;br /&gt;
 chmod 700 /root&lt;br /&gt;
&lt;br /&gt;
=== Disable root login ===&lt;br /&gt;
This will disable root login by default.&lt;br /&gt;
 usermod -L root&lt;br /&gt;
&lt;br /&gt;
=== Disable getty ===&lt;br /&gt;
Disable running &amp;lt;tt&amp;gt;getty&amp;lt;/tt&amp;gt;s on terminals as a VE does not have any:&lt;br /&gt;
 sed -i -e '/getty/d' /etc/inittab&lt;br /&gt;
&lt;br /&gt;
=== Disable &amp;lt;tt&amp;gt;sync()&amp;lt;/tt&amp;gt; for syslog ===&lt;br /&gt;
Turn off doing &amp;lt;tt&amp;gt;sync()&amp;lt;/tt&amp;gt; on every write for &amp;lt;tt&amp;gt;syslog&amp;lt;/tt&amp;gt;'s log files, to improve I/O performance:&lt;br /&gt;
 &amp;lt;pre&amp;gt;sed -i -e 's@\([[:space:]]\)\(/var/log/\)@\1-\2@' /etc/*syslog.conf&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Fix &amp;lt;tt&amp;gt;/etc/mtab&amp;lt;/tt&amp;gt; ===&lt;br /&gt;
Link &amp;lt;tt&amp;gt;/etc/mtab&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;/proc/mounts&amp;lt;/tt&amp;gt;, so &amp;lt;tt&amp;gt;df&amp;lt;/tt&amp;gt; and friends will work:&lt;br /&gt;
 rm -f /etc/mtab&lt;br /&gt;
 ln -s /proc/mounts /etc/mtab&lt;br /&gt;
&lt;br /&gt;
=== Remove some unneeded packages ===&lt;br /&gt;
If you have any packages you'd like to remove, now's the time for it.  Here's an example:&lt;br /&gt;
 dpkg --purge modutils ppp pppoeconf pppoe pppconfig&lt;br /&gt;
&lt;br /&gt;
=== Disable services ===&lt;br /&gt;
Do not start some services, stick to bare minimum:&lt;br /&gt;
 update-rc.d -f klogd remove&lt;br /&gt;
 update-rc.d -f quotarpc remove&lt;br /&gt;
 update-rc.d -f exim4 remove&lt;br /&gt;
 update-rc.d -f inetd remove&lt;br /&gt;
&lt;br /&gt;
=== Fix SSH host keys ===&lt;br /&gt;
This is only useful if you installed SSH.  Each individual [[VE]] should have its own pair of SSH host keys.  The code below will wipe out the existing SSH keys and instruct the newly-created [[VE]] to create new SSH keys on first boot.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- please do not remove &amp;lt;source&amp;gt;...&amp;lt;/source&amp;gt; pair of tags below,&lt;br /&gt;
     otherwise quotes after -N (-N '') are not visible --&amp;gt;&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
rm -f /etc/ssh/ssh_host_*&lt;br /&gt;
cat &amp;lt;&amp;lt; EOF &amp;gt; /etc/rc2.d/S15ssh_gen_host_keys&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
ssh-keygen -f /etc/ssh/ssh_host_rsa_key -t rsa -N ''&lt;br /&gt;
ssh-keygen -f /etc/ssh/ssh_host_dsa_key -t dsa -N ''&lt;br /&gt;
rm -f \$0&lt;br /&gt;
EOF&lt;br /&gt;
chmod a+x /etc/rc2.d/S15ssh_gen_host_keys&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Note|This will not work using the dependency-based boot sequence introduced with Squeeze...}}&lt;br /&gt;
&lt;br /&gt;
=== Change timezone ===&lt;br /&gt;
&lt;br /&gt;
You might want to change timezone if you do not live in $UTC. The following example is for Germany&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ln -sf /usr/share/zoneinfo/Europe/Berlin /etc/localtime&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
or even better&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
dpkg-reconfigure tzdata&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Clean packages ===&lt;br /&gt;
After installing packages, you'll have some junk packages laying around in your cache.  Since you don't want your template to have those, this command will wipe them out.&lt;br /&gt;
 apt-get --purge clean&lt;br /&gt;
&lt;br /&gt;
Now everything is done.  Exit from the template and go back to the hardware node.&lt;br /&gt;
 exit&lt;br /&gt;
&lt;br /&gt;
== Preparing for and packing template cache ==&lt;br /&gt;
&lt;br /&gt;
We don't need an IP for the VE anymore, and we definitely do not need it in template cache, so remove it:&lt;br /&gt;
 sudo vzctl set 777 --ipdel all --save&lt;br /&gt;
&lt;br /&gt;
Also, remove DNS server and search domain information from ''/etc/resolv.conf'' file '''in VE''':&lt;br /&gt;
 sudo nano /vz/private/777/etc/resolv.conf&lt;br /&gt;
&lt;br /&gt;
Also, remove ''/etc/hostname'' file '''in VE''':&lt;br /&gt;
 sudo rm -f /vz/private/777/etc/hostname&lt;br /&gt;
&lt;br /&gt;
Stop the VE:&lt;br /&gt;
 sudo vzctl stop 777&lt;br /&gt;
&lt;br /&gt;
Go to the VE directory:&lt;br /&gt;
 cd /vz/private/777&lt;br /&gt;
&lt;br /&gt;
Now create a cached OS tarball.  In the command below, you'll want to replace &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; with your architecture (i386, amd64, ia64, etc).&lt;br /&gt;
 sudo tar --numeric-owner -zcf /vz/template/cache/debian-5.0-i386-minimal.tar.gz .&lt;br /&gt;
&lt;br /&gt;
Look at the resulting tarball to see its size is sane:&lt;br /&gt;
 # ls -lh /vz/template/cache&lt;br /&gt;
 -rw-r--r--  1 root root  51M Apr 10 03:16 debian-5.0-i386-minimal.tar.gz&lt;br /&gt;
&lt;br /&gt;
== Checking if template cache works ==&lt;br /&gt;
We can now create a VE based on the just-created template cache.  Be sure to change &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; to your architecture just like you did when you named the tarball above.&lt;br /&gt;
 sudo vzctl create 123456 --ostemplate debian-4.0-i386-minimal&lt;br /&gt;
&lt;br /&gt;
Now make sure that it works:&lt;br /&gt;
 sudo vzctl start 123456&lt;br /&gt;
 sudo vzctl exec 123456 ps ax&lt;br /&gt;
&lt;br /&gt;
You should see that a few processes are running.&lt;br /&gt;
&lt;br /&gt;
== Final cleanup ==&lt;br /&gt;
Stop and remove the test VE you just created:&lt;br /&gt;
 sudo vzctl stop 123456&lt;br /&gt;
 sudo vzctl destroy 123456&lt;br /&gt;
 sudo rm /etc/vz/conf/123456.conf.destroyed&lt;br /&gt;
&lt;br /&gt;
Finally, let's remove the VE we used for OS template cache creation:&lt;br /&gt;
 sudo vzctl destroy 777&lt;br /&gt;
 sudo rm /etc/vz/conf/777.conf.destroyed&lt;br /&gt;
&lt;br /&gt;
You might want to edit /etc/vz/vz.conf and change DEF_OSTEMPLATE to the name of the template you use most often so that you don't have to specify the template when creating a VE.&lt;br /&gt;
 DEF_OSTEMPLATE=&amp;quot;debian-5.0-amd65-minimal&amp;quot;&lt;br /&gt;
If you use iptables, you might want to include additional modules in the list for IPTABLES in /etc/vz/vz.conf. See ''man vzctl'' for a list of available modules.&lt;br /&gt;
&lt;br /&gt;
[[Category: HOWTO]]&lt;br /&gt;
[[Category: Templates]]&lt;br /&gt;
[[Category: Debian]]&lt;/div&gt;</summary>
		<author><name>Mose</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Debian_template_creation&amp;diff=9192</id>
		<title>Debian template creation</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Debian_template_creation&amp;diff=9192"/>
		<updated>2010-09-30T14:37:12Z</updated>

		<summary type="html">&lt;p&gt;Mose: /* Setting VE OSTEMPLATE */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;These are rough instructions of how to manually create basic Debian Etch (4.0) template cache, which can be used to create OpenVZ [[VE]]s based on Debian Etch (4.0). (see also &amp;lt;tt&amp;gt;/usr/share/doc/vzctl/README.Debian&amp;lt;/tt&amp;gt; in the ''vzctl'' Debian package)&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* You shouldn't be running as root, but as a user that is permitted to use sudo instead.  It's a dangerous idea, run as root at your peril.&lt;br /&gt;
* Anywhere you see &amp;lt;tt&amp;gt;/vz&amp;lt;/tt&amp;gt;, you might really need to use &amp;lt;tt&amp;gt;/var/lib/vz&amp;lt;/tt&amp;gt; instead, especially on a Debian Etch host.&lt;br /&gt;
* Anywhere you see &amp;lt;tt&amp;gt;http://http.us.debian.org/debian/&amp;lt;/tt&amp;gt;, you can substitute your favorite Debian mirror.  ([http://www.debian.org/mirror/list List of official Debian Mirrors])&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Prerequisites ==&lt;br /&gt;
You need to have a working copy of &amp;lt;tt&amp;gt;debootstrap&amp;lt;/tt&amp;gt; running on your hardware node.&lt;br /&gt;
&lt;br /&gt;
For Debian:&lt;br /&gt;
 sudo apt-get install debootstrap&lt;br /&gt;
&lt;br /&gt;
For Gentoo:&lt;br /&gt;
 sudo emerge debootstrap&lt;br /&gt;
&lt;br /&gt;
For Fedora (at least Fedora 8 have it, not sure about earlier versions):&lt;br /&gt;
 sudo yum install debootstrap&lt;br /&gt;
&lt;br /&gt;
For other distros you might need to install it from sources, or search for an appropriate package for your distribution.  An RPM is available on the [http://forum.openvz.org/index.php?t=tree&amp;amp;th=142&amp;amp;mid=584 OpenVZ Forum].&lt;br /&gt;
&lt;br /&gt;
== Bootstrapping Debian ==&lt;br /&gt;
&lt;br /&gt;
You can install different releases of Debian into a VE's private directory using the debootstrap command.&lt;br /&gt;
&lt;br /&gt;
The command parameters are:&lt;br /&gt;
&lt;br /&gt;
  debootstrap --arch ARCH NAME DIRECTORY URL&lt;br /&gt;
&lt;br /&gt;
Specify your architecture instead of &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; if you're using something other than i386/x86.  For example, for AMD64/x86_64, use &amp;lt;tt&amp;gt;amd64&amp;lt;/tt&amp;gt; or for ia64, use &amp;lt;tt&amp;gt;ia64&amp;lt;/tt&amp;gt;. You can use http or ftp in the URL.&lt;br /&gt;
&lt;br /&gt;
We use VE ID of 777 for this example, but it can be any unused ID.&lt;br /&gt;
&lt;br /&gt;
=== Lenny (current Debian stable) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap --arch i386 lenny /vz/private/777 http://http.us.debian.org/debian/&lt;br /&gt;
 or&lt;br /&gt;
 debootstrap --arch amd64 lenny /vz/private/777 ftp://ftp.us.debian.org/debian/&lt;br /&gt;
&lt;br /&gt;
=== Etch (old release) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap --arch i386 etch /vz/private/777 http://http.us.debian.org/debian/&lt;br /&gt;
&lt;br /&gt;
=== Sarge (very old release) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap sarge /vz/private/777 http://archive.debian.org/debian&lt;br /&gt;
&lt;br /&gt;
== Preparing the HN network ==&lt;br /&gt;
Append the following lines to /etc/sysctl.conf, adjust to taste and then execute &amp;quot;sysctl -p&amp;quot; for them to take effect.&lt;br /&gt;
 ### OpenVZ settings&lt;br /&gt;
 &lt;br /&gt;
 # On Hardware Node we generally need packet&lt;br /&gt;
 # forwarding enabled and proxy arp disabled&lt;br /&gt;
 &lt;br /&gt;
 net.ipv4.conf.default.forwarding=1&lt;br /&gt;
 net.ipv4.conf.default.proxy_arp = 0&lt;br /&gt;
 net.ipv4.ip_forward=1&lt;br /&gt;
 &lt;br /&gt;
 # Enables source route verification&lt;br /&gt;
 net.ipv4.conf.all.rp_filter = 1&lt;br /&gt;
 &lt;br /&gt;
 # Enables the magic-sysrq key&lt;br /&gt;
 kernel.sysrq = 1&lt;br /&gt;
 &lt;br /&gt;
 # TCP Explict Congestion Notification&lt;br /&gt;
 net.ipv4.tcp_ecn = 0&lt;br /&gt;
 &lt;br /&gt;
 # we do not want all our interfaces to send redirects&lt;br /&gt;
 net.ipv4.conf.default.send_redirects = 1&lt;br /&gt;
 net.ipv4.conf.all.send_redirects = 0&lt;br /&gt;
&lt;br /&gt;
== Preparing and starting the VE ==&lt;br /&gt;
&lt;br /&gt;
=== Setting VE config ===&lt;br /&gt;
First, we need a config for the [[VE]]:&lt;br /&gt;
 sudo vzctl set 777 --applyconfig vps.basic --save&lt;br /&gt;
&lt;br /&gt;
=== Setting VE OSTEMPLATE ===&lt;br /&gt;
Also, we need &amp;lt;tt&amp;gt;OSTEMPLATE&amp;lt;/tt&amp;gt; to be set in VE configuration file, for the [[vzctl]] to work properly.&lt;br /&gt;
&lt;br /&gt;
 sudo sh -c 'echo &amp;quot;OSTEMPLATE=debian-5.0&amp;quot; &amp;gt;&amp;gt; /etc/vz/conf/777.conf'&lt;br /&gt;
&lt;br /&gt;
=== Setting VE IP address ===&lt;br /&gt;
For the [[VE]] to be able to download updates from the Internet, we need a valid IP address for it:&lt;br /&gt;
 sudo vzctl set 777 --ipadd x.x.x.x --save&lt;br /&gt;
&lt;br /&gt;
{{Note|if you use private IP for the VE, you might have to set up NAT as described in [[Using NAT for VE with private IPs]].}}&lt;br /&gt;
&lt;br /&gt;
=== Setting DNS server for VE ===&lt;br /&gt;
For the [[VE]] to be able to download updates from the Internet, we also need to specify a DNS for it:&lt;br /&gt;
 sudo vzctl set 777 --nameserver x.x.x.x --save&lt;br /&gt;
&lt;br /&gt;
=== Creating /dev/ptmx  ===&lt;br /&gt;
The ptmx character device should normally exist, but if it doesn't, create one.&lt;br /&gt;
 sudo mknod --mode 666 /var/lib/vz/private/777/dev/ptmx c 5 2&lt;br /&gt;
&lt;br /&gt;
=== Starting VE ===&lt;br /&gt;
Now start the VE:&lt;br /&gt;
 sudo vzctl start 777&lt;br /&gt;
&lt;br /&gt;
== Customizing the installation ==&lt;br /&gt;
A few things need to be done inside a newly created VE for it to become suitable for OpenVZ. Enter the VE to begin the configuration. Exporting the path is optional.&lt;br /&gt;
 sudo vzctl enter 777&lt;br /&gt;
 export PATH=/sbin:/usr/sbin:/bin:/usr/bin&lt;br /&gt;
&lt;br /&gt;
{{Warning|Do not run the commands below on the hardware node, they are only to be run within the VE!}}&lt;br /&gt;
&lt;br /&gt;
=== Set Debian repositories ===&lt;br /&gt;
 cat &amp;lt;&amp;lt;EOF &amp;gt; /etc/apt/sources.list&lt;br /&gt;
 deb http://http.us.debian.org/debian lenny main contrib&lt;br /&gt;
 deb http://security.debian.org lenny/updates main contrib&lt;br /&gt;
 deb http://security.debian.org lenny/volatile main contrib&lt;br /&gt;
 EOF&lt;br /&gt;
&lt;br /&gt;
=== Get new security updates ===&lt;br /&gt;
 apt-get update&lt;br /&gt;
 apt-get upgrade&lt;br /&gt;
&lt;br /&gt;
=== Install some more packages ===&lt;br /&gt;
Installing packages could be an interactive process so the system might ask some questions.  You can install more packages if you'd like.  For example:&lt;br /&gt;
 apt-get install ssh quota less&lt;br /&gt;
&lt;br /&gt;
=== Set sane permissions for &amp;lt;tt&amp;gt;/root&amp;lt;/tt&amp;gt; directory ===&lt;br /&gt;
 chmod 700 /root&lt;br /&gt;
&lt;br /&gt;
=== Disable root login ===&lt;br /&gt;
This will disable root login by default.&lt;br /&gt;
 usermod -L root&lt;br /&gt;
&lt;br /&gt;
=== Disable getty ===&lt;br /&gt;
Disable running &amp;lt;tt&amp;gt;getty&amp;lt;/tt&amp;gt;s on terminals as a VE does not have any:&lt;br /&gt;
 sed -i -e '/getty/d' /etc/inittab&lt;br /&gt;
&lt;br /&gt;
=== Disable &amp;lt;tt&amp;gt;sync()&amp;lt;/tt&amp;gt; for syslog ===&lt;br /&gt;
Turn off doing &amp;lt;tt&amp;gt;sync()&amp;lt;/tt&amp;gt; on every write for &amp;lt;tt&amp;gt;syslog&amp;lt;/tt&amp;gt;'s log files, to improve I/O performance:&lt;br /&gt;
 &amp;lt;pre&amp;gt;sed -i -e 's@\([[:space:]]\)\(/var/log/\)@\1-\2@' /etc/*syslog.conf&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Fix &amp;lt;tt&amp;gt;/etc/mtab&amp;lt;/tt&amp;gt; ===&lt;br /&gt;
Link &amp;lt;tt&amp;gt;/etc/mtab&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;/proc/mounts&amp;lt;/tt&amp;gt;, so &amp;lt;tt&amp;gt;df&amp;lt;/tt&amp;gt; and friends will work:&lt;br /&gt;
 rm -f /etc/mtab&lt;br /&gt;
 ln -s /proc/mounts /etc/mtab&lt;br /&gt;
&lt;br /&gt;
=== Remove some unneeded packages ===&lt;br /&gt;
If you have any packages you'd like to remove, now's the time for it.  Here's an example:&lt;br /&gt;
 dpkg --purge modutils ppp pppoeconf pppoe pppconfig&lt;br /&gt;
&lt;br /&gt;
=== Disable services ===&lt;br /&gt;
Do not start some services, stick to bare minimum:&lt;br /&gt;
 update-rc.d -f klogd remove&lt;br /&gt;
 update-rc.d -f quotarpc remove&lt;br /&gt;
 update-rc.d -f exim4 remove&lt;br /&gt;
 update-rc.d -f inetd remove&lt;br /&gt;
&lt;br /&gt;
=== Fix SSH host keys ===&lt;br /&gt;
This is only useful if you installed SSH.  Each individual [[VE]] should have its own pair of SSH host keys.  The code below will wipe out the existing SSH keys and instruct the newly-created [[VE]] to create new SSH keys on first boot.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- please do not remove &amp;lt;source&amp;gt;...&amp;lt;/source&amp;gt; pair of tags below,&lt;br /&gt;
     otherwise quotes after -N (-N '') are not visible --&amp;gt;&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
rm -f /etc/ssh/ssh_host_*&lt;br /&gt;
cat &amp;lt;&amp;lt; EOF &amp;gt; /etc/rc2.d/S15ssh_gen_host_keys&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
ssh-keygen -f /etc/ssh/ssh_host_rsa_key -t rsa -N ''&lt;br /&gt;
ssh-keygen -f /etc/ssh/ssh_host_dsa_key -t dsa -N ''&lt;br /&gt;
rm -f \$0&lt;br /&gt;
EOF&lt;br /&gt;
chmod a+x /etc/rc2.d/S15ssh_gen_host_keys&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Note|This will not work using the dependency-based boot sequence introduced with Squeeze...}}&lt;br /&gt;
&lt;br /&gt;
=== Change timezone ===&lt;br /&gt;
&lt;br /&gt;
You might want to change timezone if you do not live in $UTC. The following example is for Germany&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ln -sf /usr/share/zoneinfo/Europe/Berlin /etc/localtime&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
or even better&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
dpkg-reconfigure tzdata&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Clean packages ===&lt;br /&gt;
After installing packages, you'll have some junk packages laying around in your cache.  Since you don't want your template to have those, this command will wipe them out.&lt;br /&gt;
 apt-get --purge clean&lt;br /&gt;
&lt;br /&gt;
Now everything is done.  Exit from the template and go back to the hardware node.&lt;br /&gt;
 exit&lt;br /&gt;
&lt;br /&gt;
== Preparing for and packing template cache ==&lt;br /&gt;
&lt;br /&gt;
We don't need an IP for the VE anymore, and we definitely do not need it in template cache, so remove it:&lt;br /&gt;
 sudo vzctl set 777 --ipdel all --save&lt;br /&gt;
&lt;br /&gt;
Also, remove DNS server and search domain information from ''/etc/resolv.conf'' file '''in VE''':&lt;br /&gt;
 sudo nano /vz/private/777/etc/resolv.conf&lt;br /&gt;
&lt;br /&gt;
Also, remove ''/etc/hostname'' file '''in VE''':&lt;br /&gt;
 sudo rm -f /vz/private/777/etc/hostname&lt;br /&gt;
&lt;br /&gt;
Stop the VE:&lt;br /&gt;
 sudo vzctl stop 777&lt;br /&gt;
&lt;br /&gt;
Go to the VE directory:&lt;br /&gt;
 cd /vz/private/777&lt;br /&gt;
&lt;br /&gt;
Now create a cached OS tarball.  In the command below, you'll want to replace &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; with your architecture (i386, amd64, ia64, etc).&lt;br /&gt;
 sudo tar --numeric-owner -zcf /vz/template/cache/debian-4.0-i386-minimal.tar.gz .&lt;br /&gt;
&lt;br /&gt;
Look at the resulting tarball to see its size is sane:&lt;br /&gt;
 # ls -lh /vz/template/cache&lt;br /&gt;
 -rw-r--r--  1 root root  51M Apr 10 03:16 debian-4.0-i386-minimal.tar.gz&lt;br /&gt;
&lt;br /&gt;
== Checking if template cache works ==&lt;br /&gt;
We can now create a VE based on the just-created template cache.  Be sure to change &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; to your architecture just like you did when you named the tarball above.&lt;br /&gt;
 sudo vzctl create 123456 --ostemplate debian-4.0-i386-minimal&lt;br /&gt;
&lt;br /&gt;
Now make sure that it works:&lt;br /&gt;
 sudo vzctl start 123456&lt;br /&gt;
 sudo vzctl exec 123456 ps ax&lt;br /&gt;
&lt;br /&gt;
You should see that a few processes are running.&lt;br /&gt;
&lt;br /&gt;
== Final cleanup ==&lt;br /&gt;
Stop and remove the test VE you just created:&lt;br /&gt;
 sudo vzctl stop 123456&lt;br /&gt;
 sudo vzctl destroy 123456&lt;br /&gt;
 sudo rm /etc/vz/conf/123456.conf.destroyed&lt;br /&gt;
&lt;br /&gt;
Finally, let's remove the VE we used for OS template cache creation:&lt;br /&gt;
 sudo vzctl destroy 777&lt;br /&gt;
 sudo rm /etc/vz/conf/777.conf.destroyed&lt;br /&gt;
&lt;br /&gt;
You might want to edit /etc/vz/vz.conf and change DEF_OSTEMPLATE to the name of the template you use most often so that you don't have to specify the template when creating a VE.&lt;br /&gt;
 DEF_OSTEMPLATE=&amp;quot;debian-5.0-amd65-minimal&amp;quot;&lt;br /&gt;
If you use iptables, you might want to include additional modules in the list for IPTABLES in /etc/vz/vz.conf. See ''man vzctl'' for a list of available modules.&lt;br /&gt;
&lt;br /&gt;
[[Category: HOWTO]]&lt;br /&gt;
[[Category: Templates]]&lt;br /&gt;
[[Category: Debian]]&lt;/div&gt;</summary>
		<author><name>Mose</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Debian_template_creation&amp;diff=9191</id>
		<title>Debian template creation</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Debian_template_creation&amp;diff=9191"/>
		<updated>2010-09-30T14:23:45Z</updated>

		<summary type="html">&lt;p&gt;Mose: /* Final cleanup */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;These are rough instructions of how to manually create basic Debian Etch (4.0) template cache, which can be used to create OpenVZ [[VE]]s based on Debian Etch (4.0). (see also &amp;lt;tt&amp;gt;/usr/share/doc/vzctl/README.Debian&amp;lt;/tt&amp;gt; in the ''vzctl'' Debian package)&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* You shouldn't be running as root, but as a user that is permitted to use sudo instead.  It's a dangerous idea, run as root at your peril.&lt;br /&gt;
* Anywhere you see &amp;lt;tt&amp;gt;/vz&amp;lt;/tt&amp;gt;, you might really need to use &amp;lt;tt&amp;gt;/var/lib/vz&amp;lt;/tt&amp;gt; instead, especially on a Debian Etch host.&lt;br /&gt;
* Anywhere you see &amp;lt;tt&amp;gt;http://http.us.debian.org/debian/&amp;lt;/tt&amp;gt;, you can substitute your favorite Debian mirror.  ([http://www.debian.org/mirror/list List of official Debian Mirrors])&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Prerequisites ==&lt;br /&gt;
You need to have a working copy of &amp;lt;tt&amp;gt;debootstrap&amp;lt;/tt&amp;gt; running on your hardware node.&lt;br /&gt;
&lt;br /&gt;
For Debian:&lt;br /&gt;
 sudo apt-get install debootstrap&lt;br /&gt;
&lt;br /&gt;
For Gentoo:&lt;br /&gt;
 sudo emerge debootstrap&lt;br /&gt;
&lt;br /&gt;
For Fedora (at least Fedora 8 have it, not sure about earlier versions):&lt;br /&gt;
 sudo yum install debootstrap&lt;br /&gt;
&lt;br /&gt;
For other distros you might need to install it from sources, or search for an appropriate package for your distribution.  An RPM is available on the [http://forum.openvz.org/index.php?t=tree&amp;amp;th=142&amp;amp;mid=584 OpenVZ Forum].&lt;br /&gt;
&lt;br /&gt;
== Bootstrapping Debian ==&lt;br /&gt;
&lt;br /&gt;
You can install different releases of Debian into a VE's private directory using the debootstrap command.&lt;br /&gt;
&lt;br /&gt;
The command parameters are:&lt;br /&gt;
&lt;br /&gt;
  debootstrap --arch ARCH NAME DIRECTORY URL&lt;br /&gt;
&lt;br /&gt;
Specify your architecture instead of &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; if you're using something other than i386/x86.  For example, for AMD64/x86_64, use &amp;lt;tt&amp;gt;amd64&amp;lt;/tt&amp;gt; or for ia64, use &amp;lt;tt&amp;gt;ia64&amp;lt;/tt&amp;gt;. You can use http or ftp in the URL.&lt;br /&gt;
&lt;br /&gt;
We use VE ID of 777 for this example, but it can be any unused ID.&lt;br /&gt;
&lt;br /&gt;
=== Lenny (current Debian stable) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap --arch i386 lenny /vz/private/777 http://http.us.debian.org/debian/&lt;br /&gt;
 or&lt;br /&gt;
 debootstrap --arch amd64 lenny /vz/private/777 ftp://ftp.us.debian.org/debian/&lt;br /&gt;
&lt;br /&gt;
=== Etch (old release) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap --arch i386 etch /vz/private/777 http://http.us.debian.org/debian/&lt;br /&gt;
&lt;br /&gt;
=== Sarge (very old release) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap sarge /vz/private/777 http://archive.debian.org/debian&lt;br /&gt;
&lt;br /&gt;
== Preparing the HN network ==&lt;br /&gt;
Append the following lines to /etc/sysctl.conf, adjust to taste and then execute &amp;quot;sysctl -p&amp;quot; for them to take effect.&lt;br /&gt;
 ### OpenVZ settings&lt;br /&gt;
 &lt;br /&gt;
 # On Hardware Node we generally need packet&lt;br /&gt;
 # forwarding enabled and proxy arp disabled&lt;br /&gt;
 &lt;br /&gt;
 net.ipv4.conf.default.forwarding=1&lt;br /&gt;
 net.ipv4.conf.default.proxy_arp = 0&lt;br /&gt;
 net.ipv4.ip_forward=1&lt;br /&gt;
 &lt;br /&gt;
 # Enables source route verification&lt;br /&gt;
 net.ipv4.conf.all.rp_filter = 1&lt;br /&gt;
 &lt;br /&gt;
 # Enables the magic-sysrq key&lt;br /&gt;
 kernel.sysrq = 1&lt;br /&gt;
 &lt;br /&gt;
 # TCP Explict Congestion Notification&lt;br /&gt;
 net.ipv4.tcp_ecn = 0&lt;br /&gt;
 &lt;br /&gt;
 # we do not want all our interfaces to send redirects&lt;br /&gt;
 net.ipv4.conf.default.send_redirects = 1&lt;br /&gt;
 net.ipv4.conf.all.send_redirects = 0&lt;br /&gt;
&lt;br /&gt;
== Preparing and starting the VE ==&lt;br /&gt;
&lt;br /&gt;
=== Setting VE config ===&lt;br /&gt;
First, we need a config for the [[VE]]:&lt;br /&gt;
 sudo vzctl set 777 --applyconfig vps.basic --save&lt;br /&gt;
&lt;br /&gt;
=== Setting VE OSTEMPLATE ===&lt;br /&gt;
Also, we need &amp;lt;tt&amp;gt;OSTEMPLATE&amp;lt;/tt&amp;gt; to be set in VE configuration file, for the [[vzctl]] to work properly.&lt;br /&gt;
&lt;br /&gt;
 sudo sh -c 'echo &amp;quot;OSTEMPLATE=debian-4.0&amp;quot; &amp;gt;&amp;gt; /etc/vz/conf/777.conf'&lt;br /&gt;
&lt;br /&gt;
=== Setting VE IP address ===&lt;br /&gt;
For the [[VE]] to be able to download updates from the Internet, we need a valid IP address for it:&lt;br /&gt;
 sudo vzctl set 777 --ipadd x.x.x.x --save&lt;br /&gt;
&lt;br /&gt;
{{Note|if you use private IP for the VE, you might have to set up NAT as described in [[Using NAT for VE with private IPs]].}}&lt;br /&gt;
&lt;br /&gt;
=== Setting DNS server for VE ===&lt;br /&gt;
For the [[VE]] to be able to download updates from the Internet, we also need to specify a DNS for it:&lt;br /&gt;
 sudo vzctl set 777 --nameserver x.x.x.x --save&lt;br /&gt;
&lt;br /&gt;
=== Creating /dev/ptmx  ===&lt;br /&gt;
The ptmx character device should normally exist, but if it doesn't, create one.&lt;br /&gt;
 sudo mknod --mode 666 /var/lib/vz/private/777/dev/ptmx c 5 2&lt;br /&gt;
&lt;br /&gt;
=== Starting VE ===&lt;br /&gt;
Now start the VE:&lt;br /&gt;
 sudo vzctl start 777&lt;br /&gt;
&lt;br /&gt;
== Customizing the installation ==&lt;br /&gt;
A few things need to be done inside a newly created VE for it to become suitable for OpenVZ. Enter the VE to begin the configuration. Exporting the path is optional.&lt;br /&gt;
 sudo vzctl enter 777&lt;br /&gt;
 export PATH=/sbin:/usr/sbin:/bin:/usr/bin&lt;br /&gt;
&lt;br /&gt;
{{Warning|Do not run the commands below on the hardware node, they are only to be run within the VE!}}&lt;br /&gt;
&lt;br /&gt;
=== Set Debian repositories ===&lt;br /&gt;
 cat &amp;lt;&amp;lt;EOF &amp;gt; /etc/apt/sources.list&lt;br /&gt;
 deb http://http.us.debian.org/debian lenny main contrib&lt;br /&gt;
 deb http://security.debian.org lenny/updates main contrib&lt;br /&gt;
 deb http://security.debian.org lenny/volatile main contrib&lt;br /&gt;
 EOF&lt;br /&gt;
&lt;br /&gt;
=== Get new security updates ===&lt;br /&gt;
 apt-get update&lt;br /&gt;
 apt-get upgrade&lt;br /&gt;
&lt;br /&gt;
=== Install some more packages ===&lt;br /&gt;
Installing packages could be an interactive process so the system might ask some questions.  You can install more packages if you'd like.  For example:&lt;br /&gt;
 apt-get install ssh quota less&lt;br /&gt;
&lt;br /&gt;
=== Set sane permissions for &amp;lt;tt&amp;gt;/root&amp;lt;/tt&amp;gt; directory ===&lt;br /&gt;
 chmod 700 /root&lt;br /&gt;
&lt;br /&gt;
=== Disable root login ===&lt;br /&gt;
This will disable root login by default.&lt;br /&gt;
 usermod -L root&lt;br /&gt;
&lt;br /&gt;
=== Disable getty ===&lt;br /&gt;
Disable running &amp;lt;tt&amp;gt;getty&amp;lt;/tt&amp;gt;s on terminals as a VE does not have any:&lt;br /&gt;
 sed -i -e '/getty/d' /etc/inittab&lt;br /&gt;
&lt;br /&gt;
=== Disable &amp;lt;tt&amp;gt;sync()&amp;lt;/tt&amp;gt; for syslog ===&lt;br /&gt;
Turn off doing &amp;lt;tt&amp;gt;sync()&amp;lt;/tt&amp;gt; on every write for &amp;lt;tt&amp;gt;syslog&amp;lt;/tt&amp;gt;'s log files, to improve I/O performance:&lt;br /&gt;
 &amp;lt;pre&amp;gt;sed -i -e 's@\([[:space:]]\)\(/var/log/\)@\1-\2@' /etc/*syslog.conf&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Fix &amp;lt;tt&amp;gt;/etc/mtab&amp;lt;/tt&amp;gt; ===&lt;br /&gt;
Link &amp;lt;tt&amp;gt;/etc/mtab&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;/proc/mounts&amp;lt;/tt&amp;gt;, so &amp;lt;tt&amp;gt;df&amp;lt;/tt&amp;gt; and friends will work:&lt;br /&gt;
 rm -f /etc/mtab&lt;br /&gt;
 ln -s /proc/mounts /etc/mtab&lt;br /&gt;
&lt;br /&gt;
=== Remove some unneeded packages ===&lt;br /&gt;
If you have any packages you'd like to remove, now's the time for it.  Here's an example:&lt;br /&gt;
 dpkg --purge modutils ppp pppoeconf pppoe pppconfig&lt;br /&gt;
&lt;br /&gt;
=== Disable services ===&lt;br /&gt;
Do not start some services, stick to bare minimum:&lt;br /&gt;
 update-rc.d -f klogd remove&lt;br /&gt;
 update-rc.d -f quotarpc remove&lt;br /&gt;
 update-rc.d -f exim4 remove&lt;br /&gt;
 update-rc.d -f inetd remove&lt;br /&gt;
&lt;br /&gt;
=== Fix SSH host keys ===&lt;br /&gt;
This is only useful if you installed SSH.  Each individual [[VE]] should have its own pair of SSH host keys.  The code below will wipe out the existing SSH keys and instruct the newly-created [[VE]] to create new SSH keys on first boot.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- please do not remove &amp;lt;source&amp;gt;...&amp;lt;/source&amp;gt; pair of tags below,&lt;br /&gt;
     otherwise quotes after -N (-N '') are not visible --&amp;gt;&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
rm -f /etc/ssh/ssh_host_*&lt;br /&gt;
cat &amp;lt;&amp;lt; EOF &amp;gt; /etc/rc2.d/S15ssh_gen_host_keys&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
ssh-keygen -f /etc/ssh/ssh_host_rsa_key -t rsa -N ''&lt;br /&gt;
ssh-keygen -f /etc/ssh/ssh_host_dsa_key -t dsa -N ''&lt;br /&gt;
rm -f \$0&lt;br /&gt;
EOF&lt;br /&gt;
chmod a+x /etc/rc2.d/S15ssh_gen_host_keys&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Note|This will not work using the dependency-based boot sequence introduced with Squeeze...}}&lt;br /&gt;
&lt;br /&gt;
=== Change timezone ===&lt;br /&gt;
&lt;br /&gt;
You might want to change timezone if you do not live in $UTC. The following example is for Germany&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ln -sf /usr/share/zoneinfo/Europe/Berlin /etc/localtime&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
or even better&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
dpkg-reconfigure tzdata&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Clean packages ===&lt;br /&gt;
After installing packages, you'll have some junk packages laying around in your cache.  Since you don't want your template to have those, this command will wipe them out.&lt;br /&gt;
 apt-get --purge clean&lt;br /&gt;
&lt;br /&gt;
Now everything is done.  Exit from the template and go back to the hardware node.&lt;br /&gt;
 exit&lt;br /&gt;
&lt;br /&gt;
== Preparing for and packing template cache ==&lt;br /&gt;
&lt;br /&gt;
We don't need an IP for the VE anymore, and we definitely do not need it in template cache, so remove it:&lt;br /&gt;
 sudo vzctl set 777 --ipdel all --save&lt;br /&gt;
&lt;br /&gt;
Also, remove DNS server and search domain information from ''/etc/resolv.conf'' file '''in VE''':&lt;br /&gt;
 sudo nano /vz/private/777/etc/resolv.conf&lt;br /&gt;
&lt;br /&gt;
Also, remove ''/etc/hostname'' file '''in VE''':&lt;br /&gt;
 sudo rm -f /vz/private/777/etc/hostname&lt;br /&gt;
&lt;br /&gt;
Stop the VE:&lt;br /&gt;
 sudo vzctl stop 777&lt;br /&gt;
&lt;br /&gt;
Go to the VE directory:&lt;br /&gt;
 cd /vz/private/777&lt;br /&gt;
&lt;br /&gt;
Now create a cached OS tarball.  In the command below, you'll want to replace &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; with your architecture (i386, amd64, ia64, etc).&lt;br /&gt;
 sudo tar --numeric-owner -zcf /vz/template/cache/debian-4.0-i386-minimal.tar.gz .&lt;br /&gt;
&lt;br /&gt;
Look at the resulting tarball to see its size is sane:&lt;br /&gt;
 # ls -lh /vz/template/cache&lt;br /&gt;
 -rw-r--r--  1 root root  51M Apr 10 03:16 debian-4.0-i386-minimal.tar.gz&lt;br /&gt;
&lt;br /&gt;
== Checking if template cache works ==&lt;br /&gt;
We can now create a VE based on the just-created template cache.  Be sure to change &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; to your architecture just like you did when you named the tarball above.&lt;br /&gt;
 sudo vzctl create 123456 --ostemplate debian-4.0-i386-minimal&lt;br /&gt;
&lt;br /&gt;
Now make sure that it works:&lt;br /&gt;
 sudo vzctl start 123456&lt;br /&gt;
 sudo vzctl exec 123456 ps ax&lt;br /&gt;
&lt;br /&gt;
You should see that a few processes are running.&lt;br /&gt;
&lt;br /&gt;
== Final cleanup ==&lt;br /&gt;
Stop and remove the test VE you just created:&lt;br /&gt;
 sudo vzctl stop 123456&lt;br /&gt;
 sudo vzctl destroy 123456&lt;br /&gt;
 sudo rm /etc/vz/conf/123456.conf.destroyed&lt;br /&gt;
&lt;br /&gt;
Finally, let's remove the VE we used for OS template cache creation:&lt;br /&gt;
 sudo vzctl destroy 777&lt;br /&gt;
 sudo rm /etc/vz/conf/777.conf.destroyed&lt;br /&gt;
&lt;br /&gt;
You might want to edit /etc/vz/vz.conf and change DEF_OSTEMPLATE to the name of the template you use most often so that you don't have to specify the template when creating a VE.&lt;br /&gt;
 DEF_OSTEMPLATE=&amp;quot;debian-5.0-amd65-minimal&amp;quot;&lt;br /&gt;
If you use iptables, you might want to include additional modules in the list for IPTABLES in /etc/vz/vz.conf. See ''man vzctl'' for a list of available modules.&lt;br /&gt;
&lt;br /&gt;
[[Category: HOWTO]]&lt;br /&gt;
[[Category: Templates]]&lt;br /&gt;
[[Category: Debian]]&lt;/div&gt;</summary>
		<author><name>Mose</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Debian_template_creation&amp;diff=9190</id>
		<title>Debian template creation</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Debian_template_creation&amp;diff=9190"/>
		<updated>2010-09-30T14:21:49Z</updated>

		<summary type="html">&lt;p&gt;Mose: /* Final cleanup */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;These are rough instructions of how to manually create basic Debian Etch (4.0) template cache, which can be used to create OpenVZ [[VE]]s based on Debian Etch (4.0). (see also &amp;lt;tt&amp;gt;/usr/share/doc/vzctl/README.Debian&amp;lt;/tt&amp;gt; in the ''vzctl'' Debian package)&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* You shouldn't be running as root, but as a user that is permitted to use sudo instead.  It's a dangerous idea, run as root at your peril.&lt;br /&gt;
* Anywhere you see &amp;lt;tt&amp;gt;/vz&amp;lt;/tt&amp;gt;, you might really need to use &amp;lt;tt&amp;gt;/var/lib/vz&amp;lt;/tt&amp;gt; instead, especially on a Debian Etch host.&lt;br /&gt;
* Anywhere you see &amp;lt;tt&amp;gt;http://http.us.debian.org/debian/&amp;lt;/tt&amp;gt;, you can substitute your favorite Debian mirror.  ([http://www.debian.org/mirror/list List of official Debian Mirrors])&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Prerequisites ==&lt;br /&gt;
You need to have a working copy of &amp;lt;tt&amp;gt;debootstrap&amp;lt;/tt&amp;gt; running on your hardware node.&lt;br /&gt;
&lt;br /&gt;
For Debian:&lt;br /&gt;
 sudo apt-get install debootstrap&lt;br /&gt;
&lt;br /&gt;
For Gentoo:&lt;br /&gt;
 sudo emerge debootstrap&lt;br /&gt;
&lt;br /&gt;
For Fedora (at least Fedora 8 have it, not sure about earlier versions):&lt;br /&gt;
 sudo yum install debootstrap&lt;br /&gt;
&lt;br /&gt;
For other distros you might need to install it from sources, or search for an appropriate package for your distribution.  An RPM is available on the [http://forum.openvz.org/index.php?t=tree&amp;amp;th=142&amp;amp;mid=584 OpenVZ Forum].&lt;br /&gt;
&lt;br /&gt;
== Bootstrapping Debian ==&lt;br /&gt;
&lt;br /&gt;
You can install different releases of Debian into a VE's private directory using the debootstrap command.&lt;br /&gt;
&lt;br /&gt;
The command parameters are:&lt;br /&gt;
&lt;br /&gt;
  debootstrap --arch ARCH NAME DIRECTORY URL&lt;br /&gt;
&lt;br /&gt;
Specify your architecture instead of &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; if you're using something other than i386/x86.  For example, for AMD64/x86_64, use &amp;lt;tt&amp;gt;amd64&amp;lt;/tt&amp;gt; or for ia64, use &amp;lt;tt&amp;gt;ia64&amp;lt;/tt&amp;gt;. You can use http or ftp in the URL.&lt;br /&gt;
&lt;br /&gt;
We use VE ID of 777 for this example, but it can be any unused ID.&lt;br /&gt;
&lt;br /&gt;
=== Lenny (current Debian stable) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap --arch i386 lenny /vz/private/777 http://http.us.debian.org/debian/&lt;br /&gt;
 or&lt;br /&gt;
 debootstrap --arch amd64 lenny /vz/private/777 ftp://ftp.us.debian.org/debian/&lt;br /&gt;
&lt;br /&gt;
=== Etch (old release) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap --arch i386 etch /vz/private/777 http://http.us.debian.org/debian/&lt;br /&gt;
&lt;br /&gt;
=== Sarge (very old release) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap sarge /vz/private/777 http://archive.debian.org/debian&lt;br /&gt;
&lt;br /&gt;
== Preparing the HN network ==&lt;br /&gt;
Append the following lines to /etc/sysctl.conf, adjust to taste and then execute &amp;quot;sysctl -p&amp;quot; for them to take effect.&lt;br /&gt;
 ### OpenVZ settings&lt;br /&gt;
 &lt;br /&gt;
 # On Hardware Node we generally need packet&lt;br /&gt;
 # forwarding enabled and proxy arp disabled&lt;br /&gt;
 &lt;br /&gt;
 net.ipv4.conf.default.forwarding=1&lt;br /&gt;
 net.ipv4.conf.default.proxy_arp = 0&lt;br /&gt;
 net.ipv4.ip_forward=1&lt;br /&gt;
 &lt;br /&gt;
 # Enables source route verification&lt;br /&gt;
 net.ipv4.conf.all.rp_filter = 1&lt;br /&gt;
 &lt;br /&gt;
 # Enables the magic-sysrq key&lt;br /&gt;
 kernel.sysrq = 1&lt;br /&gt;
 &lt;br /&gt;
 # TCP Explict Congestion Notification&lt;br /&gt;
 net.ipv4.tcp_ecn = 0&lt;br /&gt;
 &lt;br /&gt;
 # we do not want all our interfaces to send redirects&lt;br /&gt;
 net.ipv4.conf.default.send_redirects = 1&lt;br /&gt;
 net.ipv4.conf.all.send_redirects = 0&lt;br /&gt;
&lt;br /&gt;
== Preparing and starting the VE ==&lt;br /&gt;
&lt;br /&gt;
=== Setting VE config ===&lt;br /&gt;
First, we need a config for the [[VE]]:&lt;br /&gt;
 sudo vzctl set 777 --applyconfig vps.basic --save&lt;br /&gt;
&lt;br /&gt;
=== Setting VE OSTEMPLATE ===&lt;br /&gt;
Also, we need &amp;lt;tt&amp;gt;OSTEMPLATE&amp;lt;/tt&amp;gt; to be set in VE configuration file, for the [[vzctl]] to work properly.&lt;br /&gt;
&lt;br /&gt;
 sudo sh -c 'echo &amp;quot;OSTEMPLATE=debian-4.0&amp;quot; &amp;gt;&amp;gt; /etc/vz/conf/777.conf'&lt;br /&gt;
&lt;br /&gt;
=== Setting VE IP address ===&lt;br /&gt;
For the [[VE]] to be able to download updates from the Internet, we need a valid IP address for it:&lt;br /&gt;
 sudo vzctl set 777 --ipadd x.x.x.x --save&lt;br /&gt;
&lt;br /&gt;
{{Note|if you use private IP for the VE, you might have to set up NAT as described in [[Using NAT for VE with private IPs]].}}&lt;br /&gt;
&lt;br /&gt;
=== Setting DNS server for VE ===&lt;br /&gt;
For the [[VE]] to be able to download updates from the Internet, we also need to specify a DNS for it:&lt;br /&gt;
 sudo vzctl set 777 --nameserver x.x.x.x --save&lt;br /&gt;
&lt;br /&gt;
=== Creating /dev/ptmx  ===&lt;br /&gt;
The ptmx character device should normally exist, but if it doesn't, create one.&lt;br /&gt;
 sudo mknod --mode 666 /var/lib/vz/private/777/dev/ptmx c 5 2&lt;br /&gt;
&lt;br /&gt;
=== Starting VE ===&lt;br /&gt;
Now start the VE:&lt;br /&gt;
 sudo vzctl start 777&lt;br /&gt;
&lt;br /&gt;
== Customizing the installation ==&lt;br /&gt;
A few things need to be done inside a newly created VE for it to become suitable for OpenVZ. Enter the VE to begin the configuration. Exporting the path is optional.&lt;br /&gt;
 sudo vzctl enter 777&lt;br /&gt;
 export PATH=/sbin:/usr/sbin:/bin:/usr/bin&lt;br /&gt;
&lt;br /&gt;
{{Warning|Do not run the commands below on the hardware node, they are only to be run within the VE!}}&lt;br /&gt;
&lt;br /&gt;
=== Set Debian repositories ===&lt;br /&gt;
 cat &amp;lt;&amp;lt;EOF &amp;gt; /etc/apt/sources.list&lt;br /&gt;
 deb http://http.us.debian.org/debian lenny main contrib&lt;br /&gt;
 deb http://security.debian.org lenny/updates main contrib&lt;br /&gt;
 deb http://security.debian.org lenny/volatile main contrib&lt;br /&gt;
 EOF&lt;br /&gt;
&lt;br /&gt;
=== Get new security updates ===&lt;br /&gt;
 apt-get update&lt;br /&gt;
 apt-get upgrade&lt;br /&gt;
&lt;br /&gt;
=== Install some more packages ===&lt;br /&gt;
Installing packages could be an interactive process so the system might ask some questions.  You can install more packages if you'd like.  For example:&lt;br /&gt;
 apt-get install ssh quota less&lt;br /&gt;
&lt;br /&gt;
=== Set sane permissions for &amp;lt;tt&amp;gt;/root&amp;lt;/tt&amp;gt; directory ===&lt;br /&gt;
 chmod 700 /root&lt;br /&gt;
&lt;br /&gt;
=== Disable root login ===&lt;br /&gt;
This will disable root login by default.&lt;br /&gt;
 usermod -L root&lt;br /&gt;
&lt;br /&gt;
=== Disable getty ===&lt;br /&gt;
Disable running &amp;lt;tt&amp;gt;getty&amp;lt;/tt&amp;gt;s on terminals as a VE does not have any:&lt;br /&gt;
 sed -i -e '/getty/d' /etc/inittab&lt;br /&gt;
&lt;br /&gt;
=== Disable &amp;lt;tt&amp;gt;sync()&amp;lt;/tt&amp;gt; for syslog ===&lt;br /&gt;
Turn off doing &amp;lt;tt&amp;gt;sync()&amp;lt;/tt&amp;gt; on every write for &amp;lt;tt&amp;gt;syslog&amp;lt;/tt&amp;gt;'s log files, to improve I/O performance:&lt;br /&gt;
 &amp;lt;pre&amp;gt;sed -i -e 's@\([[:space:]]\)\(/var/log/\)@\1-\2@' /etc/*syslog.conf&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Fix &amp;lt;tt&amp;gt;/etc/mtab&amp;lt;/tt&amp;gt; ===&lt;br /&gt;
Link &amp;lt;tt&amp;gt;/etc/mtab&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;/proc/mounts&amp;lt;/tt&amp;gt;, so &amp;lt;tt&amp;gt;df&amp;lt;/tt&amp;gt; and friends will work:&lt;br /&gt;
 rm -f /etc/mtab&lt;br /&gt;
 ln -s /proc/mounts /etc/mtab&lt;br /&gt;
&lt;br /&gt;
=== Remove some unneeded packages ===&lt;br /&gt;
If you have any packages you'd like to remove, now's the time for it.  Here's an example:&lt;br /&gt;
 dpkg --purge modutils ppp pppoeconf pppoe pppconfig&lt;br /&gt;
&lt;br /&gt;
=== Disable services ===&lt;br /&gt;
Do not start some services, stick to bare minimum:&lt;br /&gt;
 update-rc.d -f klogd remove&lt;br /&gt;
 update-rc.d -f quotarpc remove&lt;br /&gt;
 update-rc.d -f exim4 remove&lt;br /&gt;
 update-rc.d -f inetd remove&lt;br /&gt;
&lt;br /&gt;
=== Fix SSH host keys ===&lt;br /&gt;
This is only useful if you installed SSH.  Each individual [[VE]] should have its own pair of SSH host keys.  The code below will wipe out the existing SSH keys and instruct the newly-created [[VE]] to create new SSH keys on first boot.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- please do not remove &amp;lt;source&amp;gt;...&amp;lt;/source&amp;gt; pair of tags below,&lt;br /&gt;
     otherwise quotes after -N (-N '') are not visible --&amp;gt;&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
rm -f /etc/ssh/ssh_host_*&lt;br /&gt;
cat &amp;lt;&amp;lt; EOF &amp;gt; /etc/rc2.d/S15ssh_gen_host_keys&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
ssh-keygen -f /etc/ssh/ssh_host_rsa_key -t rsa -N ''&lt;br /&gt;
ssh-keygen -f /etc/ssh/ssh_host_dsa_key -t dsa -N ''&lt;br /&gt;
rm -f \$0&lt;br /&gt;
EOF&lt;br /&gt;
chmod a+x /etc/rc2.d/S15ssh_gen_host_keys&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Note|This will not work using the dependency-based boot sequence introduced with Squeeze...}}&lt;br /&gt;
&lt;br /&gt;
=== Change timezone ===&lt;br /&gt;
&lt;br /&gt;
You might want to change timezone if you do not live in $UTC. The following example is for Germany&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ln -sf /usr/share/zoneinfo/Europe/Berlin /etc/localtime&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
or even better&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
dpkg-reconfigure tzdata&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Clean packages ===&lt;br /&gt;
After installing packages, you'll have some junk packages laying around in your cache.  Since you don't want your template to have those, this command will wipe them out.&lt;br /&gt;
 apt-get --purge clean&lt;br /&gt;
&lt;br /&gt;
Now everything is done.  Exit from the template and go back to the hardware node.&lt;br /&gt;
 exit&lt;br /&gt;
&lt;br /&gt;
== Preparing for and packing template cache ==&lt;br /&gt;
&lt;br /&gt;
We don't need an IP for the VE anymore, and we definitely do not need it in template cache, so remove it:&lt;br /&gt;
 sudo vzctl set 777 --ipdel all --save&lt;br /&gt;
&lt;br /&gt;
Also, remove DNS server and search domain information from ''/etc/resolv.conf'' file '''in VE''':&lt;br /&gt;
 sudo nano /vz/private/777/etc/resolv.conf&lt;br /&gt;
&lt;br /&gt;
Also, remove ''/etc/hostname'' file '''in VE''':&lt;br /&gt;
 sudo rm -f /vz/private/777/etc/hostname&lt;br /&gt;
&lt;br /&gt;
Stop the VE:&lt;br /&gt;
 sudo vzctl stop 777&lt;br /&gt;
&lt;br /&gt;
Go to the VE directory:&lt;br /&gt;
 cd /vz/private/777&lt;br /&gt;
&lt;br /&gt;
Now create a cached OS tarball.  In the command below, you'll want to replace &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; with your architecture (i386, amd64, ia64, etc).&lt;br /&gt;
 sudo tar --numeric-owner -zcf /vz/template/cache/debian-4.0-i386-minimal.tar.gz .&lt;br /&gt;
&lt;br /&gt;
Look at the resulting tarball to see its size is sane:&lt;br /&gt;
 # ls -lh /vz/template/cache&lt;br /&gt;
 -rw-r--r--  1 root root  51M Apr 10 03:16 debian-4.0-i386-minimal.tar.gz&lt;br /&gt;
&lt;br /&gt;
== Checking if template cache works ==&lt;br /&gt;
We can now create a VE based on the just-created template cache.  Be sure to change &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; to your architecture just like you did when you named the tarball above.&lt;br /&gt;
 sudo vzctl create 123456 --ostemplate debian-4.0-i386-minimal&lt;br /&gt;
&lt;br /&gt;
Now make sure that it works:&lt;br /&gt;
 sudo vzctl start 123456&lt;br /&gt;
 sudo vzctl exec 123456 ps ax&lt;br /&gt;
&lt;br /&gt;
You should see that a few processes are running.&lt;br /&gt;
&lt;br /&gt;
== Final cleanup ==&lt;br /&gt;
Stop and remove the test VE you just created:&lt;br /&gt;
 sudo vzctl stop 123456&lt;br /&gt;
 sudo vzctl destroy 123456&lt;br /&gt;
 sudo rm /etc/vz/conf/123456.conf.destroyed&lt;br /&gt;
&lt;br /&gt;
Finally, let's remove the VE we used for OS template cache creation:&lt;br /&gt;
 sudo vzctl destroy 777&lt;br /&gt;
 sudo rm /etc/vz/conf/777.conf.destroyed&lt;br /&gt;
&lt;br /&gt;
You might want to edit /etc/vz/vz.conf and change DEF_OSTEMPLATE to the name of the template you use most often so that you don't have to specify the template when creating a VE.&lt;br /&gt;
 DEF_OSTEMPLATE=&amp;quot;debian-5.0-amd65-minimal&amp;quot;&lt;br /&gt;
If you use iptables, you might want to include additional modules in the list. See ''man vzctl'' for a list of available modules.&lt;br /&gt;
&lt;br /&gt;
[[Category: HOWTO]]&lt;br /&gt;
[[Category: Templates]]&lt;br /&gt;
[[Category: Debian]]&lt;/div&gt;</summary>
		<author><name>Mose</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Debian_template_creation&amp;diff=9189</id>
		<title>Debian template creation</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Debian_template_creation&amp;diff=9189"/>
		<updated>2010-09-30T14:03:44Z</updated>

		<summary type="html">&lt;p&gt;Mose: /* Clean packages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;These are rough instructions of how to manually create basic Debian Etch (4.0) template cache, which can be used to create OpenVZ [[VE]]s based on Debian Etch (4.0). (see also &amp;lt;tt&amp;gt;/usr/share/doc/vzctl/README.Debian&amp;lt;/tt&amp;gt; in the ''vzctl'' Debian package)&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* You shouldn't be running as root, but as a user that is permitted to use sudo instead.  It's a dangerous idea, run as root at your peril.&lt;br /&gt;
* Anywhere you see &amp;lt;tt&amp;gt;/vz&amp;lt;/tt&amp;gt;, you might really need to use &amp;lt;tt&amp;gt;/var/lib/vz&amp;lt;/tt&amp;gt; instead, especially on a Debian Etch host.&lt;br /&gt;
* Anywhere you see &amp;lt;tt&amp;gt;http://http.us.debian.org/debian/&amp;lt;/tt&amp;gt;, you can substitute your favorite Debian mirror.  ([http://www.debian.org/mirror/list List of official Debian Mirrors])&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Prerequisites ==&lt;br /&gt;
You need to have a working copy of &amp;lt;tt&amp;gt;debootstrap&amp;lt;/tt&amp;gt; running on your hardware node.&lt;br /&gt;
&lt;br /&gt;
For Debian:&lt;br /&gt;
 sudo apt-get install debootstrap&lt;br /&gt;
&lt;br /&gt;
For Gentoo:&lt;br /&gt;
 sudo emerge debootstrap&lt;br /&gt;
&lt;br /&gt;
For Fedora (at least Fedora 8 have it, not sure about earlier versions):&lt;br /&gt;
 sudo yum install debootstrap&lt;br /&gt;
&lt;br /&gt;
For other distros you might need to install it from sources, or search for an appropriate package for your distribution.  An RPM is available on the [http://forum.openvz.org/index.php?t=tree&amp;amp;th=142&amp;amp;mid=584 OpenVZ Forum].&lt;br /&gt;
&lt;br /&gt;
== Bootstrapping Debian ==&lt;br /&gt;
&lt;br /&gt;
You can install different releases of Debian into a VE's private directory using the debootstrap command.&lt;br /&gt;
&lt;br /&gt;
The command parameters are:&lt;br /&gt;
&lt;br /&gt;
  debootstrap --arch ARCH NAME DIRECTORY URL&lt;br /&gt;
&lt;br /&gt;
Specify your architecture instead of &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; if you're using something other than i386/x86.  For example, for AMD64/x86_64, use &amp;lt;tt&amp;gt;amd64&amp;lt;/tt&amp;gt; or for ia64, use &amp;lt;tt&amp;gt;ia64&amp;lt;/tt&amp;gt;. You can use http or ftp in the URL.&lt;br /&gt;
&lt;br /&gt;
We use VE ID of 777 for this example, but it can be any unused ID.&lt;br /&gt;
&lt;br /&gt;
=== Lenny (current Debian stable) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap --arch i386 lenny /vz/private/777 http://http.us.debian.org/debian/&lt;br /&gt;
 or&lt;br /&gt;
 debootstrap --arch amd64 lenny /vz/private/777 ftp://ftp.us.debian.org/debian/&lt;br /&gt;
&lt;br /&gt;
=== Etch (old release) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap --arch i386 etch /vz/private/777 http://http.us.debian.org/debian/&lt;br /&gt;
&lt;br /&gt;
=== Sarge (very old release) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap sarge /vz/private/777 http://archive.debian.org/debian&lt;br /&gt;
&lt;br /&gt;
== Preparing the HN network ==&lt;br /&gt;
Append the following lines to /etc/sysctl.conf, adjust to taste and then execute &amp;quot;sysctl -p&amp;quot; for them to take effect.&lt;br /&gt;
 ### OpenVZ settings&lt;br /&gt;
 &lt;br /&gt;
 # On Hardware Node we generally need packet&lt;br /&gt;
 # forwarding enabled and proxy arp disabled&lt;br /&gt;
 &lt;br /&gt;
 net.ipv4.conf.default.forwarding=1&lt;br /&gt;
 net.ipv4.conf.default.proxy_arp = 0&lt;br /&gt;
 net.ipv4.ip_forward=1&lt;br /&gt;
 &lt;br /&gt;
 # Enables source route verification&lt;br /&gt;
 net.ipv4.conf.all.rp_filter = 1&lt;br /&gt;
 &lt;br /&gt;
 # Enables the magic-sysrq key&lt;br /&gt;
 kernel.sysrq = 1&lt;br /&gt;
 &lt;br /&gt;
 # TCP Explict Congestion Notification&lt;br /&gt;
 net.ipv4.tcp_ecn = 0&lt;br /&gt;
 &lt;br /&gt;
 # we do not want all our interfaces to send redirects&lt;br /&gt;
 net.ipv4.conf.default.send_redirects = 1&lt;br /&gt;
 net.ipv4.conf.all.send_redirects = 0&lt;br /&gt;
&lt;br /&gt;
== Preparing and starting the VE ==&lt;br /&gt;
&lt;br /&gt;
=== Setting VE config ===&lt;br /&gt;
First, we need a config for the [[VE]]:&lt;br /&gt;
 sudo vzctl set 777 --applyconfig vps.basic --save&lt;br /&gt;
&lt;br /&gt;
=== Setting VE OSTEMPLATE ===&lt;br /&gt;
Also, we need &amp;lt;tt&amp;gt;OSTEMPLATE&amp;lt;/tt&amp;gt; to be set in VE configuration file, for the [[vzctl]] to work properly.&lt;br /&gt;
&lt;br /&gt;
 sudo sh -c 'echo &amp;quot;OSTEMPLATE=debian-4.0&amp;quot; &amp;gt;&amp;gt; /etc/vz/conf/777.conf'&lt;br /&gt;
&lt;br /&gt;
=== Setting VE IP address ===&lt;br /&gt;
For the [[VE]] to be able to download updates from the Internet, we need a valid IP address for it:&lt;br /&gt;
 sudo vzctl set 777 --ipadd x.x.x.x --save&lt;br /&gt;
&lt;br /&gt;
{{Note|if you use private IP for the VE, you might have to set up NAT as described in [[Using NAT for VE with private IPs]].}}&lt;br /&gt;
&lt;br /&gt;
=== Setting DNS server for VE ===&lt;br /&gt;
For the [[VE]] to be able to download updates from the Internet, we also need to specify a DNS for it:&lt;br /&gt;
 sudo vzctl set 777 --nameserver x.x.x.x --save&lt;br /&gt;
&lt;br /&gt;
=== Creating /dev/ptmx  ===&lt;br /&gt;
The ptmx character device should normally exist, but if it doesn't, create one.&lt;br /&gt;
 sudo mknod --mode 666 /var/lib/vz/private/777/dev/ptmx c 5 2&lt;br /&gt;
&lt;br /&gt;
=== Starting VE ===&lt;br /&gt;
Now start the VE:&lt;br /&gt;
 sudo vzctl start 777&lt;br /&gt;
&lt;br /&gt;
== Customizing the installation ==&lt;br /&gt;
A few things need to be done inside a newly created VE for it to become suitable for OpenVZ. Enter the VE to begin the configuration. Exporting the path is optional.&lt;br /&gt;
 sudo vzctl enter 777&lt;br /&gt;
 export PATH=/sbin:/usr/sbin:/bin:/usr/bin&lt;br /&gt;
&lt;br /&gt;
{{Warning|Do not run the commands below on the hardware node, they are only to be run within the VE!}}&lt;br /&gt;
&lt;br /&gt;
=== Set Debian repositories ===&lt;br /&gt;
 cat &amp;lt;&amp;lt;EOF &amp;gt; /etc/apt/sources.list&lt;br /&gt;
 deb http://http.us.debian.org/debian lenny main contrib&lt;br /&gt;
 deb http://security.debian.org lenny/updates main contrib&lt;br /&gt;
 deb http://security.debian.org lenny/volatile main contrib&lt;br /&gt;
 EOF&lt;br /&gt;
&lt;br /&gt;
=== Get new security updates ===&lt;br /&gt;
 apt-get update&lt;br /&gt;
 apt-get upgrade&lt;br /&gt;
&lt;br /&gt;
=== Install some more packages ===&lt;br /&gt;
Installing packages could be an interactive process so the system might ask some questions.  You can install more packages if you'd like.  For example:&lt;br /&gt;
 apt-get install ssh quota less&lt;br /&gt;
&lt;br /&gt;
=== Set sane permissions for &amp;lt;tt&amp;gt;/root&amp;lt;/tt&amp;gt; directory ===&lt;br /&gt;
 chmod 700 /root&lt;br /&gt;
&lt;br /&gt;
=== Disable root login ===&lt;br /&gt;
This will disable root login by default.&lt;br /&gt;
 usermod -L root&lt;br /&gt;
&lt;br /&gt;
=== Disable getty ===&lt;br /&gt;
Disable running &amp;lt;tt&amp;gt;getty&amp;lt;/tt&amp;gt;s on terminals as a VE does not have any:&lt;br /&gt;
 sed -i -e '/getty/d' /etc/inittab&lt;br /&gt;
&lt;br /&gt;
=== Disable &amp;lt;tt&amp;gt;sync()&amp;lt;/tt&amp;gt; for syslog ===&lt;br /&gt;
Turn off doing &amp;lt;tt&amp;gt;sync()&amp;lt;/tt&amp;gt; on every write for &amp;lt;tt&amp;gt;syslog&amp;lt;/tt&amp;gt;'s log files, to improve I/O performance:&lt;br /&gt;
 &amp;lt;pre&amp;gt;sed -i -e 's@\([[:space:]]\)\(/var/log/\)@\1-\2@' /etc/*syslog.conf&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Fix &amp;lt;tt&amp;gt;/etc/mtab&amp;lt;/tt&amp;gt; ===&lt;br /&gt;
Link &amp;lt;tt&amp;gt;/etc/mtab&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;/proc/mounts&amp;lt;/tt&amp;gt;, so &amp;lt;tt&amp;gt;df&amp;lt;/tt&amp;gt; and friends will work:&lt;br /&gt;
 rm -f /etc/mtab&lt;br /&gt;
 ln -s /proc/mounts /etc/mtab&lt;br /&gt;
&lt;br /&gt;
=== Remove some unneeded packages ===&lt;br /&gt;
If you have any packages you'd like to remove, now's the time for it.  Here's an example:&lt;br /&gt;
 dpkg --purge modutils ppp pppoeconf pppoe pppconfig&lt;br /&gt;
&lt;br /&gt;
=== Disable services ===&lt;br /&gt;
Do not start some services, stick to bare minimum:&lt;br /&gt;
 update-rc.d -f klogd remove&lt;br /&gt;
 update-rc.d -f quotarpc remove&lt;br /&gt;
 update-rc.d -f exim4 remove&lt;br /&gt;
 update-rc.d -f inetd remove&lt;br /&gt;
&lt;br /&gt;
=== Fix SSH host keys ===&lt;br /&gt;
This is only useful if you installed SSH.  Each individual [[VE]] should have its own pair of SSH host keys.  The code below will wipe out the existing SSH keys and instruct the newly-created [[VE]] to create new SSH keys on first boot.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- please do not remove &amp;lt;source&amp;gt;...&amp;lt;/source&amp;gt; pair of tags below,&lt;br /&gt;
     otherwise quotes after -N (-N '') are not visible --&amp;gt;&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
rm -f /etc/ssh/ssh_host_*&lt;br /&gt;
cat &amp;lt;&amp;lt; EOF &amp;gt; /etc/rc2.d/S15ssh_gen_host_keys&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
ssh-keygen -f /etc/ssh/ssh_host_rsa_key -t rsa -N ''&lt;br /&gt;
ssh-keygen -f /etc/ssh/ssh_host_dsa_key -t dsa -N ''&lt;br /&gt;
rm -f \$0&lt;br /&gt;
EOF&lt;br /&gt;
chmod a+x /etc/rc2.d/S15ssh_gen_host_keys&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Note|This will not work using the dependency-based boot sequence introduced with Squeeze...}}&lt;br /&gt;
&lt;br /&gt;
=== Change timezone ===&lt;br /&gt;
&lt;br /&gt;
You might want to change timezone if you do not live in $UTC. The following example is for Germany&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ln -sf /usr/share/zoneinfo/Europe/Berlin /etc/localtime&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
or even better&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
dpkg-reconfigure tzdata&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Clean packages ===&lt;br /&gt;
After installing packages, you'll have some junk packages laying around in your cache.  Since you don't want your template to have those, this command will wipe them out.&lt;br /&gt;
 apt-get --purge clean&lt;br /&gt;
&lt;br /&gt;
Now everything is done.  Exit from the template and go back to the hardware node.&lt;br /&gt;
 exit&lt;br /&gt;
&lt;br /&gt;
== Preparing for and packing template cache ==&lt;br /&gt;
&lt;br /&gt;
We don't need an IP for the VE anymore, and we definitely do not need it in template cache, so remove it:&lt;br /&gt;
 sudo vzctl set 777 --ipdel all --save&lt;br /&gt;
&lt;br /&gt;
Also, remove DNS server and search domain information from ''/etc/resolv.conf'' file '''in VE''':&lt;br /&gt;
 sudo nano /vz/private/777/etc/resolv.conf&lt;br /&gt;
&lt;br /&gt;
Also, remove ''/etc/hostname'' file '''in VE''':&lt;br /&gt;
 sudo rm -f /vz/private/777/etc/hostname&lt;br /&gt;
&lt;br /&gt;
Stop the VE:&lt;br /&gt;
 sudo vzctl stop 777&lt;br /&gt;
&lt;br /&gt;
Go to the VE directory:&lt;br /&gt;
 cd /vz/private/777&lt;br /&gt;
&lt;br /&gt;
Now create a cached OS tarball.  In the command below, you'll want to replace &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; with your architecture (i386, amd64, ia64, etc).&lt;br /&gt;
 sudo tar --numeric-owner -zcf /vz/template/cache/debian-4.0-i386-minimal.tar.gz .&lt;br /&gt;
&lt;br /&gt;
Look at the resulting tarball to see its size is sane:&lt;br /&gt;
 # ls -lh /vz/template/cache&lt;br /&gt;
 -rw-r--r--  1 root root  51M Apr 10 03:16 debian-4.0-i386-minimal.tar.gz&lt;br /&gt;
&lt;br /&gt;
== Checking if template cache works ==&lt;br /&gt;
We can now create a VE based on the just-created template cache.  Be sure to change &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; to your architecture just like you did when you named the tarball above.&lt;br /&gt;
 sudo vzctl create 123456 --ostemplate debian-4.0-i386-minimal&lt;br /&gt;
&lt;br /&gt;
Now make sure that it works:&lt;br /&gt;
 sudo vzctl start 123456&lt;br /&gt;
 sudo vzctl exec 123456 ps ax&lt;br /&gt;
&lt;br /&gt;
You should see that a few processes are running.&lt;br /&gt;
&lt;br /&gt;
== Final cleanup ==&lt;br /&gt;
Stop and remove the test VE you just created:&lt;br /&gt;
 sudo vzctl stop 123456&lt;br /&gt;
 sudo vzctl destroy 123456&lt;br /&gt;
 sudo rm /etc/vz/conf/123456.conf.destroyed&lt;br /&gt;
&lt;br /&gt;
Finally, let's remove the VE we used for OS template cache creation:&lt;br /&gt;
 sudo vzctl destroy 777&lt;br /&gt;
 sudo rm /etc/vz/conf/777.conf.destroyed&lt;br /&gt;
&lt;br /&gt;
[[Category: HOWTO]]&lt;br /&gt;
[[Category: Templates]]&lt;br /&gt;
[[Category: Debian]]&lt;/div&gt;</summary>
		<author><name>Mose</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Debian_template_creation&amp;diff=9188</id>
		<title>Debian template creation</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Debian_template_creation&amp;diff=9188"/>
		<updated>2010-09-30T13:59:45Z</updated>

		<summary type="html">&lt;p&gt;Mose: /* Customizing the installation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;These are rough instructions of how to manually create basic Debian Etch (4.0) template cache, which can be used to create OpenVZ [[VE]]s based on Debian Etch (4.0). (see also &amp;lt;tt&amp;gt;/usr/share/doc/vzctl/README.Debian&amp;lt;/tt&amp;gt; in the ''vzctl'' Debian package)&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* You shouldn't be running as root, but as a user that is permitted to use sudo instead.  It's a dangerous idea, run as root at your peril.&lt;br /&gt;
* Anywhere you see &amp;lt;tt&amp;gt;/vz&amp;lt;/tt&amp;gt;, you might really need to use &amp;lt;tt&amp;gt;/var/lib/vz&amp;lt;/tt&amp;gt; instead, especially on a Debian Etch host.&lt;br /&gt;
* Anywhere you see &amp;lt;tt&amp;gt;http://http.us.debian.org/debian/&amp;lt;/tt&amp;gt;, you can substitute your favorite Debian mirror.  ([http://www.debian.org/mirror/list List of official Debian Mirrors])&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Prerequisites ==&lt;br /&gt;
You need to have a working copy of &amp;lt;tt&amp;gt;debootstrap&amp;lt;/tt&amp;gt; running on your hardware node.&lt;br /&gt;
&lt;br /&gt;
For Debian:&lt;br /&gt;
 sudo apt-get install debootstrap&lt;br /&gt;
&lt;br /&gt;
For Gentoo:&lt;br /&gt;
 sudo emerge debootstrap&lt;br /&gt;
&lt;br /&gt;
For Fedora (at least Fedora 8 have it, not sure about earlier versions):&lt;br /&gt;
 sudo yum install debootstrap&lt;br /&gt;
&lt;br /&gt;
For other distros you might need to install it from sources, or search for an appropriate package for your distribution.  An RPM is available on the [http://forum.openvz.org/index.php?t=tree&amp;amp;th=142&amp;amp;mid=584 OpenVZ Forum].&lt;br /&gt;
&lt;br /&gt;
== Bootstrapping Debian ==&lt;br /&gt;
&lt;br /&gt;
You can install different releases of Debian into a VE's private directory using the debootstrap command.&lt;br /&gt;
&lt;br /&gt;
The command parameters are:&lt;br /&gt;
&lt;br /&gt;
  debootstrap --arch ARCH NAME DIRECTORY URL&lt;br /&gt;
&lt;br /&gt;
Specify your architecture instead of &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; if you're using something other than i386/x86.  For example, for AMD64/x86_64, use &amp;lt;tt&amp;gt;amd64&amp;lt;/tt&amp;gt; or for ia64, use &amp;lt;tt&amp;gt;ia64&amp;lt;/tt&amp;gt;. You can use http or ftp in the URL.&lt;br /&gt;
&lt;br /&gt;
We use VE ID of 777 for this example, but it can be any unused ID.&lt;br /&gt;
&lt;br /&gt;
=== Lenny (current Debian stable) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap --arch i386 lenny /vz/private/777 http://http.us.debian.org/debian/&lt;br /&gt;
 or&lt;br /&gt;
 debootstrap --arch amd64 lenny /vz/private/777 ftp://ftp.us.debian.org/debian/&lt;br /&gt;
&lt;br /&gt;
=== Etch (old release) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap --arch i386 etch /vz/private/777 http://http.us.debian.org/debian/&lt;br /&gt;
&lt;br /&gt;
=== Sarge (very old release) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap sarge /vz/private/777 http://archive.debian.org/debian&lt;br /&gt;
&lt;br /&gt;
== Preparing the HN network ==&lt;br /&gt;
Append the following lines to /etc/sysctl.conf, adjust to taste and then execute &amp;quot;sysctl -p&amp;quot; for them to take effect.&lt;br /&gt;
 ### OpenVZ settings&lt;br /&gt;
 &lt;br /&gt;
 # On Hardware Node we generally need packet&lt;br /&gt;
 # forwarding enabled and proxy arp disabled&lt;br /&gt;
 &lt;br /&gt;
 net.ipv4.conf.default.forwarding=1&lt;br /&gt;
 net.ipv4.conf.default.proxy_arp = 0&lt;br /&gt;
 net.ipv4.ip_forward=1&lt;br /&gt;
 &lt;br /&gt;
 # Enables source route verification&lt;br /&gt;
 net.ipv4.conf.all.rp_filter = 1&lt;br /&gt;
 &lt;br /&gt;
 # Enables the magic-sysrq key&lt;br /&gt;
 kernel.sysrq = 1&lt;br /&gt;
 &lt;br /&gt;
 # TCP Explict Congestion Notification&lt;br /&gt;
 net.ipv4.tcp_ecn = 0&lt;br /&gt;
 &lt;br /&gt;
 # we do not want all our interfaces to send redirects&lt;br /&gt;
 net.ipv4.conf.default.send_redirects = 1&lt;br /&gt;
 net.ipv4.conf.all.send_redirects = 0&lt;br /&gt;
&lt;br /&gt;
== Preparing and starting the VE ==&lt;br /&gt;
&lt;br /&gt;
=== Setting VE config ===&lt;br /&gt;
First, we need a config for the [[VE]]:&lt;br /&gt;
 sudo vzctl set 777 --applyconfig vps.basic --save&lt;br /&gt;
&lt;br /&gt;
=== Setting VE OSTEMPLATE ===&lt;br /&gt;
Also, we need &amp;lt;tt&amp;gt;OSTEMPLATE&amp;lt;/tt&amp;gt; to be set in VE configuration file, for the [[vzctl]] to work properly.&lt;br /&gt;
&lt;br /&gt;
 sudo sh -c 'echo &amp;quot;OSTEMPLATE=debian-4.0&amp;quot; &amp;gt;&amp;gt; /etc/vz/conf/777.conf'&lt;br /&gt;
&lt;br /&gt;
=== Setting VE IP address ===&lt;br /&gt;
For the [[VE]] to be able to download updates from the Internet, we need a valid IP address for it:&lt;br /&gt;
 sudo vzctl set 777 --ipadd x.x.x.x --save&lt;br /&gt;
&lt;br /&gt;
{{Note|if you use private IP for the VE, you might have to set up NAT as described in [[Using NAT for VE with private IPs]].}}&lt;br /&gt;
&lt;br /&gt;
=== Setting DNS server for VE ===&lt;br /&gt;
For the [[VE]] to be able to download updates from the Internet, we also need to specify a DNS for it:&lt;br /&gt;
 sudo vzctl set 777 --nameserver x.x.x.x --save&lt;br /&gt;
&lt;br /&gt;
=== Creating /dev/ptmx  ===&lt;br /&gt;
The ptmx character device should normally exist, but if it doesn't, create one.&lt;br /&gt;
 sudo mknod --mode 666 /var/lib/vz/private/777/dev/ptmx c 5 2&lt;br /&gt;
&lt;br /&gt;
=== Starting VE ===&lt;br /&gt;
Now start the VE:&lt;br /&gt;
 sudo vzctl start 777&lt;br /&gt;
&lt;br /&gt;
== Customizing the installation ==&lt;br /&gt;
A few things need to be done inside a newly created VE for it to become suitable for OpenVZ. Enter the VE to begin the configuration. Exporting the path is optional.&lt;br /&gt;
 sudo vzctl enter 777&lt;br /&gt;
 export PATH=/sbin:/usr/sbin:/bin:/usr/bin&lt;br /&gt;
&lt;br /&gt;
{{Warning|Do not run the commands below on the hardware node, they are only to be run within the VE!}}&lt;br /&gt;
&lt;br /&gt;
=== Set Debian repositories ===&lt;br /&gt;
 cat &amp;lt;&amp;lt;EOF &amp;gt; /etc/apt/sources.list&lt;br /&gt;
 deb http://http.us.debian.org/debian lenny main contrib&lt;br /&gt;
 deb http://security.debian.org lenny/updates main contrib&lt;br /&gt;
 deb http://security.debian.org lenny/volatile main contrib&lt;br /&gt;
 EOF&lt;br /&gt;
&lt;br /&gt;
=== Get new security updates ===&lt;br /&gt;
 apt-get update&lt;br /&gt;
 apt-get upgrade&lt;br /&gt;
&lt;br /&gt;
=== Install some more packages ===&lt;br /&gt;
Installing packages could be an interactive process so the system might ask some questions.  You can install more packages if you'd like.  For example:&lt;br /&gt;
 apt-get install ssh quota less&lt;br /&gt;
&lt;br /&gt;
=== Set sane permissions for &amp;lt;tt&amp;gt;/root&amp;lt;/tt&amp;gt; directory ===&lt;br /&gt;
 chmod 700 /root&lt;br /&gt;
&lt;br /&gt;
=== Disable root login ===&lt;br /&gt;
This will disable root login by default.&lt;br /&gt;
 usermod -L root&lt;br /&gt;
&lt;br /&gt;
=== Disable getty ===&lt;br /&gt;
Disable running &amp;lt;tt&amp;gt;getty&amp;lt;/tt&amp;gt;s on terminals as a VE does not have any:&lt;br /&gt;
 sed -i -e '/getty/d' /etc/inittab&lt;br /&gt;
&lt;br /&gt;
=== Disable &amp;lt;tt&amp;gt;sync()&amp;lt;/tt&amp;gt; for syslog ===&lt;br /&gt;
Turn off doing &amp;lt;tt&amp;gt;sync()&amp;lt;/tt&amp;gt; on every write for &amp;lt;tt&amp;gt;syslog&amp;lt;/tt&amp;gt;'s log files, to improve I/O performance:&lt;br /&gt;
 &amp;lt;pre&amp;gt;sed -i -e 's@\([[:space:]]\)\(/var/log/\)@\1-\2@' /etc/*syslog.conf&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Fix &amp;lt;tt&amp;gt;/etc/mtab&amp;lt;/tt&amp;gt; ===&lt;br /&gt;
Link &amp;lt;tt&amp;gt;/etc/mtab&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;/proc/mounts&amp;lt;/tt&amp;gt;, so &amp;lt;tt&amp;gt;df&amp;lt;/tt&amp;gt; and friends will work:&lt;br /&gt;
 rm -f /etc/mtab&lt;br /&gt;
 ln -s /proc/mounts /etc/mtab&lt;br /&gt;
&lt;br /&gt;
=== Remove some unneeded packages ===&lt;br /&gt;
If you have any packages you'd like to remove, now's the time for it.  Here's an example:&lt;br /&gt;
 dpkg --purge modutils ppp pppoeconf pppoe pppconfig&lt;br /&gt;
&lt;br /&gt;
=== Disable services ===&lt;br /&gt;
Do not start some services, stick to bare minimum:&lt;br /&gt;
 update-rc.d -f klogd remove&lt;br /&gt;
 update-rc.d -f quotarpc remove&lt;br /&gt;
 update-rc.d -f exim4 remove&lt;br /&gt;
 update-rc.d -f inetd remove&lt;br /&gt;
&lt;br /&gt;
=== Fix SSH host keys ===&lt;br /&gt;
This is only useful if you installed SSH.  Each individual [[VE]] should have its own pair of SSH host keys.  The code below will wipe out the existing SSH keys and instruct the newly-created [[VE]] to create new SSH keys on first boot.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- please do not remove &amp;lt;source&amp;gt;...&amp;lt;/source&amp;gt; pair of tags below,&lt;br /&gt;
     otherwise quotes after -N (-N '') are not visible --&amp;gt;&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
rm -f /etc/ssh/ssh_host_*&lt;br /&gt;
cat &amp;lt;&amp;lt; EOF &amp;gt; /etc/rc2.d/S15ssh_gen_host_keys&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
ssh-keygen -f /etc/ssh/ssh_host_rsa_key -t rsa -N ''&lt;br /&gt;
ssh-keygen -f /etc/ssh/ssh_host_dsa_key -t dsa -N ''&lt;br /&gt;
rm -f \$0&lt;br /&gt;
EOF&lt;br /&gt;
chmod a+x /etc/rc2.d/S15ssh_gen_host_keys&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Note|This will not work using the dependency-based boot sequence introduced with Squeeze...}}&lt;br /&gt;
&lt;br /&gt;
=== Change timezone ===&lt;br /&gt;
&lt;br /&gt;
You might want to change timezone if you do not live in $UTC. The following example is for Germany&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ln -sf /usr/share/zoneinfo/Europe/Berlin /etc/localtime&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
or even better&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
dpkg-reconfigure tzdata&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Clean packages ===&lt;br /&gt;
After installing packages, you'll have some junk packages laying around in your cache.  Since you don't want your template to have those, this command will wipe them out.&lt;br /&gt;
 apt-get clean&lt;br /&gt;
&lt;br /&gt;
Now everything is done.  Exit from the template and go back to the hardware node.&lt;br /&gt;
 exit&lt;br /&gt;
&lt;br /&gt;
== Preparing for and packing template cache ==&lt;br /&gt;
&lt;br /&gt;
We don't need an IP for the VE anymore, and we definitely do not need it in template cache, so remove it:&lt;br /&gt;
 sudo vzctl set 777 --ipdel all --save&lt;br /&gt;
&lt;br /&gt;
Also, remove DNS server and search domain information from ''/etc/resolv.conf'' file '''in VE''':&lt;br /&gt;
 sudo nano /vz/private/777/etc/resolv.conf&lt;br /&gt;
&lt;br /&gt;
Also, remove ''/etc/hostname'' file '''in VE''':&lt;br /&gt;
 sudo rm -f /vz/private/777/etc/hostname&lt;br /&gt;
&lt;br /&gt;
Stop the VE:&lt;br /&gt;
 sudo vzctl stop 777&lt;br /&gt;
&lt;br /&gt;
Go to the VE directory:&lt;br /&gt;
 cd /vz/private/777&lt;br /&gt;
&lt;br /&gt;
Now create a cached OS tarball.  In the command below, you'll want to replace &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; with your architecture (i386, amd64, ia64, etc).&lt;br /&gt;
 sudo tar --numeric-owner -zcf /vz/template/cache/debian-4.0-i386-minimal.tar.gz .&lt;br /&gt;
&lt;br /&gt;
Look at the resulting tarball to see its size is sane:&lt;br /&gt;
 # ls -lh /vz/template/cache&lt;br /&gt;
 -rw-r--r--  1 root root  51M Apr 10 03:16 debian-4.0-i386-minimal.tar.gz&lt;br /&gt;
&lt;br /&gt;
== Checking if template cache works ==&lt;br /&gt;
We can now create a VE based on the just-created template cache.  Be sure to change &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; to your architecture just like you did when you named the tarball above.&lt;br /&gt;
 sudo vzctl create 123456 --ostemplate debian-4.0-i386-minimal&lt;br /&gt;
&lt;br /&gt;
Now make sure that it works:&lt;br /&gt;
 sudo vzctl start 123456&lt;br /&gt;
 sudo vzctl exec 123456 ps ax&lt;br /&gt;
&lt;br /&gt;
You should see that a few processes are running.&lt;br /&gt;
&lt;br /&gt;
== Final cleanup ==&lt;br /&gt;
Stop and remove the test VE you just created:&lt;br /&gt;
 sudo vzctl stop 123456&lt;br /&gt;
 sudo vzctl destroy 123456&lt;br /&gt;
 sudo rm /etc/vz/conf/123456.conf.destroyed&lt;br /&gt;
&lt;br /&gt;
Finally, let's remove the VE we used for OS template cache creation:&lt;br /&gt;
 sudo vzctl destroy 777&lt;br /&gt;
 sudo rm /etc/vz/conf/777.conf.destroyed&lt;br /&gt;
&lt;br /&gt;
[[Category: HOWTO]]&lt;br /&gt;
[[Category: Templates]]&lt;br /&gt;
[[Category: Debian]]&lt;/div&gt;</summary>
		<author><name>Mose</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Debian_template_creation&amp;diff=9187</id>
		<title>Debian template creation</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Debian_template_creation&amp;diff=9187"/>
		<updated>2010-09-30T13:57:18Z</updated>

		<summary type="html">&lt;p&gt;Mose: /* Set Debian repositories */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;These are rough instructions of how to manually create basic Debian Etch (4.0) template cache, which can be used to create OpenVZ [[VE]]s based on Debian Etch (4.0). (see also &amp;lt;tt&amp;gt;/usr/share/doc/vzctl/README.Debian&amp;lt;/tt&amp;gt; in the ''vzctl'' Debian package)&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* You shouldn't be running as root, but as a user that is permitted to use sudo instead.  It's a dangerous idea, run as root at your peril.&lt;br /&gt;
* Anywhere you see &amp;lt;tt&amp;gt;/vz&amp;lt;/tt&amp;gt;, you might really need to use &amp;lt;tt&amp;gt;/var/lib/vz&amp;lt;/tt&amp;gt; instead, especially on a Debian Etch host.&lt;br /&gt;
* Anywhere you see &amp;lt;tt&amp;gt;http://http.us.debian.org/debian/&amp;lt;/tt&amp;gt;, you can substitute your favorite Debian mirror.  ([http://www.debian.org/mirror/list List of official Debian Mirrors])&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Prerequisites ==&lt;br /&gt;
You need to have a working copy of &amp;lt;tt&amp;gt;debootstrap&amp;lt;/tt&amp;gt; running on your hardware node.&lt;br /&gt;
&lt;br /&gt;
For Debian:&lt;br /&gt;
 sudo apt-get install debootstrap&lt;br /&gt;
&lt;br /&gt;
For Gentoo:&lt;br /&gt;
 sudo emerge debootstrap&lt;br /&gt;
&lt;br /&gt;
For Fedora (at least Fedora 8 have it, not sure about earlier versions):&lt;br /&gt;
 sudo yum install debootstrap&lt;br /&gt;
&lt;br /&gt;
For other distros you might need to install it from sources, or search for an appropriate package for your distribution.  An RPM is available on the [http://forum.openvz.org/index.php?t=tree&amp;amp;th=142&amp;amp;mid=584 OpenVZ Forum].&lt;br /&gt;
&lt;br /&gt;
== Bootstrapping Debian ==&lt;br /&gt;
&lt;br /&gt;
You can install different releases of Debian into a VE's private directory using the debootstrap command.&lt;br /&gt;
&lt;br /&gt;
The command parameters are:&lt;br /&gt;
&lt;br /&gt;
  debootstrap --arch ARCH NAME DIRECTORY URL&lt;br /&gt;
&lt;br /&gt;
Specify your architecture instead of &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; if you're using something other than i386/x86.  For example, for AMD64/x86_64, use &amp;lt;tt&amp;gt;amd64&amp;lt;/tt&amp;gt; or for ia64, use &amp;lt;tt&amp;gt;ia64&amp;lt;/tt&amp;gt;. You can use http or ftp in the URL.&lt;br /&gt;
&lt;br /&gt;
We use VE ID of 777 for this example, but it can be any unused ID.&lt;br /&gt;
&lt;br /&gt;
=== Lenny (current Debian stable) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap --arch i386 lenny /vz/private/777 http://http.us.debian.org/debian/&lt;br /&gt;
 or&lt;br /&gt;
 debootstrap --arch amd64 lenny /vz/private/777 ftp://ftp.us.debian.org/debian/&lt;br /&gt;
&lt;br /&gt;
=== Etch (old release) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap --arch i386 etch /vz/private/777 http://http.us.debian.org/debian/&lt;br /&gt;
&lt;br /&gt;
=== Sarge (very old release) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap sarge /vz/private/777 http://archive.debian.org/debian&lt;br /&gt;
&lt;br /&gt;
== Preparing the HN network ==&lt;br /&gt;
Append the following lines to /etc/sysctl.conf, adjust to taste and then execute &amp;quot;sysctl -p&amp;quot; for them to take effect.&lt;br /&gt;
 ### OpenVZ settings&lt;br /&gt;
 &lt;br /&gt;
 # On Hardware Node we generally need packet&lt;br /&gt;
 # forwarding enabled and proxy arp disabled&lt;br /&gt;
 &lt;br /&gt;
 net.ipv4.conf.default.forwarding=1&lt;br /&gt;
 net.ipv4.conf.default.proxy_arp = 0&lt;br /&gt;
 net.ipv4.ip_forward=1&lt;br /&gt;
 &lt;br /&gt;
 # Enables source route verification&lt;br /&gt;
 net.ipv4.conf.all.rp_filter = 1&lt;br /&gt;
 &lt;br /&gt;
 # Enables the magic-sysrq key&lt;br /&gt;
 kernel.sysrq = 1&lt;br /&gt;
 &lt;br /&gt;
 # TCP Explict Congestion Notification&lt;br /&gt;
 net.ipv4.tcp_ecn = 0&lt;br /&gt;
 &lt;br /&gt;
 # we do not want all our interfaces to send redirects&lt;br /&gt;
 net.ipv4.conf.default.send_redirects = 1&lt;br /&gt;
 net.ipv4.conf.all.send_redirects = 0&lt;br /&gt;
&lt;br /&gt;
== Preparing and starting the VE ==&lt;br /&gt;
&lt;br /&gt;
=== Setting VE config ===&lt;br /&gt;
First, we need a config for the [[VE]]:&lt;br /&gt;
 sudo vzctl set 777 --applyconfig vps.basic --save&lt;br /&gt;
&lt;br /&gt;
=== Setting VE OSTEMPLATE ===&lt;br /&gt;
Also, we need &amp;lt;tt&amp;gt;OSTEMPLATE&amp;lt;/tt&amp;gt; to be set in VE configuration file, for the [[vzctl]] to work properly.&lt;br /&gt;
&lt;br /&gt;
 sudo sh -c 'echo &amp;quot;OSTEMPLATE=debian-4.0&amp;quot; &amp;gt;&amp;gt; /etc/vz/conf/777.conf'&lt;br /&gt;
&lt;br /&gt;
=== Setting VE IP address ===&lt;br /&gt;
For the [[VE]] to be able to download updates from the Internet, we need a valid IP address for it:&lt;br /&gt;
 sudo vzctl set 777 --ipadd x.x.x.x --save&lt;br /&gt;
&lt;br /&gt;
{{Note|if you use private IP for the VE, you might have to set up NAT as described in [[Using NAT for VE with private IPs]].}}&lt;br /&gt;
&lt;br /&gt;
=== Setting DNS server for VE ===&lt;br /&gt;
For the [[VE]] to be able to download updates from the Internet, we also need to specify a DNS for it:&lt;br /&gt;
 sudo vzctl set 777 --nameserver x.x.x.x --save&lt;br /&gt;
&lt;br /&gt;
=== Creating /dev/ptmx  ===&lt;br /&gt;
The ptmx character device should normally exist, but if it doesn't, create one.&lt;br /&gt;
 sudo mknod --mode 666 /var/lib/vz/private/777/dev/ptmx c 5 2&lt;br /&gt;
&lt;br /&gt;
=== Starting VE ===&lt;br /&gt;
Now start the VE:&lt;br /&gt;
 sudo vzctl start 777&lt;br /&gt;
&lt;br /&gt;
== Customizing the installation ==&lt;br /&gt;
A few things need to be done inside a newly created VE for it to become suitable for OpenVZ.  All those things are done inside the VE, so first command is:&lt;br /&gt;
 sudo vzctl enter 777&lt;br /&gt;
 export PATH=/sbin:/usr/sbin:/bin:/usr/bin&lt;br /&gt;
&lt;br /&gt;
{{Warning|Do not run the commands below on the hardware node, they are only to be run within the VE!}}&lt;br /&gt;
&lt;br /&gt;
=== Set Debian repositories ===&lt;br /&gt;
 cat &amp;lt;&amp;lt;EOF &amp;gt; /etc/apt/sources.list&lt;br /&gt;
 deb http://http.us.debian.org/debian lenny main contrib&lt;br /&gt;
 deb http://security.debian.org lenny/updates main contrib&lt;br /&gt;
 deb http://security.debian.org lenny/volatile main contrib&lt;br /&gt;
 EOF&lt;br /&gt;
&lt;br /&gt;
=== Get new security updates ===&lt;br /&gt;
 apt-get update&lt;br /&gt;
 apt-get upgrade&lt;br /&gt;
&lt;br /&gt;
=== Install some more packages ===&lt;br /&gt;
Installing packages could be an interactive process so the system might ask some questions.  You can install more packages if you'd like.  For example:&lt;br /&gt;
 apt-get install ssh quota less&lt;br /&gt;
&lt;br /&gt;
=== Set sane permissions for &amp;lt;tt&amp;gt;/root&amp;lt;/tt&amp;gt; directory ===&lt;br /&gt;
 chmod 700 /root&lt;br /&gt;
&lt;br /&gt;
=== Disable root login ===&lt;br /&gt;
This will disable root login by default.&lt;br /&gt;
 usermod -L root&lt;br /&gt;
&lt;br /&gt;
=== Disable getty ===&lt;br /&gt;
Disable running &amp;lt;tt&amp;gt;getty&amp;lt;/tt&amp;gt;s on terminals as a VE does not have any:&lt;br /&gt;
 sed -i -e '/getty/d' /etc/inittab&lt;br /&gt;
&lt;br /&gt;
=== Disable &amp;lt;tt&amp;gt;sync()&amp;lt;/tt&amp;gt; for syslog ===&lt;br /&gt;
Turn off doing &amp;lt;tt&amp;gt;sync()&amp;lt;/tt&amp;gt; on every write for &amp;lt;tt&amp;gt;syslog&amp;lt;/tt&amp;gt;'s log files, to improve I/O performance:&lt;br /&gt;
 &amp;lt;pre&amp;gt;sed -i -e 's@\([[:space:]]\)\(/var/log/\)@\1-\2@' /etc/*syslog.conf&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Fix &amp;lt;tt&amp;gt;/etc/mtab&amp;lt;/tt&amp;gt; ===&lt;br /&gt;
Link &amp;lt;tt&amp;gt;/etc/mtab&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;/proc/mounts&amp;lt;/tt&amp;gt;, so &amp;lt;tt&amp;gt;df&amp;lt;/tt&amp;gt; and friends will work:&lt;br /&gt;
 rm -f /etc/mtab&lt;br /&gt;
 ln -s /proc/mounts /etc/mtab&lt;br /&gt;
&lt;br /&gt;
=== Remove some unneeded packages ===&lt;br /&gt;
If you have any packages you'd like to remove, now's the time for it.  Here's an example:&lt;br /&gt;
 dpkg --purge modutils ppp pppoeconf pppoe pppconfig&lt;br /&gt;
&lt;br /&gt;
=== Disable services ===&lt;br /&gt;
Do not start some services, stick to bare minimum:&lt;br /&gt;
 update-rc.d -f klogd remove&lt;br /&gt;
 update-rc.d -f quotarpc remove&lt;br /&gt;
 update-rc.d -f exim4 remove&lt;br /&gt;
 update-rc.d -f inetd remove&lt;br /&gt;
&lt;br /&gt;
=== Fix SSH host keys ===&lt;br /&gt;
This is only useful if you installed SSH.  Each individual [[VE]] should have its own pair of SSH host keys.  The code below will wipe out the existing SSH keys and instruct the newly-created [[VE]] to create new SSH keys on first boot.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- please do not remove &amp;lt;source&amp;gt;...&amp;lt;/source&amp;gt; pair of tags below,&lt;br /&gt;
     otherwise quotes after -N (-N '') are not visible --&amp;gt;&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
rm -f /etc/ssh/ssh_host_*&lt;br /&gt;
cat &amp;lt;&amp;lt; EOF &amp;gt; /etc/rc2.d/S15ssh_gen_host_keys&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
ssh-keygen -f /etc/ssh/ssh_host_rsa_key -t rsa -N ''&lt;br /&gt;
ssh-keygen -f /etc/ssh/ssh_host_dsa_key -t dsa -N ''&lt;br /&gt;
rm -f \$0&lt;br /&gt;
EOF&lt;br /&gt;
chmod a+x /etc/rc2.d/S15ssh_gen_host_keys&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Note|This will not work using the dependency-based boot sequence introduced with Squeeze...}}&lt;br /&gt;
&lt;br /&gt;
=== Change timezone ===&lt;br /&gt;
&lt;br /&gt;
You might want to change timezone if you do not live in $UTC. The following example is for Germany&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ln -sf /usr/share/zoneinfo/Europe/Berlin /etc/localtime&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
or even better&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
dpkg-reconfigure tzdata&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Clean packages ===&lt;br /&gt;
After installing packages, you'll have some junk packages laying around in your cache.  Since you don't want your template to have those, this command will wipe them out.&lt;br /&gt;
 apt-get clean&lt;br /&gt;
&lt;br /&gt;
Now everything is done.  Exit from the template and go back to the hardware node.&lt;br /&gt;
 exit&lt;br /&gt;
&lt;br /&gt;
== Preparing for and packing template cache ==&lt;br /&gt;
&lt;br /&gt;
We don't need an IP for the VE anymore, and we definitely do not need it in template cache, so remove it:&lt;br /&gt;
 sudo vzctl set 777 --ipdel all --save&lt;br /&gt;
&lt;br /&gt;
Also, remove DNS server and search domain information from ''/etc/resolv.conf'' file '''in VE''':&lt;br /&gt;
 sudo nano /vz/private/777/etc/resolv.conf&lt;br /&gt;
&lt;br /&gt;
Also, remove ''/etc/hostname'' file '''in VE''':&lt;br /&gt;
 sudo rm -f /vz/private/777/etc/hostname&lt;br /&gt;
&lt;br /&gt;
Stop the VE:&lt;br /&gt;
 sudo vzctl stop 777&lt;br /&gt;
&lt;br /&gt;
Go to the VE directory:&lt;br /&gt;
 cd /vz/private/777&lt;br /&gt;
&lt;br /&gt;
Now create a cached OS tarball.  In the command below, you'll want to replace &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; with your architecture (i386, amd64, ia64, etc).&lt;br /&gt;
 sudo tar --numeric-owner -zcf /vz/template/cache/debian-4.0-i386-minimal.tar.gz .&lt;br /&gt;
&lt;br /&gt;
Look at the resulting tarball to see its size is sane:&lt;br /&gt;
 # ls -lh /vz/template/cache&lt;br /&gt;
 -rw-r--r--  1 root root  51M Apr 10 03:16 debian-4.0-i386-minimal.tar.gz&lt;br /&gt;
&lt;br /&gt;
== Checking if template cache works ==&lt;br /&gt;
We can now create a VE based on the just-created template cache.  Be sure to change &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; to your architecture just like you did when you named the tarball above.&lt;br /&gt;
 sudo vzctl create 123456 --ostemplate debian-4.0-i386-minimal&lt;br /&gt;
&lt;br /&gt;
Now make sure that it works:&lt;br /&gt;
 sudo vzctl start 123456&lt;br /&gt;
 sudo vzctl exec 123456 ps ax&lt;br /&gt;
&lt;br /&gt;
You should see that a few processes are running.&lt;br /&gt;
&lt;br /&gt;
== Final cleanup ==&lt;br /&gt;
Stop and remove the test VE you just created:&lt;br /&gt;
 sudo vzctl stop 123456&lt;br /&gt;
 sudo vzctl destroy 123456&lt;br /&gt;
 sudo rm /etc/vz/conf/123456.conf.destroyed&lt;br /&gt;
&lt;br /&gt;
Finally, let's remove the VE we used for OS template cache creation:&lt;br /&gt;
 sudo vzctl destroy 777&lt;br /&gt;
 sudo rm /etc/vz/conf/777.conf.destroyed&lt;br /&gt;
&lt;br /&gt;
[[Category: HOWTO]]&lt;br /&gt;
[[Category: Templates]]&lt;br /&gt;
[[Category: Debian]]&lt;/div&gt;</summary>
		<author><name>Mose</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Debian_template_creation&amp;diff=9186</id>
		<title>Debian template creation</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Debian_template_creation&amp;diff=9186"/>
		<updated>2010-09-30T13:54:11Z</updated>

		<summary type="html">&lt;p&gt;Mose: /* Creating /dev/ptmx */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;These are rough instructions of how to manually create basic Debian Etch (4.0) template cache, which can be used to create OpenVZ [[VE]]s based on Debian Etch (4.0). (see also &amp;lt;tt&amp;gt;/usr/share/doc/vzctl/README.Debian&amp;lt;/tt&amp;gt; in the ''vzctl'' Debian package)&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* You shouldn't be running as root, but as a user that is permitted to use sudo instead.  It's a dangerous idea, run as root at your peril.&lt;br /&gt;
* Anywhere you see &amp;lt;tt&amp;gt;/vz&amp;lt;/tt&amp;gt;, you might really need to use &amp;lt;tt&amp;gt;/var/lib/vz&amp;lt;/tt&amp;gt; instead, especially on a Debian Etch host.&lt;br /&gt;
* Anywhere you see &amp;lt;tt&amp;gt;http://http.us.debian.org/debian/&amp;lt;/tt&amp;gt;, you can substitute your favorite Debian mirror.  ([http://www.debian.org/mirror/list List of official Debian Mirrors])&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Prerequisites ==&lt;br /&gt;
You need to have a working copy of &amp;lt;tt&amp;gt;debootstrap&amp;lt;/tt&amp;gt; running on your hardware node.&lt;br /&gt;
&lt;br /&gt;
For Debian:&lt;br /&gt;
 sudo apt-get install debootstrap&lt;br /&gt;
&lt;br /&gt;
For Gentoo:&lt;br /&gt;
 sudo emerge debootstrap&lt;br /&gt;
&lt;br /&gt;
For Fedora (at least Fedora 8 have it, not sure about earlier versions):&lt;br /&gt;
 sudo yum install debootstrap&lt;br /&gt;
&lt;br /&gt;
For other distros you might need to install it from sources, or search for an appropriate package for your distribution.  An RPM is available on the [http://forum.openvz.org/index.php?t=tree&amp;amp;th=142&amp;amp;mid=584 OpenVZ Forum].&lt;br /&gt;
&lt;br /&gt;
== Bootstrapping Debian ==&lt;br /&gt;
&lt;br /&gt;
You can install different releases of Debian into a VE's private directory using the debootstrap command.&lt;br /&gt;
&lt;br /&gt;
The command parameters are:&lt;br /&gt;
&lt;br /&gt;
  debootstrap --arch ARCH NAME DIRECTORY URL&lt;br /&gt;
&lt;br /&gt;
Specify your architecture instead of &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; if you're using something other than i386/x86.  For example, for AMD64/x86_64, use &amp;lt;tt&amp;gt;amd64&amp;lt;/tt&amp;gt; or for ia64, use &amp;lt;tt&amp;gt;ia64&amp;lt;/tt&amp;gt;. You can use http or ftp in the URL.&lt;br /&gt;
&lt;br /&gt;
We use VE ID of 777 for this example, but it can be any unused ID.&lt;br /&gt;
&lt;br /&gt;
=== Lenny (current Debian stable) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap --arch i386 lenny /vz/private/777 http://http.us.debian.org/debian/&lt;br /&gt;
 or&lt;br /&gt;
 debootstrap --arch amd64 lenny /vz/private/777 ftp://ftp.us.debian.org/debian/&lt;br /&gt;
&lt;br /&gt;
=== Etch (old release) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap --arch i386 etch /vz/private/777 http://http.us.debian.org/debian/&lt;br /&gt;
&lt;br /&gt;
=== Sarge (very old release) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap sarge /vz/private/777 http://archive.debian.org/debian&lt;br /&gt;
&lt;br /&gt;
== Preparing the HN network ==&lt;br /&gt;
Append the following lines to /etc/sysctl.conf, adjust to taste and then execute &amp;quot;sysctl -p&amp;quot; for them to take effect.&lt;br /&gt;
 ### OpenVZ settings&lt;br /&gt;
 &lt;br /&gt;
 # On Hardware Node we generally need packet&lt;br /&gt;
 # forwarding enabled and proxy arp disabled&lt;br /&gt;
 &lt;br /&gt;
 net.ipv4.conf.default.forwarding=1&lt;br /&gt;
 net.ipv4.conf.default.proxy_arp = 0&lt;br /&gt;
 net.ipv4.ip_forward=1&lt;br /&gt;
 &lt;br /&gt;
 # Enables source route verification&lt;br /&gt;
 net.ipv4.conf.all.rp_filter = 1&lt;br /&gt;
 &lt;br /&gt;
 # Enables the magic-sysrq key&lt;br /&gt;
 kernel.sysrq = 1&lt;br /&gt;
 &lt;br /&gt;
 # TCP Explict Congestion Notification&lt;br /&gt;
 net.ipv4.tcp_ecn = 0&lt;br /&gt;
 &lt;br /&gt;
 # we do not want all our interfaces to send redirects&lt;br /&gt;
 net.ipv4.conf.default.send_redirects = 1&lt;br /&gt;
 net.ipv4.conf.all.send_redirects = 0&lt;br /&gt;
&lt;br /&gt;
== Preparing and starting the VE ==&lt;br /&gt;
&lt;br /&gt;
=== Setting VE config ===&lt;br /&gt;
First, we need a config for the [[VE]]:&lt;br /&gt;
 sudo vzctl set 777 --applyconfig vps.basic --save&lt;br /&gt;
&lt;br /&gt;
=== Setting VE OSTEMPLATE ===&lt;br /&gt;
Also, we need &amp;lt;tt&amp;gt;OSTEMPLATE&amp;lt;/tt&amp;gt; to be set in VE configuration file, for the [[vzctl]] to work properly.&lt;br /&gt;
&lt;br /&gt;
 sudo sh -c 'echo &amp;quot;OSTEMPLATE=debian-4.0&amp;quot; &amp;gt;&amp;gt; /etc/vz/conf/777.conf'&lt;br /&gt;
&lt;br /&gt;
=== Setting VE IP address ===&lt;br /&gt;
For the [[VE]] to be able to download updates from the Internet, we need a valid IP address for it:&lt;br /&gt;
 sudo vzctl set 777 --ipadd x.x.x.x --save&lt;br /&gt;
&lt;br /&gt;
{{Note|if you use private IP for the VE, you might have to set up NAT as described in [[Using NAT for VE with private IPs]].}}&lt;br /&gt;
&lt;br /&gt;
=== Setting DNS server for VE ===&lt;br /&gt;
For the [[VE]] to be able to download updates from the Internet, we also need to specify a DNS for it:&lt;br /&gt;
 sudo vzctl set 777 --nameserver x.x.x.x --save&lt;br /&gt;
&lt;br /&gt;
=== Creating /dev/ptmx  ===&lt;br /&gt;
The ptmx character device should normally exist, but if it doesn't, create one.&lt;br /&gt;
 sudo mknod --mode 666 /var/lib/vz/private/777/dev/ptmx c 5 2&lt;br /&gt;
&lt;br /&gt;
=== Starting VE ===&lt;br /&gt;
Now start the VE:&lt;br /&gt;
 sudo vzctl start 777&lt;br /&gt;
&lt;br /&gt;
== Customizing the installation ==&lt;br /&gt;
A few things need to be done inside a newly created VE for it to become suitable for OpenVZ.  All those things are done inside the VE, so first command is:&lt;br /&gt;
 sudo vzctl enter 777&lt;br /&gt;
 export PATH=/sbin:/usr/sbin:/bin:/usr/bin&lt;br /&gt;
&lt;br /&gt;
{{Warning|Do not run the commands below on the hardware node, they are only to be run within the VE!}}&lt;br /&gt;
&lt;br /&gt;
=== Set Debian repositories ===&lt;br /&gt;
 cat &amp;lt;&amp;lt;EOF &amp;gt; /etc/apt/sources.list&lt;br /&gt;
 deb http://http.us.debian.org/debian etch main contrib&lt;br /&gt;
 deb http://security.debian.org etch/updates main contrib&lt;br /&gt;
 EOF&lt;br /&gt;
&lt;br /&gt;
=== Get new security updates ===&lt;br /&gt;
 apt-get update&lt;br /&gt;
 apt-get upgrade&lt;br /&gt;
&lt;br /&gt;
=== Install some more packages ===&lt;br /&gt;
Installing packages could be an interactive process so the system might ask some questions.  You can install more packages if you'd like.  For example:&lt;br /&gt;
 apt-get install ssh quota less&lt;br /&gt;
&lt;br /&gt;
=== Set sane permissions for &amp;lt;tt&amp;gt;/root&amp;lt;/tt&amp;gt; directory ===&lt;br /&gt;
 chmod 700 /root&lt;br /&gt;
&lt;br /&gt;
=== Disable root login ===&lt;br /&gt;
This will disable root login by default.&lt;br /&gt;
 usermod -L root&lt;br /&gt;
&lt;br /&gt;
=== Disable getty ===&lt;br /&gt;
Disable running &amp;lt;tt&amp;gt;getty&amp;lt;/tt&amp;gt;s on terminals as a VE does not have any:&lt;br /&gt;
 sed -i -e '/getty/d' /etc/inittab&lt;br /&gt;
&lt;br /&gt;
=== Disable &amp;lt;tt&amp;gt;sync()&amp;lt;/tt&amp;gt; for syslog ===&lt;br /&gt;
Turn off doing &amp;lt;tt&amp;gt;sync()&amp;lt;/tt&amp;gt; on every write for &amp;lt;tt&amp;gt;syslog&amp;lt;/tt&amp;gt;'s log files, to improve I/O performance:&lt;br /&gt;
 &amp;lt;pre&amp;gt;sed -i -e 's@\([[:space:]]\)\(/var/log/\)@\1-\2@' /etc/*syslog.conf&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Fix &amp;lt;tt&amp;gt;/etc/mtab&amp;lt;/tt&amp;gt; ===&lt;br /&gt;
Link &amp;lt;tt&amp;gt;/etc/mtab&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;/proc/mounts&amp;lt;/tt&amp;gt;, so &amp;lt;tt&amp;gt;df&amp;lt;/tt&amp;gt; and friends will work:&lt;br /&gt;
 rm -f /etc/mtab&lt;br /&gt;
 ln -s /proc/mounts /etc/mtab&lt;br /&gt;
&lt;br /&gt;
=== Remove some unneeded packages ===&lt;br /&gt;
If you have any packages you'd like to remove, now's the time for it.  Here's an example:&lt;br /&gt;
 dpkg --purge modutils ppp pppoeconf pppoe pppconfig&lt;br /&gt;
&lt;br /&gt;
=== Disable services ===&lt;br /&gt;
Do not start some services, stick to bare minimum:&lt;br /&gt;
 update-rc.d -f klogd remove&lt;br /&gt;
 update-rc.d -f quotarpc remove&lt;br /&gt;
 update-rc.d -f exim4 remove&lt;br /&gt;
 update-rc.d -f inetd remove&lt;br /&gt;
&lt;br /&gt;
=== Fix SSH host keys ===&lt;br /&gt;
This is only useful if you installed SSH.  Each individual [[VE]] should have its own pair of SSH host keys.  The code below will wipe out the existing SSH keys and instruct the newly-created [[VE]] to create new SSH keys on first boot.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- please do not remove &amp;lt;source&amp;gt;...&amp;lt;/source&amp;gt; pair of tags below,&lt;br /&gt;
     otherwise quotes after -N (-N '') are not visible --&amp;gt;&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
rm -f /etc/ssh/ssh_host_*&lt;br /&gt;
cat &amp;lt;&amp;lt; EOF &amp;gt; /etc/rc2.d/S15ssh_gen_host_keys&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
ssh-keygen -f /etc/ssh/ssh_host_rsa_key -t rsa -N ''&lt;br /&gt;
ssh-keygen -f /etc/ssh/ssh_host_dsa_key -t dsa -N ''&lt;br /&gt;
rm -f \$0&lt;br /&gt;
EOF&lt;br /&gt;
chmod a+x /etc/rc2.d/S15ssh_gen_host_keys&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Note|This will not work using the dependency-based boot sequence introduced with Squeeze...}}&lt;br /&gt;
&lt;br /&gt;
=== Change timezone ===&lt;br /&gt;
&lt;br /&gt;
You might want to change timezone if you do not live in $UTC. The following example is for Germany&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ln -sf /usr/share/zoneinfo/Europe/Berlin /etc/localtime&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
or even better&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
dpkg-reconfigure tzdata&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Clean packages ===&lt;br /&gt;
After installing packages, you'll have some junk packages laying around in your cache.  Since you don't want your template to have those, this command will wipe them out.&lt;br /&gt;
 apt-get clean&lt;br /&gt;
&lt;br /&gt;
Now everything is done.  Exit from the template and go back to the hardware node.&lt;br /&gt;
 exit&lt;br /&gt;
&lt;br /&gt;
== Preparing for and packing template cache ==&lt;br /&gt;
&lt;br /&gt;
We don't need an IP for the VE anymore, and we definitely do not need it in template cache, so remove it:&lt;br /&gt;
 sudo vzctl set 777 --ipdel all --save&lt;br /&gt;
&lt;br /&gt;
Also, remove DNS server and search domain information from ''/etc/resolv.conf'' file '''in VE''':&lt;br /&gt;
 sudo nano /vz/private/777/etc/resolv.conf&lt;br /&gt;
&lt;br /&gt;
Also, remove ''/etc/hostname'' file '''in VE''':&lt;br /&gt;
 sudo rm -f /vz/private/777/etc/hostname&lt;br /&gt;
&lt;br /&gt;
Stop the VE:&lt;br /&gt;
 sudo vzctl stop 777&lt;br /&gt;
&lt;br /&gt;
Go to the VE directory:&lt;br /&gt;
 cd /vz/private/777&lt;br /&gt;
&lt;br /&gt;
Now create a cached OS tarball.  In the command below, you'll want to replace &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; with your architecture (i386, amd64, ia64, etc).&lt;br /&gt;
 sudo tar --numeric-owner -zcf /vz/template/cache/debian-4.0-i386-minimal.tar.gz .&lt;br /&gt;
&lt;br /&gt;
Look at the resulting tarball to see its size is sane:&lt;br /&gt;
 # ls -lh /vz/template/cache&lt;br /&gt;
 -rw-r--r--  1 root root  51M Apr 10 03:16 debian-4.0-i386-minimal.tar.gz&lt;br /&gt;
&lt;br /&gt;
== Checking if template cache works ==&lt;br /&gt;
We can now create a VE based on the just-created template cache.  Be sure to change &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; to your architecture just like you did when you named the tarball above.&lt;br /&gt;
 sudo vzctl create 123456 --ostemplate debian-4.0-i386-minimal&lt;br /&gt;
&lt;br /&gt;
Now make sure that it works:&lt;br /&gt;
 sudo vzctl start 123456&lt;br /&gt;
 sudo vzctl exec 123456 ps ax&lt;br /&gt;
&lt;br /&gt;
You should see that a few processes are running.&lt;br /&gt;
&lt;br /&gt;
== Final cleanup ==&lt;br /&gt;
Stop and remove the test VE you just created:&lt;br /&gt;
 sudo vzctl stop 123456&lt;br /&gt;
 sudo vzctl destroy 123456&lt;br /&gt;
 sudo rm /etc/vz/conf/123456.conf.destroyed&lt;br /&gt;
&lt;br /&gt;
Finally, let's remove the VE we used for OS template cache creation:&lt;br /&gt;
 sudo vzctl destroy 777&lt;br /&gt;
 sudo rm /etc/vz/conf/777.conf.destroyed&lt;br /&gt;
&lt;br /&gt;
[[Category: HOWTO]]&lt;br /&gt;
[[Category: Templates]]&lt;br /&gt;
[[Category: Debian]]&lt;/div&gt;</summary>
		<author><name>Mose</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Debian_template_creation&amp;diff=9185</id>
		<title>Debian template creation</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Debian_template_creation&amp;diff=9185"/>
		<updated>2010-09-30T13:53:56Z</updated>

		<summary type="html">&lt;p&gt;Mose: /* Creating /dev/ptmx */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;These are rough instructions of how to manually create basic Debian Etch (4.0) template cache, which can be used to create OpenVZ [[VE]]s based on Debian Etch (4.0). (see also &amp;lt;tt&amp;gt;/usr/share/doc/vzctl/README.Debian&amp;lt;/tt&amp;gt; in the ''vzctl'' Debian package)&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* You shouldn't be running as root, but as a user that is permitted to use sudo instead.  It's a dangerous idea, run as root at your peril.&lt;br /&gt;
* Anywhere you see &amp;lt;tt&amp;gt;/vz&amp;lt;/tt&amp;gt;, you might really need to use &amp;lt;tt&amp;gt;/var/lib/vz&amp;lt;/tt&amp;gt; instead, especially on a Debian Etch host.&lt;br /&gt;
* Anywhere you see &amp;lt;tt&amp;gt;http://http.us.debian.org/debian/&amp;lt;/tt&amp;gt;, you can substitute your favorite Debian mirror.  ([http://www.debian.org/mirror/list List of official Debian Mirrors])&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Prerequisites ==&lt;br /&gt;
You need to have a working copy of &amp;lt;tt&amp;gt;debootstrap&amp;lt;/tt&amp;gt; running on your hardware node.&lt;br /&gt;
&lt;br /&gt;
For Debian:&lt;br /&gt;
 sudo apt-get install debootstrap&lt;br /&gt;
&lt;br /&gt;
For Gentoo:&lt;br /&gt;
 sudo emerge debootstrap&lt;br /&gt;
&lt;br /&gt;
For Fedora (at least Fedora 8 have it, not sure about earlier versions):&lt;br /&gt;
 sudo yum install debootstrap&lt;br /&gt;
&lt;br /&gt;
For other distros you might need to install it from sources, or search for an appropriate package for your distribution.  An RPM is available on the [http://forum.openvz.org/index.php?t=tree&amp;amp;th=142&amp;amp;mid=584 OpenVZ Forum].&lt;br /&gt;
&lt;br /&gt;
== Bootstrapping Debian ==&lt;br /&gt;
&lt;br /&gt;
You can install different releases of Debian into a VE's private directory using the debootstrap command.&lt;br /&gt;
&lt;br /&gt;
The command parameters are:&lt;br /&gt;
&lt;br /&gt;
  debootstrap --arch ARCH NAME DIRECTORY URL&lt;br /&gt;
&lt;br /&gt;
Specify your architecture instead of &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; if you're using something other than i386/x86.  For example, for AMD64/x86_64, use &amp;lt;tt&amp;gt;amd64&amp;lt;/tt&amp;gt; or for ia64, use &amp;lt;tt&amp;gt;ia64&amp;lt;/tt&amp;gt;. You can use http or ftp in the URL.&lt;br /&gt;
&lt;br /&gt;
We use VE ID of 777 for this example, but it can be any unused ID.&lt;br /&gt;
&lt;br /&gt;
=== Lenny (current Debian stable) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap --arch i386 lenny /vz/private/777 http://http.us.debian.org/debian/&lt;br /&gt;
 or&lt;br /&gt;
 debootstrap --arch amd64 lenny /vz/private/777 ftp://ftp.us.debian.org/debian/&lt;br /&gt;
&lt;br /&gt;
=== Etch (old release) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap --arch i386 etch /vz/private/777 http://http.us.debian.org/debian/&lt;br /&gt;
&lt;br /&gt;
=== Sarge (very old release) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap sarge /vz/private/777 http://archive.debian.org/debian&lt;br /&gt;
&lt;br /&gt;
== Preparing the HN network ==&lt;br /&gt;
Append the following lines to /etc/sysctl.conf, adjust to taste and then execute &amp;quot;sysctl -p&amp;quot; for them to take effect.&lt;br /&gt;
 ### OpenVZ settings&lt;br /&gt;
 &lt;br /&gt;
 # On Hardware Node we generally need packet&lt;br /&gt;
 # forwarding enabled and proxy arp disabled&lt;br /&gt;
 &lt;br /&gt;
 net.ipv4.conf.default.forwarding=1&lt;br /&gt;
 net.ipv4.conf.default.proxy_arp = 0&lt;br /&gt;
 net.ipv4.ip_forward=1&lt;br /&gt;
 &lt;br /&gt;
 # Enables source route verification&lt;br /&gt;
 net.ipv4.conf.all.rp_filter = 1&lt;br /&gt;
 &lt;br /&gt;
 # Enables the magic-sysrq key&lt;br /&gt;
 kernel.sysrq = 1&lt;br /&gt;
 &lt;br /&gt;
 # TCP Explict Congestion Notification&lt;br /&gt;
 net.ipv4.tcp_ecn = 0&lt;br /&gt;
 &lt;br /&gt;
 # we do not want all our interfaces to send redirects&lt;br /&gt;
 net.ipv4.conf.default.send_redirects = 1&lt;br /&gt;
 net.ipv4.conf.all.send_redirects = 0&lt;br /&gt;
&lt;br /&gt;
== Preparing and starting the VE ==&lt;br /&gt;
&lt;br /&gt;
=== Setting VE config ===&lt;br /&gt;
First, we need a config for the [[VE]]:&lt;br /&gt;
 sudo vzctl set 777 --applyconfig vps.basic --save&lt;br /&gt;
&lt;br /&gt;
=== Setting VE OSTEMPLATE ===&lt;br /&gt;
Also, we need &amp;lt;tt&amp;gt;OSTEMPLATE&amp;lt;/tt&amp;gt; to be set in VE configuration file, for the [[vzctl]] to work properly.&lt;br /&gt;
&lt;br /&gt;
 sudo sh -c 'echo &amp;quot;OSTEMPLATE=debian-4.0&amp;quot; &amp;gt;&amp;gt; /etc/vz/conf/777.conf'&lt;br /&gt;
&lt;br /&gt;
=== Setting VE IP address ===&lt;br /&gt;
For the [[VE]] to be able to download updates from the Internet, we need a valid IP address for it:&lt;br /&gt;
 sudo vzctl set 777 --ipadd x.x.x.x --save&lt;br /&gt;
&lt;br /&gt;
{{Note|if you use private IP for the VE, you might have to set up NAT as described in [[Using NAT for VE with private IPs]].}}&lt;br /&gt;
&lt;br /&gt;
=== Setting DNS server for VE ===&lt;br /&gt;
For the [[VE]] to be able to download updates from the Internet, we also need to specify a DNS for it:&lt;br /&gt;
 sudo vzctl set 777 --nameserver x.x.x.x --save&lt;br /&gt;
&lt;br /&gt;
=== Creating /dev/ptmx  ===&lt;br /&gt;
The ptmx character device should normally exists, but if it doesn't, create one.&lt;br /&gt;
 sudo mknod --mode 666 /var/lib/vz/private/777/dev/ptmx c 5 2&lt;br /&gt;
&lt;br /&gt;
=== Starting VE ===&lt;br /&gt;
Now start the VE:&lt;br /&gt;
 sudo vzctl start 777&lt;br /&gt;
&lt;br /&gt;
== Customizing the installation ==&lt;br /&gt;
A few things need to be done inside a newly created VE for it to become suitable for OpenVZ.  All those things are done inside the VE, so first command is:&lt;br /&gt;
 sudo vzctl enter 777&lt;br /&gt;
 export PATH=/sbin:/usr/sbin:/bin:/usr/bin&lt;br /&gt;
&lt;br /&gt;
{{Warning|Do not run the commands below on the hardware node, they are only to be run within the VE!}}&lt;br /&gt;
&lt;br /&gt;
=== Set Debian repositories ===&lt;br /&gt;
 cat &amp;lt;&amp;lt;EOF &amp;gt; /etc/apt/sources.list&lt;br /&gt;
 deb http://http.us.debian.org/debian etch main contrib&lt;br /&gt;
 deb http://security.debian.org etch/updates main contrib&lt;br /&gt;
 EOF&lt;br /&gt;
&lt;br /&gt;
=== Get new security updates ===&lt;br /&gt;
 apt-get update&lt;br /&gt;
 apt-get upgrade&lt;br /&gt;
&lt;br /&gt;
=== Install some more packages ===&lt;br /&gt;
Installing packages could be an interactive process so the system might ask some questions.  You can install more packages if you'd like.  For example:&lt;br /&gt;
 apt-get install ssh quota less&lt;br /&gt;
&lt;br /&gt;
=== Set sane permissions for &amp;lt;tt&amp;gt;/root&amp;lt;/tt&amp;gt; directory ===&lt;br /&gt;
 chmod 700 /root&lt;br /&gt;
&lt;br /&gt;
=== Disable root login ===&lt;br /&gt;
This will disable root login by default.&lt;br /&gt;
 usermod -L root&lt;br /&gt;
&lt;br /&gt;
=== Disable getty ===&lt;br /&gt;
Disable running &amp;lt;tt&amp;gt;getty&amp;lt;/tt&amp;gt;s on terminals as a VE does not have any:&lt;br /&gt;
 sed -i -e '/getty/d' /etc/inittab&lt;br /&gt;
&lt;br /&gt;
=== Disable &amp;lt;tt&amp;gt;sync()&amp;lt;/tt&amp;gt; for syslog ===&lt;br /&gt;
Turn off doing &amp;lt;tt&amp;gt;sync()&amp;lt;/tt&amp;gt; on every write for &amp;lt;tt&amp;gt;syslog&amp;lt;/tt&amp;gt;'s log files, to improve I/O performance:&lt;br /&gt;
 &amp;lt;pre&amp;gt;sed -i -e 's@\([[:space:]]\)\(/var/log/\)@\1-\2@' /etc/*syslog.conf&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Fix &amp;lt;tt&amp;gt;/etc/mtab&amp;lt;/tt&amp;gt; ===&lt;br /&gt;
Link &amp;lt;tt&amp;gt;/etc/mtab&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;/proc/mounts&amp;lt;/tt&amp;gt;, so &amp;lt;tt&amp;gt;df&amp;lt;/tt&amp;gt; and friends will work:&lt;br /&gt;
 rm -f /etc/mtab&lt;br /&gt;
 ln -s /proc/mounts /etc/mtab&lt;br /&gt;
&lt;br /&gt;
=== Remove some unneeded packages ===&lt;br /&gt;
If you have any packages you'd like to remove, now's the time for it.  Here's an example:&lt;br /&gt;
 dpkg --purge modutils ppp pppoeconf pppoe pppconfig&lt;br /&gt;
&lt;br /&gt;
=== Disable services ===&lt;br /&gt;
Do not start some services, stick to bare minimum:&lt;br /&gt;
 update-rc.d -f klogd remove&lt;br /&gt;
 update-rc.d -f quotarpc remove&lt;br /&gt;
 update-rc.d -f exim4 remove&lt;br /&gt;
 update-rc.d -f inetd remove&lt;br /&gt;
&lt;br /&gt;
=== Fix SSH host keys ===&lt;br /&gt;
This is only useful if you installed SSH.  Each individual [[VE]] should have its own pair of SSH host keys.  The code below will wipe out the existing SSH keys and instruct the newly-created [[VE]] to create new SSH keys on first boot.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- please do not remove &amp;lt;source&amp;gt;...&amp;lt;/source&amp;gt; pair of tags below,&lt;br /&gt;
     otherwise quotes after -N (-N '') are not visible --&amp;gt;&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
rm -f /etc/ssh/ssh_host_*&lt;br /&gt;
cat &amp;lt;&amp;lt; EOF &amp;gt; /etc/rc2.d/S15ssh_gen_host_keys&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
ssh-keygen -f /etc/ssh/ssh_host_rsa_key -t rsa -N ''&lt;br /&gt;
ssh-keygen -f /etc/ssh/ssh_host_dsa_key -t dsa -N ''&lt;br /&gt;
rm -f \$0&lt;br /&gt;
EOF&lt;br /&gt;
chmod a+x /etc/rc2.d/S15ssh_gen_host_keys&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Note|This will not work using the dependency-based boot sequence introduced with Squeeze...}}&lt;br /&gt;
&lt;br /&gt;
=== Change timezone ===&lt;br /&gt;
&lt;br /&gt;
You might want to change timezone if you do not live in $UTC. The following example is for Germany&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ln -sf /usr/share/zoneinfo/Europe/Berlin /etc/localtime&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
or even better&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
dpkg-reconfigure tzdata&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Clean packages ===&lt;br /&gt;
After installing packages, you'll have some junk packages laying around in your cache.  Since you don't want your template to have those, this command will wipe them out.&lt;br /&gt;
 apt-get clean&lt;br /&gt;
&lt;br /&gt;
Now everything is done.  Exit from the template and go back to the hardware node.&lt;br /&gt;
 exit&lt;br /&gt;
&lt;br /&gt;
== Preparing for and packing template cache ==&lt;br /&gt;
&lt;br /&gt;
We don't need an IP for the VE anymore, and we definitely do not need it in template cache, so remove it:&lt;br /&gt;
 sudo vzctl set 777 --ipdel all --save&lt;br /&gt;
&lt;br /&gt;
Also, remove DNS server and search domain information from ''/etc/resolv.conf'' file '''in VE''':&lt;br /&gt;
 sudo nano /vz/private/777/etc/resolv.conf&lt;br /&gt;
&lt;br /&gt;
Also, remove ''/etc/hostname'' file '''in VE''':&lt;br /&gt;
 sudo rm -f /vz/private/777/etc/hostname&lt;br /&gt;
&lt;br /&gt;
Stop the VE:&lt;br /&gt;
 sudo vzctl stop 777&lt;br /&gt;
&lt;br /&gt;
Go to the VE directory:&lt;br /&gt;
 cd /vz/private/777&lt;br /&gt;
&lt;br /&gt;
Now create a cached OS tarball.  In the command below, you'll want to replace &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; with your architecture (i386, amd64, ia64, etc).&lt;br /&gt;
 sudo tar --numeric-owner -zcf /vz/template/cache/debian-4.0-i386-minimal.tar.gz .&lt;br /&gt;
&lt;br /&gt;
Look at the resulting tarball to see its size is sane:&lt;br /&gt;
 # ls -lh /vz/template/cache&lt;br /&gt;
 -rw-r--r--  1 root root  51M Apr 10 03:16 debian-4.0-i386-minimal.tar.gz&lt;br /&gt;
&lt;br /&gt;
== Checking if template cache works ==&lt;br /&gt;
We can now create a VE based on the just-created template cache.  Be sure to change &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; to your architecture just like you did when you named the tarball above.&lt;br /&gt;
 sudo vzctl create 123456 --ostemplate debian-4.0-i386-minimal&lt;br /&gt;
&lt;br /&gt;
Now make sure that it works:&lt;br /&gt;
 sudo vzctl start 123456&lt;br /&gt;
 sudo vzctl exec 123456 ps ax&lt;br /&gt;
&lt;br /&gt;
You should see that a few processes are running.&lt;br /&gt;
&lt;br /&gt;
== Final cleanup ==&lt;br /&gt;
Stop and remove the test VE you just created:&lt;br /&gt;
 sudo vzctl stop 123456&lt;br /&gt;
 sudo vzctl destroy 123456&lt;br /&gt;
 sudo rm /etc/vz/conf/123456.conf.destroyed&lt;br /&gt;
&lt;br /&gt;
Finally, let's remove the VE we used for OS template cache creation:&lt;br /&gt;
 sudo vzctl destroy 777&lt;br /&gt;
 sudo rm /etc/vz/conf/777.conf.destroyed&lt;br /&gt;
&lt;br /&gt;
[[Category: HOWTO]]&lt;br /&gt;
[[Category: Templates]]&lt;br /&gt;
[[Category: Debian]]&lt;/div&gt;</summary>
		<author><name>Mose</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Debian_template_creation&amp;diff=9184</id>
		<title>Debian template creation</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Debian_template_creation&amp;diff=9184"/>
		<updated>2010-09-30T13:34:55Z</updated>

		<summary type="html">&lt;p&gt;Mose: /* Bootstrapping Debian */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;These are rough instructions of how to manually create basic Debian Etch (4.0) template cache, which can be used to create OpenVZ [[VE]]s based on Debian Etch (4.0). (see also &amp;lt;tt&amp;gt;/usr/share/doc/vzctl/README.Debian&amp;lt;/tt&amp;gt; in the ''vzctl'' Debian package)&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* You shouldn't be running as root, but as a user that is permitted to use sudo instead.  It's a dangerous idea, run as root at your peril.&lt;br /&gt;
* Anywhere you see &amp;lt;tt&amp;gt;/vz&amp;lt;/tt&amp;gt;, you might really need to use &amp;lt;tt&amp;gt;/var/lib/vz&amp;lt;/tt&amp;gt; instead, especially on a Debian Etch host.&lt;br /&gt;
* Anywhere you see &amp;lt;tt&amp;gt;http://http.us.debian.org/debian/&amp;lt;/tt&amp;gt;, you can substitute your favorite Debian mirror.  ([http://www.debian.org/mirror/list List of official Debian Mirrors])&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Prerequisites ==&lt;br /&gt;
You need to have a working copy of &amp;lt;tt&amp;gt;debootstrap&amp;lt;/tt&amp;gt; running on your hardware node.&lt;br /&gt;
&lt;br /&gt;
For Debian:&lt;br /&gt;
 sudo apt-get install debootstrap&lt;br /&gt;
&lt;br /&gt;
For Gentoo:&lt;br /&gt;
 sudo emerge debootstrap&lt;br /&gt;
&lt;br /&gt;
For Fedora (at least Fedora 8 have it, not sure about earlier versions):&lt;br /&gt;
 sudo yum install debootstrap&lt;br /&gt;
&lt;br /&gt;
For other distros you might need to install it from sources, or search for an appropriate package for your distribution.  An RPM is available on the [http://forum.openvz.org/index.php?t=tree&amp;amp;th=142&amp;amp;mid=584 OpenVZ Forum].&lt;br /&gt;
&lt;br /&gt;
== Bootstrapping Debian ==&lt;br /&gt;
&lt;br /&gt;
You can install different releases of Debian into a VE's private directory using the debootstrap command.&lt;br /&gt;
&lt;br /&gt;
The command parameters are:&lt;br /&gt;
&lt;br /&gt;
  debootstrap --arch ARCH NAME DIRECTORY URL&lt;br /&gt;
&lt;br /&gt;
Specify your architecture instead of &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; if you're using something other than i386/x86.  For example, for AMD64/x86_64, use &amp;lt;tt&amp;gt;amd64&amp;lt;/tt&amp;gt; or for ia64, use &amp;lt;tt&amp;gt;ia64&amp;lt;/tt&amp;gt;. You can use http or ftp in the URL.&lt;br /&gt;
&lt;br /&gt;
We use VE ID of 777 for this example, but it can be any unused ID.&lt;br /&gt;
&lt;br /&gt;
=== Lenny (current Debian stable) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap --arch i386 lenny /vz/private/777 http://http.us.debian.org/debian/&lt;br /&gt;
 or&lt;br /&gt;
 debootstrap --arch amd64 lenny /vz/private/777 ftp://ftp.us.debian.org/debian/&lt;br /&gt;
&lt;br /&gt;
=== Etch (old release) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap --arch i386 etch /vz/private/777 http://http.us.debian.org/debian/&lt;br /&gt;
&lt;br /&gt;
=== Sarge (very old release) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap sarge /vz/private/777 http://archive.debian.org/debian&lt;br /&gt;
&lt;br /&gt;
== Preparing the HN network ==&lt;br /&gt;
Append the following lines to /etc/sysctl.conf, adjust to taste and then execute &amp;quot;sysctl -p&amp;quot; for them to take effect.&lt;br /&gt;
 ### OpenVZ settings&lt;br /&gt;
 &lt;br /&gt;
 # On Hardware Node we generally need packet&lt;br /&gt;
 # forwarding enabled and proxy arp disabled&lt;br /&gt;
 &lt;br /&gt;
 net.ipv4.conf.default.forwarding=1&lt;br /&gt;
 net.ipv4.conf.default.proxy_arp = 0&lt;br /&gt;
 net.ipv4.ip_forward=1&lt;br /&gt;
 &lt;br /&gt;
 # Enables source route verification&lt;br /&gt;
 net.ipv4.conf.all.rp_filter = 1&lt;br /&gt;
 &lt;br /&gt;
 # Enables the magic-sysrq key&lt;br /&gt;
 kernel.sysrq = 1&lt;br /&gt;
 &lt;br /&gt;
 # TCP Explict Congestion Notification&lt;br /&gt;
 net.ipv4.tcp_ecn = 0&lt;br /&gt;
 &lt;br /&gt;
 # we do not want all our interfaces to send redirects&lt;br /&gt;
 net.ipv4.conf.default.send_redirects = 1&lt;br /&gt;
 net.ipv4.conf.all.send_redirects = 0&lt;br /&gt;
&lt;br /&gt;
== Preparing and starting the VE ==&lt;br /&gt;
&lt;br /&gt;
=== Setting VE config ===&lt;br /&gt;
First, we need a config for the [[VE]]:&lt;br /&gt;
 sudo vzctl set 777 --applyconfig vps.basic --save&lt;br /&gt;
&lt;br /&gt;
=== Setting VE OSTEMPLATE ===&lt;br /&gt;
Also, we need &amp;lt;tt&amp;gt;OSTEMPLATE&amp;lt;/tt&amp;gt; to be set in VE configuration file, for the [[vzctl]] to work properly.&lt;br /&gt;
&lt;br /&gt;
 sudo sh -c 'echo &amp;quot;OSTEMPLATE=debian-4.0&amp;quot; &amp;gt;&amp;gt; /etc/vz/conf/777.conf'&lt;br /&gt;
&lt;br /&gt;
=== Setting VE IP address ===&lt;br /&gt;
For the [[VE]] to be able to download updates from the Internet, we need a valid IP address for it:&lt;br /&gt;
 sudo vzctl set 777 --ipadd x.x.x.x --save&lt;br /&gt;
&lt;br /&gt;
{{Note|if you use private IP for the VE, you might have to set up NAT as described in [[Using NAT for VE with private IPs]].}}&lt;br /&gt;
&lt;br /&gt;
=== Setting DNS server for VE ===&lt;br /&gt;
For the [[VE]] to be able to download updates from the Internet, we also need to specify a DNS for it:&lt;br /&gt;
 sudo vzctl set 777 --nameserver x.x.x.x --save&lt;br /&gt;
&lt;br /&gt;
=== Creating /dev/ptmx  ===&lt;br /&gt;
Before starting the VE we need a valide character ptmx device&lt;br /&gt;
 sudo mknod --mode 666 /var/lib/vz/private/777/dev/ptmx c 5 2&lt;br /&gt;
&lt;br /&gt;
=== Starting VE ===&lt;br /&gt;
Now start the VE:&lt;br /&gt;
 sudo vzctl start 777&lt;br /&gt;
&lt;br /&gt;
== Customizing the installation ==&lt;br /&gt;
A few things need to be done inside a newly created VE for it to become suitable for OpenVZ.  All those things are done inside the VE, so first command is:&lt;br /&gt;
 sudo vzctl enter 777&lt;br /&gt;
 export PATH=/sbin:/usr/sbin:/bin:/usr/bin&lt;br /&gt;
&lt;br /&gt;
{{Warning|Do not run the commands below on the hardware node, they are only to be run within the VE!}}&lt;br /&gt;
&lt;br /&gt;
=== Set Debian repositories ===&lt;br /&gt;
 cat &amp;lt;&amp;lt;EOF &amp;gt; /etc/apt/sources.list&lt;br /&gt;
 deb http://http.us.debian.org/debian etch main contrib&lt;br /&gt;
 deb http://security.debian.org etch/updates main contrib&lt;br /&gt;
 EOF&lt;br /&gt;
&lt;br /&gt;
=== Get new security updates ===&lt;br /&gt;
 apt-get update&lt;br /&gt;
 apt-get upgrade&lt;br /&gt;
&lt;br /&gt;
=== Install some more packages ===&lt;br /&gt;
Installing packages could be an interactive process so the system might ask some questions.  You can install more packages if you'd like.  For example:&lt;br /&gt;
 apt-get install ssh quota less&lt;br /&gt;
&lt;br /&gt;
=== Set sane permissions for &amp;lt;tt&amp;gt;/root&amp;lt;/tt&amp;gt; directory ===&lt;br /&gt;
 chmod 700 /root&lt;br /&gt;
&lt;br /&gt;
=== Disable root login ===&lt;br /&gt;
This will disable root login by default.&lt;br /&gt;
 usermod -L root&lt;br /&gt;
&lt;br /&gt;
=== Disable getty ===&lt;br /&gt;
Disable running &amp;lt;tt&amp;gt;getty&amp;lt;/tt&amp;gt;s on terminals as a VE does not have any:&lt;br /&gt;
 sed -i -e '/getty/d' /etc/inittab&lt;br /&gt;
&lt;br /&gt;
=== Disable &amp;lt;tt&amp;gt;sync()&amp;lt;/tt&amp;gt; for syslog ===&lt;br /&gt;
Turn off doing &amp;lt;tt&amp;gt;sync()&amp;lt;/tt&amp;gt; on every write for &amp;lt;tt&amp;gt;syslog&amp;lt;/tt&amp;gt;'s log files, to improve I/O performance:&lt;br /&gt;
 &amp;lt;pre&amp;gt;sed -i -e 's@\([[:space:]]\)\(/var/log/\)@\1-\2@' /etc/*syslog.conf&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Fix &amp;lt;tt&amp;gt;/etc/mtab&amp;lt;/tt&amp;gt; ===&lt;br /&gt;
Link &amp;lt;tt&amp;gt;/etc/mtab&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;/proc/mounts&amp;lt;/tt&amp;gt;, so &amp;lt;tt&amp;gt;df&amp;lt;/tt&amp;gt; and friends will work:&lt;br /&gt;
 rm -f /etc/mtab&lt;br /&gt;
 ln -s /proc/mounts /etc/mtab&lt;br /&gt;
&lt;br /&gt;
=== Remove some unneeded packages ===&lt;br /&gt;
If you have any packages you'd like to remove, now's the time for it.  Here's an example:&lt;br /&gt;
 dpkg --purge modutils ppp pppoeconf pppoe pppconfig&lt;br /&gt;
&lt;br /&gt;
=== Disable services ===&lt;br /&gt;
Do not start some services, stick to bare minimum:&lt;br /&gt;
 update-rc.d -f klogd remove&lt;br /&gt;
 update-rc.d -f quotarpc remove&lt;br /&gt;
 update-rc.d -f exim4 remove&lt;br /&gt;
 update-rc.d -f inetd remove&lt;br /&gt;
&lt;br /&gt;
=== Fix SSH host keys ===&lt;br /&gt;
This is only useful if you installed SSH.  Each individual [[VE]] should have its own pair of SSH host keys.  The code below will wipe out the existing SSH keys and instruct the newly-created [[VE]] to create new SSH keys on first boot.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- please do not remove &amp;lt;source&amp;gt;...&amp;lt;/source&amp;gt; pair of tags below,&lt;br /&gt;
     otherwise quotes after -N (-N '') are not visible --&amp;gt;&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
rm -f /etc/ssh/ssh_host_*&lt;br /&gt;
cat &amp;lt;&amp;lt; EOF &amp;gt; /etc/rc2.d/S15ssh_gen_host_keys&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
ssh-keygen -f /etc/ssh/ssh_host_rsa_key -t rsa -N ''&lt;br /&gt;
ssh-keygen -f /etc/ssh/ssh_host_dsa_key -t dsa -N ''&lt;br /&gt;
rm -f \$0&lt;br /&gt;
EOF&lt;br /&gt;
chmod a+x /etc/rc2.d/S15ssh_gen_host_keys&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Note|This will not work using the dependency-based boot sequence introduced with Squeeze...}}&lt;br /&gt;
&lt;br /&gt;
=== Change timezone ===&lt;br /&gt;
&lt;br /&gt;
You might want to change timezone if you do not live in $UTC. The following example is for Germany&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ln -sf /usr/share/zoneinfo/Europe/Berlin /etc/localtime&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
or even better&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
dpkg-reconfigure tzdata&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Clean packages ===&lt;br /&gt;
After installing packages, you'll have some junk packages laying around in your cache.  Since you don't want your template to have those, this command will wipe them out.&lt;br /&gt;
 apt-get clean&lt;br /&gt;
&lt;br /&gt;
Now everything is done.  Exit from the template and go back to the hardware node.&lt;br /&gt;
 exit&lt;br /&gt;
&lt;br /&gt;
== Preparing for and packing template cache ==&lt;br /&gt;
&lt;br /&gt;
We don't need an IP for the VE anymore, and we definitely do not need it in template cache, so remove it:&lt;br /&gt;
 sudo vzctl set 777 --ipdel all --save&lt;br /&gt;
&lt;br /&gt;
Also, remove DNS server and search domain information from ''/etc/resolv.conf'' file '''in VE''':&lt;br /&gt;
 sudo nano /vz/private/777/etc/resolv.conf&lt;br /&gt;
&lt;br /&gt;
Also, remove ''/etc/hostname'' file '''in VE''':&lt;br /&gt;
 sudo rm -f /vz/private/777/etc/hostname&lt;br /&gt;
&lt;br /&gt;
Stop the VE:&lt;br /&gt;
 sudo vzctl stop 777&lt;br /&gt;
&lt;br /&gt;
Go to the VE directory:&lt;br /&gt;
 cd /vz/private/777&lt;br /&gt;
&lt;br /&gt;
Now create a cached OS tarball.  In the command below, you'll want to replace &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; with your architecture (i386, amd64, ia64, etc).&lt;br /&gt;
 sudo tar --numeric-owner -zcf /vz/template/cache/debian-4.0-i386-minimal.tar.gz .&lt;br /&gt;
&lt;br /&gt;
Look at the resulting tarball to see its size is sane:&lt;br /&gt;
 # ls -lh /vz/template/cache&lt;br /&gt;
 -rw-r--r--  1 root root  51M Apr 10 03:16 debian-4.0-i386-minimal.tar.gz&lt;br /&gt;
&lt;br /&gt;
== Checking if template cache works ==&lt;br /&gt;
We can now create a VE based on the just-created template cache.  Be sure to change &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; to your architecture just like you did when you named the tarball above.&lt;br /&gt;
 sudo vzctl create 123456 --ostemplate debian-4.0-i386-minimal&lt;br /&gt;
&lt;br /&gt;
Now make sure that it works:&lt;br /&gt;
 sudo vzctl start 123456&lt;br /&gt;
 sudo vzctl exec 123456 ps ax&lt;br /&gt;
&lt;br /&gt;
You should see that a few processes are running.&lt;br /&gt;
&lt;br /&gt;
== Final cleanup ==&lt;br /&gt;
Stop and remove the test VE you just created:&lt;br /&gt;
 sudo vzctl stop 123456&lt;br /&gt;
 sudo vzctl destroy 123456&lt;br /&gt;
 sudo rm /etc/vz/conf/123456.conf.destroyed&lt;br /&gt;
&lt;br /&gt;
Finally, let's remove the VE we used for OS template cache creation:&lt;br /&gt;
 sudo vzctl destroy 777&lt;br /&gt;
 sudo rm /etc/vz/conf/777.conf.destroyed&lt;br /&gt;
&lt;br /&gt;
[[Category: HOWTO]]&lt;br /&gt;
[[Category: Templates]]&lt;br /&gt;
[[Category: Debian]]&lt;/div&gt;</summary>
		<author><name>Mose</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Debian_template_creation&amp;diff=9165</id>
		<title>Debian template creation</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Debian_template_creation&amp;diff=9165"/>
		<updated>2010-09-22T13:12:30Z</updated>

		<summary type="html">&lt;p&gt;Mose: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;These are rough instructions of how to manually create basic Debian Etch (4.0) template cache, which can be used to create OpenVZ [[VE]]s based on Debian Etch (4.0). (see also &amp;lt;tt&amp;gt;/usr/share/doc/vzctl/README.Debian&amp;lt;/tt&amp;gt; in the ''vzctl'' Debian package)&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* You shouldn't be running as root, but as a user that is permitted to use sudo instead.  It's a dangerous idea, run as root at your peril.&lt;br /&gt;
* Anywhere you see &amp;lt;tt&amp;gt;/vz&amp;lt;/tt&amp;gt;, you might really need to use &amp;lt;tt&amp;gt;/var/lib/vz&amp;lt;/tt&amp;gt; instead, especially on a Debian Etch host.&lt;br /&gt;
* Anywhere you see &amp;lt;tt&amp;gt;http://http.us.debian.org/debian/&amp;lt;/tt&amp;gt;, you can substitute your favorite Debian mirror.  ([http://www.debian.org/mirror/list List of official Debian Mirrors])&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Prerequisites ==&lt;br /&gt;
You need to have a working copy of &amp;lt;tt&amp;gt;debootstrap&amp;lt;/tt&amp;gt; running on your hardware node.&lt;br /&gt;
&lt;br /&gt;
For Debian:&lt;br /&gt;
 sudo apt-get install debootstrap&lt;br /&gt;
&lt;br /&gt;
For Gentoo:&lt;br /&gt;
 sudo emerge debootstrap&lt;br /&gt;
&lt;br /&gt;
For Fedora (at least Fedora 8 have it, not sure about earlier versions):&lt;br /&gt;
 sudo yum install debootstrap&lt;br /&gt;
&lt;br /&gt;
For other distros you might need to install it from sources, or search for an appropriate package for your distribution.  An RPM is available on the [http://forum.openvz.org/index.php?t=tree&amp;amp;th=142&amp;amp;mid=584 OpenVZ Forum].&lt;br /&gt;
&lt;br /&gt;
== Bootstrapping Debian ==&lt;br /&gt;
&lt;br /&gt;
You can install different releases of Debian into a VE's private directory using the debootstrap command.&lt;br /&gt;
&lt;br /&gt;
The command parameters are:&lt;br /&gt;
&lt;br /&gt;
  debootstrap --arch ARCH NAME DIRECTORY URL&lt;br /&gt;
&lt;br /&gt;
Specify your architecture instead of &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; if you're using something other than i386/x86.  For example, for AMD64/x86_64, use &amp;lt;tt&amp;gt;amd64&amp;lt;/tt&amp;gt; or for ia64, use &amp;lt;tt&amp;gt;ia64&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
We use VE ID of 777 for this example; surely it can be any other unused ID.&lt;br /&gt;
&lt;br /&gt;
=== Lenny (current Debian stable) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap --arch i386 lenny /vz/private/777 http://http.us.debian.org/debian/&lt;br /&gt;
&lt;br /&gt;
=== Etch (old release) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap --arch i386 etch /vz/private/777 http://http.us.debian.org/debian/&lt;br /&gt;
&lt;br /&gt;
=== Sarge (very old release) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap sarge /vz/private/777 http://archive.debian.org/debian&lt;br /&gt;
&lt;br /&gt;
== Preparing the HN network ==&lt;br /&gt;
Append the following lines to /etc/sysctl.conf, adjust to taste and then execute &amp;quot;sysctl -p&amp;quot; for them to take effect.&lt;br /&gt;
 ### OpenVZ settings&lt;br /&gt;
 &lt;br /&gt;
 # On Hardware Node we generally need packet&lt;br /&gt;
 # forwarding enabled and proxy arp disabled&lt;br /&gt;
 &lt;br /&gt;
 net.ipv4.conf.default.forwarding=1&lt;br /&gt;
 net.ipv4.conf.default.proxy_arp = 0&lt;br /&gt;
 net.ipv4.ip_forward=1&lt;br /&gt;
 &lt;br /&gt;
 # Enables source route verification&lt;br /&gt;
 net.ipv4.conf.all.rp_filter = 1&lt;br /&gt;
 &lt;br /&gt;
 # Enables the magic-sysrq key&lt;br /&gt;
 kernel.sysrq = 1&lt;br /&gt;
 &lt;br /&gt;
 # TCP Explict Congestion Notification&lt;br /&gt;
 net.ipv4.tcp_ecn = 0&lt;br /&gt;
 &lt;br /&gt;
 # we do not want all our interfaces to send redirects&lt;br /&gt;
 net.ipv4.conf.default.send_redirects = 1&lt;br /&gt;
 net.ipv4.conf.all.send_redirects = 0&lt;br /&gt;
&lt;br /&gt;
== Preparing and starting the VE ==&lt;br /&gt;
&lt;br /&gt;
=== Setting VE config ===&lt;br /&gt;
First, we need a config for the [[VE]]:&lt;br /&gt;
 sudo vzctl set 777 --applyconfig vps.basic --save&lt;br /&gt;
&lt;br /&gt;
=== Setting VE OSTEMPLATE ===&lt;br /&gt;
Also, we need &amp;lt;tt&amp;gt;OSTEMPLATE&amp;lt;/tt&amp;gt; to be set in VE configuration file, for the [[vzctl]] to work properly.&lt;br /&gt;
&lt;br /&gt;
 sudo sh -c 'echo &amp;quot;OSTEMPLATE=debian-4.0&amp;quot; &amp;gt;&amp;gt; /etc/vz/conf/777.conf'&lt;br /&gt;
&lt;br /&gt;
=== Setting VE IP address ===&lt;br /&gt;
For the [[VE]] to be able to download updates from the Internet, we need a valid IP address for it:&lt;br /&gt;
 sudo vzctl set 777 --ipadd x.x.x.x --save&lt;br /&gt;
&lt;br /&gt;
{{Note|if you use private IP for the VE, you might have to set up NAT as described in [[Using NAT for VE with private IPs]].}}&lt;br /&gt;
&lt;br /&gt;
=== Setting DNS server for VE ===&lt;br /&gt;
For the [[VE]] to be able to download updates from the Internet, we also need to specify a DNS for it:&lt;br /&gt;
 sudo vzctl set 777 --nameserver x.x.x.x --save&lt;br /&gt;
&lt;br /&gt;
=== Starting VE ===&lt;br /&gt;
Now start the VE:&lt;br /&gt;
 sudo vzctl start 777&lt;br /&gt;
&lt;br /&gt;
== Customizing the installation ==&lt;br /&gt;
A few things need to be done inside a newly created VE for it to become suitable for OpenVZ.  All those things are done inside the VE, so first command is:&lt;br /&gt;
 sudo vzctl enter 777&lt;br /&gt;
 export PATH=/sbin:/usr/sbin:/bin:/usr/bin&lt;br /&gt;
&lt;br /&gt;
{{Warning|Do not run the commands below on the hardware node, they are only to be run within the VE!}}&lt;br /&gt;
&lt;br /&gt;
=== Set Debian repositories ===&lt;br /&gt;
 cat &amp;lt;&amp;lt;EOF &amp;gt; /etc/apt/sources.list&lt;br /&gt;
 deb http://http.us.debian.org/debian etch main contrib&lt;br /&gt;
 deb http://security.debian.org etch/updates main contrib&lt;br /&gt;
 EOF&lt;br /&gt;
&lt;br /&gt;
=== Get new security updates ===&lt;br /&gt;
 apt-get update&lt;br /&gt;
 apt-get upgrade&lt;br /&gt;
&lt;br /&gt;
=== Install some more packages ===&lt;br /&gt;
Installing packages could be an interactive process so the system might ask some questions.  You can install more packages if you'd like.  For example:&lt;br /&gt;
 apt-get install ssh quota less&lt;br /&gt;
&lt;br /&gt;
=== Set sane permissions for &amp;lt;tt&amp;gt;/root&amp;lt;/tt&amp;gt; directory ===&lt;br /&gt;
 chmod 700 /root&lt;br /&gt;
&lt;br /&gt;
=== Disable root login ===&lt;br /&gt;
This will disable root login by default.&lt;br /&gt;
 usermod -L root&lt;br /&gt;
&lt;br /&gt;
=== Disable getty ===&lt;br /&gt;
Disable running &amp;lt;tt&amp;gt;getty&amp;lt;/tt&amp;gt;s on terminals as a VE does not have any:&lt;br /&gt;
 sed -i -e '/getty/d' /etc/inittab&lt;br /&gt;
&lt;br /&gt;
=== Disable &amp;lt;tt&amp;gt;sync()&amp;lt;/tt&amp;gt; for syslog ===&lt;br /&gt;
Turn off doing &amp;lt;tt&amp;gt;sync()&amp;lt;/tt&amp;gt; on every write for &amp;lt;tt&amp;gt;syslog&amp;lt;/tt&amp;gt;'s log files, to improve I/O performance:&lt;br /&gt;
 &amp;lt;pre&amp;gt;sed -i -e 's@\([[:space:]]\)\(/var/log/\)@\1-\2@' /etc/*syslog.conf&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Fix &amp;lt;tt&amp;gt;/etc/mtab&amp;lt;/tt&amp;gt; ===&lt;br /&gt;
Link &amp;lt;tt&amp;gt;/etc/mtab&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;/proc/mounts&amp;lt;/tt&amp;gt;, so &amp;lt;tt&amp;gt;df&amp;lt;/tt&amp;gt; and friends will work:&lt;br /&gt;
 rm -f /etc/mtab&lt;br /&gt;
 ln -s /proc/mounts /etc/mtab&lt;br /&gt;
&lt;br /&gt;
=== Remove some unneeded packages ===&lt;br /&gt;
If you have any packages you'd like to remove, now's the time for it.  Here's an example:&lt;br /&gt;
 dpkg --purge modutils ppp pppoeconf pppoe pppconfig&lt;br /&gt;
&lt;br /&gt;
=== Disable services ===&lt;br /&gt;
Do not start some services, stick to bare minimum:&lt;br /&gt;
 update-rc.d -f klogd remove&lt;br /&gt;
 update-rc.d -f quotarpc remove&lt;br /&gt;
 update-rc.d -f exim4 remove&lt;br /&gt;
 update-rc.d -f inetd remove&lt;br /&gt;
&lt;br /&gt;
=== Fix SSH host keys ===&lt;br /&gt;
This is only useful if you installed SSH.  Each individual [[VE]] should have its own pair of SSH host keys.  The code below will wipe out the existing SSH keys and instruct the newly-created [[VE]] to create new SSH keys on first boot.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- please do not remove &amp;lt;source&amp;gt;...&amp;lt;/source&amp;gt; pair of tags below,&lt;br /&gt;
     otherwise quotes after -N (-N '') are not visible --&amp;gt;&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
rm -f /etc/ssh/ssh_host_*&lt;br /&gt;
cat &amp;lt;&amp;lt; EOF &amp;gt; /etc/rc2.d/S15ssh_gen_host_keys&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
ssh-keygen -f /etc/ssh/ssh_host_rsa_key -t rsa -N ''&lt;br /&gt;
ssh-keygen -f /etc/ssh/ssh_host_dsa_key -t dsa -N ''&lt;br /&gt;
rm -f \$0&lt;br /&gt;
EOF&lt;br /&gt;
chmod a+x /etc/rc2.d/S15ssh_gen_host_keys&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Note|This will not work using the dependency-based boot sequence introduced with Squeeze...}}&lt;br /&gt;
&lt;br /&gt;
=== Change timezone ===&lt;br /&gt;
&lt;br /&gt;
You might want to change timezone if you do not live in $UTC. The following example is for Germany&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ln -sf /usr/share/zoneinfo/Europe/Berlin /etc/localtime&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
or even better&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
dpkg-reconfigure tzdata&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Clean packages ===&lt;br /&gt;
After installing packages, you'll have some junk packages laying around in your cache.  Since you don't want your template to have those, this command will wipe them out.&lt;br /&gt;
 apt-get clean&lt;br /&gt;
&lt;br /&gt;
Now everything is done.  Exit from the template and go back to the hardware node.&lt;br /&gt;
 exit&lt;br /&gt;
&lt;br /&gt;
== Preparing for and packing template cache ==&lt;br /&gt;
&lt;br /&gt;
We don't need an IP for the VE anymore, and we definitely do not need it in template cache, so remove it:&lt;br /&gt;
 sudo vzctl set 777 --ipdel all --save&lt;br /&gt;
&lt;br /&gt;
Also, remove DNS server and search domain information from ''/etc/resolv.conf'' file '''in VE''':&lt;br /&gt;
 sudo nano /vz/private/777/etc/resolv.conf&lt;br /&gt;
&lt;br /&gt;
Also, remove ''/etc/hostname'' file '''in VE''':&lt;br /&gt;
 sudo rm -f /vz/private/777/etc/hostname&lt;br /&gt;
&lt;br /&gt;
Stop the VE:&lt;br /&gt;
 sudo vzctl stop 777&lt;br /&gt;
&lt;br /&gt;
Go to the VE directory:&lt;br /&gt;
 cd /vz/private/777&lt;br /&gt;
&lt;br /&gt;
Now create a cached OS tarball.  In the command below, you'll want to replace &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; with your architecture (i386, amd64, ia64, etc).&lt;br /&gt;
 sudo tar --numeric-owner -zcf /vz/template/cache/debian-4.0-i386-minimal.tar.gz .&lt;br /&gt;
&lt;br /&gt;
Look at the resulting tarball to see its size is sane:&lt;br /&gt;
 # ls -lh /vz/template/cache&lt;br /&gt;
 -rw-r--r--  1 root root  51M Apr 10 03:16 debian-4.0-i386-minimal.tar.gz&lt;br /&gt;
&lt;br /&gt;
== Checking if template cache works ==&lt;br /&gt;
We can now create a VE based on the just-created template cache.  Be sure to change &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; to your architecture just like you did when you named the tarball above.&lt;br /&gt;
 sudo vzctl create 123456 --ostemplate debian-4.0-i386-minimal&lt;br /&gt;
&lt;br /&gt;
Now make sure that it works:&lt;br /&gt;
 sudo vzctl start 123456&lt;br /&gt;
 sudo vzctl exec 123456 ps ax&lt;br /&gt;
&lt;br /&gt;
You should see that a few processes are running.&lt;br /&gt;
&lt;br /&gt;
== Final cleanup ==&lt;br /&gt;
Stop and remove the test VE you just created:&lt;br /&gt;
 sudo vzctl stop 123456&lt;br /&gt;
 sudo vzctl destroy 123456&lt;br /&gt;
 sudo rm /etc/vz/conf/123456.conf.destroyed&lt;br /&gt;
&lt;br /&gt;
Finally, let's remove the VE we used for OS template cache creation:&lt;br /&gt;
 sudo vzctl destroy 777&lt;br /&gt;
 sudo rm /etc/vz/conf/777.conf.destroyed&lt;br /&gt;
&lt;br /&gt;
[[Category: HOWTO]]&lt;br /&gt;
[[Category: Templates]]&lt;br /&gt;
[[Category: Debian]]&lt;/div&gt;</summary>
		<author><name>Mose</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Debootstrap_from_foreign_version_or_distribution&amp;diff=9146</id>
		<title>Debootstrap from foreign version or distribution</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Debootstrap_from_foreign_version_or_distribution&amp;diff=9146"/>
		<updated>2010-09-21T13:03:25Z</updated>

		<summary type="html">&lt;p&gt;Mose: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category: HOWTO]]&lt;br /&gt;
[[Category: Templates]]&lt;br /&gt;
[[Category: Ubuntu]]&lt;br /&gt;
[[Category: Debian]]&lt;br /&gt;
&lt;br /&gt;
This procedure (for Debian, Ubuntu, and derivatives) is useful when need to create a template, and your host doesn't have that suite's script (in /usr/share/debootstrap/scripts). This situation can happen when your host and your next template are from different distributions, or when your host is an older version than the next template you want to create (for example, creating an Ubuntu 10.04 template in an Ubuntu 8.04 host).&lt;br /&gt;
&lt;br /&gt;
To do this, you need a computer to install a clean system or a virtual one with [http://www.qemu.org/ Qemu] or similar.&lt;br /&gt;
&lt;br /&gt;
'''Step 1: install a system with the distribution &amp;amp; version you want for template'''&lt;br /&gt;
&lt;br /&gt;
We will call it the &amp;quot;satellite system&amp;quot;. You can do it also with another already installed computer with the same distribution &amp;amp; version you want. Also with a Live-CD with sufficient RAM memory available for a subsystem installation.&lt;br /&gt;
&lt;br /&gt;
'''Step 2: debootstrap''' (in satellite system)&lt;br /&gt;
 cd /tmp&lt;br /&gt;
 sudo apt-get install debootstrap&lt;br /&gt;
 mkdir newsystem&lt;br /&gt;
 # The parameter ''--variant=minbase'' is optional. You may want it to make a minimal template.&lt;br /&gt;
 sudo debootstrap --variant=minbase $(lsb_release -cs) newsystem&lt;br /&gt;
&lt;br /&gt;
'''Step 3: package for moving'''&lt;br /&gt;
 cd newsystem&lt;br /&gt;
 sudo tar czf /tmp/debootstrap_$(lsb_release -is | tr &amp;quot;[:upper:]&amp;quot; &amp;quot;[:lower:]&amp;quot;)-$(lsb_release -rs)-$(uname -m)''_minbase''.tar.gz .&lt;br /&gt;
 cd ..&lt;br /&gt;
 sudo rm -R newsystem&lt;br /&gt;
Now you have a package like &amp;quot;debootstrap_ubuntu-10.04-i686.tar.gz&amp;quot; to copy to the OpenVZ host/server.&lt;br /&gt;
&lt;br /&gt;
'''Unpacking for a new templaye''' (example for 777 container, in the OpenVZ server)&lt;br /&gt;
 sudo mkdir -p /vz/private/777&lt;br /&gt;
 cd /vz/private/777&lt;br /&gt;
 sudo tar xf /tmp/debootstrap_ubuntu-10.04-i686.tar.gz&lt;br /&gt;
&lt;br /&gt;
'''Next''' steps are the usual for [http://wiki.openvz.org/Category:Templates creating a template].&lt;/div&gt;</summary>
		<author><name>Mose</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Debootstrap_from_foreign_version_or_distribution&amp;diff=9145</id>
		<title>Debootstrap from foreign version or distribution</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Debootstrap_from_foreign_version_or_distribution&amp;diff=9145"/>
		<updated>2010-09-21T13:02:28Z</updated>

		<summary type="html">&lt;p&gt;Mose: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category: HOWTO]]&lt;br /&gt;
[[Category: Templates]]&lt;br /&gt;
[[Category: Ubuntu]]&lt;br /&gt;
[[Category: Debian]]&lt;br /&gt;
&lt;br /&gt;
This procedure (for Debian, Ubuntu, and derivatives) is useful when need to create a template, and your host doesn't have that suite's script (in /usr/share/debootstrap/scripts). This situation can be caused because your host and your next template are from different distributions, or because your host is an older version than the next template you want to create (for example, creating an Ubuntu 10.04 template in an Ubuntu 8.04 host).&lt;br /&gt;
&lt;br /&gt;
To do this, you need a computer to install a clean system or a virtual one with [http://www.qemu.org/ Qemu] or similar.&lt;br /&gt;
&lt;br /&gt;
'''Step 1: install a system with the distribution &amp;amp; version you want for template'''&lt;br /&gt;
&lt;br /&gt;
We will call it the &amp;quot;satellite system&amp;quot;. You can do it also with another already installed computer with the same distribution &amp;amp; version you want. Also with a Live-CD with sufficient RAM memory available for a subsystem installation.&lt;br /&gt;
&lt;br /&gt;
'''Step 2: debootstrap''' (in satellite system)&lt;br /&gt;
 cd /tmp&lt;br /&gt;
 sudo apt-get install debootstrap&lt;br /&gt;
 mkdir newsystem&lt;br /&gt;
 # The parameter ''--variant=minbase'' is optional. You may want it to make a minimal template.&lt;br /&gt;
 sudo debootstrap --variant=minbase $(lsb_release -cs) newsystem&lt;br /&gt;
&lt;br /&gt;
'''Step 3: package for moving'''&lt;br /&gt;
 cd newsystem&lt;br /&gt;
 sudo tar czf /tmp/debootstrap_$(lsb_release -is | tr &amp;quot;[:upper:]&amp;quot; &amp;quot;[:lower:]&amp;quot;)-$(lsb_release -rs)-$(uname -m)''_minbase''.tar.gz .&lt;br /&gt;
 cd ..&lt;br /&gt;
 sudo rm -R newsystem&lt;br /&gt;
Now you have a package like &amp;quot;debootstrap_ubuntu-10.04-i686.tar.gz&amp;quot; to copy to the OpenVZ host/server.&lt;br /&gt;
&lt;br /&gt;
'''Unpacking for a new templaye''' (example for 777 container, in the OpenVZ server)&lt;br /&gt;
 sudo mkdir -p /vz/private/777&lt;br /&gt;
 cd /vz/private/777&lt;br /&gt;
 sudo tar xf /tmp/debootstrap_ubuntu-10.04-i686.tar.gz&lt;br /&gt;
&lt;br /&gt;
'''Next''' steps are the usual for [http://wiki.openvz.org/Category:Templates creating a template].&lt;/div&gt;</summary>
		<author><name>Mose</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Debian_template_creation&amp;diff=8312</id>
		<title>Debian template creation</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Debian_template_creation&amp;diff=8312"/>
		<updated>2010-03-12T15:50:03Z</updated>

		<summary type="html">&lt;p&gt;Mose: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;These are rough instructions of how to manually create basic Debian Etch (4.0) template cache, which can be used to create OpenVZ [[VE]]s based on Debian Etch (4.0). (see also &amp;lt;tt&amp;gt;/usr/share/doc/vzctl/README.Debian&amp;lt;/tt&amp;gt; in the ''vzctl'' Debian package)&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* You shouldn't be running as root, but as a user that is permitted to use sudo instead.  It's a dangerous idea, run as root at your peril.&lt;br /&gt;
* Anywhere you see &amp;lt;tt&amp;gt;/vz&amp;lt;/tt&amp;gt;, you might really need to use &amp;lt;tt&amp;gt;/var/lib/vz&amp;lt;/tt&amp;gt; instead, especially on a Debian Etch host.&lt;br /&gt;
* Anywhere you see &amp;lt;tt&amp;gt;http://http.us.debian.org/debian/&amp;lt;/tt&amp;gt;, you can substitute your favorite Debian mirror.  ([http://www.debian.org/mirror/list List of official Debian Mirrors])&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Prerequisites ==&lt;br /&gt;
You need to have a working copy of &amp;lt;tt&amp;gt;debootstrap&amp;lt;/tt&amp;gt; running on your hardware node.&lt;br /&gt;
&lt;br /&gt;
For Debian:&lt;br /&gt;
 sudo apt-get install debootstrap&lt;br /&gt;
&lt;br /&gt;
For Gentoo:&lt;br /&gt;
 sudo emerge debootstrap&lt;br /&gt;
&lt;br /&gt;
For Fedora (at least Fedora 8 have it, not sure about earlier versions):&lt;br /&gt;
 sudo yum install debootstrap&lt;br /&gt;
&lt;br /&gt;
For other distros you might need to install it from sources, or search for an appropriate package for your distribution.  An RPM is available on the [http://forum.openvz.org/index.php?t=tree&amp;amp;th=142&amp;amp;mid=584 OpenVZ Forum].&lt;br /&gt;
&lt;br /&gt;
== Bootstrapping Debian ==&lt;br /&gt;
&lt;br /&gt;
You can install different releases of Debian into a VE's private directory using the debootstrap command.&lt;br /&gt;
&lt;br /&gt;
The command parameters are:&lt;br /&gt;
&lt;br /&gt;
  debootstrap --arch ARCH NAME DIRECTORY URL&lt;br /&gt;
&lt;br /&gt;
Specify your architecture instead of &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; if you're using something other than i386/x86.  For example, for AMD64/x86_64, use &amp;lt;tt&amp;gt;amd64&amp;lt;/tt&amp;gt; or for ia64, use &amp;lt;tt&amp;gt;ia64&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
We use VE ID of 777 for this example; surely it can be any other unused ID.&lt;br /&gt;
&lt;br /&gt;
=== Lenny (current Debian stable) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap --arch i386 lenny /vz/private/777 http://http.us.debian.org/debian/&lt;br /&gt;
&lt;br /&gt;
=== Etch (old release) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap --arch i386 etch /vz/private/777 http://http.us.debian.org/debian/&lt;br /&gt;
&lt;br /&gt;
=== Sarge (very old release) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap sarge /vz/private/777 http://archive.debian.org/debian&lt;br /&gt;
&lt;br /&gt;
== Preparing the HN network ==&lt;br /&gt;
Append the following lines to /etc/sysctl.conf, adjust to taste and then execute &amp;quot;sysctl -p&amp;quot; for them to take effect.&lt;br /&gt;
 ### OpenVZ settings&lt;br /&gt;
 &lt;br /&gt;
 # On Hardware Node we generally need packet&lt;br /&gt;
 # forwarding enabled and proxy arp disabled&lt;br /&gt;
 &lt;br /&gt;
 net.ipv4.conf.default.forwarding=1&lt;br /&gt;
 net.ipv4.conf.default.proxy_arp = 0&lt;br /&gt;
 net.ipv4.ip_forward=1&lt;br /&gt;
 &lt;br /&gt;
 # Enables source route verification&lt;br /&gt;
 net.ipv4.conf.all.rp_filter = 1&lt;br /&gt;
 &lt;br /&gt;
 # Enables the magic-sysrq key&lt;br /&gt;
 kernel.sysrq = 1&lt;br /&gt;
 &lt;br /&gt;
 # TCP Explict Congestion Notification&lt;br /&gt;
 net.ipv4.tcp_ecn = 0&lt;br /&gt;
 &lt;br /&gt;
 # we do not want all our interfaces to send redirects&lt;br /&gt;
 net.ipv4.conf.default.send_redirects = 1&lt;br /&gt;
 net.ipv4.conf.all.send_redirects = 0&lt;br /&gt;
&lt;br /&gt;
== Preparing and starting the VE ==&lt;br /&gt;
&lt;br /&gt;
=== Setting VE config ===&lt;br /&gt;
First, we need a config for the [[VE]]:&lt;br /&gt;
 sudo vzctl set 777 --applyconfig vps.basic --save&lt;br /&gt;
&lt;br /&gt;
=== Setting VE OSTEMPLATE ===&lt;br /&gt;
Also, we need &amp;lt;tt&amp;gt;OSTEMPLATE&amp;lt;/tt&amp;gt; to be set in VE configuration file, for the [[vzctl]] to work properly.&lt;br /&gt;
&lt;br /&gt;
 sudo sh -c 'echo &amp;quot;OSTEMPLATE=debian-4.0&amp;quot; &amp;gt;&amp;gt; /etc/vz/conf/777.conf'&lt;br /&gt;
&lt;br /&gt;
=== Setting VE IP address ===&lt;br /&gt;
For the [[VE]] to be able to download updates from the Internet, we need a valid IP address for it:&lt;br /&gt;
 sudo vzctl set 777 --ipadd x.x.x.x --save&lt;br /&gt;
&lt;br /&gt;
{{Note|if you use private IP for the VE, you have to set up NAT as described in [[Using NAT for VE with private IPs]].}}&lt;br /&gt;
&lt;br /&gt;
=== Setting DNS server for VE ===&lt;br /&gt;
For the [[VE]] to be able to download updates from the Internet, we also need to specify a DNS for it:&lt;br /&gt;
 sudo vzctl set 777 --nameserver x.x.x.x --save&lt;br /&gt;
&lt;br /&gt;
=== Starting VE ===&lt;br /&gt;
Now start the VE:&lt;br /&gt;
 sudo vzctl start 777&lt;br /&gt;
&lt;br /&gt;
== Customizing the installation ==&lt;br /&gt;
A few things need to be done inside a newly created VE for it to become suitable for OpenVZ.  All those things are done inside the VE, so first command is:&lt;br /&gt;
 sudo vzctl enter 777&lt;br /&gt;
 export PATH=/sbin:/usr/sbin:/bin:/usr/bin&lt;br /&gt;
&lt;br /&gt;
{{Warning|Do not run the commands below on the hardware node, they are only to be run within the VE!}}&lt;br /&gt;
&lt;br /&gt;
=== Set Debian repositories ===&lt;br /&gt;
 cat &amp;lt;&amp;lt;EOF &amp;gt; /etc/apt/sources.list&lt;br /&gt;
 deb http://http.us.debian.org/debian etch main contrib&lt;br /&gt;
 deb http://security.debian.org etch/updates main contrib&lt;br /&gt;
 EOF&lt;br /&gt;
&lt;br /&gt;
=== Get new security updates ===&lt;br /&gt;
 apt-get update&lt;br /&gt;
 apt-get upgrade&lt;br /&gt;
&lt;br /&gt;
=== Install some more packages ===&lt;br /&gt;
Installing packages could be an interactive process so the system might ask some questions.  You can install more packages if you'd like.  For example:&lt;br /&gt;
 apt-get install ssh quota less&lt;br /&gt;
&lt;br /&gt;
=== Set sane permissions for &amp;lt;tt&amp;gt;/root&amp;lt;/tt&amp;gt; directory ===&lt;br /&gt;
 chmod 700 /root&lt;br /&gt;
&lt;br /&gt;
=== Disable root login ===&lt;br /&gt;
This will disable root login by default.&lt;br /&gt;
 usermod -L root&lt;br /&gt;
&lt;br /&gt;
=== Disable getty ===&lt;br /&gt;
Disable running &amp;lt;tt&amp;gt;getty&amp;lt;/tt&amp;gt;s on terminals as a VE does not have any:&lt;br /&gt;
 sed -i -e '/getty/d' /etc/inittab&lt;br /&gt;
&lt;br /&gt;
=== Disable &amp;lt;tt&amp;gt;sync()&amp;lt;/tt&amp;gt; for syslog ===&lt;br /&gt;
Turn off doing &amp;lt;tt&amp;gt;sync()&amp;lt;/tt&amp;gt; on every write for &amp;lt;tt&amp;gt;syslog&amp;lt;/tt&amp;gt;'s log files, to improve I/O performance:&lt;br /&gt;
 &amp;lt;pre&amp;gt;sed -i -e 's@\([[:space:]]\)\(/var/log/\)@\1-\2@' /etc/*syslog.conf&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Fix &amp;lt;tt&amp;gt;/etc/mtab&amp;lt;/tt&amp;gt; ===&lt;br /&gt;
Link &amp;lt;tt&amp;gt;/etc/mtab&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;/proc/mounts&amp;lt;/tt&amp;gt;, so &amp;lt;tt&amp;gt;df&amp;lt;/tt&amp;gt; and friends will work:&lt;br /&gt;
 rm -f /etc/mtab&lt;br /&gt;
 ln -s /proc/mounts /etc/mtab&lt;br /&gt;
&lt;br /&gt;
=== Remove some unneeded packages ===&lt;br /&gt;
If you have any packages you'd like to remove, now's the time for it.  Here's an example:&lt;br /&gt;
 dpkg --purge modutils ppp pppoeconf pppoe pppconfig&lt;br /&gt;
&lt;br /&gt;
=== Disable services ===&lt;br /&gt;
Do not start some services, stick to bare minimum:&lt;br /&gt;
 update-rc.d -f klogd remove&lt;br /&gt;
 update-rc.d -f quotarpc remove&lt;br /&gt;
 update-rc.d -f exim4 remove&lt;br /&gt;
 update-rc.d -f inetd remove&lt;br /&gt;
&lt;br /&gt;
=== Fix SSH host keys ===&lt;br /&gt;
This is only useful if you installed SSH.  Each individual [[VE]] should have its own pair of SSH host keys.  The code below will wipe out the existing SSH keys and instruct the newly-created [[VE]] to create new SSH keys on first boot.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- please do not remove &amp;lt;source&amp;gt;...&amp;lt;/source&amp;gt; pair of tags below,&lt;br /&gt;
     otherwise quotes after -N (-N '') are not visible --&amp;gt;&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
rm -f /etc/ssh/ssh_host_*&lt;br /&gt;
cat &amp;lt;&amp;lt; EOF &amp;gt; /etc/rc2.d/S15ssh_gen_host_keys&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
ssh-keygen -f /etc/ssh/ssh_host_rsa_key -t rsa -N ''&lt;br /&gt;
ssh-keygen -f /etc/ssh/ssh_host_dsa_key -t dsa -N ''&lt;br /&gt;
rm -f \$0&lt;br /&gt;
EOF&lt;br /&gt;
chmod a+x /etc/rc2.d/S15ssh_gen_host_keys&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Change timezone ===&lt;br /&gt;
&lt;br /&gt;
You might want to change timezone if you do not live in $UTC. The following example is for Germany&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ln -sf /usr/share/zoneinfo/Europe/Berlin /etc/localtime&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
or even better&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
dpkg-reconfigure tzdata&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Clean packages ===&lt;br /&gt;
After installing packages, you'll have some junk packages laying around in your cache.  Since you don't want your template to have those, this command will wipe them out.&lt;br /&gt;
 apt-get clean&lt;br /&gt;
&lt;br /&gt;
Now everything is done.  Exit from the template and go back to the hardware node.&lt;br /&gt;
 exit&lt;br /&gt;
&lt;br /&gt;
== Preparing for and packing template cache ==&lt;br /&gt;
&lt;br /&gt;
We don't need an IP for the VE anymore, and we definitely do not need it in template cache, so remove it:&lt;br /&gt;
 sudo vzctl set 777 --ipdel all --save&lt;br /&gt;
&lt;br /&gt;
Also, remove DNS server and search domain information from ''/etc/resolv.conf'' file '''in VE''':&lt;br /&gt;
 sudo nano /vz/private/777/etc/resolv.conf&lt;br /&gt;
&lt;br /&gt;
Also, remove ''/etc/hostname'' file '''in VE''':&lt;br /&gt;
 sudo rm -f /vz/private/777/etc/hostname&lt;br /&gt;
&lt;br /&gt;
Stop the VE:&lt;br /&gt;
 sudo vzctl stop 777&lt;br /&gt;
&lt;br /&gt;
Go to the VE directory:&lt;br /&gt;
 cd /vz/private/777&lt;br /&gt;
&lt;br /&gt;
Now create a cached OS tarball.  In the command below, you'll want to replace &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; with your architecture (i386, amd64, ia64, etc).&lt;br /&gt;
 sudo tar --numeric-owner -zcf /vz/template/cache/debian-4.0-i386-minimal.tar.gz .&lt;br /&gt;
&lt;br /&gt;
Look at the resulting tarball to see its size is sane:&lt;br /&gt;
 # ls -lh /vz/template/cache&lt;br /&gt;
 -rw-r--r--  1 root root  51M Apr 10 03:16 debian-4.0-i386-minimal.tar.gz&lt;br /&gt;
&lt;br /&gt;
== Checking if template cache works ==&lt;br /&gt;
We can now create a VE based on the just-created template cache.  Be sure to change &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; to your architecture just like you did when you named the tarball above.&lt;br /&gt;
 sudo vzctl create 123456 --ostemplate debian-4.0-i386-minimal&lt;br /&gt;
&lt;br /&gt;
Now make sure that it works:&lt;br /&gt;
 sudo vzctl start 123456&lt;br /&gt;
 sudo vzctl exec 123456 ps ax&lt;br /&gt;
&lt;br /&gt;
You should see that a few processes are running.&lt;br /&gt;
&lt;br /&gt;
== Final cleanup ==&lt;br /&gt;
Stop and remove the test VE you just created:&lt;br /&gt;
 sudo vzctl stop 123456&lt;br /&gt;
 sudo vzctl destroy 123456&lt;br /&gt;
 sudo rm /etc/vz/conf/123456.conf.destroyed&lt;br /&gt;
&lt;br /&gt;
Finally, let's remove the VE we used for OS template cache creation:&lt;br /&gt;
 sudo vzctl destroy 777&lt;br /&gt;
 sudo rm /etc/vz/conf/777.conf.destroyed&lt;br /&gt;
&lt;br /&gt;
[[Category: HOWTO]]&lt;br /&gt;
[[Category: Templates]]&lt;br /&gt;
[[Category: Debian]]&lt;/div&gt;</summary>
		<author><name>Mose</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Debian_template_creation&amp;diff=8309</id>
		<title>Debian template creation</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Debian_template_creation&amp;diff=8309"/>
		<updated>2010-03-12T02:23:25Z</updated>

		<summary type="html">&lt;p&gt;Mose: /* Change timezone */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;These are rough instructions of how to manually create basic Debian Etch (4.0) template cache, which can be used to create OpenVZ [[VE]]s based on Debian Etch (4.0). (see also &amp;lt;tt&amp;gt;/usr/share/doc/vzctl/README.Debian&amp;lt;/tt&amp;gt; in the ''vzctl'' Debian package)&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* You shouldn't be running as root, but as a user that is permitted to use sudo instead.  It's a dangerous idea, run as root at your peril.&lt;br /&gt;
* Anywhere you see &amp;lt;tt&amp;gt;/vz&amp;lt;/tt&amp;gt;, you might really need to use &amp;lt;tt&amp;gt;/var/lib/vz&amp;lt;/tt&amp;gt; instead, especially on a Debian Etch host.&lt;br /&gt;
* Anywhere you see &amp;lt;tt&amp;gt;http://http.us.debian.org/debian/&amp;lt;/tt&amp;gt;, you can substitute your favorite Debian mirror.  ([http://www.debian.org/mirror/list List of official Debian Mirrors])&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Prerequisites ==&lt;br /&gt;
You need to have a working copy of &amp;lt;tt&amp;gt;debootstrap&amp;lt;/tt&amp;gt; running on your hardware node.&lt;br /&gt;
&lt;br /&gt;
For Debian:&lt;br /&gt;
 sudo apt-get install debootstrap&lt;br /&gt;
&lt;br /&gt;
For Gentoo:&lt;br /&gt;
 sudo emerge debootstrap&lt;br /&gt;
&lt;br /&gt;
For Fedora (at least Fedora 8 have it, not sure about earlier versions):&lt;br /&gt;
 sudo yum install debootstrap&lt;br /&gt;
&lt;br /&gt;
For other distros you might need to install it from sources, or search for an appropriate package for your distribution.  An RPM is available on the [http://forum.openvz.org/index.php?t=tree&amp;amp;th=142&amp;amp;mid=584 OpenVZ Forum].&lt;br /&gt;
&lt;br /&gt;
== Bootstrapping Debian ==&lt;br /&gt;
&lt;br /&gt;
You can install different releases of Debian into a VE's private directory using the debootstrap command.&lt;br /&gt;
&lt;br /&gt;
The command parameters are:&lt;br /&gt;
&lt;br /&gt;
  debootstrap --arch ARCH NAME DIRECTORY URL&lt;br /&gt;
&lt;br /&gt;
Specify your architecture instead of &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; if you're using something other than i386/x86.  For example, for AMD64/x86_64, use &amp;lt;tt&amp;gt;amd64&amp;lt;/tt&amp;gt; or for ia64, use &amp;lt;tt&amp;gt;ia64&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
We use VE ID of 777 for this example; surely it can be any other unused ID.&lt;br /&gt;
&lt;br /&gt;
=== Lenny (current Debian stable) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap --arch i386 lenny /vz/private/777 http://http.us.debian.org/debian/&lt;br /&gt;
&lt;br /&gt;
=== Etch (old release) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap --arch i386 etch /vz/private/777 http://http.us.debian.org/debian/&lt;br /&gt;
&lt;br /&gt;
=== Sarge (very old release) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap sarge /vz/private/777 http://archive.debian.org/debian&lt;br /&gt;
&lt;br /&gt;
== Preparing and starting the VE ==&lt;br /&gt;
&lt;br /&gt;
=== Setting VE config ===&lt;br /&gt;
First, we need a config for the [[VE]]:&lt;br /&gt;
 sudo vzctl set 777 --applyconfig vps.basic --save&lt;br /&gt;
&lt;br /&gt;
=== Setting VE OSTEMPLATE ===&lt;br /&gt;
Also, we need &amp;lt;tt&amp;gt;OSTEMPLATE&amp;lt;/tt&amp;gt; to be set in VE configuration file, for the [[vzctl]] to work properly.&lt;br /&gt;
&lt;br /&gt;
 sudo sh -c 'echo &amp;quot;OSTEMPLATE=debian-4.0&amp;quot; &amp;gt;&amp;gt; /etc/vz/conf/777.conf'&lt;br /&gt;
&lt;br /&gt;
=== Setting VE IP address ===&lt;br /&gt;
For the [[VE]] to be able to download updates from the Internet, we need a valid IP address for it:&lt;br /&gt;
 sudo vzctl set 777 --ipadd x.x.x.x --save&lt;br /&gt;
&lt;br /&gt;
{{Note|if you use private IP for the VE, you have to set up NAT as described in [[Using NAT for VE with private IPs]].}}&lt;br /&gt;
&lt;br /&gt;
=== Setting DNS server for VE ===&lt;br /&gt;
For the [[VE]] to be able to download updates from the Internet, we also need to specify a DNS for it:&lt;br /&gt;
 sudo vzctl set 777 --nameserver x.x.x.x --save&lt;br /&gt;
&lt;br /&gt;
=== Starting VE ===&lt;br /&gt;
Now start the VE:&lt;br /&gt;
 sudo vzctl start 777&lt;br /&gt;
&lt;br /&gt;
== Customizing the installation ==&lt;br /&gt;
A few things need to be done inside a newly created VE for it to become suitable for OpenVZ.  All those things are done inside the VE, so first command is:&lt;br /&gt;
 sudo vzctl enter 777&lt;br /&gt;
 export PATH=/sbin:/usr/sbin:/bin:/usr/bin&lt;br /&gt;
&lt;br /&gt;
{{Warning|Do not run the commands below on the hardware node, they are only to be run within the VE!}}&lt;br /&gt;
&lt;br /&gt;
=== Set Debian repositories ===&lt;br /&gt;
 cat &amp;lt;&amp;lt;EOF &amp;gt; /etc/apt/sources.list&lt;br /&gt;
 deb http://http.us.debian.org/debian etch main contrib&lt;br /&gt;
 deb http://security.debian.org etch/updates main contrib&lt;br /&gt;
 EOF&lt;br /&gt;
&lt;br /&gt;
=== Get new security updates ===&lt;br /&gt;
 apt-get update&lt;br /&gt;
 apt-get upgrade&lt;br /&gt;
&lt;br /&gt;
=== Install some more packages ===&lt;br /&gt;
Installing packages could be an interactive process so the system might ask some questions.  You can install more packages if you'd like.  For example:&lt;br /&gt;
 apt-get install ssh quota less&lt;br /&gt;
&lt;br /&gt;
=== Set sane permissions for &amp;lt;tt&amp;gt;/root&amp;lt;/tt&amp;gt; directory ===&lt;br /&gt;
 chmod 700 /root&lt;br /&gt;
&lt;br /&gt;
=== Disable root login ===&lt;br /&gt;
This will disable root login by default.&lt;br /&gt;
 usermod -L root&lt;br /&gt;
&lt;br /&gt;
=== Disable getty ===&lt;br /&gt;
Disable running &amp;lt;tt&amp;gt;getty&amp;lt;/tt&amp;gt;s on terminals as a VE does not have any:&lt;br /&gt;
 sed -i -e '/getty/d' /etc/inittab&lt;br /&gt;
&lt;br /&gt;
=== Disable &amp;lt;tt&amp;gt;sync()&amp;lt;/tt&amp;gt; for syslog ===&lt;br /&gt;
Turn off doing &amp;lt;tt&amp;gt;sync()&amp;lt;/tt&amp;gt; on every write for &amp;lt;tt&amp;gt;syslog&amp;lt;/tt&amp;gt;'s log files, to improve I/O performance:&lt;br /&gt;
 &amp;lt;pre&amp;gt;sed -i -e 's@\([[:space:]]\)\(/var/log/\)@\1-\2@' /etc/*syslog.conf&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Fix &amp;lt;tt&amp;gt;/etc/mtab&amp;lt;/tt&amp;gt; ===&lt;br /&gt;
Link &amp;lt;tt&amp;gt;/etc/mtab&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;/proc/mounts&amp;lt;/tt&amp;gt;, so &amp;lt;tt&amp;gt;df&amp;lt;/tt&amp;gt; and friends will work:&lt;br /&gt;
 rm -f /etc/mtab&lt;br /&gt;
 ln -s /proc/mounts /etc/mtab&lt;br /&gt;
&lt;br /&gt;
=== Remove some unneeded packages ===&lt;br /&gt;
If you have any packages you'd like to remove, now's the time for it.  Here's an example:&lt;br /&gt;
 dpkg --purge modutils ppp pppoeconf pppoe pppconfig&lt;br /&gt;
&lt;br /&gt;
=== Disable services ===&lt;br /&gt;
Do not start some services, stick to bare minimum:&lt;br /&gt;
 update-rc.d -f klogd remove&lt;br /&gt;
 update-rc.d -f quotarpc remove&lt;br /&gt;
 update-rc.d -f exim4 remove&lt;br /&gt;
 update-rc.d -f inetd remove&lt;br /&gt;
&lt;br /&gt;
=== Fix SSH host keys ===&lt;br /&gt;
This is only useful if you installed SSH.  Each individual [[VE]] should have its own pair of SSH host keys.  The code below will wipe out the existing SSH keys and instruct the newly-created [[VE]] to create new SSH keys on first boot.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- please do not remove &amp;lt;source&amp;gt;...&amp;lt;/source&amp;gt; pair of tags below,&lt;br /&gt;
     otherwise quotes after -N (-N '') are not visible --&amp;gt;&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
rm -f /etc/ssh/ssh_host_*&lt;br /&gt;
cat &amp;lt;&amp;lt; EOF &amp;gt; /etc/rc2.d/S15ssh_gen_host_keys&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
ssh-keygen -f /etc/ssh/ssh_host_rsa_key -t rsa -N ''&lt;br /&gt;
ssh-keygen -f /etc/ssh/ssh_host_dsa_key -t dsa -N ''&lt;br /&gt;
rm -f \$0&lt;br /&gt;
EOF&lt;br /&gt;
chmod a+x /etc/rc2.d/S15ssh_gen_host_keys&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Change timezone ===&lt;br /&gt;
&lt;br /&gt;
You might want to change timezone if you do not live in $UTC. The following example is for Germany&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ln -sf /usr/share/zoneinfo/Europe/Berlin /etc/localtime&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
or even better&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
dpkg-reconfigure tzdata&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Clean packages ===&lt;br /&gt;
After installing packages, you'll have some junk packages laying around in your cache.  Since you don't want your template to have those, this command will wipe them out.&lt;br /&gt;
 apt-get clean&lt;br /&gt;
&lt;br /&gt;
Now everything is done.  Exit from the template and go back to the hardware node.&lt;br /&gt;
 exit&lt;br /&gt;
&lt;br /&gt;
== Preparing for and packing template cache ==&lt;br /&gt;
&lt;br /&gt;
We don't need an IP for the VE anymore, and we definitely do not need it in template cache, so remove it:&lt;br /&gt;
 sudo vzctl set 777 --ipdel all --save&lt;br /&gt;
&lt;br /&gt;
Also, remove DNS server and search domain information from ''/etc/resolv.conf'' file '''in VE''':&lt;br /&gt;
 sudo nano /vz/private/777/etc/resolv.conf&lt;br /&gt;
&lt;br /&gt;
Also, remove ''/etc/hostname'' file '''in VE''':&lt;br /&gt;
 sudo rm -f /vz/private/777/etc/hostname&lt;br /&gt;
&lt;br /&gt;
Stop the VE:&lt;br /&gt;
 sudo vzctl stop 777&lt;br /&gt;
&lt;br /&gt;
Go to the VE directory:&lt;br /&gt;
 cd /vz/private/777&lt;br /&gt;
&lt;br /&gt;
Now create a cached OS tarball.  In the command below, you'll want to replace &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; with your architecture (i386, amd64, ia64, etc).&lt;br /&gt;
 sudo tar --numeric-owner -zcf /vz/template/cache/debian-4.0-i386-minimal.tar.gz .&lt;br /&gt;
&lt;br /&gt;
Look at the resulting tarball to see its size is sane:&lt;br /&gt;
 # ls -lh /vz/template/cache&lt;br /&gt;
 -rw-r--r--  1 root root  51M Apr 10 03:16 debian-4.0-i386-minimal.tar.gz&lt;br /&gt;
&lt;br /&gt;
== Checking if template cache works ==&lt;br /&gt;
We can now create a VE based on the just-created template cache.  Be sure to change &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; to your architecture just like you did when you named the tarball above.&lt;br /&gt;
 sudo vzctl create 123456 --ostemplate debian-4.0-i386-minimal&lt;br /&gt;
&lt;br /&gt;
Now make sure that it works:&lt;br /&gt;
 sudo vzctl start 123456&lt;br /&gt;
 sudo vzctl exec 123456 ps ax&lt;br /&gt;
&lt;br /&gt;
You should see that a few processes are running.&lt;br /&gt;
&lt;br /&gt;
== Final cleanup ==&lt;br /&gt;
Stop and remove the test VE you just created:&lt;br /&gt;
 sudo vzctl stop 123456&lt;br /&gt;
 sudo vzctl destroy 123456&lt;br /&gt;
 sudo rm /etc/vz/conf/123456.conf.destroyed&lt;br /&gt;
&lt;br /&gt;
Finally, let's remove the VE we used for OS template cache creation:&lt;br /&gt;
 sudo vzctl destroy 777&lt;br /&gt;
 sudo rm /etc/vz/conf/777.conf.destroyed&lt;br /&gt;
&lt;br /&gt;
[[Category: HOWTO]]&lt;br /&gt;
[[Category: Templates]]&lt;br /&gt;
[[Category: Debian]]&lt;/div&gt;</summary>
		<author><name>Mose</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Debian_template_creation&amp;diff=8308</id>
		<title>Debian template creation</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Debian_template_creation&amp;diff=8308"/>
		<updated>2010-03-12T02:22:15Z</updated>

		<summary type="html">&lt;p&gt;Mose: /* Customizing the installation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;These are rough instructions of how to manually create basic Debian Etch (4.0) template cache, which can be used to create OpenVZ [[VE]]s based on Debian Etch (4.0). (see also &amp;lt;tt&amp;gt;/usr/share/doc/vzctl/README.Debian&amp;lt;/tt&amp;gt; in the ''vzctl'' Debian package)&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* You shouldn't be running as root, but as a user that is permitted to use sudo instead.  It's a dangerous idea, run as root at your peril.&lt;br /&gt;
* Anywhere you see &amp;lt;tt&amp;gt;/vz&amp;lt;/tt&amp;gt;, you might really need to use &amp;lt;tt&amp;gt;/var/lib/vz&amp;lt;/tt&amp;gt; instead, especially on a Debian Etch host.&lt;br /&gt;
* Anywhere you see &amp;lt;tt&amp;gt;http://http.us.debian.org/debian/&amp;lt;/tt&amp;gt;, you can substitute your favorite Debian mirror.  ([http://www.debian.org/mirror/list List of official Debian Mirrors])&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Prerequisites ==&lt;br /&gt;
You need to have a working copy of &amp;lt;tt&amp;gt;debootstrap&amp;lt;/tt&amp;gt; running on your hardware node.&lt;br /&gt;
&lt;br /&gt;
For Debian:&lt;br /&gt;
 sudo apt-get install debootstrap&lt;br /&gt;
&lt;br /&gt;
For Gentoo:&lt;br /&gt;
 sudo emerge debootstrap&lt;br /&gt;
&lt;br /&gt;
For Fedora (at least Fedora 8 have it, not sure about earlier versions):&lt;br /&gt;
 sudo yum install debootstrap&lt;br /&gt;
&lt;br /&gt;
For other distros you might need to install it from sources, or search for an appropriate package for your distribution.  An RPM is available on the [http://forum.openvz.org/index.php?t=tree&amp;amp;th=142&amp;amp;mid=584 OpenVZ Forum].&lt;br /&gt;
&lt;br /&gt;
== Bootstrapping Debian ==&lt;br /&gt;
&lt;br /&gt;
You can install different releases of Debian into a VE's private directory using the debootstrap command.&lt;br /&gt;
&lt;br /&gt;
The command parameters are:&lt;br /&gt;
&lt;br /&gt;
  debootstrap --arch ARCH NAME DIRECTORY URL&lt;br /&gt;
&lt;br /&gt;
Specify your architecture instead of &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; if you're using something other than i386/x86.  For example, for AMD64/x86_64, use &amp;lt;tt&amp;gt;amd64&amp;lt;/tt&amp;gt; or for ia64, use &amp;lt;tt&amp;gt;ia64&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
We use VE ID of 777 for this example; surely it can be any other unused ID.&lt;br /&gt;
&lt;br /&gt;
=== Lenny (current Debian stable) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap --arch i386 lenny /vz/private/777 http://http.us.debian.org/debian/&lt;br /&gt;
&lt;br /&gt;
=== Etch (old release) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap --arch i386 etch /vz/private/777 http://http.us.debian.org/debian/&lt;br /&gt;
&lt;br /&gt;
=== Sarge (very old release) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap sarge /vz/private/777 http://archive.debian.org/debian&lt;br /&gt;
&lt;br /&gt;
== Preparing and starting the VE ==&lt;br /&gt;
&lt;br /&gt;
=== Setting VE config ===&lt;br /&gt;
First, we need a config for the [[VE]]:&lt;br /&gt;
 sudo vzctl set 777 --applyconfig vps.basic --save&lt;br /&gt;
&lt;br /&gt;
=== Setting VE OSTEMPLATE ===&lt;br /&gt;
Also, we need &amp;lt;tt&amp;gt;OSTEMPLATE&amp;lt;/tt&amp;gt; to be set in VE configuration file, for the [[vzctl]] to work properly.&lt;br /&gt;
&lt;br /&gt;
 sudo sh -c 'echo &amp;quot;OSTEMPLATE=debian-4.0&amp;quot; &amp;gt;&amp;gt; /etc/vz/conf/777.conf'&lt;br /&gt;
&lt;br /&gt;
=== Setting VE IP address ===&lt;br /&gt;
For the [[VE]] to be able to download updates from the Internet, we need a valid IP address for it:&lt;br /&gt;
 sudo vzctl set 777 --ipadd x.x.x.x --save&lt;br /&gt;
&lt;br /&gt;
{{Note|if you use private IP for the VE, you have to set up NAT as described in [[Using NAT for VE with private IPs]].}}&lt;br /&gt;
&lt;br /&gt;
=== Setting DNS server for VE ===&lt;br /&gt;
For the [[VE]] to be able to download updates from the Internet, we also need to specify a DNS for it:&lt;br /&gt;
 sudo vzctl set 777 --nameserver x.x.x.x --save&lt;br /&gt;
&lt;br /&gt;
=== Starting VE ===&lt;br /&gt;
Now start the VE:&lt;br /&gt;
 sudo vzctl start 777&lt;br /&gt;
&lt;br /&gt;
== Customizing the installation ==&lt;br /&gt;
A few things need to be done inside a newly created VE for it to become suitable for OpenVZ.  All those things are done inside the VE, so first command is:&lt;br /&gt;
 sudo vzctl enter 777&lt;br /&gt;
 export PATH=/sbin:/usr/sbin:/bin:/usr/bin&lt;br /&gt;
&lt;br /&gt;
{{Warning|Do not run the commands below on the hardware node, they are only to be run within the VE!}}&lt;br /&gt;
&lt;br /&gt;
=== Set Debian repositories ===&lt;br /&gt;
 cat &amp;lt;&amp;lt;EOF &amp;gt; /etc/apt/sources.list&lt;br /&gt;
 deb http://http.us.debian.org/debian etch main contrib&lt;br /&gt;
 deb http://security.debian.org etch/updates main contrib&lt;br /&gt;
 EOF&lt;br /&gt;
&lt;br /&gt;
=== Get new security updates ===&lt;br /&gt;
 apt-get update&lt;br /&gt;
 apt-get upgrade&lt;br /&gt;
&lt;br /&gt;
=== Install some more packages ===&lt;br /&gt;
Installing packages could be an interactive process so the system might ask some questions.  You can install more packages if you'd like.  For example:&lt;br /&gt;
 apt-get install ssh quota less&lt;br /&gt;
&lt;br /&gt;
=== Set sane permissions for &amp;lt;tt&amp;gt;/root&amp;lt;/tt&amp;gt; directory ===&lt;br /&gt;
 chmod 700 /root&lt;br /&gt;
&lt;br /&gt;
=== Disable root login ===&lt;br /&gt;
This will disable root login by default.&lt;br /&gt;
 usermod -L root&lt;br /&gt;
&lt;br /&gt;
=== Disable getty ===&lt;br /&gt;
Disable running &amp;lt;tt&amp;gt;getty&amp;lt;/tt&amp;gt;s on terminals as a VE does not have any:&lt;br /&gt;
 sed -i -e '/getty/d' /etc/inittab&lt;br /&gt;
&lt;br /&gt;
=== Disable &amp;lt;tt&amp;gt;sync()&amp;lt;/tt&amp;gt; for syslog ===&lt;br /&gt;
Turn off doing &amp;lt;tt&amp;gt;sync()&amp;lt;/tt&amp;gt; on every write for &amp;lt;tt&amp;gt;syslog&amp;lt;/tt&amp;gt;'s log files, to improve I/O performance:&lt;br /&gt;
 &amp;lt;pre&amp;gt;sed -i -e 's@\([[:space:]]\)\(/var/log/\)@\1-\2@' /etc/*syslog.conf&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Fix &amp;lt;tt&amp;gt;/etc/mtab&amp;lt;/tt&amp;gt; ===&lt;br /&gt;
Link &amp;lt;tt&amp;gt;/etc/mtab&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;/proc/mounts&amp;lt;/tt&amp;gt;, so &amp;lt;tt&amp;gt;df&amp;lt;/tt&amp;gt; and friends will work:&lt;br /&gt;
 rm -f /etc/mtab&lt;br /&gt;
 ln -s /proc/mounts /etc/mtab&lt;br /&gt;
&lt;br /&gt;
=== Remove some unneeded packages ===&lt;br /&gt;
If you have any packages you'd like to remove, now's the time for it.  Here's an example:&lt;br /&gt;
 dpkg --purge modutils ppp pppoeconf pppoe pppconfig&lt;br /&gt;
&lt;br /&gt;
=== Disable services ===&lt;br /&gt;
Do not start some services, stick to bare minimum:&lt;br /&gt;
 update-rc.d -f klogd remove&lt;br /&gt;
 update-rc.d -f quotarpc remove&lt;br /&gt;
 update-rc.d -f exim4 remove&lt;br /&gt;
 update-rc.d -f inetd remove&lt;br /&gt;
&lt;br /&gt;
=== Fix SSH host keys ===&lt;br /&gt;
This is only useful if you installed SSH.  Each individual [[VE]] should have its own pair of SSH host keys.  The code below will wipe out the existing SSH keys and instruct the newly-created [[VE]] to create new SSH keys on first boot.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- please do not remove &amp;lt;source&amp;gt;...&amp;lt;/source&amp;gt; pair of tags below,&lt;br /&gt;
     otherwise quotes after -N (-N '') are not visible --&amp;gt;&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
rm -f /etc/ssh/ssh_host_*&lt;br /&gt;
cat &amp;lt;&amp;lt; EOF &amp;gt; /etc/rc2.d/S15ssh_gen_host_keys&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
ssh-keygen -f /etc/ssh/ssh_host_rsa_key -t rsa -N ''&lt;br /&gt;
ssh-keygen -f /etc/ssh/ssh_host_dsa_key -t dsa -N ''&lt;br /&gt;
rm -f \$0&lt;br /&gt;
EOF&lt;br /&gt;
chmod a+x /etc/rc2.d/S15ssh_gen_host_keys&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Change timezone ===&lt;br /&gt;
&lt;br /&gt;
You might want to change timezone if you do not live in $UTC. The following example is for Germany&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ln -sf /usr/share/zoneinfo/Europe/Berlin /etc/localtime&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Clean packages ===&lt;br /&gt;
After installing packages, you'll have some junk packages laying around in your cache.  Since you don't want your template to have those, this command will wipe them out.&lt;br /&gt;
 apt-get clean&lt;br /&gt;
&lt;br /&gt;
Now everything is done.  Exit from the template and go back to the hardware node.&lt;br /&gt;
 exit&lt;br /&gt;
&lt;br /&gt;
== Preparing for and packing template cache ==&lt;br /&gt;
&lt;br /&gt;
We don't need an IP for the VE anymore, and we definitely do not need it in template cache, so remove it:&lt;br /&gt;
 sudo vzctl set 777 --ipdel all --save&lt;br /&gt;
&lt;br /&gt;
Also, remove DNS server and search domain information from ''/etc/resolv.conf'' file '''in VE''':&lt;br /&gt;
 sudo nano /vz/private/777/etc/resolv.conf&lt;br /&gt;
&lt;br /&gt;
Also, remove ''/etc/hostname'' file '''in VE''':&lt;br /&gt;
 sudo rm -f /vz/private/777/etc/hostname&lt;br /&gt;
&lt;br /&gt;
Stop the VE:&lt;br /&gt;
 sudo vzctl stop 777&lt;br /&gt;
&lt;br /&gt;
Go to the VE directory:&lt;br /&gt;
 cd /vz/private/777&lt;br /&gt;
&lt;br /&gt;
Now create a cached OS tarball.  In the command below, you'll want to replace &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; with your architecture (i386, amd64, ia64, etc).&lt;br /&gt;
 sudo tar --numeric-owner -zcf /vz/template/cache/debian-4.0-i386-minimal.tar.gz .&lt;br /&gt;
&lt;br /&gt;
Look at the resulting tarball to see its size is sane:&lt;br /&gt;
 # ls -lh /vz/template/cache&lt;br /&gt;
 -rw-r--r--  1 root root  51M Apr 10 03:16 debian-4.0-i386-minimal.tar.gz&lt;br /&gt;
&lt;br /&gt;
== Checking if template cache works ==&lt;br /&gt;
We can now create a VE based on the just-created template cache.  Be sure to change &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; to your architecture just like you did when you named the tarball above.&lt;br /&gt;
 sudo vzctl create 123456 --ostemplate debian-4.0-i386-minimal&lt;br /&gt;
&lt;br /&gt;
Now make sure that it works:&lt;br /&gt;
 sudo vzctl start 123456&lt;br /&gt;
 sudo vzctl exec 123456 ps ax&lt;br /&gt;
&lt;br /&gt;
You should see that a few processes are running.&lt;br /&gt;
&lt;br /&gt;
== Final cleanup ==&lt;br /&gt;
Stop and remove the test VE you just created:&lt;br /&gt;
 sudo vzctl stop 123456&lt;br /&gt;
 sudo vzctl destroy 123456&lt;br /&gt;
 sudo rm /etc/vz/conf/123456.conf.destroyed&lt;br /&gt;
&lt;br /&gt;
Finally, let's remove the VE we used for OS template cache creation:&lt;br /&gt;
 sudo vzctl destroy 777&lt;br /&gt;
 sudo rm /etc/vz/conf/777.conf.destroyed&lt;br /&gt;
&lt;br /&gt;
[[Category: HOWTO]]&lt;br /&gt;
[[Category: Templates]]&lt;br /&gt;
[[Category: Debian]]&lt;/div&gt;</summary>
		<author><name>Mose</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Debian_template_creation&amp;diff=8307</id>
		<title>Debian template creation</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Debian_template_creation&amp;diff=8307"/>
		<updated>2010-03-12T02:19:22Z</updated>

		<summary type="html">&lt;p&gt;Mose: /* Customizing the installation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;These are rough instructions of how to manually create basic Debian Etch (4.0) template cache, which can be used to create OpenVZ [[VE]]s based on Debian Etch (4.0). (see also &amp;lt;tt&amp;gt;/usr/share/doc/vzctl/README.Debian&amp;lt;/tt&amp;gt; in the ''vzctl'' Debian package)&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* You shouldn't be running as root, but as a user that is permitted to use sudo instead.  It's a dangerous idea, run as root at your peril.&lt;br /&gt;
* Anywhere you see &amp;lt;tt&amp;gt;/vz&amp;lt;/tt&amp;gt;, you might really need to use &amp;lt;tt&amp;gt;/var/lib/vz&amp;lt;/tt&amp;gt; instead, especially on a Debian Etch host.&lt;br /&gt;
* Anywhere you see &amp;lt;tt&amp;gt;http://http.us.debian.org/debian/&amp;lt;/tt&amp;gt;, you can substitute your favorite Debian mirror.  ([http://www.debian.org/mirror/list List of official Debian Mirrors])&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Prerequisites ==&lt;br /&gt;
You need to have a working copy of &amp;lt;tt&amp;gt;debootstrap&amp;lt;/tt&amp;gt; running on your hardware node.&lt;br /&gt;
&lt;br /&gt;
For Debian:&lt;br /&gt;
 sudo apt-get install debootstrap&lt;br /&gt;
&lt;br /&gt;
For Gentoo:&lt;br /&gt;
 sudo emerge debootstrap&lt;br /&gt;
&lt;br /&gt;
For Fedora (at least Fedora 8 have it, not sure about earlier versions):&lt;br /&gt;
 sudo yum install debootstrap&lt;br /&gt;
&lt;br /&gt;
For other distros you might need to install it from sources, or search for an appropriate package for your distribution.  An RPM is available on the [http://forum.openvz.org/index.php?t=tree&amp;amp;th=142&amp;amp;mid=584 OpenVZ Forum].&lt;br /&gt;
&lt;br /&gt;
== Bootstrapping Debian ==&lt;br /&gt;
&lt;br /&gt;
You can install different releases of Debian into a VE's private directory using the debootstrap command.&lt;br /&gt;
&lt;br /&gt;
The command parameters are:&lt;br /&gt;
&lt;br /&gt;
  debootstrap --arch ARCH NAME DIRECTORY URL&lt;br /&gt;
&lt;br /&gt;
Specify your architecture instead of &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; if you're using something other than i386/x86.  For example, for AMD64/x86_64, use &amp;lt;tt&amp;gt;amd64&amp;lt;/tt&amp;gt; or for ia64, use &amp;lt;tt&amp;gt;ia64&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
We use VE ID of 777 for this example; surely it can be any other unused ID.&lt;br /&gt;
&lt;br /&gt;
=== Lenny (current Debian stable) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap --arch i386 lenny /vz/private/777 http://http.us.debian.org/debian/&lt;br /&gt;
&lt;br /&gt;
=== Etch (old release) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap --arch i386 etch /vz/private/777 http://http.us.debian.org/debian/&lt;br /&gt;
&lt;br /&gt;
=== Sarge (very old release) ===&lt;br /&gt;
&lt;br /&gt;
 debootstrap sarge /vz/private/777 http://archive.debian.org/debian&lt;br /&gt;
&lt;br /&gt;
== Preparing and starting the VE ==&lt;br /&gt;
&lt;br /&gt;
=== Setting VE config ===&lt;br /&gt;
First, we need a config for the [[VE]]:&lt;br /&gt;
 sudo vzctl set 777 --applyconfig vps.basic --save&lt;br /&gt;
&lt;br /&gt;
=== Setting VE OSTEMPLATE ===&lt;br /&gt;
Also, we need &amp;lt;tt&amp;gt;OSTEMPLATE&amp;lt;/tt&amp;gt; to be set in VE configuration file, for the [[vzctl]] to work properly.&lt;br /&gt;
&lt;br /&gt;
 sudo sh -c 'echo &amp;quot;OSTEMPLATE=debian-4.0&amp;quot; &amp;gt;&amp;gt; /etc/vz/conf/777.conf'&lt;br /&gt;
&lt;br /&gt;
=== Setting VE IP address ===&lt;br /&gt;
For the [[VE]] to be able to download updates from the Internet, we need a valid IP address for it:&lt;br /&gt;
 sudo vzctl set 777 --ipadd x.x.x.x --save&lt;br /&gt;
&lt;br /&gt;
{{Note|if you use private IP for the VE, you have to set up NAT as described in [[Using NAT for VE with private IPs]].}}&lt;br /&gt;
&lt;br /&gt;
=== Setting DNS server for VE ===&lt;br /&gt;
For the [[VE]] to be able to download updates from the Internet, we also need to specify a DNS for it:&lt;br /&gt;
 sudo vzctl set 777 --nameserver x.x.x.x --save&lt;br /&gt;
&lt;br /&gt;
=== Starting VE ===&lt;br /&gt;
Now start the VE:&lt;br /&gt;
 sudo vzctl start 777&lt;br /&gt;
&lt;br /&gt;
== Customizing the installation ==&lt;br /&gt;
A few things need to be done inside a newly created VE for it to become suitable for OpenVZ.  All those things are done inside the VE, so first command is:&lt;br /&gt;
 sudo vzctl enter 777&lt;br /&gt;
 export PATH=/sbin:/usr/sbin:/bin:/usr/bin&lt;br /&gt;
&lt;br /&gt;
{{Warning|Do not run the commands below on the hardware node, they are only to be run within the VE!}}&lt;br /&gt;
&lt;br /&gt;
=== Set Debian repositories ===&lt;br /&gt;
 cat &amp;lt;&amp;lt;EOF &amp;gt; /etc/apt/sources.list&lt;br /&gt;
 deb http://http.us.debian.org/debian etch main contrib&lt;br /&gt;
 deb http://security.debian.org etch/updates main contrib&lt;br /&gt;
 EOF&lt;br /&gt;
&lt;br /&gt;
=== Get new security updates ===&lt;br /&gt;
 apt-get update&lt;br /&gt;
 apt-get upgrade&lt;br /&gt;
&lt;br /&gt;
=== Install some more packages ===&lt;br /&gt;
Installing packages could be an interactive process so the system might ask some questions.  You can install more packages if you'd like.  For example:&lt;br /&gt;
 apt-get install ssh quota less&lt;br /&gt;
&lt;br /&gt;
=== Set the timezone ===&lt;br /&gt;
 dpkg-reconfigure tzdata&lt;br /&gt;
&lt;br /&gt;
=== Set sane permissions for &amp;lt;tt&amp;gt;/root&amp;lt;/tt&amp;gt; directory ===&lt;br /&gt;
 chmod 700 /root&lt;br /&gt;
&lt;br /&gt;
=== Disable root login ===&lt;br /&gt;
This will disable root login by default.&lt;br /&gt;
 usermod -L root&lt;br /&gt;
&lt;br /&gt;
=== Disable getty ===&lt;br /&gt;
Disable running &amp;lt;tt&amp;gt;getty&amp;lt;/tt&amp;gt;s on terminals as a VE does not have any:&lt;br /&gt;
 sed -i -e '/getty/d' /etc/inittab&lt;br /&gt;
&lt;br /&gt;
=== Disable &amp;lt;tt&amp;gt;sync()&amp;lt;/tt&amp;gt; for syslog ===&lt;br /&gt;
Turn off doing &amp;lt;tt&amp;gt;sync()&amp;lt;/tt&amp;gt; on every write for &amp;lt;tt&amp;gt;syslog&amp;lt;/tt&amp;gt;'s log files, to improve I/O performance:&lt;br /&gt;
 &amp;lt;pre&amp;gt;sed -i -e 's@\([[:space:]]\)\(/var/log/\)@\1-\2@' /etc/*syslog.conf&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Fix &amp;lt;tt&amp;gt;/etc/mtab&amp;lt;/tt&amp;gt; ===&lt;br /&gt;
Link &amp;lt;tt&amp;gt;/etc/mtab&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;/proc/mounts&amp;lt;/tt&amp;gt;, so &amp;lt;tt&amp;gt;df&amp;lt;/tt&amp;gt; and friends will work:&lt;br /&gt;
 rm -f /etc/mtab&lt;br /&gt;
 ln -s /proc/mounts /etc/mtab&lt;br /&gt;
&lt;br /&gt;
=== Remove some unneeded packages ===&lt;br /&gt;
If you have any packages you'd like to remove, now's the time for it.  Here's an example:&lt;br /&gt;
 dpkg --purge modutils ppp pppoeconf pppoe pppconfig&lt;br /&gt;
&lt;br /&gt;
=== Disable services ===&lt;br /&gt;
Do not start some services, stick to bare minimum:&lt;br /&gt;
 update-rc.d -f klogd remove&lt;br /&gt;
 update-rc.d -f quotarpc remove&lt;br /&gt;
 update-rc.d -f exim4 remove&lt;br /&gt;
 update-rc.d -f inetd remove&lt;br /&gt;
&lt;br /&gt;
=== Fix SSH host keys ===&lt;br /&gt;
This is only useful if you installed SSH.  Each individual [[VE]] should have its own pair of SSH host keys.  The code below will wipe out the existing SSH keys and instruct the newly-created [[VE]] to create new SSH keys on first boot.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- please do not remove &amp;lt;source&amp;gt;...&amp;lt;/source&amp;gt; pair of tags below,&lt;br /&gt;
     otherwise quotes after -N (-N '') are not visible --&amp;gt;&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
rm -f /etc/ssh/ssh_host_*&lt;br /&gt;
cat &amp;lt;&amp;lt; EOF &amp;gt; /etc/rc2.d/S15ssh_gen_host_keys&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
ssh-keygen -f /etc/ssh/ssh_host_rsa_key -t rsa -N ''&lt;br /&gt;
ssh-keygen -f /etc/ssh/ssh_host_dsa_key -t dsa -N ''&lt;br /&gt;
rm -f \$0&lt;br /&gt;
EOF&lt;br /&gt;
chmod a+x /etc/rc2.d/S15ssh_gen_host_keys&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Change timezone ===&lt;br /&gt;
&lt;br /&gt;
You might want to change timezone if you do not live in $UTC. The following example is for Germany&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ln -sf /usr/share/zoneinfo/Europe/Berlin /etc/localtime&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Clean packages ===&lt;br /&gt;
After installing packages, you'll have some junk packages laying around in your cache.  Since you don't want your template to have those, this command will wipe them out.&lt;br /&gt;
 apt-get clean&lt;br /&gt;
&lt;br /&gt;
Now everything is done.  Exit from the template and go back to the hardware node.&lt;br /&gt;
 exit&lt;br /&gt;
&lt;br /&gt;
== Preparing for and packing template cache ==&lt;br /&gt;
&lt;br /&gt;
We don't need an IP for the VE anymore, and we definitely do not need it in template cache, so remove it:&lt;br /&gt;
 sudo vzctl set 777 --ipdel all --save&lt;br /&gt;
&lt;br /&gt;
Also, remove DNS server and search domain information from ''/etc/resolv.conf'' file '''in VE''':&lt;br /&gt;
 sudo nano /vz/private/777/etc/resolv.conf&lt;br /&gt;
&lt;br /&gt;
Also, remove ''/etc/hostname'' file '''in VE''':&lt;br /&gt;
 sudo rm -f /vz/private/777/etc/hostname&lt;br /&gt;
&lt;br /&gt;
Stop the VE:&lt;br /&gt;
 sudo vzctl stop 777&lt;br /&gt;
&lt;br /&gt;
Go to the VE directory:&lt;br /&gt;
 cd /vz/private/777&lt;br /&gt;
&lt;br /&gt;
Now create a cached OS tarball.  In the command below, you'll want to replace &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; with your architecture (i386, amd64, ia64, etc).&lt;br /&gt;
 sudo tar --numeric-owner -zcf /vz/template/cache/debian-4.0-i386-minimal.tar.gz .&lt;br /&gt;
&lt;br /&gt;
Look at the resulting tarball to see its size is sane:&lt;br /&gt;
 # ls -lh /vz/template/cache&lt;br /&gt;
 -rw-r--r--  1 root root  51M Apr 10 03:16 debian-4.0-i386-minimal.tar.gz&lt;br /&gt;
&lt;br /&gt;
== Checking if template cache works ==&lt;br /&gt;
We can now create a VE based on the just-created template cache.  Be sure to change &amp;lt;tt&amp;gt;i386&amp;lt;/tt&amp;gt; to your architecture just like you did when you named the tarball above.&lt;br /&gt;
 sudo vzctl create 123456 --ostemplate debian-4.0-i386-minimal&lt;br /&gt;
&lt;br /&gt;
Now make sure that it works:&lt;br /&gt;
 sudo vzctl start 123456&lt;br /&gt;
 sudo vzctl exec 123456 ps ax&lt;br /&gt;
&lt;br /&gt;
You should see that a few processes are running.&lt;br /&gt;
&lt;br /&gt;
== Final cleanup ==&lt;br /&gt;
Stop and remove the test VE you just created:&lt;br /&gt;
 sudo vzctl stop 123456&lt;br /&gt;
 sudo vzctl destroy 123456&lt;br /&gt;
 sudo rm /etc/vz/conf/123456.conf.destroyed&lt;br /&gt;
&lt;br /&gt;
Finally, let's remove the VE we used for OS template cache creation:&lt;br /&gt;
 sudo vzctl destroy 777&lt;br /&gt;
 sudo rm /etc/vz/conf/777.conf.destroyed&lt;br /&gt;
&lt;br /&gt;
[[Category: HOWTO]]&lt;br /&gt;
[[Category: Templates]]&lt;br /&gt;
[[Category: Debian]]&lt;/div&gt;</summary>
		<author><name>Mose</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Postgresql_and_shared_memory&amp;diff=8211</id>
		<title>Postgresql and shared memory</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Postgresql_and_shared_memory&amp;diff=8211"/>
		<updated>2010-02-14T04:06:50Z</updated>

		<summary type="html">&lt;p&gt;Mose: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;When running PostgreSQL or Oracle XE (APEX), some nuances of shared memory may arise.&lt;br /&gt;
&lt;br /&gt;
==== PostgreSQL ====&lt;br /&gt;
One of the easiest ways to increase Postgres's performance is to turn up the shared_buffers parameter in the postgresql.conf file. This is basically the amount of shared memory which the postmaster will use for buffering everything: table data, indexes, etc. The default value is really small, and if you have RAM to spare you may want to crank it up to 128MB (16384 shared buffers, for pgsql 8.1 and earlier) or even more. After changing shared_buffers, it might complain that it couldn't allocate the shared memory, even though plenty of memory exists.&lt;br /&gt;
&lt;br /&gt;
==== Oracle XE ====&lt;br /&gt;
A similar situation may occur when installing Oracle XE. The installation appears to succeed, but only the listener is running. No database instance is running. The database directory (/usr/lib/oracle/xe/oradata/XE) is empty. The log file /usr/lib/oracle/xe/app/oracle/admin/XE/bdump/alert_XE.log contains only a couple of lines that indicate success (but there should be many lines of output). Files in /usr/lib/oracle/xe/app/oracle/admin/XE/udump have an odd error &amp;quot;skgm warning: ENOSPC creating segment of size...&amp;quot; and suggest a change to the shared memory configuration, even though the shared memory values are what Oracle recommends. During installation, Oracle will add entries to /etc/sysctl.conf to set shared memory parameters.&lt;br /&gt;
&lt;br /&gt;
==== Shared Memory ====&lt;br /&gt;
There are two things that control shared memory in a container.&lt;br /&gt;
&lt;br /&gt;
1. The shmpages setting for this container (check [[UBC]]). This dictates how many pages (one page is usually 4K, see [[memory page]] for more details) are available to the container, e.g. shmpages=16384 gives a limit of 64 MB of shared memory.&lt;br /&gt;
&lt;br /&gt;
2. &amp;quot;/sbin/sysctl kernel.shmmax&amp;quot;  This is the container's self-imposed limit in bytes of how much shared memory may be allocated in a single request. You may check the shared memory configuration with &amp;quot;ipcs -l&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
The HN imposes a limit on the container's total shared memory usage through shmpages, and the container itself imposes a limit on the container's total shared memory usage through kernel.shmmax. If the size of shmpages is less than kernel.shmmax, the database will not be able to allocate sufficient memory.&lt;br /&gt;
&lt;br /&gt;
==== Notes ====&lt;br /&gt;
* Shared memory is taken from the container's overall memory allocation. It is not a second memory pool.&lt;br /&gt;
&lt;br /&gt;
* Other processes may be using shared memory, and shmpages includes things other than IPC shared memory (tmpfs, shmem, etc.). If this is the case for your container, set shmpages higher than the database requires.&lt;br /&gt;
&lt;br /&gt;
* The sysctl kernel.shmmax value set in the [[HN]]/[[CT0]] applies only to the [[HN]], not to [[container]]s.&lt;br /&gt;
&lt;br /&gt;
[[Category:FAQ]]&lt;br /&gt;
[[Category:Kernel]]&lt;br /&gt;
[[Category:Troubleshooting]]&lt;/div&gt;</summary>
		<author><name>Mose</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Oracle_XE_(APEX)_and_shared_memory&amp;diff=8210</id>
		<title>Oracle XE (APEX) and shared memory</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Oracle_XE_(APEX)_and_shared_memory&amp;diff=8210"/>
		<updated>2010-02-14T03:14:03Z</updated>

		<summary type="html">&lt;p&gt;Mose: Redirected page to Postgresql and shared memory&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;#REDIRECT:[[Postgresql_and_shared_memory]]&lt;br /&gt;
&lt;br /&gt;
[[Category:FAQ]]&lt;br /&gt;
[[Category:Kernel]]&lt;br /&gt;
[[Category:Troubleshooting]]&lt;/div&gt;</summary>
		<author><name>Mose</name></author>
		
	</entry>
</feed>