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.
- Make the disk bootable.
- Format the disk with a filesystem
- 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.
- Bootloader partition for grub: 50MB in size
- Boot partition formatted ext4: 500MB in size
- Swap partition for swap file: 2000MB in size
- 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:
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.
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):
#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)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:
#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/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.
$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.
$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/gentoo
use 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:
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.
Also copy the resolv.conf file from your livecd environment to the new root.
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 --rbind /sys /mnt/gentoo/sys
$mount --rbind /dev /mnt/gentoo/dev
It’s time for us to chroot (change root) to /mnt/gentoo.
$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.
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 set 6
Time zone and locale configurations are next:
Locate your location in this directory. In this case, it was Asia/Dili
$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.
Apply the changes:
$env-update && source /etc/profile
Now, we can get Linux kernel sources and start compiling them:
$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:
At the bottom of the file add the following lines:
/dev/sda4 / ext4 noatime 0 1
/dev/sda3 none swap sw 0 0
Install DHCP client so your VM will have network connectivity.
Make sure that the DHCP client starts at the boot process:
Setting a password for the root user is as simple as entering the command below and entering your new password when prompted:
To add a new user named bob enter the command:
$passwd bob ## This is to set a password for user bob
We would also need sudo so let’s install that:
And then allow the members of wheel group to execute any arbitrary command, we need to edit the /etc/sudoers file:
Lastly, we need to install and configure grub bootloader:
$grub-install /dev/sda
$grub-mkconfig -o /boot/grub/grub.cfg
Now we can exit the chroot environment, and unmount the filesystems:
$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!