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
- 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
- Mount the rootfs.ext4 file
mfisch@caprica:~/build$ sudo mount -o loop rootfs.ext4 tmpmnt/
- 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
- Extract the rootfs.tar.gz
mfisch@caprica:~/build$ tar -xvzf rootfs.tar.gz
- 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:
- Extract the kernel and initrd from the rootfs.tar.gz
- Write a bootimg.cfg file out using the right values for the Nexus 7.
- 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.
- 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.
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