Setup a PXE Server on Linux
This post shows how to setup a PXE server on Linux. In such a config clients can boot their OS over the network from the PXE server.
Table of Contents
Installation and Configuration
We will use Oracle Linux 8 as the OS of the PXE server. So the first step is to create a Oracle Linux 8.10 VM as described here. Then we will start with the installation and configuration of the PXE server:
# setup a NFS server that will export the OS iso image the clients will boot
dnf -y install nfs-utils
firewall-cmd --add-service nfs --permanent
mkdir -p /var/nfs-exports/iso/ol9
exportfs -i -o ro :/var/nfs-exports/iso
cat >> /etc/exports << EOF
/var/nfs-exports/iso *(ro)
EOF
exportfs -ra
systemctl enable --now nfs-server
# copy the iso file to the nfs export directory
cp /sw/OracleLinux-R9-U5-x86_64-dvd.iso /var/nfs-exports/iso/ol9
chmod o+r /var/nfs-exports/iso/ol9/*.iso
# install and configure dnsmasq that will provide dns, dhcp and tftp services
dnf -y install dnsmasq
cat >> /etc/dnsmasq.conf << EOF
dhcp-range=11.1.1.195,11.1.1.200,12h
dhcp-boot=pxelinux/pxelinux.0
log-queries
log-dhcp
enable-tftp
tftp-root=/var/lib/tftpboot
dhcp-host=08:00:27:ee:b2:ed,lin2,11.1.1.174,infinite
EOF
systemctl enable --now dnsmasq
dnf -y install syslinux
mkdir -p /var/lib/tftpboot/pxelinux/pxelinux.cfg
chown dnsmasq:dnsmasq /var/lib/tftpboot
cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/pxelinux
# copy the kernel and the ram disk
mount -o loop /var/nfs-exports/iso/ol9/OracleLinux-R9-U5-x86_64-dvd.iso /mnt
cp /mnt/images/pxeboot/vmlinuz /var/lib/tftpboot/pxelinux/vmlinuz_ol9
cp /mnt/images/pxeboot/initrd.img /var/lib/tftpboot/pxelinux/initrd_ol9.img
umount /mnt
cp /usr/share/syslinux/*.c32 /var/lib/tftpboot/pxelinux
cat > /var/lib/tftpboot/pxelinux/pxelinux.cfg/01-08-00-27-ee-b2-ed << EOF
DEFAULT menu.c32
TIMEOUT 30
ONTIMEOUT OL9
PROMPT 0
MENU INCLUDE pxelinux.cfg/pxe.conf
NOESCAPE 1
LABEL BootLocal
localboot 0
TEXT HELP
Boot to local hard disk
ENDTEXT
LABEL OL9
MENU LABEL OL9
kernel vmlinuz_ol9
append initrd=initrd_ol9.img inst.repo=nfs:11.1.1.172:/var/nfs-exports/iso/ol9 vga=836 vconsole.keymap=de
TEXT HELP
Install Oracle Linux 9
ENDTEXT
EOF
You probably need to adjust the IP and the MAC addresses to fit your environment. I used the following addreses in my environment:
- 11.1.1.195,11.1.1.200 is a range of IP addresses the DHCP server serves
- 11.1.1.172 is the IP address of the PXE boot/DHCP/NFS/tftp server
- 11.1.1.174 is the IP address of the PXE boot client
- 08-00-27-ee-b2-ed is the mac address of the PXE boot client (needs to be in lowercase, don’t forget to add the 01- in front of the configuration file name)
- lin2 is the hostname of the PXE boot client
A client (with the correct mac address) can now start a network boot and receive its IP address from the server. Then the client will start to boot the OS from the PXE/NFS server.
Creating a BIOS based VM
A new VirtualBox VM can be created as described in the section “Configure the Virtual Machine”. For the mac address enter the one you specified in the dnsmasq configuration. To start the network boot run these commands:
set "VHOST="
(set /p VHOST=Enter VM name ^(e.g. lin1^):
call vboxmanage modifyvm %VHOST% --boot1=net --boot2=disk
call vboxmanage startvm %VHOST%)
Adding support for UEFI based PXE clients
So far the configuration supports network boot of BIOS based PXE clients. In this section we will add support for UEFI based PXE clients. Run these commands on the PXE server:
# set the ip address of the tftp server
tftp_server=11.1.1.172
# we will use iPXE as the boot firmware
mkdir /var/lib/tftpboot/efi
wget -O /var/lib/tftpboot/efi/ipxe.efi http://boot.ipxe.org/ipxe.efi
# tag efi clients and set their boot firmware
cat >> /etc/dnsmasq.conf <<EOF
dhcp-match=set:efi-x86_64,option:client-arch,7
dhcp-boot=tag:efi-x86_64,efi/ipxe.efi
EOF
systemctl restart dnsmasq
# create a config file for iPXE to boot the kernel
# the ip address is from the tftp server
cat >> /var/lib/tftpboot/autoexec.ipxe <<EOF
#!ipxe
dhcp
kernel tftp://$tftp_server/efi/vmlinuz_ol9 inst.repo=nfs:11.1.1.195:/var/nfs-exports/iso/ol9 vga=836 vconsole.keymap=de
initrd tftp://$tftp_server/efi/initrd_ol9.img
boot
EOF
# copy the kernel and the ram image
mount -o loop /var/nfs-exports/iso/ol9/OracleLinux-R9-U5-x86_64-dvd.iso /mnt
cp /mnt/images/pxeboot/vmlinuz /var/lib/tftpboot/efi/vmlinuz_ol9
cp /mnt/images/pxeboot/initrd.img /var/lib/tftpboot/efi/initrd_ol9.img
umount /mnt
The IP address (11.1.1.172) needs to be adjusted to fit your environment. That is all to enable EFI clients to boot via network. Next we create a UEFI VM and boot from the PXE server
Creating a UEFI based VM
The basic steps to create a VM are described in this section. If the VM is not yet configured as a network client we need to add it to the dnsmasq configuration on the PXE server:
cat >>/etc/dnsmasq.conf <<EOF
dhcp-host=08:00:27:ee:a4:ed,11.1.1.177,lin5,infinite
EOF
systemctl restart dnsmasq
No we change the VM to EFI firmware and start the virtual machine from net. This needs to be done on the host system:
set "VHOST="
(set /p VHOST=Enter VM name ^(e.g. lin1^):
call vboxmanage modifyvm %VHOST% --boot1=net --boot2=disk --firmware=efi --nic-type1=virtio
call vboxmanage startvm %VHOST%)
Troubleshooting
Because we have log-queries and log-dhcp in the dnsmasq config file (/etc/dnsmasq.conf) a lot of helpful debug messages are written to /var/log/messages (tail -100f /var/log/messages
).
If you have another DHCP server in your network it is possibly the best to disable it (at least during the PXE boot).
Leave a Reply