Create the most streamlined and distinctive linux

Source: Internet
Author: User

 

One of the highlights of linux is open-source. Compared with Windows, we can use more imagination on linux to build a linux system with its own characteristics, first, we start with creating the most basic micro-linux system. Through the experience throughout the creation process, you can customize your complete linux system, however, the linux system that you use gives off your breath and makes it more active !!

Next, we will directly perform the operations. The principles will be described in each step:

I. preparation stage:

Hardware: A host machine, an empty disk (or a disk that no longer uses data, 1 GB is enough. If you want to continuously improve your own linux, select a large disc. In this article, we will regard it as sdb and adjust it based on our actual situation ).

1. Partition and format the disk we have prepared

Partition the new disk we have prepared (if the disk is not used for data, use dd if =/dev/zero of =/dev/sdb bc = 512 count = 1 to overwrite all data in the boot area of the disk, in this way, the disk is programmed with an empty disk ).

Fdisk/dev/sdb divides it into two zones: sdb1 100 M and sdb2 500 M. The data types are all linux. Save and exit

Formatting partitions: according to the file system type of the host, I Format ext3, so I added a-j During formatting. If I was ext2, I would not need to add-j.

Mke2fs-j/dev/sdb1 mke2fs-j/dev/sdb2 format them to ext3 format

2. Attach a disk (the mount point is selected by yourself, but the mount point for mounting sdb1 must be named boot)

Create a mount point: mkdir-pv/mnt/{boot, myroot}

Mount/dev/sdb1/mnt/boot mount sdb1 to/mnt/boot

Mount/dev/sdb2/mnt/myroot mount sdb2 to/mnt/myroot

(Note: When we mount these two partitions, the error message indicating that the partition does not exist may be displayed, mainly because the system failed to automatically obtain the information of the created partition, that is, the/proc/partitions file does not contain information about the new partition. We only need to enter the command: partproble/dev/sdb to re-read the partition information, the new partition information will be written to the/proc/parttions file, so that it is OK)

Ii. Creation stage:

1. Prepare the linux Kernel)

View the contents of the/boot directory in our host, we will find files with version numbers like vmlinuz-2.6.18-164.el5 vmlinuz-2.6.28.10, This is the kernel file of the system, we choose a kernel file, copy it to the/boot directory of our small linux, so that our linux has a heart. Command: cp/boot/vmlinuz-2.6.18-164.e15/mnt/boot/vmlinuxz rename the kernel to vlinuxz.

(Note: Renaming the kernel not only facilitates the subsequent operations, but also avoids the kernel name and grub. the kernel name in the conf file does not match and causes the kernel panic (kernel panic) problem. As a result, our small linux cannot be started, in addition, I have encountered a problem where the kernel name in the virtual machine is inconsistent with the kernel name written by the Tab key. Why! So we try to write the name of the linux Kernel File as short as possible)

2. Prepare the initrd file of our linux malicious spoofing file.

For more information about the role of the initrd file, I have explained it in detail in an article titled "Linux boot process brief". I suggest you check it out, it is very helpful for our small linux management and maintenance.

Also view the contents in the/boot directory of the host machine, you will find that there is a file similar to the initrd-2.6.18-164.el5.img initrd-2.6.28.10.img such as beginning with initrd is a version number, end with img, then this is the initrd file, each version of the kernel file has an initrd file corresponding to the version number, copy the initrd file corresponding to the kernel version we just selected to a new directory for transformation (Here we create a directory named test in the/tmp directory)

Cp/boot/initrd-2.6.18-164.el5.img/tmp/test/

We can check the file type and find that it is a file packaged with cpio compressed by gzip. We will expand it in the current directory.

Initrd-2.6.18-164.el5.img | cpio-id

After expansion, we will find an init script file, which is opened with vim, starting with mkrootdev-t ext3-o defaults and ro

Change the following content to sda2 and save and exit. (sda2 is the partition where the root file system is located when the linux system runs independently. Here we will tell the init process, the real root file system is on sda2, so that the kernel can load the root file system. Note that there is a space between ro and sda2)

After the modification is complete, I asked to encapsulate the recovery (it is best to delete the initrd-2.6.18-164.el5.img files in this directory before encapsulating the recovery, using the command: rm-rf initrd-2.6.18-164.el5.img), using the command:

Find. | cpio-H newc-o | gzip-9>/mnt/boot/initrd.gz

(Note: find. indicates all contents in the current directory.

Cpio-H news-o packages all contents in the directory cpio

After gzip-9 is packaged, gzip is compressed because the file must be in a compressed format.

>/Mnt/boot/initrd.gz put the compressed package in the/mnt/bootdirectory and name it initrd.gz. Rename the package for ease of use)

3. Install the grub boot program on our small linux disk. (The Role of grub is introduced in the article I just mentioned. I will not describe it here)

Installation command: grub-install -- root-directory =/mnt/dev/sda

^ This is the parent directory of the boot directory.

Go to the/mnt/boot directory to check whether there is a new directory, grub. If not, the installation fails. reinstall the directory. If yes, go directly to the directory.

Use vim to create the file grub. conf and write the following content (Note: # post-comments for me, do not write)

Default = 0

Timeout = 10 # Here we set the menu selection time for system startup, and we set it to 10 seconds.

Title my linux # title is the name displayed in our small linux in the boot operating system selection menu. You can enter any name

Root (hd0, 0) # Tell grub the kernel, root file system, and disk of the initrd file of the small linux System

Kernel/vmlinuz ro root =/dev/sda2 rhgb quiet # Tell grub kernel name and partition where the root file system is located

Initrd/initrd.gz # Tell grub initrd file name

Save and exit.

4. Create a simple root file system for our small linux System

Go to the/etc/myroot directory and use the command to create the following directory:

Mkdir-p {boot, proc, sys, dev, home, root, etc/{rc. d, sysconfig, init. d}, bin, sbin, lib, usr/{bin, sbin, lib, include}, var/{log, run}, tmp, mnt, opt, media}

To port basic commands, we will use the library files related to commands on the host machine to port the required commands for our small linux (the porting methods for each command are the same. Here we take the command bash as an example)

Use which bash to view the location of the bash command and copy it to the corresponding location of our small linux

Cp/bin/bash/mnt/myroot/bin/bash

Since the system does not read the bash command itself but sh at startup, we create a sh link for the bash command (only this command needs to be linked)

Go to the/mnt/myroot/bin directory.

Create the link ln-sv bash sh and view the directory to see sh.

View and copy the library files on which the bash command depends (no library files are available, and the command is just a decoration)

Run the command ldd/bin/bash to display all library files on which bash depends, as shown in

Linux-gate.so.1 => (0x0061c000)

Libtermcap. so.2 =>/lib/libtermcap. so.2 (0x00c8f000)

Libdl. so.2 =>/lib/libdl. so.2 (0x00c60000)

Libc. so.6 =>/lib/libc. so.6 (0x00b18000)

/Lib/ld-linux.so.2 (0x00af5000)

The first library file does not need to be copied. The following library files must be copied to the corresponding directory of small linux. For example, the second library file is copied to cp/lib/libtermcap. so.2/mnt/myroot/lib/libtermcap. so.2

The porting process of each command is the same, and the init command is also required. You can select other commands as needed.

4. Create/etc/inittab and/etc/rc for linux. d/rc. sysinit file (the roles of these two files are described in the article I just mentioned. I will not describe them here)

Create an inittab file in the/etc directory. The content is as follows: (the same as # is followed by the annotation content)

Id: 3: initdefault: # Set the default startup level to 3.

Si: sysinit:/etc/rc. d/rc. sysinit # Run the/etc/rc. d/rc. sysinit script on the system.

Save and exit

Create the script rc. sysinit in the/etc/rc. d directory. The content is as follows:

Bin/bash # Run the bash command

Save and exit, and grant the script execution permission chmod + x rc. sysinit

So far, the most streamlined linux has been built by us. We need to put the disks containing the fruits of our work on other bare metal machines and run them on them. Remember, you cannot eat hot tofu in a hurry, before removing the disk, you must give the host machine enough time to detach the cached data from our small linux disk. Otherwise, the consequences can be imagined!

This is the most basic linux operating system, so we have a lot of space to constantly improve it. I hope you will step by step let our little linux grow up "!

This article is from the "redhat" blog

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.