利用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)
上一篇 2017-09-18 19:58
下一篇 2017-09-19 16:17

相关推荐

  • linux程序包管理

    对第五周学习的内容进行总结

    2018-01-01
  • lvm基本应用,扩展及缩减实现

    一、    LVM的创建     LVM因为他的可扩展和可伸缩的特性,被广泛的应用于磁盘管理中,创建LVM则必须先创建pv然后创建vg最后才能创建lv,他们之间的关系如下:呈金字塔结构     1、创建查看pv     …

    Linux干货 2016-03-09
  • 毕业即高薪只是别人家的好事吗?

    1 毕业即高薪?运气?梦想?还是嘘头?  同学们,你还在担心毕业后不好找工作吗,毕业即高薪只是别人家的好事吗?马哥linux又一名在校生学员毕业即20w+高薪名企,你还在等什么呢? 真的只是梦想么?还是只存在于别人家的公司? 越来越多的在校大学生凭着强烈的上进心踊跃的加入了马哥linux的学习阵营,这名研究生同学在研究生毕业的同时也从马哥linux…

    2015-09-06
  • linux中如何使用帮助

     在linux学习过程中,会遇到许多困难,尤其是一些命令掌握不牢固,不知道具体用法;或者是想要实现一些功能而不知道使用何种命令。这时求人不如求己,上网求助不如自己学会使用帮助,下面介绍几种linux帮助的用法。 1.what is +命令    执行这条命令可以显示命令的简短描述,让大家了解命令的基本功能。同时可以看到命令相关章…

    2017-07-23
  • Linux-Centos7编译内核

    编译内核     前提:         (1)准备好开发环境         (2) 获取目标主机上硬件设备的相关信息         (3) 获取目标主机系统功能的相关信息       …

    2017-07-16
  • 一、循环语句:for, while, until 循环执行:将某代码段重复运行多次重复运行多少次:循环次数事先已知循环次数事先未知有进入条件和退出条件 1.for……do……done for 变量 in 列表 ; do    循环体done 执行机制:依次将列表中的元素赋值给“变量名”; 每次赋值后即执行一次循环体; 直到列表中…

    Linux干货 2016-08-19