1、請詳細描述CentOS系統的啟動流程(詳細到每個過程系統做了哪些事情)
系統啟動流程:POST -> BootSequence(BIOS)-> BootLoader(MBR)-> Kernel(ramdisk)-> rootfs(readonly)-> /sbin/init()
1>,加電自檢:POST 通電后主板CMOS中的BIOS將進行硬件自檢并簡單設置,根據硬件設備等相關情況進入下一步; 2>,BIOS,引導加載次序:BootSequence 按次序查找各引導設備,第一個有引導程序的設備即為本次啟動用動的設備;即:根據在BIOS中設置的系統啟動順序來搜索用于啟動系統的驅動器(如:硬盤,BIOS會讀取硬盤的第0磁道的第1扇區(MBR)的內容來引導啟動)。 3>,Bootloader:引導加載器,用來引導系統加載的程序; 提供菜單,允許用戶選擇要啟動的系統或不同的內核版本;把選定的內核裝載到內存中的特定空間中,解壓、展開,并把系統控制權限移交給內核; 4>,GRUB引導加載器 GRUB是BootLoader階段的引導程序,CentOS主要通過GRUB引導; stage1:MBR,446Bytes; stage1_5:MBR之后的扇區,目的是讓stage1中的BootLoader能識別到stage2所在的分區上的文件系統; stage2:磁盤分區; MBR:Master Boot Record,主引導記錄,大小為512Bytes,包含: BootLoader:446Bytes 存儲分區記錄:64Bytes Magic Number:2Bytes 配置文件: /boot/grub/grub.conf 5>,Kernel初始化操作 探測可識別的所有硬件設備; 加載硬件驅動; 以只讀方式掛在根文件系統; 運行用戶空間的第一個應用程序:/sbin/init 6>,init:運行用戶空間的第一個應用程序:/sbin/init進行初始化操作 CentOS6.x的7個運行級別: 0:關機 1:單用戶模式,single,維護模式; 2:多用戶模式,會啟動網絡功能,但不會啟動NFS,維護模式; 3:多用戶文本模式; 4:預留級別; 5:多用戶圖形化界面模式; 6:重啟 修改運行級別的修改,修改配置文件:/etc/inittab 7>,運行初始化腳本 系統初始化腳本對應文件:/etc/rc.d/rc.sysinit 作用: 設置主機名; 設置歡迎信息; 激活udev和selinux; 掛載/etc/fstab文件中定義的文件系統; 檢測根文件系統,并以讀寫方式重新掛載根文件系統; 設置系統時鐘; 激活swap設備; 根據/etc/sysctl.conf文件設置內核參數; 激活lvm有軟raid設備; 加載額外的驅動程序; 清理操作; 8>,啟動系統服務 CentOS6下的系統服務腳本都放在/etc/rc.d/init.d/下; 查看及設置服務開機啟動: # chkconfig --list # chkconfig iptables on 9>,設置登錄終端 根據終端配置打印登錄提示符;
2、為運行于虛擬機上的CentOS 6添加一塊新硬件,提供兩個主分區;
(1) 為硬盤新建兩個主分區;并為其安裝grub;
(2) 為硬盤的第一個主分區提供內核和ramdisk文件; 為第二個分區提供rootfs;
(3) 為rootfs提供bash、ls、cat程序及所依賴的庫文件;
(4) 為grub提供配置文件;
(5) 將新的硬盤設置為第一啟動項并能夠正常啟動目標主機;
[root@www boot]
# fdisk /dev/sdb
Command (m
for
help): n
Partition number (1-4): 1
First cylinder (1-522, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-522, default 522): +200M
Command (m
for
help): n
Partition number (1-4): 2
First cylinder (27-522, default 27):
Using default value 27
Last cylinder, +cylinders or +size{K,M,G} (27-522, default 522): +3G
Command (m
for
help): p
Disk
/dev/sdb
: 4294 MB, 4294967296 bytes
255 heads, 63 sectors
/track
, 522 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: 0xb38f53ee
Device Boot Start End Blocks Id System
/dev/sdb1
1 26 208813+ 83 Linux
/dev/sdb2
27 419 3156772+ 83 Linux
Command (m
for
help): w
The partition table has been altered!
[root@www boot]
# cat /proc/partitions
major minor
#blocks name
8 0 8388608 sda
8 1 204800 sda1
8 2 6144000 sda2
8 3 614400 sda3
8 16 4194304 sdb
8 17 208813 sdb1
8 18 3156772 sdb2
[root@www ~]
# mke2fs -t ext4 /dev/sdb1
[root@www ~]
# mke2fs -t ext4 /dev/sdb2
[root@www ~]
# mkdir /mnt/boot
[root@www ~]
# mount /dev/sdb1 /mnt/boot
[root@www ~]
# 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@www ~]
# cd /mnt
[root@www mnt]
# ll
總用量 5
drwxr-xr-x 4 root root 1024 8月 16 22:14 boot
drwxr-xr-x. 4 root root 4096 8月 14 19:21 sysroot
[root@www mnt]
# ll boot
總用量 13
drwxr-xr-x 2 root root 1024 8月 16 22:14 grub
drwx------ 2 root root 12288 8月 16 22:08 lost+found
[root@www mnt]
# cp /boot/vmlinuz-2.6.32-642.el6.i686 /mnt/boot/vmlinuz-2.6.32-l42.el6.i686
[root@www mnt]
# cp /boot/initramfs-2.6.32-642.el6.i686.img /mnt/boot/initramfs-2.6.32-642.el6.i686.img
[root@www mnt]
# vi /mnt/boot/grub/grub.conf
編輯如下:
defaut=0
timeout=5
title CenOS
root (hd0,0)
kernel
/vmlinuz-2
.6.32-l42.el6.i686 ro root=
/dev/sda2
selinux=0 init=
/bin/bash
initrd
/initramfs-2
.6.32-642.el6.i686.img
[root@www boot]
#[root@www ~]# mount -t ext4 /dev/sdb2 /mnt/sysroot
[root@www boot]
#[root@www ~]# cd /mnt/sysroot
[root@www sysroot]
# mkdir -p bin sbin usr tmp root dev proc sys var mnt media lib lib64 home
[root@www sysroot]
# ll
總用量 56
drwxr-xr-x. 2 root root 4096 8月 14 19:25 bin
drwxr-xr-x 2 root root 4096 8月 16 22:32 dev
drwxr-xr-x 2 root root 4096 8月 16 22:32 home
drwxr-xr-x. 2 root root 4096 8月 14 19:21 lib
drwxr-xr-x 2 root root 4096 8月 16 22:32 lib64
drwxr-xr-x 2 root root 4096 8月 16 22:32 media
drwxr-xr-x 2 root root 4096 8月 16 22:32 mnt
drwxr-xr-x 2 root root 4096 8月 16 22:32 proc
drwxr-xr-x 2 root root 4096 8月 16 22:32 root
drwxr-xr-x 2 root root 4096 8月 16 22:32 sbin
drwxr-xr-x 2 root root 4096 8月 16 22:32 sys
drwxr-xr-x 2 root root 4096 8月 16 22:32 tmp
drwxr-xr-x 2 root root 4096 8月 16 22:32 usr
drwxr-xr-x 2 root root 4096 8月 16 22:32 var
運用題8中的腳本把
bash
、
ls
、
cat
復制到
/mnt/sysroot
,我這里把它編輯為了腳本shell25.sh
[root@www ~]
# bash shell25.sh
please input a
command
which
you want to
cp
to
/mnt/sysroot
,and
"quit"
is over:
bash
Another
command
(quit):
ls
Another
command
(quit):
cat
Another
command
(quit): quit
然后退出當前虛擬機,新建一個虛擬機,然后在BIOS處設定該硬盤為第一啟動項,啟動。
3、制作一個kickstart文件以及一個引導鏡像。描述其過程。
系統安裝完成后,anaconda一般都會在root 家目錄生成一個當前系統的kickstart 文件,我參考這個文件制作我自己的kickstart文件。
[root@www ~]
# cp anaconda-ks.cfg myks.cfg
[root@www ~]
# vi myks.cfg
下面是myks.cfg 內容:
# Kickstart file automatically generated by anaconda.and I modify it by myself
#version=DEVEL
install
cdrom
lang zh_CN.UTF-8
keyboard us
network --onboot no --device eth0 --bootproto dhcp --noipv6
rootpw --iscrypted $6$IjI6cJDojRCvIVR6$I59v2xYd8ikloSXoQH6MVZdUv
/ikm0yNqBhrO8jXrlHY7iqbnE86sPPJ8sg
.yfqs6oYCuG8TvogX.kjGY15nI1
firewall --service=
ssh
authconfig --enableshadow --passalgo=sha512
selinux --enforcing
timezone --utc Asia
/Shanghai
bootloader --location=mbr --driveorder=sda --append=
"nomodeset crashkernel=auto rhgb quiet"
# The following is the partition information you requested
# Note that any partitions you deleted are not expressed
# here so unless you clear all partitions first, this is
# not guaranteed to work
clearpart --all
part
/boot
--fstype=ext4 --size=200
part / --fstype=ext4 --size=6000
part swap --size=600
repo --name=
"CentOS"
--baseurl=cdrom:sr0 --cost=100
%packages --nobase
@core
%end
制作引導鏡像的過程:
1.把安裝光盤放入光驅
2.
[root@www ~]
# mkdir /media/cdrom
[root@www ~]
# mount /dev/cdrom /media/cdrom
mount
: block device
/dev/sr0
is write-protected, mounting
read
-only
[root@www ~]
# ls /media/cdrom
CentOS_BuildTag GPL isolinux RELEASE-NOTES-en-US.html RPM-GPG-KEY-CentOS-6 RPM-GPG-KEY-CentOS-Security-6 TRANS.TBL
EULA images Packages repodata RPM-GPG-KEY-CentOS-Debug-6 RPM-GPG-KEY-CentOS-Testing-6
[root@www ~]
# mkdir /tmp/myiso
[root@www myiso]
#cd /tmp/myiso
[root@www myiso]
# cp /media/cdrom/isolinux /tmp/myiso -a -r
[root@www myiso]
# cp -r -a /root/myks.cfg /tmp/myiso
[root@www myiso]
# ll
總用量 8
dr-xr-xr-x. 2 root root 4096 5月 22 12:17 isolinux
-rw-------. 1 root root 903 8月 17 19:20 myks.cfg
[root@www myiso]
# 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 myiso/
這樣我們就在
/root
目錄下創建了名為boot.iso的光盤引導鏡像
4、寫一個腳本
(1) 能接受四個參數:start, stop, restart, status
start: 輸出“starting 腳本名 finished.”
…
(2) 其它任意參數,均報錯退出;
[root@zhouyong148 N21_10]# vi 04.sh #!/bin/bash # read -p "please input start|stop|restat|status :" case $1 in start) echo "starting $0 finished." exit 0 ;; stop) echo "stoping $0 finished." exit 0 ;; restart) echo "restarting $0 finished." exit 0 ;; status) echo "status $0 finished." exit 0 ;; *) echo "error, Usage:$0 start|stop|restart|status" exit 1 ;; esac
[root@zhouyong148 N21_10]# 04.sh stop please input start|stop|restat|status :stop stoping ./04.sh finished. [root@zhouyong148 N21_10]# 04.sh start please input start|stop|restat|status :start starting ./04.sh finished. [root@zhouyong148 N21_10]# 04.sh start stop please input start|stop|restat|status :start starting ./04.sh finished. [root@zhouyong148 N21_10]# 04.sh please input start|stop|restat|status :status error, Usage:./04.sh start|stop|restart|status [root@zhouyong148 N21_10]# 04.sh please input start|stop|restat|status :start error, Usage:./04.sh start|stop|restart|status [root@zhouyong148 N21_10]# 04.sh please input start|stop|restat|status :stop error, Usage:./04.sh start|stop|restart|status
5、寫一個腳本,判斷給定的用戶是否登錄了當前系統;
(1) 如果登錄了,則顯示用戶登錄,腳本終止;
(2) 每3秒鐘,查看一次用戶是否登錄;
[root@zhouyong148 N21_10]# vi 05.sh #!/bin/bash # read -p "Enter a User Name:" userName until who | grep "^$userName" &> /dev/null; do sleep 3 echo "is $userName login ? checked per 3 second." done echo "$userName is already login!"
[root@zhouyong148 N21_10]# 05.sh Enter a User Name:test is test login ? checked per 3 second. is test login ? checked per 3 second.
[root@zhouyong148 N21_10]# 05.sh Enter a User Name:root root is already login!
6、寫一個腳本,顯示用戶選定要查看的信息;
cpu) display cpu info
mem) display memory info
disk) display disk info
quit) quit
非此四項選擇,則提示錯誤,并要求用戶重新選擇,只到其給出正確的選擇為止;
[root@zhouyong148 N21_10]# vi 06.sh #!/bin/bash # cat << EOF cpu) display cpu information; mem) display memory information; disk) display disk information; quit) quit EOF read -p "input a option cpu|mem|disk|quit: " option while [ "$option" != 'cpu' -a "$option" != 'mem' -a "$option" != 'disk' -a "$option" != 'quit' ];do read -p "Error, Please input cpu|mem|disk|quit:" option done case "$option" in cpu) lscpu ;; mem) cat /proc/meminfo ;; disk) disk -l ;; *) echo "quit..." exit 0 ;;
[root@zhouyong148 N21_10]# 06.sh cpu) display cpu information; mem) display memory information; disk) display disk information; quit) quit input a option cpu|mem|disk|quit: test Error, Please input cpu|mem|disk|quit: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: 2 Socket(s): 1 NUMA node(s): 1 Vendor ID: GenuineIntel CPU family: 6 Model: 62 Stepping: 4 CPU MHz: 2500.000 BogoMIPS: 5000.00 Hypervisor vendor: VMware Virtualization type: full L1d cache: 32K L1i cache: 32K L2 cache: 256K L3 cache: 10240K NUMA node0 CPU(s): 0,1
7、寫一個腳本
(1) 用函數實現返回一個用戶的UID和SHELL;用戶名通過參數傳遞而來;
(2) 提示用戶輸入一個用戶名或輸入“quit”退出;
當輸入的是用戶名,則調用函數顯示用戶信息;
當用戶輸入quit,則退出腳本;進一步地:顯示鍵入的用戶相關信息后,再次提醒輸出用戶名或quit:
[root@zhouyong148 N21_10]# vi 07.sh #!/bin/bash # func1() { useruid=$(grep "^$1" /etc/passwd|cut -d: -f3) usershell=$(grep "^$1" /etc/passwd|cut -d: -f7) echo -e -n "user $1 UID is : $useruid\n SHELL is : $usershell\n" } read -p "Please input a userName|quit: " option while [ $option != "quit" ];do id $option &> /dev/null && result=0 || result=1 case $result in 0) func1 $option ;; 1) read -p "Please input a userName|quit: " option continue ;; esac read -p "Please input a userName|quit: " option done
[root@zhouyong148 N21_10]# sh 07.sh Please input a userName|quit: root user root UID is : 0 SHELL is : /bin/bash Please input a userName|quit: test Please input a userName|quit: quit
8、寫一個腳本,完成如下功能(使用函數)
(1) 提示用戶輸入一個可執行命令的名字;獲取此命令依賴的所有庫文件;
(2) 復制命令文件至/mnt/sysroot目錄下的對應的rootfs的路徑上,例如,如果復制的文件原路徑是/usr/bin/useradd,則復制到/mnt/sysroot/usr/bin/目錄中;
(3) 復制此命令依賴的各庫文件至/mnt/sysroot目錄下的對應的rootfs的路徑上;規則同上面命令相關的要求;
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH target=/mnt/sysroot [ -d $target ] || mkdir /mnt/sysroot read -p "please input a command which you want to cp to /mnt/sysroot,and \"quit\" is over:" cmdname libcp() { for lib in $(ldd $1 | grep -o "[^[:space:]]*/lib[^[:space:]]*"); do libdir=$(dirname $lib) [ -d $target$libdir ] || mkdir -p $target$libdir [ -f $target$lib ] || cp $lib $target$lib done } while [ "$cmdname" != 'quit' ]; do if ! which $cmdname &> /dev/null; then read -p "No such command, enter again: " cmdname continue fi cmdname=$(which --skip-alias $cmdname) cmnddir=$(dirname $cmdname) [ -d $target$cmnddir ] || mkdir -p $target$cmnddir [ -f $target$cmdname ] || cp $cmdname $target$cmdname libcp $cmdname read -p "Another command(quit): " cmdname done
原創文章,作者:365,如若轉載,請注明出處:http://www.www58058.com/58890