1、请详细描述CentOS系统的启动流程(详细到每个过程系统做了哪些事情)
1.POST 开机加电自检,对计算机的cpu,硬盘,内存等进行检测。
2.BIOS 对引导程序进行探测,并把控制权交给引导程序。
3.MBR Master bootloader record主引导程序.通常位于硬盘第一扇区/dev/hda(0,0)或/dev/sda(0,0).
此阶段分三阶段
a.启动bootloader
b.1.5 stage 识别 grub所在磁盘分区
c.加载grub
4.grub 加载 kernel,而后把控制权交给kernel.
5.kernel自身初始化,加载驱动程序,此过程有可能会借助与ramdisk临时根文件系统加载硬件驱动程序,进而
加载真正的根文件系统,然后进行根系统的切换,以只读方式挂在根文件系统。
6.执行/sbin/init脚本,设定默认系统运行级别,运行系统初始化脚本,关闭或运行相关服务,启动终端。
7.进入用户空间
2、为运行于虚拟机上的CentOS 6添加一块新硬件,提供两个主分区; (1) 为硬盘新建两个主分区;并为其安装grub; (2) 为硬盘的第一个主分区提供内核和ramdisk文件; 为第二个分区提供rootfs; (3) 为rootfs提供bash、ls、cat程序及所依赖的库文件; (4) 为grub提供配置文件; (5) 将新的硬盘设置为第一启动项并能够正常启动目标主机;
(1)建立分区:
fdisk /dev/sdb
挂在硬盘
mount /dev/sdb1 /mnt/boot
安装grub:
grub-install --root-directory=/mnt /dev/sdb
(2) cp /boot/vmlinuz-2.6.32-431.el6.x86_64 /mnt/boot/
cp /boot/initramfs-2.6.32-431.el6.x86_64.img /mnt/boot/
mount /dev/sdb2 /mnt/sysroot/
mkdir -pv /mnt/sysroot/{bin,sbin,etc,lib,lib64,var,home,usr,mnt,media}
(3) cp /bin/{bash,ls,cat} /mnt/sysroot/bin/
[root@www ~]# ldd /bin/{bash,ls,cat}
/bin/bash:
linux-vdso.so.1 => (0x00007fff95fff000)
libtinfo.so.5 => /lib64/libtinfo.so.5 (0x0000003bab200000)
libdl.so.2 => /lib64/libdl.so.2 (0x0000003ba1600000)
libc.so.6 => /lib64/libc.so.6 (0x0000003ba0e00000)
/lib64/ld-linux-x86-64.so.2 (0x0000003ba0a00000)
/bin/ls:
linux-vdso.so.1 => (0x00007fffad7ff000)
libselinux.so.1 => /lib64/libselinux.so.1 (0x0000003ba2a00000)
librt.so.1 => /lib64/librt.so.1 (0x0000003ba1a00000)
libcap.so.2 => /lib64/libcap.so.2 (0x0000003ba5600000)
libacl.so.1 => /lib64/libacl.so.1 (0x0000003bae200000)
libc.so.6 => /lib64/libc.so.6 (0x0000003ba0e00000)
libdl.so.2 => /lib64/libdl.so.2 (0x0000003ba1600000)
/lib64/ld-linux-x86-64.so.2 (0x0000003ba0a00000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x0000003ba1200000)
libattr.so.1 => /lib64/libattr.so.1 (0x0000003bada00000)
/bin/cat:
linux-vdso.so.1 => (0x00007fffbc59e000)
libc.so.6 => /lib64/libc.so.6 (0x0000003ba0e00000)
/lib64/ld-linux-x86-64.so.2 (0x0000003ba0a00000)
cp /lib64/libtinfo.so.5 /mnt/sysroot/lib64/
(4)配置grub
default=0
timeout=5
hiddenmenu
password --md5 $1$TVpnE/$3jasVZKWFHAqhIaqdAcgD1
title CentOS (simple express)
root (hd0,0)
kernel /vmlinuz-2.6.32-431.el6.x86_64 ro selinux=0 root=/dev/sda2 init=/bin/bash
initrd /initramfs-2.6.32-431.el6.x86_64.img
password --md5 $1$TVpnE/$3jasVZKWFHAqhIaqdAcgD1
(5)

3、制作一个kickstart文件以及一个引导镜像。描述其过程。
(1) kickstart是内核安装程序anaconda的一个配置文件,采用此文 件可以使安装能自动完成配置而无需人为干预.kickstart文件主要分三段,命令段,程序包段已经脚本段;命令段的内容主要包括安装的方式,安装源的介质,安装过程使用的语言,键盘的类型,网络的配置,登陆用户的设置,以及认证的方式,防火墙以及selinux的配置,时区的设置,引导程序的安装,安装仓库的设置,硬盘分区的设置。程序包段主要包括所选择要安装的程序包,以%packages开头,以%end结束。脚本段分为安装前执行的脚本和安装完成后执行的脚本。 命令段: #version=DEVEL install cdrom lang en_US.UTF-8 keyboard us network --onboot yes --device eth0 --bootproto dhcp rootpw --iscrypted $1$WoADsfda$jd47Pzu7bqMsqYuu2vszw1 # Reboot after installation reboot firewall --service=ssh authconfig --useshadow --enablemd5 selinux --enforcing timezone --utc America/Los_Angeles bootloader --location=mbr --driveorder=sda --append="crashkernel=auto rhgb quiet" # The following is the partition information you requested reboot firewall --service=ssh authconfig --useshadow --enablemd5 selinux --enforcing timezone --utc America/Los_Angeles bootloader --location=mbr --driveorder=sda --append="crashkernel=auto rhgb quiet" # The following is the partition information you requested # Note that any partitions you deleted are not expressed # here so unless you clear all partitions first, this is # not guaranteed to work #clearpart --all --initlabel #part /boot --fstype=ext4 --size=300 #part / --fstype=ext4 --grow --size=3000 #part swap --grow --maxsize=1984 --size=1984 repo --name="CentOS" --baseurl=cdrom:sr1 --cost=100 程序包段: %packages @Printing client @X Window System binutils gcc kernel-devel python @Fonts @Printing client @X Window System binutils gcc kernel-devel python %end 脚本段: %post if [ -f /boot/grub/menu.lst -a ! -h /boot/grub/menu.lst ]; then cp /boot/grub/menu.lst /boot/grub/menu.lst.bak && sed -i 's/ rhgb//' /boot/grub/menu.lst; fi if [ -f /boot/grub/grub.conf -a ! -h /boot/grub/grub.conf ]; then cp /boot/grub/grub.conf /boot/grub/grub.conf.bak && sed -i 's/ rhgb//' /boot/grub/grub.conf; fi if [ -f /boot/grub2/grub.conf -a ! -h /boot/grub2/grub.conf ]; then cp /boot/grub2/grub.conf /boot/grub2/grub.conf.bak && sed -i 's/ rhgb//' /boot/grub2/grub.conf; fi if [ -f /etc/rc.d/rc.local ]; then cp /etc/rc.d/rc.local /etc/rc.d/rc.local.backup; fi cat >>/etc/rc.d/rc.local <<EOF #!/bin/bash echo echo "Installing VMware Tools, please wait..." if [ -x /usr/sbin/getenforce ]; then oldenforce=\$(/usr/sbin/getenforce); /usr/sbin/setenforce permissive || true; fi mkdir -p /tmp/vmware-toolsmnt0 for i in hda sr0 scd0; do mount -t iso9660 /dev/\$i /tmp/vmware-toolsmnt0 && break; done cp -a /tmp/vmware-toolsmnt0 /opt/vmware-tools-installer chmod 755 /opt/vmware-tools-installer cd /opt/vmware-tools-installer mv upgra32 vmware-tools-upgrader-32 mv upgra64 vmware-tools-upgrader-64 mv upgrade.sh run_upgrader.sh chmod +x /opt/vmware-tools-installer/*upgr* umount /tmp/vmware-toolsmnt0 rmdir /tmp/vmware-toolsmnt0 if [ -x /usr/bin/rhgb-client ]; then /usr/bin/rhgb-client --quit; fi cd /opt/vmware-tools-installer for s in sr0 sr1; do eject -s /dev/\$s; done ./run_upgrader.sh if [ -f /etc/rc.d/rc.local.backup ]; then mv /etc/rc.d/rc.local.backup /etc/rc.d/rc.local; else rm -f /etc/rc.d/rc.local; fi rm -rf /opt/vmware-tools-installer sed -i 's/3:initdefault/5:initdefault/' /etc/inittab if [ -f /boot/grub/menu.lst.bak ]; then mv /boot/grub/menu.lst.bak /boot/grub/menu.lst; fi if [ -f /boot/grub/grub.conf.bak ]; then mv /boot/grub/grub.conf.bak /boot/grub/grub.conf; fi if [ -f /boot/grub2/grub.conf.bak ]; then mv /boot/grub2/grub.conf.bak /boot/grub2/grub.conf; fi if [ -x /usr/sbin/getenforce ]; then /usr/sbin/setenforce \$oldenforce || true; fi if [ -x /bin/systemd ]; then systemctl restart prefdm.service; else telinit 5; fi EOF chmod 755 /etc/rc.d/rc.local if [ -x /bin/systemd ]; then systemctl enable rc-local.service; fi /usr/sbin/adduser admin /usr/sbin/usermod -p '$1$WoADsfda$jd47Pzu7bqMsqYuu2vszw1' admin /usr/bin/chfn -f "Linux 6.5-2" admin /bin/echo done %end 除了直接对配置文件进行修改,可以安装程序system-config-kickstart,使用图形界面来配置kickstart文件:  生成引导镜像 复制安装光盘内的isolinux文件夹(cp -r)以及kickstart配置文件至指定目录,使用命令 mkisofs -R -J -T -v --no-emul-boot --boot-load-size 4 --boot-info-table -V "centos 6" -c isolinux/boot.cat -b isolinux/isolinux.bin -o /root/no8.iso myroot/ 生成iso镜像,载入即可进行自动安装,需要注意的是,由于kickstart有多种方式提供,需要指明具体的路径,可以是http,ftp,cdrom等,且有可能需要配置ip,netmask,gateway,dns等。
4、写一个脚本 (1) 能接受四个参数:start, stop, restart, status start: 输出“starting 脚本名 finished.” … (2) 其它任意参数,均报错退出;
#!/bin/bash
if [ "$1" = "start" ]; then
echo "starting $0 finished"
elif [ "$1" = "stop" ]; then
echo "stop $0 finished"
elif [ "$1" = "restart" ]; then
echo "restart $0 finished"
elif [ "$1" = "status" ]; then
echo " status $0 finished"
else
echo ' error,please input one of them "start, stop,restart,status"'
fi
[root@www tmp]# bash ss.sh start
starting ss.sh finished
[root@www tmp]# bash ss.sh stop
stop ss.sh finished
[root@www tmp]# bash ss.sh sd
error,please input one of them "start, stop,restart,status"
[root@www tmp]# bash ss.sh restart
restart ss.sh finished
5、写一个脚本,判断给定的用户是否登录了当前系统; (1) 如果登录了,则显示用户登录,脚本终止; (2) 每3秒钟,查看一次用户是否登录;
#!/bin/bash
while true; do
if $( who | grep "^$1" &> /dev/null); then
echo "$1 already loged in "
break
fi
sleep 3
done
[root@www tmp]# bash -x logtest.sh tt
+ true
++ grep '^tt'
++ who
+ sleep 3
+ true
++ grep '^tt'
++ who
+ sleep 3
+ true
++ grep '^tt'
++ who
+ sleep 3
+ true
++ grep '^tt'
++ who
+ echo 'tt already loged in '
tt already loged in
+ break
6、写一个脚本,显示用户选定要查看的信息; cpu) display cpu info mem) display memory info disk) display disk info quit) quit 非此四项选择,则提示错误,并要求用户重新选择,只到其给出正确的选择为止;
#!/bin/bash cat << EOF cpu) display cpu info mem) display memory info disk) display disk info quit) quit — ————————– EOF while true;do
read -p " please choose the item you want to know:" item
while [ "$item" != "cpu" -a "$item" != "mem" -a "$item" != "disk" -a "$item" != "quit" ]; do
echo ' please input one of above option" cpu,mem,disk,quit" '
read -p" cpu,mem,disk,quit:" item
done
if [ "$item" = "quit" ];then
break
fi
case $item in
cpu)
lscpu
;;
mem)
free
;;
disk)
fdisk -l
;;
quit)
exit
;;
esac
done
~
7、写一个脚本 (1) 用函数实现返回一个用户的UID和SHELL;用户名通过参数传递而来; (2) 提示用户输入一个用户名或输入“quit”退出; 当输入的是用户名,则调用函数显示用户信息; 当用户输入quit,则退出脚本;进一步地:显示键入的用户相关信息后,再次提醒输出用户名或quit:
#!/bin/bash
userinfo() {
if id $username &> /dev/null; then
grep "$username" /etc/passwd | awk -F: '{print $3,$7}'
else
echo " $username doesn't exist "
fi
}
while true;do
read -p " please input username or quit: " option
if [ "$option" = "quit" ]; then
break
else
username=$option
userinfo
fi
[root@www tmp]# vim function.sh
[root@www tmp]# bash function.sh
please input username or quit: tt
501 /bin/bash
please input username or quit: admin
500 /bin/bash
please input username or quit: quit
8、写一个脚本,完成如下功能(使用函数) (1) 提示用户输入一个可执行命令的名字;获取此命令依赖的所有库文件; (2) 复制命令文件至/mnt/sysroot目录下的对应的rootfs的路径上,例如,如果复制的文件原路径是/usr/bin/useradd,则复制到/mnt/sysroot/usr/bin/目录中; (3) 复制此命令依赖的各库文件至/mnt/sysroot目录下的对应的rootfs的路径上;规则同上面命令相关的要求;”
#!/bin/bash
df() {
[ -d /mnt/sysroot$p ] || mkdir -pv /mnt/sysroot$p
}
read -p "please input a excutable command:" comm
p=$(dirname $(which $comm))
df
cp -r $(which $comm) /mnt/sysroot$p
for i in $(ldd $(which $comm ) | grep -o "/.*[^[:space:]]$" |awk -F' ' '{print $1}')
do
p=$(dirname $i)
df
cp $i /mnt/sysroot$p
done
[root@www tmp]# bash -x copyter.sh ls
+ read -p 'please input a excutable command:' comm
please input a excutable command:ls
+++ which ls
++ dirname /bin/ls
+ path1=/bin
+ p=/bin
+ df
+ '[' -d /mnt/sysroot/bin ']'
++ which ls
+ cp -r /bin/ls /mnt/sysroot/bin
++ awk '-F ' '{print $1}'
++ grep -o '/.*[^[:space:]]$'
+++ which ls
++ ldd /bin/ls
+ for i in '$(ldd $(which $comm ) | grep -o "/.*[^[:space:]]$" |awk -F'\'' '\'' '\''{print $1}'\'')'
++ dirname /lib64/libselinux.so.1
+ path2=/lib64
+ p=/lib64
+ df
+ '[' -d /mnt/sysroot/lib64 ']'
+ cp /lib64/libselinux.so.1 /mnt/sysroot/lib64
+ for i in '$(ldd $(which $comm ) | grep -o "/.*[^[:space:]]$" |awk -F'\'' '\'' '\''{print $1}'\'')'
++ dirname /lib64/librt.so.1
+ path2=/lib64
+ p=/lib64
+ df
+ '[' -d /mnt/sysroot/lib64 ']'
+ cp /lib64/librt.so.1 /mnt/sysroot/lib64
+ for i in '$(ldd $(which $comm ) | grep -o "/.*[^[:space:]]$" |awk -F'\'' '\'' '\''{print $1}'\'')'
++ dirname /lib64/libcap.so.2
+ path2=/lib64
+ p=/lib64
+ df
+ '[' -d /mnt/sysroot/lib64 ']'
+ cp /lib64/libcap.so.2 /mnt/sysroot/lib64
+ for i in '$(ldd $(which $comm ) | grep -o "/.*[^[:space:]]$" |awk -F'\'' '\'' '\''{print $1}'\'')'
++ dirname /lib64/libacl.so.1
+ path2=/lib64
+ p=/lib64
+ df
+ '[' -d /mnt/sysroot/lib64 ']'
+ cp /lib64/libacl.so.1 /mnt/sysroot/lib64
+ for i in '$(ldd $(which $comm ) | grep -o "/.*[^[:space:]]$" |awk -F'\'' '\'' '\''{print $1}'\'')'
++ dirname /lib64/libc.so.6
+ path2=/lib64
+ p=/lib64
+ df
+ '[' -d /mnt/sysroot/lib64 ']'
+ cp /lib64/libc.so.6 /mnt/sysroot/lib64
+ for i in '$(ldd $(which $comm ) | grep -o "/.*[^[:space:]]$" |awk -F'\'' '\'' '\''{print $1}'\'')'
++ dirname /lib64/libdl.so.2
+ path2=/lib64
+ p=/lib64
+ df
+ '[' -d /mnt/sysroot/lib64 ']'
+ cp /lib64/libdl.so.2 /mnt/sysroot/lib64
+ for i in '$(ldd $(which $comm ) | grep -o "/.*[^[:space:]]$" |awk -F'\'' '\'' '\''{print $1}'\'')'
++ dirname /lib64/ld-linux-x86-64.so.2
+ path2=/lib64
+ p=/lib64
+ df
+ '[' -d /mnt/sysroot/lib64 ']'
+ cp /lib64/ld-linux-x86-64.so.2 /mnt/sysroot/lib64
+ for i in '$(ldd $(which $comm ) | grep -o "/.*[^[:space:]]$" |awk -F'\'' '\'' '\''{print $1}'\'')'
++ dirname /lib64/libpthread.so.0
+ path2=/lib64
+ p=/lib64
+ df
+ '[' -d /mnt/sysroot/lib64 ']'
+ cp /lib64/libpthread.so.0 /mnt/sysroot/lib64
+ for i in '$(ldd $(which $comm ) | grep -o "/.*[^[:space:]]$" |awk -F'\'' '\'' '\''{print $1}'\'')'
++ dirname /lib64/libattr.so.1
+ path2=/lib64
+ p=/lib64
+ df
+ '[' -d /mnt/sysroot/lib64 ']'
+ cp /lib64/libattr.so.1 /mnt/sysroot/lib64
原创文章,作者:diglinux,如若转载,请注明出处:http://www.178linux.com/72180

