Customized Linux System
I. Preface
Since the birth of Linux operating system 1991.10.5, its open source and freedom have been favored by many technical experts. Every Linux enthusiast has contributed his/her own efforts, both Linux kernel and open-source software provide us with a good learning and research environment. As a Linuxer, I would like to thank our predecessors for providing us with a free space so that we can study Linux while learning.
This document describes how to crop an existing Linux system to create a Linux small system that can load Nic drivers, configure IP addresses, and implement network functions.
Ii. Principles
Start Process Introduction
Before creating a linux system, we need to learn about the linux Startup Process:
1. First, linux must pass POST self-check to check whether the hardware device is faulty.
2. If you have multiple boot disks, You need to select boot disks in BIOS.
3. Start the bootloader boot program in MBR.
4. Load the Kernel File
5. Execute the parent process and ancestor init of all processes
6. Print the welcome page
In the linux Startup Process, when loading the kernel file, you must use the following two files:
1) initrd is a disk device simulated by memory on CentOS5.
2) initramfs is a file system simulated by memory on CentOS6.
In the process of departure, what operations is init mainly used?
Init calls the/etc/inittab configuration file and then executes the system initialization script of/etc/rc. d/rc. sysinit.
Inspiration
On the linux Print welcome page, it indicates that the system has been started successfully. If we want to create a linux small system, we only need to load all the files used in the boot process together to light up our own system, while linux is a modular operating system, A good multi-functional component is implemented through modular tools and supports dynamic loading and unloading. To implement a function, we only need to load the corresponding module, in this way, our linux operating system is greatly reduced.
3. Procedure
1. Target disk partition
Mount a new disk on the host machine and name it soft-linux. This disk is the second disk on the host machine, so here it is/dev/sdb, when the disk is mounted to the target host, the name of the target host must be/dev/sda. First, we need to divide the target disk into two zones and format them. The first partition is 500 M, which is used to install the boot program; the second partition is 10G, which is used to install the root file system. Then mount/dev/sdb1 to/mnt/boot and mount/dev/sdb2 to/mnt/sysroot.
[Root @ changsheng ~] # Mount/dev/sdb1/mnt/boot
Mount: mount point/mnt/boot does not exist
[Root @ changsheng ~] # Mkdir-p/mnt/boot/mnt/sysroot
[Root @ changsheng ~] # Mount/dev/sdb1/mnt/boot
[Root @ changsheng ~] # Mount/dev/sdb2/mnt/sysroot/
[Root @ changsheng ~] #
2. Install grub to the target disk
Boot is required when a system can be started. Therefore, we must first install a grub boot program on our new disk. There are two main commands for installing the grub boot program: grub-install, the other is setup. You 'd better use grub-install to install it. Because:
① Grub-install will install the grub pilot second-stage File
② Setup will not install the second-stage boot program, which is to install boot information to MBR
The second thing to note is that the path followed by -- root-directory = should be the location of the boot directory, rather than/mnt/boot, because the boot directory is under mnt; the target disk is/dev/sdb.
[Root @ changsheng ~] # Grub-install -- root-directory =/mnt/dev/sdb
Probing devices to guess BIOS drives. This may take a long time.
Installation finished. No error reported.
This is the contents of the device map/mnt/boot/grub/device. map.
Check if this is correct or not. If any of the lines is incorrect,
Fix it and re-run the script 'grub-install '.
(Fd0)/dev/fd0
(Hd0)/dev/sda
(Hd1)/dev/sdb
[Root @ changsheng ~] # Cd/mnt/boot/
[Root @ changsheng boot] # ls
Grub lost + found
[Root @ changsheng boot] # cd grub/
[Root @ changsheng grub] # ls
Device. map e2fs_stage%5 fat_stage%5 ffs_stage%5 iso9660_stage%5 jfs_stage%5 minix_stage%5 %stage1 stage2 %vstafs_stage%5 xfs_stage%5
[Root @ changsheng grub] #
After grub is installed, go to the grub directory and you will find that there is no grub. conf configuration file, which will lead to imperfect boot program, so we need to manually write a configuration file in it, but we need to know the kernel version, after the kernel version is transplanted, let's go back and add this step.
3. Copy the Kernel File and initrd File
Init is a program used in the system to generate all other processes. It exists as a daemon. The process number is 1, and init is the parent process of all processes. It cannot be transplanted. It calls the/etc/inittab configuration file and then executes the system initialization script of/etc/rc. d/rc. sysinit.
Copy the Kernel File and initrd file to the boot directory under/dev/sdb.
[Root @ changsheng grub] # cp/boot/vmlinuz-2.6.32-358.el6.x86_64/mnt/boot/vmlinuz-soft
[Root @ changsheng grub] # cp/boot/initramfs-2.6.32-358.el6.x86_64.img/mnt/boot/initramfs-soft.img
[Root @ changsheng grub] #
4. Create the root file system of the target host
① Use the command line to expand and create a file system
[Root @ changsheng sysroot] # mkdir-pv/mnt/sysroot/{etc/rc. d, usr, var, proc, sys, dev, lib, lib64, bin, sbin, boot, srv, mnt, media, home, root}
Mkdir: created directory '/mnt/sysroot/etc'
Mkdir: created directory '/mnt/sysroot/etc/rc. d'
Mkdir: created directory '/mnt/sysroot/usr'
Mkdir: created directory '/mnt/sysroot/var'
Mkdir: created directory '/mnt/sysroot/proc'
Mkdir: created directory '/mnt/sysroot/sys'
Mkdir: created directory '/mnt/sysroot/dev'
Mkdir: created directory '/mnt/sysroot/lib'
Mkdir: created directory '/mnt/sysroot/lib64'
Mkdir: created directory '/mnt/sysroot/bin'
Mkdir: created directory '/mnt/sysroot/sbin'
Mkdir: created directory '/mnt/sysroot/boot'
Mkdir: created directory '/mnt/sysroot/srv'
Mkdir: created directory '/mnt/sysroot/mnt'
Mkdir: created directory '/mnt/sysroot/media'
Mkdir: created directory '/mnt/sysroot/home'
Mkdir: created directory '/mnt/sysroot/root'
[Root @ changsheng sysroot] # ls
Bin boot dev etc home lib lib64 lost + found media mnt proc root sbin srv sys usr var
[Root @ changsheng sysroot] #
② Port bash commands and their library files to the root file system
[Root @ changsheng mnt] # sh ~ /Scripts/cporder. sh
Enter a command: bash
Enter a command: shutdown
Enter a command: reboot
Enter a command: vim
Enter a command: touch
Enter a command: mkdir
Enter a command: rm
Enter a command: ls
Enter a command: cat
Enter a command: less
Enter a command: ifconfig
Enter a command: ip
Enter a command: route
Enter a command: quit
Quit
[Root @ changsheng mnt] # sync
[Root @ changsheng mnt] # sync
[Root @ changsheng mnt] # ls
Boot sysroot
[Root @ changsheng mnt] # cd sysroot/
[Root @ changsheng sysroot] # ls
Bin lib64 sbin usr
[Root @ changsheng sysroot] # cd bin/
[Root @ changsheng bin] # ls
Bash cat ls mkdir rm touch
[Root @ changsheng bin] # ln-sv bash sh
'Sh'-> 'bash'
[Root @ changsheng bin] # sync
[Root @ changsheng bin] #
Attachment: command port script
#! /Bin/bash
#
Target =/mnt/sysroot
ClearCmd (){
If which $ cmd &>/dev/null; then
Export Path = 'which -- skip-alias $ cmd'
Else
Echo "No such command"
Return 5
Fi
}
Copying copy (){
Export dir = 'dirname $1'
[-D $ {target }$ {export dir}] | mkdir-p $ {target }$ {export dir}
[-F $ {target }$ {1}] | cp $1 $ {target }$ {destination dir}
}
LibCopy (){
For lib in 'ldd $1 | grep-o "/[^ [: space:] \ {1, \}" '; do
LibDir = 'dirname $ lib'
[-D $ {target }$ {libDir}] | mkdir-p $ {target }$ {libDir}
[-F $ {target }$ {lib}]
| Cp $ lib $ {target }$ {libDir}
Done
}
While true; do
Read-p "Enter a command:" cmd
If ["$ cmd" = 'quit']; then
Echo "quit"
Exit 0
Fi
ClearCmd $ cmd
[$? -Eq 5] & continue
Export copy $ export path
LibCopy $ export path
Done
5. Provide configuration files for grub
After porting the kernel and initrd files, we can compile the grub. conf configuration file based on the kernel version and initrd version.
[Root @ changsheng grub] # vim grub. conf
Default = 0
Timeout = 5
Title changsheng soft-linux
Root (hd0, 0)
Kernel/vmlinuz-soft ro root =/dev/sda2 quiet selinux = 0 init =/bin/bash
Initrd/initramfs-soft.img
~
Quiet is a silent installation without displaying a large amount of information during installation. Selinux should be turned off later, and init should use/bin/bash to tell the kernel not to look for the init program again. If you do not specify this step, the kernel panic (kernel panic) will be reported during the startup process, thinking that the system will be one, without the init process, panic will not work.
6. Start the test
7. Special reminder
If you do this experiment on vmvare, You must select "Store virtual disk as a single file" when creating a new virtual machine to create a new disk. Otherwise, the kernel panic may also appear.
For more details, please continue to read the highlights on the next page: