[lfs-chat] Initramfs (part 1)

Peng HG craftyflicker at hotmail.com
Sun Sep 4 10:37:13 PDT 2016


Rob Landley published an article about initramfs (https://landley.net/writing/rootfs-howto.html) which I think is interesting not just on account of initramfs alone, but also because it answers a whole bunch of Linux FAQs like how small can you make Linux, how to make a LiveCD, can Linux exist apart from GNU tools, etc. Everyone should try the exercise in the article -- it's really short and simple; I took about 15 minutes to do it. Basically it goes like this (I'm using Lubuntu, you may adjust some details to fit your system):

Requirements: 64-bit system, UEFI non-secure-boot, a USB stick containing a FAT32 partition with esp and boot flags turned on.

First make sure your host system has the packages cpio, gcc, libncurses5-dev and make installed, then download any of the kernel balls from kernel.org. Create a work directory:

mkdir ~/test

Write a simple C program, source code:

#include <stdio.h>
#include <unistd.h>
int main(int argc, char *argv[]) {printf("Hello world\n"); sleep(300);}

and save it as ~/test/hello.c, then compile it:

cd ~/test
gcc -static hello.c -o init

Test run init to make sure the program works, then remove the source file (rm hello.c). Still inside the test directory, create a couple of device files:

mkdir dev
sudo mknod -m 622 dev/console c 5 1
sudo mknod -m 666 dev/tty0 c 5 0

Now you have the simplest file structure that you can boot into, containing an init program and two devices to bring up the console. Bundle them up (still in ~/test):

find . | cpio -o -H newc | gzip > ../test.cpio.gz

Now prepare to compile a minimal kernel:

cd ~/Downloads
tar xf linux-4.7.2.tar.xz
cd linux-4.7.2
make mrproper
make allnoconfig
mv ~/test.cpio.gz .
make menuconfig

Configure it thus:

64-bit kernel [*]
General setup → Initial RAM filesystem and RAM disk (initramfs/initrd) support [*]
General setup → Initramfs source file(s) [set to test.cpio.gz]
General setup → Support initial ramdisks compressed using bzip2/LZMA/XZ/LZO/LZ4 [all n, except gzip y]
Bus options (PCI etc.) → PCI support [*]
Processor type and features → EFI runtime service support [*]
Processor type and features → EFI stub support [*]
Executable file formats / Emulations → Kernel support for ELF binaries [*]
Device Drivers → Input device support → Generic input layer (needed for keyboard, mouse, ...) [*]
Device Drivers → Character devices → Enable TTY [*]
Device Drivers → Graphics support → Frame buffer Devices → Support for frame buffer devices [*]
Device Drivers → Graphics support → Frame buffer Devices → EFI-based Framebuffer support [*]
Device Drivers → Graphics support → Console display driver support → Framebuffer Console support [*]
Kernel hacking → Magic SysRq key [*]

and then compile it:

make

Now mount your USB stick, say on /mnt and create the required EFI directory:

sudo mkdir -p /mnt/efi/boot

Finally copy the compiled kernel into the stick:

sudo cp ~/Downloads/linux-4.7.2/arch/x86/boot/bzImage /mnt/efi/boot/bootx64.efi

Now shut down, boot into the stick, and see it print "Hello world". To shut down this minimal environment, hold down Shift + Alt + PrintScreen, and press one-by-one the keys r, e, i, s, u, o.


More information about the lfs-chat mailing list