通过PXE服务器批量安装系统

Posted jks212454

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了通过PXE服务器批量安装系统相关的知识,希望对你有一定的参考价值。

一、环境介绍

1.PXE服务器版本

[root@PXE ~]# cat /etc/redhat-release 
CentOS Linux release 8.2.2004 (Core) 

2.挂载光盘

[root@PXE ~]# mount /dev/cdrom /mnt/cdrom/
mount: /mnt/cdrom: WARNING: device write-protected, mounted read-only.
[root@PXE ~]# df -h
Filesystem           Size  Used Avail Use% Mounted on
devtmpfs             1.9G     0  1.9G   0% /dev
tmpfs                1.9G     0  1.9G   0% /dev/shm
tmpfs                1.9G   12M  1.9G   1% /run
tmpfs                1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/mapper/cl-root   17G  5.0G   13G  30% /
/dev/sda1            976M  193M  717M  22% /boot
tmpfs                376M  4.6M  371M   2% /run/user/0
tmpfs                376M  4.0K  376M   1% /run/user/1000
tmpfs                376M  1.2M  375M   1% /run/user/42
/dev/sr0             7.7G  7.7G     0 100% /mnt/cdrom

3.配置yum仓库

[development]
name=rhel8
baseurl=file:///mnt/cdrom/BaseOS
baseurl=file://mnt/cdrom/AppStream
enabled=1
gpgcheck=0


[root@PXE yum.repos.d]# yum clean all
0 files removed
[root@PXE yum.repos.d]# yum makecache 
rhel8                                                       170 MB/s | 5.7 MB     00:00    
Last metadata expiration check: 0:00:01 ago on Tue 10 Aug 2021 08:12:33 PM CST.
Metadata cache created.

二、部署DHCP服务器

1.安装相关软件包

[root@PXE yum.repos.d]# yum -y install dhcp-server.x86_64 tftp-server.x86_64 
Last metadata expiration check: 0:02:19 ago on Tue 10 Aug 2021 08:23:28 PM CST.
Dependencies resolved.

2.编辑DHCP配置文件

[root@PXE dhcp]# cp /usr/share/doc/dhcp-server/dhcpd.conf.example /etc/dhcp/dhcpd.conf 
cp: overwrite '/etc/dhcp/dhcpd.conf'? yes
[root@PXE dhcp]#vim /etc/dhcp/dhcpd.conf
 # A slightly different configuration for an internal subnet.
subnet 192.168.3.0 netmask 255.255.255.0 {
  range 192.168.3.100 192.168.3.200;
  option domain-name-servers 192.168.3.1, 192.168.3.201;
  option domain-name "example.com";
  option routers 192.168.3.1;
  option broadcast-address 192.168.3.255;
  default-lease-time 600;
  max-lease-time 7200;

3.重启dhcp服务

[root@PXE dhcp]# systemctl status dhcpd
● dhcpd.service - DHCPv4 Server Daemon
   Loaded: loaded (/usr/lib/systemd/system/dhcpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2021-08-10 20:53:18 CST; 9s ago
     Docs: man:dhcpd(8)
           man:dhcpd.conf(5)
 Main PID: 35901 (dhcpd)
   Status: "Dispatching packets..."
    Tasks: 1 (limit: 23644)
   Memory: 5.5M
   CGroup: /system.slice/dhcpd.service
           └─35901 /usr/sbin/dhcpd -f -cf /etc/dhcp/dhcpd.conf -user dhcpd -group dhcpd --n>

Aug 10 20:53:18 PXE dhcpd[35901]: ** Ignoring requests on virbr0.  If this is not what
Aug 10 20:53:18 PXE dhcpd[35901]:    you want, please write a subnet declaration
Aug 10 20:53:18 PXE dhcpd[35901]:    in your dhcpd.conf file for the network segment
Aug 10 20:53:18 PXE dhcpd[35901]:    to which interface virbr0 is attached. **
Aug 10 20:53:18 PXE dhcpd[35901]: 
Aug 10 20:53:18 PXE dhcpd[35901]: Listening on LPF/ens33/00:0c:29:cd:cd:e1/192.168.3.0/24
Aug 10 20:53:18 PXE dhcpd[35901]: Sending on   LPF/ens33/00:0c:29:cd:cd:e1/192.168.3.0/24
Aug 10 20:53:18 PXE dhcpd[35901]: Sending on   Socket/fallback/fallback-net
Aug 10 20:53:18 PXE dhcpd[35901]: Server starting service.
Aug 10 20:53:18 PXE systemd[1]: Started DHCPv4 Server Daemon.

4.放行防火墙端口

[root@PXE dhcp]# firewall-cmd --permanent --add-port=67/udp
success
[root@PXE dhcp]# firewall-cmd --reload 
success
[root@PXE dhcp]# firewall-cmd --list-ports 
67/udp

5.关闭selinux

[root@PXE ~]# setenforce 0
[root@PXE ~]# getenforce 
Permissive

三、部署TFTP服务器

1.安装相关包

[root@PXE ~]# yum -y install tftp-server.x86_64 
[root@PXE ~]# yum -y install xinetd.x86_64 
Last metadata expiration check: 0:38:36 ago on Tue 10 Aug 2021 08:30:02 PM CST.
Dependencies resolved.
============================================================================================
 Package          Architecture     Version                      Repository             Size
============================================================================================
Installing:
 xinetd           x86_64           2:2.3.15-24.el8              development           135 k

Transaction Summary
============================================================================================
Install  1 Package

Total size: 135 k
Installed size: 379 k
Downloading Packages:
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                    1/1 
  Installing       : xinetd-2:2.3.15-24.el8.x86_64                                      1/1 
  Running scriptlet: xinetd-2:2.3.15-24.el8.x86_64                                      1/1 
  Verifying        : xinetd-2:2.3.15-24.el8.x86_64                                      1/1 
Installed products updated.

Installed:
  xinetd-2:2.3.15-24.el8.x86_64                                                             

Complete!

2.修改TFTP服务配置文件

[root@PXE ~]# vim /etc/xinetd.d/tftp

service tftp
{
        socket_type             = dgram
        protocol                = udp
        wait                    = yes
        user                    = root
        server                  = /usr/sbin/in.tftpd
        server_args             = -s /var/lib/tftpboot
        disable                 = no
        per_source              = 11
        cps                     = 100 2
        flags                   = IPv4
}



3.重启服务

[root@PXE ~]# systemctl enable --now xinetd
[root@PXE ~]# systemctl restart xinetd
[root@PXE ~]# systemctl enable dhcpd
[root@PXE ~]# systemctl restart dhcpd

4.放行防火墙

[root@PXE ~]# firewall-cmd --permanent --add-port=69/udp
success
[root@PXE ~]# firewall-cmd --reload 
success

5.检查相关服务运行端口

[root@PXE ~]# netstat -tunlp |grep 69
udp        0      0 0.0.0.0:69              0.0.0.0:*                           38484/xinetd        
[root@PXE ~]# netstat -tunlp |grep 67
udp        0      0 0.0.0.0:67              0.0.0.0:*                           37672/dhcpd         
udp        0      0 0.0.0.0:67              0.0.0.0:*                           1492/dnsmasq        
[root@PXE ~]# 

四 、在 dhcp 配置文件中加入 tftp 服务相关参数


[root@PXE ~]# vim /etc/dhcp/dhcpd.conf 

# option definitions common to all supported networks...
option domain-name "example.org";
option domain-name-servers ns1.example.org, ns2.example.org;

next-server 192.168.3.150;
filename "pxelinux.0"




default-lease-time 600;
max-lease-time 7200;



五、配置tftp下服务文件

1.安装 syslinux软件包



[root@PXE ~]# yum -y install syslinux
Last metadata expiration check: 1:42:48 ago on Tue 10 Aug 2021 08:30:02 PM CST.
Dependencies resolved.
============================================================================================
 Package                   Architecture   Version               Repository             Size
============================================================================================

2.将 pxelinux.0 复制到/var/lib/tftpboot

[root@PXE ~]# cp /usr/share/syslinux/pxelinux.0  /var/lib/tftpboot/
[root@PXE ~]# 

3.将光盘 isolinux 目录所有内容,拷贝到/var/lib/tftpboot 中

[root@PXE ~]# cp -r /mnt/cdrom/isolinux/* /var/lib/tftpboot/

4.创建/var/lib/tftpboot/pxelinux.cfg目录

[root@PXE ~]# mkdir /var/lib/tftpboot/pxelinux.cfg
[root@PXE ~]# 

5.将 isolinux.cfg 文件拷贝并改名为 default

[root@PXE ~]# cp /var/lib/tftpboot/isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default
[root@PXE ~]# 

六、编辑 pxe 网络安装启动菜单条

1.创建安装系统对应目录

[root@PXE tftpboot]# ls
boot.cat   initrd.img    ldlinux.c32   memtest       splash.png    vmlinuz
boot.msg   isolinux.bin  libcom32.c32  pxelinux.0    TRANS.TBL
grub.conf  isolinux.cfg  libutil.c32   pxelinux.cfg  vesamenu.c32
[root@PXE tftpboot]# mkdir ./rhel8.0
[root@PXE tftpboot]# ls
boot.cat   initrd.img    ldlinux.c32   memtest       rhel8.0     vesamenu.c32
boot.msg   isolinux.bin  libcom32.c32  pxelinux.0    splash.png  vmlinuz
grub.conf  isolinux.cfg  libutil.c32   pxelinux.cfg  TRANS.TBL

2.挂载安装所需系统的RHEL8.0光盘

[root@PXE tftpboot]# mount /dev/cdrom /mnt/cdrom/
mount: /mnt/cdrom: WARNING: device write-protected, mounted read-only.
[root@PXE tftpboot]# df -h
Filesystem           Size  Used Avail Use% Mounted on
devtmpfs             1.9G     0  1.9G   0% /dev
tmpfs                1.9G     0  1.9G   0% /dev/shm
tmpfs                1.9G   10M  1.9G   1% /run
tmpfs                1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/mapper/cl-root   17G  5.1G   12G  30% /
/dev/sda1            976M  193M  717M  22% /boot
tmpfs                376M  1.2M  375M   1% /run/user/42
tmpfs                376M  4.6M  371M   2% /run/user/0
/dev/sr0             6.7G  6.7G     0 100% /mnt/cdrom

3.拷贝相关系统启动文件到/var/lib/tftpboot/rhel8.0/下

[root@PXE pxeboot]# pwd
/mnt/cdrom/images/pxeboot
[root@PXE pxeboot]# ls
initrd.img  TRANS.TBL  vmlinuz
[root@PXE pxeboot]# cp initrd.img  vmlinuz  /var/lib/tftpboot/rhel8.0/
[root@PXE pxeboot]# ls /var/lib/tftpboot/rhel8.0/
initrd.img  vmlinuz
[root@PXE pxeboot]# 

4.修改开机启动菜单

label linux
  menu label ^Install CentOS Linux 8
  kernel vmlinuz
  append initrd=initrd.img inst.stage2=hd:LABEL=CentOS-8-2-2004-x86_64-dvd quiet

label rhel8.0
  menu label ^Install Red Hat Enterprise Linux 8.0
  kernel /rhel8.0/vmlinuz
  append initrd=/rhel8.0/initrd.img ks=http://192.168.3.150/ks/ks.cfg quiet

5.客户端开机pxe引导测试

七、安装httpd服务

1.安装httpd软件包

[root@PXE ~]# yum -y install httpd
Last metadata expiration check: 1:58:48 ago on Tue 10 Aug 2021 08:30:02 PM CST.
Dependencies resolved.
============================================================================================
 Package             Arch    Version                                Repository         Size

2.创建/pub、/ks 目录

[root@PXE ~]# mkdir /var/www/html/pub
[root@PXE ~]# mkdir /var/www/html/ks
[root@PXE ~]# 


3.设置httpd服务开机自启

[root@PXE ~]# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
[root@PXE ~]# 

八、配置ks脚本

1.将光盘自动挂载到/var/www/html/pub

[root@PXE ~]# mount /dev/cdrom /var/www/html/pub/
mount: /var/www/html/pub: WARNING: device write-protected, mounted read-only.
[root@PXE tftpboot]# cat /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Tue Aug 10 03:43:55 2021
#
# Accessible filesystems, by reference, are maintained under '/dev/disk/'.
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
/dev/mapper/cl-root     /                       xfs     defaults        0 0
UUID=77cba1b9-3e8e-48d8-8469-5ca1be92fcfd /boot                   ext4    defaults        1 2
/dev/mapper/cl-swap     swap                    swap    defaults        0 0


/dev/cdrom /var/www/html/pub iso9660 defaults 0 0



2.将 ks 文件移动到共享目录

[root@PXE ~]# cp anaconda-ks.cfg /var/www/html/ks/ks.cfg
cp: overwrite '/var/www/html/ks/ks.cfg'? yes
[root@PXE ~]# ls /var/www/html/ks/
ks.cfg

3.编辑ks脚本

#version=RHEL8
ignoredisk --only-use=sda
autopart --type=lvm
# Partition clearing information
clearpart --all --initlabel --drives=sda
# Use graphical install
url     --url="http://192.168.3.150/pub/"
# Use CDROM installation media

# Keyboard layouts
keyboard --vckeymap=us --xlayouts='us'
# System language
lang en_US.UTF-8

# Network information
network  --bootproto=dhcp --device=ens33 --ipv6=auto --activate
network  --hostname=localhost.localdomain
repo --name="AppStream" --baseurl=file:///run/install/repo/AppStream
# Root password
rootpw --iscrypted $6$ReprMjobTM5tQxpl$Mx.0WCWlwaemvuV6Vpw55laOrJDyzNCFM8iUnGUUwjVWLEEumGgBWFn9lhJHDCeKHBtRLOkF8nDeXpZ40HhcL/
# X Window System configuration information
xconfig  --startxonboot
# Run the Setup Agent on first boot
firstboot --enable
# Systemservices --disabled="chronyd"
# System timezone
timezone Asia/Shanghai --isUtc --nontp

%packages
@^graphical-server-environment
kexec-tools

%end

%addon com_redhat_kdump --enable --reserve-mb='auto'

%end

%anaconda
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok

4.修改ks.cfg权限

[root@PXE ~]# ll /var/www/html/ks/
total 4
-rw-------. 1 root root 1262 Aug 13 23:48 ks.cfg
[root@PXE ~]# chmod a+r /var/www/html/ks/ks.cfg 
[root@PXE ~]# ll /var/www/html/ks/
total 4
-rw-r--r--. 1 root root 1262 Aug 13 23:48 ks.cfg

九、重启所有相关服务

[root@PXE ~]# systemctl restart httpd
[root@PXE ~]# systemctl restart xinetd.service 
[root@PXE ~]# systemctl restart dhcpd
[root@PXE ~]# 

十、测试PXE安装系统

以上是关于通过PXE服务器批量安装系统的主要内容,如果未能解决你的问题,请参考以下文章

pxe操作系统的批量安装原理以及步骤

centos7 搭建pxe批量安装系统

kickstart+PXE+dhcp+nfs 批量无人值守自动化安装操作系统

PXE高效批量网络装机

PXE一键批量装机

linux部署pxe+kickstart批量安装linux操作系统实施方案