PXE Server can be a great thing for lots of purposes.
The main usages:
- Remote OS installation
- Boot diskless machine (ESXi can do it with autodeploy and stateless ESXi)
Background
Before we begin, it might be better to be familiar with PXE buzzwords/terms and architecture.
Bootp – an old protocol to get an IP. it was used in the past to boot from the network. surpressed by DHCP. seems like it also implemented some PXE/TFTP features.
DHCP – service which
Tftp – simple ftp service without need of any authentication. this is the first service the PXE gets the first boot file. it works on port 69 udp.
xinetd – a deamon which implement tftp service.
pxelinux.0 – this is a basic boot file. the first file we are going to load from the TFTP service. it’s a bootstrap.
vsftpd – ftp service for linux. we use it to download installation files after pxelinux starts installation.
httpd – apache http service for linux. can be used instead of vsftp
How does it really works?
Client
- Your NIC must support PXE boot (firmware with PXE client code). PXE boot option must be enabled in BIOS.
- PXE Does not support wireless!
- By PXE client code it asked from DHCP an IP address.
- then it gets an IP back with another property – “filename” which is what the client needs to ask from TFTP.
- another optional parameter from DHCP server is “next-server” which is needed if the PXE service is not installed in the same server as the DHCP.
- Boot loader (the filename) is pooled from the network into the RAM.
- From here the image itself take control, just like you mounting a disc or usb key and boot from it. It can do anything.
pxelinux.0 publish boot menu. after you choose an option it will call other files from TFTP – vmlinuz & initrd or memtest.
pxelinux.0 also configured to take installation from specific URL (the “method” parameter) and to take kickstart file (the “ks” parameter).
Server
PXE Service.
/var/lib/tftpboot/ – folder to keep all images.
Common utilities for PXE
How to install PXE server?
Basic PXE server on CentOS
Prerequisites
- Install scratch CentOS (latest CentOS 6 recommended – update with “yum update”).
- Configure 2xNICs – one public (with internet access) and one internal (to serve as a PXE with DCHP).
- configure /etc/sysconfig/network-scripts/ifcfg-eth0 to get an IP from DHCP (your network DHCP).
- configure anew nic – eth1. install “yum install system-config-network-tui”.
- run system-config-network-tui
- select <New device> and press enter
- Device type: Ethernet
- Name: eth1
- Device:eth1
- Static IP: 192.168.2.3
- Netmask: 255.255.255.0
- Press OK
- Choose Save
- Choose Save&Quit
- now type “ifup eth1”
- validate with running “ifconfig”
- mount an CentOS installation to the machine (by copying first an ISO to the machine or connect ISO to virtual machine. mountpoint: /media/cdrom (first create folder: “mkdir -p /media/cdrom”)
- create new folder for installation files “mkdir /instfiles”.
- create new folder for distributions: “mkdir -p /instfiles/distro”.
- create new folder for your linux installation distribution: “mkdir -p /instfiles/distro/centos”
- create new folder for your linux installation arch: “mkdir -p /instfiles/distro/centos/x86_64”
- create new folder for your linux installation version: “mkdir -p /instfiles/distro/centos/x86_64/6.5”
- copy or mount this installation to the final folder (“cp -R /media/cdrom /instfiles/distro/centos/x86_64/6.5” or “mount /dev/cdrom /instfiles/distro/centos/x86_64/6.5”
- Disable firewall and SELinux for simplicity:
- chkconfig iptables off
- service iptables stop
- sed -i “/^SELINUX=/c\dSELINUX=disabled” /etc/selinux/config
DHCP Service
#Install DCHP server for PXE ROM to get an IP
yum install dhcp -y
1 2 |
#First - we're configuring the service to listen to eth1 only. sed -i "/DHCPDARGS/cDHCPDARGS=eth1" /etc/sysconfig/dhcpd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#Configuring dhcp scope in order to enable clients to get an IP. dhcpdfile="/etc/dhcp/dhcpd.conf touch $dhcpdfile echo "default-lease-time 600;" >> $dhcpdfile echo "max-lease-time 7200;" >> $dhcpdfile # allowing to boot PXE from DHCP. allowed by default but it's only for being explicit. echo "" >> $dhcpdfile echo "allow booting;" >> $dhcpdfile # not really necessary. does not use bootp to get configuration. #echo "allow bootp;" >> $dhcpdfile #echo "option option-128 code 128 = string;" >> $dhcpdfile echo "authoritative;" >> $dhcpdfile echo "" >> $dhcpdfile echo "subnet 192.168.2.0 netmask 255.255.255.0 {" >> $dhcpdfile echo -e "\trange 192.168.1.200 192.168.1.210;" >> $dhcpdfile # you can mention which server is the PXE but if it's the current server you don't need this declaration. #echo "next-server 192.168.1.10;" >> $dhcpdfile |
1 2 |
#Telling TFTP to take this bootstrap. echo -e '\tfilename "pxelinux.0";' >> $dhcpdfile |
1 |
echo '}' >> $dhcpdfile |
1 2 3 4 |
#apply dhcp server configuration service dhcpd restart chkconfig dhcpd --level 345 on Test the DCHP with booting your empty client. if it's working, let's do the next step. |
TFTP Service + boot loader example.
#For PXE ROM to pull the first image file.
yum install tftp-server -y
#allowing tftp server (can be a little bit more formatted by sed – the first tab at the line is not working…)
sed -i “/disable = /c\disable\t\t = no” /etc/xinetd.d/tftp
1 2 |
#Install the basic bootstrap which will run our specific distro installation. yum install syslinux -y |
1 2 |
#Copying the basic bootstrap - TFTP client will pull this file cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/ |
1 |
#creating folders to copy vmlinuz and initrd.img files to enable booting to installation. |
1 2 3 4 5 6 7 8 |
mkdir -p /var/lib/tftpboot/images/CentOS_x86_64_6.5 #copying basic boot files for your distribution: cp /instfiles/distro/centos/x86_64/6.5/images/pxeboot/vmlinuz /var/lib/tftpboot/images/CentOS_x86_64_6.5/ cp /instfiles/distro/centos/x86_64/6.5/images/pxeboot/initrd.img /var/lib/tftpboot/images/CentOS_x86_64_6.5/ #IT'S REALLY IMPORTANT WHICH initrd FILE YOU'RE COPYING! IT MUST BE ONE WHICH SUPPORT NETWORK #for example, ubuntu cdrom contains two initrd.gz files: /install/initrd.gz (without network support) #and /install/netboot/ubuntu-installer/amd64/initrd.gz (with network support). |
1 2 |
#also copying memtest to boot this test cp /instfiles/distro/centos/x86_64/6.5/isolinux/memtest /var/lib/tftpboot/images/CentOS_x86_64_6.5/ |
1 2 3 |
#Copying configuration for pxelinux so we will have a menu after the bootstrap. mkdir -p /var/lib/tftpboot/pxelinux.cfg cp /instfiles/distro/centos/x86_64/6.5/isolinux/isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default |
1 2 |
#Copying supporting files for distro installation cp /instfiles/distro/centos/x86_64/6.5/isolinux/vesamenu.c32 /var/lib/tftpboot |
1 |
cp /instfiles/distro/centos/x86_64/6.5/isolinux/splash.jpg /var/lib/tftpboot |
1 2 |
#change pxelinux configuration to point to the right binary files sed -i "s/kernel memtest/kernel \/images\/CentOS_x86_64_6.5\/memtest/" /var/lib/tftpboot/pxelinux.cfg/default |
1 |
sed -i "s/kernel vmlinuz/kernel \/images\/CentOS_x86_64_6.5\/vmlinuz/" /var/lib/tftpboot/pxelinux.cfg/default |
1 |
sed -i "s/initrd=initrd.img/initrd=\/images\/CentOS_x86_64_6.5\/initrd.img/" /var/lib/tftpboot/pxelinux.cfg/default |
1 2 |
#Starting tftp service service xinetd restart |
#Now boot again the client and check if it gets the menu.
#Congratulations! you’ve managed to boot from PXE!
#Now it’s time to make your PXE boot installing the operating system to your client. for that we need ftp or http service to download installation files. we need service to download from and kickstart file to tell the installation from where to take the installation files.
Method 1: FTP Service for getting the installation
1 |
yum install vsftpd -y |
1 2 |
mkdir -p /tftpboot/images/centos/x86_64/6.5 . echo "anon_root=/tftpboot/images/centos/x86_64/6.5" >> /etc/vsftpd/vsftpd.conf |
echo “local_root=/tftpboot/images/centos/x86_64/6.5” >> /etc/vsftpd/vsftpd.conf
#updating pxelinux to take the installation from this ftp service.
sed -i “/append/c\append initrd=initrd.img method=ftp://192.168.2.3/images/centos/x86_64/6.5 ks=ftp://192.168.2.3/kickstart/anaconda-ks.cfg” /var/lib/tftpboot/
service vsftpd start
ftp client
yum install ftp -y
Method 2: HTTP service to get installation files (Prefered)
yum install httpd -y
#configure apache httpd to point our images directory: (cobbler works with httpd and not with vsftpd).
touch /etc/httpd/conf.d/instfiles.conf
echo ‘AliasMatch ^/instfiles(.*)?$ “/instfiles$1” ‘ >> /etc/httpd/conf.d/instfiles.conf
echo “” >> /etc/httpd/conf.d/instfiles.conf
echo ‘<Directory “/instfiles”>’ >> /etc/httpd/conf.d/instfiles.conf
echo -e “\tOptions Indexes FollowSymLinks” >> /etc/httpd/conf.d/instfiles.conf
echo -e “\tOrder allow,deny” >> /etc/httpd/conf.d/instfiles.conf
echo -e “\tAllow from all” >> /etc/httpd/conf.d/instfiles.conf
echo -e “</Directory>” >> /etc/httpd/conf.d/instfiles.conf
chkconfig httpd –level 3 on
service httpd start
sed -i “/append/c\append initrd=initrd.img method=http://192.168.2.3/instfiles/centos/x86_64/6.5 ks=ftp://192.168.2.3/kickstart/anaconda-ks.cfg” /var/lib/tftpboot/pxelinux.cfg/default
#Now check if you can surf to the server: http://192.168.2.3/instfiles
#Congratulations! now you can direct download files from your http service.
Last configuration to make it work
Making anaconda kickstart configuration file
under /instfiles create a new folder – kickstart.
mkdir -p /instfiles/kickstart
the file you need to put in it will be called anaconda-ks.cfg .
download example file:
cd /instfiles/kickstart
#install wget
yum install wget -y
wget http://www.cs.ndsu.nodak.edu/~hennebry/anaconda-ks.cfg
#Update boot menu to also take kickstart file to the installation
#add to append line at the end: ks=http://192.168.2.3/instfiles/kickstart/anaconda-ks.cfg” check how to do it with sed.
Testing the whole process
Summary
You’ve managed to install PXE server to install operating system automatically. you need only choose your option from the menu (this one can be non-interactive but we’ll do it in the next part).
This is the basics, and also the basics for Cobbler, which make automation for configuring PXE.
How to install and configure cobbler? in the next part!
Just to remember, the important files and folders are:
- /etc/sysconfig/dhcpd – main configuration for dhcp service, which ethernet interfaces to listen to.
- /etc/dhcp/dhcpd.conf – your dhcp service configuration and scopes.
- /etc/xinetd.d/tftp – your tftp service configuration, from where to pull files and to enable it.
- /var/lib/tftpboot – main tftp files, from which the client can pull files. this is the root directory (“/”) seen for tftp clients.
- /var/lib/tftpboot/pxelinux.0 – first bootstrap loading by our client.
- /var/lib/tftpboot/pxelinux.cfg/default – pxelinux configuration file. what to do and menu configurations.
- /var/lib/tftpboot/images – distributions boot files, include vmlinuz and initrd.img which loads the installation program.
- /etc/httpd/conf.d – directory with http virtual directories configuration. it means that if you want to link http url to your local directory, you do it by configuring x.conf inside this directory, as x is your destination directory.
by the way, in order to take the installation files (the method parameter), you need to put a line in the kickstart file (right after “install” line:
url –url ftp://192.168.2.3/images/centos/x86_64/6.5/
Basic PXE server on Ubuntu
Packages we’re going to install
tftpd-hpa –
inetutils-inetd –
First, install Ubuntu server (LTS version recommended, I used 12.04.4 but I guess 14.04 can be also great).
The specs can be 1vCPU, 256MB memory and 8GB storage.
Just install the default installation.
Don’t forget to update all packages (sudo apt-get update && sudo apt-get upgrade).
Next,
Videos:
- https://www.youtube.com/watch?v=zpzPuK6LNQ4 – Basic understanding.
- http://pxe.dev.aboveaverageurl.com/index.php/PXE_Booting – a bit more about the process.
- https://www.youtube.com/watch?v=B-QpCYKrVew – cobbler installation
How to install:
https://help.ubuntu.com/community/PXEInstallServer