Building Linux Root File System

Posted by fgm on Sun, 19 May 2019 21:28:46 +0200

Building Linux Root File System

1. Basic knowledge

1.1. Basic concepts

In computing, a file system or file system controls how data is stored and retrieved. Without a file system, information placed in a storage medium would be a large body of data with no way to tell where one piece of information stops PS and the next begins. By separating the data in to pieces and giving each piece a name, the information is easy I Take its name from the way paper-based information systems are named, each group of data is called a "file". The structure and logic rules are used to manage the groups of information and their names are called a "file system" from 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 file system on top of which all other file systems are mounted as the system boots up.

1.2. Linux Root File System Directory Structure

FHS (Filesystem Hierarchy Standard) defines the principles of directory and file classification storage in file system; the minimum set of files and directories required for system operation; and lists the exceptions and reasons for not following these principles. Most Linux and UNIX distributions follow FHS. When building a file system, it is recommended to follow the FHS standard.

/
├── bin					//This directory holds basic commands that all users can use.
├── dev					//Equipment files stored in this directory
├── etc					//This directory holds various configuration files
│   ├── export			//Used to configure NFS file system(Optional file)
│   ├── fstab			//Used to indicate when to execute`mount -a`When the file system needs to be hooked up(Optional file)
│   ├── ftpusers			//Used to configure user access rights when starting FTP services(Optional file)
│   ├── group			//User Group Files(Optional file)
│   ├── inittab			//Configuration file for init process(Optional file)
│   ├── ld.so.conf		//Paths to other shared libraries(Optional file)
│   ├── mtab				//A link file used to display loaded file systems, usually / proc/mounts(Optional file)
│   ├── opt				//Used to configure programs under / opt(Optional directory)
│   ├── passwd			//Password file(Optional file)
│   ├── sgml				//Used to configure SGML(Optional directory)
│   ├── X11				//Used to configure X Windows(Optional directory)
│   └── xml				//Used to configure XML(Optional directory)
├── home				//User directory(Optional)
├── lib					//Shared libraries and loadable modules are stored in this directory
│   ├── ld*				//Connector, Loader
│   ├── libc.so.*		//Dynamic library
│   └── modules			//Store kernel loadable modules
├── mnt					//Temporarily mount a file system's mount point
├── proc				//Connection Points of proc File System
├── root				//root user directory
├── sbin				//Store system commands that only administrators can use
├── tmp					//Storage of temporary documents
├── usr					//Store shared, read-only programs and data
│   ├── bin
│   ├── games			//Game
│   ├── include			//header file
│   ├── lib				//Library file
│   ├── local			//Local directory
│   ├── sbin				//Non-essential system commands
│   ├── share			//Architecture-independent data
│   ├── src				//source code
│   └── X11R6			//X Windows System
└── var					//Store variable data

1.3. Comparison of NAND and NORFlash

NOR NAND
Intel first developed NOR flash technology in 1988, which completely changed the situation that EPROM and EEPROM dominated the world. Toshiba released the NAND flash architecture in 1989, emphasizing lower cost per bit, higher performance, and easy upgrade through interfaces like disks.
NOR is characterized by on-chip execution (XIP, eXecute In Place), so that applications can run directly in flash memory without having to read code into system RAM. /
Read faster than you can. Reading speed is slightly slower
Write slowly Writing speed is fast
Erase speed is slower Erasion speed is faster than that.
NOR flash has SRAM interface and has enough address pins to address it. It can easily access every byte in it. NAND devices use complex I/O ports to access data serially. Each product or manufacturer may use different methods. Eight pins are used to transmit control, address and data information.
Relatively small capacity Relatively large capacity
Maximum number of erases per block in NOR flash memory is 100,000 The maximum number of erases per block in NAND flash memory is one million

1.4. Common File Systems for Embedded Systems

In embedded systems, FLASH is commonly used as storage medium. Because of its special hardware structure, ordinary file systems such as ext2 and ext3 are not suitable for use on it. So there are file systems specially for FLASH. The commonly used ones are JFFS2, YAFFS2, UBIFS, CRAMFS, SQUASHFS and 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 process startup process

The init process (process ID=1) is the first and only user process started by the kernel.

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 Entry Step Implementation Content

  • Set to SVC mode to close all interrupts.
  • Get the CPU ID and extract the corresponding proc info.
  • Verify tags or dtb;
  • Create page table entries for temporary kernel page tables;
  • Configure the r13 register, which is the function to jump to after opening the MMU.
  • Enable MMU;
  • Jump to start_kernel.
    Detailed analysis

2.2. busybox-init process

Busybox integrated init process is commonly used in embedded field. View the / sbin/init executable, which is a soft connection to busybox.

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

2.3, /etc/inittab configuration file

If the / etc/inittab file exists, the busybox-init process parses the file and creates the corresponding sub-process according to the configuration requirements; otherwise, the default configuration is used to create the sub-process. A detailed description of the inittab file is provided in the busybox home directory / examples/inittab file.

3. Steps of Making Embedded linux Root File System with busybox

#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

Topics: Programming Linux vim Unix Windows