Skip to content

Instantly share code, notes, and snippets.

@Jamlee
Last active June 24, 2025 13:48
Show Gist options
  • Save Jamlee/641fe6534ec1ddab7412be46bd9ecc53 to your computer and use it in GitHub Desktop.
Save Jamlee/641fe6534ec1ddab7412be46bd9ecc53 to your computer and use it in GitHub Desktop.
使用 busybox 制作内存文件系统 initramfs
  1. 下载解压 busybox 并配置环境变量

    wget https://busybox.net/downloads/busybox-1.36.1.tar.bz2
    tar -xf busybox-1.36.1.tar.bz2
    cd busybox-1.36.1
    # 配置环境变量
    export ARCH=arm64
    export CROSS_COMPILE=aarch64-linux-gnu-
  2. 配置编译内核的参数

    # busybox-1.36.1目录下
    make menuconfig
    # 修改配置,选中如下项目,静态编译
    # Settings -> Build Options -> [*] Build static binary (no share libs)
  3. 编译

    make -j `nproc`
  4. 安装

    make install

​ 安装后目录下生成了_install 目录

  1. 在_install 目录下创建后续所需的文件和目录

    cd _install
    mkdir proc sys dev tmp
    mknod -m 600 dev/console c 5 1
    mknod -m 666 dev/null c 1 3
    mknod -m 666 dev/tty c 5 0
    mknod -m 666 dev/tty1 c 4 1
    mknod -m 666 dev/tty2 c 4 2
    mknod -m 666 dev/tty3 c 4 3
    mknod -m 666 dev/tty4 c 4 4
    
    touch init
    chmod +x init
  2. 用任意的文本编辑器编辑 init 文件内容如下

    #!/bin/sh
    
    # 挂载一些必要的文件系统
    mount -t proc none /proc
    mount -t sysfs none /sys
    mount -t tmpfs none /tmp
    mount -t devtmpfs none /dev
    
    # 停留在控制台
    exec /bin/sh
    
  3. 用 busybox 制作 initramfs 文件

    # _install目录
    find . -print0 | cpio --null -ov --format=newc | gzip -9 > ../initramfs.cpio.gz

    执行成功后可在 busybox-1.35.0 目录下找到 initramfs.cpio.gz 文件

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment