Skip to content

Instantly share code, notes, and snippets.

@mrmichalis
Last active December 15, 2015 00:29
Show Gist options
  • Save mrmichalis/5173420 to your computer and use it in GitHub Desktop.
Save mrmichalis/5173420 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# createPXE.sh v.02
#
# You MUST edit variables to your environment
# or the deployment will be unsuccessful.
# READ script before executing!
# Note that the subnet variable is NOT your subnet MASK,
# rather the subnet (or network) on which the machine would reside.
SYSNAME=cdhvm
DOMAIN=lunix.co
INTERFACE=eth0
IPADDR=192.168.2.1
SUBNET=192.168.2.0
NETMASK=255.255.255.0
SOURCEPATH=/media
# DHCP Range variables
# range dynamic-bootp
BEGIN1=192.168.2.127
END1=192.168.2.254
# Range
BEGIN2=192.168.2.5
END2=192.168.2.126
# PXE variable RHEL_V is a short descriptor of the Red Hat version
# This variable will be the name of a tftp directory that is created, so best to not include spaces
RHEL_V=RHEL6.3
# Valid values for Network share as of this writing are:
# ftp | http
NETWORK_SHARE=ftp
# Here is where the script will attempt check for errors
# before proceeding.
# If you would like to add additional protections before
# changes are made to the system, this would be the place
# to add them.
if [ ! -f $SOURCEPATH/media.repo ]; then
echo "Please check that your RHEL CD is mounted at $SOURCEPATH"
exit
fi
if
[ $NETWORK_SHARE != "ftp" -a $NETWORK_SHARE != "http" ]; then
echo "$NETWORK_SHARE is not an allowable value for \$NETWORK_SHARE, please revise and rerun the script."
exit
fi
cat > /etc/sysconfig/network << EOF
NETWORKING=yes
HOSTNAME=$SYSNAME.$DOMAIN
EOF
hostname $SYSNAME.$DOMAIN
cat > /etc/sysconfig/network-scripts/ifcfg-$INTERFACE << EOF
DEVICE="$INTERFACE"
BOOTPROTO="static"
#HWADDR="$(cat /etc/udev/rules.d/70-persistent-net.rules | grep $INTERFACE | awk -F\" '{print $8}' | tr [a-z] [A-Z])"
HWADDR="$(ifconfig eth0 | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}')"
IPV6INIT="no"
MTU="1500"
NM_CONTROLLED="no"
ONBOOT="yes"
TYPE="Ethernet"
IPADDR="$IPADDR"
NETMASK="$NETMASK"
DNS1="$IPADDR"
DOMAIN="$DOMAIN"
EOF
cp /media/media.repo /etc/yum.repos.d/
cat >> /etc/yum.repos.d/media.repo << EOF
baseurl=file:///media/Server
enabled=1
EOF
yum clean metadata
# Install services
yum -y install dhcp tftp-server syslinux policycoreutils-python
# Adjust firewall
cat > /etc/sysconfig/iptables << EOF
# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 21 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 53 -j ACCEPT
-A INPUT -m state --state NEW -m udp -p udp --dport 53 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 69 -j ACCEPT
-A INPUT -m state --state NEW -m udp -p udp --dport 69 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
EOF
service iptables restart
# Configuration of chosen service and Kickstart file
cat > /root/ks.cfg << EOFF
# Kickstart file automatically generated by anaconda.
#version=DEVEL
install
url --url $NETWORK_SHARE://$IPADDR/inst
lang en_US.UTF-8
keyboard us
network --onboot yes --device eth0 --bootproto dhcp
rootpw RedHat!
firewall --service=ssh
authconfig --enableshadow --passalgo=sha512
selinux --enforcing
timezone --utc America/North_Dakota/Center
bootloader --location=mbr --driveorder=vda --append=" rhgb crashkernel=auto quiet"
# The following is the partition information you requested
# Note that any partitions you deleted are not expressed
# here so unless you clear all partitions first, this is
# not guaranteed to work
zerombr
part /boot --fstype=ext4 --size=500
part pv.253002 --grow --size=1
clearpart --all --drives=sda
volgroup VolGroup --pesize=4096 pv.253002
logvol / --fstype=ext4 --name=lv_root --vgname=VolGroup --grow --size=1024 --maxsize=51200
logvol swap --name=lv_swap --vgname=VolGroup --grow --size=512 --maxsize=1024
#repo --name="Red Hat Enterprise Linux" --baseurl=cdrom:sr0 --cost=100
%packages --nobase
@core
openssh-clients
%post
cat >/etc/yum.repos.d/media.repo <<EOF
[InstallMedia]
name=Red Hat Server Linux 6
baseurl=$NETWORK_SHARE://$IPADDR/inst/Server
mediaid=1321544350.537856
enabled=1
gpgcheck=0
cost=500
EOF
%end
EOFF
# DHCP configuration
cat > /etc/dhcp/dhcpd.conf << EOF
# DHCP Server Configuration file.
# see /usr/share/doc/dhcp*/dhcpd.conf.sample
# see 'man 5 dhcpd.conf'
#
subnet $SUBNET netmask $NETMASK {
range dynamic-bootp $BEGIN1 $END1 ;
range $BEGIN2 $END2 ;
option routers $IPADDR;
default-lease-time 6000;
max-lease-time 7200;
allow booting;
allow bootp;
class "pxeclients" {
match if substring(option vendor-class-identifier, 0,9) = "PXEClient";
next-server $IPADDR;
filename "pxelinux.0";
}
}
EOF
# TFTP server configuration
mkdir -p /var/lib/tftpboot/$RHEL_V
mkdir -p /var/lib/tftpboot/pxelinux.cfg
cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/
cp /usr/share/syslinux/menu.c32 /var/lib/tftpboot/
# pxe default file configuration
cat > /var/lib/tftpboot/pxelinux.cfg/default << EOF
default menu.c32
prompt 0
timeout 150
ONTIMEOUT local
MENU TITLE Welcome
label local
MENU LABEL Boot to hard drive
LOCALBOOT 0
label $RHEL_V
kernel $RHEL_V/vmlinuz
append initrd=$RHEL_V/initrd.img noipv6 ks=$NETWORK_SHARE://$IPADDR/pub/ks.cfg ksdevice=$INTERFACE
EOF
case $NETWORK_SHARE in
ftp)
yum -y install vsftpd
mv /root/ks.cfg /var/ftp/pub/ks.cfg
chmod 755 /var/ftp/pub/ks.cfg
restorecon -R -v /var/ftp/pub
mkdir -p /var/ftp/inst
cp -var /media/. /var/ftp/inst/
semanage fcontext -a -t public_conent_t "/var/ftp/inst(/.*)?"
restorecon -R -v /var/ftp/inst/
iptables -I INPUT -m state --state NEW -m tcp -p tcp --dport 21 -j ACCEPT
service iptables save
service vsftpd start
chkconfig vsftpd on
# Add appropriate line to modules to /etc/sysconfig/iptables-config
sed -i 's/IPTABLES_MODULES=""/IPTABLES_MODULES="ip_conntrack_ftp ip_conntrack_tftp"/' /etc/sysconfig/iptables-config
# Readjust repo
sed -i 's/baseurl=file:\/\/\/media\/Server/baseusr=file:\/\/\/var\/ftp\/inst\/Server/' /etc/yum.repos.d/media.repo
yum clean metadata
# Finish PXE configuration
cp /var/ftp/inst/images/pxeboot/* /var/lib/tftpboot/$RHEL_V/
;;
http)
yum -y install httpd
sed -i 's/#ServerName www.example.com:80/ServerName $SYSNAME$DOMAINNAME/g' /etc/httpd/conf/httpd.conf
mkdir /var/www/html/pub
mv /root/ks.cfg /var/www/html/pub/ks.cfg
chmod 755 /var/www/html/pub/ks.cfg
semanage fcontext -a -t httpd_sys_content_t "/var/www/html/pub(/.*)?"
restorecon -R -v /var/www/html/pub
mkdir /var/www/html/inst
cp -var /media/. /var/www/html/inst/
semanage fcontext -a -t httpd_sys_content_t "/var/www/html/inst(/.*)?"
restorecon -R -v /var/www/html/inst
iptables -I INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
service iptables save
service httpd start
chkconfig httpd on
# Readjust repo
sed -i 's/baseurl=file:\/\/\/media\/Server/baseusr=file:\/\/\/var\/www\/html\/inst\/Server/' /etc/yum.repos.d/media.repo
yum clean metadata
# Finish PXE configuration
cp /var/www/html/inst/images/pxeboot/* /var/lib/tftpboot/$RHEL_V/
;;
esac
# Start services, ensure services start with system boot
service xinetd start
chkconfig xinetd on
chkconfig tftp on
service dhcpd start
chkconfig dhcpd on
service network restart
service iptables restart
echo ""
echo "Set up is complete. You are now free to test"
# Roger Heslop
# [email protected]
# 20121220
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment