Create a linux root file system

Source: Internet
Author: User
1. root file system

A file system is a directory structure that includes a disk (including a CD, floppy disk, flash disk, and other storage devices) or a partition. An applicable disk device can contain one or more file systems; to access a file system, you must first mount the file system. To mount the file system, you must specify a mount point.

Note: For our application development, when purchasing the Development Board, the manufacturer will provide the ready-made root file system and bootloader. If necessary, we can change the commands without creating a new root file system from the beginning. However, the creation process can give us a better understanding of the Linux File System.

2. Main root file system

* In Linux, rootfs is essential. Ramdisk and direct mounting of the root file system on HD (hard disk) are mainly implemented on the PC. in embedded systems, it is generally started from flash instead of HD, the simplest method is to load rootfs to ram's ramdisk. The more complex method is to directly read cramfs from flash, and more complicated is to partition on flash and build file systems such as jffs2.
* Ramdisk compresses the created rootfs and writes it to flash. during startup, the bootloader loads it to ram, decompress it, And then mounts it /. This method is simple, but the file system in Ram is not compressed, so it needs to occupy a lot of rare resources in embedded systems Ram.
Ramdisk uses memory space to simulate hard disk partitions. ramdisk usually uses the compression of the disk file system and stores it in flash. during system initialization, It is decompressed to SDRAM and mounted to the root file system, in Linux, there are two types of ramdisk. One is to format and load ramdisk, which is supported in Linux kernel 2.0/2.2. The disadvantage is that the size is fixed; the other is supported by the 2.4 kernel. It is implemented through ramfs and cannot be formatted, but it is easy to use. Its size increases or decreases as needed, ramdisk is a common ramdisk Technology in Linux.
* Initrd is the ramdisk format. Before kernel 2.4 was image-initrd, kernel 2.5 introduced cpio-initrd, which greatly simplified the Linux Startup Process and attached the basic philosophy of Linux: keep it simple, stupid (kiss ). however, cpio-initrd is not widely tested as a new format, and image-initrd is mainly used in embedded Linux.
* Cramfs is a simple file system written by Linus. It can be run directly from flash without being loaded to ram, thus saving Ram. However, cramfs is read-only and inconvenient for directories (such as/etc,/var,/tmp) that need to be modified during runtime. Therefore, these directories are generally made into writable FS such as ramfs.
* Squashfs is an enhancement to cramfs. It breaks through some cramfs restrictions and also has advantages in flash and RAM usage. However, according to developers, the performance may be inferior to that of cramfs. This is also a new method. More tests are required before embedded systems are used.

3. ramdisk Creation

Ramdisk is created as follows:

(1) generate a file that can be virtualized into a block device in the Linux operating system of the PC, assuming the file name is init. IMG.

$ Dd If =/dev/Zero of = init. img bs = 4096 COUNT = 1024

BS * count indicates the block device size (in bytes). After the init. imgfile is generated, the file must be formatted.

$ Mke2fs-M0-F init. img

(2) create a folder Ram and mount init. IMG to the ram directory.

$ Mkdir Ram

$ Mount init. img ram/-o loop

In this case, reading and writing the ram directory is equivalent to reading and writing the init. imgfile. You can write the files required by the root file system to the ram directory. After writing a file to the ram directory, you must use the umount Ram command to uninstall init. IMG to save the written file to init. IMG.

(3) umount Ram is required

4. Create cramfs

Cramfs (compressed ROM file system) is a file system developed by Linux Torvalds during transmeta. It is a new type of read-only file system designed for Linux kernel Versions later than 2.4. It adopts zlib compression, and the compression ratio can generally reach, but it can still perform efficient random reading, in Linux, directories that do not need to be modified frequently are compressed and stored, and the compressed files are unzipped during system boot. Cramfs does not affect the speed of reading files, and is a highly compressed file system. Therefore, it is widely used in embedded systems.

In an embedded environment, both memory and external storage resources need to be saved. If you use the ramdisk method to use the file system, after the system runs, you must first extract the image file on flash to the memory and construct a ramdisk environment to start running the program. But it also has critical weaknesses. Under normal circumstances, the same Code not only occupies space in Flash (it exists in the form of compression ), it also occupies more space in the memory (which exists in the form of decompression), which violates the requirement of saving resources as much as possible in the embedded environment.

Using the cramfs file system is a way to solve this problem. Cramfs is a compressed file system that does not need to decompress all the content in the file system to the memory at a time, but only when the system needs to access data at a certain location, calculate the location of the data in cramfs, decompress it To the memory in real time, and then obtain the data to be read in the file system through access to the memory. The data storage location in the cramfs after decompression and decompression is maintained by the cramfs file system. users do not need to know the specific implementation process. Therefore, this method enhances transparency, it is convenient for developers and saves storage space.

However, cramfs also has its disadvantages: latency, less than 16 MB, write is not supported, and only the page_create_size = 4096 kernel is supported.

Command: (the file system is stored in the root folder)

# Mkcramfs root cramfs. img

5. create other root file systems

Create a yaffs File System: mkyaffsimage myroots. img

Create squashfs: mksquashfs $ (fs_dir) $ (fs_name)-noappend-be-lzma-no-fragments-Noi

6. Compile and port busybox (create and transplant the command in the root file system) to create the empty root directory folder of the target board and the folder in the root directory:

[Root @ 190 friendly-arm] # mkdir myroots

[Root @ 190 friendly-arm] # pwd

/Friendly-arm/myroots

[Root @ 190 friendly-arm] # cd myroots

[Root @ 190 myroots] # mkdir bin sbin USR lib Dev MNT opt root etc home proc TMP VaR

[Root @ 190 myroots] # mkdir etc/init. d

Go to the etc/init. d directory, create a script file, and name it "RCS". Open it with gedit and add the following content:

#! /Bin/sh

Path =/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:

Runlevel = s

Prevlevel = N

Umask 022

Export path runlevel prevlevel

#

# Trap CTRL-C & C only in this shell so we can interrupt subprocesses.

#

Trap ":" int quit tstp

Create a node in Dev

If "Linux 2.6.xx" kernel is used, you should create "console" and "null" nodes in advance ". Otherwise, the following error is prompted during system startup:

Warning: unable to open an initial console.
Kernel panic - not syncing: Attempted to kill init!

The command for creating a node is as follows:

# mknod dev/console c 5 1
# mknod dev/null c 1 3
Port busybox:

Go to the directory where the compressed files are stored and decompress the package. Enter the decompressed busybox directory folder and configure busybox.

[Root @ 190 busybox-1.2.0] # Make menuconfig

After execution, the configuration page shown in is displayed:

The configuration options of each version are organized differently. In any case, pay attention to the following options:

1) Support for devfs

2) Build busybox as a static binary (no shared libs) // compile busybox into a static Link

3) Do you want to build busybox with a cross compile?

(/Usr/local/ARM/3.3.2/bin/ARM-Linux-) cross compile prefix // specify the Cross Compiler

4) Init

5) support reading an inittab file // supports init to read the/etc/inittab configuration file

6) (x) ASH: Select ash // The created RCS script for execution.

7) ash

8) Select the CP cat ls mkdir MV // select the executable command tool. You can choose the tool as needed.

9) Mount

10) umount

11) Support loopback mounts

12) Support for the old/etc/mtab File

13) insmod

14) support version 2.2.x to 2.4.x linux kernels

15) support version 2.6.x linux kernels

16) vi

17) Don't use user

The above content must be selected, and others can be selected by default. If you want to support other functions, such as network support, you can select as needed. If the English is not very bad, there is no problem.

After configuration, save and exit. Then compile and install it in the root file system directory just created:

[Root @ 190 busybox-1.2.0] Make target_arch = arm cross = arm-Linux-\ prefix =/friendly-arm/myroots/all install

After installation, copy the binary file to the corresponding directory of the root file system:

  • Copy some administrator control programs to the/sbin directory. The most important thing is to include an init command;
  • Copy the library required when the application program is running to/lib, library files can be copied from the PC on the Cross-compiler tool installation directory, such as libc-2.2.2.so, libcryt-2.2.2.so, libm-2.2.2.so, libutil-2.2.2.so, etc, create shortcuts for some corresponding libraries and provide some applications for calling by shortcut name. It is worth noting that the C library uses the glibc version of the C library, and glibc is located in/lib/libc. so.6

Here (http://blog.chinaunix.net/u1/34474/showart_485837.html) there is a good written blog can learn from.

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.