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

相关推荐

  • 第二周作业

    1. Linux上文件管理类命令总结及示例 文件管理命令主要由查看类命令和管理类命令组成 查看类命令 cat:显示文本 cat [OPTION]… [FILE].. tac:倒序查看文件内容 tac [OPTION]… [FILE].. head:显示文件前几行内容 head [OPTION]… [FILE].. -n#:指定获取前#行,也可直接使用-# -…

    Linux干货 2016-12-10
  • 根据作业浅析正则表达式

        什么是正则?正则就是,那种体现出某种规律的不变性或者对称性的物理量或关系。     正则表达式(Regular Expression):由一类特殊字符及文本字符所编写的模式,其中有些字符(元字符)不表示字符字面意义,而表示控制或通配的功能(linux中,可以使用:man …

    Linux干货 2017-07-30
  • 源码安装

    1.连接教室yum源: [root@localhost ~]#lftp 10.1.0.1 2.进入httpd目录下: [root@localhost ~]#lftp10.1.0.1:/pub/Sources/sources/httpd> ls 3.下载安装包: [root@localhost ~]#lftp10.1.0.1:/pub/Sources/s…

    Linux干货 2016-09-19
  • openvpn安装配置过程

    前言     之前为了方便远程办公时访问公司的内部系统,如:svn、OA、wiki、禅道等等;通通在防火墙上做了端口映射。然后有个内部系统被黑了,各种弱口令没办法。果断关闭端口映射,看来还是得搭建个VPN服务器,vpn设备感觉大材小用。马上就想到了开源的openvpn,下面就来介绍openvpn的安装配置过程。 ope…

    Linux干货 2015-05-11
  • linux基础

    1.计算机的组成及其功能。 计算机的主要组成部分可以归纳为以下五个部分:控制器、运算器、存储器、输入设备、和输出设备。 控制器 是整个计算机的中枢神经,其功能是对程序规定的控制信息进行解释,根据其要求进行控制,调度程序、数据、地址,协调计算机各部分工作及内存与外设的访问等。 运算器 运算器的功能是对数据进行各种算术运算…

    Linux干货 2016-08-15
  • 文件系统层次标准FHS

    FHS针对目录树架构仅定义出三层目录下应该放置哪些数据,分别是下面三个目录: /(根目录):与开机系统有关; /usr(unix software resource):与软件安装执行有关; /var(variable):与系统运作过程有关。   下面分别对上述三层目录进行详细的阐述。   (1) /(根目录)   根目录是整个系统最重要的一个目录,…

    Linux干货 2016-10-19