Automating Operating System Deployment with PXE Network Booting
PXE (Preboot Execution Environment) enables computers to boot from a network server, facilitating automated, large-scale operating system installations without physical media. This method relies on a combination of DHCP, TFTP, and FTP services to deliver boot files and installation images to client machines.
Boot Methods for System Installation
- Local Disk Boot: Initiates from an existing operating system on a hard drive.
- External Media Boot: Uses optical drives or USB sticks to load installation media.
- Network Boot (PXE): Downloads boot images and installation files from a remote server over the network.
System Installation Sequence
- Boot Loader Initialization: Loads the bootloader to set up hardware and memory.
- Boot Menu Display: Presents installation options to the user.
- Kernel and Initrd Loading: Loads the Linux kernel and initial RAM disk for the installation environment.
- Root Filesystem Mounting: Prepares the root filesystem for the instaler.
- Anaconda Execution: Launches the graphical or text-based installation wizard.
PXE Overview
PXE is a network boot standard that allows systems to retrieve boot files via TFTP after obtaining an IP address from a DHCP server. Key components include:
- PXE-Enabled NIC: Network interface card with PXE firmware to initiate network booting.
- DHCP Server: Assigns IP addresses and directs clients to the TFTP server.
- TFTP Server: Hosts boot files like the PXE bootloader, kernel, and initrd.
Advantages of PXE:
- Scalability: Supports simultaneous installation on multiple systems.
- Automation: Reduces manual intervention during deployment.
- Remote Capability: Eliminates the need for physical installation media.
Prerequisites:
- Client hardware must support PXE booting.
- A DHCP server configured for PXE opsions.
- A TFTP server to serve boot files.
Kickstart for Unattended Installation
Kickstart automates the installation process by using a configuration file (ks.cfg) that specifies installation parameters, such as partitioning, software selection, and network settings. This file is fetched during the PXE boot to guide the installation without user input.
Setting Up a PXE Server
Environment Setup
- Server IP: 192.168.10.17
- Network: Use a dedicated subnet (e.g., 192.168.100.0/24) for PXE clients.
- Disable SELinux and firewall on the server.
Required Packages
Install the following on the PXE server:
yum install -y dhcp tftp-server syslinux xinetd vsftpd system-config-kickstart
DHCP Configuration
Edit /etc/dhcp/dhcpd.conf:
ddns-update-style none;
subnet 192.168.100.0 netmask 255.255.255.0 {
range 192.168.100.40 192.168.100.50;
option routers 192.168.100.100;
option domain-name-servers 114.114.114.114;
next-server 192.168.100.100;
filename "pxelinux.0";
}
Start and enable DHCP:
systemctl start dhcpd
systemctl enable dhcpd
TFTP Configuration
Configure /etc/xinetd.d/tftp:
service tftp
{
socket_type = dgram
protocol = udp
wait = no
user = root
server = /usr/sbin/in.tftpd
server_args = -s /var/lib/tftpboot
disable = no
flags = IPv4
}
Start services:
systemctl start xinetd tftp
systemctl enable xinetd tftp
Boot File Preparation
Copy necessary files to the TFTP directory:
cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/
mount /dev/cdrom /mnt
cp /mnt/images/pxeboot/vmlinuz /var/lib/tftpboot/
cp /mnt/images/pxeboot/initrd.img /var/lib/tftpboot/
Boot Menu Configurasion
Create /var/lib/tftpboot/pxelinux.cfg/default:
default auto
prompt 1
label auto
kernel vmlinuz
append initrd=initrd.img method=ftp://192.168.100.100/centos7
label text
kernel vmlinuz
append text initrd=initrd.img method=ftp://192.168.100.100/centos7
label rescue
kernel vmlinuz
append rescue initrd=initrd.img method=ftp://192.168.100.100/centos7
FTP Setup for Installation Media
Start and enable vsftpd:
systemctl start vsftpd
systemctl enable vsftpd
Place the CentOS installation files in /var/ftp/centos7.
Kickstart Configuration
Generate a ks.cfg file using system-config-kickstart or manually create it. Example content:
install
keyboard 'us'
rootpw --iscrypted $1$9Rn8W7vF$npbKh8CeIbTxjwHUlcW4n0
url --url="ftp://192.168.100.100/centos7"
lang en_US
auth --useshadow --passalgo=sha512
text
firstboot --disable
selinux --disabled
firewall --disabled
network --bootproto=dhcp --device=ens33
reboot
timezone Asia/Shanghai
bootloader --location=mbr
zerombr
clearpart --all --initlabel
part / --fstype="xfs" --grow --size=1
part swap --fstype="swap" --size=2048
part /boot --fstype="xfs" --size=300
%packages
@base
@^gnome-desktop-environment
%end
Copy the file to the FTP server:
cp ks.cfg /var/ftp/
Update the PXE boot menu to include the Kickstart file:
default auto
prompt 1
label auto
kernel vmlinuz
append initrd=initrd.img method=ftp://192.168.100.100/centos7 ks=ftp://192.168.100.100/ks.cfg
label text
kernel vmlinuz
append text initrd=initrd.img method=ftp://192.168.100.100/centos7 ks=ftp://192.168.100.100/ks.cfg
label rescue
kernel vmlinuz
append rescue initrd=initrd.img method=ftp://192.168.100.100/centos7 ks=ftp://192.168.100.100/ks.cfg
Post-Installation Scripts
Create a local YUM repository for the installed system:
mkdir -p /etc/yum.repos.d/backup
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup/
echo '[local]
name=Local Repository
baseurl=ftp://192.168.100.100/centos7
enabled=1
gpgcheck=0' > /etc/yum.repos.d/local.repo
NTP Configuration for Time Synchronization
Install and configure NTP to keep server time accurate:
yum install -y ntp ntpdate
Edit /etc/ntp.conf:
restrict 192.168.100.0 mask 255.255.255.0 nomodify notrap
server ntp1.aliyun.com
server ntp2.aliyun.com prefer
Start and enable NTP:
systemctl start ntpd
systemctl enable ntpd
ntpdate ntp1.aliyun.com
Check synchronization status:
ntpq -p
ntpstat