How to: build a VirtualBox Android Linux kernel

From time to time, people ask me about building VirtualBox in Google's master branch or at the ICS release tag. They often wish to tweak the kernel. Building a Linux kernel for VirtualBox and including it in the image isn't difficult - provided that you have a little experience with the tools. Teaching the tools (git, make, ...) is beyond the scope of this posting, but I will provide some information on how to:
  • download the Linux kernel source
  • build the Linux kernel binary for a VB image
  • build a Linux kernel binary based on a newer kernel

  • Subsequent postings will provide information on how to include the kernel in your VB image, how to build your VB image, how to boot it under VB and how to boot it on real (physical) hardware.

    The AOSP master branch has a Linux kernel binary located in the prebuilts/qemu-kernel/x86/ directory. The kernel may be re-generated using a script (see the README) in the QEMU directory at external/qemu/distrib/build-kernel.sh. The file external/qemu/docs/ANDROID-KERNEL.TXT has the procedure for re-making the emulator kernel... but the same procedure is (mostly) used to remake the VB kernel. The document is a bit out of date (has the wrong URL and procedure for checking out the kernel tree). I plan to fix that... hopefully the fixes will be uploaded in the next few days.

    Building the 2.6.29 kernel
    The kernel-vbox and vmlinux-vbox binaries are based on the 2.6.29 Android Linux kernel tree.
    First, set some convenience variables:

    AOSP_SOURCE=$HOME/AOSP-ics
    KERN_SOURCE=$HOME/kernel


    The next step is to clone this tree:

    mkdir $KERN_SOURCE
    cd $KERN_SOURCE
    git clone https://android.googlesource.com/kernel/goldfish
    cd goldfish
    git checkout remotes/origin/android-goldfish-2.6.29


    Note: The kernel config file for vbox is in arch/x86/configs/vbox_defconfig

    There is a helper script in external/qemu/distrib/build-kernel.sh to rebuild the kernel... but you don't have to use it. If you don't mind using your installed (distro) compiler, you can build your kernel using the expected Linux kernel build procedure.

    cd $KERN_SOURCE/goldfish/
    make ARCH=x86 vbox_defconfig
    make -j10 ARCH=x86 bzImage


    Building a more recent kernel
    Unlike the emulator kernel, the VirtualBox kernel doesn't really rely on any special devices that are not already in the upstream. You can bulid a VirtualBox kernel based on the latest kernel.org kernel (say... the 3.4 kernel) with little difficulty. Just use the vbox_defconfig to configure your 3.4 kernel tree and it will very likely "just boot".

    First, pull the latest kernel.org kernel:

    cd $KERN_SOURCE
    git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git 3.4
    cd 3.4


    You can now configure and build your kernel:

    cd $KERN_SOURCE/3.4

    # Copy in the defconfig file for vbox
    cp $KERN_SOURCE/goldfish/arch/x86/configs/vbox_defconfig arch/x86/configs


    # Build...
    make ARCH=x86 vbox_defconfig
    make -j10 ARCH=x86 bzImage


    Now, copy your kernel to your AOSP tree

    # If working with the master branch...
    cp vmlinux $AOSP_SOURCE/prebuilts/qemu-kernel/x86/vmlinux-vbox
    cp arch/x86/boot/bzImage $AOSP_SOURCE/prebuilts/qemu-kernel/x86/kernel-vbox


    # If working with the ics tag...
    cp vmlinux $AOSP_SOURCE/prebuilt/android-x86/kernel/vmlinux-vbox
    cp arch/x86/boot/bzImage $AOSP_SOURCE/prebuilt/android-x86/kernel/kernel-vbox


    My next posting will be on how to re-build the vbox image and to boot it.
    Pour de plus amples informations sur les optimisations de compilation, consultez notre Avertissement concernant les optimisations.