Linux OS system analysis (7)-build your own Linux release

Source: Internet
Author: User
Tags temporary file storage
Abstract

Linux boot process, including BIOS power-on self-check post, copy MBR information (start bootloader), load kernel, if you are familiar with grub when mounting the root file security system, you will know that there are three grub types at Linux Startup: Root, kernel, and initrd. The three functions are as follows:

1. Specify the directory where the kernel is located
2. Specify the kernel name, mount the root directory, and pass certain parameters to the kernel.
3. initrd is actually a small Linux system. In general, initrd is used to start a very small Linux system to mount a real Linux system.

Today's goal is to build a Linux of its own starting from the kernel.

Environment: ubuntu13.04 gcc4.7.3

Preparations:

Kernel Compilation

Qemu Installation

Create the root system directory 1) Create the INIT program first and create an init. c file. The Code is as follows:
#include<stdio.h>int main(){    printf("Welcome! My student No. is sa*****310.\n");    return 0;}

Static compilation into an executable file. Gcc-static-O init. c2) create a boot root directory and run the Image Terminal: dd If =/dev/Zero of = initrd. img bs = 4096 COUNT = 1024mkfs. ext3 initrd. IMG
Enter y as prompted.
About dd

Dd is a very useful command in Linux/Unix. It is used to copy an object with a specified size block and perform the specified conversion at the same time.
Syntax: dd [Option]
If = input file (or device name ).
Of = output file (or device name ).
IBS = Bytes: the number of bytes read from the buffer zone.

...


3) create a rootfs directory and mount mkdir rootfs
Sudo Mount-o loop initrd. IMG rootfs

4) add some files in rootfs to copy init to initrd4m. IMG's target root directory (because after Linux is started, it will find an application in the root directory to run, and providing init in the root directory is an optional solution)
CP init rootfs/
Prepare the dev directory:
Sudo mkdir rootfs/dev
The console device is enabled during Linux Startup:
Sudo mknod rootfs/dev/console C 5 1
In addition, we need to provide a Linux Root device. We use RAM:
Sudo mknod rootfs/dev/Ram B 1 0
Sudo umount rootfs
Now, the initrd4m. IMG image in the root directory of a simple application is ready.MknodUsed to create characters or block-Related Files

Run qemu-kernel ../linux-3.9.2/ARCH/x86/boot/bzimage-initrd. IMG-APPEND "root =/dev/Ram init =/init"

Integrate busybox

Busybox Introduction

Busybox is a software integrated with more than one hundred common Linux commands and tools. Busybox contains some simple tools, such as LS, cat, and echo. It also contains some larger and more complex tools, such as grep, find, mount, and telnet. Some people call busybox the Swiss army knife in Linux. To put it simply, busybox is like a big toolbox. It integrates many Linux tools and commands and also contains the built-in shell of the Android system.
Busybox combines many common minor UNIX tools into a single executable file. This collection can replace most commonly used tools such as GNU fileutils and shellutils. busybox provides a comprehensive environment that can be applied to any small or embedded system.


Download Source: http://www.busybox.net/

Select stable version 1.20.

Decompress the package and run the following command on the terminal:

Make menuconfig

Select the following options:

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.

If you directly make the file, the following error will be reported: 'rlimit _ fsize 'undeclared

If the answer on the forum does not contain sys/resource. H, add the following in include/libbb. h:

#include <sys/resource.h>

Run the following command:

# Compiling busybox

Make

# Installing busybox
Make install

 

After installation, A _ Install Folder appears in the folder. The compilation is complete.

Next we will integrate the root file system.

Create a new folder, CD the terminal, and copy the previous initrd. IMG.

# Creating folders

Mkdir rootfs

# Attach an image

Sudo Mount-o loop initrd. IMG rootfs/

# Add busybox

CD ../busybox-1.20.2/

Sudo make config_prefix = ../opuntu/rootfs/install

# View the structure in rootfs

CD ../opuntu

Ls rootfs

# Detaching a partition

Sudo umount rootfs/

Pay attention to the directory structure when running the command!

What are the directories in Linux?

File structure in Linux to see how each folder is used
/Bin binary executable command
/Dev special file
/ETC system management and configuration file
Configuration files and scripts started by/etc/rc. d
The base point of the/home user's home directory. For example, if the user's home directory is/home/user, you can use ~ User
/Lib standard programming library, also known as dynamic link shared library, works similar to. DLL files in Windows
/Sbin system management command, which stores the management program used by the system administrator.
/Tmp public temporary file storage point
/Root system administrator's home directory (haha, privileged class)
/The MNT system provides this directory for users to temporarily Mount other file systems.

...


The final initrd. imgfile is the root file system!

# Qemu Test

Qemu-kernel ../linux-3.9.2/ARCH/x86/boot/bzimage-initrd. IMG-APPEND "root =/dev/Ram init =/bin/sh"

After loading, you can run the busybox command in qemu. The effect is as follows:

Integrate grub

About grub

GNU grub ("Grub" for short) is a multi-operating system startup program from the GNU project. Grub is the implementation of multi-boot specifications. It allows you to have multiple operating systems in your computer and select the operating system you want to run when the computer starts. Grub can be used to select different kernels on the operating system partition or to pass startup parameters to these kernels.

First, test grub.

Download the last grub-0.97-i386-pc.tar.gz compiled by grub legacyfrom ftp://alpha.gnu.org/gnu/grub.

Decompress the package and run the CD command.

# Create a floppy image:
Dd If =/dev/Zero of = boot. img bs = 512 COUNT = 2880
# Install grub in Boot. IMG:
Sudo losetup/dev/loop0 boot. img
Sudo dd If =./grub-0.97-i386-pc/boot/GRUB/stage1 of =/dev/loop0 BS = 512 COUNT = 1
Sudo dd If =./grub-0.97-i386-pc/boot/GRUB/stage2 of =/dev/loop0 BS = 512 seek = 1
Sudo losetup-D/dev/loop0

Test whether grub can be accessed in qemu.

Qemu-FDA boot. img

OK.

Next we will integrate grub, kernel, and busybox together, leaving the victory only one step --.

First, you can obtain the name of a library, such as opuntu... (opensuse + UBUNTU), and create a folder named after it.

# Copy boot. IMG to the current directory

Sudo CP ../grub-0.97-i386-pc/boot. IMG ./

# Create a rootfs folder

Mkdir rootfs

# Create a 32 M Disk Image File
Dd If =/dev/Zero of = opuntu. img bs = 4096 COUNT = 8192
# Partitioning a disk image
Fdisk-C 16065-H 255-s 63 opuntu. img

Explanation: Set opuntu. IMG to 255 bid, 16065 bid, 63 slice, and partition the disk.
Here we only split the partition into one and set it as the pilot partition. As follows:

# Formatting partitions
Sudo losetup-O 1048576/dev/loop0 opuntu. img

Sudo mkfs. ext3-M 0/dev/loop0

Explanation: we set the first 2048 sectors (0 ~ 2047) as the Boot Sector, the formatting partition starts from 2048th sectors, so 1048576 = 2048*512

# Copy initrd. IMG and bzimage. IMG to rootfs.

Sudo Mount/dev/loop0 rootfs/
Sudo CP ../linux-3.9.2/ARCH/x86/boot/bzimage./rootfs/
Sudo CP ../rootfs/initrd. IMG./rootfs/

# Add grub

Sudo mkdir rootfs/boot
Sudo mkdir rootfs/boot/GRUB

Sudo CP ../grub-0.97-i386-pc/boot/GRUB/*./rootfs/boot/GRUB
Sudo VI./rootfs/boot/GRUB/menu. lst

Content:

default 0timeout 30title linux on 32M.imgroot (hd0,0)kernel (hd0,0)/bzImage root=/dev/ram init=/bin/ashinitrd (hd0,0)/initrd.img

# Detaching a disk image

Sudo umount rootfs
Sudo losetup-D/dev/loop0

# Use grub to start a floppy disk and add the grub function to the hard disk image
Qemu-boot a-FDA boot. IMG-hda opuntu. img

Perform the two steps in the figure (note the space ):

After running successfully, opuntu. IMG is our final result. It integrates busybox, grub, and Linux kernel3.92!

Qemu-hda opuntu. img

Run it with qemu:

Refer:

Chapter 4: switch-on process and loader

Create a disk image file with GRUB boot Linux available-http://blog.sina.com.cn/s/blog_70dd169101013gcw.html

Linux Startup Process and the configuration file and script used for startup-http://guodayong.blog.51cto.com/263451/1168731

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.