Friday, June 5, 2015

Install Linux Without Physical Media With PXE

Install Linux Without Physical Media With PXE

Install Linux Without Physical Media With PXE

(If you have an existing DHCP server (CENTOs) and you can configure that use this  )


Recently I picked up an older DellPoweredge 2650 server from Craigslist for $50. Even with the age of the device, I couldn’t turn down such a steal. Loaded with two single core 32bit Xeons, a whopping 2GB of RAM, and a few SCSI drives this 2U server was the perfect development server. There was just one issue, it didn’t have an operating system on it. “Ok, not a problem”, I thought “I’ll just put CentOS 7 on a USB stick and install it”. Only to discover that this server doesn’t support USB booting and that RHEL dropped support for 32bit CPUs. Being that this server only has a CD drive, and I don’t have any CD-R/RWs lying around, a PXE install was the only choice for me. In this tutorial, I will show you how to set up a PXE server on CentOS 7.


Ingredients

For this you’re going to need a computer running CentOS 7 (or similar) that will act as the DHCP, TFTP, FTP, and PXE server. I used a virtual machine with 8GB of RAM and a dual-core processor. You’re also going to need a client machine that supports PXE boot. A virtual machine can be used to test your PXE server.

Install the prerequisites

We need the following packages to perform our remote installation:
  • dnsmasq
  • vsftpd
  • syslinux
  • tftp-server
You can install all of these (or update if they are out-of-date) using:
yum install -y dnsmasq vsftpd syslinux tftp-server
You’re also going to need an ISO of your favourite Linux distribution. This will be the operating system that will be installed on your client. I chose Fedora Server 21 NetInstall. I recommend that you use a NetInstall image as it reduces the space needed on your server and speeds up the PXE install.

Setting up dnsmasq

DNSMASQ will act as the DHCP and PXE Server for us. The first thing we need to do is configure our DHCP options.

Configure a DHCP Server

  1. Run ip addr and note down the IP Address of this server (highlighted in red), and the interface name (highlighted in blue)
    ip addr shows you basic information about your network interfacesip addr shows you basic information about your network interfaces
  2. Make a backup of the default dnsmasq configuration file:
    mv /etc/dnsmasq.conf  /etc/dnsmasq.conf.backup
  3. Edit /etc/dnsmasq.conf and add the following lines – make sure to edit the parameters to meet your network:
    interface=,lo
    
    # DHCP range-leases
    dhcp-range= ,,,,1h
    
    # PXE
    dhcp-boot=pxelinux.0,pxeserver,
    
    # Gateway
    dhcp-option=3,
    
    # DNS
    dhcp-option=6, 8.8.8.8
    
    # Broadcast Address
    dhcp-option=28,
    
    If you don’t know your broadcast address you can find it using this Online IP Subnet Calculator
  4. Keep that file open, we’re going to keep using it in the next step

Configure a PXE Server

In the same file we were editing for our DHCP server, we’re going to add on this configuration options for our PXE server:
pxe-prompt="Press F8 for menu.", 60
pxe-service=x86PC, "Install Fedora 21", pxelinux
enable-tftp
tftp-root=/var/lib/tftpboot
You can change around the pxe-prompt and the pxe-service but the important one is tftp-root To see all of the possible configuration options, check the DNSMASQ Manual.

Set up Syslinux

Syslinux is a group of smaller projects, one of which is PXELINUX which is a minimal Linux distribution meant to be used with PXE Installations. PXELINUX is not going to be installed on your system, rather just loaded into memory and used to install your actual operating system.
  1. Copy over syslinux into the tftpboot directory so that it will be copied onto our clients:
    cp -r /usr/share/syslinux/* /var/lib/tftpboot
  2. Create the configuration directory that PXELINUX will use to find its configuration file (made in the next step):
    mkdir /var/lib/tftpboot/pxelinux.cfg
  3. Create the file /var/lib/tftpboot/pxelinux.cfg/default and open it
  4. Add the following to the file, making sure to edit the IP address to match your servers:
    default menu.c32
    prompt 0
    timeout 100
    ONTIMEOUT local
    
    menu title ########## PXE Boot Menu ##########
    
    label 1
    menu label ^1) Install Fedora 21 Server NetInstall
    kernel fedora21/vmlinuz
    append initrd=fedora21/initrd.img method=ftp:///pub devfs=nomount
    
    label 2
    menu label ^2) Boot from local drive
    
  5. Save and close that file
Tip: notice how we are using FTP to transfer the kernal and initrd, if you didn’t want to use FTP, you could use HTTP, HTTPS, or NFS to name a few
Now we have to copy over the linux kernel and the initramdisk to our tftpboot directory, and the rest of the operating system to our FTP directory
  1. Start by mounting the ISO:
    mount -o loop  /mnt
    
  2. Copy the kernel and the ramdisk:
    mkdir /var/lib/tftpboot/fedora21
    cp /mnt/images/pxeboot/vmlinuz  /var/lib/tftpboot/fedora21
    cp /mnt/images/pxeboot/initrd.img  /var/lib/tftpboot/fedora21
    
  3. Copy the entire contents of the ISO to our public ftp directory and make it globally readable:
    cp -r /mnt/*  /var/ftp/pub/ 
    chmod -R 755 /var/ftp/pub
    

Start and Verify the services

Go ahead and fireup dnsmasq and vsftpd:
systemctl restart dnsmasq
systemctl status dnsmasq
systemctl restart vsftpd
systemctl status vsftpd
CentOS 7 has firewalld enabled by default. You can configure the firewall to allow the required incoming connections but in this tutorial we will simple disable the firewall:
systemctl stop firewalld
You should now be able to browse the public FTP directory of your server and see the contents of the ISO:
The Public FTP directory containing the installation files for Fedora 21The Public FTP directory containing the installation files for Fedora 21

Start a PXE Network Install

Start up your client machine and make sure that you eiter configure your boot order so that your network interface comes before your hard drive, or select it in your boot menu. Your client will then obtain an IP address from your DHCP Server and download pxelinux. You’ll be asked to press F8, and once you do you’ll be given a menu with only one option. Press enter to start pxelinux.
After pressing F8, you’ll see this menu with only one optionAfter pressing F8, you’ll see this menu with only one option
You will then see the PXE Boot Menu with the two options we configured earlier, you can either wait 10 seconds for the default item to be selected or press enter.
The PXE Boot menu, please note that mine will look different as I have changed some of the valuesThe PXE Boot menu, please note that mine will look different as I have changed some of the values
Your client will then download the kernel and ramdisk from your server, then initialize the installer. There may be moments when the system appears to not be doing anything when it is actually downloading the files from your server over FTP. If everything worked, the installation GUI will appear
Welcome to Fedora-Server 21!Welcome to Fedora-Server 21!
And that’s all there is to it! At this point, the job of the PXE Server is complete and the rest of the installation is completed like any other. You’ve successfully set up a PXE Server and can now install Linux on clients without a DVD, USB, or CD! I hope you enjoyed this tutorial and if you have questions please submit them in the comments below.

No comments:

Post a Comment