CentOS 7.2 下 PXE+kickstart 自动安装系统

一、简单概述

1.1 Kickstart 概述

      对于网络安装系统,在linux 下面最熟悉的应该就是 Kickstart 以及 cobbler。写这篇文章的目的在于我公司目前使用的就是 Kickstart,所有操作系统的版本为 Centos 6.4,和个人的习惯不同,个人崇尚前沿技术以及由于以前做Docker相关,所有操作系统也是Centos 7.2以上版本,对于网络安装系统选择,个人也推荐使用 cobbler。好了,废话就不多说了,如果大家有兴趣可以参考cobbler相关文档,接下来我们就准备相关实验环境。

1.2 满足Kickstart安装系统的需求

  1) 网卡必须支持PXE引导;

      2) 客户端需要有与Kickstart 通信的IP地址,这里一般都是通过DHCP自动分配IP地址来实现;

  3) 提供引导所需的文件,内核文件,内核镜像文件等,此时一般通过tftp-server等实现;

  4) kickstart文件,它主要实现自动化安装的过程,比如配置主机名、添加用户、安装系统后相关操作;

      5) 提供安装源,一般就是NFS/ftp/httpd等;

总结:通过如上需求,我们就知道需要安装的相关服务,如DHCP/tftp-server/syslinux/kickstart/vsftpd等; 

 

二、配置Kickstart

2.1 环境准备

[root@localhost ~]# systemctl stop firewalld

[root@localhost ~]# systemctl disable firewalld

[root@localhost ~]# getenforce 
Permissive

2.2 准备安装源

提示:我这里是能出公网,所以就不配置私有yum原,如果不能出公网,根据自己环境配置yum源即可,这里过程就略;

2.3 安装DHCP服务

[root@localhost ~]# yum install dhcp ntpdate syslinux -y && ntpdate time.windows.com

###备份原始配置文件

cp /etc/dhcp/dhcpd.conf /etc/dhcp/dhcpd.conf.bak_$(date +%Y-%m-%d-%H-%M)

[root@localhost ~]# cat /etc/dhcp/dhcpd.conf
default-lease-time 600;
max-lease-time 7200;
log-facility local7;

subnet 192.168.100.0 netmask 255.255.255.0 {
option routers 192.168.100.1;
option subnet-mask 255.255.255.0;
option domain-name-servers 192.168.100.1;
option time-offset -18000; # Eastern Standard Time 
range dynamic-bootp 192.168.100.60 192.168.100.100;
default-lease-time 21600;
max-lease-time 43200;
next-server 192.168.100.1;
filename "pxelinux.0";
}

###在多网卡的情况下绑定网卡启动DHCP服务

[root@localhost pub]# cat /usr/lib/systemd/system/dhcpd.service 
[Unit]
Description=DHCPv4 Server Daemon
Documentation=man:dhcpd(8) man:dhcpd.conf(5)
Wants=network-online.target
After=network-online.target
After=time-sync.target

[Service]
Type=notify
ExecStart=/usr/sbin/dhcpd -f -cf /etc/dhcp/dhcpd.conf -user dhcpd -group dhcpd --no-pid eno33554984

[Install]
WantedBy=multi-user.target

###启动服务

systemctl start dhcpd.service

systemctl enable dhcpd.service

 

2.4 安装ftp

yum install -y vsftpd

###挂载镜像

mount /dev/cdrom /mnt

cp -rf /mnt/* /var/ftp/pub/

提示:如果是光盘镜像可以这样挂载,mount /opt/CentOS-7-x86_64-Minimal-1511.iso /var/ftp/pub -o loop,但是如果你的ks.cfg文件也放到/var/ftp/pub目录下,注意不要被覆盖掉;

###查看相关内容

 ls /var/ftp/pub

###启动vsftp服务

systemctl start vsftpd
systemctl enable vsftpd 

2.5 tftp安装

yum install -y tftp-server tftp syslinux-tftpboot

cp /etc/xinetd.d/tftp /etc/xinetd.d/tftp.bak_$(date +%Y-%m-%d-%H-%M)

cat /etc/xinetd.d/tftp

service tftp
{
socket_type	= dgram
protocol	= udp
wait	= yes
user	= root
server	= /usr/sbin/in.tftpd
server_args	= -s /var/lib/tftpboot
disable	= no #此处将yes该为no
per_source	= 11
cps	= 100 2
flags	= IPv4
}

###配置tftp-server

cp -rf /usr/share/syslinux/* /var/lib/tftpboot

mkdir /var/lib/tftpboot/pxelinux.cfg

mkdir /var/lib/tftpboot/centos7
cp /mnt/images/pxeboot/vmlinuz /var/lib/tftpboot/centos7
cp /mnt/images/pxeboot/initrd.img /var/lib/tftpboot/centos7

touch /var/lib/tftpboot/pxelinux.cfg/default 
chmod 755 /var/lib/tftpboot/pxelinux.cfg/default 


[root@localhost ~]# cat /var/lib/tftpboot/pxelinux.cfg/default 
default menu.c32
prompt 1
timeout 10

menu title ########## PXE Boot Menu ##########

label 1
menu label ^1) Install CentOS 7 x64 with Local Repo
menudefault
kernel centos7/vmlinuz
append initrd=centos7/initrd.img text ks=ftp://192.168.100.1/pub/ks.cfg

label 2
menu label ^2) Install CentOS 7 x64 with http://mirror.centos.org Repo
kernel centos7/vmlinuz
append initrd=centos7/initrd.img method=http://mirror.centos.org/centos/7/os/x86_64/ devfs=nomount ip=dhcp

label 3
menu label ^3) Install CentOS 7 x64 with Local Repo using VNC
kernel centos7/vmlinuz
append initrd=centos7/initrd.img method=ftp://192.168.100.1/pub devfs=nomount inst.vnc inst.vncpassword=password 

2.6 配置Kickstart

 

[root@localhost ~]# cp ~/anaconda-ks.cfg /var/ftp/pub/ks.cfg

[root@localhost ~]# cat /var/ftp/pub/ks.cfg 
#version=DEVEL
# System authorization information
auth --enableshadow --passalgo=sha512
# Install OS instead of upgrade
install
# Use network installation
url --url="ftp://192.168.100.1/pub"
# Use graphical install
graphical
# 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=eno16777736 --onboot=yes --ipv6=auto
network --hostname=localhost.localdomain

# Root password
rootpw --iscrypted $6$JqUyORpkn8xu8.i/$60aNcdZwMEjSkRgB3nqDrNw0sWJ7PYTUM/EKjvZWYgD8jkfiuEU8vjhI/BqVYcJvs6UiOpf5GtH3jwBAfhBSP0
# System timezone
timezone Asia/Shanghai --isUtc
# System bootloader configuration
bootloader --append=" crashkernel=auto" --location=mbr --boot-drive=sda
autopart --type=lvm
# Partition clearing information
clearpart --none --initlabel
# SELinux configuration
selinux --disabled

%packages
@^minimal
@core
kexec-tools

%end

%addon com_redhat_kdump --enable --reserve-mb='auto'

%end

###检查是否存在错误

yum install pykickstart
ksvalidator /var/ftp/pub/anaconda-ks.cfg

###启动相关服务

systemctl restart ftpd

systemctl restart vsftpd

systemctl restart dhcpd

相关链接:https://access.redhat.com/documentation/zh-CN/Red_Hat_Enterprise_Linux/7/html/Installation_Guide/sect-kickstart-syntax.html

相关链接:http://www.tecmint.com/install-pxe-network-boot-server-in-centos-7/

posted @ 2017-03-11 15:50  Go_小易  阅读(7491)  评论(0编辑  收藏  举报