4

How do I properly build an Android kernel then build the AOSP with this kernel? For now I am virtualizing Android on a Linux x86_64 host. The AOSP lunch option I use is aosp_cf_x86_64_phone-userdebug. The AOSP is on the master branch and synced back in January 2022.

I am building my own Android kernel by following these instructions: https://source.android.com/setup/build/building-kernels

I have done these steps:

  1. repo synced to the common-android12-5.10 branch on https://android.googlesource.com/kernel/manifest
  2. BUILD_CONFIG=common/build.config.gki.x86_64 build/build.sh
  3. BUILD_CONFIG=common-modules/virtual-device/build.config.virtual_device.x86_64 build/build.sh

This gives me a android-kernel/out/android12-510/dist/bzImage. I tried following the instructions of https://source.android.com/setup/build/building-kernels#running by doing export TARGET_PREBUILT_KERNEL=$(realpath android-kernel/out/android12-510/dist/bzImage) then m bootimage and make bootimage, neither make or m copied over the kernel.

So I then tried hard copy-pasting over the prebuilt kernel in aosp/kernel/prebuilts/5.10/x86_64 where I pasted over kernel-5.10 and kernel-5.10-allsyms then just kernel-5.10 where make bootimage and m bootimage copied over my built kernel. But both ways didn't have Android bootup via launch_cvd, it kept rebooting over and over.

What is the proper way of providing your custom built Android kernel to the AOSP and building the AOSP with the kernel? Why isn't Android starting up with a hard copy-pasted kernel file?

davidj361
  • 147
  • 7
  • 2
    https://github.com/nathanchance/android-kernel-clang/blob/master/README.md – alecxs Feb 04 '22 at 17:48
  • Why clang? Isn't that the non-normal way? From what I've seen so far from Google's documentation there isn't any mention of Clang and utilizes Soong instead. – davidj361 Feb 08 '22 at 15:11
  • Sorry I don't know how to build ROM but I guess your kernel did not cross-compile for aarch64 successfully – alecxs Feb 08 '22 at 15:51
  • 1
    Afaik, the kernel was `x86_64`. Notice the`build.config.virtual_device.x86_64` and `build.config.gki.x86_64`. Also the AOSP should be `x86_64` as the lunch option picked was `aosp_cf_x86_64_phone-userdebug` – davidj361 Feb 09 '22 at 14:53
  • most mobile phones running arm cpu guess it's some kind of notebook or tv box? – alecxs Feb 09 '22 at 15:25
  • I'm purely virtualizing for now with everything x86_64. – davidj361 Feb 09 '22 at 16:43

2 Answers2

0

I had similar problem when following this article to build kernel and try to use it with goldfish emulator (the emulator which runs with "emulator" command). Although not exactly the same as cuttlefish, but the problem is similar. Just post it here in case anybody need this.

https://source.android.google.cn/docs/setup/build/building-kernels#embedding-into-Android

It says you have to set TARGET_PREBUILT_KERNEL to point to your kernel image. However, this won't work for emulator.

export TARGET_PREBUILT_KERNEL=DIST_DIR/Image.lz4-dtb

  1. The TARGET_PREBUILT_KERNEL will not be used when building with "lunch sdk_x86_64-eng" config.
  2. There is no Image.lz4-dtb in the kernel build output.

The reason is with "lunch sdk_x86_64-eng", AOSP will use build config for goldfish, located at device/generic/goldfish. And the kernel config for gold fish is specificed in device/generic/goldfish/x86_64-kernel.mk

TARGET_KERNEL_USE ?= 5.10

KERNEL_MODULES_PATH := kernel/prebuilts/common-modules/virtual-device/$(TARGET_KERNEL_USE)/x86-64

KERNEL_MODULES_EXCLUDE :=
$(KERNEL_MODULES_PATH)/virt_wifi.ko
$(KERNEL_MODULES_PATH)/virt_wifi_sim.ko

BOARD_VENDOR_RAMDISK_KERNEL_MODULES +=
$(filter-out $(KERNEL_MODULES_EXCLUDE), $(wildcard $(KERNEL_MODULES_PATH)/*.ko))

EMULATOR_KERNEL_FILE := kernel/prebuilts/$(TARGET_KERNEL_USE)/x86_64/kernel-$(TARGET_KERNEL_USE)

Three settings are specified here

  1. Use kernel version 5.10
  2. The kernel module path, which is the GKI moduoles, a bunch of *.ko files - KERNEL_MODULES_PATH. Note this is adirectory.
  3. The kernel file path, the kernel image file path - EMULATOR_KERNEL_FILE.

You can actaully find 2) and 3) in the AOSP kernel directory.

What you should do is change KERNEL_MODULES_PATH and EMULATOR_KERNEL_FILE to the custom built kernel out put dir and file. Say

KERNEL_MODULES_PATH=/kernel/out/dir # a dir containing a bunch of *.ko files
KERNEL_MODULES_PATH=/kernel/out/dir/bzImage # a file which is about 20M or so

After changing this file, clean and rebuild AOSP. Then the emulator will run with your custom kernel.

For cuttle fish, aosp_cf_x86_64_phone-userdebug target, the kernel config file is located at device/google/cuttlefish/vsoc_x86_64/kernel.mk

TARGET_KERNEL_USE ?= 5.10 TARGET_KERNEL_PATH ?= kernel/prebuilts/$(TARGET_KERNEL_USE)/x86_64/kernel-$(TARGET_KERNEL_USE) PRODUCT_COPY_FILES += $(TARGET_KERNEL_PATH):kernel

I'm not sure why it only has the TARGET_KERNEL_PATH, but no kernel module path(the GKI modules, the *.ko files). You should be able to trace that from this file device/google/cuttlefish/vsoc_x86_64/phone/aosp_cf.mk

tj444
  • 1
0

Step 1 Code sync repo synced to the common-android12-5.10 branch on https://android.googlesource.com/kernel/manifest

Step 2 Build the Android common kernel Build Config for Android common kernel I am going to build common-android13-5.15 hence using below configuration

BUILD_CONFIG=common-modules/virtual-device/build.config.cuttlefish.x86_64 
build/build.sh

Then run below command for Emulator

BUILD_CONFIG=common-modules/virtual-device/build.config.virtual_device.x86_64
build/build.sh

After build complete, check the kernel file

/home/saurabh/kernel-common-android13-5.15/out/android13-5.15/dist/bzImage

Step 3 Include custom kernel in AOSP Build System

Edit the kernel make file that generally lies in your device folder. For Emulator path is

device/generic/goldfish/x86_64-kernel.mk

Step 4 Build Complete AOSP and Run Emulator After modifying the Kernel make file , you need to rebuild AOSP completely to get Kernel image replaced.

make -j8

After successful build Run the emulator with below Command

emulator -verbose -show-kernel -selinux permissive -writable-system

For more detail follow my blog https://saurabhsharma123k.blogspot.com/2023/07/build-android-kernel-and-load-your.html

SAURABH_12
  • 2,262
  • 1
  • 19
  • 19