#!/bin/bash
make a pxe machine
v1.0 by 2020-03-30
name=centos
host_ip=$(ip a|grep brd|grep inet|awk ‘{print $2}’|awk -F ‘/’ '{print KaTeX parse error: Expected 'EOF', got '}' at position 2: 1}̲')
host_vlan=(ip a|grep brd|grep inet|awk ‘{print $2}’|awk -F ‘/’ ‘{print $1}’|awk -F ‘.’ ‘{print $1"."$2"."$3}’)
ISO=/root/CentOS-7-x86_64-Everything-1810.iso
tmp_dir=/mnt
yum_dir=/etc/yum.repos.d
apache_dir=/var/www/html
tftp_dir=/var/lib/tftpboot
service_install() {
if [ $? -eq 0 ];then
yum install -y $1 &>/dev/null
if [ $? -eq 0 ];then
echo “$1 service is installed secuessfully!”
else
exit 2
fi
fi
}
menu() {
cat <<-EOF
##########################################
# a. install legacy mode pxe machine #
# b. install uefi mode pxe machine #
# c. 待完善 #
# d. 待完善 #
##########################################
EOF
}
menu
read -p "please press [a|b|c|d] to select your machine: " num
sed -ri ‘/^SELINUX=/cSELINUX=disabled’ /etc/selinux/config
setenforce 0
systemctl stop firewalld &>/dev/null
systemctl disable firewalld &>/dev/null
mount |grep $tmp_dir
if [ $? -ne 0 ];then
mount $ISO $tmp_dir &>/dev/null
echo “ISO is mounted secussfully!”
fi
if [ -d $yum_dir/bak ];then
continue
else
mkdir $yum_dir/bak; mv $yum_dir/*.repo $yum_dir/bak
fi
if [ -f yumdir/yum_dir/yumdir/name.repo ];then
echo "name.repoisexist!"elsecat>name.repo is exist!"
else
cat >name.repoisexist!"elsecat>yum_dir/name.repo<<−EOF[name.repo<<-EOF
[name.repo<<−EOF[name]
name=namebaseurl=file://name
baseurl=file://namebaseurl=file://tmp_dir
enabled=1
gpgcheck=0
EOF
echo “$name.repo is created secussfully!”
fi
case “$num” in
a)
yum makecache >/dev/null
service_install httpd
service_install dhcp
service_install tftp-server
service_install xinetd
service_install syslinux
service_install vim
umount $tmp_dir
if [ $? -eq 0 ];then
cat >$yum_dir/$name.repo <<-EOF
[$name]
name=$name
baseurl=http://${host_ip}/$name
enabled=1
gpgcheck=0
EOF
else
exit 2
fi
if [ $? -eq 0 ];then
cat >/etc/dhcp/dhcpd.conf <<-EOF
subnet $host_vlan.0 netmask 255.255.255.0 {
range $host_vlan.1 $host_vlan.253;
option routers $host_vlan.254;
option broadcast-address $host_vlan.255;
default-lease-time 600;
max-lease-time 1800;
next-server ${host_ip};
filename "pxelinux.0";
}
EOF
else
echo "dhcpd error......"
fi
systemctl start dhcpd
if [ $? -eq 0 ];then
echo "dhcp service is up!"
systemctl enable dhcpd &>/dev/null
else
echo "Error......please check dhcp config file!"
fi
if [ $? -eq 0 ];then
cat >/etc/xinetd.d/tftp <<-EOF
service tftp
{
socket_type = dgram
protocol = udp
wait = yes
user = root
server = /usr/sbin/in.tftpd
server_args = -s /var/lib/tftpboot
disable = no
per_source = 11
cps = 100 2
flags = IPv4
}
EOF
else
echo "xinetd error......"
fi
systemctl start xinetd
if [ $? -eq 0 ];then
echo "tftp service is up!"
systemctl enable xinetd &>/dev/null
else
echo "Error......"
fi
mkdir -p $apache_dir/$name
if [ $? -eq 0 ];then
cat >>/etc/fstab<<-EOF
$ISO $apache_dir/$name iso9660 defaults 0 0
EOF
fi
mount -a &>/dev/null
mkdir -p $tftp_dir/pxelinux.cfg
cp $apache_dir/$name/isolinux/isolinux.cfg $tftp_dir/pxelinux.cfg/default
cp $apache_dir/$name/images/pxeboot/initrd.img $tftp_dir
cp $apache_dir/$name/images/pxeboot/vmlinuz $tftp_dir
cp /usr/share/syslinux/pxelinux.0 $tftp_dir
chmod a+w $tftp_dir/pxelinux.cfg/default
cat >$tftp_dir/pxelinux.cfg/default<<-EOF
default $name
timeout 600
label $name
menu label ^Install CentOS 7
kernel vmlinuz
append initrd=initrd.img ks=http://$host_ip/ks.cfg
EOF
systemctl start httpd
if [ $? -eq 0 ];then
echo "httpd service is up!"
systemctl enable httpd &>/dev/null
fi
;;
b)
yum makecache >/dev/null
service_install httpd
service_install tftp-server
service_install xinetd
service_install dhcp
service_install grub2-efi-modules
umount $tmp_dir
if [ $? -eq 0 ];then
cat >$yum_dir/$name.repo<<-EOF
[$name]
name=$name
baseurl=http://${host_ip}/$name
enabled=1
gpgcheck=0
EOF
else
exit 2
fi
cat >/etc/dhcp/dhcpd.conf<<-EOF
log-facility local7;
allow booting;
allow bootp;
option space pxelinux;
option pxelinux.magic code 208 = string;
option pxelinux.configfile code 209 = text;
option pxelinux.pathprefix code 210 = text;
option pxelinux.reboottime code 211 = unsigned integer 32;
option architecture-type code 93 = unsigned integer 16;
subnet $host_vlan.0 netmask 255.255.255.0 {
range $host_vlan.1 $host_vlan.253;
option routers $host_vlan.254;
next-server ${host_ip};
filename "bootx64.efi";
}
EOF
systemctl start dhcpd
if [ $? -eq 0 ];then
echo "dhcp service is up!"
systemctl enable dhcpd &>/dev/null
else
echo "Error......please check dhcp config file!"
fi
cat >/etc/xinetd.d/tftp <<-EOF
service tftp
{
socket_type = dgram
protocol = udp
wait = yes
user = root
server = /usr/sbin/in.tftpd
server_args = -s /var/lib/tftpboot
disable = no
per_source = 11
cps = 100 2
flags = IPv4
}
EOF
systemctl start xinetd
if [ $? -eq 0 ];then
echo "tftp service is up!"
systemctl enable xinetd &>/dev/null
else
echo "Error......"
fi
mkdir -p $apache_dir/$name
if [ $? -eq 0 ];then
cat >>/etc/fstab<<-EOF
$ISO $apache_dir/$name iso9660 defaults 0 0
EOF
fi
mount -a &>/dev/null
cp $apache_dir/$name/images/pxeboot/initrd.img $tftp_dir
cp $apache_dir/$name/images/pxeboot/vmlinuz $tftp_dir
grub2-mkstandalone -d /usr/lib/grub/x86_64-efi/ -O x86_64-efi --modules="tftp net efinet linux part_gpt efifwsetup" -o $tftp_dir/bootx64.efi &>/dev/null
if [ ! -f $tftp_dir/bootx64.efi ];then
exit 2
fi
cp $apache_dir/$name/EFI/BOOT/grub.cfg $tftp_dir
if [ $? -eq 0 ];then
cat >$tftp_dir/grub.cfg<<-EOF
set default="0"
function load_video {
insmod efi_gop
insmod efi_uga
insmod video_bochs
insmod video_cirrus
insmod all_video
}
load_video
set gfxpayload=keep
insmod net
insmod efinet
insmod tftp
insmod gzio
insmod part_gpt
insmod ext2
set timeout=60
### END /etc/grub.d/00_header ###
search --no-floppy --set=root -l 'CentOS 7 x86_64'
### BEGIN /etc/grub.d/10_linux ###
menuentry 'Install CentOS 7' --class fedora --class gnu-linux --class gnu --class os {
linuxefi (tftp)/vmlinuz inst.repo=http://${host_ip}/centos ks=http://${host_ip}/ks.cfg ip=dhcp
initrdefi (tftp)/initrd.img
}
EOF
fi
systemctl start httpd
if [ $? -eq 0 ];then
echo "httpd service is up!"
systemctl enable httpd &>/dev/null
else
echo "httpd service is failed......"
fi
;;
c)
;;
*)
menu
esac
echo “please put the ks.cfg and ip.txt on $apache_dir catalogue !”
echo “finish… !”