Building the Linux Asahi kernel

Published on March 20, 2023

Introduction

Since the announcement of the Asahi Linux Alpha Release, the team behind Asahi Linux provides a very simple script that you can run from a terminal on MacOS to install AsahiLinux:

curl https://alx.sh | sh

If you carefully follow the instructions while running the shell script, you should end up with a functional ArchLinux-based Linux system (depending on the option you chose during the installation, of course). Since support for Apple Silicon is still being upstreamed to the Linux kernel, ArchLinux does not yet support Apple Silicon out of the box. Instead Asahi Linux provides its own packages for ArchLinux to support Apple Silicon hardware, including their fork of the Linux kernel through the linux-asahi package. These PKGBUILDs can be found on Github.

As such, if you are trying to build a Linux kernel module, for example, you may need to install the linux-asahi-headers package (not the linux-aarch64-headers package):

sudo pacman -Syu linux-asahi-headers

Sometimes it might be interesting to try out a newer version of the Linux kernel, or to run the Linux kernel with custom patches. This guide shows how to build m1n1 and Linux Asahi from their respective source code using the provided PKGBUILDs, as well as how to modify the PKGBUILDs.

Building m1n1 and the Linux Asahi kernel from source

First, we clone the PKGBUILDs repository:

git clone https://github.com/AsahiLinux/PKGBUILDs.git

Then we point the terminal to the m1n1 directory:

pushd PKGBUILDs/m1n1

Run makepkg -s to build the m1n1 package:

makepkg -s

If everything went well, you should now be able to install the m1n1 package using pacman:

sudo pacman -U m1n1-1.2.3-1-aarch64.pkg.tar.xz

Finally, run popd to go back to the original directory.

To build the Linux kernel, we need to install version 1.62 of the Rust toolchain (at the time of writing). We first install rustup through pacman:

sudo pacman -Syu rustup

Then we use rustup to install the toolchain:

rustup toolchain install 1.62.0

Similar to building the m1n1 package, we point the terminal to the linux-asahi directory:

pushd PKGBUILDs/linux-asahi

Run makepkg -s with MAKEFLAGS set to -j8 (where 8 is the number of CPU cores):

MAKEFLAGS="-j8" makepkg -s

Install the linux-asahi package using pacman:

sudo pacman -U linux-asahi-6.1.asahi3-1-aarch64.pkg.tar.xz linux-asahi-headers-6.1.asahi3-1-aarch64.pkg.tar.xz

You may also want to install the linux-asahi-edge if you are normally running linux-asahi-edge (make sure you have mesa-asahi-edge installed as well):

sudo pacman -U linux-asahi-edge-6.1.asahi3-1-aarch64.pkg.tar.xz

Update the GRUB configuration file to reference the newly installed Linux kernel:

sudo update-grub

Finally, run popd to go back to the original directory.

If everything went well, you should be able to boot the system with the new Linux kernel.

Updating m1n1 and Linux Asahi

The released versions of m1n1 can be found on Github. For instance, to install m1n1 1.2.6, we can simply edit pkgver in PKGBUILDs/m1n1/PKGBUILD:

pkgname=m1n1
pkgver=1.2.6
pkgrel=1

Then we need to run updpkgsums to update the checksums and we should be able to install the package as before:

pushd PKGBUILDs/m1n1
updpkgsums
makepkg -s
sudo pacman -U m1n1-v1.2.6-1-aarch64.pkg.tar.xz
popd

Similarly, the released versions of Linux Asahi can be found on Github too. To install Linux Asahi 6.2.11, we can set _rcver to 6.2 and _asahirel to 11 in PKGBUILDs/linux-asahi/PKGBUILD:

_rcver=6.2
#_rcrel=
_asahirel=11
pkgrel=1

Also update _m1n1_version, if you changed the version of m1n1:

_m1n1_version=1.2.6

Update the package checksums and install the new package as follows:

pushd PKGBUILDs/linux-asahi
updpkgsums
MAKEFLAGS="-j8" makepkg -s
sudo pacman -U linux-asahi-6.2.asahi11-1-aarch64.pkg.tar.xz linux-asahi-edge-6.2.asahi11-1-aarch64.pkg.tar.xz linux-asahi-headers-6.2.asahi11-1-aarch64.pkg.tar.xz
popd
sudo update-grub

If everything went well, you should now be able to boot the system with linux-asahi-6.2-11. In addition, running uname -a should show something like the following:

Linux asahi 6.2.0-asahi-11-1-edge-ARCH #2 SMP PREEMPT_DYNAMIC Mon, 20 Mar 2023 07:17:32 +0000 aarch64 GNU/Linux

Patching the Linux kernel

To start writing and applying patches for the Linux kernel, we need to clone the Linux source code for the version that we installed using the PKGBUILDs (e.g. the tag for linux-asahi-6.2-11 is asahi-6.2-11). We will make sure to use --depth=1 to minimize the amount of disk space used:

git clone https://github.com/AsahiLinux/linux -b asahi-6.2-11 --depth=1

After modifying the Linux source code in the linux directory, we can simply create a patch using git diff:

git diff > ../PKGBUILDs/asahi-linux/0001-my-patch.patch

Add the patch(es) to source in PKGBUILDs/linux-asahi/PKGBUILD:

source=(
  https://github.com/AsahiLinux/linux/archive/${_commit_id}.tar.gz
  config         # the main kernel config file
  config.edge    # overrides for linux-asahi-edge
  0001-my-patch.patch

Update the package checksums:

pushd PKGBUILDs/linux-asahi
updpkgsums

Make sure the patch command is available:

sudo pacman -Syu patch

Build the package (you may need to pass -f if you already had the package built);

MAKEFLAGS="-j8" makepkg -s

Install the patched kernel using pacman:

sudo pacman -U linux-asahi-6.2.asahi11-1-aarch64.pkg.tar.xz linux-asahi-edge-6.2.asahi11-1-aarch64.pkg.tar.xz linux-asahi-headers-6.2.asahi11-1-aarch64.pkg.tar.xz

Update the GRUB configuration file to reference the newly installed Linux kernel:

sudo update-grub

Finally, run popd to go back to the original directory.

If everything went well, you should be able to boot the system with the patched Linux kernel.

Building Linux kernel modules

If you are building Linux kernel modules for Asahi Linux, you need to make sure the build directory is present in /lib/modules/$(uname -r)/build.

For the base config, you can simply copy the build directory from the PKGBUILD you built locally:

sudo cp -r ~/PKGBUILDs/linux-asahi/src/linux-asahi-6.2-11/build/base /lib/modules/6.2.0-asahi-11-1-ARCH/build

For the edge config, the command would be as follows:

sudo cp -r ~/PKGBUILDs/linux-asahi/src/linux-asahi-6.2-11/build/edge /lib/modules/6.2.0-asahi-11-1-edge-ARCH/build

You can also create symbolic links if you prefer that instead. With the build directory available, you should be able to simply build external Linux kernel modules.


If you like my work or if my work has been useful to you in any way, then feel free to donate me a cup of coffee. Any donation is much appreciated!