PXE boot CentOS from RPI

NOTE: THIS METHOD DOES NOT CONSIDER NFS INSTALLATION OR NFS BOOT. YOU NEED A 512MB > GREATER CAPACITY USB DRIVE AND A 5GB+ HDD TO PROCEED.

[STORY]
One fine day i needed a server. Now i could easily host it on Amazon Cloud, my OpenStack private server or on a Docker Container or On the Raspberry Pi B+ or On my Mac but no I am an Engineer and so i searched my pile of old electronics and found my old laptop.

This laptop (Sony Vaio VGN-S46GP/S) is special as i had screwed it up by running it in my drawer on full power for 3 hours downloading stuff. Well for those who don’t know what happens when u do such a thing, well basically you need to use a towel folded twice preferably to take the laptop out of the desk. Then u need to scrape off the molten rubber from ur towel and whatever surface u place the laptop on. And your hard drive never ever again works. I tried putting the hdd in the freezer to make it work but that too didn’t do the trick so it was pretty screwed up.

So this was my platform of choice for todays experiment.
[/STORY]

———————————————————————————————————

So firstly you need this script to work.
Place the following script in a file called “pps.sh”.
And

chmod 777 pps.sh

Replace

PIIP=’192.168.1.37′ <- with your ip
NMSK='255.255.255.0' <- default netmask

In the “pps.sh” file place the following


#!/bin/bash
#
# MAKE PXE SERVER
#
#The network mask is set to a default of /24 or 255.255.255.0 if yo wish to modify this go to -> line ????
PIIP='192.168.1.37' # this will determine the ip address of the rpi's eth0 interface and thus the rest of the network settings it uses.
# use /24 Network Masks least you make have to edit this script.
#----------------------------------------------------------------------------------------------------------------------------------
#----------------------------------------------------------------------------------------------------------------------------------
#----------------------------------------------------------------------------------------------------------------------------------
NMSK='255.255.255.0'
IFS=. read -r if1 if2 if3 if4 <<< "$PIIP"
IFS=. read -r msk1 msk2 msk3 msk4 <<< "$NMSK"
NETIP=$(printf "%d.%d.%d.%d\n" "$((if1 & msk1))" "$((if2 & msk2))" "$((if3 & msk3))" "$((if4 & msk4))")

IFS=. read -r ip1 ip2 ip3 ip4 <<< "$PIIP"
DHCPL=$(printf "%d.%d.%d.%d\n" "$((ip1))" "$((ip2))" "$((ip3))" "$((if4 + 4 ))")

IFS=. read -r ip1 ip2 ip3 ip4 << $1
}

check_tubes() {
   printf "\nChecking your tubes..."
   if ! ping -c 1 google.com > /dev/null 2>&1  ; then
      if ! ping -c 1 yahoo.com > /dev/null 2>&1  ; then
         if ! ping -c 1 bing.com > /dev/null 2>&1 ; then
             clear
             printf "\n\nDo you have an internet connection???\n\n"
             exit 2
         fi
      fi
   fi
   printf "\n\ntubes working....\n\n"
}

# func requires aguments ****
check_website() {
   if ! curl -sSf $1 > /dev/null 2>&1 ; then
      printf "\nYou seem to have a internet connection but\nCannot communicate "$1"..."
      exit 3
   fi
}

# func requires aguments ****
get_aptpkg() {
   if ! apt-get -y install $1; then
       printf "\n\nAPT failed to install "$1", are your repos working?\nexiting...\n\n"
       exit 4
   fi
}

# func requires aguments 1:(dowload directory) 2:(url) ****
use_wget() {
   printf "\nFetching from "$2"...\n\n"
   if ! wget --tries=4 --read-timeout=20 -P $1 $2 ; then
       printf "\nSomething went wrong downloading from "$2" \nexiting...."
       exit 5
   fi
}

get_permission() {
 while true; do
     read answer
     case $answer in
          [Yy] ) break;;
          [Yy][eE][sS] ) break;;
          [nN] ) printf "\n Exiting Now \n"; exit;;
          [Nn][oO] ) printf "\n Exiting Now \n"; exit;;
            *  ) printf "\n Not Valid, Answer y or n\n";;
     esac
 done
}

printf "\nTo run this script properly it need to run as root and have a working internet connection."
printf "\nThis script will download necessary plop system files from the plop website"
printf "\nit will also install the apt packages dnsmasq and syslinux to create a working pxe server."
printf "\nThe network configuration of your eth0 interface on your rpi will be modified\nacording to the above variables."
printf "\n\nWARNING---WARNING---WARNING\n"
printf "\nIf you are using the eth0 connection for your main internet connection this will most likely"
printf "\ndisable your internet connection\n\n You may have to connect to the internet over WiFi or use a usb to ethernet adapter.\n"
printf "\nThe program wicd-curses may prove usefull for establising a wireless connection in your rpi with ease through ssh."
printf "\nCONTINUE? (y/n)\n\n"

get_permission
check_user root
check_tubes

#if [ ! -e /var/log/rpi-pxe ]; then

#   apt-get update
#   get_aptpkg dnsmasq
#   get_aptpkg syslinux-common

# else

#   printf "\nYou ran this script before...\nto continue it will erase the /tftpboot folder"
#   printf "\nalong with the interface and dnsmasq configuration files."
#   printf "\nYou need to perform a backup if any of the files are important to you."
#   printf "\nCONTINUE? (y/n)?\n\n"

#   get_permission
   rm -r /tftpboot

#fi

mkdir -p /tftpboot/{pxelinux.cfg,ploplinux-netboot}
cp /usr/lib/syslinux/{memdisk,menu.c32,vesamenu.c32,pxelinux.0} /tftpboot

#Begin writing to pxelinux.cfg/default
#||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

cat > /tftpboot/pxelinux.cfg/default < /etc/network/interfaces < /etc/dnsmasq.conf << EOL
enable-tftp
tftp-root=/tftpboot
dhcp-boot=pxelinux.0
interface=eth0
dhcp-range=eth0,$DHCPL,$DHCPH,$NMSK,12h
log-queries
log-facility=/var/log/dnsmasq.log
server=208.67.222.222
no-resolv

EOL

# wrapping up....
ifdown eth0 && ifup eth0

if ! service dnsmasq stop; then
     killall dnsmasq
      if ! service dnsmasq start; then
           printf "\n\nSomething is probably using port 53 use"
           printf "\nsudo netstat -tapen | grep ":53""
           printf "\nto find out which program is causing problems"
           exit 7
      fi
 else
    service dnsmasq start
fi

make_runlog /var/log/rpi-pxe
printf "\nScript finished successfully\nexiting...\n\n"

exit 0

Now that this is setup, we need to we first need to download CentOS.

Steps
1. Download CentOS 6.6 minimal to your PC
2. Extract the initrd.img and vmlinuz from the isolinux folder and transfer it to your RPI using SCP or any other method you prefer to a folder called /tftpboot/centos/
3. Copy all the folder isolinux to a USB drive and connect the drive to the PC

On the PI open the terminal and follow the below steps.

Goto /tftpboot/pxelinux.cfg/
and open the default file in your favourite editor (i prefer nano :p)

Add an entry as follows
label centos 
      kernel /tftpboot/centos/vmlinuz
      append initrd=/tftpboot/centos/initrd.img

Setup your PC to boot from network. And start.
NEXT
I believe you are all engineers and can do a normal OS install.

Reference’s used:

[1]: https://github.com/xor-function/plop-pxe-server

[2]: http://patrick-henriksson.blogspot.in/2013/04/raspberry-pi-as-pxe-server-with-proxy.html

NOTE: Using the link [2] instructions i ran into problem like PXE-E74 error for which i did not find any solutions online. So use that link only to understand how it all works.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s