Have a router with OpenWrt installed ? Why not use it for something else ? As a OpenWrt PXE server for booting your favorite Linux/UNIX distro.
Possibilities with a PXE network boot server is huge,
- Boot a live CD in one or many PC over network
- Installing a new system over network
- Booting a recovery or utility live CD like Knoppix, CloneZilla etc.
- Configuring disk-less computer clusters etc. etc.
The advantage of using a router as PXE server is far more cheaper than a PC with several NIC, run it 24x7 and low power consumption.
I have seen that the cheapest routers with 32MB RAM works as good as a PC with 4GB of RAM, so lets do it.
ContentsShow / Hide
- A router with an USB port and running OpenWrt
- A PC with PXE network boot support or virtual machine
- Ethernet cable for connecting and little bit of patience
Router's USB is port is must for storing the installation files and installing extra software like NFS server. A PC or virtual machine is for testing purpose.
An working extroot setup is necessary for routers with 4MB NOR flash storage, have a look on this detailed guide to setup extroot on openwrt router.
2. Installing necessary packages
Connect the router to internet via the WAN ethernet port and make sure it could reach the internet
ping -c 4 google.com
Install the packages for USB storage and ext4 filesystem support
opkg update opkg install kmod-usb-storage kmod-scsi-core opkg install block-mount kmod-fs-ext4
Install the NFS server
opkg install nfs-kernel-server
An extroot setup may be necessary for your router depending on the amount of free NOR flash space to install the packages and it's dependencies.
Enjoy free shipping and One-Day delivery, cancel any time.
3. Partitioning the external USB storage device
You could use a USB pendrive or USB external HDD, I'm using a 4GB USB pendrive for this purpose. There are two ext4 partitions, one 200MB partition for OpenWrt extroot setup and another partition as storage.
4. Copy installation files to USB drive
Assuming you already have some Ubuntu, Debian, Fedora etc. installer ISO file in your home folder. Now create mount points
sudo mkdir /mnt/iso sodo mkdir /mnt/data
Mount the USB drive and the ISO file
sudo mount /dev/sdd2 /mnt/data/ sudo mount lubuntu-14.04.1-desktop-i386.iso /mnt/iso/
Copy the installation media data
sodo mkdir -p /mnt/data/PXEboot/lubuntu sodo cp -r /mnt/iso/* /mnt/data/PXEboot/lubuntu
Wait some time to let the copying finished.
Download the latest syslinux and extract it
wget -c https://www.kernel.org/pub/linux/utils/boot/syslinux/syslinux-6.03.tar.xz tar -xf syslinux-6.03.tar.xz
Copy the necessary files for PXE booting
cd syslinux-6.03/ sudo cp ./bios/core/pxelinux.0 /mnt/data/PXEboot sudo cp ./bios/com32/elflink/ldlinux/ldlinux.c32 /mnt/data/PXEboot sudo cp ./bios/com32/lib/libcom32.c32 /mnt/data/PXEboot sudo cp ./bios/com32/libutil/libutil.c32 /mnt/data/PXEboot sudo cp ./bios/com32/menu/vesamenu.c32 /mnt/data/PXEboot
Unmount the USB drive and the ISO file
sync sudo umount /dev/sdd2 sudo umount /mnt/iso
Clean up the system
sudo rmdir /mnt/iso/ sudo rmdir /mnt/data/
Now disconnect the USB drive and connect it to the routers USB port reboot the router.
6. Mount the USB drive on the router
After rebooting the router, login to it with SSH and create the USB drive to a mountpoints. I'm using the first partition as extroot and second partition as data storage, so modify the commands bellow according to your
/etc/config/fstab to automount drives at startup
block detect > /etc/config/fstab /etc/init.d/fstab enable
Now edit the
/etc/config/fstab with vi to enable automount, look at my sample configuration bellow
config 'global' option anon_swap '0' option anon_mount '0' option auto_swap '1' option auto_mount '1' option delay_root '5' option check_fs '0' config 'mount' option target '/overlay' option uuid '0b805158-8288-43d8-9189-e84f14bfb978' option enabled '1' config 'mount' option target '/mnt/sda2' option uuid '1bd7f672-5b3b-4de1-866a-8a5ebf982908' option enabled '1'
Just make sure to to edit the option enabled line from 0 to 1 and start the fstab service
7. Configure dnsmasq as a tftp server
After rebooting the router, edit the config dnsmasq section of the /etc/config/dhcp file to enable dnsmasq tftp server. Add the following two lines at the top
option enable_tftp '1' option tftp_root '/mnt/sda2/PXEboot'
It should look like this
config dnsmasq option enable_tftp '1' option tftp_root '/mnt/sda2/PXEboot'
Also add these few lines at the end of the /etc/config/dhcp file
config boot linux option filename 'pxelinux.0' option serveraddress '192.168.1.1' option servername 'OpenWRT PXE SERVER'
And finally restart the dnsmasq server
8. Configure the NFS server
/etc/exports file to setup the
/mnt/sda2/PXEboot/ folder as NFS export directory, it should look like bellow.
Now enable the NFS server and portmap service at startup and start them
/etc/init.d/nfsd enable /etc/init.d/nfsd start
/etc/init.d/portmap enable /etc/init.d/portmap start
Fixing nfsd automatic startup: If nfsd failed to start at boot up then fix it, I'm not going into the details, just run this command bellow
mv /etc/rc.d/S20network /etc/rc.d/S14network
9. Creating the pxelinux configuration file
Create a file named default under the
/mnt/sda2/PXEboot/pxelinux.cfg/ folder, which contains the pxelinux configuration
mkdir -p /mnt/sda2/PXEboot/pxelinux.cfg/ vi /mnt/sda2/PXEboot/pxelinux.cfg/default
At this step the configuration file will be slightly different for each different Linux/UNIX distribution.Look at the sample configuration for booting lubuntu 14.04 32bit.
DEFAULT vesamenu.c32 MENU TITLE OpenWrt PXE Boot Menu PROMPT 0 TIMEOUT 40 label Lubuntu 32bit KERNEL /lubuntu/casper/vmlinuz INITRD /lubuntu/casper/initrd.lz APPEND netboot=nfs nfsroot=192.168.1.1:/mnt/sda2/PXEboot/lubuntu boot=casper quiet splash --
If you want to boot other distros you have to figure out an working pxelinux configuration yourself. But remember this is not hard at all, just put your pxelinux configurations at the /
mnt/sda2/PXEboot/pxelinux.cfg/default file and test them.
It is also possible to directly loop mount the ISO files and use them for PXE boot server without extracting, read more about it here > setup a multiboot OpenWrt OpenWrt PXE server .
10. Testing the PXE network boot
Now reboot your PC and enter the Boot device selection menu at BIOS or UEFI, it is usually done by pressing F12 , Del or the Esc key at the very early stage of power on.
Select the Network Boot and hit enter, within few seconds your PC should get an IP from the router and drop in a pxelinux boot menu.This testing also could be done with a virtual machine like VirtualBox or VMware.
For VirtulaBox add a Bridged network adapter from the Settings menu and select your ethernet interface like eth0, eth1, enp2s0 etc. And Change the Boot order to boot it from network.
I hope this simple tutorial will help you to setup your PXE boot server on your OpenWrt router.
Also don't forget to check the list of best ethernet cable I've made. You're going to need cables a lot with this experiment.
If you have any question,problem or suggestion just leave a comment.