Giuseppe Parrello

 

How to install a PXE server on a router


In this page I describe how to install a "PXE server" on a router (Asus RT-AC56U) - we use some Entware packages, so please refer to this page on how to install Entware on a router.
We will install the "DHCP server", the "Xinetd server" and the "TFTP server" using Entware packages.
Note : if your router already has a "DHCP server", you have to configure the existing "DHCP server", so read the section "How to configure an existing DHCP server" and discard the section "How to install the DHCP server".


How to configure an existing DHCP server

If your router already has a "DHCP server", you cannot install another "DHCP server", so you have to use your current "DHCP server" configuration. In this case you have to update the "DNSmasq" configuration, configuration file is named "dnsmasq.conf", and usually it is located in "/etc" folder. Sometimes your router provides a way to update the configuration of file "dnsmasq.conf", in both cases you have to add the following lines to file "dnsmasq.conf":

dhcp-boot=pxelinux.0
pxe-service=x86PC, "Install Linux", pxelinux
enable-tftp
tftp-root=/opt/tftpboot

Please refer to this page to have a sample "dnsmasq.conf" file and please refer to this page for further details about "dnsmasq.conf" configuration file. Briefly, in above-mentioned example, we:

In this page we will use folder "/opt/tftpboot" as root of the TFTP server, so in above-mentioned example we use it to tell "DNSmasq" server where is the root folder of the TFTP server. Furthermore in above-mentioned root folder of the TFTP server we will have the file "pxelinux.0" used to boot the PXE service.

Please skip the next section "How to install the DHCP server" and go to the section "How to install Xinetd and TFTP servers".


How to install the DHCP server

The DHCP server must be installed only if your router lacks one, this is to avoid any kind of conflicts.
In order to install the "DHCP server" with Entware packages, we have to execute the following line:
opkg install isc-dhcp-server-ipv6
After this, make a backup of file "/opt/etc/dhcpd.conf", and create a new file "/opt/etc/dhcpd.conf":

# dhcpd.conf

allow booting;
allow bootp;

#Definition of the first subnet
subnet 192.168.1.0 netmask 255.255.255.0 {
range 192.168.1.25 192.168.1.50;

#Lease time in seconds - 24 hours
default-lease-time 86400;
max-lease-time 86400;

option domain-name-servers 192.168.1.1;
option broadcast-address 192.168.1.255;
option subnet-mask 255.255.255.0;

#Gateway
option routers 192.168.1.1;
}

log-facility local7;

# tftp-Server Settings for PXE
next-server 192.168.1.4;
filename "pxelinux.0";

host myhostname { hardware ethernet 11:22:33:44:55:66; fixed-address 192.168.1.150; }

Please refer to this page for further details about configuration file "dhcpd.conf".
In the above-mentioned example, the IP address "192.168.1.1" is the address of the main router, while IP address "192.168.1.4" is the second router where we install the "DHCP server", the "Xinetd server" and the "TFTP server".
We also need to create a new file "/opt/etc/init.d/S56dhcp" that we will use to start/stop the DHCP daemon:

#!/bin/sh

ENABLED=yes
PROCS=dhcpd
ARGS="-lf /tmp/dhcpd.leases"
PREARGS=""
PRECMD="touch /tmp/dhcpd.leases"
POSTCMD=""
DESC=$PROCS
PATH=/opt/sbin:/opt/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

. /opt/etc/init.d/rc.func

In the above-mentioned file, the parameter "PRECMD" is used to execute command line "touch /tmp/dhcpd.leases" before starting the DHCP daemon. In this way we are sure that file "/tmp/dhcpd.leases" is always created before starting the DHCP daemon.
We run script "/opt/etc/init.d/S56dhcp start" in order to start DHCP daemon. This script must be executed everytime the router is restarted.


How to install Xinetd and TFTP servers

In order to install the "Xinetd" and "TFTP" servers with Entware packages, we have to execute the following line:
opkg install tftpd-hpa xinetd
After this, make a backup of file "/opt/etc/xinetd.conf", and change the existing file "/opt/etc/xinetd.conf" in order to have:

defaults
{
    only_from = localhost 10.0.0.0/8 172.16.0.0/12 192.168.1.0/16
    instances = 60
    log_type = SYSLOG authpriv info
    log_on_success = HOST PID
    log_on_failure = HOST
    cps = 25 30
}

includedir /opt/etc/xinetd.d

Please refer to this page for further details about configuration file "xinetd.conf".
We also need to create a new file "/opt/etc/xinetd.d/tftp" that Xinetd will use to start the TFTP daemon (we don't need to manually start the TFTP daemon):

service tftp
{
    flags = IPv4
    socket_type = dgram
    protocol = udp
    instances = 30
    wait = yes
    user = admin
    server = /opt/sbin/tftpd-hpa
    server_args = -s /opt/tftpboot
    cps = 100 2
    log_on_success = HOST PID
    log_on_failure = HOST
    disable = no
    per_source = 11
}

Please refer to this page for further details about additional services of "xinetd server".
We run script "/opt/etc/init.d/S10xinetd start" in order to start Xinetd daemon. This script must be executed everytime the router is restarted.


How to setup the root folder of TFTP server

Before starting to test the "PXE service", we need to setup the root folder of the "TFTP server".
Firstly we create the root folder using the following command line:
mkdir /opt/tftpboot
Due to the fact that the setup of the root folder of the "TFTP server" is not simple, we download a compressed file from "Debian Linux" website and we extract all its files into root folder "/opt/tftpboot".
In above-mentioned root folder we have 3 important items, "ldlinux.c32", "pxelinux.0" and "pxelinux.cfg". The first two are mandatory files for the PXE service and cannot be changed or deleted, the last one is a configuration folder containing a file called "default" that may be changed. The content of sample file "pxelinux.cfg/default" is:

# D-I config version 2.0
# search path for the c32 support libraries (libcom32, libutil etc.)
path debian-installer/amd64/boot-screens/
include debian-installer/amd64/boot-screens/menu.cfg
default debian-installer/amd64/boot-screens/vesamenu.c32
prompt 0
timeout 0

First parameter "path" is used as base directory for the "PXE service", the "include" parameter is used to include a configuration file to be used to create the main menu of the "PXE service", while "default" parameter is used to show the graphical menu of the "PXE service". This is the sample content of the "menu.cfg" file:

menu hshift 7
menu width 61

menu title Debian GNU/Linux installer boot menu
include debian-installer/amd64/boot-screens/stdmenu.cfg
include debian-installer/amd64/boot-screens/txt.cfg
include debian-installer/amd64/boot-screens/amdtxt.cfg
include debian-installer/amd64/boot-screens/gtk.cfg
include debian-installer/amd64/boot-screens/amdgtk.cfg
menu begin advanced
    menu label ^Advanced options
        menu title Advanced options
        include debian-installer/amd64/boot-screens/stdmenu.cfg
        label mainmenu
             menu label ^Back..
             menu exit
        include debian-installer/amd64/boot-screens/adtxt.cfg
        include debian-installer/amd64/boot-screens/amdadtxt.cfg
        include debian-installer/amd64/boot-screens/adgtk.cfg
        include debian-installer/amd64/boot-screens/amdadgtk.cfg
menu end
label help
        menu label ^Help
        text help
            Display help screens; type 'menu' at boot prompt to return to this menu
        endtext
        config debian-installer/amd64/boot-screens/prompt.cfg
include debian-installer/amd64/boot-screens/spk.cfg
include debian-installer/amd64/boot-screens/amdspk.cfg
include debian-installer/amd64/boot-screens/spkgtk.cfg
include debian-installer/amd64/boot-screens/amdspgtk.cfg

Please refer to this page for further details about configuration files used to create menus and submenus for the "PXE server".


Examples of boot menus for the PXE server

An example of (sub)menu is the following one, this is used to execute the "GParted" Live-USB:

menu background bootmenu/splash_gparted.png

label GParted Live32
MENU LABEL GParted Live 32-bit
kernel bootmenu/gparted32/vmlinuz
append initrd=bootmenu/gparted32/initrd.img boot=live config components union=overlay username=user nosplash noprompt noswap noeject 
ip= vga=788 ethdevice-timeout=600 fetch=tftp://192.168.1.4/bootmenu/gparted32/filesystem.squashfs

label GParted Live64
MENU LABEL GParted Live 64-bit
kernel bootmenu/gparted64/vmlinuz
append initrd=bootmenu/gparted64/initrd.img boot=live config components union=overlay username=user nosplash noprompt noswap noeject 
ip= vga=788 ethdevice-timeout=600 fetch=tftp://192.168.1.4/bootmenu/gparted64/filesystem.squashfs

In above-mentioned example, the "filesystem.squashfs" file is loaded from the "TFTP server". Remember to include the correct IP address of the "TFTP server", in this case is "192.168.1.4". Note that these menus are similar to the menus used by Live-USB distributions (that include a "SysLinux" boot-loader), even the booting files "vmlinuz" and "initrd.img" are the same.
Another example of (sub)menu is suitable to run an ".ISO" image, for example a Live-CD. This is a particular option that must be tested case-by-case, not all .ISO images may be executed via the "PXE service". Usually only small .ISO images may be executed.
An example of .ISO images is the installations via network of the Linux distributions, they do not include all the Linux packages as they are downloaded from Internet.
An example of boot menu to execute an .ISO image is the following one:

menu background bootmenu/splash_opensuse.png

LABEL Opensuse install 32 bit
    text help
       Install OpenSuse 13.2 (32 bit) via Network Installer
    endtext
    MENU LABEL Opensuse 13.2 (NET Install 32 bit)
    LINUX bootmenu/memdisk
    INITRD bootmenu/linux/opensuse/openSUSE-13.2-NET-i586.iso
    APPEND iso vga=normal vmalloc=512M

LABEL Opensuse install 64 bit
    text help
       Install OpenSuse 13.2 (64 bit) via Network Installer
    endtext
    MENU LABEL Opensuse 13.2 (NET Install 64 bit)
    LINUX bootmenu/memdisk
    INITRD bootmenu/linux/opensuse/openSUSE-13.2-NET-x86_64.iso
    APPEND iso vga=normal vmalloc=512M

In above-mentioned example, the "PXE server" uses the "memdisk service" to execute the Network Installation of "openSUSE Linux" distribution.
To sum up, if we want to run a Live-CD/USB via "PXE server", we have two choices:

  1. the first one is to use the Live-USB files in order to have a kernel and a squashfs file (it will be downloaded via "TFTP server");
  2. the second one is to use the .ISO Live-CD file with the "memdisk service".

How to test the PXE server

To test the "PXE server" you need a computer that can boot from the network or eventually you can use a virtual machine installed on your computer. The virtual machine must be configured to boot from the network, removing all the other boot options.
During boot from a "PXE server", the network interface will try to search for a "DHCP server" and when found will ask to execute the main code of the "PXE server". It is important to have a fast network connection during all the phases of the booting process from a "PXE server", all the work is done via network.
I remind all of you that the boot process of the "PXE service" is similar to the boot process of "SysLinux service", last one is used to create menu-driven multi-boot pendrives. So the "PXE service" will directly use the usual "vmlinuz" and "initrd.img" booting files, while via "TFTP server", it will load the "squashfs" file. This last phase will be slow if the network speed is not fast enough.