Monday, March 2, 2015

Build and run android linux kernel in emulator

If we want to learn android linux kernel, do something about it is the best way. Here records the steps to build and run android linux kernel in emulator. It is a good start for customizing the kernel.

Some steps may have alternative choices, but the following steps are proved by my own practice.
0. We need a linux-x86/linux-x86_64 machine.

1. Download aosp sources. We need the prebuilt gcc tool chain. From https://source.android.com/source/downloading.html.
   $cd aosp_master
   $repo init -u https://android.googlesource.com/platform/manifest
   $repo sync -c -j256
   $export PATH=$PATH:`pwd`/prebuilts/gcc/linux-x86/arm/arm-eabi-4.8/bin

2. Download and build goldfish kernel source. It's the kernel used by android emulator. From http://source.android.com/source/building-kernels.html.
   $git clone https://android.googlesource.com/kernel/goldfish.git
   $cd goldfish
   # We can use git branch -r to find which remote branch we want to checkout.
   $git checkout -t origin/android-goldfish-3.4 -b goldfish3.4
   $export ARCH=arm
   $export SUBARCH=arm
   $export CROSS_COMPILE=arm-eabi-
   $make goldfish_armv7_defconfig
   $make
   Now we get the kernel zImage in arch/arm/boot/zImage

3. Download android sdk. From http://developer.android.com/sdk/installing/index.html and http://developer.android.com/sdk/installing/adding-packages.html.
   Download android sdk from http://developer.android.com/sdk/installing/index.html.
   Run tools/android, install packages (In my experience, the default option to install is enough for this experiment).
   Create an android virtual device for arm_v7. You can see a list of available targets by `tools/android list targets` command. In my enviroment, I create a avd by the following command:
   $./android create avd --name myavd --target 1 --abi default/armeabi-v7a
   You can see what avds you have by:
   $./emulator -list-avds

4. Good, now its time to run emulator with the kernel.
   $./emulator -avd myavd -kernel "path to the arch/arm/boot/zImage"
   We can verify what kernel is used by add some options in emulator.
   $./emulator -avd myavd -kernel "path to the arch/arm/boot/zImage" -show-kernel -verbose 2>&1 | tee emulator.log

Now we can have fun, looking around to see what we can do with the android linux kernel!


No comments:

Post a Comment