Shell 脚本实现PXE装机及Kickstart 无人值守自动安装

目录

 PXE装机

Kickstart 无人值守


本机IP:192.168.142.22

本机网关:192.168.142.2

PXE装机

systemctl stop firewalld
setenforce 0
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
yum clean all && yum makecache
yum -y install tftp-server xinetd dhcp syslinux vsftpd
sed -i -e "10 s/yes/no/" -e "14 s/yes/no/" /etc/xinetd.d/tftp
systemctl start tftp
systemctl enable tftp
systemctl start xinetd
systemctl enable xinetd
cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example /etc/dhcp/dhcpd.conf
sed -i -e "32 s/10.254.239.0/192.168.142.0/" -e "32 s/224/0/" /etc/dhcp/dhcpd.conf
sed -i -e "33 s/10.254.239.10/192.168.142.100/" -e "33 s/10.254.239.20/192.168.142.200/" /etc/dhcp/dhcpd.conf
sed -i "34c option routers 192.168.142.22;" /etc/dhcp/dhcpd.conf
sed -i "14 s/^#//" /etc/dhcp/dhcpd.conf
sed -i "14a next-server 192.168.142.22;" /etc/dhcp/dhcpd.conf
sed -i "15a filename \"pxelinux.0\";" /etc/dhcp/dhcpd.conf
systemctl start dhcpd
systemctl enable dhcpd
cp /mnt/images/pxeboot/vmlinuz /var/lib/tftpboot/
cp /mnt/images/pxeboot/initrd.img /var/lib/tftpboot/
cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/
mkdir /var/ftp/centos7
cp -rf /mnt/* /var/ftp/centos7/
systemctl start vsftpd
systemctl enable vsftpd
mkdir /var/lib/tftpboot/pxelinux.cfg
echo "default auto
prompt 0
label auto
kernel vmlinuz
append initrd=initrd.img method=ftp://192.168.142.22/centos7

label linux text
kernel vmlinuz
append text initrd=initrd.img method=ftp://192.168.142.22/centos7

label linux rescue
kernel vmlinuz
append rescue initrd=initrd.img method=ftp://192.168.142.22/centos7
" > /var/lib/tftpboot/pxelinux.cfg/default

Kickstart 无人值守

systemctl stop firewalld
setenforce 0
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
yum clean all && yum makecache
yum -y install tftp-server xinetd dhcp syslinux vsftpd
sed -i -e "10 s/yes/no/" -e "14 s/yes/no/" /etc/xinetd.d/tftp
systemctl start tftp
systemctl enable tftp
systemctl start xinetd
systemctl enable xinetd
cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example /etc/dhcp/dhcpd.conf
sed -i -e "32 s/10.254.239.0/192.168.142.0/" -e "32 s/224/0/" /etc/dhcp/dhcpd.conf
sed -i -e "33 s/10.254.239.10/192.168.142.100/" -e "33 s/10.254.239.20/192.168.142.200/" /etc/dhcp/dhcpd.conf
sed -i "34c option routers 192.168.142.22;" /etc/dhcp/dhcpd.conf
sed -i "14 s/^#//" /etc/dhcp/dhcpd.conf
sed -i "14a next-server 192.168.142.22;" /etc/dhcp/dhcpd.conf
sed -i "15a filename \"pxelinux.0\";" /etc/dhcp/dhcpd.conf
systemctl start dhcpd
systemctl enable dhcpd
cp /mnt/images/pxeboot/vmlinuz /var/lib/tftpboot/
cp /mnt/images/pxeboot/initrd.img /var/lib/tftpboot/
cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/
mkdir /var/ftp/centos7
cp -rf /mnt/* /var/ftp/centos7/
systemctl start vsftpd
systemctl enable vsftpd
mkdir /var/lib/tftpboot/pxelinux.cfg
echo "default auto
prompt 0
label auto
kernel vmlinuz
append initrd=initrd.img method=ftp://192.168.142.22/centos7 ks=ftp://192.168.142.22/ks.cfg

label linux text
kernel vmlinuz
append text initrd=initrd.img method=ftp://192.168.142.22/centos7

label linux rescue
kernel vmlinuz
append rescue initrd=initrd.img method=ftp://192.168.142.22/centos7
" > /var/lib/tftpboot/pxelinux.cfg/default
yum -y install system-config-kickstart.noarch
echo '#platform=x86, AMD64, or Intel EM64T
#version=DEVEL
# Install OS instead of upgrade
install
# Keyboard layouts
keyboard 'us'
# Root password
rootpw --iscrypted $1$azNge1rF$GqkP/j9OnkIqecxdU78wT1
# Use network installation
url --url="ftp://192.168.142.22/centos7"
# System language
lang zh_CN
# Firewall configuration
firewall --disabled
# System authorization information
auth  --useshadow  --passalgo=sha512
# Use graphical install
graphical
firstboot --disable
# SELinux configuration
selinux --disabled

# Network information
network  --bootproto=dhcp --device=ens33
# Reboot after installation
reboot
# System timezone
timezone Asia/Shanghai
# System bootloader configuration
bootloader --location=mbr
# Clear the Master Boot Record
zerombr
# Partition clearing information
clearpart --all --initlabel
# Disk partitioning information
part /boot --fstype="xfs" --size=500
part swap --fstype="swap" --size=4096
part / --fstype="xfs" --grow --size=1

%post --interpreter=/bin/bash
mkdir /etc/yum.repos.d/repo.bak
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/repo.bak
echo '[local]
name=local
baseurl=ftp://192.168.142.22/centos7
gpgcheck=0
enabled=1' > /etc/yum.repos.d/local.repo
%end

%packages
@base
@core
@desktop-debugging
@dial-up
@directory-client
@fonts
@gnome-desktop
@guest-desktop-agents
@input-methods
@internet-browser
@java-platform
@multimedia
@network-file-system-client
@print-client
@x11
binutils
chrony
ftp
gcc
kernel-devel
kexec-tools
make
open-vm-tools
patch
python

%end' > /var/ftp/ks.cfg

你可能感兴趣的:(运维,linux,服务器)