<?xml version='1.0' encoding="UTF-8"?>
<!-- $Header: /var/cvsroot/gentoo/xml/htdocs/proj/en/vps/vserver-howto.xml,v 1.8 2007/03/26 18:50:21 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.10</version>
<date>2007-03-26</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. Two init
scripts have been installed by util-vserver which run the <c>vprocunhide</c>
command for you and take care of virtual servers during shutdown of the host.
</p>

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

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

<chapter>
<title>Guest creation</title>
<section>
<title>Download a precompiled stage3/4</title>
<body>

<p>
Since many hardware related commands are not available inside a virtual server,
there has been a patched version of baselayout known as baselayout-vserver.
However, since baselayout-1.13.0_alpha12, all required changes have been
integrated into normal baselayout, eliminating the need for seperate vserver
stages, profiles and baselayout. The only (temporary) drawback is that
baselayout-1.13 is still considered to be in alpha stage and there are no
stages with baselayout-1.13 available on the mirrors yet.
</p>

<p>
As soon as baselayout-1.13 is stable you can use a precompiled stage3/4 from
one of <uri link="/main/en/mirrors.xml">our mirrors</uri>. In the meantime
please download a stage3/4 from
<uri link="http://people.linux-vserver.org/~hollow/stages/">here</uri>.
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>

</body>
</section>
<section>
<title>Using the template build method</title>
<body>

<p>
For a long time now, plain init style was the only init style available for
gentoo, i.e. a normal init process will be started inside the guest, just like
on any common Unix system. However this approach has some drawbacks:
</p>

<ul>
<li>No possibility to see output of init/rc scripts</li>
<li>Wasted resources for idle init processes in each guest</li>
<li>Annoying conflicts for <path>/etc/inittab</path></li>
</ul>

<p>
Therefore, many users have requested to reimplement the gentoo init style,
which has been abandonned since it was a very hacky implementation and more
or less worked by accident due to other modifications done to baselayout back
then. However, as of util-vserver-0.30.212 the gentoo init style has been
reimplemented in a concise manner and will get the default in the future.
</p>

<note>
If there is not a good reason for using an extra init process for each guest
or if you don't know what to do here, you should stick with gentoo init style.
</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>--initstyle gentoo \</i> <comment>(replace if needed)</comment>
  <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>


Gentoo/Linux 1.13.0_alpha12; http://www.gentoo.org/
 Copyright 1999-2007 Gentoo Foundation; Distributed under the GPLv2

Press I to enter interactive boot mode

 * Using existing device nodes in /dev                    [ ok ]
 * root filesystem is mounted read-write - skipping
 * Checking all filesystems                               [ ok ]
 * Mounting local filesystems                             [ ok ]
 * Activating (possible) swap                             [ ok ]
 * Setting hostname to myguest                            [ ok ]
 * Updating environment                                   [ ok ]
 * Cleaning /var/lock, /var/run                           [ ok ]
 * Cleaning /tmp directory                                [ ok ]
 * Initializing random number generator                   [ ok ]
 * Setting system clock using the hardware clock [VPS]    [ ok ]
 * Starting syslog-ng                                     [ ok ]
 * Starting vixie-cron                                    [ ok ]
 * Starting local                                         [ ok ]
# <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>
