How to make PXE_Boot script

How to make PXE_Boot script

Introduction of PXE booting

PXE (Pre-Boot Execution Environment) booting is a technology that enables a computer to boot from a network device, rather than a local hard drive or other storage media. PXE booting is commonly used in enterprise environments to automate the deployment of operating systems and software applications on a large scale.

With PXE booting, a client computer first requests an IP address from a DHCP (Dynamic Host Configuration Protocol) server, then requests the location of a boot server from a TFTP (Trivial File Transfer Protocol) server. The boot server provides the client computer with the necessary files to boot, including the operating system, drivers, and other software.

PXE booting provides several benefits, including faster deployment and provisioning of operating systems and applications, and reduced administrative overhead. It also enables centralized management of operating systems and software applications, ensuring that they are consistent and up-to-date across all client computers.

PXE booting is commonly used in combination with other technologies, such as containerization and virtualization, to automate the deployment and management of software applications in large-scale environments.



System Requirements

Hardware requirements:

  1. RAM: 8 GB (Preferred 16)
  2. PROCESSOR: i5 10 gen
  3. SSD: 256GB

Software requirements:

  1. VMware workstation
  2. Centos7 iso
  3. Editor: - vi/vim/nano

Virtual Machine Requirement for Master Node

  1. RAM: 4GB
  2. Cores: 2
  3. Storage: 50GB
  4. OS: Centos 7 (Linux)
  5. Network Adapter: 2
    1. NAT
    2. Custom


Prerequisite For Creating a PXE Cluster

Disable SELinux:

  1.       vim /etc/selinux/config


Stop & Disable Firewall:

  1. systemctl stop firewalld
  2. systemctl disable firewalld
  3. systemctl status firewalld


Code

  1. Crate a file in any editor (vi/vim/nano) and paste all following code
  2. Save to [file_name].sh
  3. For example- pxe_boot_script.sh
  4. Change file permission to rwx(777)
  5. Execute script using command (./[file_name].sh) or ( bash [file_name].sh
  6. For example 1) ./pxe_boot_script.sh           2). bash pxe_boot_script.sh
  7. User inputs after execution: - Provide only necessary IP addresses like
  8. Server IP address (Master Node)
  9. Subnet
  10. Network Mask
  11. Starting IP Range (for DHCP pool ip)
  12. Ending IP Range (for DHCP pool ip)

Install all necessary packages

  1. yum install -y dhcp* syslinux xinetd tftp-server httpd
Description: - yum is a package installer, this code installs necessary packages like: - DHCPD (for IP Address pool and lease), SYSLINUX, XINTED TFTP-SERVER and HTTPD.

Configure dhcpd network

  1. tee /etc/dhcp/dhcpd.conf<<EOF
  2. #host_change=server Host ip, subnet_change=subnet, netmask_change=netmask, range_start=starting ip, range_end=ending ip,
  3. option domain-name "host_change";
  4. subnet subnet_change netmask netmask_change{
  5. range range_start range_end;
  6. option routers host_change;
  7. filename "pxelinux.0";
  8. next-server host_change;
  9. }
  10. EOF
Description: - TEE command is use stdin to stdout, (save inputs to a file), This code configure DHCP network, assigning SERVER IP, SUBNET, NETWORK MASK, POOL RANGE. All this inputs will provide by user after execution of script.

User Input

  1. #read ip address, subnet, netmask, and DHCP network range from user
  2. read -p "enter server ip: " s_ip
  3. read -p "enter subnet mask: " sub_ip
  4. read -p "enter netmask: " net_ip
  5. read -p "enter starting ip range:" start_ip
  6. read -p "enter ending ip range:" end_ip
  7. sed -i -e "s/host_change/${s_ip}/g" /etc/dhcp/dhcpd.conf
  8. sed -i -e "s/subnet_change/${sub_ip}/g" /etc/dhcp/dhcpd.conf
  9. sed -i -e "s/netmask_change/${net_ip}/g" /etc/dhcp/dhcpd.conf
  10. sed -i -e "s/range_start/${start_ip}/g" /etc/dhcp/dhcpd.conf
  11. sed -i -e "s/range_end/${end_ip}/g" /etc/dhcp/dhcpd.conf
  12. #make directory
  13. mkdir /var/lib/tftpboot/pxelinux.cfg
  14. #copy pxelinux.0 file to newly created direcoty
  15. cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/
Description: - User provides the all IP Address and pool range, script automatically changes the values to all the configuration files.

Configure xinted file

  1. #configure xinted file
  2. tee /etc/xinetd.d/tftp<<EOF
  3. #default: off
  4. #description: The tftp server serves files using the trivial file transfer \
  5. #protocol.  The tftp protocol is often used to boot diskless \
  6. #workstations, download configuration files to network-aware printers, \
  7. #and to start the installation process for some operating systems.
  8. service tftp
  9.       {
  10.       socket_type          = dgram
  11.       protocol                 = udp
  12.       wait                        = yes
  13.       user                        = root
  14.       server                     = /usr/sbin/in.tftpd
  15.       server_args           = -s /var/lib/tftpboot
  16.       disable                   = no
  17.       per_source            = 11
  18.       cps                          = 100 2
  19.       flags                       = IPv4
  20.       }
  21. EOF
  1. #create a directory and copy all boot related files
  2. mkdir -p /var/pxe/centos7
  3. mkdir -p /var/lib/tftpboot/centos7
  4. mount -t iso9660 -o loop /dev/cdrom /var/pxe/centos7/
  5. cp /var/pxe/centos7/images/pxeboot/vmlinuz /var/lib/tftpboot/centos7/
  6. cp /var/pxe/centos7/images/pxeboot/initrd.img /var/lib/tftpboot/centos7/
  7. cp /usr/share/syslinux/menu.c32 /var/lib/tftpboot/ 

Configure pxelinux.cfg file

  1. #configure pxelinux.cfg file
  2. tee /var/lib/tftpboot/pxelinux.cfg/default<<EOF
  3. #server_ip
  4. timeout 100
  5. default menu.c32
  6. menu title #######PXE BOOT MENU####
  7. label 1
  8.       menu label ^1) Install centos7
  9.      kernel centos7/vmlinuz
  10.                      append initrd=centos7/initrd.img ks=http://host_change/ks/centos7-ks.cfg method=http://host_change/centos7/ devfs=nomount
  11. label 2
  12.       menu label ^2) Boot from local drive
  13.                     localboot
  14. EOF
  15. sed -i -e "s/host_change/${s_ip}/g" /var/lib/tftpboot/pxelinux.cfg/default

Configure pxeboot.conf file

  1. #configure pxeboot.conf file
  2. tee /etc/httpd/conf.d/pxeboot.conf<<EOF
  3. #subnet_ip
  4. Alias /centos7 /var/pxe/centos7
  5. <Directory /var/pxe/centos7>
  6. options Indexes FollowSymlinks
  7. Require ip 127.0.0.1 subnet_change/24
  8. </Directory>
  9. EOF
  10. sed -i -e "s/subnet_change/${sub_ip}/g" /etc/httpd/conf.d/pxeboot.conf

Kickstart file

  1. #kickstart
  2. #python -c 'import crypt,getpass;print(crypt.crypt(getpass.getpass(),crypt.mksalt(crypt.METHOD_SHA512)))' > pass.txt
  3. mkdir /var/www/html/ks
  4. tee /var/www/html/ks/centos7-ks.cfg<<EOF
  5. #create new
  6. install
  7. # automatically proceed for each steps
  8. autostep
  9. #reboot after installing
  10. reboot
  11. #encrypt algorithm
  12. auth --enableshadow --passalgo=sha512
  13. #installation source
  14. url --url=http://host_change/centos7/
  15. # install disk
  16. ignoredisk --only-use=sda
  17. # keyboard layouts
  18. keyboard --vckeymap=jp106 --xlayouts='jp','us'
  19. # system locale
  20. lang en_US.UTF-8
  21. # network settings
  22. network --bootproto=dhcp --ipv6=auto --activate --hostname=localhost
  23. #root password you generated above
  24. #rootpw --iscrypted < pass.txt
  25. #timezone
  26. timezone Asia/Tokyo --isUtc --nontp
  27. #bootloader's settings
  28. bootloader --location=mbr --boot-drive=sda
  29. #initialize all partition tables
  30. zerombr
  31. clearpart --all --initlabel
  32. #partitioning
  33. autopart --type=lvm
  34. #part /boot --fstype="xfs" --ondisk=sda --size=500
  35. #part pv.10 --fstype="lvmpv" --ondisk=sda --size=51200
  36. #volgroup VolGroup --pesize=4096 pv.10
  37. #logvol / --fstype="xfs" --size=20480 --name=root --vgname=VolGroup
  38. #logvol swap --fstype="swap" --size=4096 --name=swap --vgname=VolGroup
  39. %packages
  40. @core
  41. %end
  42. EOF
  43. sed -i -e "s/host_change/${s_ip}/g" /var/www/html/ks/centos7-ks.cfg

Start all the services

  1. systemctl start xinetd
  2. systemctl enable xinetd
  3. systemctl start httpd
  4. systemctl enable httpd
  5. systemctl start dhcpd
  6. systemctl enable dhcpd
  7. systemctl start tftp
  8. systemctl enable tftp

Screenshots of output

User provides IP Addresses


DHCP file created after execution

pxelinux.cfg

pxeboot.conf file



xinted file

Kickstart File



for full script please click here


    • Related Articles

    • How to create Virtual Machine in VMWare

      What is Virtualisation Virtualization is technology that lets you create useful IT services using resources that are traditionally bound to hardware. It allows you to use a physical machine’s full capacity by distributing its capabilities among many ...
    • What is Boot Process in Linux OS.

      Boot Process in Linux OS. Have you ever wondered what happens behind the scenes from the time you press the power button until the Linux login prompt appears? Press the power button on your system, and after few moments you see the Linux login ...
    • Configuring BMC in ASUS Server

      To configure the Baseboard Management Controller (BMC) IP address on an ASUS server, initially access the server's BIOS/UEFI settings and then configure the BMC network settings. Here's a step-by-step instructions Server BMC Configuration preparation ...
    • Initial steps for the troubleshooting/detecting error if NIC card is not detecting on server.

      What is NIC(Network interface Card) Card? A network interface card is a piece of hardware that allows computers to communicate with other devices on a network. It can also be called an Ethernet card, LAN card, or network adaptor. A NIC provides a ...
    • Initial Troubleshooting Steps When Server Power Socket Is Getting Yellow Light/Red Light.

      Power Socket Power Socket is the point on server where the power cable is plugged in. These sockets are part of the server's power supply unit (PSU) and are essential for delivering electrical power to the server. Server with different colored lights ...