在学习linux时,学习制作根文件系统是一个必不可少的环节,它并没有大家想象中的那么困难。
1、制作目录树
1.1 制作根目录
[lingyun@localhost linux-3.0]$mkdir rootfs_tree
[lingyun@localhost keyue]$ cd rootfs_tree/
[lingyun@localhost rootfs_tree]$ ls
[lingyun@localhost rootfs_tree]$ 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}}}
[lingyun@localhost rootfs_tree]$ tree
1.2 Dev 目录下创建设备文件
因为内核挂载完文件系统后,init 进程需要用到/dev/console 和/dev/null 这两个设备文件来调用mdev 构建dev,所以必须在制作文件系统
时静态创建这两个设备文件,否则在系统启动时将提示:Waring:unable to open an initial console:
[lingyun@localhost rootfs_tree]$ sudo mknod -m666 dev/console c 5 1
[lingyun@localhost rootfs_tree]$ sudo mknod -m666 dev/ttyS0 c 4 64
[lingyun@localhost rootfs_tree]$ sudo mknod -m666 dev/ttySAC0 c 4 64
[lingyun@localhost rootfs_tree]$ sudo mknod dev/mtdblock0 b 31 0
[lingyun@localhost rootfs_tree]$ sudo mknod dev/mtdblock1 b 31 1
[lingyun@localhost rootfs_tree]$ sudo mknod dev/mtdblock2 b 31 2
[lingyun@localhost rootfs_tree]$ sudo mknod dev/mtdblock3 b 31 3
[lingyun@localhost rootfs_tree]$ sudo mknod dev/mtdblock4 b 31 4
[lingyun@localhost rootfs_tree]$ sudo mknod dev/mtdblock5 b 31 5
[lingyun@localhost rootfs_tree]$ sudo mknod dev/mtdblock6 b 31 6
[lingyun@localhost rootfs_tree]$ sudo mknod dev/mtdblock7 b 31 7
[lingyun@localhost rootfs_tree]$ sudo mknod dev/mtdblock8 b 31 8
[lingyun@localhost rootfs_tree]$ sudo mknod dev/mtdblock9 b 31 9
[lingyun@localhost rootfs_tree]$ ls -l dev/
1.3 Var 目录下创建符号链接文件
[lingyun@localhost rootfs_tree]$ ln -s /tmp var/lock
[lingyun@localhost rootfs_tree]$ ln -s /tmp var/log
[lingyun@localhost rootfs_tree]$ ln -s /tmp var/run
[lingyun@localhost rootfs_tree]$ ln -s /tmp var/tmp
[lingyun@localhost rootfs_tree]$ ls -l var/
1.4 拷贝交叉编译器中的动态库到相应的目录下
[lingyun@localhost rootfs_tree]$ cp -rf /opt/buildroot-2012.08/arm920t/usr/arm-unknown-linux-uclibcgnueabi/sysroot/lib/*so* lib/
[lingyun@localhost rootfs_tree]$ cp -rf /opt/buildroot-2012.08/arm920t/usr/arm-unknown-linux-uclibcgnueabi/lib/*so* lib/
[lingyun@localhost rootfs_tree]$ ls lib/
1.5 Etc 目录下创建一些文件
1.5.1 创建 inittab 文件
[lingyun@localhost rootfs_tree]$ cd etc/
[lingyun@localhost etc]$ vim inittab
# /etc/inittab
# 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/info partition
null::wait:/bin/mount -o sync,noatime,ro -t jffs2 /dev/mtdblock6 /apps
null::wait:/bin/mount -o sync,noatime,ro -t jffs2 /dev/mtdblock7 /info
#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 /info
null::shutdown:/bin/killall klogd
null::shutdown:/bin/killall syslogd
null::shutdown:/bin/umount -a -r
#null::shutdown:/sbin/swapoff -a
1.5.2 创建/etc/init.d/rcS 脚本
[lingyun@localhost etc]$ vim init.d/rcS
#!/bin/sh
# Start all init scripts in /etc/init.d
# executing them in numerical order.
#
for i in /etc/init.d/S??* ; do
$i
done
1.5.3 配置网卡的启动脚本
[lingyun@localhost etc]$ vim init.d/S01_network
#!/bin/sh
ifconfig eth0 192.168.1.111 netmask 255.255.255.0 up
1.5.3 创建支持/apps/etc 目录下的启动脚本
[lingyun@localhost etc]$ vim init.d/S99_rcsApp
#!/bin/sh
# 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
1.5.4 修改 init.d 目录下的文件权限
[lingyun@localhost etc]$ chmod 777 init.d/*
[lingyun@localhost etc]$ ll init.d/
1.5.5 创建 fstab 文件
[lingyun@localhost etc]$ vim fstab
# /etc/fstab: static file system information.
#<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
1.5.6 创建 hostname,hosts,TZ 文件
[lingyun@localhost etc]$ echo "root" > hostname
[lingyun@localhost etc]$ echo "127.0.0.1 localhost" >> hosts
[lingyun@localhost etc]$ echo "MST7MDT" >> TZ
[lingyun@localhost etc]$ echo "Copyright (C) keyue <keyue654321.163.com>" >> issue
1.5.7 创建 profile 文件
[lingyun@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=01;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:*.dl=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 export TSLIB_TSDEVICE=/dev/event0
export TSLIB_CONSOLEDEVICE=none
export TSLIB_FBDEVICE=/dev/fb0
fi;
1.5.8 创建指定一些协议所使用的端口号文件 protocols
[lingyun@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
1.5.9 创建mdev.conf 文件
[lingyun@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)
1.5.10 创建用户组group 文件
[lingyun@localhost etc]$ vim group
root:x:0:root
1.5.11 创建用户passwd 文件
[lingyun@localhost etc]$ vim passwd
root:x:0:0:root:/:/bin/sh
1.5.12 创建密码映射shadow 文件
[lingyun@localhost etc]$ vim shadow //显示的是加密之后的文件
注1:group、passwd、shadow的内容可在服务器下获取。
[lingyun@localhost etc]$ vim /etc/group
[lingyun@localhost etc]$ vim /etc/passwd
[lingyun@localhost etc]$sudo vim /etc/shadow
注2:如果设为不用密码登陆,需将passwd的内容清空
[lingyun@localhost rootfs]$ vim etc/shadow
root::0:0:99999:7:::
2、在文件系统中安装busybox
[lingyun@localhost keyue]$tar -xjf ~/keyue/busybox-1.20.2.tar.bz2
[lingyun@localhost keyue]$ cd busybox-1.20.2/
[lingyun@localhost busybox-1.20.2]$vim Makefile
#修改164行 CROSS_COMPILER 为:
CROSS_COMPILE ?= /opt/buildroot-2012.08/arm920t/usr/bin/arm-linux-
[lingyun@localhost busybox-1.20.2]$ vt100
[lingyun@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) --->
(../rootfs_tree) BusyBox installation prefix
[lingyun@localhost busybox-1.20.2]$ make
[lingyun@localhost busybox-1.20.2]$ file busybox
busybox: ELF 32-bit LSB executable, ARM, version 1 (SYSV), statically linked, stripped
[lingyun@localhost busybox-1.20.2]$ make install
[lingyun@localhost busybox-1.20.2]$ cd ../rootfs_tree/
[lingyun@localhost rootfs_tree]$ ln -s bin/busybox init
[lingyun@localhost busybox-1.20.2]$ ls ../rootfs_tree/
至此文件系统的目录树基本完成。