You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
shell-project/os/get-pxe.sh

150 lines
4.2 KiB

2 years ago
#!/usr/bin/bash
color(){
unset c
declare -A c=([Error]=31 [Success]=32 [Warning]=33 [Info]=34)
#echo -e "\033[${c[$1]}m[`date +%T`]($1) $2\033[0m"
printf "\033[${c[$1]}m%-10s%-10s %-30s\033[0m\n" "[`date +%T`]" "($1)" "$2"
sleep 0.5
}
# pxe服务器初始化
color Info "pxe服务器初始化"
curl -s -L https://download.beyourself.org.cn/shell-project/os/get-os-init.sh | sh
# 安装dhcp、tftp、tftp-server、syslinux、wget、vsftpd、pykickstart
color Info "安装pxe服务依赖"
yum install -y dhcp tftp tftp-server syslinux wget vsftpd pykickstart
# 配置dhcp
color Info "配置dhcp"
ipaddr=$(ip -4 -f inet a show dev ens33 | awk '/inet/{print $2}')
2 years ago
gateway=$(ip r show dev ens33 | awk '/default via/{print $3}')
2 years ago
cat > /etc/dhcp/dhcpd.conf << EOF
ddns-update-style interim;
ignore client-updates;
authoritative;
allow booting;
allow bootp;
allow unknown-clients;
# A slightly different configuration for an internal subnet.
subnet ${ipaddr%.*}.0 netmask 255.255.255.0
{
range ${ipaddr%.*}.100 ${ipaddr%.*}.200;
2 years ago
option domain-name-servers ${gateway};
2 years ago
option domain-name "server1.example.com";
2 years ago
option routers ${gateway};
2 years ago
option broadcast-address ${ipaddr%.*}.255;
default-lease-time 600;
max-lease-time 7200;
# PXE SERVER IP
next-server ${ipaddr%/*}; # DHCP server ip
filename "pxelinux.0";
}
EOF
# 配置tftp
color Info "配置tftp"
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 /tftpboot
disable = no
per_source = 11
cps = 100 2
flags = IPv4
}
EOF
# 配置vsftpd
color Info "配置vsftpd"
cp /usr/share/syslinux/{pxelinux.0,menu.c32,memdisk,mboot.c32,chain.c32} /var/lib/tftpboot/
mkdir /var/lib/tftpboot/pxelinux.cfg
mkdir /var/lib/tftpboot/netboot
mount /dev/cdrom /mnt
cp -rvf /mnt/* /var/ftp/pub
cp /var/ftp/pub/images/pxeboot/vmlinuz /var/lib/tftpboot/netboot/
cp /var/ftp/pub/images/pxeboot/initrd.img /var/lib/tftpboot/netboot/
cat > /var/ftp/pub/ks.cfg <<EOF
#platform=x86, AMD64, or Intel EM64T
#version=DEVEL
# Firewall configuration
firewall --disabled
# Install OS instead of upgrade
install
# Use NFS installation media
url --url="ftp://${ipaddr%/*}/pub/"
rootpw --plaintext 123456
#root的密码设为123456
# Use graphical install
graphical
firstboot disable
# System keyboard
keyboard us
# System language
lang en_US
# SELinux configuration
selinux disabled
# Installation logging level
logging level=info
# System timezone
timezone Asia/Shanghai
# System bootloader configuration
bootloader location=mbr
clearpart --all --initlabel
part swap --asprimary --fstype="swap" --size=1024
part /boot --fstype xfs --size=200
part pv.01 --size=1 --grow
volgroup rootvg01 pv.01
logvol / --fstype xfs --name=lv01 --vgname=rootvg01 --size=1 --grow
reboot
%packages
@core
wget
lsof
net-tools
vim
%end
%post
2 years ago
sed -i 's/#UseDNS.*/UseDNS=no/g' /etc/ssh/sshd_config
2 years ago
echo "curl -s -L https://download.beyourself.org.cn/shell-project/os/get-os-init.sh | sh" >> /etc/rc.local
chmod a+x /etc/rc.local
2 years ago
#yum install -y python3 python3-devel gcc
#curl -s -L https://download.beyourself.org.cn/python-project/os/jumpserver-register.py -o /opt/jumpserver-register.py
#pip3 install requests httpsig -i https://mirrors.aliyun.com/pypi/simple
2 years ago
#python3 /opt/jumpserver-register.py \`ip -4 -f inet a show dev ens33 | awk '/inet/{print $2}' | cut -d'/' -f 1\` 192.168.96.180 47849e56-81b5-4fe6-a7d4-7d7578abfc32 ab17fa93-8f4f-4547-802f-be81a6197937 d6638212-8896-4596-9b30-ec0e5aa5905c 9ead5ec2-ba29-4b0a-b4d1-75657eec4306
2 years ago
2 years ago
%end
EOF
# 配置启动项
color Info "配置启动项"
cat >/var/lib/tftpboot/pxelinux.cfg/default <<EOF
default menu.c32
prompt 0
timeout 30
MENU TITLE Togogo.net Linux Training
LABEL centos7_x64
MENU LABEL CentOS 7 X64 for newrain
KERNEL /netboot/vmlinuz
APPEND initrd=/netboot/initrd.img inst.repo=ftp://${ipaddr%/*}/pub ks=ftp://${ipaddr%/*}/pub/ks.cfg
EOF
# 启动服务
color Info "启动服务"
systemctl enable dhcpd vsftpd tftp
2 years ago
systemctl restart dhcpd vsftpd tftp