Gentoo

Install Gentoo in VirtualBox

Installing Gentoo as a VirtualBox VM

Gentoo is one of the most esoteric distributions out there. It offers customizability at the expense of user-friendliness. It does, however, shed light on the internal workings of a Linux installation. To experiment with a Gentoo environment inside a VM is probably a great way to start learning about operating systems in general.

Let’s go through a step-by-step process of installing the base OS along with the explanation behind every step.

1. Getting the right installation media

The easiest way to install Gentoo is by using the Hybrid ISO (LiveDVD). Which means that the disk image can be used for installing the operating system onto another disk or it can just be used as a live environment to boot into for diagnostic purposes.

You can get the image file here. Pick the one that belongs to your hardware platform. Modern Intel and AMD processors usually offer AMD64 architecture.

Next you need to create a VM on VirtualBox. Open VirtualBox, and click on the button that says “New” now you can enter the name of the VM and select ‘Type’ as Linux and ‘Version’ as Gentoo 64-bit.

Set the memory size to 2048MB and then click on “Create” to proceed with the creation of virtual hard disk.

The default values would work just fine in this case (although we are going to work with 32GB of disk space instead of 8GB) and now you can click on ‘Create’ one last time to finalize the details.

Now, select the newly created VM from VirtualBox dashboard and you will be prompted with a start-up disk. Click on the file icon beside it and in the file explorer that opens after that navigate to the gentoo livecd iso file that you have downloaded earlier.

Once you start up the VM you will see the boot menu with the following options:

Selecting x86_64 is the most appropriate option in this case.

After that you will be greeted with a login screen with default gentoo user, click on login without entering any password (as indicated in the screenshot below).

You are now technically inside a Gentoo Operating System. However, this is a live media, which means you can’t use it as a system installed in your (virtual) hard disk. But you can use this environment to install Gentoo onto your virtual hard disk.

2. Partitioning the virtual hard disk

The .vdi disk that we created earlier is just a raw disk at this point, attached to the virtual machine and the VM itself is running the Gentoo live media. Now to install the Gentoo environment onto this disk so that it can boot itself we would need to do several things.

  1. Make the disk bootable.
  2. Format the disk with a filesystem
  3. Install the base Gentoo image onto the root filesystem.

To accomplish the 1st task we just need to make 4 partitions of the following size and preferably in the following order.

  1. Bootloader partition for grub: 50MB in size
  2. Boot partition formatted ext4: 500MB in size
  3. Swap partition for swap file: 2000MB in size
  4. Root partition for main OS and related binaries to reside. Formatted with ext4 filesystem and is going to take the remaining majority of the space of the virtual disk.

To make the partitions we first need to get the name of the virtual disk attached to the system. Open terminal (konsole) and run sudo -i to become root user and then run lsblk to list all the block storage devices.The device name in our case is sda is sda and it is of size 32GB. Next we need to enter parted utility to partition this disk. To do so run, as root:

$ parted -a optimal /dev/sda

Now we are in parted utility CLI, let’s start by listing all the partitions by typing print:

And we get an error message saying that the disk is not recognized. This is to be expected since the installation process with Gentoo is not automated, you get to manually configure every little detail including the partitioning details for your base system. First things first, let’s give our disk a proper label.

(parted) mklabel gpt

GPT labels are essential for uniquely identifying a device even after, say, the system reboots and the device is plugged in a different port this label will be responsible for telling the operating system that the disk has just changed SATA ports but it’s still the same data and format as before.

Partition the disk by running the following commands (Lines starting with ‘#’ symbol are comments to explain the command above them):

(parted)unit MB
#Sets the unit to MegaBytes

(parted)mkpart primary 1 20
#Makes a primary partition starting from 1 MegaByte to #20th for bios

(parted)mkpart primary 21 500
#Partition /boot filesystem

(parted)mkpart primary 501 2500
#Partition of size 2000MB made for swap

(parted)mkpart primary 2501 -1
#Partition for the /(root) filesystem. -1 indicates that
#this partition goes upto the very end of the disk.

You can see that each partition has a number and is of the type primary. However, they are not formatted with any particular file system or have any use names given or flags set to them. Let’s do that.

(parted)name 1 grub
(parted)set 1 bios_grub on
#The partition number 1 has its bios_grub flag set to one
#and is given an appropriate name.
(parted)name 2 boot
(parted)name 3 swap
(parted)name 4 root
(parted)quit

After setting appropriate names to all the 4 partitions and 1 bios_grub flag to the first partition we quit the parted utility. Now we move on to formatting the partitions with an appropriate filesystem in our usual bash shell (still as root user) by first running the following command:

$lsblk
#To list all the partition and check their device node names

You can see that the partitioning scheme has labeled the first partition sda1 which corresponds to grub partition and so on and so forth until sda4. The devices are present in /dev directory as /dev/sda1 , /dev/sda2 and so on.

To format them accordingly, run the commands:

$mkfs.ext4 /dev/sda2
$mkfs.ext4 /dev/sda4
$mkswap /dev/sda3
$swapon /dev/sda3

Now we can mount these partitions into the current live environment so that all the necessary operations like building the kernel can be performed in there and stored persistently.

$mount /dev/sda4 /mnt/gentoo
$mkdir /mnt/gentoo/boot
$mount /dev/sda2 /mnt/gentoo/boot

Our would be root partition is mounted at /mnt/gentoo of the current livecd environment and similarly our would be boot partition is mounted on /mnt/gentoo/boot.

3. Getting tarball and using chroot

Now that we have our disk prepared for us, it is time for us to get the tarball of gentoo source code and place it in there. In the live CD environment, open the browser open this link and click on the Stage3 archives under the amd64 section at the top of the page.

Once the download is complete, copy the tarball to the /mnt/gentoo directory and extract it’s content in there.

$cp /home/gentoo/Downloads /mnt/gentoo
$tar xpf stage3-*.tar.xz --xattrs-include='*.*'
--numeric-owner

In the second command, the tar utility is used to uncompress the tarball. xpf  tells the tar command that we want to x extract, p preserve permissions on the files and f to imply that we are extracting files and not standard input.

The file extension may not be tar.xz in your case. Observe the name of your tarball file and type in accordingly.

The --xattrs-include part of the command preserves the attributes (read, write and execute) of individual files

And the --numeric-owner ensures a group and user ID number as approved by the Gentoo convention for a typical setup.

If you see the contents that would be extracted in your /mnt/gentoo directory they would resemble a typical Unix root environment with directory like /etc /sbin, etc. The idea behind this is that, once all the files necessary for a working Gentoo environment are extracted we will change our root directory to /mnt/gentoouse tools and package managers to configure it. The package manager would make changes in the / directory but it would be cheated into making changes in /mnt/gentoo directory instead.

Since our would be root partition is mounted on this directory, all the changes would be made over there and then we would boot off this new environment once we are done.

But first, let’s make a few changes in the configuration files:

$nano /gentoo/mnt/etc/portage/make.conf

After the CFLAGS line you should add the following lines which would let portage to treat c++ files the same way as c files. Portage is gentoo’s package manager, loosely speaking. It is used to fetch source code of programs so you can compile them all (automatically) on your native system.

$CXXFLAGS="${CFLAGS}"

Also copy the resolv.conf file from your livecd environment to the new root.

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

Now we make sure that all the filesystems necessary for the Linux kernel to gather information about the system are available when it tries to boot. So we take the information gathered by the livecd image about our virtual machine and its hardware and we bind those to our new root filesystem.

$mount -t proc /proc /mnt/gentoo/proc
$mount --rbind /sys /mnt/gentoo/sys
$mount --rbind /dev /mnt/gentoo/dev

It’s time for us to chroot (change root) to /mnt/gentoo.

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

4. Compiling the Linux kernel

Let’s sync our portage tree (software repository) with the official version. This is similar to apt update in Ubuntu.

$emerge-webrsync

Once that finishes, we can select a profile for our system. Which essentially tunes the system for a specific use-case (server, workstation, etc.). We will be going with a KDE plasma environment listed at number six.

$eselect profile list
$eselect profile set 6

Time zone and locale configurations are next:

$ls /usr/share/zoneinfo

Locate your location in this directory. In this case, it was Asia/Dili

$cp /usr/share/zoneinfo/Continent/City /etc/localtime
$echo " Asia/Dili" > /etc/timezone

Next uncomment the location specific character set and language that you want to use from the file /etc/locale.gen. We uncommented the line en US.UTF-8 UTF-8.

$nano /etc/locale.gen

Apply the changes:

$locale-gen
$env-update && source /etc/profile

Now, we can get Linux kernel sources and start compiling them:

$emerge gentoo-sources
$emerge genkernel
$genkernel all

The last command will start compiling the kernel which would take a long time, especially if you have limited hardware resources.

5. Finishing touches

After the kernel compilation we just need to make a few more changes like installing grub, adding a user and making sure that the operating system mounts the essential filesystems automatically during the boot process.

To make sure that the last of those things happen, in the chrooted environment edit the file /etc/fstab where fstab stands for filesystem table:

$nano /etc/fstab

At the bottom of the file add the following lines:

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

Install DHCP client so your VM will have network connectivity.

$emerge dhcpcd

Make sure that the DHCP client starts at the boot process:

$rc-update add dhcpcd default

Setting a password for the root user is as simple as entering the command below and entering your new password when prompted:

$passwd

To add a new user named bob enter the command:

$useradd -m -G users,wheel,portage -s /bin/bash bob
$passwd bob ## This is to set a password for user bob

We would also need sudo so let’s install that:

$emerge sudo

And then allow the members of wheel group to execute any arbitrary command, we need to edit the /etc/sudoers file:

$visudo

Lastly, we need to install and configure grub bootloader:

$emerge grub
$grub-install /dev/sda
$grub-mkconfig -o /boot/grub/grub.cfg

Now we can exit the chroot environment, and unmount the filesystems:

$exit
$umount -l /mnt/gentoo/dev{/shm,/pts,}
$umount -l /mnt/gentoo{/boot,/proc,}
$shutdown now

Go to the VM’s setting and in the storage section, remove the livedvd image attached to the VM.

Upon starting the VM again you will be greeted with a tty interface to your newly installed Gentoo operating system. Login as using the username and password that you chose during the installation.

Conclusion

Just the installation process for Gentoo reveals a lot more about the internal workings and structure underneath most Unix environments. If one desires to attain a deeper knowledge of the system the Gentoo Handbook would be a great place to start!

About the author

Ranvir Singh

Ranvir Singh

I am a tech and science writer with quite a diverse range of interests. A strong believer of the Unix philosophy. Few of the things I am passionate about include system administration, computer hardware and physics.