通过pxe来实现linux自动化安装需要准备3个服务和6个文件。
本实验将三个服务安装在一个机器上,生产环境下可以将http和dhcp分开部署。
三个服务
用http配置安装yum 源
安装httpd服务
yum install httpd
systemctl start httpd
systemctl enable httpd
准备yum源文件
mkdir -pv /var/www/html/{6,7}/os/x86_64/
[root@www ~]# tree /var/www/html
/var/www/html
├── 6
│ └── os
│ └── x86_64
└── 7
└── os
└── x86_64
将下载好的os iso文件mount到对应的目录
mount /data/CentOS-7-x86_64-DVD-2003.iso /var/www/html/7/os/x86_64/
配置tftp
# 1. 安装tftp服务器
yum install tftp-server
# 2. 启动tftp服务器
systemctl start tftp
配置dhcp
安装dhcpd服务
yum install dhcp
配置dhcp服务
#1 复制example dhcpd配置文件
cp /usr/share/doc/dhcp*/dhcpd.conf.example /etc/dhcp/dhcpd.conf
#2 编辑dhcpd.exe配置文件
# 设置确实的dns
option domain-name "coding.com";
option domain-name-servers 180.76.76.76;
# 分配的网段,默认gateway和设置tftp地址和文件
subnet 192.168.80.0 netmask 255.255.255.0 {
range 192.168.80.50 192.168.80.100;
option routers 192.168.80.2;
next-server 192.168.80.11; #tftp地址
filename "pxelinux.0";
}
# 设置租期时间
default-lease-time 86400; # 1天
max-lease-time 864000;
启动dhcp服务
systemctl start dhcp
systemctl enable dhcp
6个文件
kickstart文件
使用创建kickstart文件工具 system-config-kickstart
NOTE: 生成kickstart文件的yum源id要改为development
在post script中加入系统安装好后的基本配置工作,比如:
配置yum源
配置ssh key
将kickstart文件放到http中
mkdir /var/www/html/ksdir
cp ks.cfg /var/www/html/ksdir/
配置文件ks.cfg
#platform=x86, AMD64, or Intel EM64T
#version=DEVEL
# Install OS instead of upgrade
install
# Keyboard layouts
keyboard 'us'
# Root password
rootpw --plaintext teamw0rk
# System language
lang en_US
# System authorization information
auth --useshadow --passalgo=sha512
# Use text mode install
text
# SELinux configuration
selinux --disabled
# Do not configure the X Window System
skipx
# Firewall configuration
firewall --disabled
# Network information
network --bootproto=dhcp --device=eth0
# Reboot after installation
reboot
# System timezone
timezone Asia/Shanghai
# Use network installation
url --url="http://192.168.80.11/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 /boot --fstype="xfs" --size=1000
part / --fstype="xfs" --size=30000
part swap --fstype="swap" --size=2000
%post
mkdir /root/.ssh
chmod 700 /root/.ssh
cat > /root/.ssh/authorized_keys << EOF
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC4iC8RdrGFB72uXS67BK3N0SS9qcso3+tbyM1RhhyZS82GqhizvxFHPSW8KGJulgrfeA0eKXNMX1VwgSWZegp9FWwKWEeDzS5HCK+PkSUjwb3UMroGDIyXFL/BKJJrBkRXwZ+U1ncctQ/d6GZq1J+tXk2UqMIVX+gODPl26rswm9/j0IhKV3ewHb//ynnRBGY4eqm3PpMZ4afQ94DE//20G4yV/wuJsRErcxbA6jfoNDsw6cLIT1Q8nwMw5v1p389AYSp3ioovYBR4LWcxSbXPE1ngtEud0WbTQlR8piM+fiRFJJxAxXFpKaUU8MoWv0npgJoLM9RWld2GEIxiuoij root@80_11
EOF
chmod 600 /root/.ssh/authorized_keys
mkdir /etc/yum.repos.d/bak
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak
cat > /etc/yum.repos.d/base.repo <
[base]
name=base source
baseurl=http://192.168.80.11/7/os/x86_64/
enable=1
gpgcheck=0
EOF
%end
%packages
@development
%end
准备vmlinux, initrd.img, iso.linux.cfg,pxelinux.0,menu.c32和isolinux.cfg文件
从centos iso文件中copy vmlinux, initrd.img和iso.linux.cfg文件到tftp文件目录/var/lib/tftpboot
mount cento7.iso /mnt.iso
cp /mnt/iso/isolinux/vmlinuz /var/lib/tftpboot/
cp /mnt/iso/isolinux/initrd.img /var/lib/tftpboot/
cp /mnt/iso/isolinux/isolinux.cfg /var/lib/tftpboot/
从软件包syslinux中复制pxelinux.0和menuc32文件到/var/lib/tftpboot
yum install syslinux -y
cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/
cp /usr/share/syslinux/menu.c32 /var/lib/tftpboot/
编辑/var/lib/tftpboot/isolinux.cfg
default menu.c32
timeout 600
menu title CentOS7 Auto Install
label Mini Install
menu label Install CentOS 7 ^Mini Install
kernel vmlinuz
append initrd=initrd.img ks=http://192.168.80.11/ksdir/ks.cfg
### 准备不同的ks文件
label Desktop
menu label Install CentOS 7 wich ^X
menu default
kernel vmlinuz
append initrd=initrd.img ks=http://192.168.80.11/ksdir/ks_v2.cfg
label local
menu default
menu label Boot from ^local drive
localboot 0xffff
整理文件目录结构
mkdir pxelinux.cfg
mv isolinux.cfg pxelinux.cfg/default
# 最终的目录结构
tree /var/lib/tftpboot/
/var/lib/tftpboot/
├── initrd.img
├── menu.c32
├── pxelinux.0
├── pxelinux.cfg
│ └── default
└── vmlinuz
在此架构下还可以支持多版本操作系统启动
准备不同内核,kickstart,menu项,还有yum源
├── centos6
│ ├── initrd.img
│ └── vmlinuz
├── centos7
│ ├── initrd.img
│ └── vmlinuz
├── menu.c32
├── pxelinux.0
└── pxelinux.cfg
└── default
启动要安装的机器即可进行自动化安装