利用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

相關推薦

  • ?入學了

    入學了

    Linux干貨 2016-10-31
  • 源碼包安裝Apache服務

    1、安裝development tools程序包組 安裝Apache服務器前需要解決依賴性問題,比如需要安裝GCC、GCC++、OPENSSL等等,因為是在虛擬機里做實驗,這里就通過直接安裝包組的方法解決依賴性問題。 (1)掛載安裝光盤,并安裝“Development tools”包組 [root@localhost ~]# mount&…

    Linux干貨 2016-08-24
  • 網絡配置——命令家族

    Linux網絡屬性配置命令: ifcfg家族:ifconfig,route,netstat ifconfig命令:接口及地址查看和管理          ·ifconfig [INTERFACE]       &nbsp…

    Linux干貨 2016-09-07
  • N21_遲來的第三周練習

    最近忙成狗,落后了許多 1、列出當前系統上所有已經登錄的用戶的用戶名,注意:同一個用戶登錄多次,則只顯示一次即可。 # who | cut -d ' ' -f1 | uniq 2、取出最后登錄到當前系統的用戶的相關信息。 # last&nbsp…

    Linux干貨 2016-08-02
  • 馬哥教育網絡班21期+第五周課程練習

    1. 顯示/boot/grub/grub.conf中至少以一個空白字符開頭的行 [root@hadoop ~]# grep "^[[:space:]]\+" /boot/grub/grub.conf 2. 顯示/etc/rc.d/rc.sysinit文件中以#開頭,后面至少一個空白字符,而后又有至少…

    Linux干貨 2016-08-05
  • 文件查找命令之find

    文件查找命令之find   特點:實時查找,精確查找,由于find是全磁盤文件查找所有查找速度要比locate略慢一些。   find查找功能強大,下面主要介紹find查找條件的一個重要特性–德·摩根定律     德·摩根定律      非(A &&B)=(非A…

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