根文件系统制作

时间:2021-03-11 16:31:27
    制作根文件系统大致内容:
手工构建最简化的/dev目录
利用udev构建完整的/dev目录
利用交叉编译工具链,构建/lib目录
手工构建/etc目录
编译/安装busybox,生成/bin、/sbin、/usr/bin、/usr/sbin目录
配置系统自动生成/proc目录

创建其它空目录


1.根文件系统树制作
在/opt/下新建文件夹rootfs

[luminqi@localhost opt]$ mkdir rootfs
[luminqi@localhost opt]$ cd rootfs
[luminqi@localhost rootfs]$ mkdir -p {apps,bin,data,dev,info,proc,root,sbin,sys,tmp,var,etc/{,init.d,dropbear},mnt/{,usb,sdc,nfs,dev},usr/{,bin,sbin,lib,share},lib/{,modules/{,3.0.0}}}
[luminqi@localhost rootfs]$ tree -L 3 
.
├── apps               #挂载Application所在分区用的目录
├── bin                #必备的用户命令,例如ls、cp等
├── data               #挂载data分区所在的目录
├── dev                #设备文件,例如mtdblock0
├── etc                #系统配置文件,包括启动文件,例如inittab等
│?? ├── dropbear      #dropbear ssh server依赖的文件
│?? └── init.d        #系统启动初始化脚本
├── info               #挂载info分区所在的目录
├── lib                #动态库所存放的目录
│?? └── modules       #insmod时,依赖/lib/modules/内核版本目录
│??     └── 3.0.0     #我们将Linux驱动放到该目录下,目录名对应内核版本号
├── mnt                #设备在运行时的一些挂载点
│?? ├── dev           #保留备用
│?? ├── nfs           #NFS挂载点
│?? ├── sdc           #SD卡挂载点
│?? └── usb           #U盘挂载点
├── proc               #proc文件挂载点,用来提供内核与进程信息的虚拟文件系统,由内核自动生成目录下的内容
├── root               #root用户目录
├── sbin               #必备的系统管理员命令,例如ifconfig、reboot等
├── sys                #sys文件系统挂载点,用来提供内核与设备信息的虚拟文件系统,由内核自动生成目录下的内容
├── tmp                #tmpfs文件系统挂载点,临时性的文件,重启后将自动清除
├── usr       
│?? ├── bin           #非必备的用户程序,例如find、du等
│?? ├── lib           #用户程序动态库放到这里 
│?? ├── sbin          #非必备的管理员程序
│?? └── share
└── var                #守护程序和工具程序所存放的可变,例如日志文件

27 directories, 0 files


2.Dev目录下创建设备文件
因为内核挂载完文件系统后,init进程需要用到/dev/console和/dev/null这两个设备文件来调用mdev构建dev,所以必须在制作文件系统时静态创建这两个设备文件,否则在系统启动时将提示
Waring:unable to open an initial console: 

[luminqi@localhost rootfs]$ sudo mknod -m666 dev/null c 1 3                     #空设备通常被用于丢弃不需要的输出流,或作为用于输入流的空文件
[luminqi@localhost rootfs]$ sudo mknod -m666 dev/console c 5 1                  #系统控制终端,系统的错误信息什么的都输出到这里
[luminqi@localhost rootfs]$ sudo mknod -m666 dev/ttyS0 c 4 64                   #串行端口终端,是使用计算机串行端口连接的终端设备。
[luminqi@localhost rootfs]$ sudo mknod -m666 dev/ttySAC0 c 4 64                 #串口驱动设备文件
[luminqi@localhost rootfs]$ sudo mknod dev/mtdblock0 b 31 0                     #mtd块设备
[luminqi@localhost rootfs]$ sudo mknod dev/mtdblock1 b 31 1                     #mtd块设备
[luminqi@localhost rootfs]$ sudo mknod dev/mtdblock2 b 31 2                     #mtd块设备
[luminqi@localhost rootfs]$ sudo mknod dev/mtdblock3 b 31 3                     #mtd块设备
[luminqi@localhost rootfs]$ sudo mknod dev/mtdblock4 b 31 4                     #mtd块设备

3.Var目录下创建符号链接文件

[luminqi@localhost rootfs]$ ln -s /tmp var/lock
[luminqi@localhost rootfs]$ ln -s /tmp var/log
[luminqi@localhost rootfs]$ ln -s /tmp var/run
[luminqi@localhost rootfs]$ ln -s /tmp var/tmp


4.拷贝交叉编译器中的动态库到相应的目录下

[luminqi@localhost rootfs]$ cp -af /opt/buildroot-2011.02/arm920t/usr/arm-linux/sysroot/lib/*so* lib/
[luminqi@localhost rootfs]$ cp -af /opt/buildroot-2011.02/arm920t/usr/arm-linux/lib/*so* lib/


5.etc目录下创建一些文件

5.1创建inittab文件

[luminqi@localhost rootfs]$ cd etc/
[luminqi@localhost etc]$ vim inittab
# /etc/inittab                                                                                                                     
#
# Copyright (C) 2017 lulinux <lulinux@gmail.com>
#
# Note: BusyBox init doesn't support runlevels.  The runlevels field is
# completely ignored by BusyBox init. If you want runlevels, use sysvinit.
#
# Format for each entry: <id>:<runlevels>:<action>:<process>
#
# id        == tty to run on, or empty for /dev/console. 
#              If specified, then /dev/$id device must exist
# runlevels == ignored, busybox doesn't support it
# action    == one of sysinit, respawn, askfirst, wait, and once
# process   == program to run

# Startup the system
# mount all the file systems specified in /etc/fstab 
::sysinit:/bin/mount -a

#Use mdev as hotplug to auto mount USB storage or SD card 
::sysinit:/bin/echo /sbin/mdev > /proc/sys/kernel/hotplug

#Use mdev to auto generate the device node in /dev path
::sysinit:/sbin/mdev -s

#make shm, pts support
::sysinit:/bin/mkdir -p /dev/pts
::sysinit:/bin/mkdir -p /dev/shm
::sysinit:/bin/mount -t devpts devpts /dev/pts

#Mount our apps/data partition
null::wait:/bin/mount -o sync,noatime,ro -t jffs2 /dev/mtdblock3 /apps
null::wait:/bin/mount -o sync,noatime,ro -t jffs2 /dev/mtdblock4 /data

#Set hostname 
null::sysinit:/bin/hostname -F /etc/hostname

#Enable console logon
null::respawn:/sbin/getty -L ttyS0 115200 vt100

# now run any rc scripts
null::wait:/etc/init.d/rcS

# system daemon
null::respawn:/sbin/syslogd -n
null::respawn:/sbin/klogd -n

# Stuff to do before rebooting
null::shutdown:/bin/umount /apps
null::shutdown:/bin/umount /data
null::shutdown:/bin/killall klogd
null::shutdown:/bin/killall syslogd
null::shutdown:/bin/umount -a -r
#null::shutdown:/sbin/swapoff -a

5.2创建/etc/init.d/rcS脚本

[luminqi@localhost etc]$ vim init.d/rcS
#!/bin/sh

# Copyright (C) 2017 lulinux <lulinux@gmail.com>
# Start all init scripts in /etc/init.d
# executing them in numerical order.
#                           

for i in /etc/init.d/S??* ; do
            $i
done

5.3配置网卡的启动脚本

[luminqi@localhost etc]$ vim init.d/S01_network
#!/bin/sh

ifconfig eth0 192.168.1.111 netmask 255.255.255.0 up


5.4创建支持/apps/etc目录下的启动脚本

[luminqi@localhost etc]$ vim init.d/S99_rcsApp
#!/bin/sh

# Copyright (C) 2017 lulinux <lulinux@gmail.com>
#
# Start all init scripts in /apps/etc/init.d
# executing them in numerical order.
#
if (test -d /apps/etc/init.d)
    then
    for i in /apps/etc/init.d/S??* ; do
    $i
    done
fi


5.5修改init.d目录下的文件权限
[luminqi@localhost etc]$ chmod 777 init.d/*


5.6创建fstab文件

[luminqi@localhost etc]$ vim fstab
# /etc/fstab: static file system information.
# Copyright (C) 2017 lulinux <lulinux@gmail.com>
#
#<File system> <mount pt>     <type>   <options>         <dump> <pass>
#devpts          /dev/pts       devpts   defaults          0      0
#/dev/root       /              ext2     rw,noauto         0      1
proc            /proc          proc     defaults          0      0                      
tmpfs           /tmp           tmpfs    defaults          0      0
tmpfs           /dev           tmpfs    defaults          0      0
sysfs           /sys           sysfs    defaults          0      0


5.7创建hostname,hosts,TZ文件

[luminqi@localhost etc]$ echo "root" > hostname
[luminqi@localhost etc]$ echo "127.0.0.1      localhost" >> hosts
[luminqi@localhost etc]$ echo "MST7MDT" >> TZ
[luminqi@localhost etc]$ echo "Copyright (C) 2017 linux<linux@gmail.com>" >> issue   # 系统登录时的提示信息
5.8创建profile文件

[luminqi@localhost etc]$ vim profile
# /etc/profile: system-wide .profile file for the Bourne shells.

export PATH=\
/bin:\
/sbin:\
/usr/bin:\
/usr/sbin:\
/usr/local/bin:\
/apps/bin:\
/apps/tools:\
/apps/tslib/bin\

# If running interactively, then:
if [ "$PS1" ]; then

    if [ "$BASH" ]; then
        export PS1="[\u@\h \W]\\$ "
        alias ll='/bin/ls --color=tty -laFh'
        alias ls='/bin/ls --color=tty -F'
        export LS_COLORS='no=00:fi=00:di=01;34:ln=01;36:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:ex=01;
32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.gz=01;31:*.bz2=01;31:*.deb=0
1;31:*.rpm=01;31:*.jar=01;31:*.jpg=01;35:*.jpeg=01;35:*.png=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35
:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.mpg=01;35:*.mpeg=01;35:*.avi=01;35:*.fli=01;35:*.gl=01;35:*.d
l=01;35:*.xcf=01;35:*.xwd=01;35:';
    else
      if [ "`id -u`" -eq 0 ]; then
        export PS1='>: '
      else
        export PS1='>: '
      fi
    fi
# System Setting
    set -o vi
    alias  ll='ls -l'
    export USER=`id -un`
    export LOGNAME=$USER
    export HOSTNAME=`/bin/hostname`
    export HISTSIZE=1000
    export HISTFILESIZE=1000
    export PAGER='/bin/more '
    export EDITOR='/bin/vi'
    export INPUTRC=/etc/inputrc
    export DMALLOC_OPTIONS=debug=0x34f47d83,inter=100,log=logfile
    export VAR1=
    export VAR2=
    export VAR3=
    export VAR4=
    export VAR5=
    export LD_LIBRARY_PATH=/lib:/usr/lib/
# QT Extendded 4.4.3 Setting
    export QTDIR=/apps/qt-extended-4.4.3
    export QWS_MOUSE_PROTO='TSLIB:/dev/event0'
    export QWS_DISPLAY='LinuxFB:/dev/fb0'
    export QWS_DISPLAY='LinuxFB:mmWidth240:mmHeight320:0'
    export QWS_SIZE='240x320'
    export QT_PLUGIN_PATH=$QTDIR/plugins/
    export QT_QWS_FONTDIR=$QTDIR/lib/fonts
    export PATH=$QTDIR/bin:$PATH
    export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$QTDIR/lib
# Touch Scree tslib Setting
    export TSLIB_ROOT=/apps/tslib
    export TSLIB_CONFFILE=$TSLIB_ROOT/etc/ts.conf
    export TSLIB_CALIBFILE=$TSLIB_ROOT/etc/pointercal
    export TSLIB_TSDEVICE=/dev/event0
    export TSLIB_CONSOLEDEVICE=none
    export TSLIB_FBDEVICE=/dev/fb0
fi;

5.9创建指定一些协议所使用的端口号文件protocols

[luminqi@localhost etc]$ vim protocols
# /etc/protocols:
# $Id: protocols,v 1.1.1.1 2001/09/12 19:03:24 andersee Exp $
#
# Internet (IP) protocols
#
#       from: @(#)protocols     5.1 (Berkeley) 4/17/89
#
# Updated for NetBSD based on RFC 1340, Assigned Numbers (July 1992).

ip      0       IP              # internet protocol, pseudo protocol number
icmp    1       ICMP            # internet control message protocol
igmp    2       IGMP            # Internet Group Management
ggp     3       GGP             # gateway-gateway protocol
ipencap 4       IP-ENCAP        # IP encapsulated in IP (officially ``IP'')
st      5       ST              # ST datagram mode
tcp     6       TCP             # transmission control protocol
egp     8       EGP             # exterior gateway protocol
pup     12      PUP             # PARC universal packet protocol
udp     17      UDP             # user datagram protocol
hmp     20      HMP             # host monitoring protocol
xns-idp 22      XNS-IDP         # Xerox NS IDP
rdp     27      RDP             # "reliable datagram" protocol
iso-tp4 29      ISO-TP4         # ISO Transport Protocol class 4
xtp     36      XTP             # Xpress Tranfer Protocol
ddp     37      DDP             # Datagram Delivery Protocol
idpr-cmtp       39      IDPR-CMTP       # IDPR Control Message Transport
rspf    73      RSPF            #Radio Shortest Path First.
vmtp    81      VMTP            # Versatile Message Transport
ospf    89      OSPFIGP         # Open Shortest Path First IGP
ipip    94      IPIP            # Yet Another IP encapsulation
encap   98      ENCAP           # Yet Another IP encapsulation
5.10创建mdev.conf文件
mdev会在/etc目录下找mdev的配置文件: mdev.conf. 如果该文件不存在,那么在执行mdev -s这个命令时,会提示找不到mdev.conf,这时我们可以建一个空的mdev.conf文件解决这个问题。下面创建使用mdev自动挂载u盘和SD卡的配置/etc/mdev.conf

[luminqi@localhost etc]$ vim mdev.conf
sd[a-z][0-9]      0:0 0777        @(mount /dev/$MDEV /mnt/usb)
sd[a-z]           0:0 0777        $(umount /mnt/usb)
ub[a-z][0-9]      0:0 0777        @(mount /dev/$MDEV /mnt/usb)
ub[a-z]           0:0 0777        $(umount /mnt/usb)
mmcblk[0-9]p[0-9] 0:0 0777        @(mount /dev/$MDEV /mnt/sdc)
mmcblk[0-9]       0:0 0777        $(umount /mnt/sdc)

5.11创建用户组group文件
[luminqi@localhost etc]$ vim group
root:x:0:root


5.12创建用户passwd文件,这里我们设置为不用密码登陆
[luminqi@localhost etc]$ vim passwad
root::0:0:99999:7:::


5.13创建密码映射shadow文件
[luminqi@localhost etc]$ vim shadow
root:($jGZIHmtT$y8ZXoPllK12/wl51kMw4e/:0:0:99999:7:::)


6.在文件系统中安装busybox

[luminqi@localhost etc] cd /opt
[luminqi@localhost opt]$ ls rootfs
apps  bin  data  dev  etc  info  lib  mnt  proc  root  sbin  sys  tmp  usr  var
[luminqi@localhost opt] sudo tar -xjf /opt/busybox-1.20.2.tar.bz2    //事先准备好的源码包
[luminqi@localhost opt] cd busybox-1.20.2
[luminqi@localhost busybox-1.20.2]$ vim Makefile
#修改CROSS_COMPILER为:
CROSS_COMPILE ?= /opt/buildroot-2012.08/arm920t/usr/bin/arm-linux-

[luminqi@localhost busybox-1.20.2]$ vt100
[luminqi@localhost busybox-1.20.2]$ sudo make menuconfig
Busybox Settings  --->
       General Configuration  --->
             [*] Don't use /usr
       Installation Options ("make install" behavior)  --->
             What kind of applet links to install (as soft-links)  --->   //安装busybox时将各个命令安装为指向busybox的软链接
             (/opt/rootfs) BusyBox installation prefix
[luminqi@localhost busybox-1.20.2]$ sudo make
[luminqi@localhost busybox-1.20.2]$ file busybox
busybox: ELF 32-bit LSB executable, ARM, version 1 (SYSV), statically linked, stripped
[luminqi@localhost busybox-1.20.2]$ sudo make install
[luminqi@localhost busybox-1.20.2]$ ls /opt/rootfs
apps  bin  data  dev  etc  info  lib  linuxrc  mnt  proc  root  sbin  sys  tmp  usr  var
##仔细看多了一个linuxrc文件
linuxrc -> bin/busybox


7.移植dropbear
7.1首先编译生成PC版的,在制作密钥时用到

[luminqi@localhost busybox-1.20.2]$ cd /home/luminqi/fl2440/3rdparty
[luminqi@localhost 3rdparty]$ wget http://matt.ucc.asn.au/dropbear/releases/dropbear-0.53.1.tar.bz2
[luminqi@localhost 3rdparty]$ tar -xjf dropbear-0.53.1.tar.bz2 
[luminqi@localhost 3rdparty]$ cd dropbear-0.53.1
[luminqi@localhost dropbear-0.53.1]$ ./configure && make
[luminqi@localhost dropbear-0.53.1]$ ./dropbearkey -t rsa -f /opt/rootfs/etc/dropbear/dropbear_rsa_host_key
[luminqi@localhost dropbear-0.53.1]$ ./dropbearkey -t dss -f /opt/rootfs/etc/dropbear/dropbear_dss_host_key
[luminqi@localhost dropbear-0.53.1]$ chmod 666 /opt/rootfs/etc/dropbear/dropbear_*


7.2编译ARM版本的

[luminqi@localhost dropbear-0.53.1]$ make distclean
[luminqi@localhost dropbear-0.53.1]$ ./configure CC=/opt/buildroot-2012.08/arm920t/usr/bin/arm-linux-gcc --build=i686 --host=arm-linux --disable-zlib
[luminqi@localhost dropbear-0.53.1]$ make
[luminqi@localhost dropbear-0.53.1]$ file dropbear
dropbear: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), not stripped
[luminqi@localhost dropbear-0.53.1]$ file dbclient
dbclient: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), not stripped
[luminqi@localhost dropbear-0.53.1]$ mv dbclient ssh
[luminqi@localhost dropbear-0.53.1]$ arm-linux-strip dropbear
[luminqi@localhost dropbear-0.53.1]$ arm-linux-strip ssh
[luminqi@localhost dropbear-0.53.1]$ cp dropbear ssh /opt/rootfs/usr/sbin/
 
[luminqi@localhost rootfs]$ ll
总用量 60
drwxrwxr-x. 2 luminqi luminqi 4096 3月   3 23:17 apps
drwxrwxr-x. 2 luminqi luminqi 4096 3月   4 00:29 bin
drwxrwxr-x. 2 luminqi luminqi 4096 3月   3 23:17 data
drwxrwxr-x. 2 luminqi luminqi 4096 3月   3 23:20 dev
drwxrwxr-x. 4 luminqi luminqi 4096 3月   4 14:52 etc
drwxrwxr-x. 2 luminqi luminqi 4096 3月   3 23:17 info
drwxrwxr-x. 3 luminqi luminqi 4096 3月   3 23:24 lib
lrwxrwxrwx. 1 root    root      11 3月   4 00:29 linuxrc -> bin/busybox
drwxrwxr-x. 6 luminqi luminqi 4096 3月   3 23:17 mnt
drwxrwxr-x. 2 luminqi luminqi 4096 3月   3 23:17 proc
drwxrwxr-x. 2 luminqi luminqi 4096 3月   3 23:17 root
drwxrwxr-x. 2 luminqi luminqi 4096 3月   4 00:29 sbin
drwxrwxr-x. 2 luminqi luminqi 4096 3月   3 23:17 sys
drwxrwxr-x. 2 luminqi luminqi 4096 3月   3 23:17 tmp
drwxrwxr-x. 6 luminqi luminqi 4096 3月   3 23:17 usr
drwxrwxr-x. 2 luminqi luminqi 4096 3月   3 23:21 var

8.在文件系统中创建启动脚本

[luminqi@localhost roorfs]$ cd /opt/rootfs
[luminqi@localhost rootfs]$ vim etc/init.d/S04_dropbear
#!/bin/sh
/usr/sbin/dropbear
chmod 777 etc/init.d/S04_dropbear

到这里根文件系统制作完成,但是此时是一个文件夹,我们无法烧写到开发板的nandflash上,要对它进行“ 打包”,做成映像文件的形式。

例如:

制作Initramfs文件系统,原理:上面制成的根文件系统(rootfs这个文件夹)在编译内核的同时被编译并与内核生成一个映像文件(cpio包格式),这种系统是基于RAM(内存)的,它的挂载点在内存中,而不是在nandflash.
制作jffs2文件系统,原理:通过工具mkfs.jffs2将根文件系统(rootfs这个文件夹)制作成映像文件,这种系统是基于flash的,由MTD管理,下面显示了jffs2映像文件的类型:

[luminqi@localhost opt]$ file rootfs.jffs2 
rootfs.jffs2: Linux jffs2 filesystem data little endian
对于根文件系统和文件系统,我觉得可以这样理解:

根文件系统是最起码能使操作系统跑起来的必须文件的集合,比如/bin、/sbin下的命令文件,还有/etc下的一些配置文件、启动文件inittab等等,如果缺了某一个关键文件,就不能成为根文件系统,Linux内核不承认这种文件系统,导致报错而退出,挂载失败。

赋予根文件系统不同属性,根文件系统就变成了不同的文件系统:

比如赋予根文件系统jffs2的属性(通过工具mkfs.jffs2),便生成了jffs2文件系统.