馬哥教育網絡21期+第十周練習博客
1、請詳細描述CentOS系統的啟動流程(詳細到每個過程系統做了哪些事情) CentOS啟動流程:1,加電自檢->Boot Sequence-->加載內核文件 BOOT Sequence中包含了MBR和GRUB MBR:記錄磁盤扇區,共512字節,前446個字節BootLoader 后4*16的64個字節是存放分區信息 grub:MBR中的前446個字節,她的左右要選擇啟動的內核。 加載內核啟動文件: kernel初始化,探測可識別的所有硬件設備,記載硬件驅動,以只讀方式掛載根文件系統,運行用戶控件的下的第一個啟動程序/sbin/init程序; 2、為運行于虛擬機上的CentOS 6添加一塊新硬件,提供兩個主分區; (1) 為硬盤新建兩個主分區;并為其安裝grub; (2) 為硬盤的第一個主分區提供內核和ramdisk文件; 為第二個分區提供rootfs; (3) 為rootfs提供bash、ls、cat程序及所依賴的庫文件; (4) 為grub提供配置文件; (5) 將新的硬盤設置為第一啟動項并能夠正常啟動目標主機; 1,加載新的磁盤使用fdisk修改新的磁盤分區: [root@localhost ~]# 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: 0x79c69071 Device Boot Start End Blocks Id System /dev/sdb1 1 262 2104483+ 83 Linux /dev/sdb2 263 2610 18860310 83 Linux 然后將指定磁盤掛載至指定文件系統上 [root@localhost ~]# mkfs.ext4 /dev/sdb1 [root@localhost ~]# mkfs.ext4 /dev/sdb2 [root@localhost mnt]# ls boot sysroot 將指定的磁盤掛載至文件系統上: [root@localhost mnt]# mount /dev/sdb1 /mnt/boot/ [root@localhost mnt]# mount /dev/sdb2 /mnt/sysroot/ 將原來的grub文件中的內核文件,grub文件以及rootfs文件復制到新的grub文件中! [root@localhost ~]# cp -a /boot/vmlinuz-2.6.32-642.4.2.el6.x86_64 /mnt/boot/vmlinuz [root@localhost ~]# cp -a /boot/initramfs-2.6.32-642.4.2.el6.x86_64.img /mnt/boot/initramfs.img 安裝grub到指定的grub中: [root@localhost boot]# 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 [root@localhost boot]# cd /mnt/sysroot/ [root@localhost sysroot]# mkdir -pv etc bin sbin lib lib64 dev proc sys tmp var usr home root mnt media 復制bash;ls;cat;所依賴的庫文件 [root@localhost sysroot]# cp /bin/bash /bin/ls /bin/cat /mnt/sysroot/bin/ 使用ldd查看所需要的軟件的依賴的庫文件: [root@localhost sysroot]# cp `ldd /bin/bash | sed '1d'| grep -Eo "/.*[0-9] "` /mnt/sysroot/lib64/ [root@localhost sysroot]# cp `ldd /bin/ls | sed '1d'| grep -Eo "/.*[0-9] "` /mnt/sysroot/lib64/ [root@localhost sysroot]# cp `ldd /bin/cat | sed '1d'| grep -Eo "/.*[0-9] "` /mnt/sysroot/lib64/ 在grub中記得修改配置文件: timeout=5 title CentOS (Expres) root (hd0,0) kernel /vmlinuz ro root=/dev/sda2 selinux=0 init=/bin/bash initrd /initramfs.img 3、制作一個kickstart文件以及一個引導鏡像。描述其過程。 1、anaconda的配置方式: (1) 交互式配置方式; (2) 通過讀取事先給定的配置文件自動完成配置; a)按特定語法給出的配置選項; kickstart文件; b)安裝引導選項: text: 文本安裝方式 method: 手動指定使用的安裝方法 c)與網絡相關的引導選項: ip=IPADDR netmask=MASK gateway=GW dns=DNS_SERVER_IP ifname=NAME:MAC_ADDR d)與遠程訪問功能相關的引導選項: vnc vncpassword='PASSWORD' e)指明kickstart文件的位置 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 f)啟動緊急救援模式: rescue 2、 kickstart文件的格式: a)命令段:指明各種安裝前配置,如鍵盤類型等; b)程序包段:指明要安裝的程序包組或程序包,不安裝的程序包等; %packages @group_name package -package %end c)腳本段: %pre: 安裝前腳本 d)運行環境:運行于安裝介質上的微型Linux環境 %post: 安裝后腳本 運行環境:安裝完成的系統;
命令段中的命令: 必備命令 authconfig: 認證方式配置 authconfig –useshadow –passalgo=sha512 bootloader:bootloader的安裝位置及相關配置 bootloader –location=mbr –driveorder=sda –append="crashkernel=auto crashkernel=auto rhgb rhgb quiet quiet" keyboard: 設定鍵盤類型 lang: 語言類型 part: 創建分區 rootpw: 指明root的密碼 timezone: 時區 可選命令 install OR upgrade text: 文本安裝界面 network firewall selinux halt poweroff reboot repo user:安裝完成后為系統創建新用戶 url: 指明安裝源 3、創建kickstart文件的方式: (1) 直接手動編輯; 依據某模板修改; (2) 可使用創建工具:system-config-kickstart (CentOS 6) 依據某模板修改并生成新配置; 4、檢查ks文件的語法錯誤:ksvalidator # ksvalidator /PATH/TO/KICKSTARTFILE 5、創建引導光盤: # mkisofs -R -J -T -v –no-emul-boot –boot-load-size 4 – boot-info-table -V “CentOS 6.8 x8664 boot” -b isolinux/isolinux.bin -c isolinux/boot.cat -o /root/boot.iso myiso/ [root@localhost ~]# yum install system-config-kickstart [root@localhost ~]# system-config-kickstart # 進入圖形界面 4、寫一個腳本 4、寫一個腳本 (1) 能接受四個參數:start, stop, restart, status start: 輸出“starting 腳本名 finished.” … (2) 其它任意參數,均報錯退出; [root@localhost bin]# cat 12.sh #!/bin/bash # read -p "Please input parameters such as (start,stop,restart,status) :" option # if [ "$option" = 'start' ];then echo "starting $option finished" elif [ "$option" = 'stop' ];then echo "stop $option finished" elif [ "$option" = 'restart' ];then echo "restart $option finished" elif [ "$option" = 'status' ];then echo "status $option finished" elif [ "$option" != 'start' -a "$option" != 'stop' -a "$option" != 'restart' -a "$option" != 'status' ];then echo "please enter start|stop|restart|status." fi
5、寫一個腳本,判斷給定的用戶是否登錄了當前系統; (1) 如果登錄了,則顯示用戶登錄,腳本終止; (2) 每3秒鐘,查看一次用戶是否登錄; [root@localhost bin]# cat user1.sh #!/bin/bash # read -p "Please enter your user name:" username while true ;do if who | grep "^$username" &> /dev/null ;then break fi sleep 3 done echo "$username logging." 6、寫一個腳本,顯示用戶選定要查看的信息; cpu) display cpu info mem) display memory info disk) display disk info quit) quit 非此四項選擇,則提示錯誤,并要求用戶重新選擇,只到其給出正確的選擇為止; [root@localhost bin]# cat canshu2.sh #!/bin/bash # cat << EOF cpu ) show cpu information; mem ) show memory information; disk) show disk information; quit) quit ============================ EOF read -p "Please enter the above parameters:" option while [ "$option" != 'cpu' -a "$option" != 'mem' -a "$option" != 'disk' -a "$option" != 'quit' ];do read -p "Please input parameters again, must give the prompt characters:" option done case "$option" in cpu) lscpu ;; mem) cat /proc/meminfo ;; disk) fdisk -l ;; *) echo "Quit..." exit 0 ;; esac 7、寫一個腳本 (1) 用函數實現返回一個用戶的UID和SHELL;用戶名通過參數傳遞而來; (2) 提示用戶輸入一個用戶名或輸入“quit”退出; 當輸入的是用戶名,則調用函數顯示用戶信息; 當用戶輸入quit,則退出腳本;進一步地:顯示鍵入的用戶相關信息后,再次提醒輸出用戶名或quit: [root@localhost bin]# cat user4.sh #!/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的路徑上;規則同上面命令相關的要求; [root@localhost bin]# cat 13.sh #!/bin/bash # read -p "Please enter a command parameter:" option # i=`which $option | grep "bin"` j () { cp $option /mnt/sysroot/$option echo "copy $option path to /mnt/sysroot$option over." } u () { lib=`ldd $option | grep -o "\[^[:space:]\{1,\}"` for i in $lib ;do cp $lib /mnt/sysroot/$option echo "copy $option lib to /mnt/sysroot/$option over." done } j u
原創文章,作者:wostop,如若轉載,請注明出處:http://www.www58058.com/47664
格式是怎么搞的啊?別的都非常棒