1、请详细描述CentOS系统的启动流程(详细到每个过程系统做了哪些事情)
CentOS主机按以下顺序启动 1、POST加电自检 2、BIOS读取CMOS中的BIOS设置的参数来识别基础硬件,寻找到启动设备 3、MBR 1)读取启动设备MBR中前446字节的bootloader 2)读取MBR后的扇区用来识别grub以及内核kernel所在的区域 3)启动grub 4、GRUB显示菜单界面,选择运行内核kernel;配置文件是/boot/grub/grub.conf 1)提供菜单、并提供交互式接口 2)加载用户选择的内核或操作系统 3)为菜单提供了保护机制 5、KERNEL自身初始化 1)探测可识别到的所有硬件设备 2)加载硬件驱动程序(有可能会借助于ramdisk加载驱动) 3)以只读方式挂载根文件系统 4)运行用户空间的第一个应用程序:/sbin/init 6、INIT 运行/sbin/init程序,配置文件/etc/inittab和/etc/init/*.conf 设置默认运行级别如id:3:initdefault 运行系统初始化脚本/etc/rc.d/rc.sysinit 1)设置主机名; 2)设置欢迎信息; 3)激活udev和selinux 4)挂载/etc/fstab文件中定义的文件系统; 5)检测根文件系统,并以读写方式重新挂载根文件系统; 6)设置系统时钟; 7)激活swap设备; 8)根据/etc/sysctl.conf文件设置内核参数; 9)激活lvm及software raid设备 10)加载额外设备的驱动程序 11)清理操作 关闭对应的脚本中需要关闭的服务,启动需要启动服务(实际服务命令位于/etc/rc.d/init.d) 设置登录终端
2、为运行于虚拟机上的CentOS 6添加一块新硬件,提供两个主分区;
(1) 为硬盘新建两个主分区;并为其安装grub;
(2) 为硬盘的第一个主分区提供内核和ramdisk文件; 为第二个分区提供rootfs;
(3) 为rootfs提供bash、ls、cat程序及所依赖的库文件;
(4) 为grub提供配置文件;
(5) 将新的硬盘设置为第一启动项并能够正常启动目标主机;
为硬盘新建两个主分区;并为其安装grub;
添加一块硬盘
[root@localhost ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x495813a2.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-10443, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-10443, default 10443): +1G
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (133-10443, default 133):
Using default value 133
Last cylinder, +cylinders or +size{K,M,G} (133-10443, default 10443): +2G
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# partx -a /dev/sdb
BLKPG: Device or resource busy
error adding partition 1
BLKPG: Device or resource busy
error adding partition 2
[root@localhost ~]# mke2fs -t ext4 /dev/sdb1
[root@localhost ~]# mke2fs -t ext4 /dev/sdb2
[root@localhost ~]# mkdir /mnt/{boot,sysroot}
[root@localhost ~]# mount /dev/sdb1 /mnt/boot/
[root@localhost ~]# mount /dev/sdb2 /mnt/sysroot/
[root@localhost ~]# grub-install --root-directory=/mnt/ /dev/sdb
为硬盘的第一个主分区提供内核和ramdisk文件; 为第二个分区提供rootfs;
[root@localhost ~]# cp -r /boot/initramfs-2.6.32-504.el6.x86_64.img /mnt/boot/vmlinuz
[root@localhost ~]# cp /boot/initramfs-2.6.32-504.el6.x86_64.img /mnt/boot/initramfs.img
[root@localhost ~]# ls /mnt/boot/
grub initramfs.img lost+found vmlinuz
为rootfs提供bash、ls、cat程序及所依赖的库文件
[root@localhost ~]# mkdir -p /mnt/sysroot/{bin,lib64}
[root@localhost ~]# cp -r /bin/{bash,ls,cat} /mnt/sysroot/bin/
[root@localhost ~]# cp $(ldd /bin/bash | awk '$3~/^\/lib64/{print $3}') /mnt/sysroot/lib64/
[root@localhost ~]# cp $(ldd /bin/ls | awk '$3~/^\/lib64/{print $3}') /mnt/sysroot/lib64/
[root@localhost ~]# cp $(ldd /bin/cat | awk '$3~/^\/lib64/{print $3}') /mnt/sysroot/lib64/
为grub提供配置文件
[root@localhost ~]# vim /mnt/boot/grub/grub.conf
default=0
timeout=5
title CentOS 6.6 (Test)
root (hd0,0)
kernel /vmlinuz ro root=/dev/sda2 selinux=0 init=/bin/bash
initrd /initramfs.img
在BIOS中选择sdb作为启动盘
3、制作一个kickstart文件以及一个引导镜像。描述其过程。
可以直接手动编辑或使用工具在桌面模式下用system-config-kickstart(centos6.x)来创建ks.cfg
#命令段
firewall --disabled//禁用防火墙
install//执行新安装
cdrom//用光盘安装
lang en_US.UTF-8//默认安装语言
keyboard us//选择键盘
rootpw --iscrypted $6$AF1u4TWzFxa/SzHI$yJvJdbhyw/2rG8dtr.PY6c15sZ.qNc6US/z7PMQ4lADdlIis/qIMO738b9czXK/rX1YDiL7Uv/C6Bi99ig8ov0//管理员加密密码
authconfig --enableshadow --passalgo=sha512
selinux --enforcing//激活selinux
logging --level=info//信息等级
timezone --utc Asia/Shanghai//系统时区
bootloader --location=mbr --driveorder=sdb --append="crashkernel=auto rhgb quiet"
clearpart --all//删除所有现在分区
part /boot --fstype=ext4 --size=200//分区挂载
part / --fstype=ext4 --size=40960
part swap --size=4096
#脚本段
%pre//安装前脚本
echo "start install"
%end
%post//安装后脚本
echo "install end"
#程序包段
%packages
@chinese-support//中文支持
@development//开发工具
@graphical-desktop-clients//图形化工具
@remote-desktop-clients//远程桌面客户端
%end
简单引导镜像光盘制作:
1)复制系统安装光盘除Packages和repodata外的所有目录下的所有文件到一自定义目录(/centos6.6)
mount -r /dev/sr0 /mnt/
mkdir /centos6.6
cp -r /mnt/{CentOS_BuildTag,isolinux,R*,E*,GPL,T*,images} /centos6.6/
cd /centos6.6/
2)创建引导光盘:把/centos6.6目录创建为光盘镜像boot.iso
mkisofs -R -J -T -v --no-emul-boot --boot-load-size 4 --boot-info-table -V
"CentOS 6.6 x86_64 boot" -b isolinux/isolinux.bin -c isolinux/boot.cat
-o /root/boot.iso /centos6.6/
简单自动安装系统光盘制作
1)复制安装系统光盘的所有目录下的所有文件和ks.cfg文件到一自制目录(/centos6.6)
cd /centos6.6/
cp -r /mnt/* .
cp /root/ks.cfg .
2)在/centos6.6/isolinux/isolinux.cfg文件中append initrd=initrd.img条目中添加ks文件读取路径 ks=cdrom:/ks.cfg
3)创建引导光盘:把centos6.6目录创建为光盘镜像centos.iso
mkisofs -R -J -T -v --no-emul-boot --boot-load-size 4 --boot-info-table
-V "CentOS 6.6 x86_64 boot" -b isolinux/isolinux.bin -c isolinux/boot.cat
-o /root/boot.iso /centos6.6/
4、写一个脚本
(1) 能接受四个参数:start, stop, restart, status
start: 输出“starting 脚本名 finished.”
…
(2) 其它任意参数,均报错退出;
#!/bin/bash # case $1 in start) echo "starting $0 finished." ;; stop) echo "stoping $0 finished." ;; restart) echo "restart $0 finished." ;; *) echo "error,exit!" exit 1 ;; esac
5、写一个脚本,判断给定的用户是否登录了当前系统;
(1) 如果登录了,则显示用户登录,脚本终止;
(2) 每3秒钟,查看一次用户是否登录;
#!/bin/bash while [ 0 ];do if `w | grep "^$1\>" &> /dev/null`;then echo "$1 is login." exit 0 else sleep 3 fi done
6、写一个脚本,显示用户选定要查看的信息;
cpu) display cpu info
mem) display memory info
disk) display disk info
quit) quit
非此四项选择,则提示错误,并要求用户重新选择,只到其给出正确的选择为止;
#!/bin/bash # while [ 0 ];do echo "please input a option:" echo "cpu) display cpu info" echo "mem) display memory info" echo "disk) display disk info" echo "quit) quit" read test case $test in cpu) lscpu ;; mem) cat /proc/meminfo ;; disk) fdisk -l ;; quit) exit 0 ;; *) echo "you have wrong choose." continue ;; esac done
7、写一个脚本
(1) 用函数实现返回一个用户的UID和SHELL;用户名通过参数传递而来;
(2) 提示用户输入一个用户名或输入“quit”退出;
当输入的是用户名,则调用函数显示用户信息;
当用户输入quit,则退出脚本;进一步地:显示键入的用户相关信息后,再次提醒输出用户名或quit:
#!/bin/bash
#
function userinfo {
if id $username &> /dev/null;then
id -u $username
grep "^$username\>" /etc/passwd | cut -d: -f7
else
echo "wrong user!"
fi
echo $username
}
while true;do
read -p "Please input a username or quit:" username
if [ $username == "quit" ];then
exit 0
else
userinfo $username
fi
done
8、写一个脚本,完成如下功能(使用函数)
(1) 提示用户输入一个可执行命令的名字;获取此命令依赖的所有库文件;
(2) 复制命令文件至/mnt/sysroot目录下的对应的rootfs的路径上,例如,如果复制的文件原路径是/usr/bin/useradd,则复制到/mnt/sysroot/usr/bin/目录中;
(3) 复制此命令依赖的各库文件至/mnt/sysroot目录下的对应的rootfs的路径上;规则同上面命令相关的要求;
#!/bin/bash
#
libcp(){
for lib in $(ldd $1 | grep -o "[^[:space:]]*/lib[^[:space:]]*");do
libdir=$(dirname $lib)
[ -d $target$libdir ] || mkdir $target$libdir
[ -f $target$lib ] || cp $lib $target$lib
done
}
target=/mnt/sysroot
[ -d $target ] || mkdir $target
read -p "Input a command:" command
while [ "$command" != 'quit' ];do
if ! which $command &> /dev/null;then
read -p "No such command,enter again:" command
continue
fi
command=$(which --skip-alias $command)
cmnddir=$(dirname $command)
[ -d $target$cmnddir ] || mkdir -p $target$cmnddir
[ -f $target$command ] || cp $command $target$command
libcp $command
read -p "Another command(quit):" command
done
原创文章,作者:N21-天天,如若转载,请注明出处:http://www.178linux.com/47840


评论列表(1条)
写的很好,最后一个脚本可以优化一下,