PXE 装机详解

===========================================================================

概述:

   在实际工作中,我们经常会遇到这样的情况:想要安装Linux但是计算机没有光驱,或者是有大批量的计算机需要同时安装Linux,如果通过光驱的方式一个个安装,不仅效率低,也不利于维护。这是时候你就需要PXE的强大功能了。本文就将讲解利用PXE自动化安装CentOS6和CentOS7的安装过程

===========================================================================

1.安装原理

安装流程

  • 客户机从自己的PXE网卡启动,向本网络中的DHCP服务器索取IP

  • DHCP服务器返回分给客户机IP

  • 客户机向本网络中的TFTP服务器索取文件

  • 客户机取得bootstrap文件后之执行引导文件完成引导

  • 然后读取配置文件,通过TFTP服务器加载内核和文件系统

  • 进入安装画面, 此时可以通过选择HTTP、FTP、NFS方式进行安装

实现PXE网络安装必需的4个要素

  • 客户机的网卡必须为PXE网卡

  • 网络中必须要有DHCP和TFTP服务器,当然这两个服务器可以是同一台物理主机

  • 所安装的操作系统必须支持网络安装。

  • 必须要有FTP,HTTP,NFS至少一个服务器,当然也可以和DHCP和TFTP服务器同为一台物理主机

2.CentOS 6和7 PXE配置流程图

3.实验环境描述:

  • 文件服务器为ftp(CentOS 7)http(CentOS 6)

  • dhcp和tftp服务器都为同一台CentOS 主机;

  • 网络环境都为VMnat1.

CentOS 7具体实现如下:

 1.安装dhcp服务器,配置自定义作用域,如下:

[root@centos7 ~]# yum install dhcp -y[root@centos7 ~]# cd /etc/dhcp/[root@centos7 dhcp]# lsdhclient.d  dhcpd6.conf  dhcpd.conf[root@centos7 dhcp]# cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example ./dhcpd.confcp: overwrite ‘./dhcpd.conf’? y   [root@centos7 dhcp]# vim dhcpd.conf  subnet 10.1.0.0 netmask 255.255.0.0 {     range 10.1.252.151 10.1.252.161;	 filename "pxelinux.0";	 next-server 10.1.252.153;	 }

 启动dhcp服务,验证端口(67/udp)是否处于监听状态

[root@centos7 dhcp]# systemctl start dhcpd[root@centos7 dhcp]# ss -unlState       Recv-Q Send-Q Local Address:Port               Peer Address:Port              UNCONN      0      0              *:67                         *:*                  UNCONN      0      0              *:68                         *:*                  UNCONN      0      0      127.0.0.1:323                        *:*                  UNCONN      0      0              *:31084                      *:*                  UNCONN      0      0              *:3031                       *:*                  UNCONN      0      0              *:514                        *:*                  UNCONN      0      0             :::69                        :::*                  UNCONN      0      0             :::50914                     :::*                  UNCONN      0      0             :::46863                     :::*                  UNCONN      0      0            ::1:323                       :::*                  UNCONN      0      0             :::514                       :::*

 2.配置tftp服务器,启动,查看其端口69/udp如下:

[root@centos7 ~]# yum install tftp-server tftp -y  # 安装 [root@centos7 ~]# systemctl start tftp.socket      # 启动[root@centos7 dhcp]# ss -unl                       # 查看端口69/udpState       Recv-Q Send-Q Local Address:Port               Peer Address:Port              UNCONN      0      0              *:67                         *:*                  UNCONN      0      0              *:68                         *:*                  UNCONN      0      0      127.0.0.1:323                        *:*                  UNCONN      0      0              *:31084                      *:*                  UNCONN      0      0              *:3031                       *:*                  UNCONN      0      0              *:514                        *:*                  UNCONN      0      0             :::69                        :::*

 3.准备安装树

[root@centos7 ~]# mkdir /var/ftp/pub/CentOS7[root@centos7 ~]# mount -r /dev/cdrom /var/ftp/pub/CentOS7[root@centos7 ~]# dfFilesystem     1K-blocks     Used Available Use% Mounted on/dev/sda2       41922560  1017984  40904576   3% /devtmpfs          477776        0    477776   0% /devtmpfs             500644        0    500644   0% /dev/shmtmpfs             500644    13168    487476   3% /runtmpfs             500644        0    500644   0% /sys/fs/cgroup/dev/sda3       20961280 12449428   8511852  60% /usr/dev/sda1         496300   192472    303828  39% /boot/dev/sda6        5094652    11284   5022748   1% /testdirtmpfs             100132        0    100132   0% /run/user/0/dev/sr0         7587292  7587292         0 100% /var/ftp/pub/CentOS7  # 挂载成功[root@centos7 CentOS7]# lsCentOS_BuildTag  EFI  EULA  GPL  p_w_picpaths  isolinux  LiveOS  Packages  repodata  RPM-GPG-KEY-CentOS-7  RPM-GPG-KEY-CentOS-Testing-7  TRANS.TBL[root@centos7 CentOS7]# systemctl start vsftpd # 启动ftp服务

 4.准备/var/lib/tftpboot下文件

 我们需要安装syslinux程序包,它会提供 pxelinux.0 文件,然后复制到其目录下;[root@centos7 dhcp]#  yum install syslinux -y[root@centos7 dhcp]# cp /usr/share/syslinux/pxelinux.0  /var/lib/tftpboot/ 挂载关盘,复制光盘中p_w_picpaths/pxeboot下的文件(为pxe安装专用的内核等文件)[root@centos7 ~]# cp /var/ftp/pub/CentOS7/p_w_picpaths/pxeboot/{initrd.img,vmlinuz} /var/lib/tftpboot/[root@centos7 ~]# ls /var/lib/tftpboot/initrd.img  pxelinux.0  vmlinuz 复制syslinux程序包下的文件,分别为基于链式方式引导文件,内存磁盘引导项,菜单项,内存当磁盘的模拟文件[root@centos7 ~]# cp /usr/share/syslinux/{chain.c32,mboot.c32,menu.c32,memdisk}  /var/lib/tftpboot[root@centos7 ~]# ls /var/lib/tftpboot/chain.c32  initrd.img  mboot.c32  memdisk  menu.c32  pxelinux.0  vmlinuz

  在/var/lib/tftpboot下创建为menu.c32提供菜单的配置文件(菜单加载时用到的配置文件信息)

[root@centos7 tftpboot]# mkdir pxelinux.cfg[root@centos7 tftpboot]# vim pxelinux.cfg/default default menu.c32         prompt 5     timeout 30     MENU TITLE CentOS 7 PXE     LABEL linux     MENU LABEL Install CentOS 7 X86_64     KERNEL vmlinuz     APPEND initrd=initrd.img inst.repo=ftp://10.1.252.153/pub/CentOS7/

 5.此时,如果我们想手动安装,到这步就已经可以了,不防我们先测试一下

  1)新建一个虚拟机,网卡改到和10.1.252.153同一网络,如下

  2)启动此虚拟机,可以看到能正常获取到IP地址,并显示我们自己设定的安装界面,如下:

  3)5s钟之后开始加载程序,然后进入到手动安装界面,如下:

 6.制作kickstart文件,放到/var/ftp/pub/kickstarts/下

[root@centos7 ~]# cd /var/ftp/pub/[root@centos7 pub]# mkdir kickstarts[root@centos7 pub]# lsCentOS7  kickstarts# kickstart文件如下:[root@centos7 kickstarts]# cat centos7.cfg#platform=x86, AMD64, 或 Intel EM64T#version=DEVEL# Install OS instead of upgradeinstall# X Window System configuration informationxconfig  --startxonboot# Keyboard layouts# old format: keyboard us# new format:keyboard --vckeymap=cn --xlayouts='cn'# Reboot after installationreboot# Root passwordrootpw --iscrypted $1$HDHf2v4i$LMo.xGHfxQDOf8e043C.g/# System timezonetimezone Asia/Shanghai# Use network installationurl --url="   # 地址要和/var/lib/tftpboot/pxelinux.cfg/default中的 # System languagelang zh_CNuser --groups=wheel --name=taotao --password=$6$1eWrOetY$DfLZgquGiOaNkwpEF4ItMipz.1kpn7Q.s6oXyoW2bALkWesRWHCCACICkJ.2zBXDbwEw0ibR.wxStLI41ZhfF1 --iscrypted --gecos="MageEdu"# Firewall configurationfirewall --disabled# Network informationnetwork  --bootproto=dhcp --device=eth0# System authorization informationauth  --useshadow  --passalgo=sha512# Use text mode installtextfirstboot --disable# SELinux configurationselinux --permissiveignoredisk --only-use=sda# System bootloader configurationbootloader --location=mbr --boot-drive=sda# Clear the Master Boot Recordzerombr# Partition clearing informationclearpart --all --initlabel # Disk partitioning informationpart /boot --asprimary --fstype="xfs" --size=512part swap --fstype="swap" --size=2048part /usr --fstype="xfs" --size=20480part / --fstype="xfs" --size=20480%packages@base@core@fonts@internet-browser@java-platform@kde-apps@kde-desktop@network-file-system-client@networkmanager-submodules@x11%end%post%end

 7.在/var/lib/tftpboot/pxelinux.cfg中配置引导保证能加载kickstart文件,如下:

[root@centos7 tftpboot]# cd pxelinux.cfg/[root@centos7 pxelinux.cfg]# lsdefault[root@centos7 pxelinux.cfg]# vim default default menu.c32       prompt 5   timeout 30   MENU TITLE CentOS 7 PXE   LABEL linux_auto   MENU LABEL Install CentOS 7 x86_64 auto   KERNEL vmlinuz                                     # 添加ks文件的url   APPEND initrd=initrd.img inst.repo=ftp://10.1.252.153/pub/CentOS7/ ks=ftp://10.1.252.153/pub/kickstarts/centos7.cfg   LABEL linux   MENU LABEL Install CentOS 7 x86_64   KERNEL vmlinuz   APPEND initrd=initrd.img inst.repo=ftp://10.1.252.153/pub/CentOS7/

 8.测试,如上步骤5中建立的虚拟机,打开,发现我们新建立的自动安装项,并且自动化安装开始

如上,就是使用PXE实现CentOS 7自动化安装的整个过程了。。。

===========================================================================

CentOS 6 具体实现

实验环境描述:

  • 文件服务器为http

  • dhcp和tftp及httpd服务器都为同一台CentOS 6主机;

  • 网络环境都为VMnat1.

1.安装dhcp服务器,配置自定义作用域,如下:

[root@CentOS6 ~]# yum install dhcp -y[root@CentOS6 ~]# cd /etc/dhcp/[root@CentOS6 dhcp]# lsdhclient.d  dhclient-eth0.conf  dhcpd6.conf  dhcpd.conf[root@CentOS6 dhcp]# cp /usr/share/doc/dhcp-4.1.1/dhcpd.conf.sample ./dhcpd.conf[root@CentOS6 dhcp]# vim dhcpd.conf subnet 10.1.0.0 netmask 255.255.0.0 {     range 10.1.252.71 10.1.252.80;     filename "pxelinux.0";     next-server 10.1.252.205;}

  启动dhcp服务,查看其端口67/udp

[root@CentOS6 pxelinux.cfg]# service dhcpd startStarting dhcpd:                                            [  OK  ][root@CentOS6 ~]# ss -unlState       Recv-Q Send-Q         Local Address:Port           Peer Address:Port UNCONN      0      0                          *:111                       *:*     UNCONN      0      0                          *:631                       *:*     UNCONN      0      0                          *:898                       *:*     UNCONN      0      0                          *:49172                     *:*     UNCONN      0      0                  127.0.0.1:955                       *:*     UNCONN      0      0                          *:67                        *:*     UNCONN      0      0                          *:68                        *:*     UNCONN      0      0                         :::57053                    :::*     UNCONN      0      0                         :::111                      :::*     UNCONN      0      0                         :::898                      :::*

 2.配置tftp服务器,启动,查看其端口69/udp如下:

[root@centos7 ~]# yum install tftp-server tftp -y  # 安装 [root@CentOS6 ~]# chkconfig tftp on[root@CentOS6 ~]# service xinetd restart Stopping xinetd:                                           [FAILED]Starting xinetd:                                           [  OK  ][root@centos7 dhcp]# ss -unl                       # 查看端口69/udpState       Recv-Q Send-Q Local Address:Port               Peer Address:Port              UNCONN      0      0              *:67                         *:*                  UNCONN      0      0              *:68                         *:*                  UNCONN      0      0      127.0.0.1:323                        *:*                  UNCONN      0      0              *:31084                      *:*                  UNCONN      0      0              *:3031                       *:*                  UNCONN      0      0              *:514                        *:*                  UNCONN      0      0             :::69

 3.准备安装树

[root@CentOS6 ~]# mkdir /var/www/html/CentOS6[root@CentOS6 ~]# mount -r /dev/cdrom /var/www/html/CentOS6[root@CentOS6 ~]# cd /var/www/html/CentOS6[root@CentOS6 CentOS6]# lsCentOS_BuildTag  EULA  p_w_picpaths    Packages                  repodata              RPM-GPG-KEY-CentOS-Debug-6     RPM-GPG-KEY-CentOS-Testing-6EFI              GPL   isolinux  RELEASE-NOTES-en-US.html  RPM-GPG-KEY-CentOS-6  RPM-GPG-KEY-CentOS-Security-6  TRANS.TBL[root@CentOS6 ~]# service httpd start

 4.准备/var/lib/tftpboot下文件

[root@CentOS6 ~]# yum install syslinux -y[root@CentOS6 ~]# cp /usr/share/syslinux/pxelinux.0  /var/lib/tftpboot/[root@CentOS6 ~]# cp /var/www/html/CentOS6/p_w_picpaths/pxeboot/{vmlinuz,initrd.img} /var/lib/tftpboot/[root@CentOS6 ~]# cp /var/www/html/CentOS6/isolinux/{boot.msg,vesamenu.c32,splash.jpg} /var/lib/tftpboot/[root@CentOS6  ~]# ls /var/lib/tftpboot/boot.msg  initrd.img  pxelinux.0  splash.jpg  vesamenu.c32  vmlinuz

  /var/lib/tftpboot下创建提供菜单的配置文件(菜单加载时用到的配置文件信息)

[root@CentOS6 ~]# mkdir /var/lib/tftpboot/pxelinux.cfg[root@CentOS6 ~]# cp /var/www/html/CentOS6/isolinux/isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default

 5.制作kickstart文件,放到/var/www/html/kickstarts/下

[root@CentOS6 html]# mkdir kickstarts# kickstart文件如下:[root@CentOS6 kickstarts]# cat centos6.cfg#platform=x86, AMD64, or Intel EM64T#version=DEVEL# Firewall configurationfirewall --disabled# Install OS instead of upgradeinstall# Use network installationurl --url="http://10.1.252.205/CetnOS6/"# Root passwordrootpw --iscrypted $1$iRHppr42$VMesh73wBqhUTjKp6OYOD.# System authorization informationauth  --useshadow  --passalgo=sha512# Use text mode installtextfirstboot --disable# System keyboardkeyboard us# System languagelang en_US# SELinux configurationselinux --disabled# Installation logging levellogging --level=info --host=10.1.0.1# Reboot after installationreboot# System timezonetimezone  Asia/Shanghai# System bootloader configurationbootloader --append="rhgb crashkernel=auto quiet" --location=mbr --driveorder="sda"# Clear the Master Boot Recordzerombr# Partition clearing informationclearpart --all  # Disk partitioning informationpart /boot --fstype="ext4" --size=200part pv.008 --size=61440volgroup vg0 --pesize=8192 pv.008logvol / --fstype=ext4 --name=root --vgname=vg0 --size=20480logvol swap --name=swap --vgname=vg0 --size=2048logvol /usr --fstype=ext4 --name=usr --vgname=vg0 --size=10240logvol /var --fstype=ext4 --name=var --vgname=vg0 --size=20480%post%end%packages@base@basic-desktop@chinese-support@client-mgmt-tools@core@desktop-platform@fonts@general-desktop@graphical-admin-tools@legacy-x@network-file-system-client@perl-runtime@remote-desktop-clients@x11%end

 6.在/var/lib/tftpboot/pxelinux.cfg中配置引导保证能加载kickstart文件,如下:

[root@CentOS6 pxelinux.cfg]# cat defaultdefault vesamenu.c32#prompt 1timeout 600display boot.msgmenu background splash.jpgmenu title Welcome to CentOS 6.8!menu color border 0 #ffffffff #00000000menu color sel 7 #ffffffff #ff000000menu color title 0 #ffffffff #00000000menu color tabmsg 0 #ffffffff #00000000menu color unsel 0 #ffffffff #00000000menu color hotsel 0 #ff000000 #ffffffffmenu color hotkey 7 #ffffffff #ff000000menu color scrollbar 0 #ffffffff #00000000label linux  menu label ^Install or upgrade an existing system  kernel vmlinuz  append initrd=initrd.imglabel linux_auto      # 自定义的安装label  menu label ^Install or upgrade an existing system auto  menu default  kernel vmlinuz          # ks文件路径   append initrd=initrd.img ks=http://10.1.252.205/CentOS6/  label vesa  menu label Install system with ^basic video driver  kernel vmlinuz  append initrd=initrd.img nomodesetlabel rescue  menu label ^Rescue installed system  kernel vmlinuz  append initrd=initrd.img rescuelabel local  menu label Boot from ^local drive  localboot 0xfffflabel memtest86  menu label ^Memory test  kernel memtest  append -

 7.新建一个虚拟机,要和服务器主机在同一网络中(VMnat1),启动后,成功获取ip,跳转到安装界面,可以看到我们自定义的auto安装项;

   启动安装,如下:

如上,就是使用PXE技术安装CentOS 6的整个过程了。。。

==========================================================================

总结:

  • CentOS 7

  • CentOS 6

实验中要注意的问题:

  • 一般出现的问题会在kickstart文件中,因为自动化安装会检查所依赖的环境和程序包,所以,如果不存在的话就会报错。