How to Run Gentoo Linux in the Cloud

virtual machine for a rather banal thing – VPN in the Russian Federation. I started making a virtual machine in the Selectel control panel, and at the OS selection stage, I wanted to experiment. Do not use quite standard Debian / Ubuntu, but run your server with Gentoo Linux, which you can’t install out of the box.

Next, I decided to describe my experience in order to make life easier for Linux users and everyone involved.



Ode to Gentoo Linux


The operating system was created in the early 2000s, when there were no modern capacities and optimization really played a big role in software performance. Tests have shown that if you build the entire OS for a specific platform from source, and not from binary assemblies, then the performance gain will be 10-20%. Therefore, the idea arose to make a distribution that is built entirely from source. In this case, the assembly would not turn into a real test, as is the case with Linux From Scratch. This is how Gentoo Linux was born.

By the way, the very name of the distribution hints at performance. It was named after the fastest penguins, Pygoscelis papua, which can reach speeds of up to 36 km/h in water.

Key features of Gentoo

Unfortunately, there is one significant disadvantage: installing and / or updating the operating system is often a time-consuming and non-trivial task, especially if you have the target OS in production.

Since then, a lot of water has flowed under the bridge, but Gentoo Linux has remained the talk of the town for many system administrators. Previously, the use of such an OS was some kind of status among admins. Someone joked and compared the Gentushniks with samurai, someone called them “red-eyed” for sleepless nights. However, some companies still use Gentoo commercially.

Personally, I got acquainted with this operating system back in 2006, when I first got a job in a data center. On the very first working day, I got the task of installing Gentoo on a client dedicated server. Colleagues told me so: “Here we will check you.” Not on the first try, but I did it. I liked the operating system, and since then I have been using it for personal purposes: on a home server as a router and NAS, on a laptop in dualboot, and on virtual machines in the Selectel cloud.

Commercial use


Now the distribution is more used in DIY projects, for example, remember the video with the multi-touch guitar and Gentoo Linux inside? Feel old – this video is 13 years old. Or all these cases of DOOM launches on unusual devices? But if you look at the labor market, knowledge of Gentoo is required quite often until now.

The commercial use of Gentoo has become, in a sense, a niche story. On the one hand, ChromeOS is based on it. But there are few such projects. On the other hand, the development of technology has almost taken devices with limited resources out of use. Here we can distinguish two areas in which sensitivity to iron still remains.

To take you from wanting to test Gentoo to launch, here’s a guide to help you save time and not burn out before launch.

How to Install Gentoo Linux on a Virtual Server


I tried to put together a guide that installs Gentoo OS from stage3 with a simple copypaste of almost every command mentioned in the article.

Preparation

Create a virtual machine

arbitrary configuration. OS – any, I prefer Debian, so this manual will contain several commands from Debian.

In the Selectel cloud control panel, create a new disk with a minimum size of 6 GB and connect it to the virtual machine. It is on this disk that the root partition of your future Gentoo will be located. The disk will be defined as /dev/sdb.

Why exactly 6 GB? On a smaller volume, you will not have enough space to build the OS: you need to update portage, compile the kernel, and so on. As a reminder, we are installing Gentoo Linux with OpenRC.



Operating system installation


Prepare Debian:

apt-get update
apt-get install xz-utils

Create one primary partition for the entire /dev/sdb:

fdisk /dev/sdb

Create a file system (for example, ext4, but the choice depends on your tastes):

mkfs.ext4 /dev/sdb1
e2label /dev/sdb1 gentoo-root

Mounting our new partition:

mount /dev/sdb1 /mnt/gentoo

Download stage3:

wget http://<mirror URL>/stage3-<xxxxxx>.tar.xz

Unpack stage3 into our future root:

tar -xJvf stage3-<xxxxx>.tar.xz -C /mnt/gentoo

We mount service file systems for future work in chroot:

mount -t proc /proc /mnt/gentoo/proc
mount –-rbind /sys /mnt/gentoo/sys
mount --make-rslave /mnt/gentoo/sys
mount --rbind /dev /mnt/gentoo/dev
mount --make-rslave /mnt/gentoo/dev
mount --bind /run /mnt/gentoo/run
mount --make-slave /mnt/gentoo/run

Copy DNS settings:

cp /etc/resolv.conf /mnt/gentoo/etc/resolv.conf

We chroot into our new root:

chroot /mnt/gentoo /bin/bash
source /etc/profile 

Hooray, now you can use our usual tools to build the OS. First of all, set the root password: passwd

Configure rsync to use ipv4 addresses by default:

nano -w /etc/conf.d/rsyncd

RSYNC_OPTS="-4"

Update portage:

emerge —-sync

Install the OS profile (you can leave it as default):

eselect profile list
eselect profile set X

nano -w /etc/portage/make.conf

Set the general compiler flags:

COMMON_FLAGS="-O2 -pipe -march=native -mtune=native"

There is an important note about march=native and mtune=native. In the Selectel cloud, your virtual machine runs on a specific host with processors – for example, Intel Xeon Gold 6240. The virtual machine will leave this host only if the physical server fails for some reason and the virtual machines are migrated to other hosts. In the vast majority of cases, your machine is migrating to a similar host with the same hardware configuration, meaning you will get the same Xeon Gold 6240, most likely even from the same batch.

There is a tiny chance that you will be migrated to another processor. A nice bonus is migration only to a newer and / or powerful server.

Migration from Intel Xeon Gold 6240 to E5-2670 or AMD Epyc Selectel never initiates. But if you create an image of a machine and want to run it on an older pool, you could be in trouble.

We specify CPU_FLAGS to the compiler:

emerge -1v app-portage/cpuid2cpuflags
echo "*/* $(cpuid2cpuflags)" > /etc/portage/package.use/00cpu-flags

It is the flags to the compiler that do all the magic with speed. Set the time zone:

echo "Europe/Moscow" > /etc/timezone
emerge --config sys-libs/timezone-data

Setting up the network:

nano -w /etc/conf.d/net

dns_domain_lo="YOUR_DOMAIN"

config_eth0="192.168.0.2/24"
routes_eth0="default via 192.168.0.1"
dns_servers_eth0="188.93.16.19 188.93.17.19"

Notice I am using 192.168.0.2/24 for the VM. You need to copy the current network settings from Debian.

Enable network autoconfiguration at OS startup:

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

Install the kernel, bootloader and SSH:

emerge -1v grub gentoo-sources openssh
rc-update add sshd default

Building the kernel:

eselect kernel list
eselect kernel set 1
cd /usr/src/linux
lscpu (проверьте модель процессора)
make menuconfig

I will not describe the entire process of building the kernel, there are different ways to do this: you can do it completely manually, you can use genkernel, you can do it with or without initrd. I will describe the simplest option: a monolithic kernel with minimal requirements to run.

What to pay attention to

Processor selection

Processor type and features  --->
	Processor family  --->
(X) (Core 2/newer Xeon) для Intel
или 
(X) Opteron/Athlon64/Hammer/K8 для AMD

VirtIO drivers must be included in the kernel (as a rule, they are enabled by default):

Processor type and features  --->
    [*] Linux guest support --->
        [*] Enable Paravirtualization code
        [*] KVM Guest support (including kvmclock)
Device Drivers  --->
    [*] Virtio drivers  --->
        <*> PCI driver for virtio devices
    [*] Block devices  --->
        <*> Virtio block driver
    SCSI device support  --->
        [*] SCSI low-level drivers  --->
            [*] virtio-scsi support
    [*] Network device support  --->
        [*] Network core driver support
            <*> Virtio network driver
    Graphics support  --->
        <*> Virtio GPU driver /// А НАДО ЛИ????
    Character devices ---> 
       <*>   Hardware Random Number Generator Core support --->
           <*>   VirtIO Random Number Generator support

Without the above drivers, you simply won’t be able to boot the server. In recent kernels, these options come by default.

make -jX

Where X is the number of your cores.

make install && make modules_install

Install the GRUB bootloader. Edit boot options:

nano -w /etc/default/grub

GRUB_CMDLINE_LINUX="net.ifnames=0"
GRUB_GFXPAYLOAD_LINUX=text

  • The first line is needed so that we have the usual interface names (eth0, eth1, and so on).
  • In the second line, we tell the kernel that we are giving text to the screen. This is necessary for the console to work in the Selectel control panel.
grub-install /dev/sdb
grub-mkconfig > /boot/grub/grub.cfg

Add the root section to /etc/fstab:

blkid /dev/sdb1 | awk '{print $3" / ext4 noatime 0 2"}' >> /etc/fstab

Hooray, we have completed the initial assembly of the OS.

Reboot to new OS


Now in the control panel you need to do special magic with disks. Each VM can only have one boot drive, and that is the drive that was first attached to the VM. We need to make our new Gentoo disk bootable. To do this, turn off the VM and in the Disks section, disable all disks. Then mount the Gentoo drive first, so it will automatically become bootable.

Next, turn on the VM. If you did everything right and saw how your new OS booted up to login, congratulations, you are great!

Then, as unnecessary, you should disconnect and remove the Debian disk you no longer need and the extra CPU cores. Next, you know what to do: connect to the server via SSH and start a real holiday – assembling your own world.

emerge -v --update --deep --changed-use @world

Instead of an afterword

No magic, just sleight of hand. In the next article, we’ll show you how to make stage4 and orchestrate Gentoo on the Selectel cloud platform.

Useful materials on the topic



Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *