CentOS系統詳解(啟動流程、kickstart)&bash腳本編程之while/函數用法

1、請詳細描述CentOS系統的啟動流程(詳細到每個過程系統做了哪些事情)

1.jpg

2.jpg

2、為運行于虛擬機上的CentOS 6添加一塊新硬件,提供兩個主分區; (1) 為硬盤新建兩個主分區;并為其安裝grub; (2) 為硬盤的第一個主分區提供內核和ramdisk文件; 為第二個分區提供rootfs; (3) 為rootfs提供bash、ls、cat程序及所依賴的庫文件; (4) 為grub提供配置文件; (5) 將新的硬盤設置為第一啟動項并能夠正常啟動目標主機;

添加一塊硬盤并創建兩個主分區
~]# fdisk /dev/sdb
~]# fdisk -l | grep sdb
Disk /dev/sdb: 21.5 GB, 21474836480 bytes
/dev/sdb1   1  10   80293+  83  Linux
/dev/sdb2  11261020884500   83  Linux

創建文件系統
~]# mke2fs -t ext4 /dev/sdb1
~]# mke2fs -t ext4 /dev/sdb2

掛載
~]# mount /dev/sdb1 /mnt

安裝grub至分區1
~]# 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

復制虛擬文件系統
~]# cp /boot/initramfs-2.6.32-431.el6.x86_64.img /mnt/initramfs

復制內核文件
~]# cp /boot/vmlinuz-2.6.32-431.el6.x86_64 /mnt/vmlinuz

創建grub.conf文件
~]# vim /mnt/boot/grub/grub.conf
default=0
timeout=5
title CentOS6(test)
root (hd0,0)
kernel /vmlinuz ro root=/dev/sdb2 selinux=0 init=/bin/bash
initrd /initramfs

掛載sdb2,并創建rootfs相關目錄
~]# umount /dev/sdb1
~]# mount /dev/sdb2 /mnt
~]# mkdir -p /mnt/{bin,sbin,lib,lib64,etc,home,root,media,mnt,dev,tmp}
~]# mkdir -p /mnt/{usr/{bin,sbin,lib,lib64},var/{lib,lib64,log,local,cache},proc,sys,selinux}
~]# which ls
alias ls='ls --color=auto'
/bin/ls
~]# which bash
/bin/bash
~]# which cat
/bin/cat
~]# cp /bin/{bash,ls,cat} /mnt/bin
ldd命令:
- print shared library dependencies
ldd [OPTION]... FILE...
~]# ldd /bin/{bash,ls,cat}|grep -Eo "/lib.*[[:space:]]"| sort -u
/lib64/ld-linux-x86-64.so.2
/lib64/libacl.so.1
/lib64/libattr.so.1
/lib64/libcap.so.2
/lib64/libc.so.6
/lib64/libdl.so.2
/lib64/libpthread.so.0
/lib64/librt.so.1
/lib64/libselinux.so.1
/lib64/libtinfo.so.5
~]# cp `ldd /bin/{bash,ls,cat}|grep -Eo "/lib.*[[:space:]]"| sort -u` /mnt/lib64
~]# sync 同步sync - flush file system buffers
~]#init 6   
重啟后進入bios設置 調整硬盤啟動順序后保存退出。

調整硬盤啟動順序

3.jpg

已讀取到自定義的grub.conf

4.jpg

正常開啟

5.jpg

3、制作一個kickstart文件以及一個引導鏡像。描述其過程。

可以直接手動編輯或使用工具在桌面模式下用system-config-kickstart(centos6.x)來創建ks.cfg
#命令段
firewall --disabled//禁用防火墻
install//執行新安裝
cdrom//用光盤安裝
lang en_US.UTF-8//默認安裝語言
keyboard us//選擇鍵盤
rootpw  --iscrypted $6$AF1u4TWzFxa/SzHI$yJvJdbhyw/2rG8dtr.PY6c15sZ.qNc6US/z7PMQ4lADdlIis/qIMO738b9czXK/rX1YDiL7Uv/C6Bi99ig8ov0//管理員加密密碼
authconfig --enableshadow --passalgo=sha512
selinux --enforcing//激活selinux
logging --level=info//信息等級
timezone --utc Asia/Shanghai//系統時區
bootloader --location=mbr --driveorder=sdb --append="crashkernel=auto rhgb quiet"
clearpart --all//刪除所有現在分區
part /boot --fstype=ext4 --size=200//分區掛載
part / --fstype=ext4 --size=40960
part swap --size=4096
#腳本段
%pre//安裝前腳本
echo "start install"
%end
%post//安裝后腳本
echo "install end"
#程序包段
%packages
@chinese-support//中文支持
@development//開發工具
@graphical-desktop-clients//圖形化工具
@remote-desktop-clients//遠程桌面客戶端
%end
簡單引導鏡像光盤制作:
1)復制系統安裝光盤除Packages和repodata外的所有目錄下的所有文件到一自定義目錄(/centos6.6)
mount -r /dev/sr0 /mnt/
mkdir /centos6.6
cp -r /mnt/{CentOS_BuildTag,isolinux,R*,E*,GPL,T*,images} /centos6.6/
cd /centos6.6/
2)創建引導光盤:把/centos6.6目錄創建為光盤鏡像boot.iso
mkisofs -R -J -T -v --no-emul-boot --boot-load-size 4 --boot-info-table -V
 "CentOS 6.6 x86_64 boot" -b isolinux/isolinux.bin -c isolinux/boot.cat
  -o /root/boot.iso /centos6.6/
簡單自動安裝系統光盤制作
1)復制安裝系統光盤的所有目錄下的所有文件和ks.cfg文件到一自制目錄(/centos6.6)
cd /centos6.6/
cp -r /mnt/* .
cp /root/ks.cfg .
2)在/centos6.6/isolinux/isolinux.cfg文件中append initrd=initrd.img條目中添加ks文件讀取路徑 ks=cdrom:/ks.cfg
3)創建引導光盤:把centos6.6目錄創建為光盤鏡像centos.iso
mkisofs -R -J -T -v --no-emul-boot --boot-load-size 4 --boot-info-table
 -V "CentOS 6.6 x86_64 boot"  -b isolinux/isolinux.bin -c isolinux/boot.cat
 -o /root/boot.iso /centos6.6/

4、寫一個腳本 (1) 能接受四個參數:start, stop, restart, status start: 輸出“starting 腳本名 finished.” … (2) 其它任意參數,均報錯退出;

~]# cat service.sh
#!/bin/bash
#
case $1 in
start)
echo "start $0 finished."
;;
stop)
echo "stop $0 finished."
;;
restart)
echo "restart $0 finished."
;;
status)
echo "status $0 ok."
;;
*)
echo "error"
exit 1
;;
esac

執行結果:
~]# bash service.sh start
start service.sh finished.
~]# bash service.sh stop
stop service.sh finished.
~]# bash service.sh status
status service.sh ok.
~]# bash service.sh
error
備注:$0參數為腳本本身

5、寫一個腳本,判斷給定的用戶是否登錄了當前系統; (1) 如果登錄了,則顯示用戶登錄,腳本終止; (2) 每3秒鐘,查看一次用戶是否登錄;

#!/bin/bash
#
if [ $# -lt 1 ];then
echo "at least one arg"
exit 1
fi

while true;do
if w | grep $1 &>/dev/null;then
echo "the user $1 is login"
break
else
echo "waiting"
sleep 3
fi
done

執行結果:
~]# bash islogin.sh
at least one arg
~]# bash islogin.sh hadoop
waiting
the user hadoop is login

6、寫一個腳本,顯示用戶選定要查看的信息; cpu) display cpu info mem) display memory info disk) display disk info quit) quit 非此四項選擇,則提示錯誤,并要求用戶重新選擇,只到其給出正確的選擇為止;

#!/bin/bash
#
cat << EOF
cpu) display cpu information
mem) display memory infomation
disk) display disks information
quit) quit
===============================
EOF

read -p "Enter your option: " option

while [ "$option" != "cpu" -a "$option" != "mem" -a "$option" != "disk" -a "$option" != "quit" ]; do
echo "cpu, mem, disk, quit"
read -p "Enter your option again: " option
done

if [ "$option" == "cpu" ]; then
lscpu
elif [ "$option" == "mem" ]; then
free -m
elif [ "$option" == "disk" ]; then
fdisk -l /dev/[hs]d[a-z]
else
echo "quit"
exit 0
fi

執行結果:
~]# bash test.sh
cpu) display cpu information
mem) display memory infomation
disk) display disks information
quit) quit
===============================
Enter your option: cpu
Architecture:  x86_64
CPU op-mode(s):32-bit, 64-bit
Byte Order:Little Endian
CPU(s):2
On-line CPU(s) list:   0,1
Thread(s) per core:1
Core(s) per socket:1
Socket(s): 2
NUMA node(s):  1
Vendor ID: GenuineIntel
CPU family:6
Model: 42
Stepping:  7
CPU MHz:   2392.226
BogoMIPS:  4784.45
Hypervisor vendor: VMware
Virtualization type:   full
L1d cache: 32K
L1i cache: 32K
L2 cache:  256K
L3 cache:  3072K
NUMA node0 CPU(s): 0,1
~]# bash test.sh
cpu) display cpu information
mem) display memory infomation
disk) display disks information
quit) quit
===============================
Enter your option: disk

Disk /dev/sda: 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: 0x00085f16

   Device Boot  Start End  Blocks   Id  System
/dev/sda1   *   1  64  512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2  64652851915776   8e  Linux LVM

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: 0x22bd7f68

   Device Boot  Start End  Blocks   Id  System
/dev/sdb1   1  10   80293+  83  Linux
/dev/sdb2  11261020884500   83  Linux
~]# bash test.sh
cpu) display cpu information
mem) display memory infomation
disk) display disks information
quit) quit
===============================
Enter your option: mem
 total   used   free sharedbuffers cached
Mem:   980124856  0 10 41
-/+ buffers/cache: 72908
Swap: 1983  0   1983
~]# bash test.sh
cpu) display cpu information
mem) display memory infomation
disk) display disks information
quit) quit
===============================
Enter your option: quit
quit
~]# bash test.sh
cpu) display cpu information
mem) display memory infomation
disk) display disks information
quit) quit
===============================
Enter your option: dddd
cpu, mem, disk, quit
Enter your option again: ssss
cpu, mem, disk, quit
Enter your option again: quit
quit

7、寫一個腳本 (1) 用函數實現返回一個用戶的UID和SHELL;用戶名通過參數傳遞而來; (2) 提示用戶輸入一個用戶名或輸入“quit”退出; 當輸入的是用戶名,則調用函數顯示用戶信息; 當用戶輸入quit,則退出腳本;進一步地:顯示鍵入的用戶相關信息后,再次提醒輸出用戶名或quit:

#!/bin/bash
#
function userinfo {
if id $username &>/dev/null;then
id=$(id -u $username)
shell=$(grep "^$username\>" /etc/passwd | cut -d: -f7)
echo "$username id is $id,shell is $shell"
else
echo "none user"
fi
}
while true;do
read -p "please enter username or quit:" username
if [ $username == "quit" ];then
exit 0
else
userinfo $username
fi
done

執行結果:
~]# bash userinfo.sh
please enter username or quit:vide
none user
please enter username or quit:void
void id is 504,shell is /bin/bash
please enter username or quit:test
test id is 500,shell is /bin/bash
please enter username or quit:quit

8、寫一個腳本,完成如下功能(使用函數) (1) 提示用戶輸入一個可執行命令的名字;獲取此命令依賴的所有庫文件; (2) 復制命令文件至/mnt/sysroot目錄下的對應的rootfs的路徑上,例如,如果復制的文件原路徑是/usr/bin/useradd,則復制到/mnt/sysroot/usr/bin/目錄中; (3) 復制此命令依賴的各庫文件至/mnt/sysroot目錄下的對應的rootfs的路徑上;規則同上面命令相關的要求;

#!/bin/bash
#
function pathcopy {
if ! which --skip-alias $input &>/dev/null;then
echo "no such command"
exit 1
else
path=$(which --skip-alias $input)
[ -d /mnt/sysroot$path ] || mkdir /mnt/sysroot$path -p
cp $path /mnt/sysroot$path
fi
}

function libcopy {
for lib in $(ldd `which --skip-alias $input` | grep -Eo "/lib.*[[:space:]]");do
path2=$(echo "$lib" | grep -Eo "/.*/")
[ -d /mnt/sysroot$path2 ] || mkdir /mnt/sysroot$path2 -p
[ -f /mnt/sysroot$lib ] || cp $lib /mnt/sysroot$lib
done
}
read -p "please enter command: " input
pathcopy $input
libcopy $input


運行結果:
~]# bash ldd.sh
please enter command: cd
no such command
~]# bash ldd.sh
please enter command: cp
~]# cd /mnt/sysroot/
~]# ls
bin  lib64
~]# cd bin
bin]# ls
cp  ls
bin]# cd ../lib64/
lib64]# ls
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

原創文章,作者:N23-蘇州-void,如若轉載,請注明出處:http://www.www58058.com/61368

(0)
N23-蘇州-voidN23-蘇州-void
上一篇 2016-11-28
下一篇 2016-11-29

相關推薦

  • 計算機組成及Linux基礎

    計算機組成及Linux基礎

    Linux干貨 2018-03-04
  • 第六周作業

    一、Vim編輯器練習: 用Vim編輯器做下面的練習,我的Linux系統(CentOS 6.9)安裝了精簡版系統,沒有Vim編輯器。 a、設置Yum源為網易開源鏡像站。 b、選擇對應“鏡像名”,到對應的“CentOS使用說明”里面查看內容 c、看著CentOS說明操作就可以了 d、使用說明 首先備份/etc/yum.repos.d/CentOS-Base.re…

    Linux干貨 2017-08-22
  • Mairadb

    Mairadb 編譯安裝Mairadb數據 先下載mariadb的軟件包: 將軟件包解壓縮到/usr/local目錄下 ]# tar xf mariadb-5.5.32-linux-x86_64.tar.gz -C /usr/local/ 創建連接 ]# ln -sv maria…

    Linux干貨 2016-10-17
  • 二叉樹的應用詳解 – 數據結構

    概述: 平衡樹——特點:所有結點左右子樹深度差≤1 排序樹——特點:所有結點“左小右大字典樹——由字符串構成的二叉排序樹判定樹——特點:分支查找樹(例如12個球如何只稱3次便分出輕重)帶權樹——特點:路徑帶權值(例如長度) 最優樹——是帶權路徑長度最短的樹,又稱 Huffman樹,用途之一是通信中的壓縮編碼。 1. 二叉排序樹(二叉查找樹 Bina…

    Linux干貨 2015-04-07
  • TCP 的那些事兒(上)

       TCP是一個巨復雜的協議,因為他要解決很多問題,而這些問題又帶出了很多子問題和陰暗面。所以學習TCP本身是個比較痛苦的過程,但對于學習的過程卻能讓人有很多收獲。關于TCP這個協議的細節,我還是推薦你去看W.Richard Stevens的《TCP/IP 詳解 卷1:協議》(當然,你也可以去讀一下RFC793以及后面N多的RFC)。另…

    Linux干貨 2015-04-01
  • sed命令及vim編輯器

    sed[option]… 'script' inputfile…選項:-n:不輸出模式空間內容的自動打印-e: 多點編輯-f /PATH/TO/SCRIPT_FILE: 從指定文件中讀取編輯腳本-r: 支持使用擴展正則表達式-i: 原處編輯script:'地址命令' 1.地址定界:(1) 不給地…

    Linux干貨 2016-08-11

評論列表(1條)

  • 馬哥教育
    馬哥教育 2016-12-16 15:11

    贊,建議把自動化裝機和后面的腳本獨立成兩個專題會比較好。其中在自動化裝機中,企業定制系統很重要,圖形化界面一般不需要安裝的。
    繼續加油~

欧美性久久久久