构建Linux根文件系统

构建Linux根文件系统

1、基础知识

1.1、基本概念

In computing, a file system or filesystem controls how data is stored and retrieved. Without a file system, information placed in a storage medium would be one large body of data with no way to tell where one piece of information stops and the next begins. By separating the data into pieces and giving each piece a name, the information is easily isolated and identified. Taking its name from the way paper-based information systems are named, each group of data is called a “file”. The structure and logic rules used to manage the groups of information and their names is called a “file system”.摘自wiki

The root file system is the file system contained on the same disk partition on which the root directory is located; it is the filesystem on top of which all other file systems are mounted as the system boots up.摘自wiki

1.2、Linux根文件系统目录结构

FHS标准(Filesystem Hierarchy Standard,文件系统层次标准)定义了文件系统中目录、文件分类存放的原则;系统运行所需的最小文件、目录的集合;列举了不遵循这些原则的例外情况及原因。大多的Linux、UNIX发行版本都遵循FHS。在构建文件系统时,建议遵循FHS标准。

/
├── bin					//该目录下存放所有用户都可以使用的基本命令
├── dev					//该目录存放的设备文件
├── etc					//该目录存放各种配置文件
│   ├── export			//用于配置NFS文件系统(可选文件)
│   ├── fstab			//用来指明当执行`mount -a`时,需要挂接的文件系统(可选文件)
│   ├── ftpusers			//启动FTP服务时,用来配置用户的访问权限(可选文件)
│   ├── group			//用户的组文件(可选文件)
│   ├── inittab			//init进程的配置文件(可选文件)
│   ├── ld.so.conf		//其他共享库的路径(可选文件)
│   ├── mtab				//用来显示已经加载的文件系统,通常是/proc/mounts的链接文件(可选文件)
│   ├── opt				//用来配置/opt下的程序(可选目录)
│   ├── passwd			//密码文件(可选文件)
│   ├── sgml				//用来配置SGML(可选目录)
│   ├── X11				//用来配置X Windows(可选目录)
│   └── xml				//用来配置XML(可选目录)
├── home				//用户目录(可选)
├── lib					//该目录下存放共享库和可加载模块
│   ├── ld*				//连接器、加载器
│   ├── libc.so.*		//动态库
│   └── modules			//存放内核可加载模块
├── mnt					//临时挂接某个文件系统的挂接点
├── proc				//proc文件系统的挂接点
├── root				//root用户目录
├── sbin				//存放只有管理员可使用的系统命令
├── tmp					//存放临时文件
├── usr					//存放共享、只读的程序和数据
│   ├── bin
│   ├── games			//游戏
│   ├── include			//头文件
│   ├── lib				//库文件
│   ├── local			//本地目录
│   ├── sbin				//非必需的系统命令
│   ├── share			//架构无关的数据
│   ├── src				//源代码
│   └── X11R6			//X Windows系统
└── var					//存放可变的数据

1.3、NAND与NORFlash对比

NORNAND
Intel于1988年首先开发出NOR flash技术,彻底改变了原先由EPROM和EEPROM一统天下的局面东芝公司1989年发表了NAND flash结构,强调降低每比特的成本,更高的性能,并且象磁盘一样可以通过接口轻松升级
NOR的特点是芯片内执行(XIP, eXecute In Place),这样应用程序可以直接在flash闪存内运行,不必再把代码读到系统RAM中/
读速度相比稍快读速度相比稍慢
写速度相比慢写速度相比很快
擦除速度相比慢擦除速度相比很快
NOR flash带有SRAM接口,有足够的地址引脚来寻址,可以很容易地存取其内部的每一个字节NAND器件使用复杂的I/O口来串行地存取数据,各个产品或厂商的方法可能各不相同,8个引脚用来传送控制、地址和数据信息
容量相对小容量相对大
NOR闪存中每个块的最大擦写次数是十万次NAND闪存中每个块的最大擦写次数是一百万次

1.4、嵌入式系统常用文件系统

在嵌入式系统中,常用FLASH作为存储介质,由于其特殊的硬件结构,所以普通的文件系统如ext2,ext3等都不适合在其上使用,于是就出现了专门针对FLASH的文件系统,比较常用的有:JFFS2、YAFFS2、UBIFS、CRAMFS、SQUASHFS、LOGFS

1.5、Busybox

  • BusyBox combines tiny versions of many common UNIX utilities into a single small executable.
  • BusyBox has been written with size-optimization and limited resources in mind, both to produce small binaries and to reduce run-time memory usage.
  • Busybox is also extremely modular so you can easily include or exclude commands (or features) at compile time. This makes it easy to customize embedded systems; to create a working system, just add /dev, /etc, and a Linux kernel.

2、init进程启动流程

init进程(进程ID=1)是由内核启动的第一个也是唯一一个用户进程。

uboot:
......bootm → do_bootm
......................| → do_bootm_linux
........................................| → theKernel
kernel:
stext(head.S)
.....| → start_kernel
.....................| → rest_init
..................................| → kernel_init
.................................................| → init_post → /sbin/init   

2.1、kernel入口stext实现内容

  • 设置为SVC模式,关闭所有中断;
  • 获取CPU ID,提取相应的proc info;
  • 验证tags或者dtb;
  • 创建临时内核页表的页表项;
  • 配置r13寄存器,也就是设置打开MMU之后要跳转到的函数;
  • 使能MMU;
  • 跳转到start_kernel。
    详细分析

2.2、busybox-init进程

在嵌入式领域,通常使用的是busybox集成的init进程。查看/sbin/init可执行文件,其是busybox的一个软连接。

lrwxrwxrwx    1 root     root           14 Jan  3  2014 /sbin/init -> ../bin/busybox

2.3、/etc/inittab配置文件

如果存在/etc/inittab文件,busybox-init进程会解析此文件,并按照配置要求创建相应子进程;否则使用默认的配置创建子进程。在busybox主目录/examples/inittab文件中有对inittab文件的详细说明。

3、利用busybox制作嵌入式linux根文件系统步骤

#download busybox source https://busybox.net/downloads/
#(1)Decompression busybox-1.30.1.tar.bz2
tar -xjvf busybox-1.30.1.tar.bz2 -C ./busybox
cd ./busybox/busybox-1.30.1
#(2)Modify Makefile 
CROSS_COMPILE=arm-linux-
ARCH=arm
#(3)configurate
make menuconfig
make
make install
#directory(bin/sbin/usr)and file(linuxrc) are found in ./_install after executing make install
#(4)create rootfs directory
mkdir -p /opt/rootfs
#(5)copy directories and files from ./_install to rootfs
cp -rf ./_install/* /opt/rootfs
#(6)creat etc/ directory
mkdir -p /opt/rootfs/etc
#(7)copy files from busybox-1.30.1/examples/bootfloppy/etc/* to rootfs
cp -rf ./examples/bootfloppy/etc/* /opt/rootfs/etc
cd /opt/rootfs
#(8)create other directories
mkdir dev home lib mnt proc sys tmp var opt root
cd /opt/rootfs/etc
#(9)modify etc/inittab file
#/etc/inittab
::sysinit:/etc/init.d/rcS
::askfirst:-/bin/sh
::ctrlaltdel:/sbin/reboot
::shutdown:/bin/umount -a -r
#(10)modify etc/init.d/rcS file
#! /bin/sh
/bin/mount -a
#(11)modify etc/fstab file
proc		/proc	    proc	defaults     0	0
none		/dev/pts	devpts	mode=0622    0	0
tmpfs       /tmp        tmpfs   defaults     0  0
#(12)modify etc/profile file
# Ash profile 
# vim: syntax=sh
# No core files by default
ulimit -S -c 0 > /dev/null 2>&1
USER="`id -un`"
LOGNAME=$USER
PS1='[\u@\h \W]\# '
PATH=$PATH
HOSTNAME=`/bin/hostname`
export USER LOGNAME PS1 PATH
#(13)creat etc/passwd & etc/group
touch passwd group
vim passwd
root:x:0:0:root:/root:/bin/sh
vim group
root:x:0:root
#(14)creat dev/ directory (static)
mknod console c 5 1
mknod null c 1 3
mknod ttySAC0 c 204 64
mknod mtdblock0 b 31 0
mknod mtdblock0 b 31 1
mknod mtdblock0 b 31 2
mknod mtdblock0 b 31 3
or (14)creat dev/ directory (mdev)
#etc/fstab
proc		/proc	    proc	defaults     0	0
tmpfs       /tmp        tmpfs   defaults     0  0
sysfs       /sys        sysfs   defaults     0  0
tmpfs       /dev        tmpfs   defaults     0  0
#etc/init.d/rcS
mount -a
mkdir /dev/pts
mount -t devpts devpts /dev/pts
echo /sbin/mdev > /proc/sys/kernel/hotplug
mdev -s
#etc/inittab
::askfirst:-/bin/sh
#dev/
sudo mknod console c 5 1
sudo mknod null c 1 3
#create other directories
mkdir proc mnt tmp sys root
#(15)copy lib files
cd /opt/rootfs/lib
cp arm-none-linux-gnueabi/lib/*.so* ./ -d 
#(16)concrete NFS root filesystem
console=ttySAC0 root=/dev/nfs nfsroot=host_ip:/opt/rootfs ip=des_ip:host_ip:gatew:255.255.255.0:host_name:eth0:off
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值