1、请详细描述CentOS系统的启动流程(详细到每个过程系统做了哪些事情) 系统启动流程: POST --> BootSequence(BIOS) --> BootLoader (MBR) --> Kernel (ramdisk) --> rootfs (readonly) --> /sbin/init () 1. POST: 加电自检 2. BootSequence:按次序查找各引导设备,第一个有引导程序的设备即为本次启动要用的设备 3. BootLoader:加载设备的引导程序 4. Kernel:探测可识别到的所有硬件设备,加载硬件驱动程序(有可能借助于ramdisk加载驱动) 5. rootfs:以只读方式挂载根文件系统 6. init:运行用户空间的第一个应用程序:/sbin/init 进行初始化操作 2、为运行于虚拟机上的CentOS 6添加一块新硬件,提供两个主分区; (1) 为硬盘新建两个主分区;并为其安装grub; (2) 为硬盘的第一个主分区提供内核和ramdisk文件; 为第二个分区提供rootfs; (3) 为rootfs提供bash、ls、cat程序及所依赖的库文件; (4) 为grub提供配置文件; (5) 将新的硬盘设置为第一启动项并能够正常启动目标主机; 为硬盘新建两个主分区 [root@localhost ~]# fdisk /dev/sdg Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 1 First cylinder (1-3916, default 1): 1 Last cylinder, +cylinders or +size{K,M,G} (1-3916, default 3916): +200M Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 2 First cylinder (27-3916, default 27): Using default value 27 Last cylinder, +cylinders or +size{K,M,G} (27-3916, default 3916): +10G [root@localhost ~]# partx -a /dev/sdg [root@localhost ~]# mke2fs -t ext4 /dev/sdg1 [root@localhost ~]# mke2fs -t ext4 /dev/sdg2 挂载第一个分区,并安装grub [root@localhost mnt]# mkdir /mnt/boot [root@localhost mnt]# mount /dev/sdg1 /mnt/boot [root@localhost mnt]# grub-install --root-directory=/mnt /dev/sdg 复制内核和ramdisk文件到第一个分区 [root@localhost mnt]# cp /boot/vmlinuz-2.6.32-642.el6.x86_64 /mnt/boot/vmlinuz [root@localhost mnt]# cp /boot/initramfs-2.6.32-642.el6.x86_64.img /mnt/boot/initramfs.img 配置grub.conf文件 [root@localhost mnt]# vim /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 为第二个分区提供rootfs [root@localhost mnt]# mkdir /mnt/sysroot [root@localhost mnt]# mount /dev/sdg2 /mnt/sysroot [root@localhost sysroot]# mkdir -pv /mnt/sysroot/{etc,bin,sbin,lib,lib64,dev,proc,sys,tmp,var,usr,home,root,mnt,media} 复制命令及其库文件 [root@localhost mnt]# vim /tmp/cpcmd2.sh #!/bin/bash dst="/mnt/sysroot" [ ! -d $dst ] && mkdir $dst copycmd(){ if `which $1 &>/dev/null`;then cmddir=`which --skip-alias $1` dir=`dirname $cmddir` mkdir -pv ${dst}${dir} cp $cmddir ${dst}${dir}/ else echo "command not found" return 0 fi } copylib(){ liblist=$(ldd `which --skip-alias $1`|grep -Eo "/[^[:space:]]+") for libfile in $liblist;do libdir=`dirname $libfile` [ ! -d ${dst}${libdir} ] && mkdir ${dst}${libdir} cp $libfile ${dst}${libdir} done } read -p "please input a command:" cmd while [ "$cmd" != "quit" ];do copycmd $cmd copylib $cmd echo "files copied" read -p "please input a command:" cmd done [root@localhost mnt]# bash /tmp/cpcmd2.sh [root@test tmp]# bash cpcmd.sh please input a command:bash files copied please input a command:ls files copied please input a command:cat files copied please input a command:quit 新建虚拟机,将新的硬盘设置为第一启动项,启动bash并测试ls,cat命令 3、制作一个kickstart文件以及一个引导镜像。描述其过程。 安装kickstart程序 [root@localhost ~]# yum install system-config-kickstart 制作ks.cfg文件 [root@localhost ~]# system-config-kickstart & #将ks.cfg保存在root 目录下 [root@localhost ~]# cat ks.cfg #platform=x86, AMD64, or Intel EM64T #version=DEVEL # Firewall configuration firewall --enabled --service=ssh # Install OS instead of upgrade install # Use CDROM installation media cdrom # Root password rootpw --iscrypted $1$7e5bbhUU$5ikWQctYMuPQc3O9Wko9d/ # System authorization information auth --useshadow --passalgo=sha512 # Use graphical install graphical firstboot --disable # System keyboard keyboard us # System language lang en_US # SELinux configuration selinux --enforcing # Installation logging level logging --level=info # System timezone timezone Asia/Shanghai # System bootloader configuration bootloader --append="crashkernel=auto rhgb quiet" --location=mbr --driveorder="sda" # Partition clearing information clearpart --all --initlabel # Disk partitioning information part /boot --fstype="ext4" --size=200 part / --fstype="ext4" --size=10000 part swap --fstype="swap" --size=1024 挂载安装光盘 [root@localhost ~]# mount /dev/cdrom /media/cdrom/ [root@localhost ~]# mkdir -p /root/auto_install [root@localhost ~]# cp -arf /media/cdrom /root/auto_install/ 定位ks.cfg文件 [root@localhost ~]# cp /root/ks.cfg /root/auto_install/cdrom/ks.cfg [root@localhost ~]# chmod +w /root/auto_install/cdrom/isolinux/isolinux.cfg [root@localhost ~]# vim /root/auto_install/cdrom/isolinux/isolinux.cfg label linux menu label ^Install or upgrade an existing system menu default kernel vmlinuz append initrd=initrd.img ks=cdrom:/ks.cfg # 在这里添加ks文件路径参数 制作自动安装光盘 [root@localhost ~]# mkisofs -R -J -T -v --no-emul-boot --boot-load-size 4 \ --boot-info-table -V "CentOS 6 Auto install" \ -b isolinux/isolinux.bin -c isolinux/boot.cat \ -o /root/centos6_auto_install.iso \ /root/auto_install/cdrom/ 4、写一个脚本 (1) 能接受四个参数:start, stop, restart, status start: 输出“starting 脚本名 finished.” ... (2) 其它任意参数,均报错退出; [root@mail tmp]# vim start.sh #!/bin/bash case $1 in start) echo "starting $0 finished." ;; stop) echo "stop $0 finished" ;; restart) echo "restart $0 finished" ;; status) echo "$0 status" ;; *) echo "error,please input {start|stop|restart|status}" exit 1 esac [root@mail tmp]# bash /tmp/start.sh start 5、写一个脚本,判断给定的用户是否登录了当前系统; (1) 如果登录了,则显示用户登录,脚本终止; (2) 每3秒钟,查看一次用户是否登录; [root@mail tmp]# vim login.sh #!/bin/bash read -p "Please input a user:" 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 login" else echo "$user is not exist" fi [root@mail tmp]# bash login.sh 6、写一个脚本,显示用户选定要查看的信息; cpu) display cpu info mem) display memory info disk) display disk info quit) quit 非此四项选择,则提示错误,并要求用户重新选择,只到其给出正确的选择为止; [root@mail tmp]# vim info.sh #!/bin/bash read -p "input cpu|mem|disk|quit :" option while [ "$option" != "cpu" -a "$option" != "mem" -a "$option" != "disk" -a "$option" != "quit" ];do read -p "input cpu|mem|disk|quit :" option done if [ "$option" == "cpu" ];then lscpu elif [ "$option" == "mem" ];then free -m elif [ "$option" == "disk" ];then fdisk -l /dev/[hs]d[a-z] else echo "quit" exit 0 fi [root@mail tmp]# bash info.sh 7、写一个脚本 (1) 用函数实现返回一个用户的UID和SHELL;用户名通过参数传递而来; (2) 提示用户输入一个用户名或输入“quit”退出; 当输入的是用户名,则调用函数显示用户信息; 当用户输入quit,则退出脚本;进一步地:显示键入的用户相关信息后,再次提醒输出用户名或quit: [root@test tmp]# vim userinfo.sh #!/bin/bash userinfor(){ uid=`id -u $username` ushell=`grep "^$username\>" /etc/passwd |cut -d: -f7` echo "USERNAME:$username UID:$uid SHELL:$ushell" } read -p "Please input a username|quit :" username while [ "$username" != "quit" ];do id $username &>/dev/null if [ $? -eq 0 ] ;then userinfor $username read -p "Please input a username|quit :" username else echo "user not exist" read -p "Please input a username|quit :" username fi done [root@test tmp]# bash userinfo.sh 8、写一个脚本,完成如下功能(使用函数) (1) 提示用户输入一个可执行命令的名字;获取此命令依赖的所有库文件; (2) 复制命令文件至/mnt/sysroot目录下的对应的rootfs的路径上,例如,如果复制的文件原路径是/usr/bin/useradd,则复制到/mnt/sysroot/usr/bin/目录中; (3) 复制此命令依赖的各库文件至/mnt/sysroot目录下的对应的rootfs的路径上;规则同上面命令相关的要求; [root@localhost mnt]# vim /tmp/cpcmd2.sh #!/bin/bash dst="/mnt/sysroot" [ ! -d $dst ] && mkdir $dst copycmd(){ if `which $1 &>/dev/null`;then cmddir=`which --skip-alias $1` dir=`dirname $cmddir` mkdir -pv ${dst}${dir} cp $cmddir ${dst}${dir}/ else echo "command not found" return 0 fi } copylib(){ liblist=$(ldd `which --skip-alias $1`|grep -Eo "/[^[:space:]]+") for libfile in $liblist;do libdir=`dirname $libfile` [ ! -d ${dst}${libdir} ] && mkdir ${dst}${libdir} cp $libfile ${dst}${libdir} done } read -p "please input a command:" cmd while [ "$cmd" != "quit" ];do copycmd $cmd copylib $cmd echo "files copied" read -p "please input a command:" cmd done [root@localhost mnt]# bash /tmp/cpcmd2.sh
原创文章,作者:N22_上海_长清,如若转载,请注明出处:http://www.178linux.com/54836