从零开始构建嵌入式 rootfs

根目录&&根文件系统

根目录:在 Linux 中,所有的目录都属于根目录

根文件系统:挂载在根目录下面的文件系统

什么是根文件系统

根文件系统是 Kernel 启动后第一个加载的文件系统

根文件系统的作用

Kernel 启动后,总得先挂载一个文件系统,这样其它文件系统才能被挂载和使用。

所以,根文件系统,为其它文件系统提供挂载点,是一个载体。挂载点就是一个个目录。

根文件系统包含哪些内容

  • 基本的目录结构(/bin、/dev、/etc、/lib、/proc、/usr、/var)
  • 启动程序:init=/linuxrc
  • 配置文件

为什么要这样设计

  • 扩展性好,VFS 设计,
  • Linux 一切皆文件,为了让 Linux 完整地启动,进入一个可以让用户操作的环境

文件系统需要那些东西

  1. bootloader 使用命令行参数,告诉内核,根文件系统在什么地方;什么样的格式
  2. 告诉 Kernel 第一个启动的应用程序
  3. 基本目录
  4. 配置文件

怎样构建文件系统

从零开始,在 PC 上构建一个可以被 ARM 板挂载的 NFS rootfs

一、创建基本目录启动程序 linuxrc

$ mkdir mini_rootfs
$ cd mini_rootfs
$ mkdir bin dev etc lib proc usr var
$ ls
bin  dev  etc  lib  proc  usr  var

交叉编译一个 ARM 板可用的 busybox,这里为了简单直接从已有的 ARM 板系统中拷了一个

$ ln -sf bin/busybox linuxrc

二、搭建 NFS 服务器

ubuntu 16.04 nfs服务的搭建

三、ARM 板设置内核启动参数

setenv bootargs 'root=/dev/nfs nfsroot=192.168.31.228:/home/liyongjun/tmp/mini_rootfs ip=192.168.31.6 init=/linuxrc console=ttySAC0,115200'
saveenv
reset

四、重启报错

/init: line 103: can't open /r/dev/console: no such file
Kernel panic - not syncing: Attempted to kill init!

解决

$ sudo mknod console c 5 1

五、重启报错

switch_root: bad init /linuxrc: No such file or directory
Kernel panic - not syncing: Attempted to kill init!

解决

$ scp root@192.168.31.5:/lib/libcrypt.so.1 ./lib
$ scp root@192.168.31.5:/lib/libc.so.6 ./lib
$ scp root@192.168.31.5:/lib/libm.so.6 ./lib
$ scp root@192.168.31.5:/lib/ld-linux.so.3 ./lib

六、重启可进入系统,但报错且没有sh

can't run '/etc/init.d/rcS': No such file or directory

can't open /dev/tty3: No such file or directory

Please press Enter to activate this console. 
can't open /dev/tty2: No such file or directory

can't open /dev/tty4: No such file or directory


can't open /dev/tty4: No such file or directory

can't open /dev/tty2: No such file or directory

can't open /dev/tty3: No such file or directory


can't open /dev/tty3: No such file or directory

can't open /dev/tty4: No such file or directory

can't open /dev/tty2: No such file or directory

解决

$ ln -sf busybox sh
$ ln -sf busybox ls
$ ln -sf busybox cat
$ touch etc/init.d/rcS
$ chmod +x rcS
$ cd ../dev/
$ touch tty2 tty3 tty4

七、重启进入系统,有 sh

成功
在这里插入图片描述

八、完善

$ ln -sf busybox rm
$ ln -sf busybox ifconfig
$ ln -sf busybox ping
$ ln -sf busybox route

总结

rootfs 原来可以这么简单,几个目录 + busybox + 几个库 + 几个设备文件,就构成了一个 rootfs。

参考

Linux内核及文件系统制作

ubuntu 16.04 nfs服务的搭建

玩转mini2440开发板之【使用nfs无法启动的问题(Virtualbox桥接网络)】

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Li-Yongjun

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值