pxe 自动化
【企业案例】
某公司新增一个机房,需要大规模部署操作系统环境,由于手动部署操作系统效率极为低下,现需搭建一台PXE服务器来自动化部署操作系统。
具体要求如下:
【实验环境】
操作系统及内核版本
- [root@pxe ~]# cat /etc/redhat-release
- CentOS Linux release 7.3.1611 (Core)
- [root@pxe ~]# uname -r
- 3.10.0-514.el7.x86_64
网络地址
服务器 | 网络地址 |
DHCP | 192.168.10.254/24 |
PXE | 192.168.10.254/24 |
TFTP | 192.168.10.254/24 |
FTP | 192.168.10.254/24 |
安装DHCP服务器
- [root@pxe ~]# yum - y install dhcp
配置DHCP服务器
- [root@pxe ~]# cat >> /etc/dhcp/dhcpd.conf <<EOF
- subnet 192.168.10.0 netmask 255.255.255.0 {
- range 192.168.10.1 192.168.10.200;
- next-server 192.168.10.254;
- filename "pxelinux.0";
- }
- EOF
- [root@pxe ~]# systemctl start dhcpd
安装TFTP服务和syslinux软件包
- [root@pxe ~]# yum - y install tftp - server syslinux
配置TFTP服务
- [root@pxe ~]# cp -v /usr/share/syslinux/{menu.c32,pxelinux.0} /var/lib/tftpboot/
- ‘/usr/share/syslinux/menu.c32’ -> ‘/var/lib/tftpboot/menu.c32’
- ‘/usr/share/syslinux/pxelinux.0’ -> ‘/var/lib/tftpboot/pxelinux.0’
- [root@pxe ~]# mkdir -v /var/lib/tftpboot/{pxelinux.cfg,centos7.3,centos6.9}
- mkdir: created directory ‘/var/lib/tftpboot/pxelinux.cfg’
- mkdir: created directory ‘/var/lib/tftpboot/centos7.3’
- mkdir: created directory ‘/var/lib/tftpboot/centos6.9’
- [root@pxe ~]# cat > /var/lib/tftpboot/pxelinux.cfg/default <<EOF
- default menu.c32
- timeout 300
- menu title ########## PXE Boot Menu ##########
- label centos6.9
- menu label ^Kickstart Inatall CentOS 6.9
- menu default
- kernel centos6.9/vmlinuz
- append ks=ftp://192.168.10.254/pub/ks/ks6.cfg initrd=centos6.9/initrd.img
- label centos7.3
- menu label ^Kickstart Inatall CentOS 7.3
- menu default
- kernel centos7.3/vmlinuz
- append ks=ftp://192.168.10.254/pub/ks/ks7.cfg initrd=centos7.3/initrd.img
- EOF
安装ftp服务器
- [root@pxe ~]# yum - y install vsftpd
配置ftp服务器
- [root@pxe ~]# mkdir -pv /var/ftp/pub/centos/{6,7}
- #光驱插入centos7镜像
- [root@pxe ~]# cp -Rv /misc/cd/* /var/ftp/pub/centos/7/
- #光驱插入centos6镜像
- [root@pxe ~]# cp -Rv /misc/cd/* /var/ftp/pub/centos/6/
- [root@pxe ~]# mkdir -v /var/ftp/pub//ks
- mkdir: created directory ‘/var/ftp/pub/ks’
复制内核和虚拟磁盘文件到tftp目录
- [root@pxe ~]# cp -v /var/ftp/pub/centos/6/isolinux/{vmlinuz,initrd.img} /var/lib/tftpboot/centos6.9/
- [root@pxe ~]# cp -v /var/ftp/pub/centos/7/isolinux/{vmlinuz,initrd.img} /var/lib/tftpboot/centos7.3/
- CentOS7.3 自动应答文件
- #version=DEVEL
- # System authorization information
- auth --enableshadow --passalgo=sha512
- # Use CDROM installation media
- url --url=ftp://192.168.10.254/pub/centos/7 # 指明安装源的路径
- # Use graphical install
- text # 将cdrom修改为text,基于字符界面安装
- selinux --disabled
- # Run the Setup Agent on first boot
- firstboot --enable
- ignoredisk --only-use=sda
- # Keyboard layouts
- keyboard --vckeymap=us --xlayouts=‘us‘
- # System language
- lang en_US.UTF-8
- # Network information
- network --bootproto=dhcp --device=ens33 --onboot=on --ipv6=auto --activate # --bootproto必须是dhcp获取,--onboot=on
- network --hostname=centos7.magedu.com
- # Root password = zhyang
- rootpw --iscrypted $6$wdMKFHGt773BoqCx$0TS.jHNyWO/T37yl9SdkFql0zC2TjeJQXil0/rI6CH6YbOc2tf3CtSEROtJFeHpGT0DXzEbrrqKhCAglVblwo1
- # System services
- services --disabled="chronyd"
- # System timezone
- timezone Asia/Shanghai --isUtc --nontp
- user --name=wang --password=$6$v.VphW/puRblcrFB$uaSrdEhGAwMXap27WIKTn5lyOOfoFyB/SNxyyL3og6s9/VQoAKoL2KQjKmeYFmoYTuYkSNL7BBxgbJzeryKr9. --iscrypted --gecos="wang"
- # X Window System configuration information
- xconfig --startxonboot
- # System bootloader configuration
- bootloader --append=" crashkernel=auto" --location=mbr --boot-drive=sda
- # Partition clearing information
- zerombr # 添加zerombr ,表示清除旧磁盘上原有的mbr,新磁盘安装可不写
- clearpart --all # 清除所有分区信息,新磁盘安装可不写
- # Disk partitioning information
- part swap --fstype="swap" --ondisk=sda --size=2048 # 分区表信息,如果你想添加分区,可按照该格式添加
- part /app --fstype="xfs" --ondisk=sda --size=51200
- part / --fstype="xfs" --ondisk=sda --size=51200
- part /boot --fstype="xfs" --ondisk=sda --size=1024
- reboot # 安装完成之后重启
- %packages # 安装软件包
- @^minimal
- @core
- @base
- @development tools
- autofs # 安装autofs服务包
- vim
- %end
- %addon com_redhat_kdump --enable --reserve-mb=‘auto‘
- %end
- %anaconda
- pwpolicy root --minlen=6 --minquality=50 --notstrict --nochanges --notempty
- pwpolicy user --minlen=6 --minquality=50 --notstrict --nochanges --notempty
- pwpolicy luks --minlen=6 --minquality=50 --notstrict --nochanges --notempty
- %end
- %post # 安装后脚本,我配置了yum源和ssh密钥登录设置(复制用于管理的服务器公钥)
- systemctl enable autofs
- systemctl disable firewalld.service
- sed -i ‘s/linux16 \/vmlinuz-3.*/& net.ifnames=0/‘ /boot/grub2/grub.cfg
- mv /etc/sysconfig/network-scripts/{ifcfg-ens33,ifcfg-eth0}
- sed -i ‘s/DEVICE.*/DEVICE=eth0/‘ /etc/sysconfig/network-scripts/ifcfg-eth0
- rm -rf /etc/yum.repos.d/*
- cat > /etc/yum.repos.d/base.repo <<eof
- [base]
- name=base
- baseurl=ftp://192.168.10.254/pub/centos/7/
- gpgcheck=0
- eof
- mkdir /root/.ssh
- cat >> /root/.ssh/authorized_keys << EOF
- ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCjog2/Glq8VVAe5fchc4UO7OhO609+f/NEO9TcYtzzxZx450bK/nHpfIg6l9ZLF8CVEfW5S+fV2uW2pxHY0XwDPGPVIH54tacw8ILeI6vZn00WGpEkDVgcW764svH8qDlgKcTBTaPZFJJDohUHh7XRWgnub8lvhk6L0+GRXzEUhW5hEyh7ht8LsxSTVXW3s2pqOEFNFzL4bC0JXzgJa720/J6izrxYhIGFm6fBpu9ljC+DmoJZQxbxcb8EfPKOo4BqZxdfzFI2Zwai1IxbutGcbe4gRZXSFuYzkDclwLcRV2CSbm60d9l/qcvsrq2vMEtQ04KqvPFfNO91vcze7f67 root@pxe
- EOF
- %end
- CentOS6.9自动应答文件
- #version=DEVEL
- install
- text # 基于字符界面安装
- lang en_US.UTF-8
- keyboard us
- network --onboot yes --device eth0 --bootproto dhcp --noipv6
- # Root password = zhyang
- rootpw --iscrypted $6$wdMKFHGt773BoqCx$0TS.jHNyWO/T37yl9SdkFql0zC2TjeJQXil0/rI6CH6YbOc2tf3CtSEROtJFeHpGT0DXzEbrrqKhCAglVblwo1
- firewall --service=ssh
- authconfig --enableshadow --passalgo=sha512
- url --url=ftp://192.168.10.254/pub/centos/6 # yum源的路径
- selinux --disabled # 关闭selinux
- timezone Asia/Shanghai
- bootloader --location=mbr --driveorder=sda --append="crashkernel=auto rhgb quiet"
- # The following is the partition information you requested
- # Note that any partitions you deleted are not expressed
- # here so unless you clear all partitions first, this is
- # not guaranteed to work
- zerombr # 清除mbr
- clearpart --all # 清除分区信息
- reboot # 安装完成后重启
- part /boot --fstype=ext4 --size=1000 # 分区信息
- part / --fstype=ext4 --size=50000
- part /app --fstype=ext4 --size=40000
- part swap --size=2048
- #repo --name="CentOS" --baseurl=cdrom:sr0 --cost=100 # 注释掉此行
- %packages
- @base
- @core
- @development tools
- vim
- autofs
- %end
- %post # 安装后脚本,我配置了yum源和ssh密钥登录设置(复制用于管理的服务器公钥)
- chkconfig autofs on
- chkconfig iptables off
- rm -rf /etc/yum.repos.d/*
- cat > /etc/yum.repos.d/base.repo <<eof
- [base]
- name=base
- baseurl=ftp://192.168.10.254/pub/centos/6/
- gpgcheck=0
- eof
- mkdir /root/.ssh
- cat >> /root/.ssh/authorized_keys << EOF
- ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCjog2/Glq8VVAe5fchc4UO7OhO609+f/NEO9TcYtzzxZx450bK/nHpfIg6l9ZLF8CVEfW5S+fV2uW2pxHY0XwDPGPVIH54tacw8ILeI6vZn00WGpEkDVgcW764svH8qDlgKcTBTaPZFJJDohUHh7XRWgnub8lvhk6L0+GRXzEUhW5hEyh7ht8LsxSTVXW3s2pqOEFNFzL4bC0JXzgJa720/J6izrxYhIGFm6fBpu9ljC+DmoJZQxbxcb8EfPKOo4BqZxdfzFI2Zwai1IxbutGcbe4gRZXSFuYzkDclwLcRV2CSbm60d9l/qcvsrq2vMEtQ04KqvPFfNO91vcze7f67 root@pxe
- EOF
- %end
复制应答文件到ftp共享目录
- [root@pxe ~]# cp -v ks7.cfg /var/ftp/pub/centos/ks/
- ‘ks7.cfg’ -> ‘/var/ftp/pub/centos/ks/ks7.cfg’
- [root@pxe ~]# cp -v ks6.cfg /var/ftp/pub/centos/ks/
- ‘ks6.cfg’ -> ‘/var/ftp/pub/centos/ks/ks6.cfg’
- [root@centos7 ~]# systemctl enable dhcpd.service
- [root@centos7 ~]# systemctl start dhcpd.service
- [root@centos7 ~]# systemctl enable tftp.socket
- [root@centos7 ~]# systemctl start tftp.socket
- [root@centos7 ~]# systemctl enable vsftpd.service
- [root@centos7 ~]# systemctl start vsftpd.service
- 关闭防火墙和SElinux
- [root@pxe ~]# systemctl disable firewalld.service
- [root@pxe ~]# systemctl stop firewalld.service
- [root@pxe ~]# sed -i ‘s/SELINUX=enforcing/SELINUX=disabled/‘ /etc/selinux/config
- [root@pxe ~]# setenforce 0
来源: http://www.bubuko.com/infodetail-2360519.html