Hacking and Rebuilding the Ubuntu Nexus7 Image

WARNING: This process has changed since the switch to Raring. I don’t yet have a new update for the process.

EDIT: I just added some notes about extracting files from the boot.img in the post.

Several people asked me some questions last week abut how the Nexus7 image is built and how they can hack it. Hopefully this post will help to answer some of those questions. Note that nothing described here is supported, it is just presented here to enable people interested in hacking the image to get going. This process requires the tools simg2img and make_ext4fs which you can download pre-compiled binaries for from here.

Hacking a pre-built image rootfs.img

    1. Take the rootfs.img file as input and use the tool simg2img to unpack it. It will be a large file when unpacked, 28G for the 32GB tablet, 13G for the 16GB tablet, and 6G for the 8GB tablet.

mfisch@caprica:~/build$ ./simg2img rootfs.img rootfs.ext4

    1. Mount the rootfs.ext4 file

mfisch@caprica:~/build$ sudo mount -o loop rootfs.ext4 tmpmnt/

    1. Inside tmpmnt, you’ll find the original rootfs.tar.gz. Copy this file out and unmount the directory. You can also remove the rootfs.ext4 file.

mfisch@caprica:~/build/tmpmnt$ cp rootfs.tar.gz ..
mfisch@caprica:~/build/tmpmnt$ cd ..
mfisch@caprica:~/build$ sudo umount tmpmnt/
mfisch@caprica:~/build$ rm rootfs.ext4

    1. Extract the rootfs.tar.gz

mfisch@caprica:~/build$ tar -xvzf rootfs.tar.gz

    1. The extracted filesystem is in ./binary/casper/filesystem.dir. You can copy files into and out of here or modify files.

Once you’re done with the changes, you need to rebuild the rootfs.img file. The first step is to re-tar and recompress the unpacked files.

mfisch@caprica:~/build$ tar -cvzf rebuilt.tar.gz binary/

From this point you can use the same process we use to build images and then flash them, following the process below.

Building or rebuilding a rootfs.img file

Given a tarball image, our image building script basically does a couple things:

  1. Extract the kernel and initrd from the rootfs.tar.gz
  2. Write a bootimg.cfg file out using the right values for the Nexus 7.
  3. Create a boot.img file using abootimg using the inputs of the kernel, the initrd, the bootimg.cfg. Note: We had to do some work here to make sure that the initrd was small. I think the limit was 2MB.
  4. Take the rootfs.tar.gz, and using the tool make_ext4fs, create a sparse ext4 filesystem and call the output rootfs.img.

We wrote a script to do all this which makes life easier. This process may change as we implement these image builds on cdimage.ubuntu.com and this script may not be updated, but it should be enough to get people hacking. If you do anything cool with this or have fixes for Ubuntu, please let me know or send a patch to one of our bugs.

Hacking/Rebuilding boot.img

After a few questions I decided to add a brief note about how to hack and rebuild the boot.img. It’s pretty simple and uses the abootimg tool which is in universe for quantal.

To extract the files, use abootimg -x

mfisch@caprica:~/upload-scripts$ abootimg -x boot.img
writing boot image config in bootimg.cfg
extracting kernel in zImage
extracting ramdisk in initrd.img

To rebuild, you see see the script I referenced above, or just run abootimg –create:

mfisch@caprica:~/upload-scripts$ abootimg --create newboot.img -k zImage -f ./bootimg.cfg -r initrd.img
reading config file ./bootimg.cfg
reading kernel from zImage
reading ramdisk from initrd.img
Writing Boot Image newboot.img

Tagged , ,

11 thoughts on “Hacking and Rebuilding the Ubuntu Nexus7 Image

  1. simg2img does not seem to work for me



  2. Matt Fischer says:

    Can you run file on it?
    mfisch@caprica:~/build$ file simg2img
    simg2img: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.15, BuildID[sha1]=0x05e1191449975f69fecc05188bba644d4ce72231, not stripped

  3. mark says:

    Based on these instructions would there be an easy way to include the tegra 3 video gtstreamer so video playback would work? Thanks

  4. Matthew Anderson says:

    Is it possible to port Ubuntu to other Android tablets with the Tegra 3 Processor? How complicated would this project be?

  5. Has this process changed in the last month and a half? I grabbed the latest image and it mounts and extracts as you list here.

    However when I copy the rootfs.tar.gz out of the .ext4 image and try to point the build.sh at it by using:

    ./build.sh -t rootfs.tar.gz

    I get the following spat back at me:
    Extracting kernel
    basename: missing operand
    Try `basename –help’ for more information.

    Going to poke around with things some more to see if I can figure out what is going on – but figured it couldn’t hurt to ask here as well.

    • Matt Fischer says:

      Yes it has changed. I will update the blog header to warn people. We’re no longer hand-building Quantal images instead we have daily CD images. Unfortunately I don’t know how this affects the image build process as I’ve been working on a few other projects since that change was made. I’d suggest you ask in #ubuntu-arm on Freenode on Monday morning. If you get the answer I will update this post.

  6. Javier says:

    Thanks Matt. I needed to add a preseed file the rootfs image and this post was very useful.

Leave a Reply

Your email address will not be published. Required fields are marked *