使用PXE自动化部署Linux系统

admin 2022年12月16日 648次浏览

1、PXE自动化安装流程

  • 客户机通过 DHCP 服务器获取 ip 地址和 TFTP 服务器的 ip 地址
  • 客户机向 TFTP 服务器请求引导文件
  • TFTP 服务器将 pxelinux.0 引导文件发送给客户机
  • 客户机向 TFTP 服务器请求内核文件和虚拟文件系统
  • TFTP 服务器将 vmlinuz 和 initrd.img 文件发送给客户机
  • 客户机向 HTTP 服务器请求 kickstart 文件
  • HTTP 服务器将指定 kickstart 返回给客户机
  • 客户机向 HTTP 服务器请求安装包
  • HTTP 服务器将安装需要的安装包返回给客户机

2、PXE自动化安装实现

2.1、安装相关软件包

[root@test-host ~]# yum install dhcp-server tftp-server httpd syslinux -y

2.2、启动相关服务

[root@test-host ~]# systemctl enable --now httpd tftp dhcpd

2.3、配置DHCP服务

详细介绍参见:DHCP服务原理简介与实战

[root@test-host ~]# cat /etc/dhcp/dhcpd.conf
option domain-name "test.com";
option domain-name-servers 61.139.2.69, 114.114.114.114;

default-lease-time 86400;
max-lease-time 172800;

log-facility local7;

subnet 192.168.137.0 netmask 255.255.255.0 {
  range 192.168.137.100 192.168.137.200;
  option routers 192.168.137.1;
  next-server 192.168.137.100;
  filename "pxelinux.0";
}

2.4、准备yum源

[root@test-host ~]# mkdir /var/www/html/centos7
[root@test-host ~]# mount /dev/cdrom /var/www/html/centos7/

2.5、准备kickstart文件

详细介绍参见:使用应答文件自动安装linux系统

# centos 7
[root@test-host ~]# grep -v "^#" /var/www/html/ksfile/centos7.cfg
install
keyboard 'us'
rootpw --plaintext 123456
lang zh_CN
auth  --useshadow  --passalgo=sha512
text
selinux --disabled
skipx

firewall --disabled
reboot
timezone Asia/Shanghai

url --url="http://192.168.137.100/centos7"
bootloader --location=mbr
zerombr
clearpart --all --initlabel
part /boot --fstype="ext4" --size=1024
part swap --fstype="swap" --size=2048
part / --fstype="xfs" --grow

%post --interpreter=/usr/bin/bash
useradd user
echo 123456 | passwd --stdin user
sed -i 's/^#PermitRootLogin prohibit-password/PermitRootLogin yes/g' /etc/ssh/sshd_config
%end

%packages
@^infrastructure-server-environment
@base
@core
chrony
kexec-tools
%end

# Rocky Linux 9
[root@test-host ~]# grep -v "^#" /var/www/html/ksfile/rocky9.cfg
text
reboot
url --url="http://192.168.137.100/rocky-9.1"

%post --interpreter=/usr/bin/bash
useradd user01
echo 123456 | passwd --stdin user01
sed -i 's/^#PermitRootLogin prohibit-password/PermitRootLogin yes/g' /etc/ssh/sshd_config
%end

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

keyboard --vckeymap=us --xlayouts='us'
lang en_US.UTF-8 --addsupport=zh_CN

firewall --disabled

%packages
@^minimal-environment

%end

selinux --disabled

firstboot --enable
skipx

bootloader --location=mbr
zerombr
clearpart --all --initlabel
part /boot --fstype="ext4" --size=1024
part swap --fstype="swap" --size=4096
part / --fstype="xfs" --grow

timezone Asia/Shanghai

rootpw --plaintext 123456

2.6、准备PXE启动相关文件

[root@test-host ~]# mkdir /var/lib/tftpboot/{centos7,rockylinux9}
# 准备内核文件
[root@test-host ~]# cp /var/www/html/centos7/isolinux/{vmlinuz,initrd.img} /var/lib/tftpboot/centos7/
[root@test-host ~]# cp /var/www/html/rocky-9.1/isolinux/{vmlinuz,initrd.img} /var/lib/tftpboot/rockylinux9
[root@test-host ~]# cp /var/www/html/rocky-9.1/isolinux/{ldlinux.c32,libcom32.c32,libutil.c32} /var/lib/tftpboot

# 准备启动文件
[root@test-host ~]# cp /usr/share/syslinux/{pxelinux.0,menu.c32} /var/lib/tftpboot/
# 准备启动菜单
[root@test-host ~]# cp /var/www/html/centos7/isolinux/isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default
[root@test-host ~]# tree /var/lib/tftpboot/
/var/lib/tftpboot/
├── centos7
│   ├── initrd.img
│   └── vmlinuz
├── ldlinux.c32
├── libcom32.c32
├── libutil.c32
├── menu.c32
├── pxelinux.0
├── pxelinux.cfg
│   └── default
└── rockylinux9
    ├── initrd.img
    └── vmlinuz

# 在RockyLinux9中,还需要将isolinux/ldlinux.c32,libcom32.c32,libutil.c32 复制到/var/lib/tftpboot/下

2.7、准备启动菜单

该示例支持安装 CentOS 7 和 RockyLinux 9

[root@test-host ~]# cat /var/lib/tftpboot/pxelinux.cfg/default
default menu.c32
timeout 600
menu title CentOS And RockyLinux Install

# 使用子菜单风格界面
# centos 7 安装选项
menu begin Install CentOS 7
  menu title Install CentOS 7

label linux 7
  menu label ^Auto Install CentOS 7 (minimal)
  kernel centos7/vmlinuz
  append initrd=centos7/initrd.img inst.ks=http://192.168.137.10/ksfile/centos7_minimal.cfg

label linux 7
  menu label Auto Install CentOS 7 (^GUI)
  kernel centos7/vmlinuz
  append initrd=centos7/initrd.img inst.ks=http://192.168.137.10/ksfile/centos7_gui.cfg

label linux 7
  menu label ^Manual Install CentOS 7
  kernel centos7/vmlinuz
  append initrd=centos7/initrd.img inst.repo=http://192.168.137.10/centos7

label linux 7
  menu indent count 5
  menu label ^Rescue a CentOS 7 system
  kernel centos7/vmlinuz
  append initrd=centos7/initrd.img inst.repo=http://192.168.137.10/centos7 rescue

# 插入空白行
menu separator

# 返回主菜单
label returntomain
  menu label Return to main menu
  menu exit

# 子菜单结束
menu end

# RockyLinux 9 安装选项
menu begin Install RockyLinux 9
  menu title Install RockyLinux 9

label linux 9
  menu label ^Auto Install RockyLinux 9 (minimal)
  kernel rockylinux9/vmlinuz
  append initrd=rockylinux9/initrd.img inst.ks=http://192.168.137.10/ksfile/rocky9_minimal.cfg

label linux 9
  menu label Auto Install RockyLinux 9 (^GUI)
  kernel rockylinux9/vmlinuz
  append initrd=rockylinux9/initrd.img inst.ks=http://192.168.137.10/ksfile/rocky9_gui.cfg

label linux 9
  menu label ^Manual Install RockyLinux 9
  kernel rockylinux9/vmlinuz
  append initrd=rockylinux9/initrd.img inst.repo=http://192.168.137.10/rocky-9.1

label linux 9
  menu indent count 5
  menu label ^Rescue a RockyLinux 9 system
  kernel rockylinux9/vmlinuz
  append initrd=rockylinux9/initrd.img inst.repo=http://192.168.137.10/rocky-9.1  rescue

menu separator

label returntomain
  menu label Return to main menu
  menu exit

menu end

menu separator

# 使用本地硬盘引导
label local
  menu default
  menu label Boot from ^local drive
  localboot 0xffff

menu end
  • 启动主界面

  • CentOS 7 安装界面

  • RockyLinux 9 安装界面