Use Busybox for a small Linux operating system

Source: Internet
Author: User
Article Title: Use Busybox for a small Linux operating system. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

1. Basic Knowledge

An operating system can be simply abstracted as a boot program Kernel File System.

Vmlinuz is an image file of the Linux kernel. It can be loaded by the boot program to start the Linux system.

The full name of initrd is boot loader initialized RAM disk, which is the root file system image file used during system startup. This file system contains several driver modules, it is used to load the actual root file system, such as an IDE or scsi drive module. After the kernel is loaded into this module, it is used to drive the hard disk and mount the hard disk to a subdirectory of ramdisk, then, use the cmdt_root command to convert the hard disk file system into the root file system and run the init process. At this time, the init ram disk will be umount, and it will end up.

From this we can see that we can use vmlinuz initrd. img to create a file system with the same memory-resident mini Linux.

Now the topic is started:

2. Compile the kernel

Cd/usr/src /...... -- Enter the kernel source code directory. If not, go to the official website and click

Make menuconfig -- add RAM disk support and initial RAM disk to configure compilation options.

(Initrd) support (in block device ). In addition, it is best to compile the ext3 file system into the kernel instead of modules. Save. Config (default)

Make bzImage -- compile

In many kernel compilation references

Make modules

Make modules_install

However, we didn't add the modules support here, so we don't need it. The generated bzImage file is in usr/src/linux/arch /...... In/boot, It is omitted based on your machine architecture. A prompt will be prompted during the make process. For example, if my file is x86_64, note that this file is very important. In fact, it is our final vmlinuz.

3. busybox

Busybox is a software that integrates more than one hundred common linux commands and tools. It even integrates an http server and a telnet server, all these functions are only about 1 MB in the partition area. The linux commands we usually use are like force-splitting electronic components, while busybox is like an integrated circuit, which compresses the integration of common tools and commands into an executable file, functions remain unchanged, but the size is many times smaller. In Embedded linux applications, busybox has a wide range of applications. In addition, busybox appears in most installation programs of linux releases, when linux is installed, ctrl alt F2 can get a console, and all commands in this console are links to busybox. In our mini Linux, what we need is the busybox command and tool.

Download http://busybox.net/downloads/

Many later versions have compilation errors during use, so a original version busybox-1.00 is used

# Cp busybox-1.00.tar.gz/tmp/bunny

# Cd/tmp/bunny

# Tar xvfz busybox-1.00.tar.gz

# Cd busybox-1.00

# Make menuconfig -- compile the configuration

The following are functional options that need to be compiled into busybox. Other options can be customized as needed.

General Configuration options

Show verbose applet usage messages

Runtime SUID/SGID configuration via/etc/busybox. conf

Build Options

Build BusyBox as a static binary (no shared libs)

This option must be selected, so that busybox can be compiled into a static link executable file, 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.

Installation Options

Don't use/usr

This option must also be selected. Otherwise, after make install, busybox will be installed in the/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 links to it.

Other options are some basic linux Command Options. You can compile the commands you need. Generally, you can use the default command.

After configuration, exit and save. Config.

Make

Make install

After compilation, generate the subdirectory _ install in the busybox directory. The content is as follows:

Bin

Linuxrc-> bin/busybox

Sbin

The executable file busybox is in the bin directory, and others point to its symbolic link.

4. Create your own root fs

1) directory structure

Mkdir/tmp/myOS/rootfs

Cd/tmp/myOS/rootfs

Mkdir etc usr var tmp proc home root dev

Among them, etc, proc and dev must be created, and bin and sbin do not need to be created, because busybox already has it.

Others can be symbolic.

Copy busybox

# Cp-R/tmp/bunny/busybox-1.00/_ install/*/tmp/myOS/rootfs/

2) Device Files

The method is as follows:

# Cp-R/dev/console/tmp/myOS/rootfs/dev/

# Cp-R/dev/null/tmp/myOS/rootfs/dev/

# Cp-R/dev/zero/tmp/myOS/rootfs/dev/

All you think you need is cp.

Some references indicate that fd0, hda, ram, ram1, tty1, loop1, fb0, fb, tty, and so on are essential, but some files do not

3) create a configuration file under the etc directory

I copied the built-in example of busybox directly.

Cp-R tmp/bunny/busybox-1.00/examples/bootfloppy/etc/*/tmp/myOS/rootfs/etc

4) Create an initrd. img Image File

Cd/tmp/myOS/

Dd if =/dev/zero of =/tmp/disk bs = 1 M count = 32 -- initialize 32 M memory space

Mkfs. ext3-m0/tmp/disk -- format to ext3

Mkdir/mnt/ram

Mount-o loop/tmp/disk/mnt/ram -- mount to/mnt/ram

Cp-R rootfs/*/mnt/ram -- write rootfs into memory

Umount/mnt/ram

Dd if =/tmp/disk of =/tmp/myOS/initrd. img -- retrieve the content in the memory as an image

OK, this initrd. img is our rootfs.

Some documents use ram0, that is, change/tmp/disk in the above process to/dev/ram0. Although there is no problem in the production process, it has a size limit.

5. Integration and startup

1) kernel

Cp/usr/src/linux/arch/x86_64/bzImage/boot/vmlinuz

2) rootfs is generally named as initrd. img.

Cp/tmp/myOS/initrd. img/boot

3) With the above two files, you can use the network dhcp, tftp server to start, and add the startup script to the tftp server as follows:

DEFAULT linux

PROMPT 0

LABEL linux

KERNEL vmlinuz

Append initrd = initrd. img devfs = nomount ramdisk_size = 52000

Problem: theoretically, this small Linux should also be able to boot from grub.

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.