Build a basic embedded linux root file system
Source: chinaunix blog Date: 2010.01.13 (0 comments in total) I want to comment
Create a root file system.
Several concepts that need to be understood before compiling busybox.
1. Build busybox as a static binary (no shared libs ). That is, whether busybox is compiled into a shared library or a static library. What are their differences?
Compile to static (in this case, we generally do not copy the libraries in the tool chain to the Lib of the file system we created ):
Then the generated/bin/busybox can be executed independently. Other libraries are not used because the Library (such as libc) is required during compilation) it is also compiled into the executable file/bin/busybox.
Commands compiled and processed by busybox, such as Mount, ls, and CAT, can also be executed independently for the same reason, that is, the libraries required for its running are also compiled into their respective executable files during compilation.
In this way, a problem occurs. Because each executable file contains a library, the compiled executable file is large, in addition, each executable file may contain the same library (such as libc), which wastes a lot of space.
For example, if I want to run a helloword, use arm-Linx-gcc-O helloword. C. Put helloword in a statically compiled busybox, which cannot be executed. Because no one in arm-Linx-gcc-O helloword. C is compiled into a dynamic library, but the Library (libc) is not required for running it, it cannot be executed. If you want to execute the command, add-static to compile it into the form of static library arm-Linx-gcc-O helloword. C-static, so that it contains the required library. In this way, the compiled helloword can be executed.
Compiled into a shared library:/bin/busybox cannot be executed separately. The required library must be included, we need to copy the library in the tool chain to the Lib of the file system we created. This is where you can share the library, the application Program (For example, helloword and CAT commands) do not need to be compiled into their respective executable files, saving space.
In embedded systems, it is generally compiled into a shared library. Unless you just perform some very simple forms.
2. Start.
When config_blk_dev_initrd = y is selected. We put the prepared File System under the directory myrootfs_shared (the directory is Bin, etc, Lib, etc.), then put the folder in the top-level directory of the kernel, and choose it during compilation.
Initial Ram filesystem and RAM disk (initramfs/initrd) Support │
│ (Myrootfs_shared) initramfs source file (s)
In this case, it can be considered as ramdisk startup.
If not selected, it is determined based on config_cmdline. config_cmdline = "root =/dev/nfs nfsroot = 192.168.1.100:/nfsroot/rootfs, rsize = 1024, wsize = 1024 IP = 192.168.1.101: 192.168.1.100: 192.168.1.100: 255.255.255.0: usb0: On console = ttys1, 115200 mem = 128 M init =/init Android uart_dma = 1 ".
Root =... indicates the device used as the root file system during kernel startup. For example, you can specify the root file as hda8: Root =/dev/hda8.
However, no matter which method is used for startup, the name of the initialization process executed by the kernel is determined by init = (a user process started in the kernel is INIT ). if this item is not set, the kernel will try/etc/init in order,
/Bin/init,/sbin/init,/bin/sh. If none of them are found, the kernel will throw the kernel panic: error.
For example, init =/linuxrc, the first process to be executed is the linuxrc under the directory.
The compiled busybox contains the bin, sbin directory, and linuxrc file. While linuxrc-> bin/busybox, we can see that bin/busybox is executed. Busybox will eventually execute the shell script/etc/init. d/RCS. We generally do what we want in the RC.
From this point of view, the name of linuxrc does not matter when a file system is being created. You only need to ensure that we run bin/busybox for the first time (so that we can ensure that the shell file of RCS is run ). This requires the combination of cmdline and the file system link to the bin/busybox file.
For example, if we specify init =/test_init in config_cmdline, we only need to execute ln-s bin/busybox test_init in the root directory of the file system when creating the file system. (Copy the bin/busybox generated by compiling busybox to the bin directory of the file system)
Note: we need to ensure that test_init and RCS can be executed. To save trouble, after completing the operation, I put the entire rootfs directory sudo chmod 777 rootfs-R
1, 1) create the basic directory structure of the root file system.
I made this process a shell script (named mkroot)
#! /Bin/sh
Echo "creatint rootfs dir ......"
Mkdir rootfs
CD rootfs
Echo "Making dir: Bin Dev etc lib proc sbin sys USR"
Mkdir bin Dev etc lib proc sbin sys USR # required 8 Directories
Mkdir usr/bin usr/lib usr/sbin lib/modules
# Don't use mknod, unless you run this script as root!
Sudo mknod-m 600 DEV/console C 5 1 # required Device
Sudo mknod-M 666 DEV/null C 1 3 # required Device
Echo "Making dir: mnt tmp Var"
Mkdir mnt tmp VaR
Chmod 1777 TMP
Mkdir MNT/etc MNT/jffs2 MNT/yaffs MNT/Data MNT/temp
Mkdir var/lib var/lock var/log var/run var/tmp
Chmod 1777 var/tmp
Echo "Making dir: Home root Boot"
Mkdir home root boot
Echo "done"
Grant the shell executable permission. Sudo chmod 777 mkroot
Then run./mkroot. In the middle, enter the password when generating the device. In this way, the basic directory will be available.
(2) configure, compile, and install busybox
It is best to go to the official website, where else may sometimes be unable to compile. I use busybox-1.15.2.tar.bz2 under the official website. Then modify makefile.
Block this part (about 185 rows)
# Subarch: = $ (shell uname-M | sed-e s/I .86/i386/-E S/sun4u/sparc64 /\
#-E s/arm. */ARM/-e s/sa110/ARM /\
#-E s/s390x/s390/-e s/parisc64/parisc /\
#-E s/PPC. */PowerPC/-e s/MIPS. */MIPS /)
2 places below
# Arch? = $ (Subarch)
# Cross_compile? =
Change
Arch = arm
Cross_compile = arm-Linux-
Note: Because I have added the location of the toolchain to the environment variable, cross_compile = arm-Linux-; otherwise, the complete path must be provided. Cross_compile =/home/mxzh/smartphone/ww21/pxalinux/toolchain/arm-linux-4.1.1/ARM-Linux-
# Make menuconfig
Busybox settings --->
Build options --->
[] Build busybox as a static binary (no shared libs) // (1)
Installation Options --->
Don't use/usr // (2)
Linux system utilities --->
Mdev // (3)
Support/etc/mdev. conf
Support Command Execution At device addition/removal
(1) This option must be selected so that busybox can be compiled into an executable file with static links, and the runtime is independent of other function libraries. otherwise, other library files must be required to run and cannot work properly on a single Linux kernel. If static compilation is selected, if not selected, copy the library in the toolchain. We do not select it here.
(2) This option must also be selected. Otherwise, after make install, busybox will be installed under/usr of the original system, which will overwrite the original command of the system. after this option is selected, make install will generate a directory named _ install under the busybox Directory, which contains busybox and its link.
(3) If not started
Udev
, Causing no device files under/dev. That is to say, all devices are not mounted. The latest busybox contains the simplified udev version, namely mdev, which is very easy to use. To use mdev, you also need to configure it in rootfs.
You must also select the sh command.
You can add some basic commands, but you can leave them unselected.
Run make & make install.
(3) copy the library in the tool chain to the Lib of the file system.
If it is compiled to static, this step does not work.
[Email = mxzh @ mxzh :~ /Work_test/mkrootfs/busybox-1.15.2 $] mxzh @ mxzh :~ /Work_test/mkrootfs/busybox-1.15.2 $ [/Email]
/Home/mxzh/smartphone/ww21/pxalinux/toolchain/arm-linux-4.1.1/bin/ARM-iwmmxt-Linux-gnueabi-readelf-D busybox
In this way, you can view the library required by busybox.
Dynamic section at offset 0x3700c contains 24 entries:
Tag type name/value
0x00000001 (needed) Shared Library: [libc. so.6]
0x0000000c (init) 0xa56c
0x0000000d (fini) 0x33700
0x00000019 (init_array) 0x47000
0x0000001b (init_arraysz) 4 (bytes)
0x0000001a (fini_array) 0x47004
0x0000001c (fini_arraysz) 4 (bytes)
0x00000004 (hash) 0x80e8
0x00000005 (strtab) 0x95e8
0x00000006 (symtab) 0x8798
0x0000000a (strsz) 1761 (bytes)
0x0000000b (syment) 16 (bytes)
0x00000015 (Debug) 0x0
0x00000003 (pltgot) 0x470f4
0x00000002 (pltrelsz) 1664 (bytes)
0x00000014 (pltrel) REL
0x00000017 (jmprel) 0x9eec
0x00000011 (rel) 0x9eb4
0x00000012 (relsz) 56 (bytes)
0x00000013 (relent) 8 (bytes)
0x6ffffffe (verneed) 0x9e94
0x6fffffff (verneednum) 1
0x6ffffff0 (versym) 0x9cca
0x00000000 (null) 0x0
As shown above, the libc library is required to copy the Library to the lib directory of the file system.
$ Cd ~ /Work_test/mkrootfs/rootfs/lib
$ CP-D/home/mxzh/smartphone/ww21/pxalinux/toolchain/arm-linux-4.1.1/ARM-iwmmxt-Linux-gnueabi/lib/LD *./
$ CP/home/mxzh/smartphone/ww21/pxalinux/toolchain/arm-linux-4.1.1/ARM-iwmmxt-Linux-gnueabi/lib/libc. so.6 ./
In this way, you can.
(4) add commands and initialization scripts
Copy the bin and sbin directories under the _ install directory generated by the compilation of busybox to the directory of the file system. For linuxrc, it is just a link instead of a copy.
Add the/etc/init. d/RCS file to add the automatic execution part.
#! /Bin/sh
Path =/sbin:/bin:/usr/sbin:/usr/bin:/usr/lib:
Runlevel = s
Prevlevel = N
Umask 022
Export path runlevel prevlevel
#
# Trap CTRL-C & C only in this shell so we can interrupt subprocesses.
#
/Bin/Mount-T proc NONE/proc
/Bin/Mount-T tmpfs NONE/root
/Bin/Mount-T tmpfs NONE/tmp
/Bin/Mount-T tmpfs NONE/var
/Bin/Mount-T tmpfs NONE/dev
/Bin/Mount-T sysfs NONE/sys
/Bin/mkdir-P/var/lib
/Bin/mkdir-P/var/run
/Bin/mkdir-P/var/log
/Bin/mknod-M 660/dev/console C 5 1
/Bin/mknod-M 660/dev/null C 1 3
/Bin/mknod-M 666/dev/ptmx C 5 2
/Bin/mkdir/dev/SHM
/Bin/mkdir/dev/PTS
Echo "Starting mdev ..."
/Sbin/mdev-S
Echo/sbin/mdev>/proc/sys/kernel/hotplug
Then execute ln-s bin/busybox test_init in the directory of the file system.
To ensure executable, sudo chmod 777 rootfs-R
In this way, the entire file system rootfs is made.
You can use rootfs as the NFS directory for Small board mounting,
2. Copy the entire rootfs directory to the top layer of the kernel directory and configure the kernel as the ramdis file system. The specific configuration is as follows: General setup --->
Initial Ram filesystem and RAM disk (initramfs/initrd) Support │
│ (Rootfs) initramfs source file (s)
Here, you also need to specify init =/test_init in cmdline, which corresponds to the above ln-s bin/busybox test_init.
If it is ln-s bin/busybox linxurc, specify init =/linxurc in cmdline.
If it is ln-s bin/busybox init, specify init =/init in cmdline.
For details, refer:
Http://blog.chinaunix.net/u1/34474/showart_485837.html
Http://blog.chinaunix.net/u3/90973/showart_1834111.html
Http://blog.chinaunix.net/u3/100239/showart_2036548.html
This article is from the chinaunix blog. If you want to view the original text, click: Http://blog.chinaunix.net/u2/66024/showart_2147649.html