Como compilar arquivos extras no diretório raiz de uma ROM Android

7

Estou construindo um kernel Android personalizado com base no código-fonte do kernel da ROM CyanogenMod. Gostaria de adicionar pastas e arquivos na pasta raiz do sistema operacional ( / ). Por exemplo, depois de ter compilado meu kernel, eu gostaria que uma pasta extra chamada toto (caminho absoluto = /toto ) fosse criada.

Eu realmente não tenho ideia de quais arquivos precisam ser editados e como fazer o trabalho.

Observação: se você for um usuário Android (não desenvolvedor de ROM) e quiser adicionar arquivos ao seu rootfs , consulte a questão relevante do Android.SE em seu lugar.

    
por deadeert 02.04.2014 / 17:55

2 respostas

7

No Android, como em muitos sistemas baseados em Linux, o kernel monta primeiro um initramfs em / . O initramfs é armazenado na RAM; Ele é carregado de um arquivo CPIO que é armazenado junto com o próprio kernel (ou em algum outro lugar onde o bootloader possa encontrá-lo).

A maioria dos sistemas Linux de desktop tem um pequeno initramfs que contém apenas programas e arquivos de configuração suficientes para montar o sistema de arquivos raiz real, que é montado em / , substituindo o initramfs. O Android, como alguns sistemas Linux embarcados, mantém o initramfs montado para sempre. O initramfs do Android contém apenas /init , adbd e alguns arquivos de configuração.

Para o Cyanogenmod, você pode encontrar instruções de criação no guia de portabilidade . Você quer copiar mais arquivos para o disco virtual (a imagem initramfs, na terminologia do Android), então você precisa adicioná-los à lista PRODUCT_COPY_FILES no device_*.mk makefile para o seu dispositivo.

    
por 03.04.2014 / 01:42
1

Os documentos do kernel explicam como empacotar uma imagem no próprio kernel. De 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.

    
por 03.04.2014 / 01:48