Automated Installation Using PXE
Introduction to PXE
PXE: Preboot Excution Environment, the pre-boot execution environment, is developed by Intel Corporation, based on the Client/Server network mode, which supports remote hosts to download images from remote servers through the network, and thus supports booting the operating system through the network, which can boot and Install Windows, Linux and other operating systems
How PXE Boot Works
pxelinux.0 is the boot file used by redhat-based Linux systems for PXE remote booting
pxelinux.cfg/default is the default boot menu file. By editing the pxelinux.cfg file, you can customize the boot options, operating system image, kernel, memory size and other information, providing more choices and flexibility for computer startup
vmlinuz is a bootable, compressed kernel that Linux can use hard disk space as virtual memory, hence the name "vm"
initrd.img is a temporary root file system used during system boot
PXE realizes the automatic installation process
- The client sends an IP address request message to the DHCP on the PXE Server, and the DHCP detects whether the client is legal (mainly detecting the MAC address of the client's network card), and if it is legal, returns the client's IP address, and at the same time starts the TFTP server address where the file pxelinux.0 is located The information is also sent to the Client
- The client sends a pxelinux.0 request message to the TFTP server. After receiving the message, the TFTP server sends the pxelinux.0 size information to the client to test whether the client is satisfied. When TFTP receives the agreed size information sent back by the client, it formally sends it to the client. pxelinux.0
- Client executes the received pxelinux.0 file and uses this file to start
- The client sends a request to the TFTP server for the local configuration information file (under the pxelinux.cfg directory of the TFTP server), and the TFTP server sends the boot menu configuration file back to the Client, and then the Client performs subsequent operations according to the boot menu configuration file
- Client sends Linux kernel and initrd file request information to TFTP according to the information in the boot menu configuration file, and TFTP sends the kernel and initrd file to Client after receiving the message
- Client sends root file request information to TFTP, and TFTP returns the Linux root file system after receiving the message
- Client starts the Linux kernel and loads relevant kernel parameters
- Client downloads the kickstart file through the kernel parameters, and according to the installation information in the kickstart file, downloads the installation source file for automatic installation
experiment
Realize PXE automation installation centos6/7/8 in centos8
environment
NAT mode, turn off the DHCP function of vmware
The DHCP server, TFTP server, and HTTP server are located on the same computer (Centos8)
Close firewalld and selinux
Packages: dhcp-server, tftp-server, httpd, syslinux-nonlinux (provides pxelinux.0)
process
Install packages on centos8 192.168.28.151
[root@wenzi ~]# dnf -y install dhcp-server tftp-server httpd syslinux-nonlinux
[root@wenzi ~]# systemctl enable --now httpd tftp dhcpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
Created symlink /etc/systemd/system/sockets.target.wants/tftp.socket → /usr/lib/systemd/system/tftp.socket.
Created symlink /etc/systemd/system/multi-user.target.wants/dhcpd.service → /usr/lib/systemd/system/dhcpd.service.
Job for dhcpd.service failed because the control process exited with error code.
See "systemctl status dhcpd.service" and "journalctl -xe" for details.
#启动dhcpd报错是因为dhcpd只有配置过/etc/dhcp/dhcpd.conf配置文件才可以正常启动
Configure DHCP
[root@wenzi ~]# vim /etc/dhcp/dhcpd.conf
#
# DHCP Server Configuration file.
# see /usr/share/doc/dhcp-server/dhcpd.conf.example
# see dhcpd.conf(5) man page
#
#
option domain-name "example.com";
option domain-name-servers 180.76.76.76,223.6.6.6;
default-lease-time 600;
max-lease-time 7200;
log-facility local7;
subnet 192.168.28.0 netmask 255.255.255.0 {
range 192.168.28.152 192.168.28.159;
option routers 192.168.28.2;
#指定TFTP服务器地址
next-server 192.168.28.151;
filename "pxelinux.0";
}
[root@wenzi ~]# systemctl start dhcpd
[root@wenzi ~]# ss -tunpl
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
udp UNCONN 0 0 0.0.0.0:67 0.0.0.0:* users:(("dhcpd",pid=1857,fd=10))
udp UNCONN 0 0 *:69 *:* users:(("systemd",pid=1,fd=58))
tcp LISTEN 0 128 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=889,fd=4))
tcp LISTEN 0 128 [::]:22 [::]:* users:(("sshd",pid=889,fd=6))
tcp LISTEN 0 128 *:80 *:* users:(("httpd",pid=1630,fd=4),("httpd",pid=1629,fd=4),("httpd",pid=1628,fd=4),("httpd",pid=1623,fd=4))
Prepare mirror source
[root@wenzi ~]# mkdir /var/www/html/centos/{6,7,8}
[root@wenzi ~]# tree /var/www/html/
/var/www/html/
└── centos
├── 6
├── 7
└── 8
#关闭虚拟机,再添加二个cd/dvd,分别使用centos6、centos7的ISO,开启虚拟机
[root@wenzi ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 3.7G 0 rom
sr1 11:1 1 4.4G 0 rom
sr2 11:2 1 7.7G 0 rom
nvme0n1 259:0 0 20G 0 disk
├─nvme0n1p1 259:1 0 1G 0 part /boot
└─nvme0n1p2 259:2 0 19G 0 part
├─cl-root 253:0 0 17G 0 lvm /
└─cl-swap 253:1 0 2G 0 lvm [SWAP]
#将iso分别对应挂载至httpd
[root@wenzi ~]# mount /dev/sr0 /var/www/html/centos/6
mount: /var/www/html/centos/6: WARNING: device write-protected, mounted read-only.
[root@wenzi ~]# mount /dev/sr1 /var/www/html/centos/7
mount: /var/www/html/centos/7: WARNING: device write-protected, mounted read-only.
[root@wenzi ~]# mount /dev/sr2 /var/www/html/centos/8
mount: /var/www/html/centos/8: WARNING: device write-protected, mounted read-only.
Visit 192.168.28.151/centos to view each mirror resource
prepare answer file
[root@wenzi ~]# mkdir /var/www/html/ks/{6,7,8} -p
[root@wenzi ~]# tree /var/www/html/ks/
/var/www/html/ks/
├── 6
├── 7
└── 8
centos8 kickstart answer file
[root@wenzi ~]# vim /var/www/html/ks/8/centos8_ks.cfg
#version=RHEL8
#只使用系统中的第一块磁盘
ignoredisk --only-use=nvme0n1
#清除所有磁盘的分区表
zerombr
#文本方式安装
text
#graphical
#成功安装后重启
reboot
# 清除系统上所有分区
clearpart --all --initlabel
#关闭selinux
selinux --disabled
#关闭防火墙
firewall --disabled
#通过http远程服务器的目录树安装
url --url=http://192.168.28.151/centos/8/
#键盘
keyboard --vckeymap=us --xlayouts='us'
#系统语言
lang en_US.UTF-8
#网络配置
network --bootproto=dhcp --device=ens160 --ipv6=auto --activate
#network --bootproto=static --device=ens160 --gateway=192.168.28.2 --ip=192.168.28.152 --nameserver=223.5.5.5,180.76.76.76 --netmask=255.255.255.0 --ipv6=auto --activate
#主机名
network --hostname=wenzi
#root加密后的密码
rootpw --iscrypted $6$CEK/siEJwPyzpudd$cJ/rz9oiqDmc8LmI1uZmI8BRFxje4OnuGS0qLBRZ4cAFWqKg482sPuN1BstCMY62NN58ivBzZ.bGD.0QFaYnf/
# Run the Setup Agent on first boot
firstboot --enable
#不启动图形化
skipx
#开机时启动或关闭的服务
services --disabled="chronyd"
#系统时区
timezone Asia/Shanghai --isUtc --nontp
#分区信息
part /boot --fstype="ext4" --ondisk=nvme0n1 --size=1024
part /swap --fstype="swap" --ondisk=nvme0n1 --size=2048
part / --fstype="xfs" --ondisk=nvme0n1 --grow --size=1 #根分区/使用磁盘剩余的所有空间
#需要的安装包
%packages
@^minimal-environment
kexec-tools
vim
%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
pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
%end
centos7 kickstart answer file
[root@wenzi ~]# vim /var/www/html/ks/7/centos7_ks.cfg
install
keyboard --vckeymap=us --xlayouts='us'
rootpw --iscrypted $6$CEK/siEJwPyzpudd$cJ/rz9oiqDmc8LmI1uZmI8BRFxje4OnuGS0qLBRZ4cAFWqKg482sPuN1BstCMY62NN58ivBzZ.bGD.0QFaYnf/
url --url="http://192.168.28.151/centos/7/"
lang en_US
auth --useshadow --passalgo=sha512
text
firstboot --enable
selinux --disabled
skipx
services --disabled="chronyd"
ignoredisk --only-use=nvme0n1
firewall --disabled
network --bootproto=dhcp --device=ens33
reboot
timezone Asia/Shanghai --isUtc --nontp
bootloader --append="crashkernel=auto" --location=mbr --boot-drive=nvme0n1
zerombr
clearpart --all --initlabel
part /boot --fstype="ext4" --ondisk=nvme0n1 --size=1024
part /swap --fstype="swap" --ondisk=nvme0n1 --size=2048
part / --fstype="xfs" --ondisk=nvme0n1 --grow --size=1
%post
%end
%packages
@^minimal
vim
%end
centos6 kickstart answer file
[root@wenzi ~]# vim /var/www/html/ks/6/centos6_ks.cfg
install
text
reboot
url --url=http://192.168.28.151/centos/6/
lang en_US.UTF-8
keyboard us
network --onboot yes --device eth0 --bootproto dhcp --noipv6
rootpw --iscrypted $6$CEK/siEJwPyzpudd$cJ/rz9oiqDmc8LmI1uZmI8BRFxje4OnuGS0qLBRZ4cAFWqKg482sPuN1BstCMY62NN58ivBzZ.bGD.0QFaYnf/
firewall --disabled
authconfig --enableshadow --passalgo=sha512
selinux --disabled
timezone Asia/Shanghai
bootloader --location=mbr --driveorder=nvme0n1 --append="crashkernel=auto rhgb quiet"
zerombr
clearpart --all --initlabel
part /boot --fstype="ext4" --ondisk=nvme0n1 --size=1024
part /swap --fstype="swap" --ondisk=nvme0n1 --size=2048
part / --fstype="ext4" --ondisk=nvme0n1 --grow --size=1
%packages
@core
@server-policy
@workstation-policy
vim
%end
%post
%end
Visit 192.168.28.151/ks, whether you can view the content of each kickstart file
Prepare PXE boot related files
Create the kernel related files of centos6, centos7 and centos8
[root@wenzi ~]# mkdir /var/lib/tftpboot/centos{6,7,8}
[root@wenzi ~]# tree /var/lib/tftpboot/
/var/lib/tftpboot/
├── centos6
├── centos7
└── centos8
[root@wenzi ~]# cp /var/www/html/centos/6/isolinux/{vmlinuz,initrd.img} /var/lib/tftpboot/centos6/
[root@wenzi ~]# cp /var/www/html/centos/7/isolinux/{vmlinuz,initrd.img} /var/lib/tftpboot/centos7/
[root@wenzi ~]# cp /var/www/html/centos/8/isolinux/{vmlinuz,initrd.img} /var/lib/tftpboot/centos8/
[root@wenzi ~]# cp /usr/share/syslinux/{pxelinux.0,menu.c32} /var/lib/tftpboot/
#centos8需要额外三个文件,centos6/7不需要
[root@wenzi ~]# cp /var/www/html/centos/8/isolinux/{ldlinux.c32,libcom32.c32,libutil.c32} /var/lib/tftpboot/
[root@wenzi ~]# mkdir /var/lib/tftpboot/pxelinux.cfg
[root@wenzi ~]# cp /var/www/html/centos/8/isolinux/isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default
[root@wenzi ~]# tree /var/lib/tftpboot/
/var/lib/tftpboot/
├── centos6
│ ├── initrd.img
│ └── vmlinuz
├── centos7
│ ├── initrd.img
│ └── vmlinuz
├── centos8
│ ├── initrd.img
│ └── vmlinuz
├── ldlinux.c32
├── libcom32.c32
├── libutil.c32
├── menu.c32
├── pxelinux.0
└── pxelinux.cfg
└── default
4 directories, 12 files
Prepare boot menu file
[root@wenzi ~]# vim /var/lib/tftpboot/pxelinux.cfg/default
#菜单样式
default menu.c32
#等待选择时间
timeout 600
#菜单名字
menu title WenZi Install System Menu
label linux8
#符号^ 可指定快捷键,在选择见面按 8 即可选中该项
menu label Auto Install CentOS Linux ^8
kernel centos8/vmlinuz
append initrd=centos8/initrd.img ks=http://192.168.28.151/ks/8/centos8_ks.cfg
label linux7
menu label Auto Install CentOS Linux ^7
kernel centos7/vmlinuz
append initrd=centos7/initrd.img ks=http://192.168.28.151/ks/7/centos7_ks.cfg
label linux6
menu label Auto Install CentOS Linux ^6
kernel centos6/vmlinuz
append initrd=centos6/initrd.img ks=http://192.168.28.151/ks/6/centos6_ks.cfg
#手动安装
label manual
menu label ^Manual Install CentOS Linux 8
kernel centos8/vmlinuz
append initrd=centos8/initrd.img inst.repo=http://192.168.28.151/centos/8/
label rescue
menu label ^Rescue a CentOS Linux system 8
kernel centos8/vmlinuz
append initrd=centos8/initrd.img inst.repo=http://192.168.28.151/centos/8/ rescue quiet
label local
menu default
menu label Boot from ^local drive
localboot 0xffff
test
Create a new virtual machine, open it directly without specifying the ISO file
Select and press Enter. test all
Step on the pit
It prompts that the nvme0n1 hard disk cannot be found, that is, the nvme0n1 specified in the kickstart file is invalid
solve:
Check whether the hard disk type (IDE, SCSI, SATA, NVMe) in the newly created virtual machine used for the test is consistent with the hard disk type specified in kickstart, just modify it to be consistent