linux- system startup process -7

1, BIOS POST, check the cpu hardware and boot sequence, first look for information MBR disk head and load BOOtloader, and then control to the bootloader
2, GRUB
GRUB (Grand Unified Bootloader), multi-system startup program

stage1: Find load stage 1.5

stage1.5: you can identify a file system that recognizes / boot / partition the disk first

stage 2: grub /boot/grub/grub.conf identification, according to the configuration file, Kernel read information, and then load the kernel while also stage2 initrd (Initial RAM Disk) comprises a driver disk) are copied to memory, core decompression after loading the driver from the temporary file system, the root file system identification, / etc / fstab mount the file system, the kernel and root cutting, then performs the init process according to a first

Extracting the kernel while extracting micro initramfs file system, via the file system, the kernel driver acquires the disk, and drives all of the hardware peripherals, the root file system will read the disk
switches the disk to the file system, via / etc / fstab to mount all file systems. Then the implementation of the core init program, according to the / etc / inittab, execute script

System Process

In the kernel is loaded, and complete hardware detection and driver is loaded, the host hardware is ready, the kernel will automatically call the first process, that is, / sbin / init

 CentOS 5.x system also has a greater change compared. In CentOS 5.x system, mainly to set the system, and start by Linux init process configuration file / etc / inittab. But in the CentOS 6.x system, due to replace the previous init with Upstart start the service, so in the / etc / inittab configuration file can define system's default run level, while the other function is to rely on / etc / init / directory the other configuration file implementation

stage2:
boot time to see Grub options, information, and modify the GRUB stage2 background and other functions are provided, stage2 will read the configuration file /boot/grub/grub.conf or menu.lst etc.

4. Read grub.conf file
read grub.conf file to determine parameters of the core boot data, the kernel is ready to start

5. Start the kernel
load the kernel, the core began to decompress, start some of the most core of the program.
Because in order to allow enough light small kernel, hardware drivers and kernel files not on the inside, we can see that the kernel is very small, only about 4M, we can imagine the Windows drive, install the system when it requires a long time to download driver software it
therefore requires the use of /initramfs-2.6.32-696.el6.x86_64.img to drive hardware

6. Load pseudo file system (ramdisk),
the kernel has started up, then call ramdisk file, try to drive all the hardware devices, to this point, the core of it, all the drivers have installed, so you can boot back to the program

7. Start init process
grub specified default init = / sbin / init process executes the program can customize init = / bin / bash, the following steps may be bypassed in case the grub.conf kernel line interface directly into bash.
Display kernel source code files in about 996 lines, provides init sequence started, / sbin / init -> / etc / init -> / bin / init -> / bin / sh, / bin / bash did not write, it should be and / bin / sh as it

(1) read / etc / inittab file
inittab file which defines the system default run level, this step is to do some work as follows:

a) initial run level (the RUN the LEVEL)
B) system initialization script
c) corresponding to run-level scripts directory
d) define the UPS power supply terminal / recovery script
e) generating a getty virtual console to generate the terminal
f) running the initialization level 5 the X-
(2) /etc/rc.d/rc.sysinit program execution
system initialization scripts, mainly to complete the following work

a) providing a host name
b) a banner
c) may activate udev and selinux the grub.conf, kernel row is added to close selinux selinux = 0
D) mounted / etc / fstab file in the file system defined in
e) detecting the root file system, and read-write remount the root filesystem
f) set the system clock
g) activate the swap device
h) The kernel parameter setting file /etc/sysctl.conf
i) activate lvm equipment and software raid
loading additional equipment j) the driver
k) cleanup operations
(3) /etc/rc#.d/ file (services)
which are defined by a variety of services startup script, you can see ls, S represents the beginning of the start-up of the service, begin with K It is off tasks to be performed. # Represents a number, a number representing a run level, run a total of seven levels, there is not much to say

(4) /etc/rc.d/rc.local file
there can boot from the command definition.

8. Perform / bin / login
execute / bin / login program, waits for the user to log

CentOS7 startup process
CentOS7 and CentOS6 start a process similar, but the init program, when instead of systemd, therefore explain in detail the process started after the systemd

1.uefi or BIOS initialization, POST starts post
2. mbr loaded into memory
3.GRUB stage
4. Load the kernel module and inintamfs
The kernel starts initialization, instead of using systemd centos6 previous init program
(1) executes initrd. target
comprising a mount system / etc / fstab file, the mount case, you can switch to the root directory

(2) switch from the initramfs root file system to disk root directory
(3) systemd execute the default target configuration
centos7 surface is a "run level" concept, in fact, is for compatibility with the previous system, each of the so-called "operational level" are there are corresponding soft connection point, the default start level /etc/systemd/system/default.target, based on its points can be found in the system which mode you want to enter the
mode:

== 0> runlevel0.target, poweroff.target
. 1 ==> runlevel1.target, rescue.target
2 ==> runlevel2.target, multi-user.target
. 3 ==> runlevel3.target, multi-user.target
. 4 = => runlevel4.target, multi-user.target
5 ==> runlevel5.target, graphical.target
6 ==> runlevel6.target, reboot.target
(4) systemd execution sysinit.target
have a very familiar? Yes, on CentOS6 is called rc.sysint program, the system is initialized and ready operating system basic.target

(. 5) and a server systemd activate this service under multi-user.target
(. 6) /etc/rc.d/rc.local performed under systemd multi-user.target
the 6.Systemd execution of multi-user.target getty.target the log in
getty.target we are familiar, it is the start systemd target terminal. If this step, the system has not been specified to start the graphical desktop, this will be over, if you want to start the graphical interface, you need to start desktop program on the basis of

7.systemd graphical execution services needed
CentOS6,7 start the difference between
the system and start the server daemon manager, it is different from centos5 of Sysv init, centos6 of Upstart (Ubuntu made out), systemd is a staff Redhat presented first to the it started in the kernel, what services have all been taken over systemd, kernel only used to manage hardware resources, equivalent to a mere figurehead kernel, so it is not satisfied with Redhat linus this practice.

Experimental system startup process

centos   6    grub.conf 

grub/grub.conf 
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title CentOS 6 (2.6.32-754.el6.x86_64)
    root (hd0,0)
    kernel /vmlinuz-2.6.32-754.el6.x86_64 ro root=UUID=f2230c8f-30fb-4c8a-b7c9-a08f0e42bf7b rd_NO_LUKS rd_NO_LVM LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
    initrd /initramfs-2.6.32-754.el6.x86_64.img

1, / boot / grub.conf break ring showing

Core and out of the tab option initrd

root (hd0,0) means the boot partition, root = / dev / sda2 partition the finger

Creating /boot/grub/grub.conf after entering

default=0
timeout=5
title CentOS 6 (2.6.32-754.el6.x86_64)
root (hd0,0)
kernel /vmlinuz-2.6.32-754.el6.x86_64 ro root=/dev/sda2
initrd /initramfs-2.6.32-754.el6.x86_64.img

Is not configured root = / dev / sda2 given below

into the rescue mode

chroot /mnt/sysimage

grub-install --root-directory=/ /dev/sda

Creating /boot/grub/grub.conf

default=0
timeout=3
title centos6
root (hd0,0)
kernel /vmlinuz-------- root=/dev/sda2
initrd /initramfs ----

2, the broken ring mbr 446 bytes
dd if = / dev / zero of = / dev / sda bs = 1 count = 446
phenomenon

can also be seen to be able to mount the partition table and / mnt / sysimage


chroot /mnt/sysimage

grub-install /dev/sda

Sync
Sync data is written to disk

Exit Restart

3, delete the / boot / initramfs- · kernel version ·
phenomenon

Can display a list of kernel grub, but can not enter the carriage return after the next, the cursor flashes in the upper left corner, can not enter centoes 6logo interface

Rescue CD into the bash
cut root chroot / mnt / sysimage

生成 initramfs.img
mkinitrd /boot/initramfs-uname -r.img uname -r

mkinitrd /boot/initramfs-$(uname -r).img $(uname -r)

/boot/vmlinuz--

You can see grub grub.conf can be identified
but can not be loaded into the kernel properly,

Unable to load the kernel error

Rescue mode into the system, mount the CD-ROM
into the mirror directory, copy vmlinuz to vmlinuz- uname -rconsistent with the /boot/grub/grub.conf in the kernel

或者通过挂载光盘使用rpm强制安装内核生成
 rpm -ivh --force kernel-2.6.32-71.el6.x86_64.rpm 


https://www.ibm.com/developerworks/cn/linux/l-cn-disa-recov/

CentOS6 start the process

1. Load the BIOS hardware information, acquiring the first boot device
boot information is read first boot device 2. The MBR boot loader (GRUB) of
3. Load Information core operating system core, core decompression begins, and try to drive all the hardware devices
4. the core of the init program, and to get the default operating information
5.init /etc/rc.d/rc.sysinit program execution file
6. start the core of the plug-in module
7.init execution of each batch run processing files (scripts)
8.init performed /etc/rc.d/rc.local
9. The execute / bin / login program, waits for the user log
after the log 10 begins to control the host Shell

Kernel

Core file
compressed kernel image: / boot / vmlinuz-version- release
cores can mount disc image, acquired in the iso

RAMDISK auxiliary pseudo root system
in the stage-2 phase is read into the memory, after being temporarily released kernel mount decompressed,
the kernel reads the hardware drivers, disk identification system, mounts the real file system in turn, and then switch to the root of
the real root on.

CentOS 5 /boot/initrd-VERSION-release.img
CentOS 6.7 /boot/initramfs-VERSION-release.img

Tools may be used to generate
the mkinitrd
the mkinitrd / Boot / the initramfs - $ (the uname -R & lt) .img $ (the uname -R & lt)

inittab file of CentOS 5

init initialization

init reads its initialization file: / etc / inittab
initial run level (RUN LEVEL)
corresponding to the run-level system initialization script directory script
to capture a key order
defined UPS power supply terminal / restore script
generated virtual console getty
run level 5 X initialization

CentOS inittab file 6

/etc/rc.d/rc.sysinit: system initialization script

(1) Set the host name

(2) a banner

(3) activation and udev selinux

(4) mounted / etc file system / fstab file defined

(5) detection of the root file system, and read-write remount the root file system

(6) Set the system clock

(7) activating the device swap

(8) Set the kernel parameters file /etc/sysctl.conf

(9) activated lvm equipment and software raid

(10) an additional device driver is loaded

(11) clean-up operation

CentOS 7

CentOS 7
boot sequence UEFi or BIOS initialization, POST POST operation
to select the boot
boot loader, centos7 grub2 is
loaded loader profile:
/etc/grub.d/
/ etc / default / GRUB
/ Boot / grub2 / GRUB .cfg
loading initramfs driver module
is loaded the kernel options
kernel initialization, centos7 used instead of systemd init
execution initrd.target all cells, including mount / etc / fstab
switched from the disk to the root file system initramfs root
systemd target to execute the default configuration, the profile / etc / systemd / system / default.target
systemd initialize the system and perform sysinit.target basic.target preparation operation based
systemd start the machine with a server in the service multi-user.target
systemd execution / etc under multi-user.target / rc .d / rc.local
systemd execution getty.target the log in under multi-user.target
systemd execute graphical needed

6 crack

------ e ------ grub menu to modify the kernel plus 1 single-user mode

7 to crack passwords

When you start to hold down any key
press e to enter the edit mode
to move the cursor line starting linux16 added kernel parameters rd.break
press ctrl-x start
Mount the remount -o, RW / sysroot
CHROOT / sysroot
the passwd the root
Touch /.autorelabe
of LexiT
reboot

Method 2
start to hold down any key
press e to enter the edit mode
to move the cursor line starting linux16 instead rw init = / sysroot / bin / sh
by ctrl-x start
CHROOT / sysroot
the passwd the root
Touch /.autorelabel
Exit
reboot

Repair grub2

GRUB "the Grand Unified Bootloader"
can use the command line interface at the boot prompt can boot from a file system
main configuration file /boot/grub2/grub.cfg
repair the configuration file grub2-mkconfig -o /boot/grub2/grub.cfg
repair GRUB
grub2 -install / dev / sda BIOS environment
grub2-install UEFI environment
adjust the default boot kernel
Vim / etc / default / GRUB
GRUB_DEFAULT = 0
or
grub2-set-default "CentOS Linux (3.10.0-327.el7.x86_64) 7 (core ) "
to see if the amendment is successful
grub2-editenv list

Homemade Linux system

Add a new hard disk, the new disk scan

echo "- - -" /sys/

1, create a partition file system

fdisk / dev / sdb two partitions necessary
/ dev / sdb1 corresponding to / boot / dev / sdb2 corresponding to the root /

mkfs.ext4 /dev/sdb1
mkfs.ext4 /dev/sdb2

Mount the Boot
mkdir / mnt / subdirectory must be the Boot the Boot
Mount / dev / sdb1 / mnt / the Boot
and install grub

grub-install --root-directory=/mnt /dev/sdb

OK busybox Copy / dev / sdb1 instead of / dev / sdb2

2, restore the kernel and initramfs

cp /boot/vmlinuz-2.6.32-642.el6.x86_64 /mnt/boot/
cp /boot/initramfs-2.6.32-642.el6.x86_64.img /mnt/boot

Establish grub.conf

vim /mnt/boot/grub/grub.conf

title wanglinux
root (hd0,0)kernel
/vmlinuz-2.6.32-642.el6.x86_64 root=/dev/sda2 selinux=0 init=/bin/bash initrd
/initramfs-2.6.32-642.el6.x86_64.img

Kernel configuration file name and directory files remain consistent

3, create a directory and copy the relevant command to the specified directory

mkdir /mnt/sysroot
mount /dev/sdb2 /mnt/sysroot
mkdir –pv
/mnt/sysroot/{etc,lib,lib64,bin,sbin,tmp,var,usr,sys,proc,opt,home,root,boot,dev,mnt,media}

Copy the relevant commands and libraries

ifconfig,insmod,ping,mount,ls,cat,df,lsblk,blkid

ldd which swapon
to determine link library files, copy to the appropriate directory

If you need to use the network function, but also need to copy the driver e1000
cp /lib/modules/2.6.32-696.el6.x86_64/kernel/drivers/net/e1000/e1000.ko / mnt / sysroot / lib /

Check the network card driver
ethtool -i eth0
modinfo e1000 View driving position, copy NIC driver
insmod e1000.ko load the module
startup with a load module function

Copy the script to use the dynamic link library file to the relevant directory, copy into the drive module together

Copy the dynamic link library scripts
if the command and the presence of the target directory, copy command and dynamic link libraries to the specified directory
which --skip-alias mv skip alias


#!/bin/bash
bincopy(){
 if which $1 &> /dev/null ;then
 local bin_cmd=`which --skip-alias $1`
 local dir_cmd=`dirname $bin_cmd `
 [ -d ${chroot}${dir_cmd} ] || mkdir -pv ${chroot}${dir_cmd}
 [ -f ${chroot}${bin_cmd} ] || { cp $bin_cmd ${chroot}${dir_cmd}; action
"cp $bin_cmd ${chroot}${dir_cmd}" ; }
 return 0
 else
 echo "$1 is not found"
 return 1
 fi
}
libcopy(){
 ldd $(which --skip-alias $1)|grep -Eo "/[^[:space:]]+" |while read line ;do
 local lib_dir=`dirname $line`
 [ -d ${chroot}${lib_dir} ] || mkdir -pv ${chroot}${lib_dir}
 [ -f ${chroot}${line} ] || { cp $line ${chroot}${lib_dir}; action "cp
$line ${chroot}${lib_dir}" ; }
 done
}
source /etc/init.d/functions
chroot=/mnt/sysroot
[ -d $chroot ] || mkdir -pv $chroot
while : ;do
 read -p "please input a command you want to copy:" cmd
 [[ $cmd =~ ^q(uit)?$ ]] && break
 bincopy $cmd
 if [ $? -eq 0 ]; then
     libcopy $cmd
  fi
done

Compile the kernel module


lsmod        |  grep  xfs
显示由核心装载的模块 内容来自  /proc/modules
需要写模块路径,不能解决依赖关系
depmod  
insmod  指定模块文件,不自动解决依赖模块

insmod `modinfo –n exportfs`

modprobe   -r   装载模块

modinfo [ -k kernel ] [ modulename|filename... ]
-n:只显示模块文件路径
-p:显示模块参数-
a:作者
-d:描述


编译内核

前提:获取目标主机硬件设备信息,系统信息,开发环境准备
目标主机硬件设备相关信息
cpu  
cat /proc/cpuinfo
x86info -a
lscpu
硬件设备
lspci 
-v 
-vv
lsusb
-v
-vv
lsblk 
hal-device:CentOS 6
编译系统


安装开发包组
下载源码文件
.config:准备文本配置文件
make menuconfig:配置内核选项
make [-j #]    (选择相应的功能)
make modules_install:安装模块
make install :安装内核相关文件
    安装bzImage为/boot/vmlinuz-VERSION-RELEASE
    生成initramfs文件
    编辑grub的配置文件

部分编译内核
编译内核的一部分功能:
(a) 只编译某子目录中的相关代码
    cd /usr/src/linux
    make dir/
(b) 只编译一个特定的模块
    cd /usr/src/linux
    make dir/file.ko
示例:只为e1000编译驱动:
    make drivers/net/ethernet/intel/e1000/e1000.ko



示例
tar xf linux-3.10.67.tar.xz -C /usr/src
cd /usr/src
ln -sv linux-3.10.67 linux
cd /usr/src/linux
cp /boot/config-$(uname -r) ./.config
make help
make menuconfig
make -j 2
make modules_install
make install
reboot 


busybox compiled

centos 6 centos7 not compiled successfully, you can use the 6

编译安装busybox
环境
yum install gcc gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel zlib-devel libmcrypt-devel glibc-static ncurses-devel

Download the source code

wget https://busybox.net/downloads/busybox-1.30.0.tar.bz2

make menuconfig
in the following options to busybox also compiled static binary, not the shared library
Busybox Settings -> Build Options -> [*] Build BusyBox as a static binary (no shared libs)

make && make install If an error occurs, after the implementation of make clean, re-run the above command
mkdir / mnt / sysroot /
cp -a _install / * / mnt / sysroot /

_install directory file stored in soft connection

Interview

查看加载模块
lsmod          modinfo      modprobe 

开机启动文件 /etc/rc.d/

引导加载程序   LILO  GRUB

开机启动流程

查看BIOS版本   biosdecode
查看服务器型号: dmidecode | grep 'Product Name' 
查看主板的序列号: dmidecode |grep 'Serial Number'
ethtool  -i    eth0

内核参数处理
sysctl -a 显示当前所有可用的内核参数
sysctl kernel.hostname 读特定的内核参数,比如kernel.hostname
sysctl -w kernel.hostname=abc 把hostname改为abc


linux 系统运行级别

0: 系统停机(关机)模式,系统默认运行级别不能设置为0,否则不能正常启动,一开机就自动关机。
1:单用户模式,root权限,用于系统维护,禁止远程登陆,就像Windows下的安全模式登录。
2:多用户模式,没有NFS网络支持。
3:完整的多用户文本模式,有NFS,登陆后进入控制台命令行模式。
4:系统未使用,保留一般不用,在一些特殊情况下可以用它来做一些事情。例如在笔记本电脑的电池用尽时,可以切换到这个模式来做一些设置。
5:图形化模式,登陆后进入图形GUI模式或GNOME、KDE图形化界面,如X Window系统。
6:重启模式,默认运行级别不能设为6,否则不能正常启动,就会一直开机重启开机重启。


/etc/rc.d/rc.local   写入此文件的命令登陆前自动执行

系统关机及重启
shutdown    -h    10 自动关机 
                   -r    10  自动重启



centos  6  设置grub密码
vim   /boot/grub.conf  
default=0
timeout=8
password  centos  
........

###############
破坏的文件是需要命令进行恢复,即切根后可以执行完整的命令
如果没有命令,需要从光盘进行安装软件

#######
1,破坏stage-1    :mbr破坏掉
切换跟使用grub-install      /dev/sda  

2,删除vmlinuz和initramfs文件后无法启动
切跟后
mkinitrd /boot/initramfs-`uname -r`.img `uname -r `
cp /mnt/cdrom/isolinux/vmlinuz /boot/vmlinuz-`uname -r`
sync
sync     
或 

rpm -ivh /mnt/cdrom/Packages/kernel-2.6.32-696.e16.x86_64.rpm --root=/mnt/sysimage/    --force



centos 7 编译安装的服务配置service 服务脚本

Guess you like

Origin www.cnblogs.com/g2thend/p/11621103.html