initramfs - bunnyamin/bunnix GitHub Wiki

BusyBox

Configure

# Out of source code build:
make O="$COMPILE_DIR" defconfig

# If no `CONFIG_STATIC=y` then write it:
grep -q '^CONFIG_STATIC=y' "$COMPILE_DIR/.config" || \
  sed -i '/^# CONFIG_STATIC is not set/d' "$COMPILE_DIR/.config" && \
  echo 'CONFIG_STATIC=y' >> "$COMPILE_DIR/.config"

Compile and install

The installation creates the directory structure:

$INITRAMFS_DIR
+ bin
+ sbin
+ usr
  + bin
  + sbin
make \
  ARCH=$ARCH_LNX \
  CROSS_COMPILE="$CROSS_COMPILE" \
  CONFIG_PREFIX="$INITRAMFS_DIR" \
  busybox install

Confirm build:

file busybox

Expected output:

  • busybox: ELF 64-bit LSB executable, ARM aarch64, version 1 (GNU/Linux), statically linked, for GNU/Linux 5.10.235, stripped

Create initramfs

Write the init script:

cat > "$INITRAMFS_DIR/init" << 'EOF'
#!/bin/sh
mount -t proc none /proc
mount -t sysfs none /sys
mount -t devtmpfs none /dev

exec /bin/sh
EOF

chmod +x "$INITRAMFS_DIR/init"

Create directory and device nodes:

mkdir -p "$INITRAMFS_DIR"/{proc,sys,dev}

fakeroot sh -c "
  mknod -m 622 '$INITRAMFS_DIR/dev/console' c 5 1
  mknod -m 666 '$INITRAMFS_DIR/dev/null' c 1 3
"

Build the initramfs.img

find $INITRAMFS_DIR -print0 | cpio --null -ov --format=newc | gzip -9 > "$INITRAMFS_DIR/initramfs-linux.cpio.gz"

Debug

Examine Example
What is the content of initramfs-linux.img? analyze $ lsinitcpio -a /boot/initramfs-linux.cpio.gz, `gzip -dc initramfs.cpio.gz
What is the content of initramfs-linux.img? extract $ lsinitcpio -x /boot/initramfs-linux.cpio.gz