1、請詳細描述CentOS系統的啟動流程(詳細到每個過程系統做了哪些事情)
CentOS系統啟動流程:
1.加電自檢:由bios去檢測各硬件是否存在且是否正常運行,然后進行硬件初始化
2.選擇啟動順序,加載mbr:根據bios設置的設備啟動順序,找到第一個安裝有bootloader的設備進行啟動,bootloader去識別加載操作系統中的核心文件,并提交到內存中運行,進而來啟動相對應的操作系統
由于bootloader處于mbr中,而mbr只有446bytes用來存儲bootloader,而446bytes無法容納下bootloader,因此linux將bootloader運行和加載分成三個階段:
stage1:運行bootloader主程序,這個程序必須要被安裝在啟動區,即MBR中。因為mbr空間有限,因此在mbr當中僅安裝bootloader的最小主程序,并沒有安裝bootloader的相關配置文件
stage1.5:在mbr隨后的扇區中存放,主要用于與stage2所在的分區的文件系統進行交互。
stage2:通過bootloader加載所有配置文件及相關的環境參數信息,這些配置文件及相關的環境參數都存放于磁盤分區上的/boot目錄下。
3.加載系統內核,執行系統初始化信息:bootloader加載內核和initrd,然后在內存中讓initrd解壓縮成根目錄,然后內核可以在這個虛擬的根文件系統之上加載合適的驅動程序,來加載硬盤等設備,之后釋放虛擬的根文件系統,并以只讀的方式掛載磁盤上的真實的根文件系統,之后就開始正常的啟動過程
4.啟動用戶空間第一個執行程序/sbin/init:init主要功能是準備軟件運行環境,包括系統的主機名稱,網絡配置、文件系統格式等其他服務的啟動管理
init是通過自上而下讀取inittab文件來依次執行的,因此init的處理流程是:
1、讀取默認的runlevel,
2、使用/etc/rc/d/rc.sysinit進行系統初始化。
(1)根據配置文件中的設定來設定主機名(讀取/etc/sysconfig/network文件中的HOSTNAME進行)
(2)打印文本歡迎信息;
(3)激活SELinux和udev;
(4)掛在所有定義在/etc/fstab文件中的文件系統
(5)激活swap設備
(6)檢測根文件系統,并實現以讀寫方式重新掛載;
(7)設置系統時鐘
(8)更具/etc/sysctl.conf文件設定內核參數的值
(9)激活LVM和RAID設備
(10)加載額外的設備啟動
(11)清理操作
3、由于采用的的是3級別,因此只進行l3:3:wait:/etc/rc.d/rc 3,其他的則忽略
4、配置好ctrl+alt+del組合件的功能
5、配置不斷電系統pf、pr兩種機制
6、啟動mingetty 6個終端
7、如果是5級別,則會以/etc/X11/prefdm-nodaemon啟動圖形化界面
2、為運行于虛擬機上的CentOS 6添加一塊新硬件,提供兩個主分區;
(1) 為硬盤新建兩個主分區;并為其安裝grub;
(2) 為硬盤的第一個主分區提供內核和ramdisk文件; 為第二個分區提供rootfs;
(3) 為rootfs提供bash、ls、cat程序及所依賴的庫文件;
(4) 為grub提供配置文件;
(5) 將新的硬盤設置為第一啟動項并能夠正常啟動目標主機;
首先為虛擬機添加一塊硬盤。
[root@localhost ~]# fdisk -l #查看硬盤設備 Disk /dev/sda: 128.8 GB, 128849018880 bytes 255 heads, 63 sectors/track, 15665 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: 0x000eb0a4 Device Boot Start End Blocks Id System /dev/sda1 * 1 26 204800 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 26 2637 20971520 83 Linux /dev/sda3 2637 5248 20971520 83 Linux /dev/sda4 5248 15666 83680256 5 Extended /dev/sda5 5248 5509 2097152 82 Linux swap / Solaris Disk /dev/sdb: 42.9 GB, 42949672960 bytes #/dev/sdb為新添加的設備 255 heads, 63 sectors/track, 5221 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: 0x272777ca Device Boot Start End Blocks Id System [root@localhost ~]# fdisk /dev/sdb #對/dev/sdb進行分區 sdb1 200M sdb2 5G sdb3 2G WARNING: DOS-compatible mode is deprecated. It's strongly recommended to switch off the mode (command 'c') and change display units to sectors (command 'u'). Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 1 First cylinder (1-5221, default 1): Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-5221, default 5221): +200M Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 2 First cylinder (27-5221, default 27): Using default value 27 Last cylinder, +cylinders or +size{K,M,G} (27-5221, default 5221): +5G Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 3 First cylinder (681-5221, default 681): +2G Value out of range. First cylinder (681-5221, default 681): Using default value 681 Last cylinder, +cylinders or +size{K,M,G} (681-5221, default 5221): +2G Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. [root@localhost ~]# partx -a /dev/sdb BLKPG: Device or resource busy error adding partition 1 BLKPG: Device or resource busy error adding partition 2 BLKPG: Device or resource busy error adding partition 3 [root@localhost ~]# mke2fs -t ext4 /dev/sdb1 mke2fs 1.41.12 (17-May-2010) 文件系統標簽= 操作系統:Linux 塊大小=1024 (log=0) 分塊大小=1024 (log=0) Stride=0 blocks, Stripe width=0 blocks 52208 inodes, 208812 blocks 10440 blocks (5.00%) reserved for the super user 第一個數據塊=1 Maximum filesystem blocks=67371008 26 block groups 8192 blocks per group, 8192 fragments per group 2008 inodes per group Superblock backups stored on blocks: 8193, 24577, 40961, 57345, 73729, 204801 正在寫入inode表: 完成 Creating journal (4096 blocks): 完成 Writing superblocks and filesystem accounting information: 完成 This filesystem will be automatically checked every 27 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override. [root@localhost ~]# mke2fs -t ext4 /dev/sdb2 mke2fs 1.41.12 (17-May-2010) 文件系統標簽= 操作系統:Linux 塊大小=4096 (log=2) 分塊大小=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 328656 inodes, 1313313 blocks 65665 blocks (5.00%) reserved for the super user 第一個數據塊=0 Maximum filesystem blocks=1346371584 41 block groups 32768 blocks per group, 32768 fragments per group 8016 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736 正在寫入inode表: 完成 Creating journal (32768 blocks): 完成 Writing superblocks and filesystem accounting information: 完成 This filesystem will be automatically checked every 36 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override. [root@localhost ~]# mkswap /dev/sdb3 Setting up swapspace version 1, size = 2104508 KiB no label, UUID=7c8961cc-2b83-4b2c-9774-89cf4145d2f9 [root@localhost ~]# mkdir /mnt/{boot,rootfs} #在mnt下創建 boot 和 rootfs 目錄 [root@localhost ~]# mount /dev/sdb1 /mnt/boot #將sdb1掛載到 boot 目錄下 [root@localhost ~]# mount /dev/sdb2 /mnt/rootfs #將sdb2掛載到 rootfs 目錄下 [root@localhost ~]# grub-install --root-directory=/mnt /dev/sdb #安裝grub到/mnt,grub會自動尋找/mnt目錄并安裝grub。 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 ~]# cp /boot/vmlinuz-2.6.32-696.el6.x86_64 /mnt/boot/vmlinuz #grub安裝完成但boot目錄下沒有內核以及initrd文件需手動復制當前主機的內核及initrd至boot目錄 [root@localhost ~]# cp /boot/initramfs-2.6.32-696.el6.x86_64.img /mnt/boot/initramfs.img [root@localhost ~]# vim /mnt/boot/grub/grub.conf #新安裝的grub沒有配置文件需要手動創建grub.conf的配置文件 default=0 timeout=5 title centos6 root (hd0,0) kernel /vmlinuz selinux=0 ro root=/dev/sda2 init=/bin/bash #由于啟動時第一個程序是bash,所以需要指定init為/bin/bash initrd /initramfs.img [root@localhost ~]# mkdir -p /mnt/rootfs/{root,bin,sbin,lib,lib64,var,proc,sys,tmp,media,home,etc,dev} #為根目錄創建各子目錄 [root@localhost ~]# ldd /bin/bash #查找bash,ls,cat的依賴關系,并復制各依賴的文件至相對應的目錄下
linux-vdso.so.1 => (0x00007ffe711e9000) libtinfo.so.5 => /lib64/libtinfo.so.5 (0x0000003cf4a00000) libdl.so.2 => /lib64/libdl.so.2 (0x0000003cec600000) libc.so.6 => /lib64/libc.so.6 (0x0000003ceca00000) /lib64/ld-linux-x86-64.so.2 (0x0000003cec200000) [root@localhost ~]# cd /mnt/rootfs/lib64 [root@localhost lib64]# cp /lib64/libtinfo.so.5 . [root@localhost lib64]# cp /lib64/libdl.so.2 . [root@localhost lib64]# cp /lib64/libc.so.6 . [root@localhost lib64]# cp /lib64/ld-linux-x86-64.so.2 . [root@localhost lib64]# ldd /bin/ls linux-vdso.so.1 => (0x00007fff5d726000) libselinux.so.1 => /lib64/libselinux.so.1 (0x0000003cee200000) librt.so.1 => /lib64/librt.so.1 (0x0000003ced200000) libcap.so.2 => /lib64/libcap.so.2 (0x0000003cf9e00000) libacl.so.1 => /lib64/libacl.so.1 (0x0000003cf7a00000) libc.so.6 => /lib64/libc.so.6 (0x0000003ceca00000) libdl.so.2 => /lib64/libdl.so.2 (0x0000003cec600000) /lib64/ld-linux-x86-64.so.2 (0x0000003cec200000) libpthread.so.0 => /lib64/libpthread.so.0 (0x0000003cece00000) libattr.so.1 => /lib64/libattr.so.1 (0x0000003cfc200000) [root@localhost lib64]# cp /lib64/libse libselinux.so.1 libsemanage.so.1 libsepol.so.1 [root@localhost lib64]# cp /lib64/libselinux.so.1 . [root@localhost lib64]# cp /lib64/librt.so.1 . [root@localhost lib64]# cp /lib64/libcap.so.2 . [root@localhost lib64]# cp /lib64/libacl.so.1 . [root@localhost lib64]# cp /lib64/libpthread.so.0 . [root@localhost lib64]# cp /lib64/libattr.so.1 . [root@localhost lib64]# ldd /bin/cat linux-vdso.so.1 => (0x00007fffbcd6c000) libc.so.6 => /lib64/libc.so.6 (0x0000003ceca00000) /lib64/ld-linux-x86-64.so.2 (0x0000003cec200000) [root@localhost ~]# cp /bin/bash /mnt/rootfs/bin [root@localhost ~]# cp /bin/cat /mnt/rootfs/bin [root@localhost ~]# cp /bin/ls /mnt/rootfs/bin [root@localhost ~]# chroot /mnt/rootfs #切換根進行測試 bash-4.1# ls bin dev etc home lib lib64 lost+found media proc root sbin sys tmp var
以上步驟結束后就可以將新添加的硬盤裝到新的虛擬機上進行運行。
3、制作一個kickstart文件以及一個引導鏡像。描述其過程。
kickstart文件可以管理員根目錄下的anaconda-ks.cfg文件為模板進行修改,也可以使用system-config-kickstart工具進行制作
以下為使用system-config-kickstart工具制作kickstart文件:
1.配置默認語言、鍵盤、時區以及root密碼
2、配置安裝方法,可以使用網絡鏡像的方法進行安裝
3、配置grub相關的設置
4、分區的設置,此處注意布局分區大小時所有分區的總和一定不要超過硬盤總大小,否則自動引導會失敗
5、由于使用的是網絡鏡像的安裝方法,此處網絡的配置是必須的。
6、驗證相關、
7、防火墻相關的配置,此處可根據情況使用激活或者禁用
8、圖形界面配置、可安裝也可不安裝
9、選擇軟件包的安裝
10、此處可以配置安裝前的腳本
11、在此處可以進行安裝后腳本的配置
以上設置全部完成后進行保存。
手動編輯ks文件,將repo這行的baseurl改為阿里云
repo --name="CentOS" --baseurl="http://mirrors.aliyun.com/centos/6.9/os/x86_64/" --cost=100
現在ks文件有了,可以進行制作引導鏡像了
首先將CenOS光盤鏡像掛載上將其中的的ioslinux目錄及目錄下的文件復制出一份至/tmp/myboot目錄內
[root@localhost ~]# mount /dev/sr0 /mnt mount: block device /dev/sr0 is write-protected, mounting read-only [root@localhost ~]# mkdir /tmp/myboot [root@localhost ~]# cp -a /mnt/isolinux/ /tmp/myboot/
將制作好的ks文件放到myboot目錄中
[root@localhost ~]# cp ks.cfg /tmp/myboot/ [root@localhost ~]# cd /tmp/myboot/ [root@localhost myboot]# ls isolinux ks.cfg
現在需要修改isolinux中的isolinux.conf這個配置文件。
[root@localhost isolinux]# chmod +w isolinux.cfg [root@localhost isolinux]# vim isolinux.cfg timeout 600 display boot.msg menu background splash.jpg menu title Welcome to CentOS 6.9! menu color border 0 #ffffffff #00000000 menu color sel 7 #ffffffff #ff000000 menu color title 0 #ffffffff #00000000 menu color tabmsg 0 #ffffffff #00000000 menu color unsel 0 #ffffffff #00000000 menu color hotsel 0 #ff000000 #ffffffff menu color hotkey 7 #ffffffff #ff000000 menu color scrollbar 0 #ffffffff #00000000 label linux menu label ^Install or upgrade an existing system menu default kernel vmlinuz append initrd=initrd.img ks=cdrom:/ks.cfg #在此處添加ks=cdrom:/ks.cfg 由于ks文件已經配置網卡以及網絡此處只需要給出ks文件路徑就可以,如果沒有dhcp服務,此處需要手動指定,指定方法ip=xxx.xxx.xxx.xxx netmast=xxx.xxx.xxx.xxx gateway=xxx.xxx.xxx.xxx
以上全部完成后需要用mkisofs命令進行制作 iso文件,使用mkisofs命令制作iso文件時,工作目錄必須要在myboot目錄之外
[root@localhost tmp]# 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 /tmp/boot.iso myboot/ I: -input-charset not specified, using utf-8 (detected in locale settings) genisoimage 1.1.9 (Linux) Scanning myboot/ Scanning myboot/isolinux Excluded: myboot/isolinux/TRANS.TBL Excluded by match: myboot/isolinux/boot.cat Writing: Initial Padblock Start Block 0 Done with: Initial Padblock Block(s) 16 Writing: Primary Volume Descriptor Start Block 16 Done with: Primary Volume Descriptor Block(s) 1 Writing: Eltorito Volume Descriptor Start Block 17 Size of boot image is 4 sectors -> No emulation Done with: Eltorito Volume Descriptor Block(s) 1 Writing: Joliet Volume Descriptor Start Block 18 Done with: Joliet Volume Descriptor Block(s) 1 Writing: End Volume Descriptor Start Block 19 Done with: End Volume Descriptor Block(s) 1 Writing: Version block Start Block 20 Done with: Version block Block(s) 1 Writing: Path table Start Block 21 Done with: Path table Block(s) 4 Writing: Joliet path table Start Block 25 Done with: Joliet path table Block(s) 4 Writing: Directory tree Start Block 29 Done with: Directory tree Block(s) 2 Writing: Joliet directory tree Start Block 31 Done with: Joliet directory tree Block(s) 2 Writing: Directory tree cleanup Start Block 33 Done with: Directory tree cleanup Block(s) 0 Writing: Extension record Start Block 33 Done with: Extension record Block(s) 1 Writing: The File(s) Start Block 34 21.94% done, estimate finish Tue Aug 8 13:10:21 2017 43.79% done, estimate finish Tue Aug 8 13:10:21 2017 65.71% done, estimate finish Tue Aug 8 13:10:21 2017 87.57% done, estimate finish Tue Aug 8 13:10:21 2017 Total translation table size: 4701 Total rockridge attributes bytes: 1438 Total directory bytes: 2654 Path table size(bytes): 26 Done with: The File(s) Block(s) 22659 Writing: Ending Padblock Start Block 22693 Done with: Ending Padblock Block(s) 150 Max brk space used 0 22843 extents written (44 MB)
至此引導鏡像文件制作完成。
測試:
引導光盤可以正常使用
4、寫一個腳本
(1) 能接受四個參數:start, stop, restart, status
start: 輸出“starting 腳本名 finished.”
…
(2) 其它任意參數,均報錯退出;
#!/bin/bash # testsrv service testing script # # chkconfig: 2345 50 60 # discrption: test # # prog=$(basename $0) if [ $# -lt 1 ];then echo "usage:$prog {start|stop|restart|status}" exit 1 fi if [ $1 == start ];then echo "starting $prog finished" elif [ $1 == stop ];then echo "stopping $prog finished" elif [ $1 == restart ];then echo "restart $prog finished" elif [ $1 == status ];then if pidof $prog &> /dev/null;then echo "$prog is running" else echo "$prog is stopped" fi else echo "usage:$prog {start|stop|restart|status}" exit 2 fi
把次腳本復制到/etc/rc.d/init.d 下
然后執行 chkconfig –add SCRIPTNAME
服務就開啟了。
5、寫一個腳本,判斷給定的用戶是否登錄了當前系統;
(1) 如果登錄了,則顯示用戶登錄,腳本終止;
(2) 每3秒鐘,查看一次用戶是否登錄;
#!/bin/bash read -p "send a username:" UNAME while true ;do if who | grep "$UNAME" > /dev/null ;then break fi sleep 3 done
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 PARA if [ $PARA == cpu ];then lscpu break elif [ $PARA == mem ];then free break elif [ $PARA == disk ];then fdisk -l break elif [ $PARA == quit ];then break else echo "need a right choose" fi done
7、寫一個腳本
(1) 用函數實現返回一個用戶的UID和SHELL;用戶名通過參數傳遞而來;
(2) 提示用戶輸入一個用戶名或輸入“quit”退出;
當輸入的是用戶名,則調用函數顯示用戶信息;
當用戶輸入quit,則退出腳本;進一步地:顯示鍵入的用戶相關信息后,再次提醒輸出用戶名或quit:
原創文章,作者:M36-Masuri,如若轉載,請注明出處:http://www.www58058.com/79375