0
点赞
收藏
分享

微信扫一扫

centos7 pxe {uefi,legacy} boot装机


pxe装机shell脚本,将脚本保存为pxe.sh,将ks.cfg、efi.ks.cfg文件与pxe.sh放在同一目录,挂载centos7安装光盘

#!/bin/bash
#pxe install script
#version:v1.0
#
if [ -f $PWD/ks.cfg ] && [ -f $PWD/efi.ks.cfg ]
then
echo "ks.cfg and efi.ks.cofg file exists!"
break
elif [ -f $PWD/ks.cfg ]
then
echo "ks.cfg file exists!"
break
elif [ -f $PWD/efi.ks.cfg ]
then
echo "efi.ks.cfg file exists!"
break
else
echo "efi.ks.cfg or ks.cfg file does not exist!"
exit
fi

##########function definition##########
#tftp file
TFTP_FILE(){
cat > $PWD/pxe_install/tftp << EOF
# default: off
# description: The tftp server serves files using the trivial file transfer \
# protocol. The tftp protocol is often used to boot diskless \
# workstations, download configuration files to network-aware printers, \
# and to start the installation process for some operating systems.
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
}

#default file
PXE_DEF(){
cat > $PWD/pxe_install/default << EOF
default linux text
prompt 0
label linux text
kernel vmlinuz
append initrd=initrd.img ip=dhcp inst.repo=http://$USE_IP/centos inst.ks=http://$USE_IP/centos/ks.cfg
EOF
}
#dhcpd.conf
DHCP_CONF(){
cat > $PWD/pxe_install/dhcpd.conf << EOF
# dhcpd.conf
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 $USE_SUBNET netmask $USE_NETMASK {
option routers $USE_IP;
range $START_IP $END_IP;

class "pxeclients" {
match if substring (option vendor-class-identifier, 0, 9) = "PXEClient";
next-server $USE_IP;

if option architecture-type = 00:07 {
filename "uefi/shim.efi";
} else {
filename "pxelinux/pxelinux.0";
}
}
}
EOF
}

#grub.conf
GRUB_CONF(){
cat > $PWD/pxe_install/grub.cfg << EOF
set timeout=5
menuentry 'Install CentOS 7 [UEFI]' {
linuxefi uefi/vmlinuz ip=dhcp inst.repo=http://$USE_IP/centos/ inst.ks=http://$USE_IP/centos/efi.ks.cfg
initrdefi uefi/initrd.img
}
EOF
}

#begin install
echo "Start the installation..."

#mount iso
umount /dev/cdrom &> /dev/null
mount /dev/cdrom /media &> /dev/null
if [ $? -eq 0 ]
then
echo "The disc was found"
ISO=/media
break
else
echo "The disc was not found"
exit
fi

mkdir $PWD/pxe_install
#connect internet
ping -c 4 baidu.com &> /dev/null
if [ $? -eq 0 ]
then
echo "connected internet"
break
else
echo "disconnected internet,check network"
exit
fi
read -p "input localhost ip and netmask(192.168.1.1/255.255.255.0): " IP_NETMASK
USE_IP=`echo "$IP_NETMASK" | awk -F/ '{print $1}'`
USE_NETMASK=`echo "$IP_NETMASK" | awk -F/ '{print $2}'`
read -p "input ip dhcp pool network address(192.168.1.0): " USE_SUBNET
read -p "input ip dhcp pool start ip(192.168.1.2): " START_IP
read -p "input ip dhcp pool end ip(192.168.1.254): " END_IP

yum install -y tftp-server dhcp syslinux httpd xinetd
TFTP_FILE
systemctl start tftp dhcpd httpd

#syslinux
mkdir -p /var/lib/tftpboot/{pxelinux,uefi}
cp -rf $ISO/images/pxeboot/{initrd.img,vmlinuz} /var/lib/tftpboot/pxelinux
cp -rf $ISO/images/pxeboot/{initrd.img,vmlinuz} /var/lib/tftpboot/uefi

cp -rf $ISO/Packages/shim-* /tmp
cp -rf $ISO/Packages/grub2-efi-x64* /tmp
cd /tmp

rpm2cpio shim-* | cpio -dimv
rpm2cpio grub2-efi-x64*| cpio -dimv

cp -rf /tmp/boot/efi/EFI/centos/shim.efi /var/lib/tftpboot/uefi
cp -rf /tmp/boot/efi/EFI/centos/grubx64.efi /var/lib/tftpboot/uefi
cp -rf /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/pxelinux
chmod +r /var/lib/tftpboot/uefi -R
cd -

GRUB_CONF
cp -rf $PWD/pxe_install/grub.cfg /var/lib/tftpboot/uefi

PXE_DEF
mkdir -p /var/lib/tftpboot/pxelinux/pxelinux.cfg
cp -rf $PWD/pxe_install/default /var/lib/tftpboot/pxelinux/pxelinux.cfg

DHCP_CONF
cp -rf $PWD/pxe_install/dhcpd.conf /etc/dhcp/dhcpd.conf

mkdir -p /var/www/html/centos

if [ -f $PWD/ks.cfg ] && [ -f $PWD/efi.ks.cfg ]
then
cp -rf $PWD/{ks.cfg,efi.ks.cfg} /var/www/html/centos
elif [ -f $PWD/ks.cfg ]
then
cp -rf $PWD/ks.cfg /var/www/html/centos
elif [ -f $PWD/efi.ks.cfg ]
then
cp -rf $PWD/efi.ks.cfg /var/www/html/centos
else
exit
fi

cp -rf $ISO/* /var/www/html/centos
sed -i 's/^#\(ServerName\)/\1/' /etc/httpd/conf/httpd.conf
#services
systemctl daemon-reload
systemctl restart xinetd
systemctl enable xinetd
systemctl restart tftp
systemctl enable tftp
systemctl restart httpd
systemctl enable httpd
systemctl restart dhcpd
systemctl enable dhcpd
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
sed -i 's/^SELINUX=enforcing/SELINUX=disable/' /etc/selinux/config
rm -rf /tmp/*
umount /media

#end install
echo "The installation is complete"

exit



举报

相关推荐

0 条评论