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.www58058.com/70498
從系統啟動到自動化裝機~再到腳本的例子,寫的不錯~~加油!