[root@centos6 boot]# ls vmlinuz-2.6.32-696.el6.x86_64
vmlinuz-2.6.32-696.el6.x86_64
1. 首先POST加电自检,主要是检查计算机硬件是否存在,或者损坏
2. MBR引导,第1块磁盘的第0个扇区中前446个字节中,存放着于计算机启动的相关程序,在centos6中叫GRUB,GRUB的一部分就是存放在446个字节中,还有一部分在别的地方
3. GRUB在加载启动完后
4. 加载内核,就是boot下的vmlinuz-版本号
[root@centos6 boot]# ls vmlinuz-2.6.32-696.el6.x86_64
vmlinuz-2.6.32-696.el6.x86_64
[root@centos6 boot]# rm initramfs-2.6.32-696.el6.x86_64.img
rm: remove regular file `initramfs-2.6.32-696.el6.x86_64.img'? yes
[root@centos6 boot]# ls initramfs-2.6.32-696.el6.x86_64.img
ls: cannot access initramfs-2.6.32-696.el6.x86_64.img: No such file or directory
[root@centos6 boot]# mkinitrd initramfd-`uname -r`.img `uname -r` (后面跟的是内核版本号)
[root@centos6 boot]# ls initramfd-2.6.32-696.el6.x86_64.img
initramfd-2.6.32-696.el6.x86_64.img
uname -r
.img uname -r
执行这条命令修复grub文件 init读取其初始化文件;/etc/inittab
1. 初始运行级别(RUN)
2. 系统初始化脚本
3. 对应运行级别的脚本目录
4. 捕获某个关键字顺序
5. 定义UPS电源终端/恢复脚本
6. 在虚拟控制台生产getty
7. 在运行级别5初始化X
[root@centos6 ~]# chkconfig --list atd
atd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@centos6 ~]# chkconfig --level 35 atd off
[root@centos6 ~]# chkconfig --list atd
atd 0:off 1:off 2:on 3:off 4:on 5:off 6:off
[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:
这里是设置默认启动时几模式,不要设置0和6
id:3:initdefault:
[root@centos6 ~]# cat /etc/init/control-alt-delete.conf
# control-alt-delete - emergency keypress handling
#
# This task is run whenever the Control-Alt-Delete key combination is
# pressed. Usually used to shut down the machine.
#
# Do not edit this file directly. If you want to change the behaviour,
# please create a file control-alt-delete.override and put your changes there.
start on control-alt-delete
exec /sbin/shutdown -r now "Control-Alt-Delete pressed"
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
[root@centos6 ~]# chkconfig //默认不添加--list 和服务名是列出系统中所有的服务
NetworkManager 0:off 1:off 2:on 3:off 4:on 5:on 6:off
abrt-ccpp 0:off 1:off 2:off 3:on 4:off 5:on 6:off
abrtd 0:off 1:off 2:off 3:on 4:off 5:on 6:off
acpid 0:off 1:off 2:on 3:on 4:on 5:on 6:off
atd 0:off 1:off 2:on 3:off 4:on 5:off 6:off
auditd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
autofs 0:off 1:off 2:off 3:on 4:on 5:on 6:off
blk-availability 0:off 1:on 2:on 3:on 4:on 5:on 6:off
bluetooth 0:off 1:off 2:off 3:on 4:on 5:on 6:off
certmonger 0:off 1:off 2:off 3:on 4:on 5:on 6:off
cpuspeed 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
cups 0:off 1:off 2:on 3:on 4:on 5:on 6:off
dnsmasq 0:off 1:off 2:off 3:off 4:off 5:off 6:off
firstboot 0:off 1:off 2:off 3:off 4:off 5:off 6:off
haldaemon 0:off 1:off 2:off 3:on 4:on 5:on 6:off
htcacheclean 0:off 1:off 2:off 3:off 4:off 5:off 6:off
[root@centos6 ~]# chkconfig --list httpd //查看单独的某个服务
httpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
chkconfig --add name
#!/bin/bash
#LLLL 表示初始在哪个级别下启动,-表示都不启动
# chkconfig: LLLL nn nn
chkconfig [--level levels] name
--level LLLL: 指定要设置的级别;省略时表示2345
[root@centos6 ~]# chkconfig --list httpd
httpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
[root@centos6 ~]# chkconfig --level 345 httpd on
[root@centos6 ~]# chkconfig --list httpd
httpd 0:off 1:off 2:off 3:on 4:on 5:on 6:off
[root@centos6 ~]# service httpd stop //停止httpd服务
Stopping httpd: [ OK ]
[root@centos6 ~]# service httpd start //启动httpd服务
[root@centos6 ~]# service httpd restart //重启httpd服务
[root@centos6 ~]# service --status-all //显示系统中的全部状态信息
1:2345:respawn:/usr/sbin/mingetty tty1 2:2345:respawn:/usr/sbin/mingetty tty2 ... 6:2345:respawn:/usr/sbin/mingetty tty6 mingetty会自动调用login程序 x:5:respawn:/etc/X11/prefdm -nodaemon
POST --> boot sequence(BIOS) --> boot loader --> kernel(ramdisk) --> rootfs --> switchroot --> /sbin/init --> (/etc/inittab,/etc/init/*.conf) --> 设定默认运行级别 --> 系统初始化脚本rc.sysinit --> 关闭或启动对应级别的服务 --> 启动终端
[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 68 13 05 00 00 08 fa 90 90 f6 c2 80 |....h...........|
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 |[email protected].1........|
000000f0 88 f4 40 89 44 08 31 c0 88 d0 c0 e8 02 66 89 04 |[email protected].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 36 4e 0d 00 00 00 80 20 |........6N..... |
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 1a 06 00 fe |)....... .......|
000001e0 ff ff 83 fe ff ff 00 88 3a 06 00 00 71 02 00 fe |........:...q...|
000001f0 ff ff 05 fe ff ff 00 88 ab 08 00 78 54 10 55 aa |...........xT.U.|
00000200
00000200
[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
title CentOS 6 (2.6.32-696.el6.x86_64)
root (hd0,0)
kernel /vmlinuz-2.6.32-696.el6.x86_64 ro root=UUID=03352018-7cef-4ee3-9a05-b0833b67da19 rd_NO_LUKS rd_NO_L
VM 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-696.el6.x86_64.img
[root@centos6 grub]# ls
device.map ffs_stage1_5 jfs_stage1_5 reiserfs_stage1_5 stage2 xfs_stage1_5
e2fs_stage1_5 grub.conf menu.lst splash.xpm.gz ufs2_stage1_5
fat_stage1_5 iso9660_stage1_5 minix_stage1_5 stage1 vstafs_stage1_5
grub> root (hd#,#)
grub> setup (hd#)
grub> root (hd#,#)
grub> kernel /vimlinuxz-VERSION-RELEASE ro root=/dev/DEVICE
grub> initrd /initramfs-VERSION-RELEASE.img
grub> boot
root (hd0,0)
kernel /vmlinuz-2.6.32-696.el6.x86_64 ro root=UUID=03352018-7cef-4ee3-9a05-b0833b67da19 rd_NO_LUKS rd_NO_L
VM 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-696.el6.x86_64.img
操作一下吧rhgb和quiet删掉
-
- 然后启动
- 在绿色ok之前的都是启动内核参数,而其是以字符方式启动的,并没有用图形状态启动
- /proc/cmdline是存放内核参数的内容
[root@centos6 ~]# cat /proc/cmdline
ro root=UUID=03352018-7cef-4ee3-9a05-b0833b67da19 rd_NO_LUKS rd_NO_LVM LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM
[root@centos6 ~]# rm -f /boot/grub/*
[root@centos6 ~]# ls /boot/grub/
[root@centos6 ~]#
[root@centos6 ~]# reboot
修复grub1阶段就是mbr中的446个字节
- 首先用dd命令破坏系统中的446个字节
1. 交互式修复
[root@centos6 grub]# 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.000677783 s, 658 kB/s
[root@centos6 grub]#
[root@centos6 grub]# reboot
主意
- 在这里要注意grub-install 这个命令必须要在系统根上运行,进入光盘时是在光盘目录上,要用chroot切根
- grub-install 在上面命令我们没有用–root-directory= 这个选项,这里=号后面要跟/boot目录的上一级目录,如果是在根上默认可以不写。
- 运行完成后不要急着去重启等待一会,因为可能系统没有把缓冲的数据写入磁盘中,而导致出错
[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.000546253 s, 816 kB/s
[root@centos6 app]# mv /boot/grub/* .
[root@centos6 app]# ls
device.map ffs_stage1_5 jfs_stage1_5 menu.lst splash.xpm.gz ufs2_stage1_5 xfs_stage1_5
e2fs_stage1_5 grub.conf lost+found minix_stage1_5 stage1 vstafs_stage1_5
fat_stage1_5 iso9660_stage1_5 lv0 reiserfs_stage1_5 stage2
[root@centos6 app]# grub
Probing devices to guess BIOS drives. This may take a long time.
Unknown partition table signature
grub> root (hd0,0) //hd 后面跟第块硬盘,和扇区
root (hd0,0)
Filesystem type is ext2fs, partition type 0x83
grub> setup (hd0)
setup (hd0)
Checking if "/boot/grub/stage1" exists... no //这里报错了需要/boot/grub/stage1这个文件
Checking if "/grub/stage1" exists... no
Error 15t: File not found
[root@centos6 app]# grub
Probing devices to guess BIOS drives. This may take a long time.
Unknown partition table signature
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 //这里说明了1.5阶段所在的位置,就是0扇区后的27个扇区,是存放/boot文件驱动的
Running "install /grub/stage1 (hd0) (hd0)1+27 p (hd0,0)/grub/stage2 /grub/grub.conf"... succeeded
Done.
grub> quit
删除1.5阶段然后恢复
[root@centos6 app]# dd if=/dev/zero of=/dev/sda bs=1 count=5120 skip=512 seek=512
5120+0 records in
5120+0 records out
5120 bytes (5.1 kB) copied, 0.00458081 s, 1.1 MB/s
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title CentOS 6 (2.6.32-696.el6.x86_64) //这个title 后面跟的就是启动菜单名
root (hd0,0)
kernel /vmlinuz-2.6.32-696.el6.x86_64 ro root=UUID=03352018-7cef-4ee3-9a05-b0833b67da19 rd_NO_LUKS rd_NO_ //grub 眼里boot才是根
LVM LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rh
gb quiet
initrd /initramfs-2.6.32-696.el6.x86_64.img //grub 眼里boot才是根
kernel (hd0,0)/vmlinuz-2.6.32-696.el6.x86_64 ro root=UUID=03352018-7cef-4ee3-9a05-b0833b67da19 rd_NO_LUKS rd_NO_
default=0 //如果有多个系统,默认是0第一个title系统启动
timeout=5 //菜单超时时间
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title CentOS 6 (2.6.32-696.el6.x86_64) //这个title 后面跟的就是启动菜单名
root (hd0,0)
kernel /vmlinuz-2.6.32-696.el6.x86_64 ro root=UUID=03352018-7cef-4ee3-9a05-b0833b67da19 rd_NO_LUKS rd_NO_ //grub 眼里boot才是根
LVM LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rh
gb quiet
initrd /initramfs-2.6.32-696.el6.x86_64.img //grub 眼里boot才是根
title centos10 //这是我们刚刚添加
root (hd0,0)
kernel /vmlinuz-2.6.32-696.el6.x86_64 ro root=UUID=03352018-7cef-4ee3-9a05-b0833b67da19 rd_NO_LUKS rd_NO_ //grub 眼里boot才是根
LVM LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rh
gb quiet
initrd /initramfs-2.6.32-696.el6.x86_64.img //grub 眼里boot才是根
如果我们要把initrd 放在kernel 前会导致系统启动不了
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title CentOS 6 (2.6.32-696.el6.x86_64)
root (hd0,0)
initrd /initramfs-2.6.32-696.el6.x86_64.img
kernel /vmlinuz-2.6.32-696.el6.x86_64 ro root=UUID=03352018-7cef-4ee3-9a05-b0833b67da19 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 rh
gb quiet
[root@centos6 grub]# ls
grub.conf
[root@centos6 ~]# ls /app/
device.map ffs_stage1_5 lost+found minix_stage1_5 stage1 ufs2_stage1_5 xfs_stage1_5
e2fs_stage1_5 iso9660_stage1_5 lv0 reiserfs_stage1_5 stage2 vstafs_stage1_5
fat_stage1_5 jfs_stage1_5 menu.lst splash.xpm.gz www
[root@centos6 ~]# ls /boot/grub/
device.map fat_stage1_5 grub.conf jfs_stage1_5 reiserfs_stage1_5 stage2 vstafs_stage1_5
e2fs_stage1_5 ffs_stage1_5 iso9660_stage1_5 minix_stage1_5 stage1 ufs2_stage1_5 xfs_stage1_5
[root@centos6 ~]#
[root@centos6 ~]# rm -fr /boot/
rm: cannot remove `/boot': Device or resource busy
[root@centos6 ~]# ls /boot/
[root@centos6 ~]#
[root@centos6 ~]# rm -rf /boot
rm: cannot remove `/boot': Device or resource busy
[root@centos6 ~]# ls /boot/
[root@centos6 ~]# rm -f /etc/fstab
[root@centos6 ~]# cat /etc/fstab
cat: /etc/fstab: No such file or directory
[root@centos6 ~]#
[root@centos6 ~]# which init
/sbin/init
[root@centos6 ~]#
[root@centos6 ~]# rm -f /sbin/init
[root@centos6 ~]# ls /sbin/init
ls: cannot access /sbin/init: No such file or directory