initramfs.cpio.gz

 (1)linux核心要求:

開啟配置選單,修改兩個配置項,分別是:

a):General setup-->選擇 Initial RAM filesystem and RAM disk...... 項

b):Device Drivers-->Block devices-->選擇 RAM block device support 項

 

[root@centos7 images]# file initramfs.cpio.gz 
initramfs.cpio.gz: gzip compressed data, from Unix, max compression
[root@centos7 images]# 
qemu-system-riscv64 -nographic -machine virt  -bios fw_jump.elf -m 1024    
     -kernel ~/riscv_debug/tiny/linux-5.14/arch/riscv/boot/Image  -append "root=/dev/vda ro console=ttyS0,115200n8  earlycon" 
     -initrd $PWD/initramfs.cpio.gz

 启动成功

 


initramfs.cpio.gz
[root@centos7 rams]# gzip -d initramfs.cpio.gz
[root@centos7 rams]# ls
initramfs.cpio
[root@centos7 rams]# cpio -idm < initramfs.cpio
22012 blocks
[root@centos7 rams]# ls
bin etc initramfs.cpio lib64 media opt root sbin tmp var
dev init lib linuxrc mnt proc run sys usr
[root@centos7 rams]#




[root@centos7 rams]# ls
bin  dev  etc  init  lib  lib64  linuxrc  media  mnt  opt  proc  root  run  sbin  sys  tmp  usr  var
[root@centos7 rams]# find . | cpio -o | gzip -9  > ../initrd.gzip
21919 blocks
[root@centos7 rams]# 
[root@centos7 rams]# file ../initrd.gzip 
../initrd.gzip: gzip compressed data, from Unix, last modified: Wed Oct 27 08:00:25 2021, max compression
[root@centos7 rams]#
find . | cpio --quiet --dereference -o -H newc | lzma -7 > ../cusotm.initrd.lz

To keep the same folder hierarchy three steps are needed:

  1. Make CPIO file system archive with simple -o option without newc format in created before eg. base folder:

    find . | cpio -o | gzip -9 > ../base/file_system.gz

  2. Make proper archive with newc format containing kernel/x86/microcode/GenuineIntel.bin:

    find kernel/ | cpio -o -H newc > new_initrd.img

  3. Add gzipped filesystem archive to the proper new_initrd.img:

    find base/ | cpio -o >> new_initrd.img

busybox

tar -jxvf busybox-1.32.1.tar.bz2

编译

[root@centos7 images]# cd busybox-1.32.1
[root@centos7 busybox-1.32.1]# make ARCH=riscv CROSS_COMPILE=riscv64-unknown-linux-gnu- menuconfig

include/libbb.h:587:6: note: in a call to function 'xpipe'
  587 | void xpipe(int filedes[2]) FAST_FUNC;
      |      ^~~~~
  AR      shell/lib.a
  LINK    busybox_unstripped
Static linking against glibc, can't use --gc-sections
Trying libraries: crypt m resolv rt
 Library crypt is not needed, excluding it
 Library m is needed, can't exclude it (yet)
 Library resolv is needed, can't exclude it (yet)
 Library rt is not needed, excluding it
 Library m is needed, can't exclude it (yet)
 Library resolv is needed, can't exclude it (yet)
Final link with: m resolv

 编译并安装

make -j $((`nproc`-1))
sudo make install

准备根文件系统

cd  busybox-1.32.1/_install
sudo mkdir dev
sudo mknod dev/console c 5 1
sudo mknod dev/ram b 1 0
sudo touch init
[root@centos7 rams]# ls dev/
console  null
[root@centos7 rams]# mknod dev/ram b 1 0
[root@centos7 rams]# ls dev/
console  null  ram
[root@centos7 rams]# 

在init中写入以下内容,你可以使用vim或gedit编辑器写入(vim tutorial),执行sudo vim init 或 sudo gedit init.

#!/bin/sh
echo "INIT SCRIPT"
mkdir /proc
mkdir /sys
mount -t proc none /proc
mount -t sysfs none /sys
mkdir /tmp
mount -t tmpfs none /tmp
echo -e "
This boot took $(cut -d' ' -f1 /proc/uptime) seconds
"
exec /bin/sh

赋予init脚本执行,制作initramfs文件,将x86-busybox下面的内容打包归档成cpio文件,以供 Linux内核做initramfs启动执行

sudo chmod +x init
cd  busybox-1.32.1/_install
find . -print0 | cpio --null -ov --format=newc | gzip -9 >
~/oslab/initramfs-busybox-x64.cpio.gz # 注意:该命令一定要在busybox的 _install
目录下执行
# 注意:每次修改_install,都要重新执行该命令

bootlin

[root@centos7 riscv_debug]# mkdir -p /opt/bootlin/
[root@centos7 riscv_debug]# tar jxf  riscv64--glibc--bleeding-edge-2020.08-1.tar.bz2  -C /opt/bootlin/
[root@centos7 riscv_debug]# ls /opt/bootlin/
riscv64--glibc--bleeding-edge-2020.08-1
[root@centos7 riscv_debug]# export TOOLPATH=/opt/bootlin/riscv64--glibc--bleeding-edge-2020.08-1
[root@centos7 riscv_debug]# ls /opt/bootlin/riscv64--glibc--bleeding-edge-2020.08-1
bin          buildroot.config  include  lib64    README.txt       riscv64-buildroot-linux-gnu  summary.csv
br_fragment  etc               lib      libexec  relocate-sdk.sh  share
[root@centos7 riscv_debug]# export PATH=$PATH:$TOOLPATH/bin
[root@centos7 riscv_debug]# 
scripts/gcc-version.sh: line 11: /opt/bootlin/riscv64--glibc--bleeding-edge-2020.08-1/bin/riscv64-buildroot-linux-gnu-gcc: cannot execute binary file
原来是x86
[root@centos7 busybox-1.32.1]# ls /opt/bootlin/riscv64--glibc--bleeding-edge-2020.08-1/bin/riscv64-buildroot-linux-gnu-gcc
/opt/bootlin/riscv64--glibc--bleeding-edge-2020.08-1/bin/riscv64-buildroot-linux-gnu-gcc
[root@centos7 busybox-1.32.1]# file /opt/bootlin/riscv64--glibc--bleeding-edge-2020.08-1/bin/riscv64-buildroot-linux-gnu-gcc
/opt/bootlin/riscv64--glibc--bleeding-edge-2020.08-1/bin/riscv64-buildroot-linux-gnu-gcc: symbolic link to `toolchain-wrapper'
[root@centos7 busybox-1.32.1]# ls /opt/bootlin/riscv64--glibc--bleeding-edge-2020.08-1/bin/toolchain-wrapper 
/opt/bootlin/riscv64--glibc--bleeding-edge-2020.08-1/bin/toolchain-wrapper
[root@centos7 busybox-1.32.1]# file  /opt/bootlin/riscv64--glibc--bleeding-edge-2020.08-1/bin/toolchain-wrapper 
/opt/bootlin/riscv64--glibc--bleeding-edge-2020.08-1/bin/toolchain-wrapper: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=a345d5c62a1dd5fd9ab28d6a86eaf8d32dbc675b, stripped
make ARCH=riscv CROSS_COMPILE=riscv64-buildroot-linux-gnu- menuconfig
Static linking against glibc, can't use --gc-sections
Trying libraries: crypt m resolv rt
 Library crypt is not needed, excluding it
 Library m is needed, can't exclude it (yet)
 Library resolv is needed, can't exclude it (yet)
 Library rt is not needed, excluding it
 Library m is needed, can't exclude it (yet)
 Library resolv is needed, can't exclude it (yet)
Final link with: m resolv

[Note] System unauthorized, Please contact the system supplier.

[Unauthorized System] root@internet-x86: busybox-1.32.1# 
make ARCH=riscv CROSS_COMPILE=riscv64-buildroot-linux-gnu- -j8 install

busybox-1.32.1# ls _install/
bin linuxrc sbin usr

 busybox-1.32.1/_install# ls
bin  linuxrc  sbin  usr

 

 /busybox-1.32.1/_install# mkdir dev

 

 busybox-1.32.1/_install# mknod dev/console c 5 1

 

 busybox-1.32.1/_install# mknod dev/ram b 1 0

 

 busybox-1.32.1/_install# touch init

 
 /busybox-1.32.1/_install# vi init 
#!/bin/sh
echo "INIT SCRIPT"
mkdir /proc
mkdir /sys
mount -t proc none /proc
mount -t sysfs none /sys
mkdir /tmp
mount -t tmpfs none /tmp
echo -e "
This boot took $(cut -d' ' -f1 /proc/uptime) seconds
"
exec /bin/sh
sudo chmod +x init
cd  busybox-1.32.1/_install
find . -print0 | cpio --null -ov --format=newc | gzip -9 >
 ../initramfs-busybox-x64.cpio.gz 
启动不了
[    5.126436] Kernel panic - not syncing: Attempted to kill init! exitcode=0x00000000
[    5.134646] CPU: 0 PID: 1 Comm: sh Not tainted 5.14.0 #18
[    5.140394] Hardware name: SiFive,FU500 (DT)
[    5.144499] Call Trace:
[    5.146962] [<ffffffff800047b8>] dump_backtrace+0x1c/0x24
[    5.152709] [<ffffffff806fed98>] dump_stack_lvl+0x40/0x58
[    5.158456] [<ffffffff806fedc4>] dump_stack+0x14/0x1c
[    5.163382] [<ffffffff806fc456>] panic+0xfa/0x2b4
[    5.168308] [<ffffffff8000ef88>] do_exit+0x7e0/0x7e6
[    5.173234] [<ffffffff8000fc62>] do_group_exit+0x2a/0x7e
[    5.178981] [<ffffffff8000fcce>] __wake_up_parent+0x0/0x20
[    5.184729] [<ffffffff80003002>] ret_from_syscall+0x0/0x2
[    5.190476] ---[ end Kernel panic - not syncing: Attempted to kill init! exitcode=0x00000000 ]---

加了cmmd也没用

init进程

        if (!try_to_run_init_process("/sbin/init") ||
            !try_to_run_init_process("/etc/init") ||
            !try_to_run_init_process("/bin/init") ||
            !try_to_run_init_process("/bin/sh"))
                return 0;


[    5.717569] Call Trace:
[    5.720032] [<ffffffff800047b8>] dump_backtrace+0x1c/0x24
[    5.725779] [<ffffffff806fed98>] dump_stack_lvl+0x40/0x58
[    5.731527] [<ffffffff806fedc4>] dump_stack+0x14/0x1c
[    5.736453] [<ffffffff806fc456>] panic+0xfa/0x2b4
[    5.741379] [<ffffffff80801456>] mount_block_root+0x1c0/0x25a
[    5.747126] [<ffffffff808015fc>] mount_root+0x10c/0x136
[    5.752873] [<ffffffff80801768>] prepare_namespace+0x142/0x184
[    5.758620] [<ffffffff80801032>] kernel_init_freeable+0x1f8/0x214
[    5.765188] [<ffffffff807063c6>] kernel_init+0x1e/0x104
[    5.770935] [<ffffffff80003010>] ret_from_exception+0x0/0xc
[    5.776683] ---[ end Kernel panic - not syncing: VFS: Unable to mount root fs on unknown-block(0,0) ]---
 

Warning: unable to open an initial console

[    9.145320] Warning: unable to open an initial console.
[    9.519704] Freeing unused kernel image (initmem) memory: 6612K
[    9.550903] Run /init as init process
[   28.363711] random: dd: uninitialized urandom read (512 bytes read)
[   46.215106] random: dropbear: uninitialized urandom read (32 bytes read)
[root@centos7 rams]# rm dev/console
rm: remove character special file ‘dev/console’? y
[root@centos7 rams]# rm dev/null 
rm: remove character special file ‘dev/null’? y
[root@centos7 rams]# mknod dev/console c 5 1 –m 660 
mknod: extra operand ‘–m’
Try 'mknod --help' for more information.
[root@centos7 rams]# mknod –m 660   dev/console c 5 1 
mknod: extra operand ‘5’
Try 'mknod --help' for more information.
[root@centos7 rams]# cd dev/
[root@centos7 dev]# ls
ram
[root@centos7 dev]# mknod -m 660 console c 5 1
[root@centos7 dev]# mknod -m 660 null c 1 3
[root@centos7 dev]# ls
console  null  ram
[root@centos7 dev]# 
[root@centos7 rams]# find . -print0 | cpio --null -ov --format=newc | gzip -9 > ../initrd.rv.cpio.gz

buildroot

[root@centos7 buildroot]# ls output/images/
fw_dynamic.bin  fw_dynamic.elf  fw_jump.bin  fw_jump.elf  rootfs.bak  rootfs.ext2  rootfs.tar  start-qemu.sh
[root@centos7 buildroot]# du -sh  output/images/rootfs.tar 
2.2M    output/images/rootfs.tar
[root@centos7 buildroot]# 
 tar -xvf rootfs.tar 

启动文件系统时出现错误:Warning: unable to open an initial console.

”snps,dw-apb-uart" 驱动没有打开
static const struct of_device_id dw8250_of_match[] = {
        { .compatible = "snps,dw-apb-uart" },
        { .compatible = "cavium,octeon-3860-uart" },
        { .compatible = "marvell,armada-38x-uart" },
        { .compatible = "renesas,rzn1-uart" },
        { /* Sentinel */ }
};
”snps,dw-apb-uart" 驱动加入后启动成功
[    4.283251] NET: Registered PF_INET6 protocol family
[    4.391625] Segment Routing with IPv6
[    4.397372] sit: IPv6, IPv4 and MPLS over IPv4 tunneling driver
[    4.414614] NET: Registered PF_PACKET protocol family
[    4.423645] 9pnet: Installing 9P2000 support
[    4.429392] Key type dns_resolver registered
[    4.439244] debug_vm_pgtable: [debug_vm_pgtable         ]: Validating architecture page table helpers
[    9.174876] dw-apb-uart 20000000.serial: forbid DMA for kernel console
[    9.551724] Freeing unused kernel image (initmem) memory: 6616K
[    9.633004] Run /init as init process
# 

WARNING: Unable to open an initial console

这个主要是没有建立控制台,解决方式:

内核选择如下选项:

Device Drivers  --->
Character devices  --->
    [*] Virtual terminal
 Serial drivers --->
        <*> 8250/16550 and compatible serial support 
  [*]   Console on 8250/16550 and compatible serial port
 (2) Maximum number of 8250/16550 serial ports 
 (2) Number of 8250/16550 serial ports to register at runtime  
    [*] Unix98 PTY support


缺少 /dev/console 和 /dev/null
在建立他们之前UDEV就先使用到他们了

文件系统中建立如下设备文件:
cd /dev
mknod -m 660 console c 5 1
mknod -m 660 null c 1 3



buildroot


原文地址:https://www.cnblogs.com/dream397/p/15472744.html