Skip to content

Instantly share code, notes, and snippets.

@yan12125
Created April 14, 2017 17:32
Show Gist options
  • Star 22 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save yan12125/78a9004acb1bed5faf2ffd442163e2ef to your computer and use it in GitHub Desktop.
Save yan12125/78a9004acb1bed5faf2ffd442163e2ef to your computer and use it in GitHub Desktop.
Build a custom kernel for Android emulator
git clone https://android.googlesource.com/kernel/goldfish/ -b android-goldfish-3.18
git clone https://android.googlesource.com/platform/prebuilts/gcc/linux-x86/x86/x86_64-linux-android-4.9
cd goldfish
export CROSS_COMPILE=x86_64-linux-android-
export ARCH=x86_64
export PATH=$PATH:/path/to/x86_64-linux-android-4.9/bin
make x86_64_ranchu_defconfig
make menuconfig  # enable overlayfs and namespaces support here
make -j8

Then create an Android virtual device via android avd. Here I use x86-64 image for API 24 (7.0 Nougat). Don't forget to use a larger internal memory size when creating the virtual device. (The default is 200MB and I use 2000MB) After that run the emulator with:

emulator -avd (avd_name) -kernel /path/to/goldfish/arch/x86/boot/bzImage -show-kernel
@shayan-pakzad
Copy link

Can you upload your bzImage here?

@yan12125
Copy link
Author

Seems related: anbox/anbox#337. I'm not sure whether the latest system images includes that fix or not.

I don't have time to build a custom kernel now. Will post updates if any.

@harishvk27
Copy link

Latest system images don't yet include the fix..

you have to build ASOP and get this fix to thread.cc added. I tried it worked.

I will have a blog get each and every step. But for now.. if you get the fix included things work.

@ppoffice
Copy link

I tried x86, x86_64 and arm architecture in 3.18 and they all have this problem. But the 3.10 branch runs perfectly.

@harishvk27
Copy link

With 3.10 branch.. what configuration is required while building AVD ? which API ?
If you can list some instructions that would be helpful for only kernel developers who don't want to build custom ASOP.

For me.. to get the thread.cc fix in custom ASOP has to be build.

@harishvk27
Copy link

Steps to follow

  1. SYSTEM REQUIREMENTS
    I had ubuntu 16.04 with 16GB RAM and 1TB disk

also install
openjdk-8
git

  1. BASIC:
    =====
    mkdir /bin
    PATH=
    /bin:$PATH
    curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo

  2. GET ANDROID_SOURCE CODE and BUILD same
    ===================

mkdir ~/WORKING_DIRECTORY
cd ~/WORKING_DIRECTORY
repo init -u https://android.googlesource.com/platform/manifest -b android-7.0.0_r3
repo sync
source ./build/envsetup.sh
lunch asop_x86_64-eng

GET THE FIX into thread.cc ( in simple way: remove lines prefixed with - and add lines prefixed with + )

  • // Read every page from the high address to the low.
  • volatile uint8_t dont_optimize_this;
  • UNUSED(dont_optimize_this);
  • for (uint8_t* p = stack_top; p >= pregion; p -= kPageSize) {
  • dont_optimize_this = *p;
  • }
  • struct RecurseDownStack {
  • // This function has an intentionally large stack size.
    +#pragma GCC diagnostic push
    +#pragma GCC diagnostic ignored "-Wframe-larger-than="
  • NO_INLINE
  • static void Touch(uintptr_t target) {
  •  volatile size_t zero = 0;
    
  •  // Use a large local volatile array to ensure a large frame size. Do not use anything close
    
  •  // to a full page for ASAN. It would be nice to ensure the frame size is at most a page, but
    
  •  // there is no pragma support for this.
    
  •  volatile char space[kPageSize - 256];
    
  •  char sink ATTRIBUTE_UNUSED = space[zero];
    
  •  if (reinterpret_cast<uintptr_t>(space) >= target + kPageSize) {
    
  •    Touch(target);
    
  •  }
    
  •  zero *= 2;  // Try to avoid tail recursion.
    
  • }
    +#pragma GCC diagnostic pop
  • };
  • RecurseDownStack::Touch(reinterpret_cast<uintptr_t>(pregion));

make -j8

  1. DOWNLOAD KERNEL AND BUILD
    =====================

git clone https://android.googlesource.com/kernel/goldfish/ -b android-goldfish-3.18
cd goldfish
export CROSS_COMPILE=x86_64-linux-android-
export ARCH=x86_64
export PATH=$PATH:~/WORKING_DIRECTORY/prebuilts/gcc/linux-x86/x86/x86_64-linux-android-4.9/bin
make x86_64_ranchu_defconfig
make menuconfig # enable overlayfs and namespaces support here
make -j8

  1. now it's time to lauch emulator:
    =====================

./emulator -mem 2048 ./arch/x86/boot/bzImage

To figure out all these took me some time.. as i had to build android also.. which i think could be avoided.

Any way welcome to word of android system programming, wish you all the best.

-thanks
harish

@ppoffice
Copy link

@harishvk27 Thanks. I built emulator images from the Android source master branch and booted with my custom kernel, and it worked out. It seems that they have fixed it in thread.cc.

I also used 3.10 kernel branch with the same configurations @yan12125 posted and booted it with Android N SDK 24.

@harishvk27
Copy link

One thing i am currently on is.. how to add network support..

Normal kernel which is part of ASOP when loaded on emulator.. internet is working fine.

But with custom kernel i don't see it working.

Is there a place in ASOP where i get to know the .config file ( menuconfig ) with which ASOP kernels are built?.

-thanks
harish

Copy link

ghost commented Nov 27, 2017

Hello,

@harishvk27: With the AOSP if you did not specify a TARGET_PREBUILT_KERNEL the common kernel is used by default.
You can export TARGET_PREBUILT_KERNEL=/your/default/goldfish/arch/$ARCH/boot/zImage

  • In BoardConfig.mk
    • Remove TARGET_NO_KERNEL if set
  • In your device Makefile, have something like
    ifeq ($(TARGET_PREBUILT_KERNEL),)
    LOCAL_KERNEL:= device/ti/panda/kernel
    else
    LOCAL_KERNEL:=$(TARGET_PREBUILT_KERNEL)
    endif
    PRODUCT_COPY_FILES := $(LOCAL_KERNEL):kernel

If that doesn't work you can replace the x86_64_defconfig config in the kernel folder ($AOSP/kernel/) with your own config and recompile the Kernel, you need also to remove the common Kernel that is used by default.

Best regards,

@llllIIIllll
Copy link

hello,
I run "adb devices",but it show "emulator 5554 offline"
emulator is use of AOSP
make aosp_arm64-eng
kernel 3.10 arm64
how to solve the problem?

@palratnesh05
Copy link

Hi I did not find #enable overlayfs and namespaces support here in menu

rudra@rudra-ThinkPad-E470:~/aosp7/goldfish$ make menuconfig
HOSTCC scripts/kconfig/mconf.o
HOSTCC scripts/kconfig/lxdialog/checklist.o
HOSTCC scripts/kconfig/lxdialog/util.o
HOSTCC scripts/kconfig/lxdialog/inputbox.o
HOSTCC scripts/kconfig/lxdialog/textbox.o
HOSTCC scripts/kconfig/lxdialog/yesno.o
HOSTCC scripts/kconfig/lxdialog/menubox.o
HOSTLD scripts/kconfig/mconf
scripts/kconfig/mconf Kconfig

*** End of the configuration.
*** Execute 'make' to start the build or try 'make help'.

make completed successfully (02:23 (mm:ss))

rudra@rudra-ThinkPad-E470:~/aosp7/goldfish$ make -j4
make: x86_64-linux-android-gcc: Command not found
scripts/kconfig/conf --silentoldconfig Kconfig
make: x86_64-linux-android-gcc: Command not found
SYSHDR arch/x86/syscalls/../include/generated/asm/unistd_32_ia32.h
SYSTBL arch/x86/syscalls/../include/generated/asm/syscalls_32.h
HOSTCC scripts/basic/bin2c
CHK include/config/kernel.release
UPD include/config/kernel.release
SYSHDR arch/x86/syscalls/../include/generated/asm/unistd_64_x32.h
SYSTBL arch/x86/syscalls/../include/generated/asm/syscalls_64.h
WRAP arch/x86/include/generated/asm/clkdev.h
WRAP arch/x86/include/generated/asm/cputime.h
WRAP arch/x86/include/generated/asm/dma-contiguous.h
WRAP arch/x86/include/generated/asm/early_ioremap.h
WRAP arch/x86/include/generated/asm/mcs_spinlock.h
WRAP arch/x86/include/generated/asm/scatterlist.h
SYSHDR arch/x86/syscalls/../include/generated/uapi/asm/unistd_32.h
CHK include/generated/uapi/linux/version.h
UPD include/generated/uapi/linux/version.h
SYSHDR arch/x86/syscalls/../include/generated/uapi/asm/unistd_64.h
CHK include/generated/utsrelease.h
HOSTCC scripts/genksyms/genksyms.o
UPD include/generated/utsrelease.h
SHIPPED scripts/genksyms/parse.tab.c
SYSHDR arch/x86/syscalls/../include/generated/uapi/asm/unistd_x32.h
CC scripts/mod/empty.o
/bin/sh: 1: x86_64-linux-android-gcc: not found
scripts/Makefile.build:257: recipe for target 'scripts/mod/empty.o' failed
make[2]: *** [scripts/mod/empty.o] Error 127
scripts/Makefile.build:402: recipe for target 'scripts/mod' failed
make[1]: *** [scripts/mod] Error 2
make[1]: *** Waiting for unfinished jobs....
SHIPPED scripts/genksyms/lex.lex.c
SHIPPED scripts/genksyms/keywords.hash.c
SHIPPED scripts/genksyms/parse.tab.h
HOSTCC scripts/genksyms/parse.tab.o
kernel/Makefile:145: *** No X.509 certificates found ***
HOSTCC scripts/genksyms/lex.lex.o
HOSTLD scripts/genksyms/genksyms
Makefile:556: recipe for target 'scripts' failed
make: *** [scripts] Error 2
make: *** Waiting for unfinished jobs....

make failed to build some targets (2 seconds)

please help me how to resolve this problem?

@PGoski
Copy link

PGoski commented Jan 19, 2020

rudra@rudra-ThinkPad-E470:~/aosp7/goldfish$ make -j4
make: x86_64-linux-android-gcc: Command not found

checkout the correct branch in x86_64-linux-android-4.9 repository:

  1. cd x86_64-linux-android-4.9
  2. git checkout pie-b4s4-release

now kernel should start compiling

@johnnyboy18
Copy link

rudra@rudra-ThinkPad-E470:~/aosp7/goldfish$ make -j4
make: x86_64-linux-android-gcc: Command not found

checkout the correct branch in x86_64-linux-android-4.9 repository:

1. cd x86_64-linux-android-4.9

2. git checkout pie-b4s4-release

now kernel should start compiling

Thank You!

@ysun
Copy link

ysun commented May 6, 2020

Hi, as Android kernel is moving on to v5.4, do you have guide to boot Android emulator with customized kernel v5.4 ?
As I know, latest kernel use clang instead of gcc to compile. And android 5.4 have dozens of modules (.ko) packaged in ramdisk, which make it difficult to customize kernel.

@newbit1
Copy link

newbit1 commented Apr 21, 2021

Hi, as Android kernel is moving on to v5.4, do you have guide to boot Android emulator with customized kernel v5.4 ?
As I know, latest kernel use clang instead of gcc to compile. And android 5.4 have dozens of modules (.ko) packaged in ramdisk, which make it difficult to customize kernel.

Hi, I know your post was a while ago, but I ran into the same issue and fixed it. Check out my script rootAVD, there is a little Guide linked to XDA, on how to compile the AVD Kernel, its Modules and install the Modules into the ramdisk.img
It also installs Magisk on the same shot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment