Installing Gentoo Linux in Apple Macbook Pro M1

Kellerman Rivero
Dev Genius
Published in
7 min readApr 15, 2022

--

Since long time ago I wanted to write my first post in Medium. So I decided to start my writer story showing you how to install Gentoo Linux in a Macbook Pro 2020 with M1 processor powered by a custom Linux Kernel coming from Asahi Linux project (a project that aims to bring Linux to Apple Silicon Macs).

At this point, Asahi ships with Arch Linux but there are few unofficial installing guides to get other Linux distributions installed, like Debian or Fedora. In this tutorial, we will see how we can leverage Asahi installer to do disk partitioning and setup booting process, then how to use Asahi Linux to prepare and install Gentoo Linux, and finally swapping one by the other.

Prerequisites:

  • You have previous experience installing Gentoo Linux.
  • You are familiar with Asahi Installer
  • USB Flash Stick: Used for store stage3 and portage tar files, so we do not need internet connection to fetch those files.
  • USB Ethernet Adapter: Used for internet connectivity until we get Wi-Fi adapter working and Wi-Fi configuration utilities installed in our base system.

Step 0 - Download Gentoo base system

Go to Gentoo website and then download Stage3 tarball file (for AArch64 architecture) and copy it over the USB Flash Stick.

Step 1 - Install Asahi Linux minimal environment

Let’s start running Asahi Linux installer, open a shell terminal and type the following command:

curl https://alx.sh | sh

After download installer bits and then entering your admin password, you will see a screen like this:

Asahi Linux installer, welcome screen

Press ‘Enter’ to continue (as screen suggests) to start Asahi setup, please read the instructions carefully, I found default options work for common use cases. It is outside of the scope of this tutorial Asahi Installer, I assume you will use default options and you will pick the OS option: “Asahi Linux Minimal (Arch Linux ARM)”.

At some point, the installer will display relevant bits of data, e.g. UUID of disk partitions created and then will ask us to read carefully next instructions. If you have followed the installer instructions correctly, you device will be shutdown and you will need to wait at least 15 seconds before pressing the power button.

Complete remaining steps of Asahi Linux installation and prepare for your first system boot. During the first boot you can log-in into Asahi Linux installation using default root credentials (root:root).

Let’s start preparing everything for Gentoo.

Step 2 - Preparing Gentoo base system

After log-in into Asahi Linux as root, it is necessary to mount the USB flash stick, create a folder for Gentoo files and copy the stage3 tarball contents into Gentoo folder:

# mkdir -p /mnt/gentoo
# mkdir -p /mnt/usb
# mount /dev/sda1 /mnt/usb
# cd /mnt/gentoo
# tar xpvf /mnt/usb/stage3-*.tar.xz —-xattrs-include=“*.*” —-numeric-owner
# cd

At this point, Gentoo base system files are installed into /mnt/gentoo folder. Now it’s time to copy some bits from Asahi installation, specifically the kernel, its modules and device firmware.

# cp -r /boot /mnt/gentoo
# cp -r /usr/lib/modules /mnt/gentoo/usr/lib
# cp -r /usr/lib/firmware /mnt/gentoo/usr/lib

Asahi Arch Linux uses zstd compression algorithm for kernel modules, unfortunately, Gentoo by default does not come with zstd support in kmod and systemd, therefore our Gentoo base system needs to be recompiled in order to be able to load these kernel modules.

Let’s start setting up make.conf file:

# nano /mnt/gentoo/etc/portage.conf 

Let’s tweak COMMON_FLAGS variable, I’ll enable “-march=native” to instruct system compiler to target M1 specific processor instruction set, this should translate into performance boost:

COMMON_FLAGS=“-march=native -O2 -pipe”

Now, let’s add MAKEOPTS variable:

MAKEOPTS=“-j4”

I often use “-j8”, to tell make to have up to 8 parallel build jobs, because my Macbook Pro has 8 cores, but if you have more cores, you can use a different number. Be aware, that official recommendation is to have as many make jobs as your RAM is capable of, assuming each job should have at least 2GiB of RAM available, this is really important for some packages (e.g. Chromium).

Finally, let’s add USE flags variable, I’m planning to install KDE in my Gentoo Linux, so beside zstd support, I also enable other flags.

USE=”-gtk -gnome X kde qt5 systemd zstd

Save the file, and now let’s mount some needed filesystems before we chroot into Gentoo installation.

# mount --types 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

Now, it’s time to chroot into Gentoo base system:

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

Before recompile Gentoo base system with zstd support, let’s install a Gentoo ebuild repository snapshot from the web.

# emerge-webrsync

Then, update @world set, which will recompile our system with zstd support. Depending of the stage3 tarball selected and network bandwidth this process may take several minutes.

# emerge --ask --verbose --update --deep --newuse @world

With base system updated, let’s proceed installing a couple of additional tools we will need soon.

# emerge —-ask net-misc/dhcpcd
#
emerge —-ask net-wireless/wpa_supplicant
# emerge —-ask net-wireless/iw

Now let’s setup root password:

# passwd

Step 3 - Backup Stage 4

The Gentoo base system is ready to be used, it will provide a minimal system in which a more complex system can be bootstrapped. Let’s backup our system to use it later.

First, a exclusion list is needed to avoid backing up some unnecessary folders:

# cat << EOF > /stage4.excl
.bash_history
/mnt/*
/tmp/*
/proc/*
/sys/*
/dev/*
/run/*
/stage4.excl
/stage4.tar.gz
EOF

Now, time to backup Gentoo base system (often called Stage4 at this point, since it has base system + kernel):

# tar cvzf /stage4.tar.gz -X /stage4.excl /

This process will take few minutes to backup entire system. After that, we can exit chroot, and copy our backup to the USB Flash Stick:

# exit
—- (Back to Asahi Linux)
#
cp /mnt/gentoo/stage4.tar.gz /mnt/usb

Finally, unmount filesystems and reboot the system:

# umount /mnt/usb
# reboot

Step 4 - Replace Asahi Linux with Gentoo

Next boot time we will stop in the GRUB bootloader screen by pressing arrow keys, this is a necessary step since we need to replace Asahi Linux installation with our Gentoo base system but that cannot be done with Asahi Linux running.

Then, press ‘e’ key to edit GRUB configuration. GRUB configuration tells bootloader where to find Linux Kernel and initial RAMDISK. The default configuration should looks like this:

setparams ‘Arch Linux’
load_video
set gfxpayload=keep
insmod gzip
insmod ext2
search —-no-floppy —-fs-uuid —-set=root <UUID>
echo ‘Loading Linux linux-asahi ...‘
linux /boot/vmlinuz-linux-asahi root=UUID=<UUID> rw loglevel=3 quiet
echo ’Loading initial ramdisk ...’
initrd /boot/initramfs-linux-asahi.img

Edit ‘linux’ entry and remove arguments passed to the kernel (in bold in previous block):

setparams ‘Arch Linux’
load_video
set gfxpayload=keep
insmod gzip
insmod ext2
search —-no-floppy —-fs-uuid —-set=root <UUID>
echo ‘Loading Linux linux-asahi ...‘
linux /boot/vmlinuz-linux-asahi
echo ’Loading initial ramdisk ...’
initrd /boot/initramfs-linux-asahi.img

This will cause kernel to boot into recovery mode, running an embedded environment from RAMDISK. Mount the USB Flash Disk and Asahi Root Partition:

[rootfs ]# mkdir -p /mnt/asahi /mnt/usb
[rootfs ]# mount /dev/sda1 /mnt/usb
[rootfs ]# mount /dev/nvme0n1p5 /mnt/asahi

Delete all the contents from Asahi Linux:

[rootfs ]# rm -Rf /mnt/asahi/*

Extract the contents of Stage 4 tarball file into Asahi Linux partition:

[rootfs ]# cd /mnt/asahi
[rootfs ]# tar xpvf /mnt/usb/stage4.tar.gz —-numeric-owner

Symlink Kernel modules and firmware directories:

[rootfs ]# chroot /mnt/asahi /bin/bash
—- Chroot Gentoo Linux —-
# rm -Rf /lib/firmware
# ln -s /usr/lib/firmware /lib/
# ln -s /usr/lib/modules /lib/

Finally, reboot into Gentoo:

# reboot -f

Step 5 - Getting Network Up in Gentoo Linux

After rebooting you can login with credentials you setup previously for root account. You probably will notice that there is no network connection, we need to configure Gentoo to get network access. I will show how to get Wi-Fi connection enabled manually and I assume you have enough experience from this point to finish setting up your Gentoo installation.

Let’s list all network interfaces available in the system:

# ifconfig -a

In my case, I see wlp1s0f0 network interface listed. Bring up this interface:

# ifconfig wlp1s0f0 up

Let’s validate the interface can scan networks:

# iw wlp1s0f0 scan

You should see nearby networks at this point. To connect to one of those networks:

# wpa_passphrase <SSID> <Password> >> /etc/wpa_supplicant.conf

Start wpa_supplicant and dhcpcd daemon :

# wpa_supplicant -B -i wlp1s0f0 -c /etc/wpa_supplicant.conf
# dhcpcd wlp1s0f0

At this point you should have a valid Gentoo installation and you can continue with Handbook installation guide.

Happy hacking!

--

--