Chapter 12 Linux startup and kernel management -- CentOS startup management

Keywords: Linux Operation & Maintenance server architecture Cloud Native

Chapter 12 Linux startup and kernel management

Content overview

  • Startup process for versions prior to CentOS 6
  • Service management
  • Grub management
  • Start troubleshooting
  • Kernel management
  • Launch process of CentOS version 7 and later
  • Unit introduction
  • Service management and viewing
  • Start troubleshooting
  • Crack root password
  • Repair Grub2

1.CentOS 6 startup management

1.1 Linux composition

  • kernel realizes the functions of process management, memory management, network management, driver, file system, security function and so on

  • rootfs includes programs and glibc libraries
    Programs: binary executables

    Library: function collection, function, calling interface (header file is responsible for description)

1.2 kernel design genre

  • Macro kernel: also known as single kernel and strong kernel, Unix and Linux

    Put all system services into the kernel, integrate all functions into the same program, and realize different functions hierarchically. The system is huge and complex. In fact, Linux realizes modularization in a single kernel kernel, which is equivalent to absorbing the advantages of micro kernel

  • Micro kernel: Windows, Solaris, HarmonyOS

    Simplify kernel functions, implement system services as much as possible in user states outside the kernel, and add mutual security protection. Each function is implemented by a separate subsystem, which moves kernel functions to user space, resulting in poor performance

1.3 CentOS 6 startup process

1.3.1 CentOS 6 startup process

  1. Load the hardware information of BIOS and get the first boot device
  2. Read the boot information of the boot loader (grub) of the first boot device MBR
  3. Load the core information of the core operating system, decompress the core and try to drive all hardware devices
  4. The core executes the init program and obtains the default operation information
  5. The init program executes the / etc/rc.d/rc.sysinit file to remount the root file system
  6. Start the plug-in module of the core
  7. init executes the individual batch files (scripts) that run
  8. init executes / etc/rc.d/rc.local
  9. Execute the / bin/login program and wait for the user to log in
  10. After logging in, start to control the host with Shell

1.3.2 hardware start POST

POST: power on self test is a major part of BIOS function. Be responsible for the detection of CPU, motherboard, memory, hard disk subsystem, display subsystem, serial parallel interface, keyboard and other hardware

ROM of the motherboard: BIOS, Basic Input and Output System, which stores the most important basic input and output programs related to the computer system, system information setting, power on self-test program, system startup bootstrap program, etc

RAM of the motherboard: CMOS complementary metal oxide semiconductor, save the settings of various parameters, and find the boot device in order. The first device with boot program is the boot device

1.3.3 starting the loader bootloader

1.3.3.1 grub function and composition

Bootloader: boot loader, bootloader

  • windows: ntloader, which only starts the OS
  • Linux: it has rich functions and provides menus that allow users to select the system to start or different kernel versions; The kernel selected by the user is loaded into a specific space in memory, decompressed and expanded, and the system control is transferred to the bootloader of Linux kernel
  • LILO: LInux LOader, the early bootloader, has a single function
  • GRUB: GRand Unified Bootloader, CentOS 6 GRUB 0.97: GRUB Legacy, GRUB 2.02 used after CentOS 7

GRUB startup phase

  • primary boot loader :

    1st stage: the first 446 bytes of MBR

    1.5 stage: the sector after mbr, so that the bootloader in stage 1 can recognize the file system on the partition where stage 2 is located

  • secondary boot loader: 2nd stage, partition file / boot/grub/

example:

[root@centos6 ~]# hexdump -C -n 512 /dev/sda
00000000  eb 48 90 00 00 00 00 00  00 00 00 00 00 00 00 00  |.H..............|
00000010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00000030  00 00 00 00 00 00 00 00  00 00 00 00 00 00 03 02  |................|
00000040  ff 00 00 20 01 00 00 00  00 02 fa 90 90 f6 c2 80  |... ............|
00000050  75 02 b2 80 ea 59 7c 00  00 31 c0 8e d8 8e d0 bc  |u....Y|..1......|
00000060  00 20 fb a0 40 7c 3c ff  74 02 88 c2 52 f6 c2 80  |. ..@|<.t...R...|
00000070  74 54 b4 41 bb aa 55 cd  13 5a 52 72 49 81 fb 55  |tT.A..U..ZRrI..U|
00000080  aa 75 43 a0 41 7c 84 c0  75 05 83 e1 01 74 37 66  |.uC.A|..u....t7f|
00000090  8b 4c 10 be 05 7c c6 44  ff 01 66 8b 1e 44 7c c7  |.L...|.D..f..D|.|
000000a0  04 10 00 c7 44 02 01 00  66 89 5c 08 c7 44 06 00  |....D...f.\..D..|
000000b0  70 66 31 c0 89 44 04 66  89 44 0c b4 42 cd 13 72  |pf1..D.f.D..B..r|
000000c0  05 bb 00 70 eb 7d b4 08  cd 13 73 0a f6 c2 80 0f  |...p.}....s.....|
000000d0  84 f0 00 e9 8d 00 be 05  7c c6 44 ff 00 66 31 c0  |........|.D..f1.|
000000e0  88 f0 40 66 89 44 04 31  d2 88 ca c1 e2 02 88 e8  |..@f.D.1........|
000000f0  88 f4 40 89 44 08 31 c0  88 d0 c0 e8 02 66 89 04  |..@.D.1......f..|
00000100  66 a1 44 7c 66 31 d2 66  f7 34 88 54 0a 66 31 d2  |f.D|f1.f.4.T.f1.|
00000110  66 f7 74 04 88 54 0b 89  44 0c 3b 44 08 7d 3c 8a  |f.t..T..D.;D.}<.|
00000120  54 0d c0 e2 06 8a 4c 0a  fe c1 08 d1 8a 6c 0c 5a  |T.....L......l.Z|
00000130  8a 74 0b bb 00 70 8e c3  31 db b8 01 02 cd 13 72  |.t...p..1......r|
00000140  2a 8c c3 8e 06 48 7c 60  1e b9 00 01 8e db 31 f6  |*....H|`......1.|
00000150  31 ff fc f3 a5 1f 61 ff  26 42 7c be 7f 7d e8 40  |1.....a.&B|..}.@|
00000160  00 eb 0e be 84 7d e8 38  00 eb 06 be 8e 7d e8 30  |.....}.8.....}.0|
00000170  00 be 93 7d e8 2a 00 eb  fe 47 52 55 42 20 00 47  |...}.*...GRUB .G|
00000180  65 6f 6d 00 48 61 72 64  20 44 69 73 6b 00 52 65  |eom.Hard Disk.Re|
00000190  61 64 00 20 45 72 72 6f  72 00 bb 01 00 b4 0e cd  |ad. Error.......|
000001a0  10 ac 3c 00 75 f4 c3 00  00 00 00 00 00 00 00 00  |..<.u...........|
000001b0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 80 20  |............... |
000001c0  21 00 83 aa 28 82 00 08  00 00 00 00 20 00 00 aa  |!...(....... ...|
000001d0  29 82 83 fe ff ff 00 08  20 00 00 80 3c 0c 00 fe  |)....... ...<...|
000001e0  ff ff 83 fe ff ff 00 88  5c 0c 00 00 40 06 00 fe  |........\...@...|
000001f0  ff ff 05 fe ff ff 00 88  9c 12 00 78 63 06 55 aa  |...........xc.U.|
00000200
[root@centos6 ~]# hexdump -C -n 512 -v /dev/sda
00000000  eb 48 90 00 00 00 00 00  00 00 00 00 00 00 00 00  |.H..............|
00000010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000020  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000030  00 00 00 00 00 00 00 00  00 00 00 00 00 00 03 02  |................|
00000040  ff 00 00 20 01 00 00 00  00 02 fa 90 90 f6 c2 80  |... ............|
00000050  75 02 b2 80 ea 59 7c 00  00 31 c0 8e d8 8e d0 bc  |u....Y|..1......|
00000060  00 20 fb a0 40 7c 3c ff  74 02 88 c2 52 f6 c2 80  |. ..@|<.t...R...|
00000070  74 54 b4 41 bb aa 55 cd  13 5a 52 72 49 81 fb 55  |tT.A..U..ZRrI..U|
00000080  aa 75 43 a0 41 7c 84 c0  75 05 83 e1 01 74 37 66  |.uC.A|..u....t7f|
00000090  8b 4c 10 be 05 7c c6 44  ff 01 66 8b 1e 44 7c c7  |.L...|.D..f..D|.|
000000a0  04 10 00 c7 44 02 01 00  66 89 5c 08 c7 44 06 00  |....D...f.\..D..|
000000b0  70 66 31 c0 89 44 04 66  89 44 0c b4 42 cd 13 72  |pf1..D.f.D..B..r|
000000c0  05 bb 00 70 eb 7d b4 08  cd 13 73 0a f6 c2 80 0f  |...p.}....s.....|
000000d0  84 f0 00 e9 8d 00 be 05  7c c6 44 ff 00 66 31 c0  |........|.D..f1.|
000000e0  88 f0 40 66 89 44 04 31  d2 88 ca c1 e2 02 88 e8  |..@f.D.1........|
000000f0  88 f4 40 89 44 08 31 c0  88 d0 c0 e8 02 66 89 04  |..@.D.1......f..|
00000100  66 a1 44 7c 66 31 d2 66  f7 34 88 54 0a 66 31 d2  |f.D|f1.f.4.T.f1.|
00000110  66 f7 74 04 88 54 0b 89  44 0c 3b 44 08 7d 3c 8a  |f.t..T..D.;D.}<.|
00000120  54 0d c0 e2 06 8a 4c 0a  fe c1 08 d1 8a 6c 0c 5a  |T.....L......l.Z|
00000130  8a 74 0b bb 00 70 8e c3  31 db b8 01 02 cd 13 72  |.t...p..1......r|
00000140  2a 8c c3 8e 06 48 7c 60  1e b9 00 01 8e db 31 f6  |*....H|`......1.|
00000150  31 ff fc f3 a5 1f 61 ff  26 42 7c be 7f 7d e8 40  |1.....a.&B|..}.@|
00000160  00 eb 0e be 84 7d e8 38  00 eb 06 be 8e 7d e8 30  |.....}.8.....}.0|
00000170  00 be 93 7d e8 2a 00 eb  fe 47 52 55 42 20 00 47  |...}.*...GRUB .G|
00000180  65 6f 6d 00 48 61 72 64  20 44 69 73 6b 00 52 65  |eom.Hard Disk.Re|
00000190  61 64 00 20 45 72 72 6f  72 00 bb 01 00 b4 0e cd  |ad. Error.......|
000001a0  10 ac 3c 00 75 f4 c3 00  00 00 00 00 00 00 00 00  |..<.u...........|
000001b0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 80 20  |............... |
000001c0  21 00 83 aa 28 82 00 08  00 00 00 00 20 00 00 aa  |!...(....... ...|
000001d0  29 82 83 fe ff ff 00 08  20 00 00 80 3c 0c 00 fe  |)....... ...<...|
000001e0  ff ff 83 fe ff ff 00 88  5c 0c 00 00 40 06 00 fe  |........\...@...|
000001f0  ff ff 05 fe ff ff 00 88  9c 12 00 78 63 06 55 aa  |...........xc.U.|
00000200

[root@centos6 ~]# df -Th
Filesystem     Type   Size  Used Avail Use% Mounted on
/dev/sda2      ext4    97G  908M   91G   1% /
tmpfs          tmpfs  490M     0  490M   0% /dev/shm
/dev/sda1      ext4   976M   31M  895M   4% /boot
/dev/sda3      ext4    50G   74M   47G   1% /data

[root@centos6 ~]# modinfo ext4
filename:       /lib/modules/2.6.32-754.el6.x86_64/kernel/fs/ext4/ext4.ko
license:        GPL
description:    Fourth Extended Filesystem
author:         Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others
retpoline:      Y
srcversion:     3C0D8A8996992A22750E504
depends:        mbcache,jbd2
vermagic:       2.6.32-754.el6.x86_64 SMP mod_unload modversions 

[root@centos6 ~]# ll -h /lib/modules/2.6.32-754.el6.x86_64/kernel/fs/ext4/ext4.ko
-rwxr--r--. 1 root root 636K Jun 20  2018 /lib/modules/2.6.32-754.el6.x86_64/kernel/fs/ext4/ext4.ko

[root@centos6 ~]# fdisk -l

Disk /dev/sda: 214.7 GB, 214748364800 bytes
255 heads, 63 sectors/track, 26108 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0008a04f

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1         131     1048576   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2             131       12910   102645760   83  Linux
/dev/sda3           12910       19437    52428800   83  Linux
/dev/sda4           19437       26109    53591040    5  Extended
/dev/sda5           19437       19698     2097152   82  Linux swap / Solaris

[root@centos7 ~]# fdisk -l

Disk /dev/sda: 214.7 GB, 214748364800 bytes, 419430400 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000b65a1

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200   211814399   104857600   83  Linux
/dev/sda3       211814400   316671999    52428800   83  Linux
/dev/sda4       316672000   419430399    51379200    5  Extended
/dev/sda5       316674048   320868351     2097152   82  Linux swap / Solaris

[root@centos6 ~]# ls /boot
config-2.6.32-754.el6.x86_64  grub                                 lost+found                        System.map-2.6.32-754.el6.x86_64
efi                           initramfs-2.6.32-754.el6.x86_64.img  symvers-2.6.32-754.el6.x86_64.gz  vmlinuz-2.6.32-754.el6.x86_64

[root@centos6 ~]# ls /boot/grub/
device.map     fat_stage1_5  grub.conf         jfs_stage1_5  minix_stage1_5     splash.xpm.gz  stage2         vstafs_stage1_5
e2fs_stage1_5  ffs_stage1_5  iso9660_stage1_5  menu.lst      reiserfs_stage1_5  stage1         ufs2_stage1_5  xfs_stage1_5

[root@centos6 ~]# cat /boot/grub/grub.conf
# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE:  You have a /boot partition.  This means that
#          all kernel and initrd paths are relative to /boot/, eg.
#          root (hd0,0)
#          kernel /vmlinuz-version ro root=/dev/sda2
#          initrd /initrd-[generic-]version.img
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz #Load picture
hiddenmenu
title CentOS 6 (2.6.32-754.el6.x86_64) #menu
	root (hd0,0)
	kernel /vmlinuz-2.6.32-754.el6.x86_64 ro root=UUID=f26b3084-878f-4218-a7df-0eedd8cb79af 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
#/vmlinuz-2.6.32-754.el6.x86_64 kernel root=UUID=55ed4e69-a262-485e-a2c8-9df9ac044aec / root partition UUID initrd / initramfs-2.6.32-754.el6.x86_ 64.img driver file

[root@centos6 ~]# blkid
/dev/sda1: UUID="07ff813e-74af-4047-88ad-c402b397fd1c" TYPE="ext4" 
/dev/sda2: UUID="f26b3084-878f-4218-a7df-0eedd8cb79af" TYPE="ext4" 
/dev/sda3: UUID="c8cea348-2536-46ae-9b3b-fe0543fbdc13" TYPE="ext4" 
/dev/sda5: UUID="66c77c3c-e56c-4df2-90fd-08150563b747" TYPE="swap"

[root@centos6 ~]# ls /boot
config-2.6.32-754.el6.x86_64  grub                                 lost+found                        System.map-2.6.32-754.el6.x86_64
efi                           initramfs-2.6.32-754.el6.x86_64.img  symvers-2.6.32-754.el6.x86_64.gz  vmlinuz-2.6.32-754.el6.x86_64
# initramfs-2.6.32-754.el6.x86_64.img driver file vmlinuz-2.6.32-754.el6.x86_64 kernel files

[root@centos6 ~]# file /boot/initramfs-2.6.32-754.el6.x86_64.img
/boot/initramfs-2.6.32-754.el6.x86_64.img: gzip compressed data, from Unix, last modified: Wed Oct  6 23:46:51 2021, max compression

[root@centos6 ~]# cp /boot/initramfs-2.6.32-754.el6.x86_64.img /data
cp: overwrite `/data/initramfs-2.6.32-754.el6.x86_64.img'? y
[root@centos6 ~]# cd /data
[root@centos6 data]# ls
initramfs-2.6.32-754.el6.x86_64.img  lost+found

[root@centos6 data]# mv initramfs-2.6.32-754.el6.x86_64.img initramfs-2.6.32-754.el6.x86_64.img.gz
[root@centos6 data]# gzip -d initramfs-2.6.32-754.el6.x86_64.img.gz

[root@centos6 data]# ls
initramfs-2.6.32-754.el6.x86_64.img  lost+found
[root@centos6 data]# ll -h initramfs-2.6.32-754.el6.x86_64.img 
-rw------- 1 root root 61M Nov  5 20:13 initramfs-2.6.32-754.el6.x86_64.img

[root@centos6 data]# ll /boot/initramfs-2.6.32-754.el6.x86_64.img -h
-rw-------. 1 root root 22M Nov  4 22:57 /boot/initramfs-2.6.32-754.el6.x86_64.img

[root@centos6 data]# file initramfs-2.6.32-754.el6.x86_64.img 
initramfs-2.6.32-754.el6.x86_64.img: ASCII cpio archive (SVR4 with no CRC)

[root@centos6 data]# cpio -id < initramfs-2.6.32-754.el6.x86_64.img 
123797 blocks
[root@centos6 data]# ls
bin      dracut-004-411.el6  init                initqueue-settled                    lib         mount      pre-pivot    proc  sysroot  var
cmdline  emergency           initqueue           initqueue-timeout                    lib64       netroot    pre-trigger  sbin  tmp
dev      etc                 initqueue-finished  initramfs-2.6.32-754.el6.x86_64.img  lost+found  pre-mount  pre-udev     sys   usr

[root@centos6 data]# find -name ext4.ko
./lib/modules/2.6.32-754.el6.x86_64/kernel/fs/ext4/ext4.ko

1.3.3.2 CentOS 6 grub installation

There are two ways to install grub

Method 1: GRUB install install grub stage1 and stage1_5 to / dev/DISK disk, and copy GRUB related files to DIR/boot directory

grub-install --root-directory=DIR /dev/DISK

Method 2: grub command

#grub
grub> root (hd#,#)
grub> setup (hd#)

Example: fix grub's phase 1 Failure

[root@centos6 ~]# hexdump -C -n 512 /dev/sda
00000000  eb 48 90 10 8e d0 bc 00  b0 b8 00 00 8e d8 8e c0  |.H..............|
00000010  fb be 00 7c bf 00 06 b9  00 02 f3 a4 ea 21 06 00  |...|.........!..|
00000020  00 be be 07 38 04 75 0b  83 c6 10 81 fe fe 07 75  |....8.u........u|
00000030  f3 eb 16 b4 02 b0 01 bb  00 7c b2 80 8a 74 03 02  |.........|...t..|
00000040  80 00 00 80 30 f2 04 00  00 08 fa 90 90 f6 c2 80  |....0...........|
00000050  75 02 b2 80 ea 59 7c 00  00 31 c0 8e d8 8e d0 bc  |u....Y|..1......|
00000060  00 20 fb a0 40 7c 3c ff  74 02 88 c2 52 f6 c2 80  |. ..@|<.t...R...|
00000070  74 54 b4 41 bb aa 55 cd  13 5a 52 72 49 81 fb 55  |tT.A..U..ZRrI..U|
00000080  aa 75 43 a0 41 7c 84 c0  75 05 83 e1 01 74 37 66  |.uC.A|..u....t7f|
00000090  8b 4c 10 be 05 7c c6 44  ff 01 66 8b 1e 44 7c c7  |.L...|.D..f..D|.|
000000a0  04 10 00 c7 44 02 01 00  66 89 5c 08 c7 44 06 00  |....D...f.\..D..|
000000b0  70 66 31 c0 89 44 04 66  89 44 0c b4 42 cd 13 72  |pf1..D.f.D..B..r|
000000c0  05 bb 00 70 eb 7d b4 08  cd 13 73 0a f6 c2 80 0f  |...p.}....s.....|
000000d0  84 f0 00 e9 8d 00 be 05  7c c6 44 ff 00 66 31 c0  |........|.D..f1.|
000000e0  88 f0 40 66 89 44 04 31  d2 88 ca c1 e2 02 88 e8  |..@f.D.1........|
000000f0  88 f4 40 89 44 08 31 c0  88 d0 c0 e8 02 66 89 04  |..@.D.1......f..|
00000100  66 a1 44 7c 66 31 d2 66  f7 34 88 54 0a 66 31 d2  |f.D|f1.f.4.T.f1.|
00000110  66 f7 74 04 88 54 0b 89  44 0c 3b 44 08 7d 3c 8a  |f.t..T..D.;D.}<.|
00000120  54 0d c0 e2 06 8a 4c 0a  fe c1 08 d1 8a 6c 0c 5a  |T.....L......l.Z|
00000130  8a 74 0b bb 00 70 8e c3  31 db b8 01 02 cd 13 72  |.t...p..1......r|
00000140  2a 8c c3 8e 06 48 7c 60  1e b9 00 01 8e db 31 f6  |*....H|`......1.|
00000150  31 ff fc f3 a5 1f 61 ff  26 42 7c be 7f 7d e8 40  |1.....a.&B|..}.@|
00000160  00 eb 0e be 84 7d e8 38  00 eb 06 be 8e 7d e8 30  |.....}.8.....}.0|
00000170  00 be 93 7d e8 2a 00 eb  fe 47 52 55 42 20 00 47  |...}.*...GRUB .G|
00000180  65 6f 6d 00 48 61 72 64  20 44 69 73 6b 00 52 65  |eom.Hard Disk.Re|
00000190  61 64 00 20 45 72 72 6f  72 00 bb 01 00 b4 0e cd  |ad. Error.......|
000001a0  10 ac 3c 00 75 f4 c3 00  00 00 00 00 00 00 00 00  |..<.u...........|
000001b0  00 00 00 00 00 00 00 00  4f a0 08 00 00 00 80 20  |........O...... |
000001c0  21 00 83 aa 28 82 00 08  00 00 00 00 20 00 00 aa  |!...(....... ...|
000001d0  29 82 83 fe ff ff 00 08  20 00 00 80 3c 0c 00 fe  |)....... ...<...|
000001e0  ff ff 83 fe ff ff 00 88  5c 0c 00 00 40 06 00 fe  |........\...@...|
000001f0  ff ff 05 fe ff ff 00 88  9c 12 00 78 63 06 55 aa  |...........xc.U.|
00000200

#Destruction grub stage 1
[[root@centos6 ~]# dd if=/dev/zero of=/dev/sda bs=1 count=446
446+0 records in
446+0 records out
446 bytes (446 B) copied, 0.000782486 s, 570 kB/s
[root@centos6 ~]# hexdump -C -n 512 /dev/sda
00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
000001b0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 80 20  |............... |
000001c0  21 00 83 aa 28 82 00 08  00 00 00 00 20 00 00 aa  |!...(....... ...|
000001d0  29 82 83 fe ff ff 00 08  20 00 00 80 3c 0c 00 fe  |)....... ...<...|
000001e0  ff ff 83 fe ff ff 00 88  5c 0c 00 00 40 06 00 fe  |........\...@...|
000001f0  ff ff 05 fe ff ff 00 88  9c 12 00 78 63 06 55 aa  |...........xc.U.|
00000200

[root@centos6 ~]# hexdump -C -n 512 -v /dev/sda
00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000020  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000030  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000040  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000050  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000060  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000070  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000080  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000090  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
000000a0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
000000b0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
000000c0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
000000d0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
000000e0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
000000f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000100  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000110  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000120  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000130  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000140  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000150  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000160  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000170  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000180  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000190  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
000001a0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
000001b0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 80 20  |............... |
000001c0  21 00 83 aa 28 82 00 08  00 00 00 00 20 00 00 aa  |!...(....... ...|
000001d0  29 82 83 fe ff ff 00 08  20 00 00 80 3c 0c 00 fe  |)....... ...<...|
000001e0  ff ff 83 fe ff ff 00 88  5c 0c 00 00 40 06 00 fe  |........\...@...|
000001f0  ff ff 05 fe ff ff 00 88  9c 12 00 78 63 06 55 aa  |...........xc.U.|
00000200
#The first 446 bytes are cleared

[root@centos6 ~]# reboot


The first stage is damaged. The system thinks that the hard disk cannot be started and starts automatically from the CD


language


Keyboard layout


Network not configured


Continue


/The root partition is mounted to / mnt/sysimage

CD boot, enter rescue pattern
#chroot /mnt/sysimage  #Cut to the root directory
#grub-install /dev/sda  #Repair first 446 bytes 
#sync #save
#exit #sign out
#reboot #restart


The system can start normally

example:

[root@centos6 ~]# grub
Probing devices to guess BIOS drives. This may take a long time.


    GNU GRUB  version 0.97  (640K lower / 3072K upper memory)

 [ Minimal BASH-like line editing is supported.  For the first word, TAB
   lists possible command completions.  Anywhere else TAB lists the possible
   completions of a device/filename.]
grub> root (hd0,0)
root (hd0,0)
 Filesystem type is ext2fs, partition type 0x83
grub> setup (hd0)
setup (hd0)
 Checking if "/boot/grub/stage1" exists... no
 Checking if "/grub/stage1" exists... yes
 Checking if "/grub/stage2" exists... yes
 Checking if "/grub/e2fs_stage1_5" exists... yes
 Running "embed /grub/e2fs_stage1_5 (hd0)"...  27 sectors are embedded.
succeeded
 Running "install /grub/stage1 (hd0) (hd0)1+27 p (hd0,0)/grub/stage2 /grub/grub.conf"... succeeded
Done.
grub> quit
quit

1.3.3.3 grub legacy management

Configuration file: / boot / grub / grub.conf < -- / etc / grub.conf

stage2 and kernel are usually placed in a basic disk partition

grub legacy function:

(1) Provide Startup menu and interactive interface

a: kernel parameters

e: edit mode, used to edit the menu

c: command mode, interactive interface

(2) Load the kernel or operating system selected by the user

Allows parameters to be passed to the kernel

You can hide the Startup menu

(3) Provides a protection mechanism for menus

Authenticate for edit launch menu

Authenticate to enable kernel or operating system

Command line interface for grub

help: Get help list
help KEYWORD: Detailed help
find (hd#,#)/PATH/TO/SOMEFILE: 
root (hd#,#)
kernel /PATH/TO/KERNEL_FILE: Set the kernel file for this startup; In addition, you can add many kernel supported
cmdline parameter

For example: max_loop=100 selinux=0 init=/path/to/init
initrd /PATH/TO/INITRAMFS_FILE: Sets the number of additional files available for the selected kernel ramdisk
boot: Boot boot selected kernel

cat /proc/cmdline kernel parameters

Kernel parameter documentation:

/usr/share/doc/kernel-doc-2.6.32/Documentation/kernel-parameters.txt

grub legacy identifies the hard disk device

(hd#,#)
hd#: disk number, expressed in numbers; Number from 0
#: partition number, expressed in numbers; Number from 0

Example:
(hd0,0) First hard disk, first partition

Start the system manually at the grub command line interface

grub> root (hd#,#)
grub> kernel /vmlinuz-VERSION-RELEASE ro root=/dev/DEVICE
grub> initrd /initramfs-VERSION-RELEASE.img
grub> boot

grub legacy configuration file: / boot/grub/grub.conf

default=#: set the default Startup menu item; The single item (title) number starts from 0
timeout=#: specifies how long the menu item waits for option selection
splashimage=(hd#,#)/PATH/XPM_FILE: menu background picture file path
password [--md5| --encrypt] STRING: Start menu edit authentication
hiddenmenu: Hide menu
title TITLE: Define menu item title, Can occur multiple times
root (hd#,#): find the device partition where stage2 and kernel files are located; Is the root of grub
kernel /PATH/TO/VMLINUZ_FILE [PARAMETERS]: Boot kernel
initrd /PATH/TO/INITRAMFS_FILE: Kernel matched ramfs file
password [--md5|--encrypted ] STRING: Authenticate when starting the selected kernel or operating system

Grub encryption generates grub password

grub-md5-crypt
grub-crypt

Crack root password:

(1) edit grub menu(Select the to edit title,Then use a or e command)
(2) In the selected kernel Post attachment 1, s, S,single Can enter single user mode
(3) stay kernel Line, type“ b"command

Example: crack CentOS 6 root password


Press ESC to enter menu selection, and press a


At the end, enter 1 to temporarily enter 1 mode and single user mode

#You don't need a password to come in directly. passwd changes the password directly
# passwd root

# init 3 #Enter 3 mode

Enter a new password to log in directly

Example: add a password to grub to prevent cracking the root password

[root@centos6 ~]# grub-crypt
Password: 
Retype password: 
$6$dd3QxTQiLvVTxITw$HGyYnMmaFsMA98nYgs8bDaccnw716Bm8i/toQeDRJFcYQ5L7hYdD7OKGTx30fsX9GFr7AGsByFj0SzHbZIAJY/
#Set grub generation password

[root@centos6 ~]# vim /boot/grub/grub.conf
# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE:  You have a /boot partition.  This means that
#          all kernel and initrd paths are relative to /boot/, eg.
#          root (hd0,0)
#          kernel /vmlinuz-version ro root=/dev/sda2
#          initrd /initrd-[generic-]version.img
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
password --encrypt $6$dd3QxTQiLvVTxITw$HGyYnMmaFsMA98nYgs8bDaccnw716Bm8i/toQeDRJFcYQ5L7hYdD7OKGTx30fsX9GFr7AGsByFj0SzHbZIAJY/ #Set grub password here
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=f26b3084-878f-4218-a7df-0eedd8cb79af 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

[root@centos6 ~]# reboot


Press ESC to enter menu selection, and press p


Input password


You can only enter here by entering your password

forget grub Password, go straight vim /boot/grub/grub.conf File, put password --encrypt $6$dd3QxTQiLvVTxITw$HGyYnMmaFsMA98nYgs8bDaccnw716Bm8i/toQeDRJFcYQ5L7hYdD7OKGTx30fsX9GFr7AGsByFj0SzHbZIAJY/
If this line is deleted, there is no grub Password

Example: generate background picture

[root@centos6 ~]# convert -resize 640x480 -colors 14 winner.png splash.xpm
[root@centos6 ~]# more splash.xpm

#Generate splash.xpm.gz
[root@centos6 ~]# gzip splash.xpm
[root@centos6 ~]# mv splash.xpm.gz /boot/grub

1.3. Loading kernel


kernel self initialization process

  1. Detect all identifiable hardware devices
  2. Load the hardware driver (with the help of ramdisk)
  3. Mount the root file system as read-only
  4. Run the first application in user space: / sbin/init

Linux kernel features:

  • Support Modularization:. ko (kernel object), such as file system, hardware driver, network protocol, etc
  • Support dynamic loading and unloading of kernel modules

Kernel components:

  • Core file: / boot / vmlinuz version release

    ramdisk: an auxiliary pseudo root system. Load the corresponding hardware drivers. ramdisk -- > ramfs improves the speed

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

    CentOS version 6 and later / boot/initramfs-VERSION-release.img

  • Module file: / lib / modules / version release

Example: delete kernel file / boot / vmlinuz-2.6.32-754.el6.x86 by mistake_ 64 unable to start, failure recovery

[root@centos6 ~]# rm -f /boot/vmlinuz-2.6.32-754.el6.x86_64
[root@centos6 ~]# reboot


The system will not start


Press ESC and select optical drive boot


Enter CD rescue mode


language


Keyboard layout


Network not configured


Continue


/The root partition is mounted to / mnt/sysimage

#chroot /mnt/sysimage switch to the root directory
#mount /dev/sr0 /mnt/ #Mount CD
#cp /mnt/isolinux/vmlinuz /boot/vmlinuz-2.6.32-754.el6.x86_64 #Copy kernel files from CD
#sync #save
#exit #sign out
#reboot #restart

The system can start normally

Making of ramdisk file:

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

Example: mistakenly delete / boot/initramfs-2.6.32-754.el6.x86_64.img failed to start, failure recovery

[root@centos6 ~]# rm -f /boot/initramfs-2.6.32-754.el6.x86_64.img
[root@centos6 ~]# reboot


Press ESC and select optical drive boot


Enter CD rescue mode


language


Keyboard layout


Network not configured


Continue


/The root partition is mounted to / mnt/sysimage

#chroot /mnt/sysimage   #Switch to the root directory
#mkinitrd /boot/initramfs-`uname -r`.img `uname -r`  #Generate driver file
#sync  #save
#exit  #sign out
#reboot #restart

The system can start normally

1.3.5 init initialization

Post -- > bootsequence (BIOS) - > bootloader (MBR) - > kernel (ramdisk) - > rootfs (read only) - > init (systemd)

Type of init program:

SYSV: init, before CentOS 5

Configuration file: / etc/inittab

Upstart: init,CentOS 6

Configuration files: / etc/inittab, /etc/init/*.conf

Systemd: systemd, CentOS 7

Configuration file: / usr/lib/systemd/system

​ /etc/systemd/system

1.3.5.1 operation level

Operation level: set for the purpose of system operation or maintenance; 0-6: 7 levels. Generally, 3 and 5 are used as the default levels

0: Shut down
1: Single user mode(root automatic logon), single, maintenance mode
2: In multi-user mode, the network function is started, but it will not be started NFS;maintenance mode
3: Multi user mode, normal mode; Text interface
4: Reservation level; Can be the same as 3 levels
5: Multi user mode, normal mode; Graphical interface
6: restart

Switch level:

init #

View level:

runlevel
who -r

Define run level

/etc/inittab

The inittab file of CentOS 5 also defines the following

Initial run level(RUN LEVEL)
System initialization script
 Script directory corresponding to the run level
 Capture a keyword order
 definition UPS Power terminal/Recovery script
 Generate in virtual console getty
 Initialize at run level 5 X

Format of each line of inittab file in CentOS 5:

id:runlevel:action:process

id: Is a sequence of characters that uniquely identifies the item
runlevels:  Defines the run level used by the operation
action:  Specifies the specific action to perform
		wait: Switch to this level and run once
		respawn: this process Terminate, restart
		initdefault: Set the default operation level; process ellipsis
		sysinit: Set system initialization mode
process: Defines the process to execute

Example: inittab file of CentOS 5

id:5:initdefault:
si::sysinit:/etc/rc.d/rc.sysinit
l0:0:wait:/etc/rc.d/rc 0
l1:1:wait:/etc/rc.d/rc 1
l2:2:wait:/etc/rc.d/rc 2
l3:3:wait:/etc/rc.d/rc 3
l4:4:wait:/etc/rc.d/rc 4
l5:5:wait:/etc/rc.d/rc 5
l6:6:wait:/etc/rc.d/rc 6
ca::ctrlaltdel:/sbin/shutdown -t3 -r now
pf::powerfail:/sbin/shutdown -f -h +2 "Power Failure; System Shutting Down"
pr:12345:powerokwait:/sbin/shutdown -c "Power Restored; Shutdown Cancelled"
1:2345:respawn:/sbin/mingetty tty1
2:2345:respawn:/sbin/mingetty tty2
3:2345:respawn:/sbin/mingetty tty3
4:2345:respawn:/sbin/mingetty tty4
5:2345:respawn:/sbin/mingetty tty5
6:2345:respawn:/sbin/mingetty tty6
x:5:respawn:/etc/X11/prefdm -nodaemon

CentOS 6 /etc/inittab and related files

CentOS 6 init program is upstart, and its configuration files are / etc/inittab, /etc/init/*.conf. the syntax of the configuration file follows the syntax format of upstart configuration file, which is different from that of CentOS5

/etc/inittab Set the system default run level
/etc/init/control-alt-delete.conf
/etc/init/tty.conf
/etc/init/start-ttys.conf
/etc/init/rc.conf
/etc/init/prefdm.conf

example:

[root@centos6 ~]# pstree -p
init(1)─┬─auditd(1165)───{auditd}(1166)
        ├─crond(1356)
        ├─master(1342)─┬─pickup(1350)
        │              └─qmgr(1351)
        ├─mingetty(1369)
        ├─mingetty(1371)
        ├─mingetty(1373)
        ├─mingetty(1375)
        ├─mingetty(1377)
        ├─mingetty(1379)
        ├─rsyslogd(1187)─┬─{rsyslogd}(1188)
        │                ├─{rsyslogd}(1190)
        │                └─{rsyslogd}(1191)
        ├─sshd(1263)─┬─sshd(1381)───bash(1383)───pstree(1405)
        │            └─sshd(1384)───sftp-server(1400)
        └─udevd(483)─┬─udevd(773)
                     └─udevd(783)
[root@centos6 ~]# cat /etc/inittab
# inittab is only used by upstart for the default runlevel.
#
# ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
#
# System initialization is started by /etc/init/rcS.conf
#
# Individual runlevels are started by /etc/init/rc.conf
#
# Ctrl-Alt-Delete is handled by /etc/init/control-alt-delete.conf
#
# Terminal gettys are handled by /etc/init/tty.conf and /etc/init/serial.conf,
# with configuration in /etc/sysconfig/init.
#
# For information on how to write upstart event handlers, or how
# upstart works, see init(5), init(8), and initctl(8).
#
# Default runlevel. The runlevels used are:
#   0 - halt (Do NOT set initdefault to this)
#   1 - Single user mode
#   2 - Multiuser, without NFS (The same as 3, if you do not have networking)
#   3 - Full multiuser mode
#   4 - unused
#   5 - X11
#   6 - reboot (Do NOT set initdefault to this)
# 
id:3:initdefault: #Set which mode to start after startup

[root@centos5 ~]# cat /etc/inittab 
#
# inittab       This file describes how the INIT process should set up
#               the system in a certain run-level.
#
# Author:       Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org>
#               Modified for RHS Linux by Marc Ewing and Donnie Barnes
#

# Default runlevel. The runlevels used by RHS are:
#   0 - halt (Do NOT set initdefault to this)
#   1 - Single user mode
#   2 - Multiuser, without NFS (The same as 3, if you do not have networking)
#   3 - Full multiuser mode
#   4 - unused
#   5 - X11
#   6 - reboot (Do NOT set initdefault to this)
# 
id:5:initdefault:

# System initialization.
si::sysinit:/etc/rc.d/rc.sysinit

l0:0:wait:/etc/rc.d/rc 0
l1:1:wait:/etc/rc.d/rc 1
l2:2:wait:/etc/rc.d/rc 2
l3:3:wait:/etc/rc.d/rc 3
l4:4:wait:/etc/rc.d/rc 4
l5:5:wait:/etc/rc.d/rc 5
l6:6:wait:/etc/rc.d/rc 6

# Trap CTRL-ALT-DELETE
ca::ctrlaltdel:/sbin/shutdown -t3 -r now

# When our UPS tells us power has failed, assume we have a few minutes
# of power left.  Schedule a shutdown for 2 minutes from now.
# This does, of course, assume you have powerd installed and your
# UPS connected and working correctly.  
pf::powerfail:/sbin/shutdown -f -h +2 "Power Failure; System Shutting Down"

# If power was restored before the shutdown kicked in, cancel it.
pr:12345:powerokwait:/sbin/shutdown -c "Power Restored; Shutdown Cancelled"


# Run gettys in standard runlevels
1:2345:respawn:/sbin/mingetty tty1
2:2345:respawn:/sbin/mingetty tty2
3:2345:respawn:/sbin/mingetty tty3
4:2345:respawn:/sbin/mingetty tty4
5:2345:respawn:/sbin/mingetty tty5
6:2345:respawn:/sbin/mingetty tty6

# Run xdm in runlevel 5
x:5:respawn:/etc/X11/prefdm -nodaemon

1.3.5.2 initialization script sysinit

[root@centos6 ~]# file /etc/rc.d/rc.sysinit
/etc/rc.d/rc.sysinit: Bourne-Again shell script text executable

[root@centos6 ~]# cat /etc/rc.d/rc.sysinit
#!/bin/bash
#
# /etc/rc.d/rc.sysinit - run once at boot time
#
# Taken in part from Miquel van Smoorenburg's bcheckrc.
#

HOSTNAME=$(/bin/hostname)

set -m

if [ -f /etc/sysconfig/network ]; then
    . /etc/sysconfig/network
fi
if [ -z "$HOSTNAME" -o "$HOSTNAME" = "(none)" ]; then
    HOSTNAME=localhost
fi

if [ ! -e /proc/mounts ]; then
	mount -n -t proc /proc /proc
	mount -n -t sysfs /sys /sys >/dev/null 2>&1
fi
if [ ! -d /proc/bus/usb ]; then
	modprobe usbcore >/dev/null 2>&1 && mount -n -t usbfs /proc/bus/usb /proc/bus/usb
else
	mount -n -t usbfs /proc/bus/usb /proc/bus/usb
fi

#remount /dev/shm to set attributes from fstab #669700
mount -n -o remount /dev/shm >/dev/null 2>&1
#remount /proc to set attributes from fstab #984003
mount -n -o remount /proc >/dev/null 2>&1

. /etc/init.d/functions

PLYMOUTH=
[ -x /bin/plymouth ] && PLYMOUTH=yes

# Check SELinux status
SELINUX_STATE=
if [ -e "/selinux/enforce" ] && [ "$(cat /proc/self/attr/current)" != "kernel" ]; then
	if [ -r "/selinux/enforce" ] ; then
		SELINUX_STATE=$(cat "/selinux/enforce")
	else
		# assume enforcing if you can't read it
		SELINUX_STATE=1
	fi
fi

if [ -n "$SELINUX_STATE" -a -x /sbin/restorecon ] && __fgrep " /dev " /proc/mounts >/dev/null 2>&1 ; then
	/sbin/restorecon -R -F /dev 2>/dev/null
fi

disable_selinux() {
	echo $"*** Warning -- SELinux is active"
	echo $"*** Disabling security enforcement for system recovery."
	echo $"*** Run 'setenforce 1' to reenable."
	echo "0" > "/selinux/enforce"
}

relabel_selinux() {
    # if /sbin/init is not labeled correctly this process is running in the
    # wrong context, so a reboot will be required after relabel
    AUTORELABEL=
    . /etc/selinux/config
    echo "0" > /selinux/enforce
    [ -n "$PLYMOUTH" ] && plymouth --hide-splash

    if [ "$AUTORELABEL" = "0" ]; then
	echo
	echo $"*** Warning -- SELinux ${SELINUXTYPE} policy relabel is required. "
	echo $"*** /etc/selinux/config indicates you want to manually fix labeling"
	echo $"*** problems. Dropping you to a shell; the system will reboot"
	echo $"*** when you leave the shell."
	start rcS-emergency

    else
	echo
	echo $"*** Warning -- SELinux ${SELINUXTYPE} policy relabel is required."
	echo $"*** Relabeling could take a very long time, depending on file"
	echo $"*** system size and speed of hard drives."

	/sbin/fixfiles -F restore > /dev/null 2>&1
    fi
    rm -f  /.autorelabel
    # at this point fsck was already executed see bz1236062
    [ -f /forcefsck ] && rm -f /forcefsck

    echo $"Unmounting file systems"
    umount -a
    mount -n -o remount,ro /
    echo $"Automatic reboot in progress."
    reboot -f
}

# Print a text banner.
echo -en $"\t\tWelcome to "
read -r system_release < /etc/system-release
if [[ "$system_release" == *"Red Hat"* ]]; then
 [ "$BOOTUP" = "color" ] && echo -en "\\033[0;31m"
 echo -en "Red Hat"
 [ "$BOOTUP" = "color" ] && echo -en "\\033[0;39m"
 PRODUCT=$(sed "s/Red Hat \(.*\) release.*/\1/" /etc/system-release)
 echo " $PRODUCT"
elif [[ "$system_release" == *Fedora* ]]; then
 [ "$BOOTUP" = "color" ] && echo -en "\\033[0;34m"
 echo -en "Fedora"
 [ "$BOOTUP" = "color" ] && echo -en "\\033[0;39m"
 PRODUCT=$(sed "s/Fedora \(.*\) \?release.*/\1/" /etc/system-release)
 echo " $PRODUCT"
elif [[ "$system_release" =~ "CentOS" ]]; then
 [ "$BOOTUP" = "color" ] && echo -en "\\033[0;36m"
 echo -en "CentOS"
 [ "$BOOTUP" = "color" ] && echo -en "\\033[0;39m"
 PRODUCT=$(sed "s/CentOS \(.*\) \?release.*/\1/" /etc/system-release)
 echo " $PRODUCT"
else
 PRODUCT=$(sed "s/ release.*//g" /etc/system-release)
 echo "$PRODUCT"
fi

# Only read this once.
cmdline=$(cat /proc/cmdline)

# Initialize hardware
if [ -f /proc/sys/kernel/modprobe ]; then
   if ! strstr "$cmdline" nomodules && [ -f /proc/modules ] ; then
       sysctl -w kernel.modprobe="/sbin/modprobe" >/dev/null 2>&1
   else
       # We used to set this to NULL, but that causes 'failed to exec' messages"
       sysctl -w kernel.modprobe="/bin/true" >/dev/null 2>&1
   fi
fi

touch /dev/.in_sysinit >/dev/null 2>&1

# Set default affinity
if [ -x /bin/taskset ]; then
   if strstr "$cmdline" default_affinity= ; then
     for arg in $cmdline ; do
         if [ "${arg##default_affinity=}" != "${arg}" ]; then
             /bin/taskset -p ${arg##default_affinity=} 1
             /bin/taskset -p ${arg##default_affinity=} $$
         fi
     done
   fi
fi

nashpid=$(pidof nash 2>/dev/null)
[ -n "$nashpid" ] && kill $nashpid >/dev/null 2>&1
unset nashpid

apply_sysctl

/sbin/start_udev

# Load other user-defined modules
for file in /etc/sysconfig/modules/*.modules ; do
  [ -x $file ] && $file
done

# Load modules (for backward compatibility with VARs)
if [ -f /etc/rc.modules ]; then
	/etc/rc.modules
fi

mount -n /dev/pts >/dev/null 2>&1
[ -n "$SELINUX_STATE" ] && restorecon -F /dev/pts >/dev/null 2>&1

# Configure kernel parameters
update_boot_stage RCkernelparam
apply_sysctl

# Set the hostname.
update_boot_stage RChostname
action $"Setting hostname ${HOSTNAME}: " hostname ${HOSTNAME}
[ -n "${NISDOMAIN}" ] && domainname ${NISDOMAIN}

# Sync waiting for storage.
{ rmmod scsi_wait_scan ; modprobe scsi_wait_scan ; rmmod scsi_wait_scan ; } >/dev/null 2>&1

# Device mapper & related initialization
if ! __fgrep "device-mapper" /proc/devices >/dev/null 2>&1 ; then
       modprobe dm-mod >/dev/null 2>&1
fi

if [ -f /etc/crypttab ]; then
    init_crypto 0
fi

if ! strstr "$cmdline" nompath && [ -f /etc/multipath.conf -a \
		-x /sbin/multipath ]; then
	modprobe dm-multipath > /dev/null 2>&1
	/sbin/multipath -v 0
	if [ -x /sbin/kpartx ]; then
                action_silent $"Add partition mappings: " /sbin/dmsetup ls --target multipath --exec "/sbin/kpartx -a -p p" 
	fi
fi

if ! strstr "$cmdline" nodmraid && [ -x /sbin/dmraid ]; then
	modprobe dm-mirror >/dev/null 2>&1
	dmraidsets=$(LC_ALL=C /sbin/dmraid -s -c -i)
	if [ "$?" = "0" ]; then
		for dmname in $dmraidsets; do
			if [[ "$dmname" == isw_* ]] && \
			   ! strstr "$cmdline" noiswmd; then
				continue
			fi
                        action_silent $"Activate software (ATA)RAID: " /sbin/dmraid -ay -i --rm_partitions -p "$dmname"
			/sbin/kpartx -a -p p "/dev/mapper/$dmname"
		done
	fi
fi

# Start any MD RAID arrays that haven't been started yet
[ -r /proc/mdstat -a -r /dev/md/md-device-map ] && action $"Run MD devices: " /sbin/mdadm -IRs

if [ -x /sbin/lvm ]; then
	if [ ! -f /.nolvm ] && ! strstr "$cmdline" nolvm ; then
		action $"Setting up Logical Volume Management:" /sbin/lvm vgchange -a ay --sysinit --ignoreskippedcluster
	else
		echo $"Logical Volume Management disabled at boot."
	fi
fi

if [ -f /etc/crypttab ]; then
    init_crypto 0
fi

if [ -f /fastboot ] || strstr "$cmdline" fastboot ; then
	fastboot=yes
fi

if [ -f /fsckoptions ]; then
	fsckoptions=$(cat /fsckoptions)
fi

if [ -f /forcefsck ] || strstr "$cmdline" forcefsck ; then
	fsckoptions="-f $fsckoptions"
elif [ -f /.autofsck ]; then
	[ -f /etc/sysconfig/autofsck ] && . /etc/sysconfig/autofsck
	if [ "$AUTOFSCK_DEF_CHECK" = "yes" ]; then
		AUTOFSCK_OPT="$AUTOFSCK_OPT -f"
	fi
	if [ -n "$AUTOFSCK_SINGLEUSER" ]; then
		[ -n "$PLYMOUTH" ] && plymouth --hide-splash
		echo
		echo $"*** Warning -- the system did not shut down cleanly. "
		echo $"*** Dropping you to a shell; the system will continue"
		echo $"*** when you leave the shell."
		[ -n "$SELINUX_STATE" ] && echo "0" > /selinux/enforce
		start rcS-emergency
		[ -n "$SELINUX_STATE" ] && echo "1" > /selinux/enforce
		[ -n "$PLYMOUTH" ] && plymouth --show-splash
	fi
	fsckoptions="$AUTOFSCK_OPT $fsckoptions"
fi

if [ "$BOOTUP" = "color" ]; then
	fsckoptions="-C $fsckoptions"
else
	fsckoptions="-V $fsckoptions"
fi

READONLY=
if [ -f /etc/sysconfig/readonly-root ]; then
	. /etc/sysconfig/readonly-root
fi
if strstr "$cmdline" readonlyroot ; then
	READONLY=yes
	[ -z "$RW_MOUNT" ] && RW_MOUNT=/var/lib/stateless/writable
	[ -z "$STATE_MOUNT" ] && STATE_MOUNT=/var/lib/stateless/state
fi
if strstr "$cmdline" noreadonlyroot ; then
	READONLY=no
fi

if [ "$READONLY" = "yes" -o "$TEMPORARY_STATE" = "yes" ]; then

	mount_empty() {
		if [ -e "$1" ]; then
			echo "$1" | cpio -p -vd "$RW_MOUNT" &>/dev/null
			mount -n --bind "$RW_MOUNT$1" "$1"
		fi
	}

	mount_dirs() {
		if [ -e "$1" ]; then
			mkdir -p "$RW_MOUNT$1"
			find "$1" -type d -print0 | cpio -p -0vd "$RW_MOUNT" &>/dev/null
			mount -n --bind "$RW_MOUNT$1" "$1"
		fi
	}

	mount_files() {
		if [ -e "$1" ]; then
			cp -a --parents "$1" "$RW_MOUNT"
			mount -n --bind "$RW_MOUNT$1" "$1"
		fi
	}

	# Common mount options for scratch space regardless of
	# type of backing store
	mountopts=

	# Scan partitions for local scratch storage
	rw_mount_dev=$(blkid -t LABEL="$RW_LABEL" -l -o device)

	# First try to mount scratch storage from /etc/fstab, then any
	# partition with the proper label.  If either succeeds, be sure
	# to wipe the scratch storage clean.  If both fail, then mount
	# scratch storage via tmpfs.
	if mount $mountopts "$RW_MOUNT" > /dev/null 2>&1 ; then
		rm -rf "$RW_MOUNT" > /dev/null 2>&1
	elif [ x$rw_mount_dev != x ] && mount $rw_mount_dev $mountopts "$RW_MOUNT" > /dev/null 2>&1; then
		rm -rf "$RW_MOUNT"  > /dev/null 2>&1
	else
		mount -n -t tmpfs $RW_OPTIONS $mountopts none "$RW_MOUNT"
	fi

	for file in /etc/rwtab /etc/rwtab.d/* /dev/.initramfs/rwtab ; do
		is_ignored_file "$file" && continue
	[ -f $file ] && cat $file | while read type path ; do
			case "$type" in
				empty)
					mount_empty $path
					;;
				files)
					mount_files $path
					;;
				dirs)
					mount_dirs $path
					;;
				*)
					;;
			esac
			[ -n "$SELINUX_STATE" -a -e "$path" ] && restorecon -R "$path"
		done
	done

	# Use any state passed by initramfs
	[ -d /dev/.initramfs/state ] && cp -a /dev/.initramfs/state/* $RW_MOUNT

	# In theory there should be no more than one network interface active
	# this early in the boot process -- the one we're booting from.
	# Use the network address to set the hostname of the client.  This
	# must be done even if we have local storage.
	ipaddr=
	if [ "$HOSTNAME" = "localhost" -o "$HOSTNAME" = "localhost.localdomain" ]; then
		ipaddr=$(ip addr show to 0.0.0.0/0 scope global | awk '/[[:space:]]inet / { print gensub("/.*","","g",$2) }')
		for ip in $ipaddr ; do
			HOSTNAME=
			eval $(ipcalc -h $ip 2>/dev/null)
			[ -n "$HOSTNAME" ] && { hostname ${HOSTNAME} ; break; }
		done
	fi
	
	# Clients with read-only root filesystems may be provided with a
	# place where they can place minimal amounts of persistent
	# state.  SSH keys or puppet certificates for example.
	#
	# Ideally we'll use puppet to manage the state directory and to
	# create the bind mounts.  However, until that's all ready this
	# is sufficient to build a working system.

	# First try to mount persistent data from /etc/fstab, then any
	# partition with the proper label, then fallback to NFS
	state_mount_dev=$(blkid -t LABEL="$STATE_LABEL" -l -o device)
	if mount $mountopts $STATE_OPTIONS "$STATE_MOUNT" > /dev/null 2>&1 ; then
		/bin/true
	elif [ x$state_mount_dev != x ] && mount $state_mount_dev $mountopts "$STATE_MOUNT" > /dev/null 2>&1;  then
		/bin/true
	elif [ ! -z "$CLIENTSTATE" ]; then
		# No local storage was found.  Make a final attempt to find
		# state on an NFS server.

		mount -t nfs $CLIENTSTATE/$HOSTNAME $STATE_MOUNT -o rw,nolock
	fi

	if [ -w "$STATE_MOUNT" ]; then

		mount_state() {
			if [ -e "$1" ]; then
				[ ! -e "$STATE_MOUNT$1" ] && cp -a --parents "$1" "$STATE_MOUNT"
				mount -n --bind "$STATE_MOUNT$1" "$1"
			fi
		}

		for file in /etc/statetab /etc/statetab.d/* ; do
			is_ignored_file "$file" && continue
			[ ! -f "$file" ] && continue

			if [ -f "$STATE_MOUNT/$file" ] ; then
				mount -n --bind "$STATE_MOUNT/$file" "$file"
			fi

			for path in $(grep -v "^#" "$file" 2>/dev/null); do
				mount_state "$path"
				[ -n "$SELINUX_STATE" -a -e "$path" ] && restorecon -R "$path"
			done
		done

		if [ -f "$STATE_MOUNT/files" ] ; then
			for path in $(grep -v "^#" "$STATE_MOUNT/files" 2>/dev/null); do
				mount_state "$path"
				[ -n "$SELINUX_STATE" -a -e "$path" ] && restorecon -R "$path"
			done
		fi
	fi

        if mount | grep -q /var/lib/nfs/rpc_pipefs ; then
                mount -t rpc_pipefs sunrpc /var/lib/nfs/rpc_pipefs && service rpcidmapd restart
        fi
fi

if [[ " $fsckoptions" != *" -y"* ]]; then
	fsckoptions="-a $fsckoptions"
fi

_RUN_QUOTACHECK=0
if [ -f /forcequotacheck ] || strstr "$cmdline" forcequotacheck ; then
	_RUN_QUOTACHECK=1
fi
if [ -z "$fastboot" -a "$READONLY" != "yes" ]; then

        STRING=$"Checking filesystems"
	echo $STRING
	fsck -T -t noopts=_netdev -A $fsckoptions
	rc=$?
	
	if [ "$rc" -eq "0" ]; then
		success "$STRING"
		echo
	elif [ "$rc" -eq "1" ]; then
	        passed "$STRING"
		echo
	elif [ "$rc" -eq "2" -o "$rc" -eq "3" ]; then
		echo $"Unmounting file systems"
		umount -a
		mount -n -o remount,ro /
		echo $"Automatic reboot in progress."
		reboot -f
        fi
	
        # A return of 4 or higher means there were serious problems.
	if [ $rc -gt 1 ]; then
		[ -n "$PLYMOUTH" ] && plymouth --hide-splash

		failure "$STRING"
		echo
		echo
		echo $"*** An error occurred during the file system check."
		echo $"*** Dropping you to a shell; the system will reboot"
		echo $"*** when you leave the shell."

                str=$"(Repair filesystem)"
		PS1="$str \# # "; export PS1
		[ "$SELINUX_STATE" = "1" ] && disable_selinux
		start rcS-emergency

		echo $"Unmounting file systems"
		umount -a
		mount -n -o remount,ro /
		echo $"Automatic reboot in progress."
		reboot -f
	elif [ "$rc" -eq "1" ]; then
		_RUN_QUOTACHECK=1
	fi
fi

remount_needed() {
  local state oldifs
  [ "$READONLY" = "yes" ] && return 1
  state=$(LC_ALL=C awk '/ \/ / && ($3 !~ /rootfs/) { print $4 }' /proc/mounts)
  oldifs=$IFS
  IFS=","
  for opt in $state ; do
	if [ "$opt" = "rw" ]; then
		IFS=$oldifs
		return 1
	fi
  done
  IFS=$oldifs
  return 0
}

# Remount the root filesystem read-write.
update_boot_stage RCmountfs
if remount_needed ; then
  action $"Remounting root filesystem in read-write mode: " mount -n -o remount,rw /
fi

# Clean up SELinux labels
if [ -n "$SELINUX_STATE" ]; then
   restorecon /etc/mtab /etc/ld.so.cache /etc/blkid/blkid.tab /etc/resolv.conf >/dev/null 2>&1
fi

# If relabeling, relabel mount points.
if [ -n "$SELINUX_STATE" -a "$READONLY" != "yes" ]; then
    if [ -f /.autorelabel ] || strstr "$cmdline" autorelabel ; then
	restorecon $(awk '!/^#/ && $4 !~ /noauto/ && $2 ~ /^\// { print $2 }' /etc/fstab) >/dev/null 2>&1
    fi
fi

if [ "$READONLY" != "yes" ] ; then
	# Clear mtab
	(> /etc/mtab) &> /dev/null

	# Remove stale backups
	rm -f /etc/mtab~ /etc/mtab~~

	# Enter mounted filesystems into /etc/mtab
	mount -f /
	mount -f /proc >/dev/null 2>&1
	mount -f /sys >/dev/null 2>&1
	mount -f /dev/pts >/dev/null 2>&1
	mount -f /dev/shm >/dev/null 2>&1
	mount -f /proc/bus/usb >/dev/null 2>&1
fi

# Mount all other filesystems (except for NFS and /proc, which is already
# mounted). Contrary to standard usage,
# filesystems are NOT unmounted in single user mode.
# The 'no' applies to all listed filesystem types. See mount(8).
if [ "$READONLY" != "yes" ] ; then
	action $"Mounting local filesystems: " mount -a -t nonfs,nfs4,smbfs,ncpfs,cifs,gfs,gfs2,glusterfs -O no_netdev
else
	action $"Mounting local filesystems: " mount -a -n -t nonfs,nfs4,smbfs,ncpfs,cifs,gfs,gfs2,glusterfs -O no_netdev
fi

# Check to see if a full relabel is needed
if [ -n "$SELINUX_STATE" -a "$READONLY" != "yes" ]; then
    if [ -f /.autorelabel ] || strstr "$cmdline" autorelabel ; then
	relabel_selinux
    fi
else
    if [ -d /etc/selinux -a "$READONLY" != "yes" ]; then
        [ -f /.autorelabel ] || touch /.autorelabel
    fi
fi

# Update quotas if necessary
if [ X"$_RUN_QUOTACHECK" = X1 -a -x /sbin/quotacheck ]; then
	action $"Checking local filesystem quotas: " /sbin/quotacheck -anug
fi

if [ -x /sbin/quotaon ]; then
    action $"Enabling local filesystem quotas: " /sbin/quotaon -aug
fi

# Initialize pseudo-random number generator
if [ -f "/var/lib/random-seed" ]; then
	cat /var/lib/random-seed > /dev/urandom
else
	[ "$READONLY" != "yes" ] && touch /var/lib/random-seed
fi
if [ "$READONLY" != "yes" ]; then
	chmod 600 /var/lib/random-seed
	dd if=/dev/urandom of=/var/lib/random-seed count=1 bs=4096 2>/dev/null
fi

if [ -f /etc/crypttab ]; then
    init_crypto 1
fi

# Configure machine if necessary.
if [ -f /.unconfigured ]; then

    if [ -x /bin/plymouth ]; then
        /bin/plymouth quit
    fi

    if [ -x /usr/bin/system-config-keyboard ]; then
	/usr/bin/system-config-keyboard
    fi
    if [ -x /usr/bin/passwd ]; then
        /usr/bin/passwd root
    fi
    if [ -x /usr/sbin/system-config-network-tui ]; then
	/usr/sbin/system-config-network-tui
    fi
    if [ -x /usr/sbin/timeconfig ]; then
	/usr/sbin/timeconfig
    fi
    if [ -x /usr/sbin/authconfig-tui ]; then
	/usr/sbin/authconfig-tui --nostart
    fi
    if [ -x /usr/sbin/ntsysv ]; then
	/usr/sbin/ntsysv --level 35
    fi

    # Reread in network configuration data.
    if [ -f /etc/sysconfig/network ]; then
	. /etc/sysconfig/network

	# Reset the hostname.
	action $"Resetting hostname ${HOSTNAME}: " hostname ${HOSTNAME}
    fi

    rm -f /.unconfigured
fi

# Clean out /.
rm -f /fastboot /fsckoptions /forcefsck /.autofsck /forcequotacheck /halt \
	/poweroff /.suspended &> /dev/null

# Do we need (w|u)tmpx files? We don't set them up, but the sysadmin might...
_NEED_XFILES=
[ -f /var/run/utmpx -o -f /var/log/wtmpx ] && _NEED_XFILES=1

# Clean up /var.
rm -rf /var/lock/cvs/* /var/run/screen/*
find /var/lock /var/run ! -type d -exec rm -f {} \;
rm -f /var/lib/rpm/__db* &> /dev/null
rm -f /var/gdm/.gdmfifo &> /dev/null

[ "$PROMPT" != no ] && plymouth watch-keystroke --command "touch /var/run/confirm" --keys=Ii &

# Clean up utmp/wtmp
> /var/run/utmp
touch /var/log/wtmp
chgrp utmp /var/run/utmp /var/log/wtmp
chmod 0664 /var/run/utmp /var/log/wtmp
if [ -n "$_NEED_XFILES" ]; then
  > /var/run/utmpx
  touch /var/log/wtmpx
  chgrp utmp /var/run/utmpx /var/log/wtmpx
  chmod 0664 /var/run/utmpx /var/log/wtmpx
fi
[ -n "$SELINUX_STATE" ] && restorecon /var/run/utmp* /var/log/wtmp* >/dev/null 2>&1

# Clean up various /tmp bits
[ -n "$SELINUX_STATE" ] && restorecon /tmp
rm -f /tmp/.X*-lock /tmp/.lock.* /tmp/.gdm_socket /tmp/.s.PGSQL.*
rm -rf /tmp/.X*-unix /tmp/.ICE-unix /tmp/.font-unix /tmp/hsperfdata_* \
       /tmp/kde-* /tmp/ksocket-* /tmp/mc-* /tmp/mcop-* /tmp/orbit-*  \
       /tmp/scrollkeeper-*  /tmp/ssh-* \
       /dev/.in_sysinit

# Make ICE directory
mkdir -m 1777 -p /tmp/.ICE-unix >/dev/null 2>&1
chown root:root /tmp/.ICE-unix
[ -n "$SELINUX_STATE" ] && restorecon /tmp/.ICE-unix >/dev/null 2>&1

# Start up swapping.
update_boot_stage RCswap
action $"Enabling /etc/fstab swaps: " swapon -a -e
if [ "$AUTOSWAP" = "yes" ]; then
	curswap=$(awk '/^\/dev/ { print $1 }' /proc/swaps | while read x; do get_numeric_dev dec $x ; echo -n " "; done)
	swappartitions=$(blkid -t TYPE=swap -o device)
	if [ x"$swappartitions" != x ]; then
		for partition in $swappartitions ; do
			[ ! -e $partition ] && continue
			majmin=$(get_numeric_dev dec $partition)
			echo $curswap | grep -qw "$majmin" || action $"Enabling local swap partitions: " swapon $partition
		done
	fi
fi

# Set up binfmt_misc
/bin/mount -t binfmt_misc none /proc/sys/fs/binfmt_misc > /dev/null 2>&1

# Boot time profiles. Yes, this should be somewhere else.
if [ -x /usr/sbin/system-config-network-cmd ]; then
  if strstr "$cmdline" netprofile= ; then
    for arg in $cmdline ; do
        if [ "${arg##netprofile=}" != "${arg}" ]; then
	    /usr/sbin/system-config-network-cmd --profile ${arg##netprofile=}
        fi
    done
  fi
fi

# Now that we have all of our basic modules loaded and the kernel going,
# let's dump the syslog ring somewhere so we can find it later
[ -f /var/log/dmesg ] && mv -f /var/log/dmesg /var/log/dmesg.old
dmesg -s 131072 > /var/log/dmesg

# create the crash indicator flag to warn on crashes, offer fsck with timeout
touch /.autofsck &> /dev/null

[ "$PROMPT" != no ] && plymouth --ignore-keystroke=Ii
if strstr "$cmdline" confirm ; then
	touch /var/run/confirm
fi

# Let rhgb know that we're leaving rc.sysinit
if [ -x /bin/plymouth ]; then
    /bin/plymouth --sysinit
fi

System initialization script function

(1) Set host name
(2) Set welcome message
(3) activation udev and selinux
(4) mount /etc/fstab File system defined in file
(5) Detect the root file system and remount the root file system in read-write mode
(6) Set system clock
(7) activation swap equipment
(8) according to/etc/sysctl.conf File to set kernel parameters
(9) activation lvm and software raid equipment
(10)Load drivers for additional devices
(11)Cleaning operation

example:

[root@centos6 ~]# cat /etc/init/rcS.conf
# rcS - runlevel compatibility
#
# This task runs the old sysv-rc startup scripts.
#
# Do not edit this file directly. If you want to change the behaviour,
# please create a file rcS.override and put your changes there.

start on startup

stop on runlevel

task

# Note: there can be no previous runlevel here, if we have one it's bad
# information (we enter rc1 not rcS for maintenance).  Run /etc/rc.d/rc
# without information so that it defaults to previous=N runlevel=S.
console output
pre-start script
	for t in $(cat /proc/cmdline); do
		case $t in
			emergency)
				start rcS-emergency
				break
			;;
		esac
	done
end script
exec /etc/rc.d/rc.sysinit
post-stop script
	if [ "$UPSTART_EVENTS" = "startup" ]; then
		[ -f /etc/inittab ] && runlevel=$(/bin/awk -F ':' '$3 == "initdefault" && $1 !~ "^#" { print $2 }' /etc/inittab)
		[ -z "$runlevel" ] && runlevel="3"
		for t in $(cat /proc/cmdline); do
			case $t in
				-s|single|S|s) runlevel="S" ;;
				[1-9])       runlevel="$t" ;;
			esac
		done
		exec telinit $runlevel
	fi
end script

1.3.5.3 service management

[root@centos6 ~]# cat /etc/init/rc.conf
# rc - System V runlevel compatibility
#
# This task runs the old sysv-rc runlevel scripts.  It
# is usually started by the telinit compatibility wrapper.
#
# Do not edit this file directly. If you want to change the behaviour,
# please create a file rc.override and put your changes there.

start on runlevel [0123456]

stop on runlevel [!$RUNLEVEL]

task

export RUNLEVEL
console output
exec /etc/rc.d/rc $RUNLEVEL

[root@centos6 ~]# file /etc/rc.d/rc
/etc/rc.d/rc: Bourne-Again shell script text executable
[root@centos6 ~]# cat /etc/rc.d/rc
#! /bin/bash
#
# rc            This file is responsible for starting/stopping
#               services when the runlevel changes.
#
# Original Author:       
#               Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org>
#

set -m

# check a file to be a correct runlevel script
check_runlevel ()
{
	# Check if the file exists at all.
	[ -x "$1" ] || return 1
	is_ignored_file "$1" && return 1
	return 0
}

# Now find out what the current and what the previous runlevel are.
argv1="$1"		#Run according to this $1 parameter
set $(/sbin/runlevel)
runlevel=$2
previous=$1
export runlevel previous

. /etc/init.d/functions

export CONSOLETYPE
do_confirm="no"
if [ -f /var/run/confirm ]; then
	do_confirm="yes"
fi
UPSTART=
[ -x /sbin/initctl ] && UPSTART=yes
# See if we want to be in user confirmation mode
if [ "$previous" = "N" ]; then
	if [ "$do_confirm" = "yes" ]; then
		echo $"Entering interactive startup"
	else
		echo $"Entering non-interactive startup"
	fi
fi

# Get first argument. Set new runlevel to this argument.
[ -n "$argv1" ] && runlevel="$argv1"

# Is there an rc directory for this new runlevel?
[ -d /etc/rc$runlevel.d ] || exit 0

# Set language, vc settings once to avoid doing it for every init script
# through functions
if [ -f /etc/sysconfig/i18n -a -z "${NOLOCALE:-}" ] ; then
  . /etc/profile.d/lang.sh 2>/dev/null
  export LANGSH_SOURCED=1
fi

# First, run the KILL scripts.		#Run this code first
for i in /etc/rc$runlevel.d/K* ; do

	# Check if the subsystem is already up.
	subsys=${i#/etc/rc$runlevel.d/K??}
	[ -f /var/lock/subsys/$subsys -o -f /var/lock/subsys/$subsys.init ] || continue
	check_runlevel "$i" || continue

	# Bring the subsystem down.
	[ -n "$UPSTART" ] && initctl emit --quiet stopping JOB=$subsys
	$i stop
	[ -n "$UPSTART" ] && initctl emit --quiet stopped JOB=$subsys
done

# Now run the START scripts.		#Run this code again
for i in /etc/rc$runlevel.d/S* ; do

	# Check if the subsystem is already up.
	subsys=${i#/etc/rc$runlevel.d/S??}
	[ -f /var/lock/subsys/$subsys ] && continue
	[ -f /var/lock/subsys/$subsys.init ] && continue
	check_runlevel "$i" || continue
		    
	# If we're in confirmation mode, get user confirmation
	if [ "$do_confirm" = "yes" ]; then
		confirm $subsys
		rc=$?
		if [ "$rc" = "1" ]; then
			continue
		elif [ "$rc" = "2" ]; then
			do_confirm="no"
		fi
	fi

	update_boot_stage "$subsys"
	# Bring the subsystem up.
	[ -n "$UPSTART" ] && initctl emit --quiet starting JOB=$subsys
	if [ "$subsys" = "halt" -o "$subsys" = "reboot" ]; then
		export LC_ALL=C
		exec $i start
	fi
	$i start
	[ -n "$UPSTART" ] && initctl emit --quiet started JOB=$subsys
done
[ "$do_confirm" = "yes" ] && rm -f /var/run/confirm
exit 0

[root@centos6 ~]# ls /etc/rc*.d -d
/etc/rc0.d  /etc/rc1.d  /etc/rc2.d  /etc/rc3.d  /etc/rc4.d  /etc/rc5.d  /etc/rc6.d  /etc/rc.d
[root@centos6 ~]# ls /etc/rc3.d
K10saslauthd  K87restorecond  K89rdisc  S08ip6tables  S10network  S12rsyslog    S25blk-availability  S26udev-post  S55sshd     S90crond
K61nfs-rdma   K89netconsole   S05rdma   S08iptables   S11auditd   S15mdmonitor  S25netfs             S50kdump      S80postfix  S99local

[root@centos6 ~]# ll /etc/rc.d/rc3.d
total 0
lrwxrwxrwx. 1 root root 19 Oct  6 23:45 K10saslauthd -> ../init.d/saslauthd
lrwxrwxrwx. 1 root root 18 Oct  6 23:46 K61nfs-rdma -> ../init.d/nfs-rdma
lrwxrwxrwx. 1 root root 21 Oct  6 23:45 K87restorecond -> ../init.d/restorecond
lrwxrwxrwx. 1 root root 20 Oct  6 23:45 K89netconsole -> ../init.d/netconsole
lrwxrwxrwx. 1 root root 15 Oct  6 23:45 K89rdisc -> ../init.d/rdisc
lrwxrwxrwx. 1 root root 14 Oct  6 23:46 S05rdma -> ../init.d/rdma
lrwxrwxrwx. 1 root root 19 Oct  6 23:46 S08ip6tables -> ../init.d/ip6tables
lrwxrwxrwx. 1 root root 18 Oct  6 23:45 S08iptables -> ../init.d/iptables
lrwxrwxrwx. 1 root root 17 Oct  6 23:45 S10network -> ../init.d/network
lrwxrwxrwx. 1 root root 16 Oct  6 23:46 S11auditd -> ../init.d/auditd
lrwxrwxrwx. 1 root root 17 Oct  6 23:45 S12rsyslog -> ../init.d/rsyslog
lrwxrwxrwx. 1 root root 19 Oct  6 23:45 S15mdmonitor -> ../init.d/mdmonitor
lrwxrwxrwx. 1 root root 26 Oct  6 23:45 S25blk-availability -> ../init.d/blk-availability
lrwxrwxrwx. 1 root root 15 Oct  6 23:45 S25netfs -> ../init.d/netfs
lrwxrwxrwx. 1 root root 19 Oct  6 23:45 S26udev-post -> ../init.d/udev-post
lrwxrwxrwx. 1 root root 15 Oct  6 23:46 S50kdump -> ../init.d/kdump
lrwxrwxrwx. 1 root root 14 Oct  6 23:46 S55sshd -> ../init.d/sshd
lrwxrwxrwx. 1 root root 17 Oct  6 23:45 S80postfix -> ../init.d/postfix
lrwxrwxrwx. 1 root root 15 Oct  6 23:45 S90crond -> ../init.d/crond
lrwxrwxrwx. 1 root root 11 Oct  6 23:45 S99local -> ../rc.local

service command: manually manage services

service service start|stop|restart
service --status-all

/etc/rc.d/rc controls the startup and automatic operation of the service script

for srv in /etc/rc.d/rcN.d/K*; do
    $srv stop
done
for srv in /etc/rc.d/rcN.d/S*; do
    $srv start
done

Note: RC n -- > means reading / etc/rc.d/rcN.d/

K: K ##: ## operation sequence; The smaller the number, the earlier the operation; Services with smaller numbers are usually dependent on other services

S: S ##: ## operation sequence; The smaller the number, the earlier the operation; Services with smaller numbers are usually dependent services

Configure service startup

  • chkconfig command
  • ntsysv command

chkconfig command management service

#To view the startup or shutdown settings of the service at all levels:
chkconfig [--list] [name]

#Add service
SysV The service script for is placed in/etc/rc.d/init.d (/etc/init.d)
#!/bin/bash
chkconfig: LLLL nn nn #LLLL indicates the initial startup level, and - indicates no startup
description : Description information

chkconfig --add name

#Delete service
chkconfig --del name

#Modify the specified run level
chkconfig [--level levels] name <on|off|reset>

explain:--level LLLL: Specify the level to set; If omitted, it means 2345

example:

[root@centos6 ~]# service crond status
crond (pid  1358) is running...
[root@centos6 ~]# service rdisc status
rdisc is stopped

[root@centos6 ~]# chkconfig --list
auditd         	0:off	1:off	2:on	3:on	4:on	5:on	6:off
blk-availability	0:off	1:on	2:on	3:on	4:on	5:on	6:off
crond          	0:off	1:off	2:on	3:on	4:on	5:on	6:off
ip6tables      	0:off	1:off	2:on	3:on	4:on	5:on	6:off
iptables       	0:off	1:off	2:on	3:on	4:on	5:on	6:off
kdump          	0:off	1:off	2:off	3:on	4:on	5:on	6:off
mdmonitor      	0:off	1:off	2:on	3:on	4:on	5:on	6:off
netconsole     	0:off	1:off	2:off	3:off	4:off	5:off	6:off
netfs          	0:off	1:off	2:off	3:on	4:on	5:on	6:off
network        	0:off	1:off	2:on	3:on	4:on	5:on	6:off
nfs-rdma       	0:off	1:off	2:off	3:off	4:off	5:off	6:off
postfix        	0:off	1:off	2:on	3:on	4:on	5:on	6:off
rdisc          	0:off	1:off	2:off	3:off	4:off	5:off	6:off
rdma           	0:off	1:on	2:on	3:on	4:on	5:on	6:off
restorecond    	0:off	1:off	2:off	3:off	4:off	5:off	6:off
rsyslog        	0:off	1:off	2:on	3:on	4:on	5:on	6:off
saslauthd      	0:off	1:off	2:off	3:off	4:off	5:off	6:off
sshd           	0:off	1:off	2:on	3:on	4:on	5:on	6:off
udev-post      	0:off	1:on	2:on	3:on	4:on	5:on	6:off

[root@centos6 ~]# chkconfig --level 2345 crond off
[root@centos6 ~]# chkconfig --list crond
crond          	0:off	1:off	2:off	3:off	4:off	5:off	6:off
[root@centos6 ~]# ls /etc/rc*.d/*crond
/etc/rc0.d/K60crond  /etc/rc2.d/K60crond  /etc/rc4.d/K60crond  /etc/rc6.d/K60crond
/etc/rc1.d/K60crond  /etc/rc3.d/K60crond  /etc/rc5.d/K60crond

[root@centos6 ~]# chkconfig crond on
[root@centos6 ~]# chkconfig --list crond
crond          	0:off	1:off	2:on	3:on	4:on	5:on	6:off
[root@centos6 ~]# ls /etc/rc*.d/*crond
/etc/rc0.d/K60crond  /etc/rc2.d/S90crond  /etc/rc4.d/S90crond  /etc/rc6.d/K60crond
/etc/rc1.d/K60crond  /etc/rc3.d/S90crond  /etc/rc5.d/S90crond

[root@centos6 ~]# ls /etc/rc3.d/*
/etc/rc3.d/K10saslauthd    /etc/rc3.d/S05rdma       /etc/rc3.d/S12rsyslog           /etc/rc3.d/S50kdump    /etc/rc3.d/S99local
/etc/rc3.d/K61nfs-rdma     /etc/rc3.d/S08ip6tables  /etc/rc3.d/S15mdmonitor         /etc/rc3.d/S55sshd
/etc/rc3.d/K87restorecond  /etc/rc3.d/S08iptables   /etc/rc3.d/S25blk-availability  /etc/rc3.d/S56xinetd
/etc/rc3.d/K89netconsole   /etc/rc3.d/S10network    /etc/rc3.d/S25netfs             /etc/rc3.d/S80postfix
/etc/rc3.d/K89rdisc        /etc/rc3.d/S11auditd     /etc/rc3.d/S26udev-post         /etc/rc3.d/S90crond

[root@centos6 ~]# ls /etc/init.d
auditd            functions  iptables  mdmonitor   network   rdisc        rsyslog    single   udev-post
blk-availability  halt       kdump     netconsole  nfs-rdma  rdma         sandbox    sshd
crond             ip6tables  killall   netfs       postfix   restorecond  saslauthd  testsrv

[root@centos6 ~]# ls /etc/init.d/crond
/etc/init.d/crond

[root@centos6 ~]# cat /etc/init.d/crond
#!/bin/sh
#
# crond          Start/Stop the cron clock daemon.
#
# chkconfig: 2345 90 60
# description: cron is a standard UNIX program that runs user-specified \
#              programs at periodic scheduled times. vixie cron adds a \
#              number of features to the basic UNIX cron, including better \
#              security and more powerful configuration options.

### BEGIN INIT INFO
# Provides: crond crontab
# Required-Start: $local_fs $syslog
# Required-Stop: $local_fs $syslog
# Default-Start:  2345
# Default-Stop: 90
# Short-Description: run cron daemon
# Description: cron is a standard UNIX program that runs user-specified 
#              programs at periodic scheduled times. vixie cron adds a 
#              number of features to the basic UNIX cron, including better 
#              security and more powerful configuration options.
### END INIT INFO

[ -f /etc/sysconfig/crond ] || { 
    [ "$1" = "status" ] && exit 4 || exit 6 
}

RETVAL=0
prog="crond"
exec=/usr/sbin/crond
lockfile=/var/lock/subsys/crond
config=/etc/sysconfig/crond

# Source function library.
. /etc/rc.d/init.d/functions

[ $UID -eq 0 ] && [ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog

start() {
    if [ $UID -ne 0 ] ; then
        echo "User has insufficient privilege."
        exit 4
    fi
    [ -x $exec ] || exit 5
    [ -f $config ] || exit 6
    echo -n $"Starting $prog: "
    daemon $prog $CRONDARGS
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
}

stop() {
    if [ $UID -ne 0 ] ; then
        echo "User has insufficient privilege."
        exit 4
    fi
    echo -n $"Stopping $prog: "
	if [ -n "`pidfileofproc $exec`" ]; then
		killproc $exec
		RETVAL=3
	else
		failure $"Stopping $prog"
	fi
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
}

restart() {
    rh_status_q && stop
    start
}

reload() {
	echo -n $"Reloading $prog: "
	if [ -n "`pidfileofproc $exec`" ]; then
		killproc $exec -HUP
	else
		failure $"Reloading $prog"
	fi
	retval=$?
	echo
}

force_reload() {
	# new configuration takes effect after restart
    restart
}

rh_status() {
    # run checks to determine if the service is running or use generic status
    status -p /var/run/crond.pid $prog
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}


case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
        restart
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
        exit 2
esac
exit $?

[root@centos6 ~]# ls /etc/rc3.d/*
/etc/rc3.d/K10saslauthd    /etc/rc3.d/K89rdisc      /etc/rc3.d/S10network    /etc/rc3.d/S25blk-availability  /etc/rc3.d/S55sshd
/etc/rc3.d/K61nfs-rdma     /etc/rc3.d/S05rdma       /etc/rc3.d/S11auditd     /etc/rc3.d/S25netfs             /etc/rc3.d/S80postfix
/etc/rc3.d/K87restorecond  /etc/rc3.d/S08ip6tables  /etc/rc3.d/S12rsyslog    /etc/rc3.d/S26udev-post         /etc/rc3.d/S90crond
/etc/rc3.d/K89netconsole   /etc/rc3.d/S08iptables   /etc/rc3.d/S15mdmonitor  /etc/rc3.d/S50kdump             /etc/rc3.d/S99local

[root@centos6 ~]# ll /etc/rc3.d/*
lrwxrwxrwx. 1 root root 19 Oct  6 23:45 /etc/rc3.d/K10saslauthd -> ../init.d/saslauthd
lrwxrwxrwx. 1 root root 18 Oct  6 23:46 /etc/rc3.d/K61nfs-rdma -> ../init.d/nfs-rdma
lrwxrwxrwx. 1 root root 21 Oct  6 23:45 /etc/rc3.d/K87restorecond -> ../init.d/restorecond
lrwxrwxrwx. 1 root root 20 Oct  6 23:45 /etc/rc3.d/K89netconsole -> ../init.d/netconsole
lrwxrwxrwx. 1 root root 15 Oct  6 23:45 /etc/rc3.d/K89rdisc -> ../init.d/rdisc
lrwxrwxrwx. 1 root root 14 Oct  6 23:46 /etc/rc3.d/S05rdma -> ../init.d/rdma
lrwxrwxrwx. 1 root root 19 Oct  6 23:46 /etc/rc3.d/S08ip6tables -> ../init.d/ip6tables
lrwxrwxrwx. 1 root root 18 Oct  6 23:45 /etc/rc3.d/S08iptables -> ../init.d/iptables
lrwxrwxrwx. 1 root root 17 Oct  6 23:45 /etc/rc3.d/S10network -> ../init.d/network
lrwxrwxrwx. 1 root root 16 Oct  6 23:46 /etc/rc3.d/S11auditd -> ../init.d/auditd
lrwxrwxrwx. 1 root root 17 Oct  6 23:45 /etc/rc3.d/S12rsyslog -> ../init.d/rsyslog
lrwxrwxrwx. 1 root root 19 Oct  6 23:45 /etc/rc3.d/S15mdmonitor -> ../init.d/mdmonitor
lrwxrwxrwx. 1 root root 26 Oct  6 23:45 /etc/rc3.d/S25blk-availability -> ../init.d/blk-availability
lrwxrwxrwx. 1 root root 15 Oct  6 23:45 /etc/rc3.d/S25netfs -> ../init.d/netfs
lrwxrwxrwx. 1 root root 19 Oct  6 23:45 /etc/rc3.d/S26udev-post -> ../init.d/udev-post
lrwxrwxrwx. 1 root root 15 Oct  6 23:46 /etc/rc3.d/S50kdump -> ../init.d/kdump
lrwxrwxrwx. 1 root root 14 Oct  6 23:46 /etc/rc3.d/S55sshd -> ../init.d/sshd
lrwxrwxrwx. 1 root root 17 Oct  6 23:45 /etc/rc3.d/S80postfix -> ../init.d/postfix
lrwxrwxrwx  1 root root 15 Nov  2 19:55 /etc/rc3.d/S90crond -> ../init.d/crond
lrwxrwxrwx. 1 root root 11 Oct  6 23:45 /etc/rc3.d/S99local -> ../rc.local

[root@centos6 ~]# ll /etc/rc5.d/*
lrwxrwxrwx. 1 root root 19 Oct  6 23:45 /etc/rc5.d/K10saslauthd -> ../init.d/saslauthd
lrwxrwxrwx. 1 root root 18 Oct  6 23:46 /etc/rc5.d/K61nfs-rdma -> ../init.d/nfs-rdma
lrwxrwxrwx. 1 root root 21 Oct  6 23:45 /etc/rc5.d/K87restorecond -> ../init.d/restorecond
lrwxrwxrwx. 1 root root 20 Oct  6 23:45 /etc/rc5.d/K89netconsole -> ../init.d/netconsole
lrwxrwxrwx. 1 root root 15 Oct  6 23:45 /etc/rc5.d/K89rdisc -> ../init.d/rdisc
lrwxrwxrwx. 1 root root 14 Oct  6 23:46 /etc/rc5.d/S05rdma -> ../init.d/rdma
lrwxrwxrwx. 1 root root 19 Oct  6 23:46 /etc/rc5.d/S08ip6tables -> ../init.d/ip6tables
lrwxrwxrwx. 1 root root 18 Oct  6 23:45 /etc/rc5.d/S08iptables -> ../init.d/iptables
lrwxrwxrwx. 1 root root 17 Oct  6 23:45 /etc/rc5.d/S10network -> ../init.d/network
lrwxrwxrwx. 1 root root 16 Oct  6 23:46 /etc/rc5.d/S11auditd -> ../init.d/auditd
lrwxrwxrwx. 1 root root 17 Oct  6 23:45 /etc/rc5.d/S12rsyslog -> ../init.d/rsyslog
lrwxrwxrwx. 1 root root 19 Oct  6 23:45 /etc/rc5.d/S15mdmonitor -> ../init.d/mdmonitor
lrwxrwxrwx. 1 root root 26 Oct  6 23:45 /etc/rc5.d/S25blk-availability -> ../init.d/blk-availability
lrwxrwxrwx. 1 root root 15 Oct  6 23:45 /etc/rc5.d/S25netfs -> ../init.d/netfs
lrwxrwxrwx. 1 root root 19 Oct  6 23:45 /etc/rc5.d/S26udev-post -> ../init.d/udev-post
lrwxrwxrwx. 1 root root 15 Oct  6 23:46 /etc/rc5.d/S50kdump -> ../init.d/kdump
lrwxrwxrwx. 1 root root 14 Oct  6 23:46 /etc/rc5.d/S55sshd -> ../init.d/sshd
lrwxrwxrwx. 1 root root 17 Oct  6 23:45 /etc/rc5.d/S80postfix -> ../init.d/postfix
lrwxrwxrwx  1 root root 15 Nov  2 19:55 /etc/rc5.d/S90crond -> ../init.d/crond
lrwxrwxrwx. 1 root root 11 Oct  6 23:45 /etc/rc5.d/S99local -> ../rc.local

[root@centos6 ~]# ll /etc/rc.local
lrwxrwxrwx. 1 root root 13 Oct  6 23:45 /etc/rc.local -> rc.d/rc.local

[root@centos6 ~]#  cat /etc/rc.d/rc.local
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.

touch /var/lock/subsys/local
#/etc/rc.d/rc.local boot file is written to this file

[root@centos6 ~]# vim /etc/init.d/testsrv
#!/bin/bash
#chkconfig: - 96 3
#description: the service script

. /etc/init.d/functions
start(){
        touch /var/lock/subsys/testsrv
        action "Starting testsrv"
        sleep 3
} 
stop(){
        rm -f /var/lock/subsys/testsrv
        action "Shutting down testsrv"
}
restart(){
        stop
        start
}
status(){
        if [ -e /var/lock/subsys/testsrv ];then
            echo "testsrv is runing..."
        else
            echo "testsrv is stopped"
        fi
}

case $1 in
start)
    start
    ;;
stop)
    stop
    ;;
restart)
    restart
    ;;
status)
    status
    ;;
*)
    echo "Usage: /etc/init.d/testsrv {start|stop|restart|status}"
    ;; 
esac

[root@centos6 ~]# chmod +x /etc/init.d/testsrv
[root@centos6 ~]# chkconfig --list
auditd         	0:off	1:off	2:on	3:on	4:on	5:on	6:off
blk-availability	0:off	1:on	2:on	3:on	4:on	5:on	6:off
crond          	0:off	1:off	2:on	3:on	4:on	5:on	6:off
ip6tables      	0:off	1:off	2:on	3:on	4:on	5:on	6:off
iptables       	0:off	1:off	2:on	3:on	4:on	5:on	6:off
kdump          	0:off	1:off	2:off	3:on	4:on	5:on	6:off
mdmonitor      	0:off	1:off	2:on	3:on	4:on	5:on	6:off
netconsole     	0:off	1:off	2:off	3:off	4:off	5:off	6:off
netfs          	0:off	1:off	2:off	3:on	4:on	5:on	6:off
network        	0:off	1:off	2:on	3:on	4:on	5:on	6:off
nfs-rdma       	0:off	1:off	2:off	3:off	4:off	5:off	6:off
postfix        	0:off	1:off	2:on	3:on	4:on	5:on	6:off
rdisc          	0:off	1:off	2:off	3:off	4:off	5:off	6:off
rdma           	0:off	1:on	2:on	3:on	4:on	5:on	6:off
restorecond    	0:off	1:off	2:off	3:off	4:off	5:off	6:off
rsyslog        	0:off	1:off	2:on	3:on	4:on	5:on	6:off
saslauthd      	0:off	1:off	2:off	3:off	4:off	5:off	6:off
sshd           	0:off	1:off	2:on	3:on	4:on	5:on	6:off
udev-post      	0:off	1:on	2:on	3:on	4:on	5:on	6:off

[root@centos6 ~]# ls /etc/rc*.d/*testsrv
ls: cannot access /etc/rc*.d/*testsrv: No such file or directory

[root@centos6 ~]# service testsrv status
testsrv is stopped
[root@centos6 ~]# service testsrv start
Starting testsrv                                           [  OK  ]
[root@centos6 ~]# chkconfig testsrv on
[root@centos6 ~]# chkconfig --list testsrv
testsrv        	0:off	1:off	2:on	3:on	4:on	5:on	6:off
[root@centos6 ~]# ls /etc/rc*.d/*testsrv
/etc/rc2.d/S96testsrv  /etc/rc3.d/S96testsrv  /etc/rc4.d/S96testsrv  /etc/rc5.d/S96testsrv

[root@centos6 ~]# reboot

[root@centos6 ~]# service testsrv status
testsrv is runing...

Example: service startup failure, failure recovery

[root@centos6 ~]# vim /etc/init.d/testsrv 
#!/bin/bash
#chkconfig: - 96 3
#description: the service script

. /etc/init.d/functions
start(){
        touch /var/lock/subsys/testsrv
        action "Starting testsrv"
        sleep 1000  #If the waiting time is changed to 1000, the simulation service cannot be started normally                              
}

[root@centos6 ~]# reboot


If the service gets stuck, it won't start


Press a


Enter single user mode

# chkconfig --list testsrv
# chkconfig testsrv off
# init 3

[root@centos6 ~]# chkconfig --del testsrv #Delete boot service
[root@centos6 ~]# chkconfig --list testsrv
service testsrv supports chkconfig, but is not referenced in any runlevel (run 'chkconfig --add testsrv')
[root@centos6 ~]# ls /etc/rc*.d/*testsrv
ls: cannot access /etc/rc*.d/*testsrv: No such file or directory

1.3.5.4 non independent services

Services are divided into independent services and non independent services

Transient services are managed by the super daemon xinetd process, also known as dependent services

Incoming requests are first proxied by xinetd

Profile:

/etc/xinetd.conf
/etc/xinetd.d/<service>

Controlling the startup of dependent services with chkconfig

Example: chkconfig tftp on

/etc/rc.d/rc.local

Note: under normal level, the last service to be started is S99local, which is not linked to a service script of / etc/rc.d/init.d, but points to the script of / etc/rc.d/rc.local

Commands that are inconvenient or do not need to be written as service scripts and placed in the / etc/rc.d/init.d/ directory and want to run automatically at startup can be directly placed in the / etc/rc.d/rc.local file

/etc/rc.d/rc.local runs after specifying the run level script

example:

[root@centos6 ~]# yum -y install telnet-server

[root@centos6 ~]# chkconfig --list
auditd         	0:off	1:off	2:on	3:on	4:on	5:on	6:off
blk-availability	0:off	1:on	2:on	3:on	4:on	5:on	6:off
crond          	0:off	1:off	2:on	3:on	4:on	5:on	6:off
ip6tables      	0:off	1:off	2:on	3:on	4:on	5:on	6:off
iptables       	0:off	1:off	2:on	3:on	4:on	5:on	6:off
kdump          	0:off	1:off	2:off	3:on	4:on	5:on	6:off
mdmonitor      	0:off	1:off	2:on	3:on	4:on	5:on	6:off
netconsole     	0:off	1:off	2:off	3:off	4:off	5:off	6:off
netfs          	0:off	1:off	2:off	3:on	4:on	5:on	6:off
network        	0:off	1:off	2:on	3:on	4:on	5:on	6:off
nfs-rdma       	0:off	1:off	2:off	3:off	4:off	5:off	6:off
postfix        	0:off	1:off	2:on	3:on	4:on	5:on	6:off
rdisc          	0:off	1:off	2:off	3:off	4:off	5:off	6:off
rdma           	0:off	1:on	2:on	3:on	4:on	5:on	6:off
restorecond    	0:off	1:off	2:off	3:off	4:off	5:off	6:off
rsyslog        	0:off	1:off	2:on	3:on	4:on	5:on	6:off
saslauthd      	0:off	1:off	2:off	3:off	4:off	5:off	6:off
sshd           	0:off	1:off	2:on	3:on	4:on	5:on	6:off
udev-post      	0:off	1:on	2:on	3:on	4:on	5:on	6:off
xinetd         	0:off	1:off	2:off	3:on	4:on	5:on	6:off

xinetd based services:
	chargen-dgram: 	off
	chargen-stream:	off
	daytime-dgram: 	off
	daytime-stream:	off
	discard-dgram: 	off
	discard-stream:	off
	echo-dgram:    	off
	echo-stream:   	off
	tcpmux-server: 	off
	telnet:        	off
	time-dgram:    	off
	time-stream:   	off

[root@centos6 ~]# service xinetd start
Starting xinetd:                                           [  OK  ]
[root@centos6 ~]# service xinetd status
xinetd (pid  1465) is running...

[root@centos6 ~]# chkconfig telnet on
[root@centos6 ~]# chkconfig --list
auditd         	0:off	1:off	2:on	3:on	4:on	5:on	6:off
blk-availability	0:off	1:on	2:on	3:on	4:on	5:on	6:off
crond          	0:off	1:off	2:on	3:on	4:on	5:on	6:off
ip6tables      	0:off	1:off	2:on	3:on	4:on	5:on	6:off
iptables       	0:off	1:off	2:on	3:on	4:on	5:on	6:off
kdump          	0:off	1:off	2:off	3:on	4:on	5:on	6:off
mdmonitor      	0:off	1:off	2:on	3:on	4:on	5:on	6:off
netconsole     	0:off	1:off	2:off	3:off	4:off	5:off	6:off
netfs          	0:off	1:off	2:off	3:on	4:on	5:on	6:off
network        	0:off	1:off	2:on	3:on	4:on	5:on	6:off
nfs-rdma       	0:off	1:off	2:off	3:off	4:off	5:off	6:off
postfix        	0:off	1:off	2:on	3:on	4:on	5:on	6:off
rdisc          	0:off	1:off	2:off	3:off	4:off	5:off	6:off
rdma           	0:off	1:on	2:on	3:on	4:on	5:on	6:off
restorecond    	0:off	1:off	2:off	3:off	4:off	5:off	6:off
rsyslog        	0:off	1:off	2:on	3:on	4:on	5:on	6:off
saslauthd      	0:off	1:off	2:off	3:off	4:off	5:off	6:off
sshd           	0:off	1:off	2:on	3:on	4:on	5:on	6:off
udev-post      	0:off	1:on	2:on	3:on	4:on	5:on	6:off
xinetd         	0:off	1:off	2:off	3:on	4:on	5:on	6:off

xinetd based services:
	chargen-dgram: 	off
	chargen-stream:	off
	daytime-dgram: 	off
	daytime-stream:	off
	discard-dgram: 	off
	discard-stream:	off
	echo-dgram:    	off
	echo-stream:   	off
	tcpmux-server: 	off
	telnet:        	on
	time-dgram:    	off
	time-stream:   	off

[root@centos6 ~]# ss -ntl
State       Recv-Q Send-Q                                       Local Address:Port                                         Peer Address:Port 
LISTEN      0      128                                                     :::22                                                     :::*     
LISTEN      0      128                                                      *:22                                                      *:*     
LISTEN      0      64                                                      :::23                                                     :::*     
LISTEN      0      100                                                    ::1:25                                                     :::*     
LISTEN      0      100                                              127.0.0.1:25                                                      *:* 

[root@centos6 ~]#  ss -ntlp
State       Recv-Q Send-Q                                       Local Address:Port                                         Peer Address:Port 
LISTEN      0      128                                                     :::22                                                     :::*      users:(("sshd",1301,4))
LISTEN      0      128                                                      *:22                                                      *:*      users:(("sshd",1301,3))
LISTEN      0      64                                                      :::23                                                     :::*      users:(("xinetd",1465,5)) #telnet relies on xinetd to start
LISTEN      0      100                                                    ::1:25                                                     :::*      users:(("master",1380,13))
LISTEN      0      100                                              127.0.0.1:25                                                      *:*      users:(("master",1380,12))

[root@centos6 ~]# ps aux |grep telnet
root       1496  0.0  0.0 103320   884 pts/0    S+   16:46   0:00 grep telnet
#The telnet service was not started

[root@rocky8 ~]# telnet 172.31.0.6
Trying 172.31.0.6...
Connected to 172.31.0.6.
Escape character is '^]'.
CentOS release 6.10 (Final)
Kernel 2.6.32-754.el6.x86_64 on an x86_64
centos6 login: 

[root@centos6 ~]# ps aux |grep telnet
root       1503  0.0  0.1  12528  1024 ?        Ss   16:47   0:00 in.telnetd: 172.31.1.8
root       1508  0.0  0.0 103320   884 pts/0    S+   16:48   0:00 grep telnet
#Once someone accesses the telnet service, the service starts automatically

[root@centos6 ~]# lsof -i :23
COMMAND    PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
xinetd    1465 root    5u  IPv6  10616      0t0  TCP *:telnet (LISTEN)
in.telnet 1503 root    0u  IPv4  11192      0t0  TCP 172.31.0.6:telnet->172.31.1.8:44944 (ESTABLISHED)
in.telnet 1503 root    1u  IPv4  11192      0t0  TCP 172.31.0.6:telnet->172.31.1.8:44944 (ESTABLISHED)
in.telnet 1503 root    2u  IPv4  11192      0t0  TCP 172.31.0.6:telnet->172.31.1.8:44944 (ESTABLISHED)

#Disconnect telnet access
[root@centos6 ~]# ps aux |grep telnet
root       1515  0.0  0.0 103320   884 pts/0    S+   16:49   0:00 grep telnet
#telnet service is shut down

1.3.5.5 startup file rc.local

/etc/rc.d/rc.local

Note: under normal level, the last service to be started is S99local, which is not linked to a service script of / etc/rc.d/init.d, but points to the script of / etc/rc.d/rc.local

Commands that are inconvenient or do not need to be written as service scripts and placed in the / etc/rc.d/init.d/ directory and want to run automatically at startup can be directly placed in the / etc/rc.d/rc.local file

/etc/rc.d/rc.local runs after specifying the run level script

example:

[root@rocky8 ~]# ll /etc/rc.local
lrwxrwxrwx. 1 root root 13 May 19 10:58 /etc/rc.local -> rc.d/rc.local

[root@centos8 ~]# vim /etc/rc.d/rc.local
ping 127.0.0.1

[root@rocky8 ~]# chmod +x /etc/rc.d/rc.local
[root@rocky8 ~]# reboot

[root@rocky8 ~]# ps aux |grep ping
root         847  0.0  0.3  32448  2428 ?        S    17:38   0:00 ping 127.0.0.1
root        1338  0.0  0.1  12136  1068 pts/1    R+   17:38   0:00 grep --color=auto ping

1.3.6 CentOS startup process summary

/SBIN / init -- > (/ etc / inittab) -- > set the default running level -- > run the system initial script / etc/rc.d/rc.sysinit, complete the system initialization -- > (close the corresponding services to be closed) start the services to be started / etc/rc#.d/Sxxxx,/etc/rc.d/rc.local – > set the login terminal

see: http://s4.51cto.com/wyfs02/M02/87/20/wKiom1fVBELjXsvaAAUkuL83t2Q304.jpg

Posted by ebbatten on Mon, 22 Nov 2021 08:19:26 -0800