1、请详细描述CentOS系统的启动流程(详细到每个过程系统做了哪些事情)
2、为运行于虚拟机上的CentOS 6添加一块新硬件,提供两个主分区;
(1) 为硬盘新建两个主分区;并为其安装grub;
#创建两个分区,/dev/sdb1为500M,/dev/sdb2为5G [root@centos6 mnt]# fdisk -l /dev/sdb Disk /dev/sdb: 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: 0x473aab9e Device Boot Start End Blocks Id System /dev/sdb1 1 65 522081 83 Linux /dev/sdb2 66 719 5253255 83 Linux
#格式化分区为ext4格式 [root@centos6 script]# mke2fs -t ext4 /dev/sdb1 [root@centos6 script]# mke2fs -t ext4 /dev/sdb2
#创建挂载目录并挂载分区 [root@centos6 script]# mkdir -p /mnt/{boot,sysroot} [root@centos6 script]# mount /dev/sdb1 /mnt/boot/ [root@centos6 script]# mount /dev/sdb2 /mnt/sysroot/ [root@centos6 grub]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda2 116G 1.7G 109G 2% / tmpfs 364M 0 364M 0% /dev/shm /dev/sda1 477M 32M 420M 7% /boot /dev/sdb1 486M 29M 432M 7% /mnt/boot /dev/sdb2 4.9G 11M 4.6G 1% /mnt/sysroot
(2) 为硬盘的第一个主分区提供内核和ramdisk文件; 为第二个分区提供rootfs;
#复制核心文件和虚拟镜像文件到新的boot目录下 [root@centos6 grub]#cp /boot/vmlinuz-2.6.32-642.el6.x86_64 /mnt/boot/vmlinuz [root@centos6 grub]#cp /boot/initramfs-2.6.32-642.el6.x86_64.img /mnt/boot/initramfs.img
#创建新的根目录下必要的文件夹 [root@centos6 grub]# cd /mnt/sysroot/ [root@centos6 sysroot]# mkdir bin dev etc home lib lib64 media mnt opt proc root sbin selinux srv sys tmp usr var
(3) 为rootfs提供bash、ls、cat程序及所依赖的库文件;
#查看bash、ls、cat命令所需要用到的动态链接库文件 [root@centos6 sysroot]# ldd /bin/bash linux-vdso.so.1 => (0x00007fffc89c6000) libtinfo.so.5 => /lib64/libtinfo.so.5 (0x00007f688f4e3000) libdl.so.2 => /lib64/libdl.so.2 (0x00007f688f2df000) libc.so.6 => /lib64/libc.so.6 (0x00007f688ef4a000) /lib64/ld-linux-x86-64.so.2 (0x00007f688f70d000) [root@centos6 sysroot]# ldd $(which --skip-alias ls) linux-vdso.so.1 => (0x00007ffc5dd97000) libselinux.so.1 => /lib64/libselinux.so.1 (0x00007f8dd942a000) librt.so.1 => /lib64/librt.so.1 (0x00007f8dd9222000) libcap.so.2 => /lib64/libcap.so.2 (0x00007f8dd901d000) libacl.so.1 => /lib64/libacl.so.1 (0x00007f8dd8e15000) libc.so.6 => /lib64/libc.so.6 (0x00007f8dd8a81000) libdl.so.2 => /lib64/libdl.so.2 (0x00007f8dd887c000) /lib64/ld-linux-x86-64.so.2 (0x00007f8dd9652000) libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f8dd865f000) libattr.so.1 => /lib64/libattr.so.1 (0x00007f8dd845a000) [root@centos6 sysroot]# ldd $(which --skip-alias cat) linux-vdso.so.1 => (0x00007ffc04752000) libc.so.6 => /lib64/libc.so.6 (0x00007f6754cba000) /lib64/ld-linux-x86-64.so.2 (0x00007f6755057000)
#复制动态链接库文件到新的根目录下 [root@centos6 bin]# cp /bin/cat /mnt/sysroot/bin/ [root@centos6 bin]# cp /bin/ls /mnt/sysroot/bin/ [root@centos6 sysroot]# ldd $(which --skip-alias bash) |grep -o "/.*\.[[:digit:]]"|xargs -I {} cp {} /mnt/sysroot/lib64 [root@centos6 sysroot]# ldd $(which --skip-alias ls) |grep -o "/.*\.[[:digit:]]"|xargs -I {} cp {} /mnt/sysroot/lib64 [root@centos6 sysroot]# ldd $(which --skip-alias cat) |grep -o "/.*\.[[:digit:]]"|xargs -I {} cp {} /mnt/sysroot/lib64 [root@centos6 sysroot]# ll /mnt/sysroot/lib64/ total 2560 -rwxr-xr-x 1 root root 154664 Mar 1 05:38 ld-linux-x86-64.so.2 -rwxr-xr-x 1 root root 31280 Mar 1 05:38 libacl.so.1 -rwxr-xr-x 1 root root 18712 Mar 1 05:38 libattr.so.1 -rwxr-xr-x 1 root root 16600 Mar 1 05:38 libcap.so.2 -rwxr-xr-x 1 root root 1923352 Mar 1 05:38 libc.so.6 -rwxr-xr-x 1 root root 19536 Mar 1 05:38 libdl.so.2 -rwxr-xr-x 1 root root 142688 Mar 1 05:38 libpthread.so.0 -rwxr-xr-x 1 root root 43944 Mar 1 05:38 librt.so.1 -rwxr-xr-x 1 root root 122056 Mar 1 05:38 libselinux.so.1 -rwxr-xr-x 1 root root 132408 Mar 1 05:21 libtinfo.so.5
#使用chroot命令切换根目录到/mnt/sysroot进行测试 [root@centos6 bin]# chroot /mnt/sysroot/ bash-4.1# ls bin dev etc home lib lib64 lost+found media mnt opt proc root sbin selinux srv sys tmp usr var bash-4.1# bash bash-4.1# cat <<EOF > hello world > EOF hello world
(4) 为grub提供配置文件;
#创建grub配置文件 [root@centos6 sysroot]# vim /mnt/boot/grub/grub.conf default=0 timeout=5 title CentOS (MyDIY) root (hd0,0) kernel /vmlinuz ro root=/dev/sdb2 init=/bin/bash initrd /initramfs.img
(5) 将新的硬盘设置为第一启动项并能够正常启动目标主机;
在BIOS中将新创建的磁盘作为第一启动设备
重启后进入grub,编辑kernel参数,设置selinux=0 (注意:该参数要放置在init之前)
编辑完成后,按b键启动后即可进入新建的系统。
3、制作一个kickstart文件以及一个引导镜像。描述其过程。
1)创建镜像生成目录并将光盘下isolinux目录copy到该目录下,并对其下的文件赋予写权限。
[root@centos6 ~]# mkdir -p /myboot/ [root@centos6 ~]# cp -a /mnt/cdrom/isolinux/ /myboot/ [root@centos6 ~]# cd /myboot/ [root@centos6 myboot]# chmod -R 777 isolinux/
2)创建kickstart配置文件
[root@centos6 myboot]# vim ks.cfg #platform=x86, AMD64, or Intel EM64T #version=DEVEL # Firewall configuration firewall --disabled # Install OS instead of upgrade install # Use network installation url --url="http://mirrors.aliyun.com/centos/6/os/x86_64" # Root password rootpw --iscrypted $1$ifhHlqT/$mZ5IcE3P2Nn54UG3i/SI// # System authorization information auth --useshadow --passalgo=sha512 # Use text mode install text firstboot --disable # System keyboard keyboard us # System language lang en_US # SELinux configuration selinux --disabled # Installation logging level logging --level=info # Reboot after installation reboot # System timezone timezone Asia/Shanghai # Network information network --bootproto=dhcp --device=eth0 --onboot=on # System bootloader configuration bootloader --append="crashkernel=auto rhgb quiet" --location=mbr --driveorder="sda" # Partition clearing information clearpart --all --drives=sda # Disk partitioning information part /boot --fstype=ext4 --size=500 part pv.01 --size=100000 volgroup myvg --pesize=4096 pv.01 logvol /home --fstype=ext4 --name=lv_home --vgname=myvg --size=5000 logvol / --fstype=ext4 --name=lv_root --vgname=myvg --size=50000 logvol swap --name=lv_swap --vgname=myvg --size=2000 logvol /usr --fstype=ext4 --name=lv_usr --vgname=myvg --size=15000 logvol /var --fstype=ext4 --name=lv_var --vgname=myvg --size=10000 %packages @core @server-policy @workstation-policy %end
3)创建光盘引导镜像
[root@centos6 ~]# cd /myboot/ [root@centos6 myboot]# mkisofs -R -J -T -v --no-emul-boot --boot-load-size 4 --boot-info-table -V "CentOS 6 x86_64 boot" -c isolinux/boot.cat -b isolinux/isolinux.bin -o /root/boot.iso ../myboot/
4)新建一台虚拟机并使用上一步创建的光盘引导镜像进行安装,在光盘启动菜单输入下面参数指定使用kickstart配置文件进行一键安装。
4、写一个脚本
(1) 能接受四个参数:start, stop, restart, status
start: 输出“starting 脚本名 finished.”
…
(2) 其它任意参数,均报错退出;
#!/bin/bash filename=$(basename $0) if [ $# -lt 1 ];then echo "Usage $filename {start | stop | restart| status}" exit 1 fi case $1 in start) echo "starting $filename finished." ;; stop) echo "stopping $filename finished." ;; restart) echo "restarting $filename finished." ;; status) echo "$filename is running..." ;; *) echo "Invalid argument!" exit 1 esac
执行结果: [root@centos6 script]# ./10_4.sh Usage 10_4.sh {start | stop | restart| status} [root@centos6 script]# ./10_4.sh start starting 10_4.sh finished. [root@centos6 script]# ./10_4.sh haha Invalid argument!
5、写一个脚本,判断给定的用户是否登录了当前系统;
(1) 如果登录了,则显示用户登录,脚本终止;
(2) 每3秒钟,查看一次用户是否登录;
#!/bin/bash if [ $# -lt 1 ];then echo "Usage $0 USERNAME" exit 1 fi while true;do if w|grep "^$1\>" &>/dev/null;then echo "User $1 is logged in." break else echo "User $1 is not logged in." sleep 3 fi done
执行结果: [root@centos6 script]# ./10_5.sh magedu User magedu is not logged in. User magedu is not logged in. User magedu is not logged in. User magedu is not logged in. User magedu is not logged in. User magedu is not logged in. User magedu is logged in.
6、写一个脚本,显示用户选定要查看的信息;
cpu) display cpu info
mem) display memory info
disk) display disk info
quit) quit
非此四项选择,则提示错误,并要求用户重新选择,只到其给出正确的选择为止;
cat <<EOF cpu) Display cpu info mem) Display memory info disk) Display disk info quit) Quit EOF while true;do read -p "Input your choice: " opt case $opt in cpu) lscpu exit 0 ;; mem) free -m exit 0 ;; disk) df -h exit 0 ;; quit) exit 1 ;; *) echo "Invalid input,please choice again!" continue esac done
执行结果: [root@centos6 script]# ./10_6.sh cpu) Display cpu info mem) Display memory info disk) Display disk info quit) Quit Input your choice: mem total used free shared buffers cached Mem: 726 362 364 0 24 243 -/+ buffers/cache: 94 631 Swap: 2047 0 2047 [root@centos6 script]# ./10_6.sh cpu) Display cpu info mem) Display memory info disk) Display disk info quit) Quit Input your choice: hahaha Invalid input,please choice again! Input your choice: quit
7、写一个脚本
(1) 用函数实现返回一个用户的UID和SHELL;用户名通过参数传递而来;
(2) 提示用户输入一个用户名或输入“quit”退出;
当输入的是用户名,则调用函数显示用户信息;
当用户输入quit,则退出脚本;进一步地:显示键入的用户相关信息后,再次提醒输出用户名或quit:
#!/bin/bash userinfo() { uid=$(id -u $1) shell=$(cat /etc/passwd|grep fangtao|awk -F: '{print $NF}') echo "UID: $uid" echo "SHELL: $shell" } while true;do read -p "Input username[input 'quit' if you don't want to continue]: " input [ "$input" == "quit" ] && exit 0 if [ -z "$input" ];then echo "Blank not allowed!" continue else if id $input &>/dev/null;then userinfo "$input" continue else echo "$input not exists!" continue fi fi done
执行结果: [root@centos6 script]# ./10_7.sh Input username[input 'quit' if you don't want to continue]: magedu UID: 500 SHELL: /bin/bash Input username[input 'quit' if you don't want to continue]: hahaha hahaha not exists! Input username[input 'quit' if you don't want to continue]: Blank not allowed! Input username[input 'quit' if you don't want to continue]: quit
8、写一个脚本,完成如下功能(使用函数)
(1) 提示用户输入一个可执行命令的名字;获取此命令依赖的所有库文件;
(2) 复制命令文件至/mnt/sysroot目录下的对应的rootfs的路径上,例如,如果复制的文件原路径是/usr/bin/useradd,则复制到/mnt/sysroot/usr/bin/目录中;
(3) 复制此命令依赖的各库文件至/mnt/sysroot目录下的对应的rootfs的路径上;规则同上面命令相关的要求;
#!/bin/bash chpath() { if which --skip-alias $1 &>/dev/null;then abscmd=$(which --skip-alias $1) cp --parents $abscmd $newroot ldd $abscmd|grep -o "/.*\.[[:digit:]]"|xargs -I {} cp --parents {} $newroot if [ $? -eq 0 ];then return 0 else return 2 fi else echo "Invalid command,please input again!" return 1 fi } newroot=/mnt/sysroot [ ! -d $newroot ] && mkdir $newroot while true;do read -p "Input command: " cmd chpath "$cmd" RETVAL=$? [ $RETVAL -ne 0 ] && continue || exit 0 done
[root@centos6 script]# ./10_8.sh Input command: haha Invalid command,please input again! Input command: bash [root@centos6 script]# ./10_8.sh Input command: ls #命令二进制文件和相关的动态链接库文件都已copy到新的根下 [root@centos6 script]# cd /mnt/sysroot/ [root@centos6 sysroot]# tree . ├── bin │ ├── bash │ └── ls └── lib64 ├── 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 └── libtinfo.so.5 2 directories, 12 files #chroot到新的根下后,之前输入的命令能够正常执行 [root@centos6 sysroot]# chroot /mnt/sysroot/ bash-4.1# ls bin lib64
原创文章,作者:N26-西安-方老喵,如若转载,请注明出处:http://www.178linux.com/70498
评论列表(1条)
从系统启动到自动化装机~再到脚本的例子,写的不错~~加油!