Build a K embedded Linux root file system UCFS from scratch

Source: Internet
Author: User

I have read many articles and have never understood how to create a file system. I am lucky to find such a good article on the Internet!

Reprinted here, hoping to benefit more people.

(Of course, I changed some of my mistakes in practice. I forget that the original author doesn't mind)

++
Author: melancholy bucket (tongmvp)
Date:
Copyright Disclaimer: Reprinted with the source
++

Build a K embedded linux root file system ucfs from scratch

The so-called root file system is the file system that can be used as the boot disk. The root file system (hereinafter referred to as the file system without ambiguity) mainly contains five root directories: etc/, bin/, sbin/, lib/, and proc. The busybox tool is used to create the root file system. To maintain consistency between the cross compiler and the file system library, a cross compiler is built using tools such as buildroot, uClibc, and gcc. The file system uses the uClibc library compiled by buildroot. The file system created by using this method can be compressed to about KB.

1.1 Use buildroot to create a cross compiler

1.1.1 introduction to buildroot

As an embedded system, the most scarce resource is the storage space. The library used to streamline embedded systems is one of the most common methods to reduce storage space. GNU Glibc is a very big and complete library, at least for embedded systems, its size is too large. The proposal of uClibc better solves such a problem. UClibc is as compatible with Glibc as much as possible. Most applications can replace glibc with uClibc if it is small or completely unchanged. Instead of Glibc, uClibc can greatly reduce the size of published files without changing the application functions, regardless of whether the application is compiled by static links or by Dynamic Links.

However, using uclibc instead of simply setting one or two parameters is fine. Generally, you need to use a different tool set (GCC/binutils) to compile the source code. Manual construction of such an environment is not necessarily a simple task for most common programmers. Therefore, uclibc developers create a tool set called buildroot.

Buildroot will automatically construct a uclibc-based tool set and uclibc library, and provide a configurable framework and some configuration files for building a basic system. You only need to select the corresponding target software through the configuration menu, and buildroot can start from building the basic toolset and end up building what the target system needs, for example, the common initrd Based on ext2, jffs root file system, and compressed root directory tree in embedded systems are based on uclibc instead of system glibc. Buildroot has little requirements on the host system. Generally, only the host system provides tools that are sufficient to build a toolchain, such as gcc/binutils. After the toolchain is compiled, the compilation process of the source code required by the target system has nothing to do with the development tool set of the host system. Therefore, if different hosts can complete the tool chain through the first step, then the Execution Code of the compiled target system will be almost zero due to system differences. In this way, developers may develop on their preferred Linux distributions without worrying about compatibility issues.

1.1.2 download related resources

Download idea.
Tar-zxvf buildroot-0.9.27.tar.gz
Go to the buildroot directory. By default, buildroot uses wget to download the required resources from the Internet. Here, the DL directory is created as the storage directory of the resources.
CD buildroot
Mkdir DL
Next, you need to download idea. Finally, you need to download ghost.

1.1.3 Configuration

Return to the buildroot directory and run the make menuconfig command to configure buildroot. See:


 

Figure 1-1 buildroot configuration page

Target architecture (ARM) ---> select arm because the CPU of the target board is at91rm9200 and it is an arm 920t series.
Build options --->
() Wget command: because all tools have been downloaded to the DL local directory, you do not need to download resources from the Internet. Clear this item.
[] Tar verbose
($ Build_dir/staging_dir) toolchain and header file location? This option indicates the directory where the cross compiler generated by compilation is located and does not need to be modified.
(1) Number of jobs to run simultaneously.
Toolchain Options --->
--- Kernel Header Options
Kernel Headers (Linux 2.4.27 kernel headers) ---> note that the Kernel header file version is consistent with the kernel version used by the target board. My target board is 2.4.19, which is replaced by a close method 2.4.27. At present, it seems that there is no major problem.
--- UClibc Options
[] Use daily snapshot of uClibc? Whether to use the latest version of uClibc.
[] Enable local/gettext/i18n support?
--- Binutils Options
Bintuils Version (bintuils 2.15.91.0.2) --->
--- Gcc Options
GCC compiler Version (gcc 3.3.4) -->
() Additional gcc options
[*] Build/install c ++ compiler and libstdc ++>
[] Build/install java compiler and libgcj?
--- Cache Options
[*] Enable ccache support? Do not enable the support for ccache. Otherwise, the header file path error may be raised due to the symbolic link when the cross compiler is created.
--- Gdb Options
[] Build gdb degugger for the Target
[] Build gdb server for the Target
--- Common Toolchain Options
[] Enable multilib support?
[] Enable large file (files> 2 GB) support?
[] Use software floating point by default
(-OS-pipe) Target Optimizations
Package Selection for the target ---> This item is not selected. busybox is manually created and is not intended to be automatically built by buildroot.
Target Options --->
[] Cr amfs root filesystem for the target device
[*] Ext2 root filesystem for the t arget device
[] Jffs2 root filesystem for the target device
[] Squashfs root filesystem for the target device
Finally, exit the configuration page and choose Save settings.

1.1.4 Compilation

Run the make command to compile buildroot. There are three prompts during the compilation process. The first choice is to ask about the ARM version, select 920 TB, and then select the inquiry terminal mode. Because 9200 has MMU, select Y when you ask MMU.

The compilation process lasted for more than half an hour in my VMWare virtual machine.

The cross-compiler we need is now under the build_arm/staging_dir directory. The following three directories are most concerned: bin, lib, and include. The bin directory contains all the cross compilers. We are concerned with this group of compilers starting with arm-linux-uclibc. The lib directory is the uclibc compilation result, and the inlcude is the header file of the 2.4.27 kernel.
3.3.4 the cross compiler is complete. There are many tools involved in the preparation of the cross compiler, And the Compilation Time is very long, it is a test of a person's patience. Next, we will use this cross tool chain to compile busybox.

1.2 use busybox to make binary tools

1.2.1 Overview of busybox

Is BusyBox a standard Linux? A single executable Implementation of the tool. BusyBox contains some simple tools, such as cat and echo, and some larger and more complex tools, for example, grep, find, mount, and telnet (but it has fewer options than the traditional version); some refer to BusyBox as the Swiss army knife in Linux tools. This article will explore how BusyBox works and why it is so important for environments with limited memory.

BusyBox was originally written by Bruce Perens for the Debian GNU/Linux installation disk in 1996. The goal is to create a bootable GNU/Linux system on a floppy disk, which can be used as an installation disk and an emergency disk. A floppy disk can save about 1.4-1.7 MB of content, so there is not much space left for the Linux kernel and related user applications.

BusyBox exposes the fact that many standard Linux tools can share many common elements. For example, many file-based tools (such as grep and find) need to search for file code in the directory. When these tools are merged into an executable program, they can share these same elements to produce smaller executable programs. In fact, BusyBox can pack a tool of about 3.5 MB into a size of about KB. This provides more functionality for boot disks and embedded devices using Linux. We can use BusyBox for Linux kernels 2.4 and 2.6.

To make an executable program look like many executable programs, BusyBox develops a rarely used feature for the parameters passed to the main function of C. Recall that the main function in C is defined as follows:

Int main (int argc, char * argv [])

In this definition, argc is the number of parameters passed in (number of parameters), and argv is a string array, representing the parameters passed in from the command line (parameter vector ). The argv index 0 is the name of the program called from the command line. With this feature, we can create a symbolic link to the executable program. When executing this symbolic link, the symbolic link name is argv [0].

Busybox uses symbolic links to make an executable program look like a lot of programs. For each tool in busybox, a symbolic link is created in this way, so that you can use these symbolic links to call busybox. Busybox can then use argv [0] to call internal tools.

1.2.2 download resources

Download unzip-jxvf busybox-1.00.tar.bz2 decompress to get the busybox-1.00 directory. Go to the directory. The next step is to configure busybox and compile and install it.

1.2.3 configuration and installation

Run the make menuconfig command in the busybox-1.00 directory to enter the busybox configuration interface, as shown in:


 

Figure 1-2 busybox configuration page

Because there are many options, only the main options are listed. The default options are used for every improvement:
General configuration --->
Buffer Allocation Policy (allocate on the stack) --->
[*] Show verbose applet usage messages
[*] Support for devfs
[*] Use the devpts filesystem for unix98 ptys
Build options --->
[] Build busybox as a static binary (no share libs) this option is not selected, that is, the dynamic mode is used, which can save hundreds of KB of space.
[*] Do you want to build busybox with a cross compiler?
(/Home/BR/buildroot/build_arm/staging_dir/bin/ARM-Linux-uclibc-) Cross Compiler specifies the cross compiler path, that is, the cross compiler path built using buildroot.
[*] Don't use/usr must be selected. Otherwise, the database under/usr will be overwritten.
(./_ Install) busybox installation prefix installation path, execute make install will generate the _ install directory under the busybox-1.00 directory, contains the busybox compilation result and the related tool symbolic connection.
Archival utilities ---> this tool mainly applies to File compression and decompression. Keep the default options.
Editors ---> remove VI and basically no longer run the VI editor on the embedded target board.
Finding utilities --->
Init Utilities ---> select the init option.
Login/Password Management Utilities ---> select gettty. In fact, you still need mingetty. However, busybox does not support it. It is only manually compiled and added to the file system.
Miscellaneous Utilities --->
Linux Module Utilities ---> it is related to dynamic Module loading. dynamic Module loading brings great convenience to application debugging, so support is selected. Because the kernel version is 2.4, the following options are selected:
[*] Insmod
[*] Support version 2.2.x to 2.4.x Linux kernels
[*] Lsmod
[*] Modprobe
[*] Rmmod
[*] Support tainted module checking with new kernels
Another Bourne-like Shell ---> select ash as the default bash, which saves a lot of space.
Linux System Utilities --->
To support NFS, select the following three items:
[*] Mount
[*] Support mounting NFS File System
[*] Umount
Many loop devices are used.
[*] Support for loop Devices
Debugging options ---> debugging symbols are not required
After the configuration is complete, save the settings and exit the configuration page.

1.2.4 compilation and Installation

Run make to compile busybox. After compilation, run make install. The _ install directory is generated under the busybox-1.00 Directory, which contains the bin and sbin subdirectories. Except for the busybox file, the two subdirectories are symbolic links to the file.
At this step, busybox has been compiled, and the next step is to build the root file system, because the file system is built based on uclibc and I call it ucfs.

1.3 build ucfs

1.3.1 use the loop device to create a file system

Go to the/tmp directory and create the/mnt/ucfs directory as the future Mount location. Run dd If =/dev/Zero of = ucfs BS = 1 k count = 15360 to create a 15 m file. All files are initialized to 0. If you repeatedly add or delete a file system, it will make it difficult to compress unused parts. Initializing unused parts to 0 will compress this part.

Use the losetup command to associate the loop device with the ucfs file: losetup-e NONE/dev/loop0 ucfs. If loop0 is in use, you can use loop1, loop2, and so on.
Create an ext2 File System on the loop device: mke2fs-M 0/dev/loop0 15360. -M 0 does not reserve space for the root user, thus improving the file system utilization.

Note: If the losetup and mke2fs commands cannot be found, add/sbin/N to the front.

Load the file system: Mount-T ext2/dev/loop0/mnt/ucfs.

Next, go to the/mnt/ucfs directory, and you can see that there is a lost + found directory. Add related files and directories to the/mnt/ucfs directory (next, we will explain it) to build the root file system.

1.3.2 install binary tools, libraries, and others

Before uninstalling/dev/loop0, you must first add the Directory and Related Files used to create the root file system in/mnt/ucfs. First copy the binary tool, that is, the busybox tool set. CP-A/home/busybox/busybox-1.00/_ install/*/mnt/ucfs, copy the busybox and related symbols to ucfs, observe/mnt/ucfs, added the bin and sbin directories to delete the linuxrc symbolic connections.
Next, copy the uclibc library in the cross compiler. Enter the lib directory of the Cross-compiler, CD/home/BR/buildroot/build_arm/staging_dir/lib. Then copy the necessary database. CP-A *-*. So/mnt/ucfs/lib, CP-A *. So. [* 0-9]/mnt/ucfs/lib. Return to the/mnt/ucfs/lib directory and delete libstdc ++. Currently, the standard library of C ++ is not used,
Rm-F libstdc ++ *.

Next, use the busybox example to build the/etc directory. Go to the/home/busybox/busybox-1.00/example/bootfloppy directory. Copy the etc directory to/mnt/ucfs, CP-r/etc/mnt/ucfs. You can read the bootfloppy.txt file carefully after creating the root file system, which is of great reference value. Here, you need to modify the default inittab File Created by busybox, VI/mnt/ucfs/etc/inittab, delete the last two lines, and only keep the following two lines:

: Sysinit:/etc/init. d/RCS
: Respawn:-/bin/sh

Finally, you need to create an empty directory,/mnt/ucfs/proc. At this point, the root file system has been created. Finally, exit/mnt/ucfs to uninstall the loop0 device. Run umount/dev/loop0 and losetup-D/dev/loop0 to save the modification to ucfs. This ucfs can be used as the root file system of the system after being compressed by gzip. After gzip-V9 ucfsis compressed, ucfs.gz only contains 407304 bytes, less than 400 Kb!

Finally, let's take a look at the results. Success!


 

Figure 1-3 Self-made file system startup Interface

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.