shell脚本一键装机(pxe配合kickstart无人值守)
shell脚本实现无人值守安装
图文详细步骤可以参前期博文:
vim pxe.sh
#!/bin/bash
#安装并启用 TFTP 服务
rpm -qc tftp-server.x86_64
if [ $? -ne 0 ]
then
yum install -y tftp-server.x86_64 &> /dev/null
fi
rpm -qc xinetd
if [ $? -ne 0 ]
then
yum -y install xinetd &>/dev/null
fi
sed -i 's/yes/no/g' /etc/xinetd.d/tftp
systemctl start tftp
systemctl enable tftp
systemctl start xinetd
systemctl enable xinetd
#安装并启用 DHCP 服务
yum -y install dhcp &>/dev/null
cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example /etc/dhcp/dhcpd.conf
echo 'ddns-update-style none;
next-server 192.168.126.20;
filename "pxelinux.0";' >> /etc/dhcp/dhcpd.conf
echo 'subnet 192.168.126.0 netmask 255.255.255.0 {
range 192.168.126.100 192.168.126.200;
option routers 192.168.126.20;
}' >> /etc/dhcp/dhcpd.conf
systemctl start dhcpd
systemctl enable dhcpd
#准备 Linux 内核、初始化镜像文件
df -h | grep sr0
if [ $? -eq 0 ]
then
echo "光盘镜像已挂在"
else
mount /dev/cdrom /mnt
fi
cp /mnt/images/pxeboot/vmlinuz /var/lib/tftpboot
cp /mnt/images/pxeboot/initrd.img /var/lib/tftpboot
#准备 PXE 引导程序
yum -y install syslinux &>/dev/null
cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/
#安装FTP服务,准备CentOS 7 安装源
yum -y install vsftpd &>/dev/null
mkdir /var/ftp/centos7
cp -rf /mnt/* /var/ftp/centos7/
systemctl start vsftpd
systemctl enable vsftpd
#配置启动菜单文件
mkdir /var/lib/tftpboot/pxelinux.cfg
touch /var/lib/tftpboot/pxelinux.cfg/default
cat > /var//lib/tftpboot/pxelinux.cfg/default <
default auto
prompt 0
label auto
kernel vmlinuz
append initrd=initrd.img method=ftp://192.168.126.20/centos7 ks=ftp://192.168.126.20/ks.cfg
label linux text
kernel vmlinuz
append text initrd=initrd.img method=ftp://192.168.126.20/centos7
label linux rescue
kernel vmlinuz
append rescue initrd=initrd.img method=ftp://192.168.126.20/centos7
EOF
#关闭防火墙,验证PXE安装
systemctl stop firewalld.service
setenforce 0
yum install -y system-config-kickstart &>/dev/null
cp /root/ks.cfg /var/ftp/ks.cfg
/root/ks.cfg的内容
#platform=x86, AMD64, 或 Intel EM64T
#version=DEVEL
# Install OS instead of upgrade
install
# Keyboard layouts
keyboard 'us'
# Root password
rootpw --iscrypted $1$W.DC43qQ$xKFG9eiHd9shGDTYvzucb.
# Use network installation
url --url="ftp://192.168.126.20/cenos7"
# 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 --enforcing
# Network information
network --bootproto=dhcp --device=ens33
# Halt after installation
halt
# 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 /home --fstype="xfs" --size=4096
part / --fstype="xfs" --grow --size=1
%post --interpreter=/bin/bash
rm -rf /etc/yum.repos.d/*
echo '[local]
name=local
baseurl=ftp://192.168.126.20/centos7
enabled=1
gpgcheck=0’ > /etc/yum.repos.d/local.repo
%end
%packages
@^gnome-desktop-environment
@base
@core
@desktop-debugging
@dial-up
@directory-client
@fonts
@gnome-desktop
@guest-agents
@guest-desktop-agents
@input-methods
@internet-browser
@java-platform
@multimedia
@network-file-system-client
@networkmanager-submodules
@print-client
@x11
chrony
kexec-tools
%end