Oracle cloud: turning ubuntu 20.04 into gentoo

Oracle cloud offers some resources completely free of charge. In particular, you can get 2 virtual machines for free. When creating a machine, you have a choice of centos, Ubuntu, or oracle linux. Given the limited resources of the vitruing machine, I wondered if gentoo could be used on this machine?



The gentoo documentation describes how you can turn any Linux into gentoo on the fly. Unfortunately, the instruction was last updated in 2014 and if you apply it directly, most likely you will have problems. However, on the github you can find a script that, according to the author, can automatically turn any Linux into gentoo. The script has not been updated for a long time, but after minor changes it worked as it should. The author of the original script decided not to apply my pull request, but modified his script based on my suggestions. My version of the script can be found here .



So let's do the conversion of ubuntu 20.04 to gentoo. To do this, create a virtual machine using the ubuntu 20.04 minimal image and log in to it as an ubuntu user. After that, switch to the root user:



sudo -i
      
      





Switch to single user mode:



systemctl rescue
      
      





Under normal conditions, the network falls off. This does not happen in oracle cloud, but the systemd-resolved process ends and, as a result, dns stops working. Let's fix it:



rm /etc/resolv.conf && echo 'nameserver 8.8.8.8' >/etc/resolv.conf
      
      





Now you can run the migration script and go have some tea, it will take some time:



wget -qO- https://raw.githubusercontent.com/kt97679/to-gentoo/master/to-gentoo|bash
      
      





When the script is finished, it will prompt you to reboot. Before doing this, you need to make sure that you do not lose root access after a reboot. To do this, you need to allow root access via ssh:



sed -i -e '/^PermitRootLogin.*/d' /etc/ssh/sshd_config && echo -e "\nPermitRootLogin without-password" >>/etc/ssh/sshd_config
      
      





And fix the file /root/.ssh/authorized_keys:



sed -i -e 's/.*\(ssh-rsa.*\)/\1/' /root/.ssh/authorized_keys
      
      





Now you can reboot, but the reboot command won't help, so let's bring in some heavy artillery:



sync && echo 1 > /proc/sys/kernel/sysrq && echo b > /proc/sysrq-trigger
      
      





The machine goes into reboot and after a couple of minutes you will be able to log in to it via ssh as root. You are in a gentoo environment, but you are still using the old kernel and initrd. Let's fix this.



Let's enable swap, without it you won't have enough memory to compile a new kernel.



fallocate -l2G /var/tmp/swap && chmod 0600 /var/tmp/swap && mkswap /var/tmp/swap && swapon /var/tmp/swap
      
      





I don't plan to use systemd on this machine, so I mask it and udev:



echo -e 'sys-apps/systemd\nsys-fs/udev'> /etc/portage/package.mask/systemd
      
      





After that, you can install the packages necessary for compiling the kernel:



emerge gentoo-sources app-arch/lz4
      
      





Now you can go to the directory with the kernel sources,



cd /usr/src/linux
      
      





generate a new kernel config using the loaded kernel config,



yes ""|make oldconfig
      
      





and leave in the config only those modules that are currently used.



yes ""|make localmodconfig
      
      





I plan not to use initrd on this machine, so all modules will be compiled into the kernel:



yes ""|make localyesconfig
      
      





We start compiling and installing the kernel:



make && make INSTALL_MOD_STRIP=1 modules_install && make install
      
      





Our machine is very weak, so this process will take a long time. When the compilation is over, you need to update the bootloader configuration. I added the kernel options that were used in the original ubuntu.



cat >/boot/grub/grub.cfg <<EOF
set timeout=1
menuentry 'gentoo' {
  linux $(ls -t /boot/vmlinuz-*|head -n1) root=/dev/sda1 console=tty1 console=ttyS0 nvme.shutdown_timeout=10 libiscsi.debug_libiscsi_eh=1
}
EOF
      
      





It makes sense to allow login on the serial console if there are network problems:



sed -i -e 's/^#\(.*ttyS0.*\)/\1/' /etc/inittab
      
      





Left a little. After reboot, the network interface will not be named ens3, but eth0. It is necessary to remove the initialization script of the old interface and add the initialization of the new one:



cd /etc/init.d && rm net.ens3 && ln -s net.lo net.eth0
      
      





If you need to log in via the serial console, you need to set a password for the root user:



passwd
      
      





Everything is ready, you can reboot.



If after rebooting you are unable to log into the machine via ssh, create a console connection and use it for debugging.



You now have a basic gentoo installation that can be modified to suit your needs. There are artifacts left in the system from the original ubuntu. They are no longer needed and can be removed. The scripts with migration commands can be found here .



All Articles