1、请详细描述CentOS系统的启动流程(详细到每个过程系统做了哪些事情)
2、为运行于虚拟机上的CentOS 6添加一块新硬件,提供两个主分区; (1) 为硬盘新建两个主分区;并为其安装grub; (2) 为硬盘的第一个主分区提供内核和ramdisk文件; 为第二个分区提供rootfs; (3) 为rootfs提供bash、ls、cat程序及所依赖的库文件; (4) 为grub提供配置文件; (5) 将新的硬盘设置为第一启动项并能够正常启动目标主机;
添加一块硬盘并创建两个主分区 ~]# fdisk /dev/sdb ~]# fdisk -l | grep sdb Disk /dev/sdb: 21.5 GB, 21474836480 bytes /dev/sdb1 1 10 80293+ 83 Linux /dev/sdb2 11261020884500 83 Linux 创建文件系统 ~]# mke2fs -t ext4 /dev/sdb1 ~]# mke2fs -t ext4 /dev/sdb2 挂载 ~]# mount /dev/sdb1 /mnt 安装grub至分区1 ~]# grub-install --root-directory=/mnt /dev/sdb Probing devices to guess BIOS drives. This may take a long time. Installation finished. No error reported. This is the contents of the device map /mnt/boot/grub/device.map. Check if this is correct or not. If any of the lines is incorrect, fix it and re-run the script `grub-install'. (fd0)/dev/fd0 (hd0)/dev/sda (hd1)/dev/sdb 复制虚拟文件系统 ~]# cp /boot/initramfs-2.6.32-431.el6.x86_64.img /mnt/initramfs 复制内核文件 ~]# cp /boot/vmlinuz-2.6.32-431.el6.x86_64 /mnt/vmlinuz 创建grub.conf文件 ~]# vim /mnt/boot/grub/grub.conf default=0 timeout=5 title CentOS6(test) root (hd0,0) kernel /vmlinuz ro root=/dev/sdb2 selinux=0 init=/bin/bash initrd /initramfs 挂载sdb2,并创建rootfs相关目录 ~]# umount /dev/sdb1 ~]# mount /dev/sdb2 /mnt ~]# mkdir -p /mnt/{bin,sbin,lib,lib64,etc,home,root,media,mnt,dev,tmp} ~]# mkdir -p /mnt/{usr/{bin,sbin,lib,lib64},var/{lib,lib64,log,local,cache},proc,sys,selinux} ~]# which ls alias ls='ls --color=auto' /bin/ls ~]# which bash /bin/bash ~]# which cat /bin/cat ~]# cp /bin/{bash,ls,cat} /mnt/bin ldd命令: - print shared library dependencies ldd [OPTION]... FILE... ~]# ldd /bin/{bash,ls,cat}|grep -Eo "/lib.*[[:space:]]"| sort -u /lib64/ld-linux-x86-64.so.2 /lib64/libacl.so.1 /lib64/libattr.so.1 /lib64/libcap.so.2 /lib64/libc.so.6 /lib64/libdl.so.2 /lib64/libpthread.so.0 /lib64/librt.so.1 /lib64/libselinux.so.1 /lib64/libtinfo.so.5 ~]# cp `ldd /bin/{bash,ls,cat}|grep -Eo "/lib.*[[:space:]]"| sort -u` /mnt/lib64 ~]# sync 同步sync - flush file system buffers ~]#init 6 重启后进入bios设置 调整硬盘启动顺序后保存退出。
调整硬盘启动顺序
已读取到自定义的grub.conf
正常开启
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) 其它任意参数,均报错退出;
~]# cat service.sh #!/bin/bash # case $1 in start) echo "start $0 finished." ;; stop) echo "stop $0 finished." ;; restart) echo "restart $0 finished." ;; status) echo "status $0 ok." ;; *) echo "error" exit 1 ;; esac 执行结果: ~]# bash service.sh start start service.sh finished. ~]# bash service.sh stop stop service.sh finished. ~]# bash service.sh status status service.sh ok. ~]# bash service.sh error 备注:$0参数为脚本本身
5、写一个脚本,判断给定的用户是否登录了当前系统; (1) 如果登录了,则显示用户登录,脚本终止; (2) 每3秒钟,查看一次用户是否登录;
#!/bin/bash # if [ $# -lt 1 ];then echo "at least one arg" exit 1 fi while true;do if w | grep $1 &>/dev/null;then echo "the user $1 is login" break else echo "waiting" sleep 3 fi done 执行结果: ~]# bash islogin.sh at least one arg ~]# bash islogin.sh hadoop waiting the user hadoop is login
6、写一个脚本,显示用户选定要查看的信息; cpu) display cpu info mem) display memory info disk) display disk info quit) quit 非此四项选择,则提示错误,并要求用户重新选择,只到其给出正确的选择为止;
#!/bin/bash # cat << EOF cpu) display cpu information mem) display memory infomation disk) display disks information quit) quit =============================== EOF read -p "Enter your option: " option while [ "$option" != "cpu" -a "$option" != "mem" -a "$option" != "disk" -a "$option" != "quit" ]; do echo "cpu, mem, disk, quit" read -p "Enter your option again: " 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 执行结果: ~]# bash test.sh cpu) display cpu information mem) display memory infomation disk) display disks information quit) quit =============================== Enter your option: cpu Architecture: x86_64 CPU op-mode(s):32-bit, 64-bit Byte Order:Little Endian CPU(s):2 On-line CPU(s) list: 0,1 Thread(s) per core:1 Core(s) per socket:1 Socket(s): 2 NUMA node(s): 1 Vendor ID: GenuineIntel CPU family:6 Model: 42 Stepping: 7 CPU MHz: 2392.226 BogoMIPS: 4784.45 Hypervisor vendor: VMware Virtualization type: full L1d cache: 32K L1i cache: 32K L2 cache: 256K L3 cache: 3072K NUMA node0 CPU(s): 0,1 ~]# bash test.sh cpu) display cpu information mem) display memory infomation disk) display disks information quit) quit =============================== Enter your option: disk Disk /dev/sda: 53.7 GB, 53687091200 bytes 255 heads, 63 sectors/track, 6527 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: 0x00085f16 Device Boot Start End Blocks Id System /dev/sda1 * 1 64 512000 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 64652851915776 8e Linux LVM 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: 0x22bd7f68 Device Boot Start End Blocks Id System /dev/sdb1 1 10 80293+ 83 Linux /dev/sdb2 11261020884500 83 Linux ~]# bash test.sh cpu) display cpu information mem) display memory infomation disk) display disks information quit) quit =============================== Enter your option: mem total used free sharedbuffers cached Mem: 980124856 0 10 41 -/+ buffers/cache: 72908 Swap: 1983 0 1983 ~]# bash test.sh cpu) display cpu information mem) display memory infomation disk) display disks information quit) quit =============================== Enter your option: quit quit ~]# bash test.sh cpu) display cpu information mem) display memory infomation disk) display disks information quit) quit =============================== Enter your option: dddd cpu, mem, disk, quit Enter your option again: ssss cpu, mem, disk, quit Enter your option again: quit quit
7、写一个脚本 (1) 用函数实现返回一个用户的UID和SHELL;用户名通过参数传递而来; (2) 提示用户输入一个用户名或输入“quit”退出; 当输入的是用户名,则调用函数显示用户信息; 当用户输入quit,则退出脚本;进一步地:显示键入的用户相关信息后,再次提醒输出用户名或quit:
#!/bin/bash # function userinfo { if id $username &>/dev/null;then id=$(id -u $username) shell=$(grep "^$username\>" /etc/passwd | cut -d: -f7) echo "$username id is $id,shell is $shell" else echo "none user" fi } while true;do read -p "please enter username or quit:" username if [ $username == "quit" ];then exit 0 else userinfo $username fi done 执行结果: ~]# bash userinfo.sh please enter username or quit:vide none user please enter username or quit:void void id is 504,shell is /bin/bash please enter username or quit:test test id is 500,shell is /bin/bash please enter username or quit:quit
8、写一个脚本,完成如下功能(使用函数) (1) 提示用户输入一个可执行命令的名字;获取此命令依赖的所有库文件; (2) 复制命令文件至/mnt/sysroot目录下的对应的rootfs的路径上,例如,如果复制的文件原路径是/usr/bin/useradd,则复制到/mnt/sysroot/usr/bin/目录中; (3) 复制此命令依赖的各库文件至/mnt/sysroot目录下的对应的rootfs的路径上;规则同上面命令相关的要求;
#!/bin/bash # function pathcopy { if ! which --skip-alias $input &>/dev/null;then echo "no such command" exit 1 else path=$(which --skip-alias $input) [ -d /mnt/sysroot$path ] || mkdir /mnt/sysroot$path -p cp $path /mnt/sysroot$path fi } function libcopy { for lib in $(ldd `which --skip-alias $input` | grep -Eo "/lib.*[[:space:]]");do path2=$(echo "$lib" | grep -Eo "/.*/") [ -d /mnt/sysroot$path2 ] || mkdir /mnt/sysroot$path2 -p [ -f /mnt/sysroot$lib ] || cp $lib /mnt/sysroot$lib done } read -p "please enter command: " input pathcopy $input libcopy $input 运行结果: ~]# bash ldd.sh please enter command: cd no such command ~]# bash ldd.sh please enter command: cp ~]# cd /mnt/sysroot/ ~]# ls bin lib64 ~]# cd bin bin]# ls cp ls bin]# cd ../lib64/ lib64]# ls ld-linux-x86-64.so.2 libacl.so.1 libattr.so.1 libcap.so.2 libc.so.6 libdl.so.2 libpthread.so.0 librt.so.1 libselinux.so.1
原创文章,作者:N23-苏州-void,如若转载,请注明出处:http://www.178linux.com/61368
评论列表(1条)
赞,建议把自动化装机和后面的脚本独立成两个专题会比较好。其中在自动化装机中,企业定制系统很重要,图形化界面一般不需要安装的。
继续加油~