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: mountpoint/mnt/bootdoes 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 longtime.
Installation finished. No error reported.
This is the contents of the device map/mnt/boot/grub/device. map.
Checkifthis 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 acommand: bash
Enter acommand: shutdown
Enter acommand: reboot
Enter acommand: vim
Enter acommand: touch
Enter acommand: mkdir
Enter acommand: rm
Enter acommand: ls
Enter acommand: cat
Enter acommand: less
Enter acommand: ifconfig
Enter acommand: ip
Enter acommand: route
Enter acommand: 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
Bashcatlsmkdirrmtouch
[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 (){
Ifwhich $ cmd &>/dev/null; then
Export Path = 'which -- skip-alias $ cmd'
Else
Echo "No such command"
Return5
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 (){
Forlibin '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
}
Whiletrue; do
Read-p "Enter a command:" cmd
If ["$ cmd" = 'quit']; then
Echo "quit"
Exit0
Fi
ClearCmd $ cmd
[$? -Eq5] & 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-softro root =/dev/sda2quiet 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.
4. Load modules to implement network functions
1. View Nic module information of the host
[Root @ changsheng net] # lsmod | grep e1000
E10000 170646
[Root @ changsheng net] #
2. view the detailed information of the NIC
[Root @ changsheng net] # modinfo e1000
Filename:/lib/modules/2.6.32-358. el6.x86 _ 64/kernel/drivers/net/e1000/e1000.ko
Version: 7.3.21-k8-NAPI
License: GPL
Description: Intel (R) PRO/1000 Network Driver
Author: Intel Corporation <linux.nics@intel.com>
Srcversion: 1D4F1E82BB99EA36D320B1B
Alias: pci: v20178086d20172e6esv * sd * bc * SC * I *
Alias: pci: v20178086d201710b5sv * sd * bc * SC * I *
Alias: pci: v20178086d20171099sv * sd * bc * SC * I *
Alias: pci: v20178086d00000000asv * sd * bc * SC * I *
Alias: pci: vda-8086d1_csv * sd * bc * SC * I *
The path of the NIC module is found here and copied to the/dev/sdb library file.
[Root @ changsheng net] # mkdir-pv/mnt/sysroot/lib64/modules
Mkdir: created directory '/mnt/sysroot/lib64/Les'
[Root @ changsheng net] # cp/lib/modules/2.6.32-358. el6.x86 _ 64/kernel/drivers/net/e1000/e1000.ko/mnt/sysroot/lib64/modules/e1000.ko
3. init program
Although the module has been copied, it is not available yet, and it does not meet our process needs, because there is no init program at the very least. If we want this init, there are two options: first, port the host system, but the format will be more complex. So we should first write the script and use the script as init, allows small systems to run. Init is generally in the sbin directory, so we need to write an init script on the/dev/sdb2 partition.
[Root @ changsheng ~] # Cd/mnt/sysroot/sbin/
[Root @ changsheng sbin] # vim init
#! /Bin/bash
# Print Welcome info
Echo-e "Welcome to \ 033 [34 m changsheng soft-linux \ 033 [0 m"
# Mount wei wenjian system when the system is running.
Mount-n-t proc/proc
Mount-n-t sysfs/sys
# Mount ethernet driver autl when the system is start.
Insmod/lib64/modules/e1000.ko
[$? -Eq0] & echo-e "Load e1000 module succeeded [\ 033 [32m0K \ 033 [0 m]"
Ifconfiglo 172.0.0.1/8
Ifconfigeth0 172.16.251.235/16
# Mount the/dev/sda2 to make it can be write and read.
Mount-n-o remount, rw/dev/sda2/
# Run/bin/bash
/Bin/bash
After writing this init script, we need to give it an execution permission so that it can be executed; this script also uses the mount, insmod commands, therefore, use the previous script to port these commands. Finally, you need to set/mnt/boot/grub. in conf, replace init =/bin/bash with init =/sbin/init, because I want to use this init script to start the system and no longer need to replace/bin/bash.
4. linux small systems that implement network functions
After completing the above steps, you can mount/dev/sdb to another host to experience our private customized small system.