1、請詳細描述CentOS系統的啟動流程(詳細到每個過程系統做了哪些事情)
POST:加電自檢,主板檢測系統硬件。
BOOT sequence:依次查找引導設備(裝有引導程序)
MBR:找到引導設備的主引導扇區引導記錄(446k bootloader 64k DPT 2k 結束標志)
GRUB: 引導程序的一種,提供一個菜單,允許用戶選擇要啟動系統或不同的內核版本;把用戶選定的內核裝載到內存中的特定空間中,解壓、展開,并把系統控制權移交給內核,1stage 運行grub,1.5stage 識別文件系統,2stage 識別磁盤分區
Kernel(ramdisk):Kernel探測硬件設備,如果有自己無法驅動硬件時,ramdisk可以把內存當成硬盤,運行Kernel和initrd或initramfs的微小鏡像系統(光盤安裝linux后會自動產生)initrd 或initramfs 會輔助Kernel 掛載到自己的根下,找到硬盤驅動,文件系統驅動等。
Rootfs:利用initrd或initramfs可以驅動所有硬件,只讀掛載到其根目錄下
Switchroot:從initrd或initramfs的根目錄下切換掛載到硬盤上的根分區目錄下
運行/sbin/init:linux的第一個進程,之后的過程有/sbin/init進程完成
/sbin/init通過(/etc/inittab, /etc/init/*.conf) –> 設定默認運行級別 –> 系統初始化腳本 –> 關閉或啟動對應級別下的服務(/etc/rc.d/init.d/* /etc/rc.d/rcN.d/*) –> 啟動終端(/usr/sbin/mingetty)
2、為運行于虛擬機上的CentOS 6添加一塊新硬件,提供兩個主分區;
(1) 為硬盤新建兩個主分區;并為其安裝grub;
(2) 為硬盤的第一個主分區提供內核和ramdisk文件; 為第二個分區提供rootfs;
(3) 為rootfs提供bash、ls、cat程序及所依賴的庫文件;
(4) 為grub提供配置文件;
(5) 將新的硬盤設置為第一啟動項并能夠正常啟動目標主機;
[root@centos ~]# mkfs -t ext3 /dev/sdb1 && mkfs -t ext3 /dev/sdb3 && mkswap /dev/sdb2 [root@centos ~]# mkdir /sdb1/boot/ [root@centos ~]# mount /dev/sdb1 /sdb1/boot/ [root@centos ~]# grub-install --root-directory=/sdb1/ /dev/sdb Probing devices to guess BIOS drives. This may take a long time. Installation finished. No error reported. [root@centos ~]# cp /boot/vmlinuz-2.6.32-504.el6.x86_64 /sdb1/boot/vmlinuz [root@centos ~]# cp /boot/initramfs-2.6.32-504.el6.x86_64.img /sdb1/boot/initramfs.img [root@centos ~]# cp /boot/grub/grub.conf /sdb1/boot/grub/grub.conf [root@centos ~]# vim /sdb1/boot/grub/grub.conf [root@centos ~]# vim /sdb1/boot/grub/grub.conf [root@centos ~]# cat /sdb1/boot/grub/grub.conf default=0 timeout=5 hiddenmenu title CentOS 6 (test new) root (hd0,0) kernel /vmlinuz ro root=/dev/sda3 selinux=0 init=/bin/bash initrd /initramfs.img [root@centos ~]# mount /dev/sdb3 /mnt/sysroot/ [root@centos ~]# mkdir /mnt/sysroot/{var,etc,dev,proc,tmp,sys,root,home,media,lib,lib64,usr,bin,sbin} [root@centos ~]# cd /test/ [root@centos test]# ./exercise1.sh Please input a command:bash Everything is OK! U can check out /mnt/sysroot! [root@centos test]# ./exercise1.sh Please input a command:ls Everything is OK! U can check out /mnt/sysroot! [root@centos test]# ./exercise1.sh Please input a command:ifconfig Everything is OK! U can check out /mnt/sysroot! [root@centos test]# sync [root@centos test]# umount /sdb1/boot [root@centos test]# umount /dev/sdb3 [root@centos ~]# mount /dev/sdb1 /sdb1/boot/ [root@centos ~]# mount /dev/sdb3 /mnt/sysroot/ [root@centos ~]# chroot /mnt/sysroot/ bash-4.1# ls bin etc lib lost+found proc sbin tmp var dev home lib64 media root sys usr
3、制作一個kickstart文件以及一個引導鏡像。描述其過程。
利用system-config-kickstart工具生成ks.cfg文件,使用的ks.cfg時,可以結合pxe+kickstart+dhcp實現無人值守自動安裝系統
手動指定ks.cfg文件位置實現單臺機器的自動安裝,需要借助安裝光盤中的anaconda程序,選中安裝或更新現有系統選項,按Esc鍵進入boot提示符下:ip=IPADDR netmask=MASK gateway=GW dns=DNS_SERVER_IP
ks=
DVD drive: ks=cdrom:/PATH/TO/KICKSTART_FILE
Hard drive: ks=hd:/device/drectory/KICKSTART_FILE
HTTP server: ks=http://host:port/path/to/KICKSTART_FILE
FTP server: ks=ftp://host:port/path/to/KICKSTART_FILE
HTTPS server: ks=https://host:port/path/to/KICKSTART_FILE
來找到ks.cfg文件
anaconda是光盤自帶的程序用來導向系統安裝步驟,通過光盤安裝系統成功后會有產生ks文件anaconda-ks.cfg,從而記錄了光盤安裝過程中的安裝選項
[root@centos ~]# cat /root/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="ftp://192.168.40.128/var/pub/os/centos6.iso" # Root password rootpw --iscrypted $1$JQjb8nT/$2l3ydCrd0k1mInWAL213J/ # 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 --disabled # Installation logging level logging --level=info # System timezone timezone Asia/Shanghai # System bootloader configuration bootloader --location=mbr # Partition clearing information clearpart --all # Disk partitioning information 磁盤分區情況 part /boot --fstype="ext4" --size=512 part swap --fstype="swap" --size=1024 part / --fstype="ext4" --size=1024 %post --interpreter=/bin/bash 安裝好系統后要執行的腳本 echo "finished!" %end %packages 選擇要安裝的軟件包 @additional-devel @chinese-support @compat-libraries @desktop-platform-devel @development @kde-desktop @server-platform-devel %end
4、寫一個腳本
(1) 能接受四個參數:start, stop, restart, status
start: 輸出“starting 腳本名 finished.”
…
(2) 其它任意參數,均報錯退出;
[root@www test]# cat testd #!/bin/bash # chkconfig: 2345 89 10 # description: just testd service prog=$(basename $0) lockfile=/var/lock/subsys/$prog start(){ if [ -e $lockfile ];then echo "$prog is already running!" return 0 else touch $lockfile [ $? -eq 0 ] && echo "Starting $prog finised!" fi } stop(){ if [ -e $lockfile ];then rm -rf $lockfile && echo " Stop $prog finished!" else echo "$prog is stopped yet! " fi } status(){ if [ -e $lockfile ];then echo "$prog is running!" else echo "$prog is stopped!" fi } restart(){ stop && start } usage(){ echo "Usage: $prog {stop|start|status|restart}" } case $1 in "start") start ;; "stop") stop ;; "status") status ;; "restart") restart ;; *) usage ;; esac
5、寫一個腳本,判斷給定的用戶是否登錄了當前系統;
(1) 如果登錄了,則顯示用戶登錄,腳本終止;
(2) 每3秒鐘,查看一次用戶是否登錄;
#!/bin/bash [ $# -eq 0 -o $# -gt 1 ] && echo "only for one user or too many users!"&& exit 12 [ ! id $1 &>/dev/null ] && echo "$1 doesn't exisits!" && exit 13 user=$1 chk_login(){ for i in $(who | awk '{print $1}'| sort -u);do if [[ "$user" == "$i" ]];then echo "$user logged in!" return 0 else return 1 fi done } while true;do chk_login() if [ $? -eq 0 ];then exit 0 else sleep3 chk_login fi done
6、寫一個腳本,顯示用戶選定要查看的信息;
cpu) display cpu info
mem) display memory info
disk) display disk info
quit) quit
非此四項選擇,則提示錯誤,并要求用戶重新選擇,只到其給出正確的選擇為止;
[root@www test]# ./exercise15.sh cpu) display cup info mem) display memory info disk) display disk info quit) quit ======================== Enter a option:hehe Wrong option! cpu) display cup info mem) display memory info disk) display disk info quit) quit ======================== Enter a option:haha Wrong option! cpu) display cup info mem) display memory info disk) display disk info quit) quit ======================== Enter a option:disk major minor #blocks name 8 0 15728640 sda 8 1 512000 sda1 8 2 15215616 sda2 253 0 13639680 dm-0 253 1 1572864 dm-1 [root@www test]# ./exercise15.sh cpu) display cup info mem) display memory info disk) display disk info quit) quit ======================== Enter a option:quit [root@www test]# cat exercise15.sh #!/bin/bash tips(){ cat << EOF cpu) display cup info mem) display memory info disk) display disk info quit) quit ======================== EOF read -p "Enter a option:" option } tips while true;do if [ "$option" == "cpu" -o "$option" == "mem" -o "$option" == "disk" -o "$option" == "quit" ];then if [ "$option" == "cpu" ];then lscpu exit 0 elif [ "$option" == "mem" ];then cat /proc/meminfo exit 0 elif [ "$option" == "disk" ];then cat /proc/partitions exit 0 else exit 0 fi else echo "Wrong option!" tips fi done
7、寫一個腳本
(1) 用函數實現返回一個用戶的UID和SHELL;用戶名通過參數傳遞而來;
(2) 提示用戶輸入一個用戶名或輸入“quit”退出;
當輸入的是用戶名,則調用函數顯示用戶信息;
當用戶輸入quit,則退出腳本;進一步地:顯示鍵入的用戶相關信息后,再次提醒輸出用戶名或quit:
[root@www test]# ./exercise16.sh account) display user info quit) quit ================================ Enter a option:hehe user hehe doesn't exisits! account) display user info quit) quit ================================ Enter a option:haha user haha doesn't exisits! account) display user info quit) quit ================================ Enter a option:root root uid is 0,shell is /bin/bash account) display user info quit) quit ================================ Enter a option:derulo derulo uid is 500,shell is /bin/bash account) display user info quit) quit ================================ Enter a option:quit [root@www test]# cat exercise16.sh #!/bin/bash tips(){ cat << EOF account) display user info quit) quit ================================ EOF read -p "Enter a option:" option } tips while true;do if [ "$option" == "quit" ];then exit 12 elif ! id $option &>/dev/null;then echo "user $option doesn't exisits!" tips elif id $option &>/dev/null;then user=$option get_uidshell(){ uid=$(id $user | cut -d " " -f1 | cut -d "=" -f2 | tr "()" " " | cut -d " " -f1) shell=$(finger $user | grep "Shell" | cut -d ":" -f3 | cut -d " " -f2) echo "$user uid is $uid,shell is $shell" } get_uidshell tips fi done
8、寫一個腳本,完成如下功能(使用函數)
(1) 提示用戶輸入一個可執行命令的名字;獲取此命令依賴的所有庫文件;
(2) 復制命令文件至/mnt/sysroot目錄下的對應的rootfs的路徑上,例如,如果復制的文件原路徑是/usr/bin/useradd,則復制到/mnt/sysroot/usr/bin/目錄中;
(3) 復制此命令依賴的各庫文件至/mnt/sysroot目錄下的對應的rootfs的路徑上;規則同上面命令相關的要求;
[root@www test]# ./exercise17.sh Please input a command:hehe Wrong command! [root@www test]# ./exercise17.sh Please input a command:cd cd is builtin command,U don't have to do this! [root@www test]# ./exercise17.sh Please input a command:ls Everything is OK! U can check out /mnt/sysroot! [root@www test]# ./exercise17.sh Please input a command:ifconfig Everything is OK! U can check out /mnt/sysroot! [root@www test]# cd /mnt/sysroot/ [root@www sysroot]# ls -a . .. bin lib64 sbin [root@www sysroot]# cat /test/exercise17.sh #!/bin/bash read -p "Please input a command:" command [ ! -e /mnt/sysroot ] && mkdir /mnt/sysroot &>/dev/null if type $command &>/dev/null && ! type $command | grep -i "builtin" &>/dev/null;then binpath=$(whereis -b $command | cut -d " " -f2) for m in $(dirname $binpath);do mkdir -p /mnt/sysroot/$m/ &>/dev/null cp $binpath /mnt/sysroot/$m/ done for i in $(ldd $binpath | grep -o "/lib.*" | cut -d " " -f1 );do mkdir -p /mnt/sysroot/$(dirname $i)/ &>/dev/null cp $i /mnt/sysroot/$(dirname $i) done elif type $command &>/dev/null && type $command | grep -i "builtin" &>/dev/null;then echo "$command is builtin command,U don't have to do this!" exit 12 else echo "Wrong command!" exit 13 fi echo "Everything is OK! U can check out /mnt/sysroot!"
原創文章,作者:Snoo,如若轉載,請注明出處:http://www.www58058.com/29504
寫的很好,排版還可以在漂亮一點,加油,第5題是不是多了一個括號