User:Jbara/Installing Gentoo on Free Oracle VPS

From Gentoo Wiki
Jump to:navigation Jump to:search

Oracle offers free tier aarch64 based vps running Oracle Linux. Gentoo enthusiasts will probably want to install Gentoo on such a machine to make the most out of it. This guide explains in details how you can turn that headless machine into a Gentoo system.

As of writing it is possible to get a free 4 core virtual machine with 24GB of RAM and 200GB of diskspace. A credit card or debit card is required for sign up.

Creating a temporary Gentoo Chroot

First create a temporary Gentoo root file system.

root #mkdir /gentoo

Next, download the stage3 tarball for the system Just make sure to pick the ARM64 tarball and not an amd64. Place the tarball in gentoo

root #tar xpvf stage3-*.tar.xz --xattrs-include='*.*' --numeric-owner

It is a good idea to backup the kernel modules that came with oracle Linux.

root #cp -a /lib/modules /gentoo/lib/modules
root #mount --types proc /proc /gentoo/proc
root #mount --rbind /sys /gentoo/sys
root #mount --make-rslave /gentoo/sys
root #mount --rbind /dev /gentoo/dev
root #mount --make-rslave /gentoo/dev
root #mount --bind /run /gentoo/run
root #mount --make-slave /gentoo/run
root #cp -a /etc/resolv.conf /gentoo/etc/

Now create the and bind /gentoo/mnt/oracle to /

root #mkdir /gentoo/mnt/oracle
root #mount --rbind / /gentoo/mnt/oracle

Now, enter the intermediate system.

root #chroot /gentoo
root #env-update && . /etc/profile

Replacing your old filesystem with gentoo

At this point, start by selectively deleting folders and replacing them with ones from the Gentoo fs.

root #cd /mnt/oracle
root #for dir in bin etc lib lib64 media opt sbin usr; do

> rm -rf "$dir"; cp -a "/$dir" .

> done
root #mv var deleteme
root #cp -a /var .
Note
The /var directory needs to be moved instead of deleted because some oracle programs are still using it. Trying to delete it will result in Resource busy error message.
Note
If following along in a machine that has a graphical session running. It is advised to do the previous step on tty since it breaks most programs including the terminal

Chroot to the new system

Re-run the preparation to do another chroot.

root #mount --types proc /proc /mnt/oracle/proc
root #mount --rbind /sys /mnt/oracle/sys
root #mount --make-rslave /mnt/oracle/sys
root #mount --rbind /dev /mnt/oracle/dev
root #mount --make-rslave /mnt/oracle/dev
root #mount --bind /run /mnt/oracle/run
root #mount --make-slave /mnt/oracle/run

After this chroot into your newly created Gentoo filesystem. This one being the final target.

root #chroot /mnt/oracle

The system just chrooted into is going to be the end result.

Configuration

Check the handbook Installing the base system, however simply configuring your repos.conf and running emerge-webrsync is all that is needed.

root #mkdir -p /etc/portage/repos.conf
root #cp /usr/share/portage/config/repos.conf /etc/portage/repos.conf/gentoo.conf
root #emerge-webrsync

It might be important for your setup to set your root password now. From here you can get your system ready for next reboot. You might want to install net-misc/openssh for the next reboot. Just make sure to configure it properly so you can get access to your system on your next reboot.

root #emerge --ask net-misc/openssh

Do not forget to run ssh-keygen -A and add your public rsa key to ssh/authorized_keys After that you can add it to your default runtime level.

root #ssh-keygen -A
root #rc-update add sshd default
Note
Before rebooting you may want to consider changing the port in etc/ssh/sshd_config and testing ssh with usr/sbin/sshd -ddd to check for any error messages. You will need to modify the firewall in Oracle's panel to add whichever port you decide to test externally as everything but port 22 is firewalled by default. Don't forget to get it back to port 22 afterwards.

Post Installation

Warning
It is not recommended to change the name of the volume

Managing storage

Once rebooted there are a couple of things you might want to conside doing. Such as expanding your / to take all the space instead of limiting yourself to 30G. After adding lvm to your global USE and updating world with newuse Install sys-fs/lvm2 and follow the following steps.

Logical volume

First Remove oled" logical volume and add the space to root" logical volume.

root #lvremove /dev/ocivolume/oled
root #lvextend /dev/ocivolume/root /dev/sda3

This will give you the remaining 15G on dev/sda3 The next step is to get the unallocated 153G. First allocate it by pressing n in fdisk. {RootCmd|root # fdisk /dev/sda}} After creating the new partition. Change the its type with the t key, set it to lvm Add it to the volume group and extend the logical volume.

root #vgextend ocivolume /dev/sda4
root #lvextend /dev/ocivolume/root /dev/sda4

Growing FS

Finally, to make use of the needed space on the logical volume. You need to grow your filesystem. On xfs this is done by the xfs_grow provided by sys-fs/xfsprogs

root #xfs_growfs /

"net" generic service (OpenRC)

Openrc uses netifrc to manage the network without the need for a network manager. While dhcp is probably being managed by the initramfs. This is fine for basic functionalities, but some services depend on the net generic service. First emerge a dhcp client of choice. It is advised to disable all dhcp server functionalities. (e.g. disable server use flag for net-misc/dhcp Assuming the network interface name is eth0 this should start the netifrc service for it.

root #root # ln -s /etc/init.d/net.lo /etc/init.d/net.eth0

Add to default runlevel

root #root # rc-update add net.eth0 default
CODE /etc/rc.conf
rc_hotplug="!net.*"
rc_depend_strict="NO"

Afterwards, reboot your system, the "net" generic service and all services that depend on it should work just fine.

Note
You can set net.ifnames=0 as a kernel parameter to get predictable interface names like eth0 instead of enp0s3. This can be done in grub.cfg (in boot/loader/entries/*-$(uname -r).conf *) after mounting /dev/sda2 on /boot or it can be added to GRUB_CMDLINE_LINUX_DEFAULT in etc/default/grub if you have done the Changing the bootloader part.

Docker containers (OpenRC)

In this configuration. The kernel and kernel command line arguments confuse dockerd. Making it think the Cgroup driver that needs to be used is systemd instead of cgroupfs. If you're planning on using docker. Consider adding exec-opt native.cgroupdriver=cgroupfs to DOCKER_OPTS in etc/conf.d/docker

Changing the bootloader

The boot process is currently complicated and messy. With configuration files calling other configuration files and so on. Arm's boot process is already complicated. Therefore a good practice is to switch to a clean boot process. Another advantage of this is to be able to change your kernel.

First of all, we will clean the old boot files and directories

root #mount /dev/sda2 /boot
root #mount /dev/sda1 /boot/efi
root #rm -r /boot/efi/*
root #umount /boot/efi
root #rm -r /boot/*
root #mkdir -p /boot/efi
root #mount /dev/sda1 /boot/efi

Afterwards, feel free to install the kernel and bootloader, this guide will use grub and the gentoo pre-built kernel. Note that the bootloader needs to be able to boot the XFS boot partition, therefore if you are using grub, the device-mapper use flag needs to be enabled.

root #emerge gentoo-kernel-bin
root #euse -E device-mapper -p sys-boot/grub
root #emerge sys-boot/grub sys-boot/efibootmgr
Warning
Performing an efi entry deleltion using efibootmgr -b number -B number will result in the loss of web UI device control capabilities. You will not be able to boot the device from the oracle web UI in case of shutdown.

An important part of keeping the system bootable is having the essential kernel parameters present. Grub lets you set this in the GRUB_CMDLINE_LINUX_DEFAULT variable in etc/default/grub

FILE /etc/default/grubGrub Config
GRUB_CMDLINE_LINUX_DEFAULT="console=ttyAMA0 console=ttyAMA0,115200 rd.lvm.vg=ocivolume rd.lvm.lv=ocivolume/root"

Finally install grub and generate the grub configuration file

root #grub-install --efi-directory /boot/efi
root #grub-mkconfig -o /boot/grub/grub.cfg
Warning
In this setup, it is advised to re-install grub regularly or before a reboot. This is because the default entry does get overwritten for some reason. If you're using another bootloader, you can still do this using efibootmgr to set the default entry. If you find yourself in a situation where the machine doesn't boot follow the troubleshooting section.

Troubleshooting

Firewall

If you find yourself unable to access any port except 22. That's because oracle has the firewall enabled on all ports by default. This can be fixed in their WebUI.

Cloud Shell

Cloud shell gives you access to serial connection in your browser. Check the official documentation for more information. This can be used in a lot of situations. For example, in the case where ssh is not functioning. Or in the case of a firewall problem, or sometimes even in the case of a machine that does not boot.

Use a console connection to troubleshoot a malfunctioning instance. For normal operations, you should connect to the instance using a Secure Shell (SSH) or Remote Desktop connection.

For more information about troubleshooting your instance using a console connection, see the documentation.

Default EFI entry changed

In some cases, you might reboot the machine only to find that it does not want to start. It is advised to run grub-install or an equivalent command that sets the default boot entry before shutting down to prevent this. However, if that happens, fixing it requires the following steps:

  1. Start the Cloud Shell and wait until there is no more output
  2. Press enter, you might get the UEFI Shell (with PS1 looking like this Shell>)
  3. In the case where you get a UEFI menu, navigate to Boot devices and then to UEFI Shell
  4. use map to list storage devices. The device you are looking for is most probably FS0:
  5. Type the device name, including the colon. E.g. FS0:
  6. You will find yourself in the EFI partition, enter the EFI folder, and then the gentoo folder inside it using the cd command. Note that tab completion works here.
  7. Use ls to locate the EFI file you need to boot. writing the name of the file as a command should be enough to execute it (which can be done by just pressing <TAB>)