First attempt to reduce embedded application Linux

Source: Internet
Author: User
Linux general technology-Linux programming and kernel information, the first attempt to cut embedded application Linux, is described below. Some time ago, due to the needs of embedded application development, Linux was greatly reduced. Because it is the core part of Linux Startup for the first time, it is basically a patchwork and arrangement of various cutting solutions on the Internet, including the part that I understand. In general, the effect is not ideal, and there is still a long way to go. Here we will roughly talk about the prototype of the current Linux reduction solution.

1. kernel reduction

The reduction of the Linux kernel is mainly based on the actual needs of the re-compilation, removed most of the features that are not available, as well as the actual hardware environment outside the device driver. This process has no technical content, so I will not elaborate on it.

2. initrd Transformation

During Normal Linux Startup, initrd only acts as a temporary rootfs and is uninstalled or transferred after loading the actual rootfs. For the sake of streamlining, I incorporated the original rootfs into initrd so that it can act as rootfs directly to save the startup time and reduce the volume. (This transformation scheme is relatively simple and sufficient to meet actual needs)

The transformation process is mainly based on the initrd image automatically generated by the original make install, the following "operation" is performed ":

(1) Replace the initialization script in the original initrd with the busrc of BusyBox, and merge all the initialization tasks used in the original linuxrc script into the etc/inittab to save start time.

(2) to act as rootfs, you also need to add some folders and files required for Linux Startup and running for initrd (mainly copied from the original rootfs ):

/Dev
Tty3-tty10 terminal session device (used for Console Logon)
TtyS0 and ttyS1 serial port terminal devices (used for serial port logon)
Ptmx and pts virtual terminal devices (for remote logon)
/Etc
Fstab configuration file for device loading (detailed later)
Gettydefs only retains one Virtual Console item
The group only retains the corresponding items of the root user.
Hostname contains the Host Name of the Local Machine
Inittab initialization script (detailed later)
Issue contains the full logon welcome text from the Local Machine
Passwd only retains the corresponding root item
Shadow only retains the corresponding items of the root user.
Termcap only retains configuration items for local console and remote logon.
/Etc/init. d
The secondary initialization script of the rcS (detailed later)
/Lib
Library files required for libcrypt. so.1 login authentication (login)
/Mnt
/Proc
/Sys
/Tmp
/Usr
/Var

(3) modify the etc/fstab storage device to load the configuration file, which contains only the following four lines:

Sysfs/sys sysfs defaults 0 0
Proc/proc defaults 0 0
Tmpfs/dev/shm tmpfs size = 512 m 0 0
Devpts/dev/pts devpts defaults 0 0

Due to the large memory size in the actual hardware environment, the upper limit of memory allocated to the temporary file system is relatively abundant.

(4) Create the etc/inittab initialization script. The init module Adopts the simplified version of BusyBox, And the inittab file format is different from that of the normal version. Therefore, it is specially rewritten according to the BusyBox format. The file is large, and only the key parts are listed below:

: Sysinit:/bin/mount-a load all storage devices configured in etc/fstab
: Sysinit:/bin/mkdir/dev/shm/var divide the parts used as var in tmpfs
: Sysinit:/bin/mkdir/dev/shm/tmp divide the parts used as tmp in tmpfs
: Sysinit:/bin/chmod 1777/dev/shm/var modify var to the appropriate Permissions
: Sysinit:/bin/chmod 1777/dev/shm/tmp modify tmp as appropriate Permissions
: Sysinit:/bin/mount -- bind/dev/shm/var bind/var to tmpfs
: Sysinit:/bin/mount -- bind/dev/shm/tmp bind/tmp to tmpfs

: Sysinit:/bin/hostname-F/etc/hostname read from etc/hostname and set the Host Name
: Sysinit:/sbin/ifconfig lo 127.0.0.1 up configure IP loopback interface
: Sysinit:/sbin/telnetd start Telnet Daemon
: Sysinit:/etc/init. d/rcS call the secondary initialization script (completing initialization tasks that cannot be performed in inittab)

: Sysinit:/bin/mkdir/var/log create log folder
: Sysinit:/bin/touch/var/log/messages create a log file
: Respawn:/sbin/syslogd-n-m 30-C load the system log daemon in cyclic buffer Mode
: Respawn:/sbin/klogd-n

: Askfirst:/bin/login create a logon process on the console
Tty2: askfirst:/bin/login create additional login processes on the tty2-tty6
Tty3: askfirst:/bin/login
...
TtyS0: respawn:/sbin/getty-L ttyS0 115200 vt100 create a logon process on the serial port COM1
Tty10: respawn:/sbin/logread-f print all logs to tty10

(5) Create the etc/init. d/rcS subinitialization script, which includes the following content:

/Bin/mount-o remount, rw/reload rootfs as a readable and writable Mode

Note: Due to the convenience of the test, the generated initrd adopts the Ext2 file system. In practical applications, consider replacing CRAMFS with a compressed ROM image file system to further reduce the size of the image file and improve reading efficiency.

3. Modify Boot Loader

Because this cutting scheme only uses the kernel image and initrd, and maintains high compatibility with the standard Boot mode, it is suitable for almost any Boot Loader. Because kernel and initrd images need to be replaced frequently in the experiment, GRUB, which is easy to control, is used to improve the experiment efficiency. If the Final Solution uses Flash as the image carrier, LILO is recommended to improve compatibility and save space.
To be consistent with the use of initrd as rootfs in this solution, you must modify the Boot parameter in the Boot Loader configuration file and specify "root =/dev/ram0" without additional parameters.

After the preceding reduction, the Linux Startup time has been shortened to less than 10 s. The size of the kernel + initrd image is less than 3 MB, basically reaching the expected goal. In the future, we will further analyze and optimize the various stages of startup, in order to further shorten the startup time.
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.