-
請詳細描述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.www58058.com/44572