第十周

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

1.POST 開機加電自檢,對計算機的cpu,硬盤,內存等進行檢測。
   2.BIOS 對引導程序進行探測,并把控制權交給引導程序。
   3.MBR  Master bootloader record主引導程序.通常位于硬盤第一扇區/dev/hda(0,0)或/dev/sda(0,0).
     此階段分三階段
     a.啟動bootloader 
     b.1.5 stage 識別 grub所在磁盤分區
     c.加載grub
   4.grub 加載 kernel,而后把控制權交給kernel.
   5.kernel自身初始化,加載驅動程序,此過程有可能會借助與ramdisk臨時根文件系統加載硬件驅動程序,進而
     加載真正的根文件系統,然后進行根系統的切換,以只讀方式掛在根文件系統。
   6.執行/sbin/init腳本,設定默認系統運行級別,運行系統初始化腳本,關閉或運行相關服務,啟動終端。
   7.進入用戶空間

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

(1)建立分區:
     fdisk /dev/sdb
     掛在硬盤
     mount /dev/sdb1 /mnt/boot
     安裝grub:
     grub-install --root-directory=/mnt  /dev/sdb
  (2) cp /boot/vmlinuz-2.6.32-431.el6.x86_64  /mnt/boot/
     cp /boot/initramfs-2.6.32-431.el6.x86_64.img  /mnt/boot/
      mount /dev/sdb2 /mnt/sysroot/
      mkdir -pv /mnt/sysroot/{bin,sbin,etc,lib,lib64,var,home,usr,mnt,media}
  (3) cp /bin/{bash,ls,cat} /mnt/sysroot/bin/
      [root@www ~]# ldd /bin/{bash,ls,cat}
        /bin/bash:
            linux-vdso.so.1 =>  (0x00007fff95fff000)
            libtinfo.so.5 => /lib64/libtinfo.so.5 (0x0000003bab200000)
            libdl.so.2 => /lib64/libdl.so.2 (0x0000003ba1600000)
            libc.so.6 => /lib64/libc.so.6 (0x0000003ba0e00000)
            /lib64/ld-linux-x86-64.so.2 (0x0000003ba0a00000)
        /bin/ls:
            linux-vdso.so.1 =>  (0x00007fffad7ff000)
            libselinux.so.1 => /lib64/libselinux.so.1 (0x0000003ba2a00000)
            librt.so.1 => /lib64/librt.so.1 (0x0000003ba1a00000)
            libcap.so.2 => /lib64/libcap.so.2 (0x0000003ba5600000)
            libacl.so.1 => /lib64/libacl.so.1 (0x0000003bae200000)
            libc.so.6 => /lib64/libc.so.6 (0x0000003ba0e00000)
            libdl.so.2 => /lib64/libdl.so.2 (0x0000003ba1600000)
            /lib64/ld-linux-x86-64.so.2 (0x0000003ba0a00000)
            libpthread.so.0 => /lib64/libpthread.so.0 (0x0000003ba1200000)
            libattr.so.1 => /lib64/libattr.so.1 (0x0000003bada00000)
        /bin/cat:
            linux-vdso.so.1 =>  (0x00007fffbc59e000)
            libc.so.6 => /lib64/libc.so.6 (0x0000003ba0e00000)
            /lib64/ld-linux-x86-64.so.2 (0x0000003ba0a00000)

        cp /lib64/libtinfo.so.5  /mnt/sysroot/lib64/
   (4)配置grub
       default=0
        timeout=5
        hiddenmenu
        password --md5 $1$TVpnE/$3jasVZKWFHAqhIaqdAcgD1 
        title CentOS (simple express)
        root (hd0,0)
        kernel /vmlinuz-2.6.32-431.el6.x86_64 ro selinux=0 root=/dev/sda2 init=/bin/bash 
        initrd /initramfs-2.6.32-431.el6.x86_64.img
        password --md5  $1$TVpnE/$3jasVZKWFHAqhIaqdAcgD1    
   (5)![](http://i.imgur.com/pKvYf9Z.png)
       ![](http://i.imgur.com/0TMUSU3.png)

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

(1) kickstart是內核安裝程序anaconda的一個配置文件,采用此文  件可以使安裝能自動完成配置而無需人為干預.kickstart文件主要分三段,命令段,程序包段已經腳本段;命令段的內容主要包括安裝的方式,安裝源的介質,安裝過程使用的語言,鍵盤的類型,網絡的配置,登陸用戶的設置,以及認證的方式,防火墻以及selinux的配置,時區的設置,引導程序的安裝,安裝倉庫的設置,硬盤分區的設置。程序包段主要包括所選擇要安裝的程序包,以%packages開頭,以%end結束。腳本段分為安裝前執行的腳本和安裝完成后執行的腳本。

命令段:

#version=DEVEL
install
cdrom
lang en_US.UTF-8
keyboard us
network --onboot yes --device eth0 --bootproto dhcp
rootpw  --iscrypted $1$WoADsfda$jd47Pzu7bqMsqYuu2vszw1
# Reboot after installation
reboot
firewall --service=ssh
authconfig --useshadow --enablemd5
selinux --enforcing
timezone --utc America/Los_Angeles
bootloader --location=mbr --driveorder=sda --append="crashkernel=auto rhgb quiet"
# The following is the partition information you requested
reboot
firewall --service=ssh
authconfig --useshadow --enablemd5
selinux --enforcing
timezone --utc America/Los_Angeles
bootloader --location=mbr --driveorder=sda --append="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 --initlabel

#part /boot --fstype=ext4 --size=300
#part / --fstype=ext4 --grow --size=3000
#part swap --grow --maxsize=1984 --size=1984



repo --name="CentOS"  --baseurl=cdrom:sr1 --cost=100


程序包段:

%packages
@Printing client
@X Window System
binutils
gcc
kernel-devel
python
@Fonts
@Printing client
@X Window System
binutils
gcc
kernel-devel
python

%end

腳本段:
%post
if [ -f /boot/grub/menu.lst -a ! -h /boot/grub/menu.lst ]; then cp /boot/grub/menu.lst /boot/grub/menu.lst.bak && sed -i 's/ rhgb//' /boot/grub/menu.lst; fi
if [ -f /boot/grub/grub.conf -a ! -h /boot/grub/grub.conf ]; then cp /boot/grub/grub.conf /boot/grub/grub.conf.bak && sed -i 's/ rhgb//' /boot/grub/grub.conf; fi
if [ -f /boot/grub2/grub.conf -a ! -h /boot/grub2/grub.conf ]; then cp /boot/grub2/grub.conf /boot/grub2/grub.conf.bak && sed -i 's/ rhgb//' /boot/grub2/grub.conf; fi
if [ -f /etc/rc.d/rc.local ]; then cp /etc/rc.d/rc.local /etc/rc.d/rc.local.backup; fi
cat >>/etc/rc.d/rc.local <<EOF
#!/bin/bash
echo
echo "Installing VMware Tools, please wait..."
if [ -x /usr/sbin/getenforce ]; then oldenforce=\$(/usr/sbin/getenforce); /usr/sbin/setenforce permissive || true; fi
mkdir -p /tmp/vmware-toolsmnt0
for i in hda sr0 scd0; do mount -t iso9660 /dev/\$i /tmp/vmware-toolsmnt0 && break; done
cp -a /tmp/vmware-toolsmnt0 /opt/vmware-tools-installer
chmod 755 /opt/vmware-tools-installer
cd /opt/vmware-tools-installer
mv upgra32 vmware-tools-upgrader-32
mv upgra64 vmware-tools-upgrader-64
mv upgrade.sh run_upgrader.sh
chmod +x /opt/vmware-tools-installer/*upgr*
umount /tmp/vmware-toolsmnt0
rmdir /tmp/vmware-toolsmnt0
if [ -x /usr/bin/rhgb-client ]; then /usr/bin/rhgb-client --quit; fi
cd /opt/vmware-tools-installer
for s in sr0 sr1; do eject -s /dev/\$s; done
./run_upgrader.sh
if [ -f /etc/rc.d/rc.local.backup ]; then mv /etc/rc.d/rc.local.backup /etc/rc.d/rc.local; else rm -f /etc/rc.d/rc.local; fi
rm -rf /opt/vmware-tools-installer
sed -i 's/3:initdefault/5:initdefault/' /etc/inittab
if [ -f /boot/grub/menu.lst.bak ]; then mv /boot/grub/menu.lst.bak /boot/grub/menu.lst; fi
if [ -f /boot/grub/grub.conf.bak ]; then mv /boot/grub/grub.conf.bak /boot/grub/grub.conf; fi
if [ -f /boot/grub2/grub.conf.bak ]; then mv /boot/grub2/grub.conf.bak /boot/grub2/grub.conf; fi
if [ -x /usr/sbin/getenforce ]; then /usr/sbin/setenforce \$oldenforce || true; fi
if [ -x /bin/systemd ]; then systemctl restart prefdm.service; else telinit 5; fi
EOF
chmod 755 /etc/rc.d/rc.local
if [ -x /bin/systemd ]; then systemctl enable rc-local.service; fi
/usr/sbin/adduser admin
/usr/sbin/usermod -p '$1$WoADsfda$jd47Pzu7bqMsqYuu2vszw1' admin
/usr/bin/chfn -f "Linux 6.5-2" admin
/bin/echo done
%end

除了直接對配置文件進行修改,可以安裝程序system-config-kickstart,使用圖形界面來配置kickstart文件:
![](http://i.imgur.com/WbdimYA.png)


 生成引導鏡像

 復制安裝光盤內的isolinux文件夾(cp -r)以及kickstart配置文件至指定目錄,使用命令 mkisofs -R -J -T -v --no-emul-boot --boot-load-size 4 --boot-info-table -V "centos 6" -c isolinux/boot.cat -b isolinux/isolinux.bin -o /root/no8.iso  myroot/
 生成iso鏡像,載入即可進行自動安裝,需要注意的是,由于kickstart有多種方式提供,需要指明具體的路徑,可以是http,ftp,cdrom等,且有可能需要配置ip,netmask,gateway,dns等。

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

#!/bin/bash

if [ "$1" = "start" ]; then
   echo "starting $0 finished"
elif [ "$1" = "stop" ]; then
   echo "stop $0 finished"
elif [ "$1" = "restart" ]; then
   echo "restart $0 finished"
elif [ "$1" = "status" ]; then
    echo " status $0 finished"
else
    echo ' error,please input one of them "start, stop,restart,status"'
fi


[root@www tmp]# bash ss.sh start
starting ss.sh finished
[root@www tmp]# bash ss.sh stop
stop ss.sh finished
[root@www tmp]# bash ss.sh sd
 error,please input one of them "start, stop,restart,status"
[root@www tmp]# bash ss.sh restart
restart ss.sh finished

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

#!/bin/bash

while true; do
  if $( who | grep "^$1" &> /dev/null); then
      echo "$1 already loged in "
      break
  fi
      sleep 3
  done

 [root@www tmp]# bash -x logtest.sh tt
    + true
    ++ grep '^tt'
    ++ who
    + sleep 3
    + true
    ++ grep '^tt'
    ++ who
    + sleep 3
    + true
    ++ grep '^tt'
    ++ who
    + sleep 3
    + true
    ++ grep '^tt'
    ++ who
    + echo 'tt already loged in '
    tt already loged in 
    + break

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

#!/bin/bash cat << EOF cpu) display cpu info mem) display memory info disk) display disk info quit) quit — ————————– EOF while true;do

read -p " please choose the item you want to know:" item

  while [ "$item" != "cpu" -a  "$item" != "mem" -a "$item" != "disk" -a  "$item" != "quit" ]; do
       echo ' please input one of above option" cpu,mem,disk,quit" '
       read -p" cpu,mem,disk,quit:" item
       done

  if [ "$item" = "quit" ];then
        break
  fi

  case $item in
  cpu)
      lscpu
     ;;
  mem)
      free
     ;;
  disk)
     fdisk -l
     ;;
  quit)
     exit
     ;;
  esac

done

~

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

#!/bin/bash
userinfo() {
   if  id $username &> /dev/null; then
       grep "$username" /etc/passwd | awk -F: '{print $3,$7}'
   else
        echo " $username doesn't exist "        
   fi
}

while true;do
read -p " please input username or quit: " option
     if [ "$option" = "quit" ]; then
            break
     else
        username=$option
        userinfo
     fi

[root@www tmp]# vim function.sh
[root@www tmp]# bash function.sh
 please input username or quit: tt
 501 /bin/bash
 please input username or quit: admin
 500 /bin/bash
 please input username or quit: quit

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

#!/bin/bash
df() {
      [ -d /mnt/sysroot$p ] || mkdir -pv /mnt/sysroot$p
}
read -p "please input a excutable command:" comm
p=$(dirname $(which $comm))
df
cp -r  $(which $comm) /mnt/sysroot$p
for i in $(ldd $(which $comm ) | grep -o "/.*[^[:space:]]$" |awk -F' ' '{print $1}')
    do
    p=$(dirname $i)
    df
    cp $i /mnt/sysroot$p
    done

   [root@www tmp]# bash -x copyter.sh ls
    + read -p 'please input a excutable command:' comm
    please input a excutable command:ls
    +++ which ls
    ++ dirname /bin/ls
    + path1=/bin
    + p=/bin
    + df
    + '[' -d /mnt/sysroot/bin ']'
    ++ which ls
    + cp -r /bin/ls /mnt/sysroot/bin
    ++ awk '-F ' '{print $1}'
    ++ grep -o '/.*[^[:space:]]$'
    +++ which ls
    ++ ldd /bin/ls
    + for i in '$(ldd $(which $comm ) | grep -o "/.*[^[:space:]]$" |awk -F'\'' '\'' '\''{print $1}'\'')'
    ++ dirname /lib64/libselinux.so.1
    + path2=/lib64
    + p=/lib64
    + df
    + '[' -d /mnt/sysroot/lib64 ']'
    + cp /lib64/libselinux.so.1 /mnt/sysroot/lib64
    + for i in '$(ldd $(which $comm ) | grep -o "/.*[^[:space:]]$" |awk -F'\'' '\'' '\''{print $1}'\'')'
    ++ dirname /lib64/librt.so.1
    + path2=/lib64
    + p=/lib64
    + df
    + '[' -d /mnt/sysroot/lib64 ']'
    + cp /lib64/librt.so.1 /mnt/sysroot/lib64
    + for i in '$(ldd $(which $comm ) | grep -o "/.*[^[:space:]]$" |awk -F'\'' '\'' '\''{print $1}'\'')'
    ++ dirname /lib64/libcap.so.2
    + path2=/lib64
    + p=/lib64
    + df
    + '[' -d /mnt/sysroot/lib64 ']'
    + cp /lib64/libcap.so.2 /mnt/sysroot/lib64
    + for i in '$(ldd $(which $comm ) | grep -o "/.*[^[:space:]]$" |awk -F'\'' '\'' '\''{print $1}'\'')'
    ++ dirname /lib64/libacl.so.1
    + path2=/lib64
    + p=/lib64
    + df
    + '[' -d /mnt/sysroot/lib64 ']'
    + cp /lib64/libacl.so.1 /mnt/sysroot/lib64
    + for i in '$(ldd $(which $comm ) | grep -o "/.*[^[:space:]]$" |awk -F'\'' '\'' '\''{print $1}'\'')'
    ++ dirname /lib64/libc.so.6
    + path2=/lib64
    + p=/lib64
    + df
    + '[' -d /mnt/sysroot/lib64 ']'
    + cp /lib64/libc.so.6 /mnt/sysroot/lib64
    + for i in '$(ldd $(which $comm ) | grep -o "/.*[^[:space:]]$" |awk -F'\'' '\'' '\''{print $1}'\'')'
    ++ dirname /lib64/libdl.so.2
    + path2=/lib64
    + p=/lib64
    + df
    + '[' -d /mnt/sysroot/lib64 ']'
    + cp /lib64/libdl.so.2 /mnt/sysroot/lib64
    + for i in '$(ldd $(which $comm ) | grep -o "/.*[^[:space:]]$" |awk -F'\'' '\'' '\''{print $1}'\'')'
    ++ dirname /lib64/ld-linux-x86-64.so.2
    + path2=/lib64
    + p=/lib64
    + df
    + '[' -d /mnt/sysroot/lib64 ']'
    + cp /lib64/ld-linux-x86-64.so.2 /mnt/sysroot/lib64
    + for i in '$(ldd $(which $comm ) | grep -o "/.*[^[:space:]]$" |awk -F'\'' '\'' '\''{print $1}'\'')'
    ++ dirname /lib64/libpthread.so.0
    + path2=/lib64
    + p=/lib64
    + df
    + '[' -d /mnt/sysroot/lib64 ']'
    + cp /lib64/libpthread.so.0 /mnt/sysroot/lib64
    + for i in '$(ldd $(which $comm ) | grep -o "/.*[^[:space:]]$" |awk -F'\'' '\'' '\''{print $1}'\'')'
    ++ dirname /lib64/libattr.so.1
    + path2=/lib64
    + p=/lib64
    + df
    + '[' -d /mnt/sysroot/lib64 ']'
    + cp /lib64/libattr.so.1 /mnt/sysroot/lib64

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

(0)
diglinuxdiglinux
上一篇 2017-03-30
下一篇 2017-03-30

相關推薦

  • Week3 Linux用戶和組管理及文本處理工具

      用戶和組管理 groupadd命令:添加組     groupadd [選項] group_name         -g GID:指定GID;默認是上一個組的GID+1;  …

    Linux干貨 2017-02-11
  • 一起學WINS系列(二)安裝、配置

    原創作品,允許轉載,轉載時請務必以超鏈接形式標明文章 原始出處 、作者信息和本聲明。否則將追究法律責任。http://jeffyyko.blog.51cto.com/28563/159128  此系列第二節:安裝服務端、配置客戶端。          完整的WINS系統…

    Linux干貨 2015-03-25
  • 博客作業網絡班22期+第6周(9.12-9.18)

    請詳細總結vim編輯器的使用并完成以下練習題1、復制/etc/rc.d/rc.sysinit文件至/tmp目錄,將/tmp/rc.sysinit文件中的以至少一個空白字符開頭的行的行首加#; [root@localhost /]# cp /etc/rc.d/rc.sysinit /tmp[root@localhost /]# sed -i 's/\…

    Linux干貨 2016-09-19
  • 第十二周:httpd和lamp

    看了一下作業內容,發現所有的都在51cto上寫過了。這里就不在做了。 畢竟當時幾篇博客寫下來,還是非常繁瑣的,不想在寫。 文檔地址:RHEL6.8編譯安裝LAMP環境:httpd-2.4+mysql5.6+php5.5;基于LAMP環境部署WordPress: http://afterdawn.blog.51cto.com/7503144/1876171 配…

    Linux干貨 2016-12-09
  • Linux系統啟動流程初識

    centos系統啟動流程 本篇僅僅講解centos5和6 centos7并不適用 Linux系統的組成部分:內核+根文件系統 內核功能: 進程管理 內存管理 網絡管理 驅動程序 文件系統 安全功能 有以下目錄結構的文件系統可以被識別為根文件系統,但根文件系統本身不存在 rootfs:/bin/ /sbin /etc/ /sys/…

    Linux干貨 2016-09-11
欧美性久久久久