馬哥教育網絡班21期-第十周課程練習

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

 a,post:加電自檢,通電搜索并檢查物理設備的狀態

 b,bootsequeence(BIOS):選擇啟動順序

 c,bootloader(MBR):加載主引導程序 bootloader,硬盤分區表到內存

 d,加載內核kernel

 e,只讀方式掛載根分區文件系統(rootfs)

 f,系統初始化(運行/sbin/init)

 g,根據 /etc/inittab 中的啟動級別,運行系統初始化腳本 /etc/rc.d/rc.sysinit,打開或關閉對應啟動級別下的服務(運行 /etc/rc.d/rcx.d/ 下的腳本)

 h,啟動終端

2、為運行于虛擬機上的CentOS 6添加一塊新硬件,提供兩個主分區;

  (1) 為硬盤新建兩個主分區;并為其安裝grub;

  (2) 為硬盤的第一個主分區提供內核和ramdisk文件; 為第二個分區提供rootfs;

  (3) 為rootfs提供bash、ls、cat程序及所依賴的庫文件;

  (4) 為grub提供配置文件;

  (5) 將新的硬盤設置為第一啟動項并能夠正常啟動目標主機;

[root@server ~]# fdisk /dev/sdb

Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel

Building a new DOS disklabel with disk identifier 0x9056db41.

Changes will remain in memory only, until you decide to write them.

After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

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-2088, default 1): 

Using default value 1

Last cylinder, +cylinders or +size{K,M,G} (1-2088, default 2088): +256M

Command (m for help): n

Command action

   e   extended

   p   primary partition (1-4)

p

Partition number (1-4): 2

First cylinder (35-2088, default 35):         

Using default value 35

Last cylinder, +cylinders or +size{K,M,G} (35-2088, default 2088): +4G

Command (m for help): w

The partition table has been altered!

[root@server ~]# mkfs.ext4 /dev/sdb1;mkfs.ext4 /dev/sdb2

[root@server ~]# mkdir /mnt/{boot,sysroot}

[root@server ~]# mount /dev/sdb1 /mnt/boot/ ; mount /dev/sdb2 /mnt/sysroot/

[root@server ~]# ls /mnt/{boot,sysroot}

[root@server ~]# grub-install –root-directory=/mnt /dev/sdb #復制 GRUB 鏡像到 /mnt 的 boot 目錄下

[root@server ~]# cp /boot/vmlinuz-2.6.32-431.el6.x86_64 /mnt/boot/

[root@server ~]# cp /boot/initramfs-2.6.32-431.el6.x86_64.img /mnt/boot/

[root@server ~]# vim /mnt/boot/grub/grub.conf #新建一個 grub 配置文件

default=0

timeout=15

title CentOS6.5 homemake

  root (hd0,0)

  kernel /vmlinuz-2.6.32-431.el6.x86_64 ro root=/dev/sdb2 selinux=0 init=/bin/bash

  initrd /initramfs-2.6.32-431.el6.x86_64.img

使用第8題的腳本復制bash、ls、cat響應文件到 rootfs

新建一個虛擬機掛載此虛擬機的sdb磁盤啟動

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

####制作 ks 文件的方法

  a 通過 system-config-kickstart 生成ks配置文件

  b 通過復制已有系統ks配置文件并自定義配置

[root@cent7-server ~]# cp /root/anaconda-ks.cfg  myks.cfg    #復制root目錄下的ks配置文件,修改其中選項

[root@cent7-server ~]# vim myks.cfg 

#version=RHEL7

# System authorization information

auth –enableshadow –passalgo=sha512

# Use CDROM installation media

cdrom

# Run the Setup Agent on first boot

firstboot –enable

ignoredisk –only-use=sda

# Keyboard layouts

keyboard –vckeymap=us –xlayouts='us'

# System language

lang en_US.UTF-8

# Network information

network  –bootproto=static –device=ens160 –onboot=on –ipv6=auto

network  –hostname=localhost.localdomain

# System timezone

timezone China/Beijing –isUtc

# System bootloader configuration

bootloader –location=mbr –boot-drive=sda

# Partition clearing information

clearpart –none –initlabel

part  /boot –fstype=ext4  –size=200

part  / –fstype=ext4  –size=16000

part /swap –size=1024

repo –name="CentOS"  –baseurl=cdrom:sr0 –cost=100

%packages

@base

@core

%end

[root@cent7-server ~]# ksvalidator myks.cfg    #檢查配置文件語法

#### 制作引導盤

[root@cent7-server ~]# mkdir /media/test-cdrom

[root@cent7-server ~]# mount /dev/cdrom /media/test-cdrom/

mount: /dev/sr0 is write-protected, mounting read-only

[root@cent7-server ~]# mkdir /tmp/test-iso

[root@cent7-server ~]# cd /tmp/test-iso/

[root@cent7-server test-iso]# cp -a -r /media/test-cdrom/isolinux/ .

[root@cent7-server test-iso]# cp ~/myks.cfg .

[root@cent7-server test-iso]# ll

total 8

drwxr-xr-x. 2 root root 4096 Jul  4  2014 isolinux

-rw——-. 1 root root  804 Oct 17 10:03 myks.cfg

[root@cent7-server test-iso]# mkisofs -R -J -T -v –no-emul-boot –boot-load-size 4 –boot-info-table -V "my-test-Cent7-boot" -b isolinux/isolinux.bin -c isolinux/boot.cat -o /root/boot.iso /tmp/test-iso/

4、寫一個腳本

  (1) 能接受四個參數:start, stop, restart, status

   start: 輸出“starting 腳本名 finished.”

   …

  (2) 其它任意參數,均報錯退出;

#!/bin/bash
#

if [ $# -ne 1 ];then
  echo "Usage: $0 start|stop|restart|status"
  exit
fi
case $1 in
  start)
    echo "starting $0 finished" ;;
  stop)
    echo "stop $0 finished" ;;
  restart)
    echo "restart $0 finished" ;;
  status)
    echo "$0 status" ;;
  *)
    echo "wrong argument"
    echo "Usage: $0 start|stop|restart|status" ;;
esac

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

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

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

#!/bin/bash
#
if [ $# -ne 1 ];then
  echo "Usage: $0 username"
  exit
fi

while id $1 &> /dev/null ;do
  if who | grep "\<$1\>" &> /dev/null;then
     echo "User $1 is online"
     exit
  else
     sleep 3
     echo "User $1 is offline"
  fi
done

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

   cpu) display cpu info

   mem) display memory info

   disk) display disk info

   quit) quit

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

#!/bin/bash
#
while true;do
  read  -p "input your select: cpu|mem|disk|quit  " text
  case $text in
    cpu)
      cat /proc/cpuinfo && exit;;
    mem)
      free -m && exit;;
    disk)
      df -h && exit;;
    quit)
      exit ;;
    *)
      echo "wrong choice"
      continue ;;
  esac
done

7、寫一個腳本

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

  (2) 提示用戶輸入一個用戶名或輸入“quit”退出;

    當輸入的是用戶名,則調用函數顯示用戶信息;

    當用戶輸入quit,則退出腳本;進一步地:顯示鍵入的用戶相關信息后,再次提醒輸出用戶名或quit: 

#!/bin/bash
#
UserInfo () {
  id -u $username
  sed -n "/\<$username\>/p" /etc/passwd | awk -F: '{print $7}'
}
while true;do
  read -p "pls input username:  "  username
  if [ -z "$username" ];then
     echo "you must input username"
     exit
  fi
  if id $username &> /dev/null;then
     UserInfo $username
     continue
  elif [ "$username" == "quit" ];then
     exit
  else
     echo "invaild username"
  fi
done

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

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

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

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

#!/bin/bash
#
Judge () {
  if [ -z "$cmd" ] || ! which $cmd &> /dev/null ;then
    echo "wrong command!"
    exit
  fi
}
Copy_cmd () {
  [ ! -d $cmd_dir ] && mkdir -p $cmd_dir &> /dev/null
  cp $(which $cmd) $cmd_dir
}
Copy_lib () {
  echo $lib_path
  mkdir -p /mnt/sysroot/lib{,64}
  for list in $lib_path;do
    cp  $list  /mnt/sysroot$list
  done
}

read -p "pls input an executable command: " cmd
cmd_path=$(which $cmd | grep  -o "^/.*\/")
cmd_dir=/mnt/sysroot$(dirname $(which $cmd))
lib_path=$(ldd $(which $cmd) | grep -o "/[^[:space:]]\{1,\}")
Judge
Copy_cmd
Copy_lib

原創文章,作者:Net21_木頭,如若轉載,請注明出處:http://www.www58058.com/47488

(0)
Net21_木頭Net21_木頭
上一篇 2016-10-24 09:09
下一篇 2016-10-24 09:11

相關推薦

  • linux下find(文件查找)命令的詳解

    文件查找命令locate和find詳解 locate 配合數據庫緩存,快速查看文件位置,非實時查找( 數據庫查找) find 實際搜尋硬盤查詢文件名稱 ,實時查找 locate簡介 locate命令其實是find -name的另一種寫法,但是要比后者快得多,原因在于它不搜索具體目錄,而是搜索一個數據庫/var/lib/locat…

    Linux干貨 2016-08-18
  • 文件查找

        Linux上的所有資源都以文件的形式存在,如果是手工查找的話,勢必會浪費太多的時間,這里推薦倆款大家用于查找的工具。 文件查找    文件查找經常用到的倆款軟件,locate和find    二者區別 locate:1) 非實時查找;    &nbsp…

    Linux干貨 2016-08-18
  • Linux高級文件系統管理

                                          &n…

    系統運維 2016-09-06
  • keepalived+nginx

    keepalived可以認為是VRRP協議在Linux上的實現,主要有三個模塊,分別是core、check和vrrp。core模塊為keepalived的核心,負責主進程的啟動、維護以及全局配置文件的加載和解析。check負責健康檢查,包括常見的各種檢查方式。vrrp模塊是來實現VRRP協議的。本文基于如下的拓撲圖: 配置keepalived+nginx的方…

    Linux干貨 2016-11-04
  • 第一篇博客

    我的第一篇博客以及接下來的每一篇都要獻給linux運維了。第一次接觸linux是在大三的時候,當時并沒有想到以后還會和linux邂逅并以此謀生。 首先就從最基礎的linux分區與掛載點開始了解介紹,我們熟悉的Windows系統下的分區都分配有盤符,像c盤,d盤之類的,用來存放各種軟件和文件。而linux也有自己的分區,并將分區掛載到不同的掛載點上,掛載是指將…

    2017-07-16
  • 網絡管理(二)之IP地址劃分子網、多塊網卡共用單一IP

    網絡管理(二)IP地址   一、認識學習IP地址的組成: 1、它們可唯一標識IP 網絡中的每臺設備 2、IP地址由兩部分組成: 網絡ID:標識網絡;每個網段分配一個網絡ID 主機ID:標識單個主機;由組織分配給各設備 3、點分十進制計法表示IPv4地址: 4、如下圖,將系統中的IP地址用二進制表示,再通過轉換合成的十進制數,使用ping命令可得出:…

    Linux干貨 2016-09-05

評論列表(1條)

  • 馬哥教育
    馬哥教育 2016-10-25 12:52

    開機啟動流程 如果能用圖來說明就更好了,還有你的每一條命令,如果是為同一問題的答案,可以用“代碼”美化一下

欧美性久久久久