How To Install Gentoo Onto Any OpenVZ VPS

2012-11-17 19:46 - Linux

I recently found a great deal on an OpenVZ VPS after lurking on lowendbox.com for a while. At least for a while I plan on mostly using it for backup purposes while I judge its reliability. But I want to run Gentoo on it! This isn't perfectly straightforward. OpenVZ is based on OS "templates" and they provide some precreated ones. The only Gentoo options are contribs, and as far as I can tell, they don't work. I spent a while working on my own which mostly works, but has a few niggling issues that I couldn't work out completely, at least at first.

Then I remembered reading about installing Gentoo from another distribution. Existing tutorials like that assume partitioning, which makes no sense in OpenVZ. But it was the catalyst to help me realize: all I need is the right files in the right layout! So I gave it a shot and it worked. I used a lot of information from the Gentoo template creation wiki page; but much of it is out of date; the Physical to container wiki page was also helpful.

I did this once by the seat of my pants, but now I'm repeating it with a local OpenVZ host (in its own virtual machine!), so I can write the steps down this time. That way they're available if I want to repeat them in the future, and if anyone else finds it useful. Start by installing something that is not Gentoo into your container; I'm using Debian 6.0.

For completeness, on the host I start with debian-6.0-x86.tar.gz (the precreated template as linked above) in /vz/template/cache/. I assume that your selected VPS host will be able to install this for you, or you can through a control panel. Then create and start the container with:

# vzctl create 200 --config unlimited --ostemplate 
    debian-6.0-x86 --ipadd 192.168.202.200 --hostname guest
Creating container private area (debian-6.0-x86)
Performing postcreate actions
CT configuration saved to /etc/vz/conf/200.conf
Container private area was created
# vzctl start 200
Starting container...
Container is mounted
Adding IP address(es): 192.168.202.200
Setting CPU units: 1000
Container start in progress...
# vzctl set 200 --userpasswd root:root
# vzctl set 200 --nameserver 8.8.8.8

Now it's time to get Gentoo running! We've got a working non-Gentoo OS installed in the guest. From now on, we are only acting within the guest -- as if this was a VPS from a third-party provider, and that's the only access we have. The above was just to set up a test environment to emulate that.

We start with a stage3 release tarball. Download one to /root from a local mirror and then run this script (or perform these tasks by hand):

#!/bin/sh

set -e

if [ `whoami` != "root" ]; then
  echo "This script must run as root!"
  exit 1
fi

if [ ! -f /root/stage3-*.tar.bz2 ]; then
  echo "Could not find stage3 tarball!"
  exit 1
fi

echo "Extracting stage3 tarball ..."
rm -fr /root/gentoo
mkdir /root/gentoo
tar xjpf /root/stage3-*.tar.bz2 -C /root/gentoo

echo "Patching Gentoo installation ..."

# Fix mtab.
rm -f /root/gentoo/etc/mtab
ln -s /proc/mounts /root/gentoo/etc/mtab

# The host, not the container, mounts file systems.
echo "proc  /proc       proc    defaults    0    0" > /root/gentoo/etc/fstab
echo "none  /dev/pts    devpts  rw          0    0" >> /root/gentoo/etc/fstab

# The container has no terminals, don't run agetty.
sed -i -e '/agetty/d' /root/gentoo/etc/inittab

# Force a password to be set.
sed -i -e '/^root/s/*/!/' /root/gentoo/etc/shadow

# Tell RC that we're running inside OpenVZ.
sed -i -e 's/#?rc_sys=.*/rc_sys="openvz"/' /root/gentoo/etc/rc.conf

# Update PAM so that the host can set passwords.
sed -i -e 
    '/^password/s/include.*/required pam_unix.so md5 shadow/' 
    /root/gentoo/etc/pam.d/chpasswd

# Add the network to the default run level.
ln -s net.lo /root/gentoo/etc/init.d/net.venet0
ln -s /etc/init.d/net.venet0 /root/gentoo/etc/runlevels/default/net.venet0
# Start SSH at boot.
ln -s /etc/init.d/sshd /root/gentoo/etc/runlevels/default/sshd

echo "Activating Gentoo ..."
mount | grep -q /root/new && umount /root/new
rm -fr /root/new
mkdir /root/new
mount --bind / /root/new

rsync -aH --delete-after 
    --exclude=/dev 
    --exclude=/lib/init 
    --exclude=/root 
    /root/gentoo/ /root/new/

echo "Setting root password ..."
passwd

The one problem is to make sure the network is set up correctly. There's two possibilities here:

  1. If you have control of the host node, or cooperative people in charge, you can simply set OSTEMPLATE="gentoo" in the appropriate configuration file in /etc/vz/conf/. This sets all the appropriate files' contents when your VPS is starting.
  2. If not, you'll need to bootstrap the network settings yourself. This is the standard "Networking Information" subsection of the "Configuring your system" section of the Gentoo handbook, make sure /etc/conf.d/net is set up properly.

Now you can reboot and your VPS should be running Gentoo! Install a portage snapshot (section 5c) and then continue from "System Information" in section 8c of the Gentoo handbook. And clean up all the files left in /root/.

Take note of OpenVZ bug 1812. If the container has bash 4.2 or higher (which this version of Gentoo does by default), then vzctl enter will fail, with vzctl versions before 3.0.26.2. Up-to-date debian host nodes will have 3.0.24, which fails. (Yeah, it was fixed over a year and a half ago, but Debian updates glacially. My first attempts here were on Debian, and it took a while to figure out that this was why vzctl enter was failing.)

Comments:

Udev-pocalypse
2013-04-29 20:39 - arantius

Gentoo is updating udev, to require certain kernel settings, which openvz host kernels do not set. The solution is basically just don't use udev: http://nikolauspolak.info/en/blog/2013/03/gentoo-openvz-guest-udev-problems.

Out of date!
2019-03-12 15:22 - arantius

One of OpenVZ's weaknesses is that it relies on a very old (2.6) kernel. Even with patches to keep it somewhat up to date, it's now FATAL: kernel too old. A modern stage3, as described above, will not work with it.

Post a comment:

Username
Password
  If you do not have an account to log in to yet, register your own account. You will not enter any personal info and need not supply an email address.
Subject:
Comment:

You may use Markdown syntax in the comment, but no HTML. Hints:

If you are attempting to contact me, ask me a question, etc, please send me a message through the contact form rather than posting a comment here. Thank you. (If you post a comment anyway when it should be a message to me, I'll probably just delete your comment. I don't like clutter.)