Gentoo Wiki


This article is part of the Tips & Tricks series.
Terminals / Shells Network X Window System Portage System Filesystems Kernel Other

Note that the most recent version of this document can be found at the Gentoo Forums
This page is updated from the forums on 14/9/2004.

I just finished setting up a "fake" Gentoo installation inside a chroot jail. Seems to work pretty well for development, I can experiment with no fear of sodding up my actual workstation. In case anyone is interested, here is my "Captain's Log" that details the commands that I ran to build my "Faketoo" instance.

You should run these commands from a working Gentoo installation. Do not reboot off of the Gentoo install CD or anything.

My Faketoo host (i.e. my actual workstation) is a Dell PowerEdge 400SC with a Pentium 4 2.4GHz CPU (hyperthreading enabled) and 512MB of RAM, running a 2.6.3-gentoo-r1 SMP kernel (hyperthreading makes the kernel think it has two processors) and Gentoo 2004.0.

And now, without further ado, here is the Captain's Log:

# ==========================================================
# Faketoo: Building a development Gentoo install inside a chroot jail
# Version: 1.0.2
# Changelog:
#   1.0.2
#     - Mounting /etc/init.d as loop,noexec
#     - Mounting /usr/portage as bind
#     - Creating PORTDIR_OVERLAY /usr/local/portage
#     - Stuff proxy-related environment variables into ~juser/.bashrc
#   1.0.1
#     - Preserving permissions when creating virgin tarball
#   1.0.0
#     - Initial revision
# ==========================================================

# Insert Gentoo i686 LiveCD (Disc 1)
mount /mnt/cdrom

# Create chroot jail
mkdir ~/faketoo

# Create loopback filesystems that we will need for the jail
mkdir ~/faketoo/loopbacks
dd if=/dev/zero of=~/faketoo/loopbacks/etc-init.d seek=5K count=16 bs=1
mkreiserfs -f ~/faketoo/loopbacks/etc-init.d

# Install Gentoo in jail
cd ~/faketoo
mkdir -p etc/init.d
sudo mount -o loop,noexec loopbacks/etc-init.d etc/init.d
sudo tar xvjpf /mnt/cdrom/stages/stage3-pentium4-20040218.tar.bz2
sudo tar xvjf /mnt/cdrom/snapshots/portage-20040223.tar.bz2 -C usr/
sudo mkdir usr/portage/distfiles/
sudo cp /mnt/cdrom/distfiles/* usr/portage/distfiles/
sudo cp -ar /lib/modules ~/faketoo/lib/modules
sudo rm -rf ~/faketoo/dev
sudo mkdir ~/faketoo/dev
sudo chown root:root ~/faketoo/dev
sudo chmod 755 ~/faketoo/dev
sudo rm -rf ~/faketoo/usr/portage
sudo mkdir ~/faketoo/usr/portage
sudo chown root:root ~/faketoo/usr/portage
sudo chmod 755 ~/faketoo/usr/portage

# Swap Gentoo Pentium4 Package CD into CDROM drive
umount /mnt/cdrom

# Enter jail
sudo mount -o bind -t devfs /dev ~/faketoo/dev
sudo mount -t proc none ~/faketoo/proc
sudo mount -o bind /usr/portage ~/faketoo/usr/portage
sudo cp /etc/resolv.conf ~/faketoo/etc/
sudo chroot ~/faketoo /bin/bash
source /etc/profile
export PS1=': \u@FAKETOO; '

# Set localtime
ln -sf /usr/share/zoneinfo/America/New_York /etc/localtime

# Create /etc/fstab
cat >/etc/fstab <<EOF
/loopbacks/etc-init.d   /etc/init.d     reiserfs        loop,noexec     0 0
/dev/cdroms/cdrom0      /mnt/cdrom      iso9660         noauto,ro,user  0 0

# Setup networking
echo faketoo >/etc/hostname
echo domain.tld >/etc/dnsdomainname

# Configure rc.conf
sed -i -e 's/^EDITOR/#EDITOR/' -e 's/^#\(EDITOR=.\+vim"\)$/\1/' /etc/rc.conf

# Fix USE flags
sed -i -e 's/^USE="\(.\+\)"$/USE="\1 -gpm"/' /etc/make.conf

# Setup Portage to use binary packages when available
mount /mnt/cdrom
export PKGDIR=/mnt/cdrom

# Install system logger and cron daemon
emerge -k syslog-ng
emerge -k vixie-cron

# Setup root's environment
cat >~/.bashrc <<EOF
export PS1=': \u@FAKETOO; '
export PS2=': ; '

mount -a &>/dev/null

# Accounts management
groupadd juser
useradd juser -m -g juser -G users,wheel,audio,games,portage -s /bin/bash
passwd juser
cat >~juser/.bashrc <<EOF
export PS1=': \u@FAKETOO; '
export PS2=': ; '
for i in `env |grep -i proxy`; do echo "export $i" >>~juser/.bashrc; done

# Install a decent editor
emerge vim

# Setup sudo
emerge -k sudo
sed -i -e 's/^# \(%wheel\tALL=(ALL)\tALL\)$/\1/' /etc/sudoers
cat >>/etc/sudoers <<EOF
Defaults        !lecture,timestamp_timeout=60

# Setup Portage overlay (for ebuild development)
mkdir /usr/local/portage
chown root:root /usr/local/portage
chmod 755 /usr/local/portage
cat >>/etc/make.conf <<EOF

## For ebuild development
#ACCEPT_KEYWORDS='~x86 ~amd64 ~sparc ~ppc ~alpha ~mips ~hppa ~ia64 ~ppc64'

## Debug options
#CFLAGS="-march=pentium4 -pipe -g"
#USE=" debug"
#FEATURES=" nostrip keeptemp keepwork noclean"

# Leave jail
umount /mnt/cdrom

# Create virgin image
sudo umount ~/faketoo/dev
sudo umount ~/faketoo/proc
sudo tar cvjpf ~/faketoo.tbz2 faketoo

Danger Will Robinson: the init scripts are currently too dangerous to use. Please do *not* use '/etc/init.d/foo start' to start service foo. Instead, start it by hand.

And here is a script that can be used to enter the jail:

# ==========================================================
# Enter the Faketoo jail
# Version: 1.0.1
# Changelog:
#   1.0.1
#     - Fixing the erroneous $HOME environment variable for root
#   1.0.0
#     - Initial revision
# ==========================================================

if [ $UID -ne 0 ]; then

  echo You must be root!
  exit 1

fi # if (not root)

# Are /dev and /proc mounted inside the jail?

# Mount /dev if it is not already
echo "${mounted}" | grep $HOME/faketoo/dev &>/dev/null
if [ $? -ne 0 ]; then

  echo mount -o bind -t devfs /dev $HOME/faketoo/dev
  mount -o bind -t devfs /dev $HOME/faketoo/dev

fi # if (mounting /dev)

# Mount /proc if it is not already
echo "${mounted}" | grep $HOME/faketoo/proc &>/dev/null
if [ $? -ne 0 ]; then

  echo mount -t proc none $HOME/faketoo/proc
  mount -t proc none $HOME/faketoo/proc

fi # if (mounting /proc)

# Mount /usr/portage if it is not already
echo "${mounted}" | grep $HOME/faketoo/usr/portage &>/dev/null
if [ $? -ne 0 ]; then

  echo mount -o bind /usr/portage $HOME/faketoo/usr/portage
  mount -o bind /usr/portage $HOME/faketoo/usr/portage

fi # if (mounting /usr/portage)

# Enter the jail
HOME=/root chroot ~/faketoo /bin/bash 
Retrieved from ""

Last modified: Fri, 05 Sep 2008 10:21:00 +0000 Hits: 23,385