Official website for Linux User & Developer
FOLLOW US ON:
Aug
16

Build Gentoo tutorial

by Rob Zwetsloot

Create a custom build of the Gentoo distro from the ground up, to suit your preferences and even speed up your system

Gentoo can be a double-edged sword. It’s probably the most customisable Linux distribution available, letting you build it from the ground up to be exactly what you want it to be. However, it’s not all that easy to get to grips with and requires some serious skills in Linux to get right.

Once you have, though, Gentoo can be very rewarding. Building packages from source and compiling the kernel yourself feels like you’re making the most out of your Linux experience, and can actually help make your system a lot faster than some of the more popular, preconfigured distros.

In this tutorial, we’ll cover taking an image and doing the first-time setup. While we’ll be relying on the minimal installation disc and downloading stage tarballs from the internet, there is also a more complete DVD image that you can use to create a more rudimentary setup. A lot of the steps will be the same; however, if you get stuck, the Gentoo website has some great resources and manuals to guide you through any differences.

Customise the kernel so that there’s no bloat in your system
Customise the kernel so that there’s no bloat in your system

Resources

Gentoo live image

Step-by-step

Step 01 Install and boot

Grab the minimal Gentoo live image and install it to a CD in whatever way you prefer. Restart your system and boot from CD/DVD, and press Enter at the boot screen. You’ll then be asked if you want to change your keymap with some option, otherwise it will take you to the live system command line.

Step 02 Partitioning

We now need to setup the disc so we can install to it. We’ll use Parted. First, check the current layout with:

# parted /dev/sda

And then enter print in the new Parted commandprompt.Userm [number]toremove partitions of that number that you don’t need.

Step 03 Create partitions

We’re going to start from a blank hard drive. We’ll need a boot partition, a swap and some space. Use the following three commands:

mkpart primary ext2 0 32mb

mkpart primary linux-swap 32mb [32 + RAM]mb

mkpart primary ext4 [32 + RAM]mb -1s

Agree to or ignore any prompts. The option at the end of the third command tells Parted to fill up the rest of the disc.

Step 04 Make file systems

Quit out of Parted with quit. You’ll now need to make the partitions into file systems using the following commands:

# mkfs.ext2 /dev/sda1
# mkfs.ext4 /dev/sda3

Create the swap with:

# mkswap /dev/sda2

Step 05 Mount partitions

Before we continue, we need to mount the partitions. Do this by first mounting the storage as /mnt/gentoo:

# mount /dev/sda3 /mnt/gentoo

Create a boot folder within this:

# mkdir /mnt/gentoo/boot

Mount the boot folder:

# mount /dev/sda1 /mnt/gentoo/boot

Step 06 On time

Make sure the clock is correctly configured to UTC time by simply entering date. If it’s not, make it UTC time with the following command:

# date MMDDhhmmYYYY

…where MM is the month, DD the day/date, etc.

Step 07 Take the stage

Move to mount point you just created with cd, and then type the following to get a list of mirrors for the stage 3 tarball:

# links http://www.gentoo.org/main/en/mirrors.xml

Navigate using the arrow keys to your nearest mirror, go to releases, then your architecture, current stage3, and download a stage3 tarball.

Step 08 Extraction

Unpack the tarball you just downloaded with this:

# tar xvjpf stage3-*.tar.bz2

Once it’s unpacked, open the configuration file using nano:

# nano -w /mnt/gentoo/etc/portage/make.conf

And then we will be able to start some of the initial configuration.

Step 09 Make options

The default options already in the configuration file should be good enough for most systems. We can also add an option for how many parallel compilations can occur at once by adding this to the bottom:

MAKEOPTS="-j[X]"

…where X is the number of cores you have plus one. Save and exit with Ctrl+X.

Step 10 Prepare build environment

We’re nearly ready to start building. Save the network/DNS details to the environment with:

# cp -L /etc/resolv.conf /mnt/gentoo/etc/

Next, mount the /proc file systems, and then bind them to /dev and /sys with:

# mount -t proc none /mnt/gentoo/proc
# mount --rbind /sys /mnt/gentoo/sys
# mount --rbind /dev /mnt/gentoo/dev

Step 11 Enter build environment

The build environment is technically your new Linux environment. We need to make some changes so we can enter it first – basically change the directory we created to root using chroot with:

# chroot /mnt/gentoo /bin/bash
# source /etc/profile
# export PS1="(chroot) $PS1"

We need the latest Portage snapshot before we go any further. Emerge it with:

# emerge-webrsync

This will allow us to install all the packages we need. Update Portage before continuing with:

# emerge --sync

Step 13 Installation profile

You’ll now need to choose how to build Gentoo by setting a specific profile. There are three profiles, two of which are of interest to us – Desktop and Server. This will determine the type of packages we use. Set it with:

# eselect profile set 2

2 is desktop; change it to 3 for a server.

Step 14 USE me

The USE variable in make.conf is a powerful tool to configure compiling so it only installs the package support you require. A full list of these flags can be found online or in your system at:

# less /usr/portage/profiles/use.desc

We’ll make our system so it will install files for GNOME and GTK, as well as add ALSA and DVD support. Edit the make file with:

# nano -w /etc/portage/make.conf

And change USE to:

USE="gtk gnome -qt4 -kde dvd alsa"

Step 15 Kernel time

List the available time zones with:

# ls /usr/share/zoneinfo

For London, we will do the following:

# cp /usr/share/zoneinfo/Europe/London /etc/localtime

# echo "Europe/London" > /etc/timezone

Now it’s time to download our kernel. First, get gentoo-sourceswith:

# emerge gentoo-sources

Check what kernel version gentoo-sources is pointed at with:

# ls -l /usr/src/linux

From here, you can start modifying the kernel flags by entering:

# cd /usr/src/linux
# make menuconfig

Make sure to change only the kernel options you need to. Activate any other required modules. Once done, exit the configuration.

Step 16 Compile kernel

The moment of truth – time to compile your kernel. Do this with:

# make && make modules_install

This will take a while depending on the amount of modules and options you activated. Once it’s finished, install the kernel with:

# cp arch/x86/boot/bzImage /boot/kernel-[X]-gentoo

…with X being the number we found last step.

Step 17 Boot modules

You’ll need to set what kernel modules you want to load. To find what modules are available, use:

# find /lib/modules/[X]/ -type f -iname '*.o' -or -iname '*.ko' | less

…again where X is the kernel number. You then need to add the modules you want to this file:

# nano -w /etc/conf.d/modules

Step 18 Tabbed file system

We need to set the partitions we created to be mounted properly at boot. Open fstab with:

# nano -w /etc/fstab

And then set the following options so that the file system we set up works properly:

/dev/sda1   /boot  ext2   defaults,noatime   0 2
/dev/sda2   none   swap   sw                 0 0
/dev/sda3   /      ext4   noatime            0 1

Step 19 Networking

You’ll need to configure your network for after the reboot. First enter the config file with:

# nano -w /etc/conf.d/net

And add this like:

config_eth0="dhcp"

If you’re using static IPs, you can add them instead of DHCP. Save, and then make it bootable with:

# cd /etc/init.d
# ln -s net.lo net.eth0
# rc-update add net.eth0 default

Step 20 Root setup

Set the root password with the standard passwd command. Now set some basic services by editing:

# nano -w /etc/rc.conf

Keymaps with:

# nano -w /etc/conf.d/keymaps

And the hardware clock:

# nano -w /etc/conf.d/hwclock

If this is not UTC, add clock=”local” to the file

Step 21 Your locale

Specify your locales for the system. A basic setup will need you to edit:

# nano -w /etc/locale.gen

…and add:

en_GB ISO-8859-1
en_GB.UTF-8 UTF-8

Save, exit and then type locale-gen. Set it as default in /etc/env.d/02locale with:

LANG="en_GB.UTF-8"
LC_COLLATE="C"

And then reload the environment with:

# env-update && source /etc/profile

Step 22 Bootloading

We need to install GRUB so we can boot into Gentoo after a restart. Compile it with:

# emerge grub

Now create the grub.conf file with:

And get ready to add the necessary details.

Step 23 GRUB code

default 0
timeout 15
title Gentoo Linux
root (hd0,0)
kernel /boot/kernel-3.[version]-gentoo
root=/dev/sda3

title Gentoo Linux (rescue)
root (hd0,0)
kernel /boot/kernel-[version]-gentoo
root=/dev/sda3 init=/bin/bb

Make sure to use the correct kernel number. Save and exit.

Step 24 GRUB setup and reboot

Create an mtab to install GRUB to with:

# grep -v rootfs /proc/mounts > /etc/mtab

And finally, install it with:

# grub-install --no-floppy /dev/sda

To reboot into your brand new system, exit the chroot and reboot with:

# exit
cdimage ~# cd
cdimage ~# umount -l /mnt/gentoo/dev{/shm,/pts,}
cdimage ~# umount -l /mnt/gentoo{/boot,/proc,}
cdimage ~# reboot

This tutorial originally turned up way back in issue 130 of the magazine. Grab this issue and other back issues from the Imagine Shop.

  • Tell a Friend
  • Follow our Twitter to find out about all the latest Linux news, reviews, previews, interviews, features and a whole more.