pxe配合kickstart无人值守批量装机
图文详解部分请看PXE配合Kickstart无人值守——批量装机简单如喝水(详细)
一、shell脚本一键桌面化装机
#!/bin/bash
echo -e "\033[31m =====正在验证当前为仅主机还是NAT模式===== \033[0m"
ping -c1 -W1 www.baidu.com &> /dev/null
if [ $? -eq 0 ];then echo -e "\033[31m 检测当前为NAT模式,为您配置在线yum源 \033[0m"
mkdir -p /etc/yum.repos.d/repo.bak
mv -f /etc/yum.repos.d/* /etc/yum.repos.d/repo.bak &> /dev/null
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo &> /dev/null
yum clean all &> /dev/null
yum list &> /dev/null
echo -e "\033[31m 在线源已配置完成 \033[0m"
else
echo -e "\033[31m 检测当前为仅主机模式,为您配置本地yum源 \033[0m"
mount /dev/sr0 /mnt &> /dev/null
cd /etc/yum.repos.d/
mkdir -p /etc/yum.repos.d/repo.bak
mv -f /etc/yum.repos.d/* /etc/yum.repos.d/repo.bak &> /dev/null
echo '[local]
name=local
baseurl=file:///mnt
enabled=1
gpgcheck=0' > /etc/yum.repos.d/local.repo
yum clean all &> /dev/null
yum makecache &> /dev/null
df -h | grep "/mnt"
if [ $? -ne 0 ];then
echo -e "\033[31m 检测当前为仅主机模式,但光盘未连接! \033[0m"
else
echo -e "\033[31m 本地yum源已配置完成 \033[0m"
fi
fi
#安装并启动TFTP:tftp-server xinetd
yum -y install tftp-server xinetd &> /dev/null
#修改tftp服务的配置文件:/etc/xinetd.d/tftp
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
#修改dhcp配置文件
cat /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example > /etc/dhcp/dhcpd.conf
echo 'ddns-update-style none;
next-server 192.168.184.30;
filename "pxelinux.0";' >> /etc/dhcp/dhcpd.conf
echo 'subnet 192.168.184.0 netmask 255.255.255.0 {
range 192.168.184.100 192.168.184.200;
option routers 192.168.184.30;
}' >> /etc/dhcp/dhcpd.conf
systemctl restart dhcpd
systemctl enable dhcpd
#准备linux内核、初始化镜文件
cp /mnt/images/pxeboot/initrd.img /var/lib/tftpboot
cp /mnt/images/pxeboot/vmlinuz /var/lib/tftpboot
#准备pxe引导程序
yum -y install syslinux &> /dev/null
cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/
#安装ftp服务,准备centos7安装源
yum -y install vsftpd &> /dev/null #安装vsftpd服务
mkdir /var/ftp/centos7 #在ftp根目录下创建目录centos7
cp -rf /mnt/* /var/ftp/centos7/ #将镜像文件强制复制到centos7目录中,可加&让它自己后台运行
systemctl start vsftpd #开启vsftpd服务
systemctl enable vsftpd #开启vsftpd服务开机自启
mkdir /var/lib/tftpboot/pxelinux.cfg
touch /var/lib/tftpboot/pxelinux.cfg/default
echo 'default auto
prompt 1
label auto
kernel vmlinuz
append initrd=initrd.img method=ftp://192.168.184.30/centos7
label linux text
kernel vmlinuz
append text initrd=initrd.img method=ftp://192.168.184.30/centos7
label linux rescue
kernel vmlinuz
append rescue initrd=initrd.img method=ftp://192.168.184.30/centos7' > /var/lib/tftpboot/pxelinux.cfg/default
systemctl stop firewalld.service
setenforce 0
echo "pxe配置完成"
echo "即将配置kickstart"
#安装system-config-kickstart 工具
yum install -y system-config-kickstart
cp /root/ks.cfg /var/ftp/ks.cfg
sed -i '6d' /var/lib/tftpboot/pxelinux.cfg/default
i=(append initrd=initrd.img method=ftp://192.168.184.30/centos7 ks=ftp://192.168.184.30/ks.cfg)
sed -i "5a `echo ${i[*]}`" /var/lib/tftpboot/pxelinux.cfg/default
sed -i 's/prompt 1/prompt 0/' /var/lib/tftpboot/pxelinux.cfg/default
/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$nqSbw4W/$IrR8rQYA5xOhLzPrybobg1
# Use network installation
url --url="ftp://192.168.184.30/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 / --fstype="xfs" --size=4096
part swap --fstype="swap" --size=4096
%post --interpreter=/bin/bash
rm -rf /etc/yum.repos.d/*
echo '[local]
name=local
baseurl=ftp://192.168.184.30/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