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

相关推荐

  • N22-第四周作业

    1、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。 [root@localhost ~]# cp -r /etc/skel /home/tuser1    (复制/etc/skel为/home/tuser1) [root@localho…

    Linux干货 2016-09-05
  • read,locate ,find 的总结及相关联系

    read 变量名 read 变量名1 [变量名2] < 文件名不支持管道read 变量名1 [变量名2] <<< “值1 [值2] …”使用read来把输入值分配给一个或者多个shell变量     -p 指定要显示的提示     # read -p…

    Linux干货 2017-08-12
  • 软链接与硬链接的分析

    Linux引用硬链接与软链接,是为了实现文件的共享,更有隐藏文件路径、增加权限安全及节省存储等的好处。很多新手不知道软链接与硬链接的区别,今天大家一起总结它们的区别吧^_^ 一,硬链接 硬链接的特性可以体现出什么是硬链接: 通过索引节点来进行链接,文件要有相同的inode及data block 不允许跨分区创建 只有在同一文件系统中的文件之间才可以,不能交叉…

    2017-07-22
  • Linux的用户,组及文件权限管理

    Linux用户与组的创建,删除,属性修改,文件权限管理

    Linux干货 2018-02-24
  • 程序员的相关笑话(二)

    从前,有一个牧羊人,他有很多的羊。一天他赶着他的那群羊到了一条公路边上。突然,有一辆保时洁急驶过来,上面坐着一个年轻人人,穿着Armani的衣服,和Cerutti的皮鞋,Ray-Ban的太阳眼镜,TAG-Heuer的手表,以前Versace的领带。 他走到牧羊人面前问牧羊人:“如果我能说出你有多少只羊,你能给我一只吗?” 牧羊人看了看他那一大群数都数不过来的…

    Linux干货 2016-07-10
  • shell脚本编程之运算

    一、数学运算   1、expr命令     此命令可以在命令行执行数学运算操作。     由于此命令使用时过于麻烦,所以不推荐使用。   2、方括号和双圆括号      $[ EXPRESSION ]      $((&…

    Linux干货 2015-12-31