此脚本在centos6、centos7、openeuler20.03、openeuler22.03测试成功。
1 编辑脚本文件pxeinstall.sh
#!/bin/bash
systemctl > /dev/null 2>&1
[ $? == 0 ] && systemctl stop firewalld || service iptables stop
[ $? == 0 ] && echo "防火墙已关闭" || echo "防火墙未关闭"
selinux_status=`getenforce`
if [[ $selinux_status = Disabled ]]
then
echo "selinux已关闭"
continue
else
echo "selinux未关闭"
setenforce 0
if [ $? == 0 ]
then
echo "selinux已关闭"
fi
fi
sed -i 's/^ *SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config # 重启后永久关闭
[ $? == 0 ] && echo "selinux已永久关闭" || echo "请检查selinux配置文件"
cd /etc/yum.repos.d
mount /dev/cdrom /mnt
if [ ! -e local.repo ]
then
mkdir repos.bak
mv *.repo repos.bak
echo '[local]
name=local
baseurl=file:///mnt
gpgcheck=0
enabled=1' > local.repo
fi
[ -e local.repo ] && echo "yum源设置完成" || echo "请检查配置"
yum clean all && yum makecache > /dev/null
[ $? == 0 ] && echo "下载装机需要的软件包" || echo "请检查包是否存在"
yum install -y dhcp tftp-server httpd syslinux xinetd > /dev/null
cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/
cp /usr/share/syslinux/menu.c32 /var/lib/tftpboot/
cp /mnt/isolinux/* /var/lib/tftpboot/
mkdir /var/lib/tftpboot/pxelinux.cfg
version=$1
server_ip=$2
nic=$3
systemctl > /dev/null 2>&1
if [ $? == 0 ]
then
echo "default vesamenu.c32
timeout 30
menu title iSoft-Taiji Server OS $version
label linux text
menu label ^Install iSoft-Taiji Server OS $version
menu default
kernel vmlinuz
append initrd=initrd.img inst.stage2=http://$server_ip/pub inst.ks=http://$server_ip/ks.cfg
" > /var/lib/tftpboot/pxelinux.cfg/default
else
echo "default auto
prompt 0
menu title iSoft-Taiji Server OS $version
label auto
menu label ^Install iSoft-Taiji Server OS $version
kernel vmlinuz
append initrd=initrd.img ks=http://$server_ip/ks.cfg devfs=nomount
" > /var/lib/tftpboot/pxelinux.cfg/default
fi
sed -i -e "10 s/yes/no/" -e "14 s/yes/no/" /etc/xinetd.d/tftp
systemctl > /dev/null 2>&1
[ $? == 0 ] && systemctl start xinetd || service xinetd start
systemctl > /dev/null 2>&1
[ $? == 0 ] && systemctl start tftp
echo "option domain-name \"example.org\";
option domain-name-servers 8.8.8.8, 114.114.114.114;
default-lease-time 84600;
max-lease-time 100000;
log-facility local7;
subnet 192.168.241.0 netmask 255.255.255.0 {
range 192.168.241.100 192.168.241.127;
option routers 192.168.241.2;
next-server $server_ip; # 本机ip(tftpserver的ip)
filename \"pxelinux.0\";
}" > /etc/dhcp/dhcpd.conf
systemctl > /dev/null 2>&1
[ $? == 0 ] && systemctl start dhcpd || service dhcpd start
[ $? == 0 ] && echo "dhcp服务正常重启" || echo "dhcp启动异常,请查看配置文件"
mkdir -p /var/www/html/pub
umount /mnt
mount -o loop /dev/cdrom /var/www/html/pub
echo "# Keyboard layouts
keyboard 'us'
# System language
lang zh_CN.UTF-8
# System timezone
timezone Asia/Shanghai
# Firewall configuration
firewall --disabled
# SELinux configuration
selinux --disabled
# Use graphical install
text
firstboot --disable
# Install OS instead of upgrade
# openeuler22.03需要注释install这一行
install
# System bootloader configuration
bootloader --location=mbr
# Clear the Master Boot Record
zerombr
# Partition clearing information
clearpart --all --initlabel
# Disk partitioning information
part /boot --fstype="ext4" --ondisk=sda --size=500
part pv.01 --fstype="lvmpv" --ondisk=sda --size=1 --grow
volgroup iso --pesize=4096 pv.01
logvol swap --fstype="swap" --size=1024 --name=swap --vgname=iso
logvol / --fstype="ext4" --grow --size=1 --name=root --vgname=iso
#格式化sdb盘
#part /sdb1 --fstype='ext4' --ondisk=sdb --size=1 --grow
# Network information
network --bootproto=dhcp --device=$nic --activate
network --hostname=localhost.localdomain
# Use network installation
url --url='http://$server_ip/pub'
# Root password
rootpw --plaintext abc123
# System authorization information
# openeuler22.03需要注释auth这一行
auth --useshadow --passalgo=sha512
#create a user
#user --name=isoft --gecos=isoft --password=abc123 --plaintext
%packages
@base
#@^graphical-server-environment
net-tools
@core
%end
# Reboot after installation
reboot
" > /var/www/html/ks.cfg
systemctl > /dev/null 2>&1
[ $? == 0 ] && systemctl start httpd || service httpd start
[ $? == 0 ] && echo "httpd服务正常重启" || echo "httpd启动异常,请查看配置文件"
2 执行脚本,脚本的执行需要接三个参数,分别是系统的版本,服务端ip,为系统设置的网卡名
sh pxeinstall.sh centos7(系统版本) 192.168.241.241(服务端ip) ens33/eth0(网卡名)