馬哥教育網絡班21期+第10周課程練習

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

POST -->Boot Sequence(BIOS) --> Boot Loader(MBR) --> Kernel(ramdisk) -->rootfs --> switchroot --> /sbin/init -->(/etc/inittab,/etc/init/*.conf) --> 設定默認運行級別 -->系統初始化腳本 -->關閉或啟動對應級別下的服務 -->啟動終端
第一階段:BIOS啟動引導階段;在該過程中實現硬件的初始化以及查找啟動介質;從MBR中裝載啟動引導管理器(GRUB)并運行該啟動引導管理
第二階段:GRUB啟動引導階段;裝載stage1,裝載stage1.5,裝載stage2,讀取/boot/grub.conf文件并顯示啟動菜單,裝載所選的kernel和initrd文件到內存中
第三階段:內核階段:運行內核啟動參數;解壓initrd文件并掛載initd文件系統,裝載必須的驅動;掛載根文件系統
第四階段:Sys V init初始化階段:啟動/sbin/init程序;運行rc.sysinit腳本,設置系統環境,啟動swap分區,檢查和掛載文件系統;讀取/etc/inittab文件,運行在/et/rc.d/rc.d中定義的不同運行級別的服務初始化腳本;打開字符終端1-6號控制臺/打開圖形顯示管理的7號控制臺

馬哥教育網絡班21期+第10周課程練習

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
  ...
[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.
...
 
(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]# vim /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 ~]# mount -t ext4 /dev/sdb2 /mnt/sysroot 
[root@www boot]#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 var 
執行庫文件復制

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

系統安裝完成后,anaconda一般都會在root 家目錄生成一個當前系統的kickstart 文件,我參考這個文件制作我自己的kickstart文件。
[root@www ~]# cp anaconda-ks.cfg myks.cfg
[root@www ~]# vim 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. 其它任意參數,均報錯退出;

#!/bin/bash
#	
if [ $# -lt 1 ];then
cat << EOF
Usage:service.sh start|stop|restart|status
EOF
	exit 1
fi
case $1 in
start)
	echo "start $0 finish"
;;
stop)
	echo "stop $0 finish"
;;
restart)
	echo "stop $0 fiish."
	echo "start $0 fiish."
;;
status)
	echo "$0 status"
;;
*)
	echo "Erro command."
	exit 2
;;
esac

5、寫一個腳本,判斷給定的用戶是否登錄了當前系統;

  1. 如果登錄了,則顯示用戶登錄,腳本終止;

  2. 每3秒鐘,查看一次用戶是否登錄;

#!/bin/bash
read -p "Enter a user name: " username
until who | grep "^$username" &> /dev/null; do
    sleep 3
done    
echo "$username logged on." 
    exit 0

6、寫一個腳本,顯示用戶選定要查看的信息;

  • cpu) display cpu info

  • mem) display memory info

  • disk) display disk info

  • quit) quit
      非此四項選擇,則提示錯誤,并要求用戶重新選擇,只到其給出正確的選擇為止;

#!/bin/bash
#
info() {
cat << EOF
cpu) display cpu info
mem) display memory info
disk) display disk info
quit) quit
EOF
}
info
read -p "Enter command:" cmd
case $cmd in
cpu)
	cat /proc/cpuinfo
;;
mem)
	cat /proc/meminfo
;;
disk)
	fdisk -l
;;
quit)
	echo "quiting"
	exit 0
;;
*)
	echo "Erro command."
	info			
read -p "Enter command.": $1
;;
esac

7、寫一個腳本

  1. 用函數實現返回一個用戶的UID和SHELL;用戶名通過參數傳遞而來;

  2. 提示用戶輸入一個用戶名或輸入“quit”退出;
    當輸入的是用戶名,則調用函數顯示用戶信息;
    當用戶輸入quit,則退出腳本;進一步地:顯示鍵入的用戶相關信息后,再次提醒輸出用戶名或quit:

#!/bin/bash
#
userinfo() {
   uid=`cat /etc/passwd | grep "^$user" |cut -d: -f3`
   bs=`cat /etc/passwd | grep "^$user" |cut -d: -f7`
	echo "$user uid:$uid bash:$bs"
}
qt() {
			if [ $cmd == "quit" ];then
				break
			else
				echo "No exist $cmd user."
				read -p "Enter username or quit :" cmd
			fi	
}
cs() {
	while `id $cmd &>/dev/null` ;do
		user=$cmd
		userinfo
		read -p "Enter username or quit :" cmd
		until `id $cmd &> /dev/null`;do
			qt
		done
			cs
	done
}
read -p "Enter username or quit :" cmd
if [ $cmd != '' ];then
	until `id $cmd &> /dev/null`;do
		qt
	done
		cs
else
	read -p "Enter username or quit :" cmd
	cs
fi

8、寫一個腳本,完成如下功能(使用函數)

  1. 提示用戶輸入一個可執行命令的名字;獲取此命令依賴的所有庫文件;

  2. 復制命令文件至/mnt/sysroot目錄下的對應的rootfs的路徑上,例如,如果復制的文件原路徑是/usr/bin/useradd,則復制到/mnt/sysroot/usr/bin/目錄中;

  3. 復制此命令依賴的各庫文件至/mnt/sysroot目錄下的對應的rootfs的路徑上;規則同上面命令相關的要求;

#!/bin/bash
#
syspath=/mnt/sysroot
[ -d $syspath ] || mkdir /mnt/sysroot
 
read -p "please input a command:" cmd 
libcp() {
    for lib in $(ldd $1 |  grep -o "[^[:space:]]*/lib[^[:space:]]*"); do
        libdir=$(dirname $lib)
        [ -d $syspath$libdir ] || mkdir -p $syspath$libdir
        [ -f $syspath$lib ] || cp $lib $syspath$lib
    done
}
 
while [ "$cmd" != 'quit' ]; do
    if ! which $cmdname &> /dev/null; then
        read -p "No such command, enter again: " cmd
        continue
    fi
    cmd=$(which --skip-alias $cmdname)
    cmnddir=$(dirname $cmd)
 
    [ -d $syspath$cmnddir ] || mkdir -p $syspath$cmnddir
    [ -f $syspath$cmd ] || cp $cmdname $syspath$cmd
    libcp $cmd
    read -p "Another command(quit): " cmd
done

   

原創文章,作者:Net21_Lion,如若轉載,請注明出處:http://www.www58058.com/45210

(0)
Net21_LionNet21_Lion
上一篇 2016-09-19 13:49
下一篇 2016-09-19 13:49

相關推薦

  • 網絡知識及寫腳本,一個又一個

    1、請描述網橋、集線器、二層交換機、三層交換機、路由器的功能、使用場景與區別。 集線器是屬于物理層的設備,可以理解為一根網線中間串接出了多個接口,這么多接口共享這一根網線的帶寬, 因此這些接口上的設備在使得網絡時就會出現沖突,或者‘道路’擠占的情況。(所有的設備在一個廣播域,沖突域中),集線器現在很少使用了。 交換機、網橋都是數據鏈路層的設備,網橋的出現就是…

    2017-02-10
  • Corosync+Pacemaker+DRBD實現高可用 Mariadb

    一、DRBD簡介 DRBD全稱Distributed?Replicated?Block?(分布式的復制塊設備),開源項目。它是一款基于塊設備的文件復制解決方案,速度比文件級別的軟件如NFS,samba快很多,而且不會出現單點故障,是很多中小企業的共享存儲首選解決方案。 二、DRBD的工作模式從上圖中我們可以清楚的看到DRBD是工作在內核中,將協議建立在buf…

    2017-11-05
  • 重返課堂

    1.學習目的: i. IT業發展日新月異,當前所掌握的知識已無法滿足工作需要。 ii. IT行業更新飛快,這就要求從業人員不斷的去學習,不斷的完善自己,才不至于被淘汰。 iii. 在這個大數據、云計算為王的時代,掌握Linux是入行“敲門磚”。 iv. 年齡已大,把握住人生不多的專門學習的機會,再博一次。 2.淺識Linux i. linux為何? Linu…

    2018-03-26
  • grep、egrep、vim練習

    1、復制/etc/skel目錄為/home/tuser1,要求/home/tuser1及內部文件的屬組和其它用戶均沒有任何訪問權限。         [root@localhost ~]# install -d -m 600&nb…

    Linux干貨 2016-11-18
  • 磁盤管理

    磁盤管理 本文將按順序以實例演示磁盤管理的所有操作,讓我們開始吧! 一,磁盤的添加 ① 先來查看linux系統總共有幾個磁盤,由圖可知是兩個,分別是sda,sdb。我們再加一個,按照磁盤命名順序,應是sdc,他們都在/dev目錄下。 補充: 1,磁盤命名規則: 不同磁盤,按照a-z依次標識,如sda,sdb,sdc 同一磁盤的不同分區,按照1,2,&#823…

    2017-08-19
  • Linux系統重定向和管道

    1、標準輸入和標準輸出              程序:指令+數據               &n…

    Linux干貨 2016-08-04
欧美性久久久久