<?xml version='1.0' encoding="UTF-8"?>
<!-- $Header: /var/cvsroot/gentoo/xml/htdocs/proj/en/vps/vserver-howto.xml,v 1.6 2007/01/21 14:12:12 hollow Exp $ -->
<!DOCTYPE guide SYSTEM "/dtd/guide.dtd">

<guide link="/proj/en/vps/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.8</version>
<date>2007-01-21</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>.. Below is a common configuration for 2.1.1 and above. If
you are using 2.0.x some configuration options may not be present.
</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;
<comment>(Do not enable the legacy options)</comment>
  [ ] Enable Legacy Kernel API
  [ ] Enable Legacy Networking Kernel API
<comment>(Read help text)</comment>
  [ ] Remap Source IP Address
  [*] Enable COW Immutable Link Breaking
  [ ] Enable Virtualized Guest Time
  [*] Enable Proc Security
  [*] Enable Hard CPU Limits
  [*]   Avoid idle CPUs by skipping Time
  [*]   Limit the IDLE task
      Persistent Inode Tagging (UID24/GID24)  ---&gt;
  [ ] Tag NFSD User Auth and Files
  [*] Enable Inode Tag Propagation
  [*] Honor Privacy Aspects of Guests
  [ ] VServer 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.
</p>

<pre caption="Install util-vserver">
# <i>emerge >=sys-cluster/util-vserver-0.30.212</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 guests. An init
script has been installed by util-vserver. To use it you should add it to the
default runlevel:
</p>

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

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

<chapter>
<title>Guest creation</title>
<section>
<title>Using vserver stage3/4</title>
<body>

<p>
You have to download a vserver prepared stage3/4 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.
Since a stage3/4 contains a complete root filesystem you can use the template
build method of util-vserver. However, this method is only available since
util-vserver-0.30.211, so make sure you have the right version installed.
</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 myguest build \</i>
  <i>--context 1253 \</i>
  <i>--hostname gentoo \</i>
  <i>--interface eth0:192.168.1.253/24 \</i>
  <i>-m template -- \</i>
  <i>  -d gentoo \</i>
  <i>  -t /path/to/stage4-&lt;arch&gt;-&lt;version&gt;.tar.bz2</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>

<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 add the approriate init scripts to the default runlevel.
</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>

<pre caption="Add init script to the default runlevel">
# <i>rc-update add vservers.default default</i>
</pre>

</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 --all</i>
<comment>(Sync 'myoverlay' for all guests)</comment>
# <i>vesync --all \</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>

</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 myguest -- -vp</i>
<comment>(Update 'myguest' using binary packages)</comment>
# <i>vupdateworld myguest -- -k</i>
<comment>(Update all guests using binary packages)</comment>
# <i>vupdateworld --all -- -k</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 --all</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>
