# Installing Gentoo Linux on Ryzen Mobile

## Brand new Ryzen and latest Gentoo Linux

The enthusiasm of Linux Kernel for supporting Ryzen mobile low-voltage CPU seems to match the popularity of the Ryzen laptops on the market. Although the Ryzen desktop CPU has been supported since Linux 4.11, the kernels whose version below 4.17 cannot perform normally on its mobile CPU.

When I am writing this post (2018.07), the kernel version for most distributions is < 4.17 by default, so there are only several radical Linux distributions that can meet my demand, and Gentoo is one of them.

After going through the Gentoo Handbook, which is more friendly than before it was, I felt that there came a forceful excuse for me to ‘enjoy’ it this time. The interest to use Gentoo was intrigued again 😂, so here comes the post to note several tips during installation.

## Use Arch Linux installation image

The official installation image of rolling release provided by Gentoo does not support UEFI boot (2018.07). It does offer an image made in 2016 that supports UEFI boot. However, I know clearly that this image will not load successfully on my machine (the kernel is too old for Ryzen mobile), so using Arch Linux image with latest kernel might be the best choice.

You may want to know that Gentoo Handbook has been updated. “As of August 23, 2018 the official Minimal CDs are capable of booting in UEFI mode. Previous versions boot in BIOS (MBR) mode only. Readers looking to make their system UEFI bootable must download the latest ISO.” As it mentions, the latest ISO supports UEFI boot. It is unnecessary to use Arch Linux installation image from now on.

## Partitioning and prepare to use EFISTUB to boot

My scheme for partition obeys the Preparing the disks section of the Handbook, which is mounting the /root partition onto /mnt/gentoo and then referring to EFI system partition to mount the EFI partition on /mnt/gentoo/esp and mount /esp/EFI/Gentoo on /mnt/gentoo/boot

In this way, the kernel files can be updated without needs to manually update the files in /boot, at the same time, you keep the ESP partition tidy. You only need to use the old method. Copy the files in /boot to /esp/EFI/Gentoo.

For information on the network configuration, see Network configuration

It is recommended to use the Links mentioned in the Handbook to download the Stage3 mirror. The Arch Linux image does not have Links built-in, so you should use pacman to install Lynx or Links to do this in the first place. My approach is to copy the pre-downloaded Stage3 image to the bootable disk in advance when creating the Arch Linux boot disk and then copy it to the /mnt/gentoo directory.

## Modifying compile options

Add or change some options in make.conf to add support for Ryzen.

znverl which is required by Ryzen CPU should be put in the CFLAGS -march. If your device works with Radeon VEGA GPU, you should add radeonsi to the VIDEO_CARDS option. Laptop users should turn on Synaptics support. In addition, tuna 🐟’s mirror is chosen to GENTOO_MIRRORS for its satisfying speed. The MAKEOPTS options follows the rule of the number of CPU cores plus 1. Take Ryzen 2700U as an example, i.e. -j5

The following is an example of make.conf:

CFLAGS="-march=znver1 -O2 -pipe"
CXXFLAGS="\${CFLAGS}"
MAKEOPTS="-j5"
INPUT_DEVICES="evdev synaptics"
LANGUAS="zh_CN"
GENTOO_MIRRORS="https://mirrors.tuna.tsinghua.edu.cn/gentoo"


Information about other options can be found in Configuring compile options

## Choose rsync mirrors

Gentoo allows you to specify the rsync mirror server by editing the portage repository’s profile. Of course, you can choose the mirrors of tuna or USTC, if you locate in China Mainland.

Curiosity drove me to go through the official Gentoo rsync mirrors page, but I didn’t find mirrors from China Mainland (2018.07), perhaps because of the existing mirrors of universities and companies in China Mainland haven’t meet the demands from Gentoo? It doesn’t say much for the speed of mirrors from Taiwan, China either. I found the rsync mirror by KAIST by accident. Although it only supports IPv4, it has very satisfactory performance. Let’s call it an unexpected discovery.

🎉 Congratulate TsingHua to host the only official rsync mirror certified by Gentoo Project in China Mainland (2019.01).

## About --make-rslave

Mounting the necessary filesystems mentions that enabling systemd support requires the --make-rslave parameter after mount. The man page of the mount command indicates that this allows one to assign the directory with the propagation type of -slave recursively by using --make-rslave, i.e. the directory including all of its subdirectory alters with the master, but they do not notify the master of their change.

If you don’t use systemd, you can also add this option, so that you are not able to use umount -R to force the partition to be unloaded.

## Select profile

When using eselect to select a profile, you need to pay attention to the kernel version. For Gentoo, the Linux kernel 4.17 at the time I am writing this (2018.07) is still an experimental kernel, so the options are near the bottom of the list.

If you attempt to install Gentoo in a minimal way as I do, you should choose the option requiring the fewest dependencies (that is, the option with the shortest text). In particular, avoid the option of having a desktop environment and it is not recommended to execute this task in the installation process even if it is needed later, so as to avoid overloading the CPU in a short period. No one is willing to cook their hands into roast ones by its own laptop 🔥.

## Configuration of USE Flags

USE Flags is where the charm of Gentoo says 😎, which offers the possibility of streamlining the system, keeping the system tidy and maximizing efficiency. But its configuration is not done overnight. I tend to set a few global USE Flags and set local USE Flags for each package.

Although this is an achievement that needs to be maintained for quite a time, you can configure -qt4 -qt5 -kde at the beginning to avoid unnecessary dependencies. In ordinary days, it is necessary to prevent installing softwares that rely on flags that have been banned such as Qt. In addition, the USE Flags page in the Handbook is a good way to take deeper look at it.

## Manually compiling the kernel

Choosing genkernel is a painless way to compile the kernel, but that means the kernel won’t turn on options related with Ryzen. The conservative way should be to genkernel all and then add the relevant kernel options and recompile the kernel. The disadvantage is that it costs time and resource, and the compiled kernel turns out to be huge.

First, install AMD Zen microcode by emerging linux-firmware

emerge --ask sys-kernel/linux-firmware


Don’t forget to install AMD GPUs drivers.

emerge --ask x11-drivers/xf86-video-amdgpu


Use menuconfig to enable several options mentioned below to provide support for AMD Ryzen CPU and AMD GPUs, as well as kernel options related with EFI stub kernel.

Processor type and features --->
[*] Symmetric multi-processing support
[*] AMD ACPI2Platform devices support
[*] /dev/cpu/microcode - microcode support
[*] MTRR (Memory Type Range Register) support

Processor family (Opteron/Athlon64/Hammer/K8) --->
(X) Opteron/Athlon64/Hammer/K8

[*] Supported processor vendors --->
[*] Support AMD processors (NEW)

[*] Multi-core scheduler support
[*] Machine Check / overheating reporting
[*] AMD MCE features

Processor family (MZEN) --->
(X) AMD Zen

Performance monitoring --->
<*> AMD Processor Power Reporting Mechanism

[*] EFI runtime service support
[*] EFI stub support
[ ] EFI mixed-mode support

[*] Built-in kernel command line
(root=PARTUUID=)

Power management and ACPI options --->
CPU Frequency scaling --->
<*> AMD Opteron/Athlon64 PowerNow!
<*> AMD frequency sensitivity feedback powersave bias

Device Drivers --->
Generic Driver Options --->
(amd-ucode/microcode_amd_fam17h.bin) External firmware blobs to build into the kernel binary
(/lib/firmware) Firware blobs root directory

Graphics support --->
<*/M> Direct Rendering Manager (XFree86 4.1.0 and higher DRI support) --->
[*] Enable legacy fbdev support for your modesetting driver
AMD GPU
[ /*] Enable amdgpu support for SI parts
(only needed for Southern Islands GPUs with the amdgpu driver)
[ /*] Enable amdgpu support for CIK parts
(only needed for Sea Islands GPUs with the amdgpu driver)
[*] Enable AMD powerplay component

ACP (Audio CoProcessor) Configuration --->
[*] Enable AMD Audio CoProcessor IP support (CONFIG_DRM_AMD_ACP)

Display Engine Configuration --->
[*] AMD DC - Enable new display engine
[ /*] DC support for Polaris and older ASICs
(only needed for Polaris, Carrizo, Tonga, Bonaire, Hawaii)
[ /*] AMD FBC - Enable Frame Buffer Compression
[ /*] DCN 1.0 Raven family
(only needed for Vega RX as part of Raven Ridge APUs)
<*/M> HSA kernel driver for AMD GPU devices

<*/M> Sound card support --->
<*/M> Advanced Linux Sound Architecture --->
[*] PCI sound devices --->
HD-Audio --->
<*> HD Audio PCI
<*> whatever audio codec your soundcard needs
<*> Build HDMI/DisplayPort HD-audio codec support
(2048) Pre-allocated buffer size for HD-audio driver

Firmware Drivers --->
EFI (Extensible Firmware Interface) Support --->
<*> EFI Variable Support via sysfs

[*] IOMMU Hardware Support --->
[*] AMD IOMMU support
<*> AMD IOMMU Version 2 driver

-*- Enable the block layer --->
Partition Types --->
[*] EFI GUID Partition support


Built-in kernel command line in Processor type and features needs to be filled in with paths to /root and initrd, like

root=/dev/nvme0n1p3 rw initrd=/EFI/Gentoo/initramfs-linux.img


About Ryzen, AMDGPU and AMD_microcode of the chapters in the Handbook provide a great deal of help for working on the content of this section.

## Creating fstab file

If following configuration for EFISTUB above, state the binding mounts on the EFI partitionin fstab file. Here is an example:

/dev/nvme0n1p1   /esp   vfat    defaults,noatime    0   0
/esp/EFI/Gentoo  /boot  none    defaults,bind       0   0
/dev/nvme0n1p2   none   swap    sw                  0   0
/dev/nvme0n1p3   /      btrfs   ssd,noatime         0   1


Using blkid to get the file system UUID to replace its partition or device name is recommended. Remember not to miss the prefix UUID=. If your SSD supports the TRIM command, you can add the mount option discard to improve read/write performance. If your file system is Btrfs, the ssd option is good to go, but according to the description of Optimization for SSD in the Btrfs Wiki TRIM/discard support is turned on by adding the ssd option, and ssd is also recommended for better compatibility.

Talking about the UEFI boot entry, Gentoo Handbook and other distributions’ wikis recommend use efibootmgr to manage them. efibootmgr is simple and convenient indeed, but it is not able to recognize NVMe (Non-Volatile Memory express) device partitions.

For example, /dev/nvme0n1p1 is an illegal parameter to efibootmgr.

Since efibootmgr only has a soft spot for sda, I used the UEFI Shell to manually add entries to boot multiple operating systems.

Then follow the Handbook to configure the network, and never forget to install the relevant package on the wireless network. Circling to look for network cables and humping at the corner by the router is not cool at all 😂

emerge --ask net-wireless/iw net-wireless/wpa_supplicant


## Conclusion

I have to say that if Ryzen has not given me a good enough excuse, maybe I wouldn’t install Gentoo Linux again. Anyway, thank you for offering me this wonderful experience. Hope that we can get well along in the next couples of months 💗