Changes between Version 3 and Version 4 of linux/initramfs


Ignore:
Timestamp:
02/24/2024 12:14:58 AM (9 months ago)
Author:
Tim Harvey
Comment:

added example of modifying an existing initramfs

Legend:

Unmodified
Added
Removed
Modified
  • linux/initramfs

    v3 v4  
    77
    88Note that the Linux kernel runs {{{/init}}}, {{{/sbin/init}}}, or {{{/bin/sh}}} (searched in that order) as PID1 from the initramfs (if it exists). If you want to run a different executable as PID1 you can use the {{{init=}}} kernel parameter. The init application must be executable and statically linked or it will move on to the next item in the search path.
     9
     10The word 'initramfs' and 'initrd' can be used interchangeably here.
    911
    1012see also:
     
    170172ls -l output/images/rootfs.cpio.gz
    171173}}}
     174
     175
     176== rebuilding or modifying an initramfs
     177You can modify an external initramfs fairly easily with the {{{cpio}}} tool. You merely need to uncompress it (if compressed) and use the extract parameter. You will want to do this as root in order to preserve file ownership of root files.
     178
     179Example:
     180 1. extract files from an existing gzipped cpio:
     181{{{#!bash
     182# uncompress it (if compressed) and use {{{cpio -i}}} to extract it:
     183# -i extract
     184# -d create directories
     185# -m preserve mtime
     186# -v verbose
     187mkdir rootfs_mod
     188(cd rootfs_mod; gzip -cd ../cpio | sudo cpio -idmv)
     189}}}
     190 2. modify it; for example overwrite or create your own /init:
     191cat <<EOF >rootfs_mod/init
     192#!/bin/busybox sh
     193mount -t devtmpfs  devtmpfs  /dev
     194mount -t proc      proc      /proc
     195mount -t sysfs     sysfs     /sys
     196mount -t tmpfs     tmpfs     /tmp
     197
     198echo "Hello world!"
     199sh
     200EOF
     201chmod +x rootfs_mod/init
     202}}}
     203 1. re-create it
     204{{{#!bash
     205(cd rootfs_mod; find . | cpio -ov --format=newc) | gzip -9 > cpio
     206}}}