CentOS 7 PXE boot server and automated installation with kickstart

Posted on Wed 17 February 2016 by Pavlo Khmel

Install packages:

yum -y install httpd xinetd syslinux tftp-server dhcp

Copy files from iso image:

mount -o loop CentOS-7-x86_64-Minimal-1503-01.iso /mnt/
cp -r /mnt /data/centos-7.1-64x-pxe
umount /mnt

Copy files for tftp:

cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/
cp /data/centos-7.1-64x-pxe/images/pxeboot/vmlinuz /var/lib/tftpboot/
cp /data/centos-7.1-64x-pxe/images/pxeboot/initrd.img /var/lib/tftpboot/
cp /usr/share/syslinux/menu.c32 /var/lib/tftpboot/
cp /usr/share/syslinux/chain.c32 /var/lib/tftpboot/
mkdir /var/lib/tftpboot/pxelinux.cfg
mkdir /data/kickstarts/

DHCPD configuration:

# vi /etc/dhcp/dhcpd.conf 
shared-network MY-NET {
    subnet 10.0.0.0 netmask 255.0.0.0 {
        authoritative;
        allow booting;
        option routers             10.0.0.1;
        option subnet-mask         255.0.0.0;
        option domain-name         "khmel.org";
        option domain-name-servers 10.0.0.1;
        option time-offset         -28800;
        option ntp-servers         10.0.0.1;
        host controller { hardware ethernet 00:19:19:55:37:df; fixed-address 10.4.4.1; next-server 10.0.0.19; filename "pxelinux.0"; }
        host compute1 { hardware ethernet 00:19:19:55:37:a5; fixed-address 10.4.4.2; next-server 10.0.0.19; filename "pxelinux.0"; }
        host block1 { hardware ethernet 00:19:19:55:33:fe; fixed-address 10.4.4.3; next-server 10.0.0.19; filename "pxelinux.0"; }
    }
}

Convert IP to HEX:

# Example:
printf '%02X' 10 4 4 1 ; echo
0A040401

Create boot configuration for special server.

# vi /var/lib/tftpboot/pxelinux.cfg/0A040401
default menu.c32
prompt 0
timeout 300

menu title PXE Boot Menu
label 1
   menu label ^1) Boot from local drive
   COM32 chain.c32
   APPEND hd0 0

label 2
   menu label ^2) Install CentOS 7.1 x64 minimal
   kernel vmlinuz
   append ksdevice=bootif initrd=initrd.img load_ramdisk=1 network ks=http://10.0.0.19/kickstarts/myserver.ks nameserver=10.0.0.1 ip=10.4.4.1::10.0.0.1:8:myserver::none

Create kickstart file for minimal CentOS 7.1 installation:

/var/pxe/kickstarts/myserver.ks
text
install
url --url http://10.0.0.19/centos71/
lang en_US.UTF-8
keyboard us
rootpw --iscrypted $6$0obla.....................PFGxpgX/
firewall --disabled
authconfig --enableshadow --passalgo=sha512
selinux --disabled
timezone --utc Europe/Oslo
eula --agreed
reboot
ignoredisk --only-use=sda
bootloader --location=mbr
zerombr
clearpart --all --initlabel
part /boot --fstype="xfs" --ondisk=sda --size=500
part pv.01 --fstype="lvmpv" --ondisk=sda --size=1 --grow
volgroup centos pv.01
logvol swap  --fstype="swap" --size=1024 --name=swap --vgname=centos
logvol /  --fstype="xfs" --size=1 --name=root --vgname=centos --grow
%packages --nobase --ignoremissing
@core
%end
%post
mkdir -p /root/.ssh
chmod 700 /root/.ssh
echo "ssh-rsa AAAAB3NzaC1yc2EA............++MwrB root@my-host" >> /root/.ssh/authorized_keys
chmod 600 /root/.ssh/authorized_keys
restorecon -R /root/.ssh/
%end

HTTPD configuration file:

# vi /etc/httpd/conf.d/pxeboot.conf

Alias /centos71 /data/centos-7.1-64x-pxe
<Directory /data/centos-7.1-64x-pxe>
    Options Indexes FollowSymLinks
    # IP address you allow to access
    Require ip 127.0.0.1 10.0.0.0/8
</Directory>

Alias /kickstarts /data/kickstarts
<Directory /data/kickstarts>
    Options Indexes FollowSymLinks
    # IP address you allow to access
    Require ip 127.0.0.1 10.0.0.0/8
</Directory>

Enable and start services:

sed -i 's/disable\t\t\t= yes/disable = no/' /etc/xinetd.d/tftp
systemctl enable xinetd
systemctl enable httpd
systemctl enable dhcpd
systemctl restart xinetd
systemctl restart httpd
systemctl restart dhcpd

Power on server with MAC: 00:19:19:55:37:df. You'll see PXE menu on PXE boot.