<?xml version='1.0' encoding="UTF-8"?>
<!-- $Header: /var/cvsroot/gentoo/xml/htdocs/proj/en/vps/vserver-howto.xml,v 1.2 2006/10/21 02:46:38 hollow Exp $ -->
<!DOCTYPE guide SYSTEM "/dtd/guide.dtd">

<guide link="/doc/en/vserver-howto.xml">
<title>Gentoo Linux-VServer Howto</title>

<author title="Author">
  <mail link="hollow@gentoo.org">Benedikt Boehm</mail>
</author>
<author title="Editor">
  <mail link="fox2mike@gentoo.org">Shyam Mani</mail>
</author>

<abstract>
In this Howto you will learn to setup a basic virtual server using the
Linux-VServer Technology
</abstract>

<!-- The content of this document is licensed under the CC-BY-SA license -->
<!-- See http://creativecommons.org/licenses/by-sa/2.5 -->
<license/>

<version>1.6</version>
<date>2006-06-19</date>

<chapter>
<title>Introduction</title>
<section>
<title>The Linux-VServer Concept</title>
<body>

<p>
The basic concept of the Linux-VServer solution is to separate the user-space
environment into distinct units (sometimes called Virtual Private Servers) in
such a way that each VPS looks and feels like a real server to the processes
contained within.
</p>

</body>
</section>
<section>
<title>Terms used in this Howto</title>
<body>

<table>
<tr>
  <th>Term</th>
  <th>Description</th>
</tr>
<tr>
  <th>Linux-VServer, VServer</th>
  <ti>
    Linux-VServer is the official name of the project and used in this Howto
    the same way
  </ti>
</tr>
<tr>
  <th>virtual server, vserver, guest system</th>
  <ti>
    All these are interchangable and refer to one instance of a server (i.e.
    one virtual server)
  </ti>
</tr>
<tr>
  <th>host system, host</th>
  <ti>
    The physical machine running your Gentoo Linux will host all virtual
    servers
  </ti>
</tr>
<tr>
  <th>util-vserver</th>
  <ti>
    The <c>util-vserver</c> package contains all programs necessary for
    maintaining your virtual servers
  </ti>
</tr>
</table>

</body>
</section>
</chapter>

<chapter>
<title>Host configuration</title>
<section>
<title>Install a VServer kernel</title>
<body>

<pre caption="Install vserver-sources">
# <i>emerge vserver-sources</i>
</pre>

<p>
After the vserver-sources are installed it's time to configure them using
<c>make menuconfig</c>.
</p>

<pre caption="Configure vserver-sources">
# <i>cd /usr/src/linux-&lt;KERNELVERSION&gt;-vserver-&lt;VSERVERVERSION&gt;</i>
# <i>make menuconfig</i>

Linux VServer ---&gt;
  [ ] Enable Legacy Kernel API
  [*] Disable Legacy Networking Kernel API
<comment>(Highly recommended)</comment>
  [*] Enable Proc Security
  [*] Enable Hard CPU Limits
  Persistent Inode Context Tagging (UID24/GID24)  ---&gt;
  [ ] Tag NFSD User Auth and Files
  [ ] Compile Debugging Code
</pre>

<note>
If you are using reiserfs as filesystem on the partition where guest images are
stored, you will need to enable extended attributes for reiserfs in your
kernel config and additionally add the <c>attrs</c> option in
<path>/etc/fstab</path>.
</note>

<pre caption="Configure reiserfs options">
File systems  --->
  &lt;*&gt; Reiserfs support
  [*]   ReiserFS extended attributes
</pre>

<pre caption="Example fstab with extended attributes">
/dev/hdb1 /vservers reiserfs noatime,attrs 0 0
</pre>

<p>
After you've built and installed the kernel, update your boot loader and
finally reboot to see if the kernel boots correctly.
</p>

<pre caption="Install the kernel">
<comment>(Building the kernel)</comment>
# <i>make</i>
<comment>(Installing)</comment>
# <i>make modules_install</i>
# <i>cp arch/&lt;arch&gt;/boot/bzImage /boot/kernel-&lt;KERNELVERSION&gt;-vserver-&lt;VSERVERVERSION&gt;</i>
<comment>(Edit bootloader config file as required and)</comment>
# <i>reboot</i>
</pre>

</body>
</section>
<section>
<title>Setup host environment</title>
<body>

<p>
To maintain your virtual servers you need the util-vserver package which
contains all necessary programs and many useful features. Be aware that there
is also a package called <c>vserver-utils</c>. It is a new incarnation of the
user-space implementation of the VServer kernel API, but not yet usable for
production use.
</p>

<note>
This version of the howto requires features only available in
<c>&gt;=sys-apps/util-vserver-0.30.210-r6</c>. You may have to keyword/upgrade
it first.
</note>

<pre caption="Install util-vserver">
# <i>emerge util-vserver</i>
</pre>

<p>
You have to run the <c>vprocunhide</c> command after every reboot in order to
setup <path>/proc</path> permissions correctly for vserver use. An init script
has been installed by util-vserver. To use it you should add it to a runlevel:
</p>

<pre caption="vserver init script">
# <i>rc-update add vservers default</i>
# <i>/etc/init.d/vservers start</i>
</pre>

<note>
This init script will also start your default vservers. Read below for more
information.
</note>

<p>
The vshelper script is used to restart virtual servers correctly. You
have to tell the kernel where the vshelper script is located:
</p>

<pre caption="vshelper setup">
# <i>echo 'kernel.vshelper = /usr/lib/util-vserver/vshelper' &gt;&gt; /etc/sysctl.conf</i>
# <i>sysctl -p</i>
</pre>

</body>
</section>
</chapter>

<chapter>
<title>Creating a guest template</title>
<section>
<title>Install a vserver stage3</title>
<body>

<p>
You have to download a vserver prepared stage3 from one of
<uri link="/main/en/mirrors.xml">our mirrors</uri>. Those stages are located
under the <path>experimental/&lt;arch&gt;/vserver/</path> directory.
Then use the <c>vserver-new</c> script to create the initial guest.
</p>

<p>
You have to choose a context ID for your vserver (dynamic context IDs are
discouraged) as well as the necessary network device information (In this
example eth0 is configured with 192.168.1.253/24 and the context ID is
equivalent to the last two parts of the virtual servers IP).
</p>

<note>
The context ID should be 1 &lt; ID &lt; 49152.
</note>

<pre caption="Start stage3 installation">
# <i>vserver-new gentoo-template \</i>
  <i>--hostname gentoo \</i>
  <i>--context 1253 \</i>
  <i>--interface eth0:192.168.1.253/24 \</i>
  <i>stage3 /path/to/stage3-&lt;arch&gt;-&lt;date&gt;.tar.bz2 &lt;arch&gt;</i>
</pre>

<note>
To reflect your network settings you should change
<path>/etc/conf.d/hostname</path>, <path>/etc/conf.d/domainname</path> and
<path>/etc/hosts</path> inside the guest to your needs. See <uri
link="/doc/en/handbook/handbook-x86.xml?part=1&amp;chap=8#doc_chap2_sect1">chapter
8.b.1</uri> and <uri
link="/doc/en/handbook/handbook-x86.xml?part=1&amp;chap=8#doc_chap2_sect4">chapter
8.b.4</uri>. The rest of your virtual servers network setup will be
done on the host.
</note>

</body>
</section>
<section>
<title>Create the template tarball</title>
<body>

<p>
You don't have to build a stage3 tarball for every guest. The <c>vserver-new</c>
script supports templates. In order to use them you have to create a tarball of
your vserver installation:
</p>

<pre caption="Create template tarball">
# <i>cd /vservers/gentoo-template</i>
# <i>tar cjvpf ../gentoo-template-&lt;arch&gt;-&lt;date&gt;.tar.bz2 ./</i>
</pre>

<p>
To create a new guest using this tarball just use <c>vserver-new</c>:
</p>

<pre caption="Use template tarball">
# <i>cd /vservers</i>
# <i>vserver-new myguest \</i>
  <i>--hostname myguest \</i>
  <i>--context 1252 \</i>
  <i>--interface eth0:192.168.1.252/24 \</i>
  <i>template ./gentoo-template-&lt;arch&gt;-&lt;date&gt;.tar.bz2 &lt;arch&gt;</i>
</pre>

<note>
You should use a shared portage tree to keep guests small in size. Edit
<path>/etc/vservers/myguest/fstab</path> and uncomment the entries
for <path>/usr/portage</path> and <path>/usr/portage/distfiles</path>. Guests
will have read-only access to <path>/usr/portage</path> and read-write access
to <path>/usr/portage/distfiles</path>.
</note>

</body>
</section>
<section>
<title>Test the virtual server</title>
<body>

<p>
You should be able to start and enter the vserver by using the commands below.
If you miss commands like <c>mount</c> or <c>dmesg</c> you should <c>emerge
util-linux</c> inside your virtual server as the vserver profile doesn't ship
this package by default.
</p>

<pre caption="Test the virtual server">
# <i>vserver myguest start</i>
# <i>vserver-stat</i>
CTX   PROC    VSZ    RSS  userTIME   sysTIME    UPTIME NAME
0       90   1.4G 153.4K  14m00s11   6m45s17   2h59m59 root server
1252     2     3M   286    0m00s45   0m00s42   0m02s91 myguest
# <i>vserver myguest enter</i>
# <i>ps ax</i>
PID   TTY      STAT   TIME COMMAND
    1 ?        S      0:00 init [3]
22887 ?        Ss     0:00 /usr/sbin/syslog-ng
20496 pts/0    S      0:00 /bin/bash -login
20508 pts/0    R+     0:00 ps ax
# <i>logout</i>
</pre>

</body>
</section>
</chapter>

<chapter>
<title>Maintenance made easy</title>
<section>
<title>Start guests on boot</title>
<body>

<p>
You can start certain guests during boot. Each guest can be assigned a MARK.
Now everything you have to do is configure these MARKs in the guests
configuration and tell the init script to run all MARKed guests.
</p>

<pre caption="Configure MARKs for each guest">
<comment>(Do this for every guest you want to start)</comment>
# <i>mkdir -p /etc/vservers/myguest/apps/init</i>
# <i>echo "default" > /etc/vservers/myguest/apps/init/mark</i>
</pre>

<note>
Since all guests marked with "default" are started by default, nothing more has
to be done. If you have different marks you should also update
<path>/etc/conf.d/vservers</path>.
</note>

</body>
</section>
<section>
<title>Gentoo goodies</title>
<body>

<p>
The util-vserver ebuild has installed some goodies for Gentoo guests. You know
<c>vserver-new</c> from the previous chapter. In this chapter, you will learn
how to use <c>vesync</c> and <c>vemerge</c> to keep portage in sync as well as
<c>vupdateworld</c> and <c>vdispatch-conf</c> to update guests.
</p>

</body>
</section>
<section>
<title>Keep portage in sync</title>
<body>

<p>
The script <c>vesync</c> will help you to keep the metadata cache and overlays
in sync. <c>vemerge</c> is a simple wrapper for <c>emerge</c> in guests.
</p>

<pre caption="Examples">
<comment>(Sync metadata for 'myguest')</comment>
# <i>vesync myguest</i>
<comment>(Sync metadata for all guests)</comment>
# <i>vesync -a</i>
<comment>(Sync metadata for all guests except 'myguest')</comment>
# <i>vesync -a -e myguest</i>
<comment>(Sync 'myoverlay' for all guests)</comment>
# <i>vesync -a \</i>
  <i>--overlay /usr/local/overlays/myoverlay \</i>
  <i>--overlay-host rsync://rsync.myhost.com/myoverlay \</i>
  <i>--overlay-only</i>
<comment>(emerge app-editors/vim in 'myguest')</comment>
# <i>vemerge myguest app-editors/vim -va</i>
</pre>

<note>
This script requires you to have a shared portage tree.
</note>

</body>
</section>
<section>
<title>Update guests</title>
<body>

<p>
Gentoo guests can share packages to save compilation time. In order to use
shared packages, you have to create a central directory for packages on the
host.  We will use <path>/var/cache/vpackages</path> on the host and mount it
to <path>/usr/portage/packages</path> in every guest.
</p>

<pre caption="Add bind mount to guest configuration">
# <i>mkdir -p /var/cache/vpackages</i>
# <i>$EDITOR /etc/vservers/myguest/fstab</i>
<comment>(Add this line at the end)</comment>
/var/cache/vpackages /usr/portage/packages none bind,rw 0 0
</pre>

<p>
Now you can use <c>vupdateworld</c> to update every guest. The command is
equivalent to something like <c>emerge --deep --update --newuse world</c>
depending on command line options.
</p>

<pre caption="vupdateworld examples">
<comment>(Pretend update for 'myguest')</comment>
# <i>vupdateworld -p myguest</i>
<comment>(Update 'myguest' using binary packages)</comment>
# <i>vupdateworld -k myguest</i>
<comment>(Update all guests using binary packages)</comment>
# <i>vupdateworld -ka</i>
</pre>

<note>
In order to get binary packages you can either use PORTAGE_BINHOST (see <c>man
make.conf</c>) or set FEATURES="buildpkg" in one or more guests.
</note>

<p>
After a successful update you can easily update all configuration files with
<c>vdispatch-conf</c>. It is a simple wrapper for <c>dispatch-conf</c> and
behaves exactly the same.
</p>

<pre caption="vdispatch-conf examples">
<comment>(Update configuration files for 'myguest')</comment>
# <i>vdispatch-conf myguest</i>
<comment>(Update configuration files for all guests)</comment>
# <i>vdispatch-conf -a</i>
</pre>

</body>
</section>

<section>
<title>Contact</title>
<body>

<p>
Please feel free to contact the <mail link="hollow@gentoo.org">author</mail> or
file a bug on <uri link="http://bugs.gentoo.org">Bugzilla</uri> in case of any
problems.
</p>

</body>
</section>
</chapter>
</guide>
