-
请详细描述CentOS系统的启动流程(详细到每个过程系统做了哪些事情)
1)POST:加电自检
2)BOOT Sequence:按次序查找各引导设备,第一个有grub的设备即为本次启动用到设备
3)加载kernel,先自身初始化,探测可识别到的所有硬件设备,加载硬件驱动程序,以只读方式挂载根文件系统,运行用户空间的第一个应用程序:/sbin/init
4)init启动后,首先读取/etc/inittab来读取默认级别,执行初始化系统脚本 /etc/rc.d/rc.sysinit 来初始化脚本,然后执行 /etc/rc.d/rc 脚本,最后执行/etc/rc.d/rc.local脚本,系统启动完成
2、为运行于虚拟机上的CentOS 6添加一块新硬件,提供两个主分区;
(1) 为硬盘新建两个主分区;并为其安装grub;
(2) 为硬盘的第一个主分区提供内核和ramdisk文件; 为第二个分区提供rootfs;
(3) 为rootfs提供bash、ls、cat程序及所依赖的库文件;
(4) 为grub提供配置文件;
(5) 将新的硬盘设置为第一启动项并能够正常启动目标主机;
提供两块主分区,安装grub:
~]# 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 0x47f000ad. 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-2610, default 1): Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +1G Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 2 First cylinder (133-2610, default 133): Using default value 133 Last cylinder, +cylinders or +size{K,M,G} (133-2610, default 2610): +2G Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. ~]# partx /dev/sdb # 1: 63- 2120579 ( 2120517 sectors, 1085 MB) # 2: 2120580- 6329609 ( 4209030 sectors, 2155 MB) # 3: 0- -1 ( 0 sectors, 0 MB) # 4: 0- -1 ( 0 sectors, 0 MB) ~]# partx /dev/sdb # 1: 63- 2120579 ( 2120517 sectors, 1085 MB) # 2: 2120580- 6329609 ( 4209030 sectors, 2155 MB) # 3: 0- -1 ( 0 sectors, 0 MB) # 4: 0- -1 ( 0 sectors, 0 MB) ~]# fdisk -l /dev/sdb Disk /dev/sdb: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 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: 0x47f000ad Device Boot Start End Blocks Id System /dev/sdb1 1 132 1060258+ 83 Linux /dev/sdb2 133 394 2104515 83 Linux ~]#mkfs.ext4 /dev/sdb1 ~]#mkfs.ext4 /dev/sdb2 ~]# mount /dev/sdb1 /mnt/boot/ ~]# mount /dev/sdb2 /mnt/sysroot/ ~]# df -h |grep mnt /dev/sdb1 1020M 34M 935M 4% /mnt/boot /dev/sdb2 2.0G 68M 1.9G 4% /mnt/sysroot ~]# grub-install --root-directory=/mnt /dev/sdb
为硬盘的第一个主分区提供内核和ramdisk文件; 为第二个分区提供rootfs:
~]# cp -r /boot/vmlinuz-2.6.32-431.el6.x86_64 /mnt/boot/vmlinuz ~]# ls /mnt/boot/ grub lost+found vmlinuz ~]# cp /boot/initramfs-2.6.32-431.el6.x86_64.img /mnt/boot/initramfs.img ~]# ls /mnt/boot/ grub initramfs.img lost+found vmlinuz
为rootfs提供bash、ls、cat程序及所依赖的库文件:
~]# cp -r /bin/{bash,ls,cat} /mnt/sysroot/bin/ ~]# cp $(ldd /bin/bash |awk '$3~/^\/lib64/{print $3}') /mnt/sysroot/lib64 ~]# cp $(ldd /bin/ls |awk '$3~/^\/lib64/{print $3}') /mnt/sysroot/lib64 ~]# cp $(ldd /bin/cat |awk '$3~/^\/lib64/{print $3}') /mnt/sysroot/lib64 [root@localhost sysroot]# cat /mnt/boot/grub/grub.conf default=0 timeout=5 title CentOS (Express) root (hd0,0) kernel /vmlinuz ro root=/dev/sda2 selinux=0 init=/bin/bash initrd /initramfs.img
在bios中选择sdb作为启动盘
4、写一个脚本
(1) 能接受四个参数:start, stop, restart, statusstart: 输出“starting 脚本名 finished.”…
(2) 其它任意参数,均报错退出;
#!/bin/bash read -p "Please input start,stop,restart or statusstat:" option case "$option" in start) echo "starting "$0" finished!" ;; stop) echo "stop "$0" finished!" ;; restart) echo "restart "$0" finished!" ;; startusstart) echo "statusstart "$0" finished!" ;; *) echo "unknow options!" ;; esac
5、写一个脚本,判断给定的用户是否登录了当前系统;
(1) 如果登录了,则显示用户登录,脚本终止;
(2) 每3秒钟,查看一次用户是否登录;
#!/bin/bash echo "check user login!" echo read -p "please input the username:" user id $user &> /dev/null if [ $? -eq 0 ];then while true;do if who |grep "^$user" &> /dev/null;then break fi sleep 3 done echo "$user is loginon!" else echo "unknow user!" fi
6、写一个脚本,显示用户选定要查看的信息;
cpu) display cpu info
mem) display memory info
disk) display disk info
quit) quit
非此四项选择,则提示错误,并要求用户重新选择,只到其给出正确的选择为止;
#!/bin/bash echo "show cpu,mem and disk imformations,or exit in quit" read -p "input cpu,mem,disk or quit:" option until [ "$option" == "quit" ];do case "$option" in cpu) lscpu break ;; mem) cat /proc/meminfo break ;; disk) fdisk -l break ;; *) echo "unknow options,please input again!" read -p "input cpu,mem,disk or quit:" option esac done
7、写一个脚本
(1) 用函数实现返回一个用户的UID和SHELL;用户名通过参数传递而来;
(2) 提示用户输入一个用户名或输入“quit”退出;当输入的是用户名,则调用函数显示用户信息;当用户输入quit,则退出脚本;
进一步地:显示键入的用户相关信息后,再次提醒输出用户名或quit:
#!/bin/bash info() { id $username &> /dev/null if [ $? -ne 0 ];then echo "the user is not exist!" else grep "^$username" /etc/passwd |awk -F: '{printf "USER ID:%u\nUSER SHELL:%s\n" ,$3,$NF}' fi } read -p "please input a username or quit:" username while true;do if [ $username == "quit" ];then exit 0 else info read -p "please input a username or quit:" username fi done
8、写一个脚本,完成如下功能(使用函数)
(1) 提示用户输入一个可执行命令的名字;获取此命令依赖的所有库文件;
(2) 复制命令文件至/mnt/sysroot目录下的对应的rootfs的路径上,例如,如果复制的文件原路径是/usr/bin/useradd,则复制到/mnt/sysroot/usr/bin/目录中;
(3) 复制此命令依赖的各库文件至/mnt/sysroot目录下的对应的rootfs的路径上;规则同上面命令相关的要求;(ldd /bin/ls | grep -o "/[^[:space:]]\{1,\}"可筛选出命令对应的库文件)
#!/bin/bash declare -i filesum=0 checkcom() { which $com &> /dev/null if [ $? -ne 0 ];then echo "unknow command!" exit 2 else ldd $(which $com |grep "$com$") |grep -o "/[^[:space:]]\{1,\}" fi } read -p "input a available command:" com checkcom [ ! -d /mnt/sysroot/rootfs ] && mkdir -p /mnt/sysroot/rootfs for file in $(ldd $(which $com |grep "$com$") |grep -o "/[^[:space:]]\{1,\}");do [ ! -d /mnt/sysroot/rootfs${file%/*} ] && mkdir /mnt/sysroot/rootfs${file%/*} cp -a -s $file /mnt/sysroot/rootfs$file let filesum++ done echo "the lib file copy ok,total files:$filesum"
原创文章,作者:Net20_有马,如若转载,请注明出处:http://www.178linux.com/44572