在 CentOS 8 上实现PXE自动化安装 CentOS 6,7,8

Posted 凭栏听枫

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在 CentOS 8 上实现PXE自动化安装 CentOS 6,7,8相关的知识,希望对你有一定的参考价值。

1 安装前准备

关闭防火墙和SELINUX,DHCP服务器静态IP
网络要求:关闭Vmware软件中的DHCP服务,基于NAT模式
注意:使用 1G 以下内存的主机安装CentOS 7,8 会提示空间不足,建议2G以上

2 安装相关软件包并启动

[root@centos8 ~]#dnf -y install dhcp-server tftp-server httpd syslinux-
nonlinux(或者syslinux-tftpboot)
[root@centos8 ~]#systemctl enable --now httpd tftp dhcpd

3 配置DHCP服务

[root@centos8 ~]#cp /usr/share/doc/dhcp-server/dhcpd.conf.example
/etc/dhcp/dhcpd.conf
[root@centos8 ~]#vim /etc/dhcp/dhcpd.conf
option domain-name "example.com";
option domain-name-servers 180.76.76.76,223.6.6.6;
default-lease-time 600;
max-lease-time 7200;
log-facility local7;
subnet 10.0.0.0 netmask 255.255.255.0 {
range 10.0.0.100 10.0.0.200;
option routers 10.0.0.1;
next-server 10.0.0.208;
filename "pxelinux.0";
}
[root@centos8 ~]#systemctl start dhcpd

4 准备yum源和相关目录

[root@centos8 ~]#mkdir -pv /var/www/html/centos/{6,7,8}/os/x86_64/
[root@centos8 ~]#mount /dev/sr0 /var/www/html/centos/6/os/x86_64/
[root@centos8 ~]#mount /dev/sr1 /var/www/html/centos/7/os/x86_64/
[root@centos8 ~]#mount /dev/sr2 /var/www/html/centos/8/os/x86_64/

5 准备kickstart文件

[root@centos8 ~]#mkdir /var/www/html/ks/
[root@centos8 ~]#vim /var/www/html/ks/centos6.cfg 
install
text
reboot
url --url=http://10.0.0.208/centos/6/os/x86_64/
lang en_US.UTF-8
keyboard us
network --onboot yes --device eth0 --bootproto dhcp --noipv6
rootpw --iscrypted $6$Gq/zxpJZAQEcdqxa$FC0QXQM4DOs1RPREjDPwEP4fKsQr3ksVSc4vb2YQDoEixemaR95NxX5BU1otCFH8tSH0tUn.S3CqFSHE4pwIq/
firewall --disabled
authconfig --enableshadow --passalgo=sha512
selinux --disabled
timezone Asia/Shanghai
bootloader --location=mbr --driveorder=sda --append="crashkernel=auto rhgb quiet"
zerombr
clearpart --all --initlabel
part /boot --fstype=ext4 --size=1024
part / --fstype=ext4 --size=102400
part /data --fstype=ext4 --size=51200
part swap --size=2048
%packages
@core
@server-policy
@workstation-policy
autofs
vim-enhanced
%end
%post
useradd zhang
echo zhang123 | passwd --stdin zhang &> /dev/null
mkdir /etc/yum.repos.d/bak
mv /etc/yum.repos.d/* /etc/yum.repos.d/bak
cat > /etc/yum.repos.d/base.repo <<EOF
[base]
name=base
baseurl=file:///misc/cd
gpgcheck=0
EOF
%end

[root@centos8 ~]#vim /var/www/html/ks/centos7.cfg 
#platform=x86, AMD64, or Intel EM64T
#version=DEVEL
# Install OS instead of upgrade
install
# Keyboard layouts
keyboard 'us'
# Root password
rootpw --plaintext 123456
# System language
lang en_US
# System authorization information
auth  --useshadow  --passalgo=sha512
# Use text mode install
text
firstboot --disable
# SELinux configuration
selinux --disabled
# Firewall configuration
firewall --disabled
# Network information
network  --bootproto=dhcp --device=eth0
# Reboot after installation
reboot
# System timezone
timezone Africa/Abidjan
# Use network installation
url --url="http://10.0.0.208/centos/7/os/x86_64/"
# System bootloader configuration
bootloader --append="net.ifnames=0" --location=mbr
# Clear the Master Boot Record
zerombr
# Partition clearing information
clearpart --all --initlabel
# Disk partitioning information
part / --fstype="xfs" --size=100000
part /boot --fstype="xfs" --size=1024
part swap --fstype="swap" --size=2048
part /data --fstype="xfs" --size=50000

%post
mkdir /etc/yum.repos.d/Centos
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/Centos
cat > /etc/yum.repos.d/yum.repo <<EOF
[BaseOS]
name=BaseOS
baseurl=http://10.0.0.208/centos/\\$releasever/os/\\$basearch/
	https://mirrors.aliyun.com/centos/\\$releasever/os/\\$basearch/
        https://mirrors.huaweicloud.com/centos/\\$releasever/os/\\$basearch/
        https://mirrors.cloud.tencent.com/centos/\\$releasever/os/\\$basearch/
        https://mirrors.tuna.tsinghua.edu.cn/centos/\\$releasever/os/\\$basearch/
        http://mirrors.163.com//centos/\\$releasever/os/\\$basearch/
        http://mirrors.sohu.com/centos/\\$releasever/os/\\$basearch/
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial

[EPEL]
name=EPEL
baseurl=https://mirrors.aliyun.com/epel/\\$releasever/\\$basearch/
        https://mirrors.huaweicloud.com/epel/\\$releasever/\\$basearch/
        https://mirrors.cloud.tencent.com/epel/\\$releasever/\\$basearch/
        https://mirrors.tuna.tsinghua.edu.cn/epel/\\$releasever/\\$basearch/
enabled=1
gpgcheck=0
gpgkey=https://mirrors.aliyun.com/epel/RPM-GPG-KEY-EPEL-\\$releasever

[extras]
name=extras
baseurl=https://mirrors.aliyun.com/centos/\\$releasever/extras/\\$basearch/
        https://mirrors.huaweicloud.com/centos/\\$releasever/extras/\\$basearch/
        https://mirrors.cloud.tencent.com/centos/\\$releasever/extras/\\$basearch/
        https://mirrors.tuna.tsinghua.edu.cn/centos/\\$releasever/extras/\\$basearch/
        http://mirrors.163.com/centos/\\$releasever/extras/\\$basearch/
        http://mirrors.sohu.com/centos/\\$releasever/extras/\\$basearch/
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial


[centosplus]
name=centosplus
baseurl=https://mirrors.aliyun.com/centos/\\$releasever/centosplus/\\$basearch/
        https://mirrors.huaweicloud.com/centos/\\$releasever/centosplus/\\$basearch/
        https://mirrors.cloud.tencent.com/centos/\\$releasever/centosplus/\\$basearch/
        https://mirrors.tuna.tsinghua.edu.cn/centos/\\$releasever/centosplus/\\$basearch/
        http://mirrors.163.com/centos/\\$releasever/centosplus/\\$basearch/
        http://mirrors.sohu.com/centos/\\$releasever/centosplus/\\$basearch/
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
EOF

%end
%packages
@web-server
gcc 
make 
autoconf 
gcc-c++ 
glibc 
glibc-devel 
pcre 
pcre-devel 
openssl 
openssl-devel 
systemd-devel 
zlib-devel 
vim 
lrzsz 
tree 
tmux 
lsof 
tcpdump 
wget 
net-tools 
iotop 
bc 
bzip2 
zip 
unzip 
nfs-utils 
man-pages 
bash-completion
%end

[root@centos8 ~]#vim /var/www/html/ks/centos8.cfg
ignoredisk --only-use=sda
zerombr
text
reboot
clearpart --all --initlabel
selinux --disabled
firewall --disabled
url --url=http://10.0.0.208/centos/8/os/x86_64/
keyboard --vckeymap=us --xlayouts='us'
lang en_US.UTF-8
network --bootproto=dhcp --device=eth0 --ipv6=auto --activate
bootloader --append="net.ifnames=0" --location=mbr --boot-drive=sda
network --hostname=centos8.3
rootpw --iscrypted $6$Gq/zxpJZAQEcdqxa$FC0QXQM4DOs1RPREjDPwEP4fKsQr3ksVSc4vb2YQDoEixemaR95NxX5BU1otCFH8tSH0tUn.S3CqFSHE4pwIq/
firstboot --enable
skipx
services --disabled="chronyd"
timezone Asia/Shanghai --isUtc --nontp
user --name=zhang --password=$6$9ILNKCHtg9YOb4sw$4YxLRVuEgkJYEP7bTvmjkk7BC9iS35ZLb7n9AZdvIBdogr5yc9tv2HV8sSwBxj.4ORFsOCfu0DNMQZNkohYwF1 --iscrypted --gecos="zhang"
#autopart --type=lvm
#part / --fstype xfs --size 1 --grow --ondisk sda 可以实现根自动使用所有剩余空间
part / --fstype="xfs" --ondisk=sda --size=102400
part /data --fstype="xfs" --ondisk=sda --size=51200
part swap --fstype="swap" --ondisk=sda --size=2048
part /boot --fstype="ext4" --ondisk=sda --size=1024
%packages
@^minimal-environment
kexec-tools
%end
%addon com_redhat_kdump --enable --reserve-mb='auto'
%end
%anaconda
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
%end

6 准备PXE启动相关文件

[root@centos8 ~]#mkdir /var/lib/tftpboot/centos{6,7,8}
#准备CentOS6,7,8各自的内核相关文件
[root@centos8 ~]#cp
/var/www/html/centos/6/os/x86_64/isolinux/{vmlinuz,initrd.img} /var/lib/tftpboot/centos6
[root@centos8 ~]#cp
/var/www/html/centos/7/os/x86_64/isolinux/{vmlinuz,initrd.img} /var/lib/tftpboot/centos7
[root@centos8 ~]#cp
/var/www/html/centos/8/os/x86_64/isolinux/{vmlinuz,initrd.img} /var/lib/tftpboot/centos8
[root@centos8 ~]#cp /usr/share/syslinux/{pxelinux.0,menu.c32} /var/lib/tftpboot/
#以下三个文件是CentOS8安装所必须文件,CentOS6,7则不需要
[root@centos8 ~]#cp
/var/www/html/centos/8/os/x86_64/isolinux/{ldlinux.c32,libcom32.c32,libutil.c32}
/var/lib/tftpboot/
#生成安装菜单文件
[root@centos8 ~]#mkdir /var/lib/tftpboot/pxelinux.cfg/
[root@centos8 ~]#cp /var/www/html/centos/8/os/x86_64/isolinux/isolinux.cfg
/var/lib/tftpboot/pxelinux.cfg/default
#最终目录结构如下
[root@centos8 ~]#tree /var/lib/tftpboot
.
├── centos6
│  ├── initrd.img
│  └── vmlinuz
├── centos7
│  ├── initrd.img
│  └── vmlinuz
├── centos8
│  ├── initrd.img
│  └── vmlinuz
├── ldlinux.c32
├── libcom32.c32
├── libutil.c32
├── menu.c32
├── pxelinux.0
└── pxelinux.cfg
 └── default
4 directories, 12 files

7 准备启动菜单文件

[root@centos8 ~]# vim /var/lib/tftpboot/pxelinux.cfg/default
default menu.c32                                                                                               
timeout 600 

menu title Install CentOS Linux

label linux8
    menu label Auto Install CentOS Linux ^8
    kernel centos8/vmlinuz
    append initrd=centos8/initrd.img ks=http://10.0.0.208/ks/centos8.cfg

label linux7
    menu label Auto Install CentOS Linux ^7
    kernel centos7/vmlinuz
    append initrd=centos7/initrd.img ks=http://10.0.0.208/ks/centos7.cfg

label linux6
    menu label Auto Install CentOS Linux ^6
    kernel centos6/vmlinuz
    append initrd=centos6/initrd.img ks=http://10.0.0.208/ks/centos6.cfg

label manual
    menu label ^Manual Install CentOS Linux 8.0 
    kernel centos8/vmlinuz
    append initrd=centos6/initrd.img ks=http://10.0.0.208/ks/centos6.cfg

label manual
    menu label ^Manual Install CentOS Linux 8.0
    kernel centos8/vmlinuz
    append initrd=centos8/initrd.img
    inst.repo=http://10.0.0.208/centos/8/os/x86_64/

label rescue
    menu label ^Rescue a CentOS Linux system 8
    kernel centos8/vmlinuz
    append initrd=centos8/initrd.img
    inst.repo=http://10.0.0.208/centos/8/os/x86_64/ rescue

label local
    menu default
    menu label Boot from ^local drive
    localboot 0xffff 

8 测试客户端基于PXE实现自动安装

新准备一台主机,设置网卡引导,可看到看启动菜单,并实现自动安装
注意:VMware workstation 对于不同的CentOS 版本,生成的虚拟机的硬件并不兼容

以上是关于在 CentOS 8 上实现PXE自动化安装 CentOS 6,7,8的主要内容,如果未能解决你的问题,请参考以下文章

在centos最小安装系统上实现邮件的发送

cobbler实现自动化安装centos7,8

cobbler实现自动化安装centos7,8

cobbler实现自动化安装centos7,8

利用PXE自动安装

在Centos7上实现手工编译安装mysql5.7