PCsuggest

  • Quick tip
  • SECURITY
  • NETWORKING
  • OpenWrt
  • HARDWARE

How to setup an OpenWrt PXE boot server

Updated - September 25, 2018 by Arnab Satapathi

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.

Contents

  • 1. Requirements
  • 2. Installing necessary packages
  • 3. Partitioning the external USB storage device
  • 4. Copy installation files to USB drive
  • 5. Download the syslinux bootloader and copy PXE related files
  • 6. Mount the USB drive on the router
  • 7. Configure dnsmasq as a tftp server
  • 8. Configure the NFS server
  • 9. Creating the pxelinux configuration file
  • 10. Testing the PXE network boot
  • Conclusion

1. Requirements

  1. A router with an USB port and running OpenWrt
  2. A PC with PXE network boot support or virtual machine
  3. 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.

amazon prime logo
Try AmazonPrime for free
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.

Format the USB drive with your favorite utility like Gparted, fdisk or cfdisk, this step is done on a PC running Debian testing.openwrt pxe boot server USB drive partitionChane the /dev/sdd according to your's, like /dev/sdb etc.

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.

5. Download the syslinux bootloader and copy PXE related files

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

mkdir /mnt/sda2

Configure the /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

/etc/init.d/fstab start

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

/etc/init.d/dnsmasq restart

8. Configure the NFS server

Edit the /etc/exports file to setup the /mnt/sda2/PXEboot/ folder as NFS export directory, it should look like bellow.

/mnt/sda2/PXEboot/ *(no_subtree_check,ro,all_squash,insecure,async)

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.openwrt pxe boot server on virtualboxThis 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.

Conclusion

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.

Filed Under: featured, networking, openwrt Tagged With: ethernet cable, openwrt, PXE, pxe server

Your comments
  1. miguipda says

    December 12, 2015

    Hi,

    in regard of your proposal I still have one question :
    just after KERNEL and INITRD you start the path with "tails". I presume it is due to the fact you finished the used path in the APPEND with tails. And I suppose the KERNEL and INITRD will then start from this APPEND (tails folder) as source for the vmlinuz and initrd.
    Am I right ?

    By considering this aspect it means that I must change the place where I put my iso files.
    As said I put all my iso files (Tails, Slitaz and others) in /mnt/sda1/PXEMultiBoot/iso

    Could I then keep it and just simply begin the KERNEL and INITRD with iso in place of tails ?
    Am I right ?

    Sincerely thanks.

    Reply
    • Arnab says

      December 12, 2015

      I think you know the fact that the kernel and initrd are served to the client over tftp, not from the NFS server. The NFS server serves the root filesystem.

      The client machine could get kernel and initrd from the tftp server without mentioning the IP address. Why the lines after KERNEL and INITRD starts with a tails before them ? The /mnt/sda1/PXEMultiBoot/ is the tftproot directory and we have to show pxelinux the proper path to load the kernel and initrd.

      But for the NFS server, we have to show the proper full path with IP address to function it correctly.

      I think you also know that the ISO file could not be used directly without unpacking(or loop mounting) it somewhere.

      So it concludes to, just make sure everything is in right place, i.e. set proper paths in pxelinux configuration file and keep experimenting.

      I recommend you reading this things bellow:
      http://www.syslinux.org/wiki/index.php/PXELINUX
      https://en.wikipedia.org/wiki/Preboot_Execution_Environment
      https://en.wikipedia.org/wiki/Trivial_File_Transfer_Protocol
      https://en.wikipedia.org/wiki/Network_File_System

      http://www.tldp.org/HOWTO/Remote-Boot-4.html
      http://frank.harvard.edu/~coldwell/diskless/

      Hope this will help you to understand better.

      Reply
  2. miguipda says

    December 11, 2015

    Hi,

    vmlinuz is located at : /lib/live/mount/medium/live
    There is also a vmlinuz2 at the same place

    There is no initrd.gz
    But well a initrd.img located at : /lib/live/mount/medium/live
    There is also a initrd2.img at the same place.

    The server is simply at 10.0.1.1 and the ISO are located at :
    /mnt/sda1/PXEMultiBoot/iso

    Sincerely thanks for your idea to first test this secure distribution.

    Just for information I first saw your multiboot PXE then this first PCE explanation.
    I began to write a bash script to automatically install all needed packages and parameters you explained. I still need to read it back to check it with the default installation existing on the given OpenWRT SD Card on my router.
    I remember I will need some little explanation to finally have a functional script.
    I will test it line by line to see if it works. If well I will send this script to you. You could then add it to your perfect tutorial page.

    As my router is a really perfect device to run this kind of PXE use I recommended to you to use the secure Tails distribution.
    And after that I will recommend and advice you about the same PXE Configuration file for Slitaz. Because even if it is not a secure distribution it is a really fast and light distribution with specifications : http://www.slitaz.org/en/about/
    Root filesystem taking up about 100 MB and ISO image of less than 40 MB.
    Ready to use Web/FTP server powered by Busybox with CGI support.
    Browse the Web with Midori, Firefox or Lynx in text mode.
    Sound support provided by Alsa mixer, audio player and CD ripper/encoder.
    Chat, mail and FTP clients.
    SSH client and server powered by Dropbear.
    Database engine with SQLite.
    Generate a LiveUSB device.
    ...

    We stay in contact to enhance your really perfect tutorial.

    Reply
    • Arnab says

      December 12, 2015

      Literally initrd.gz, initrd.img, initrd.lz, initramfs.img are same thing, gzip or lzma compressed cpio archive.

      So we have to try with the btoth combo, i.e. vmlinuz+initrd.img and vmlinuz2+initrd2.img .

      Assuming the tails ISO is loop mounted or extracted in the /mnt/sda1/PXEMultiBoot/tails folder, look at the sample pxelinux configuration for Tails.


      label Tails_PXE_1
      KERNEL tails/lib/live/mount/medium/live/vmlinuz
      INITRD tails/lib/live/mount/medium/live/initrd.img
      APPEND netboot=nfs nfsroot=10.0.1.1:/mnt/sda1/PXEMultiBoot/tails priority=low --


      label Tails_PXE_2
      KERNEL tails/lib/live/mount/medium/live/vmlinuz2
      INITRD tails/lib/live/mount/medium/live/initrd2.img
      APPEND netboot=nfs nfsroot=10.0.1.1:/mnt/sda1/PXEMultiBoot/tails priority=low --

      It may work or may not be, please tell me when you have a working pxelinux configuration for Tails.

      I'm waiting with excitement for your shell script to automate the whole thing. I'll soon download the Tails ISO file from somewhere, to use it myself.

      As of Slitaz, once I had it, about one year ago, before my hard drive crashed. It's really great, almost everything for a normal user with a super fast openbox based desktop inside the tiny ISO file. I liked it's simple CGI based configuration editor and the taz browser. Thanks for the advice, I'll add slitaz pxelinux configuration too very soon.

      Have a good day 🙂

      Reply
  3. miguipda says

    December 11, 2015

    Hi,

    I really want to deploy this use on my "speedy" router (banana pi R1 - 1 Gb RAM - 1 Gb LAN - internal SATA HDD 😉 ).

    And for this reason I ask you if you could help us to know what must looks like the PXELinux configuration file (as you told us a sample with LUbuntu) but for a secure distribution that works perfectly. I want to talk about Tails : https://en.wikipedia.org/wiki/Tails_(operating_system)

    It could be great to be able to use it in PXE because it is a light distribution based on Debian and it works great.

    Sincerely thanks and have a nice day.

    Miguipda 😉

    Reply
    • Arnab says

      December 11, 2015

      Your hardware is really great for this purpose.

      I don't have the Tails installer CD/DVD ISO file and currently living with a very costly internet, so I can't download it right now. As Tails is also Debian based and it is a live system, so it could be done with the same procedure as in Lubuntu.

      We have to find out the kernel and initrd location and proper NFS server IP address then modify the pxelinux configuration.

      Could you post the syslinux/isolinux related configuration files(the isolinux.cfg, txt.cfg and gtk.cfg) somewhere ? i.e. in pastebin or here ?

      If it is a purely Debian based system, here a sample configuration, please check it if it works.


      label Tails_over_PXE
      KERNEL tails/install.amd/vmlinuz
      INITRD tails/install.amd/initrd.gz
      APPEND netboot=nfs nfsroot=192.168.1.1:/mnt/some_where/PXEboot/tails priority=low vga=788 ---

      Don't forget to edit this configuration according to your system, assuming the Tails ISO file is extracted in /mnt/some_where/PXEboot/tails

      I really appreciate your comment, thank you.

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Copyright © PCsuggest.com · All rights reserved.

  • Home
  • About
  • Contact
  • Privacy Policy
  • Sitemap