利用PXE技術批量安裝linux系統

技術背景

對與運維人員來說,如何安裝操作系統想必并不陌生;但當我們面對大量需要安裝系統的環境時,自動化安裝系統就成了一項必備的技能;下面就讓我們一起走進PXE這項批量自動化安裝操作系統的技術吧。

PXE(Pre-boot Execution Environment,預啟動執行環境)是由Intel公司開發的最新技術,工作于Client/Server的網絡模式,支持工作站通過網絡從遠端服務器下載映像,并由此支持通過網絡啟動操作系統,在啟動過程中,終端要求服務器分配IP地址,再用TFTP(trivial file transfer protocol)或MTFTP(multicast trivial file transfer protocol)協議下載一個啟動軟件包到本機內存中執行,由這個啟動軟件包完成終端基本軟件設置,從而引導預先安裝在服務器中的終端操作系統。PXE可以引導和安裝Windows,linux等多種操作系統。

工作原理

(1) Client向PXE Server上的DHCP發送IP地址請求消息,DHCP檢測Client是 否合法(主要是檢測Client的網卡MAC地址),如果合法則返回Client的 IP地址,同時將啟動文件pxelinux.0的位置信息一并傳送給Client 
(2) Client向PXE Server上的TFTP發送獲取pxelinux.0請求消息,TFTP接收 到消息之后再向Client發送pxelinux.0大小信息,試探Client是否滿意,當 TFTP收到Client發回的同意大小信息之后,正式向Client發送pxelinux.0 
(3) Client執行接收到的pxelinux.0文件 
(4) Client向TFTP Server發送針對本機的配置信息文件(在TFTP 服務的 pxelinux.cfg目錄下),TFTP將配置文件發回Client,繼而Client根據配 置文件執行后續操作。 
(5) Client向TFTP發送Linux內核請求信息,TFTP接收到消息之后將內核文件 發送給Client ? Client向TFTP發送根文件請求信息,TFTP接收到消息之后返回Linux根文 件系統 
(6) Client啟動Linux內核 
(7) Client下載安裝源文件,讀取自動化安裝腳本

配置自動化安裝系統的PXE服務器——以centos7為例

安裝前準備:

關閉防火墻和SELINUX,DHCP服務器靜態IP ;DHCP自己的ip地址要是靜態 而且在自己分配的網段內

安裝相關的軟件包

我們要搭建http服務器,tftp服務器,dhcp服務器和pxe服務,用到的軟件包有httpd,tftp-server,dhcp,syslinux。這些推薦用yum進行安裝。

準備rpm軟件包源

因為是模擬,所以直接將centos6和centos7的兩張光盤分別掛載到http服務器的目錄上:

    mkdir -p /var/www/html/centos/7
    mkdir -p /var/www/html/centos/6
    mount /dev/sr0 /var/www/html/centos/7
    mount /dev/sr1 /var/www/html/centos/6

準備kickstart文件

因為要達到安裝centos6或7最小化安裝和普通安裝的目的,因此要生成四份ks文件;我們可以在http服務器上生成存放ks文件的目錄,并將所有生成的ks文件導入。

    mkdir -p /var/www/html/ks
    mv ks6-mini.cfg /var/www/html/ks
    mv ks7-mini.cfg /var/www/html/ks
    mv ks6-normal.cfg /var/www/html/ks
    mv ks7-normal.cfg /var/www/html/ks

各文件的具體配置如下:

    第一個是ks6-mini.cfg的配置
    #Kickstart file for mini install
    #platform=x86, AMD64, or Intel EM64T
    #version=DEVEL
    #command
    install
    url --url="http://172.18.65.7/centos/6"
    text
    reboot
    lang en_US.UTF-8
    keyboard us
    network --onboot yes --device eth0 --bootproto static --ip 172.18.65.66 --netmask 255.255.0.0
    rootpw  --iscrypted $1$iVVbQ7Wq$R5CRk8Evv8AuHA85Yfy0o.
    firewall --disabled
    authconfig --enableshadow --passalgo=sha512
    selinux --disabled
    timezone Asia/Shanghai
    bootloader --location=mbr 
    zerombr
    clearpart --all --initlabel
    # The following is the partition information 
    part /boot --fstype=ext4 --size=1024 --asprimary 
    part / --fstype=ext4 --size=50000 --asprimary
    part /app --fstype=ext4 --size=50000 --asprimary
    part swap --fstype=swap --size=2048  --asprimary

    %packages
    @base
    @core
    @workstation-policy
    @server-policy
    autofs
    vim
    %end
    %post
    mkdir -p /etc/yum.repo.d/repo
    mv /etc/yum.repo.d/* /etc/yum.repo.d/repo
    cat > /etc/yum.repo.d/base.repo <<eof
    [base]
    basename=base
    baseurl=file:///misc/cd
    gpgcheck=0
    [epel]
    basename=epel
    baseurl=ftp://172.18.0.1/centos/6
    gpgcheck=0
    eof
    useradd hhy
    echo 123456 | passwd --stdin hhy
    %end

    第二個是ks7-mini.cfg的配置:
    # Kickstart file for mini install
    #platform=x86, AMD64, or Intel EM64T
    #version=DEVEL
    #command
    install
    url --url="http://172.18.65.7/centos/7"
    text
    reboot
    lang en_US.UTF-8
    keyboard us
    network --onboot yes --device ens33 --bootproto static --ip 172.18.65.66 --netmask 255.255.0.0
    rootpw  --iscrypted $1$iVVbQ7Wq$R5CRk8Evv8AuHA85Yfy0o.
    firewall --disabled
    authconfig --enableshadow --passalgo=sha512
    selinux --disabled
    timezone Asia/Shanghai
    bootloader --location=mbr 
    zerombr
    clearpart --all --initlabel
    # The following is the partition information 
    part /boot --fstype=ext4 --size=1024 --asprimary 
    part / --fstype=ext4 --size=50000 --asprimary
    part /app --fstype=ext4 --size=50000 --asprimary
    part swap --fstype=swap --size=2048  --asprimary
    %packages
    @base
    @core
    autofs
    vim
    %end
    %post
    mkdir -p /etc/yum.repo.d/repo
    mv /etc/yum.repo.d/* /etc/yum.repo.d/repo
    cat > /etc/yum.repo.d/base.repo <<eof
    [base]
    basename=base
    baseurl=file:///misc/cd
    gpgcheck=0
    [epel]
    basename=epel
    baseurl=ftp://172.18.0.1/centos/7
    gpgcheck=0
    eof
    useradd hhy
    echo 123456 | passwd --stdin hhy
    %end

    第三個是ks6-normal.cfg的配置
    # Kickstart file for normal install
    #platform=x86, AMD64, or Intel EM64T
    #version=DEVEL
    #command
    install
    url --url="http://172.18.65.7/centos/6"
    text
    reboot
    lang en_US.UTF-8
    keyboard us
    network --onboot yes --device eth0 --bootproto static --ip 172.18.65.66 --netmask 255.255.0.0
    rootpw  --iscrypted $1$iVVbQ7Wq$R5CRk8Evv8AuHA85Yfy0o.
    firewall --disabled
    authconfig --enableshadow --passalgo=sha512
    selinux --disabled
    timezone Asia/Shanghai
    bootloader --location=mbr 
    zerombr
    clearpart --all --initlabel
    # The following is the partition information 
    part /boot --fstype=ext4 --size=1024 --asprimary 
    part / --fstype=ext4 --size=50000 --asprimary
    part /app --fstype=ext4 --size=50000 --asprimary
    part swap --fstype=swap --size=2048  --asprimary
    %packages
    @base
    @core
    @debugging
    @basic-desktop
    @desktop-debugging
    @desktop-platform
    @directory-client
    @fonts
    @general-desktop
    @graphical-admin-tools
    @input-methods
    @internet-applications
    @internet-browser
    @java-platform
    @kde-desktop
    @legacy-x
    @network-file-system-client
    @office-suite
    @print-client
    @remote-desktop-clients
    @server-platform
    @server-policy
    @workstation-policy
    @x11
    mtools
    pax
    python-dmidecode
    oddjob
    wodim
    sgpio
    genisoimage
    device-mapper-persistent-data
    abrt-gui
    qt-mysql
    samba-winbind
    certmonger
    pam_krb5
    krb5-workstation
    xterm
    xorg-x11-xdm
    libXmu
    rdesktop
    %end
    %post
    mkdir -p /etc/yum.repo.d/repo
    mv /etc/yum.repo.d/* /etc/yum.repo.d/repo
    cat > /etc/yum.repo.d/base.repo <<eof
    [base]
    basename=base
    baseurl=file:///misc/cd
    gpgcheck=0
    [epel]
    basename=epel
    baseurl=ftp://172.18.0.1/centos/6
    gpgcheck=0
    eof
    useradd hhy
    echo 123456 | passwd --stdin hhy
    %end

    第四個是ks7-normal.cfg的配置
    # Kickstart file for normal install
    #platform=x86, AMD64, or Intel EM64T
    #version=DEVEL
    #command
    install
    url --url="http://172.18.65.7/centos/7"
    text
    reboot
    lang en_US.UTF-8
    keyboard us
    network --onboot yes --device ens33 --bootproto static --ip 172.18.65.66 --netmask 255.255.0.0
    rootpw  --iscrypted $1$iVVbQ7Wq$R5CRk8Evv8AuHA85Yfy0o.
    firewall --disabled
    authconfig --enableshadow --passalgo=sha512
    selinux --disabled
    timezone Asia/Shanghai
    bootloader --location=mbr 
    zerombr
    clearpart --all --initlabel
    # The following is the partition information 
    part /boot --fstype=ext4 --size=1024 --asprimary 
    part / --fstype=ext4 --size=50000 --asprimary
    part /app --fstype=ext4 --size=50000 --asprimary
    part swap --fstype=swap --size=2048  --asprimary
    %packages
    @^gnome-desktop-environment
    @base
    @core
    @desktop-debugging
    @development
    @dial-up
    @directory-client
    @fonts
    @gnome-desktop
    @guest-agents
    @guest-desktop-agents
    @input-methods
    @internet-browser
    @java-platform
    @multimedia
    @network-file-system-client
    @networkmanager-submodules
    @print-client
    @x11
    kexec-tools
    vim
    autofs
    %end
    %post
    mkdir -p /etc/yum.repo.d/repo
    mv /etc/yum.repo.d/* /etc/yum.repo.d/repo
    cat > /etc/yum.repo.d/base.repo <<eof
    [base]
    basename=base
    baseurl=file:///misc/cd
    gpgcheck=0
    [epel]
    basename=epel
    baseurl=ftp://172.18.0.1/centos/7
    gpgcheck=0
    eof
    useradd hhy
    echo 123456 | passwd --stdin hhy
    %end

配置DHCP服務

cp /usr/share/doc/dhcp-4.1.1/dhcpd.conf.sample /etc/dhcp/dhcpd.conf 

手動配置dhcp文件,如下:
subnet 172.18.65.0 netmask 255.255.255.0 {
    range 172.18.65.1 172.18.65.6;
    option routers 172.18.65.254;
    filename "pxelinux.0";
    next-server 172.18.65.7;
}

準備PXE相關文件

mkdir /var/lib/tftpboot/pxelinux.cfg
mkdir /var/lib/tftpboot/centos{6,7}
cp /var/www/html/centos/7/isolinux/{initrd.img,vmlinuz} /var/lib/tftpboot/centos7
cp /var/www/html/centos/6/isolinux/{initrd.img,vmlinuz}  /var/lib/tftpboot/centos6
cp /usr/share/syslinux/{pxelinux.0,menu.c32} /var/lib/tftpboot/
cp/misc/cd/isolinux/isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default

手動配置default啟動菜單文件,如下:
default menu.c32
timeout 600
menu title Welcome to CentOS!
label Centos6 mini
  menu label Install  an centos6 mini  system
  kernel centos6/vmlinuz
  append initrd=centos6/initrd.img ks="http://172.18.65.7/ks/ks6-mini.cfg"
label Centos6 normal
  menu label Install an centos6 normal system
  kernel centos6/vmlinuz
  append initrd=centos6/initrd.img ks="http://172.18.65.7/ks/ks6-normal.cfg"
label Centos7 mini
  menu label Install an centos7 mini system
  kernel centos7/vmlinuz
  append initrd=centos7/initrd.img ks="http://172.18.65.7/ks/ks7-mini.cfg"
label Centos7 normal
  menu label Install an centos7 normal system
  kernel centos7/vmlinuz
  append initrd=centos7/initrd.img ks="http://172.18.65.7/ks/ks7-normal.cfg"
label local
  menu label Boot from ^local drive
  menu default
  localboot 0xffff

啟動所有服務

systemctl start tftpd
systemctl start httpd
systemctl start dhcp
查看個服務的端口是否打開
ss -anlp 
各服務分別對應UDP端口69,TCP端口80,UDP端口67。

本文來自投稿,不代表Linux運維部落立場,如若轉載,請注明出處:http://www.www58058.com/87411

(3)
OscaoChaserOscaoChaser
上一篇 2017-09-18 19:58
下一篇 2017-09-19 16:17

相關推薦

  • 文本處理工具和shell腳本基本編程

    文本處理工具 grep 對文本進行搜索獲取我們想要的行(關鍵信息) egrep 支持擴展正則表達式的grep fgrep 快速搜索不支持正則    -v 取反  找出不包含“pattern”的行  -i 忽略字符的大小寫  -n 顯示行號  -c 顯示統計到的行數,等同于wc -l  -o…

    2017-04-09
  • 文本處理(1)

    文本處理工具最全整理上半部

    Linux干貨 2018-03-15
  • find命令歸納

    首先,find可以實現以下方式查找 文件名:-name -iname -regex 文件類型:-type TYPE:(f,d,l,s,b,c,p) 文件大小:-size [+|-]#UNIT #UNIT(k,M,G)范圍(由小到大):[0,#-1](#-1,#](+#,oo) 時間戳:-{a|m|c}time -{a|m|c}min  范圍(現在到…

    Linux干貨 2016-04-19
  • 馬哥教育首屆IT技術博客大賽–結果揭曉

    號外、號外??!馬哥教育首屆博客大賽已圓滿結束。感謝小伙伴們的積極參與! 噼里啪啦,小伙伴們掌聲響起來,啤酒烤鴨high起來~~咳咳,跑題了,小編好像忘了一件很重要的事: 馬幫博客大賽就是與眾不同,全銀河系也就僅此一家,為啥這么說呢,您看啊,咱就喜歡原創,鼓勵原創,酷愛原創,以原創博客作為技術交流的平臺供大家隨意圍觀和吐槽,您看看,除了馬哥這里,哪里還有這么自…

    Linux干貨 2016-06-22
  • httpd服務——CentOS7

    httpd-2.4 新特性:      (1)MPM支持運行為DSO機制;以模塊形式按需加載      (2)event MPM生產環境可用      (3)異步讀寫機制      (4)支持每個模塊…

    Linux干貨 2016-10-12
欧美性久久久久