Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initramfs built into custom Linux kernel is not running

Tags:

I am building a custom initramfs image that I am building as a CPIO archive into the Linux kernel (3.2).

The issue I am having is that no matter what I try, the kernel does not appear to even attempt to run from the initramfs.

The files I have in my CPIO archive:

cpio -it < initramfs.cpio . init usr usr/sbin lib lib/libcrypt.so.1 lib/libm.so lib/libc.so.6 lib/libgcc_s.so lib/libcrypt-2.12.2.so lib/libgcc_s.so.1 lib/libm-2.12.2.so lib/libc.so lib/libc-2.12.2.so lib/ld-linux.so.3 lib/ld-2.12.2.so lib/libm.so.6 proc sbin mnt mnt/root root etc bin bin/sh bin/mknod bin/mount bin/busybox sys dev 4468 blocks 

Init is very simple, and should just init devices and spawn a shell (for now):

#!/bin/sh  mount -t devtmpfs none /dev mount -t proc none /proc mount -t sysfs none /sys /bin/busybox --install -s exec /bin/sh 

In the kernel .config I have:

CONFIG_INITRAMFS_SOURCE="../initramfs.cpio" CONFIG_INITRAMFS_ROOT_UID=0 CONFIG_INITRAMFS_ROOT_GID=0 CONFIG_BLK_DEV_INITRD=y CONFIG_BLK_DEV_RAM=y CONFIG_BLK_DEV_RAM_COUNT=1 CONFIG_BLK_DEV_RAM_SIZE=32768 

Kernel builds and the uImage size is larger depending on the initramfs size, so I know the image is being packed. However I get this output when I boot:

console [netcon0] enabled netconsole: network logging started omap_rtc omap_rtc: setting system clock to 2000-01-02 00:48:38 UTC (946774118) Warning: unable to open an initial console. Freeing init memory: 1252K mmc0: host does not support reading read-only switch. assuming write-enable. mmc0: new high speed SDHC card at address e624 mmcblk0: mmc0:e624 SU08G 7.40 GiB  mmcblk0: p1 Kernel panic - not syncing: Attempted to kill init! [<c000d518>] (unwind_backtrace+0x0/0xe0) from [<c0315cf8>] (panic+0x58/0x188) [<c0315cf8>] (panic+0x58/0x188) from [<c0021520>] (do_exit+0x98/0x6c0) [<c0021520>] (do_exit+0x98/0x6c0) from [<c0021e88>] (do_group_exit+0xb0/0xdc) [<c0021e88>] (do_group_exit+0xb0/0xdc) from [<c0021ec4>] (sys_exit_group+0x10/0x18) [<c0021ec4>] (sys_exit_group+0x10/0x18) from [<c00093a0>] (ret_fast_syscall+0x0/0x2c) 

From that output, it does not look like it is even trying to extract the CPIO archive as initramfs. I expect to see this printk output, which is present in linux code init/initramfs.c:

printk(KERN_INFO "Trying to unpack rootfs image as initramfs...\n"); 

I tried the filesystem once booting is complete (using chroot) and it works fine... so I believe the filesystem/libraries are sane.

Could anyone give me some pointers as to what I may have incorrect? Thanks in advance for any assistance!

like image 655
dag Avatar asked May 03 '12 19:05

dag


People also ask

Can Linux boot without initramfs?

Yes, you can boot a system without an initrd image.

Is initramfs part of kernel?

initramfs is a root filesystem that is embedded into the kernel and loaded at an early stage of the boot process. It is the successor of initrd. It provides early userspace which can do things the kernel can't easily do by itself during the boot process. Using initramfs is optional.

How do I run initramfs?

moves /proc from initramfs to /initramfs/root/proc. calls run-init to run the real init in your real rootfs kept in /root. run-init does something like chroot to the real rootfs and then executes the init kept in /sbin/ or /bin or whatever user requested as a boot parameter.

What is the cause of initramfs?

There are only four primary reasons to have an initramfs in the LFS environment: loading the rootfs from a network, loading it from an LVM logical volume, having an encrypted rootfs where a password is required, or for the convenience of specifying the rootfs as a LABEL or UUID.


1 Answers

I figured it out. I will post the answer in case anyone else has this issue.

I was missing a console device, this line was the clue:

Warning: unable to open an initial console. 

After adding printk's so that I better understood the startup sequence, I realized that console device is opened prior to running the init script. Therefore, the console device must be in the initramfs filesystem directly, and we cannot rely on the devtmpfs mount to create that.

I think when the init script ran the shell was trying to open the console and failed, that's why the kernel was outputting:

Kernel panic - not syncing: Attempted to kill init! 

Executing the folowing commands from within the /dev directory of initramfs on the kernel build machine will generate the required device nodes:

mknod -m 622 console c 5 1 mknod -m 622 tty0 c 4 0 

After re-CPIO archiving the filesystem and rebuilding the kernel, I finally have a working filesystem in initramfs that the kernel will boot.

like image 160
dag Avatar answered Nov 27 '22 20:11

dag