8

I'm building a custom Android kernel based on the Cyanogenmod ROM's kernel source code. I'd like to add folders and files into the root folder of the OS (/). For instance, after having compiled my kernel, I'd like for an extra folder named toto (absolute path = /toto) to be created.

I really have no idea which files have to be edited and how to do the work.


Note: If you're an Android user (not a ROM developer) who wants to add files to your rootfs, please see the relevant Android.SE question instead.

  • 3
    Android is a Linux system, but as the question is specific to Android, not to all Unixes. Better place for it is on android.stackexchange.com – enedil Apr 2 '14 at 16:00
  • @enedil Generally speaking, Android questions are off-topic here, since Android is not Linux in the common sense of the term (it merely uses a Linux kernel). However, the same question would apply to other embedded Linux systems, so I think it's ok here. – Gilles Apr 2 '14 at 23:43
  • @Graeme Actually, the root filesystem is compiled into every kernel. Usually it's empty and we unpack a cpio archive into it - our initramfs image. You can put whatever you want in it though at compile time. – mikeserv Apr 2 '14 at 23:44
  • @enedil In this case, I believe this question is entirely on-topic. Android differs most from other unixes in userspace, but from other Linuxes, the in-kernel difference amounts to only a handful of patches. In fact, Android's popularity is a major driving force behind kernel development, and has been for a couple of years. Have a look at the kernel.org changelogs and decide for yourself how relevant many are to mobile systems - Android in particular. – mikeserv Apr 3 '14 at 0:00
  • A similar question on Android.SE: How to unpack and edit boot.img for ROM porting?: answers there explain how to fetch and edit the boot.img file, allowing to persistently change device's root directory content. – WhiteWinterWolf Aug 11 '16 at 15:57
7

On Android, like on many Linux-based systems, the kernel first mounts an initramfs on /. The initramfs is stored in RAM; it is loaded from a CPIO archive which is stored together with the kernel itself (or in some other place where the bootloader can find it).

Most desktop Linux systems have a small initramfs which contains just enough programs and configuration files to mount the real root filesystem, which is then mounted on /, replacing the initramfs. Android, like some embedded Linux systems, keeps the initramfs mounted forever. Android's initramfs contains only /init, adbd and a few configuration files.

For Cyanogenmod, you can find build instructions in the porting guide. You want to copy more files to the ramdisk (the initramfs image, in Android terminology), so you need to add them to the PRODUCT_COPY_FILES list in the device_*.mk makefile for your device.

  • Actually our initramfs image file is what contains those config files, the initramfs filesystem is compiled into every kernel. – mikeserv Apr 2 '14 at 23:46
  • 1
    @mikeserv I invite you to familiarize yourself with the concept of metonymy. Technical writing uses it less than ordinary speech, but it does get the occasional use. – Gilles Apr 2 '14 at 23:57
  • I will do so, but first I have to check it in the dictionary... – mikeserv Apr 3 '14 at 0:01
  • You make a very good point, and like I said before, the only reason I'm adamant about this is it seems it's so little understood but it's really very straight-forward, so I tend to nitpick on this topic - for which I apologize. I just think it would be easier to show others how simple it can be to engineer your own system from the kernel up if the above detail was made clear. Again, I'm sorry, Gilles, I mean no insult whatsoever. – mikeserv Apr 3 '14 at 0:04
  • @mikeserv Thanks you for your advises. I located the file used to copy blobs(blbs.mk). I still do not understand which file(s) has to be edited to add a folder folder at rootfs (/). I can access to the init*.rc files of the rom, but I don't now if editing (by adding a mkdir /titi for instance) these files will allow me to add permanently my folders (/titi). Afterwhat I will add in the file PRODUCT_COPY_FILES+= /titi/myfile:<localpath>/myfiles. Any clue ? Thanks again – deadeert Apr 7 '14 at 15:43
1

The kernel docs explain how to pack an image into the kernel itself. From kernel.org:

What is rootfs?

Rootfs is a special instance of ramfs (or tmpfs, if that's enabled), which is always present in 2.6 systems. You can't unmount rootfs for approximately the same reason you can't kill the init process; rather than having special code to check for and handle an empty list, it's smaller and simpler for the kernel to just make sure certain lists can't become empty.

Most systems just mount another filesystem over rootfs and ignore it. The amount of space an empty instance of ramfs takes up is tiny.

If CONFIG_TMPFS is enabled, rootfs will use tmpfs instead of ramfs by default. To force ramfs, add "rootfstype=ramfs" to the kernel command line.

What is initramfs?

All 2.6 Linux kernels contain a gzipped "cpio" format archive, which is extracted into rootfs when the kernel boots up. After extracting, the kernel checks to see if rootfs contains a file "init", and if so it executes it as PID 1. If found, this init process is responsible for bringing the system the rest of the way up, including locating and mounting the real root device (if any). If rootfs does not contain an init program after the embedded cpio archive is extracted into it, the kernel will fall through to the older code to locate and mount a root partition, then exec some variant of /sbin/init out of that.

All this differs from the old initrd in several ways:

  • The old initrd was always a separate file, while the initramfs archive is linked into the linux kernel image. (The directory linux-*/usr is devoted to generating this archive during the build.)

  • The old initrd file was a gzipped filesystem image (in some file format, such as ext2, that needed a driver built into the kernel), while the new initramfs archive is a gzipped cpio archive (like tar only simpler, see cpio(1) and Documentation/early-userspace/buffer-format.txt). The kernel's cpio extraction code is not only extremely small, it's also __init text and data that can be discarded during the boot process.

  • The program run by the old initrd (which was called /initrd, not /init) did some setup and then returned to the kernel, while the init program from initramfs is not expected to return to the kernel. (If /init needs to hand off control it can overmount / with a new root device and exec another init program. See the switch_root utility, below.)

  • When switching another root device, initrd would pivot_root and then umount the ramdisk. But initramfs is rootfs: you can neither pivot_root rootfs, nor unmount it. Instead delete everything out of rootfs to free up the space (find -xdev / -exec rm '{}' ';'), overmount rootfs with the new root (cd /newmount; mount --move . /; chroot .), attach stdin/stdout/stderr to the new /dev/console, and exec the new init.

Since this is a remarkably persnickety process (and involves deleting commands before you can run them), the klibc package introduced a helper program (utils/run_init.c) to do all this for you. Most other packages (such as busybox) have named this command "switch_root".

Populating initramfs:

The 2.6 kernel build process always creates a gzipped cpio format initramfs archive and links it into the resulting kernel binary. By default, this archive is empty (consuming 134 bytes on x86).

The config option CONFIG_INITRAMFS_SOURCE (in General Setup in menuconfig, and living in usr/Kconfig) can be used to specify a source for the initramfs archive, which will automatically be incorporated into the resulting binary. This option can point to an *existing gzipped cpio* archive, a directory containing files to be archived, or a text file specification such as the following example:

 dir /dev 755 0 0
 nod /dev/console 644 0 0 c 5 1
 nod /dev/loop0 644 0 0 b 7 0
 dir /bin 755 1000 1000
 slink /bin/sh busybox 777 0 0
 file /bin/busybox initramfs/busybox 755 0 0
 dir /proc 755 0 0
 dir /sys 755 0 0
 dir /mnt 755 0 0
 file /init initramfs/init.sh 755 0 0

Run "usr/gen_init_cpio" (after the kernel build) to get a usage message documenting the above file format.

One advantage of the configuration file is that root access is not required to set permissions or create device nodes in the new archive.

(Note that those two example "file" entries expect to find files named "init.sh" and "busybox" in a directory called "initramfs", under the linux-2.6.* directory. See Documentation/early-userspace/README for more details.)

The kernel does not depend on external cpio tools. If you specify a directory instead of a configuration file, the kernel's build infrastructure creates a configuration file from that directory (usr/Makefile calls scripts/gen_initramfs_list.sh), and proceeds to package up that directory using the config file (by feeding it to usr/gen_init_cpio, which is created from usr/gen_init_cpio.c). The kernel's build-time cpio creation code is entirely self-contained, and the kernel's boot-time extractor is also (obviously) self-contained.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.