利用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.178linux.com/87411

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

相关推荐

  • 马哥教育网络班22期第一周课程练习1-3天

    1.描述计算机组成及其功能 CPU:  运算器  控制器  寄存器  缓存 存储器:  RAM(随机访问存储器)  I/O设备:               i:下指令,提供数据 如:键鼠     &…

    Linux干货 2016-08-15
  • 11.网络解析和网络加密

    1、详细描述一次加密通讯的过程,结合图示最佳。 加密过程 1.使用单向加密算法,提取A的文件的特征码。 2.使用A的私钥对提取出来的特征码进行加密,把加密后的特征码附加在A的文件的后面。 3.使用对称加密对刚刚的A的文件和加密后的特征码进行加密,生成对称加密密钥 4.使用B的公钥对第3步骤的对称加密的密钥进行加密,加密后附加在文件的后面。 解密过程 1.使用…

    2017-09-20
  • N26-第二周

    1、Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示。(盗图了,哈哈哈) 文件查看类命令:cat, tac, head, tail, more, less,touch    cat 查看文件内容   tac命令   反向查看文件内容  注:可对照上图 分屏查看命令:more  …

    Linux干货 2017-01-07
  • 破坏grub实验之一

    1、删除grub stage1阶段 [root@centos6 ~]# dd if=/dev/zero of=/dev/sda bs=446 count=1 1+0 records in 1+0 records out 446 bytes …

    Linux干货 2016-09-19
  • 集中练习7-bash脚本

    集中练习7-bash脚本

    Linux干货 2017-12-05
  • 逻辑卷(LVM)管理实例详解

    LVM基本术语 下面是LVM需要了解的基本术语:    物理卷(PV physical volume): 它可以是物理硬盘上的分区,也可以是整块物理硬盘;  卷组(VG volume group): 建立在物理卷之上,一个卷组至少包括一个物理卷,可以动态增删物理卷;  逻辑卷(LV logical volume):建立…

    Linux干货 2016-09-01