Centos7下PXE+kickstart自动化安装6和7

PXE自动化安装centos6centos7



PXE介绍

PXEPreboot Excution Environment): 预启动执行环境,Intel公司研发,基于Client/Server的网络模式,支持远程主机通过网络从远端服务器下载映像,并由此支持通过网络启动操作系统。 
PXE
可以引导和安装Windows,linux等多种操作系统 
PXE
工作原理:

  1. 客户端向PXE服务器上的dhcp服务发送IP地址请求消息,dhcp服务检测客户端请求是否合法,主要是检测客户机的网卡MAC地址,如果合法则分配给客户端IP地址,同时将启动文件名pxelinux.0和启动文件位置信息一并传送给客户端。
  2. 客户端向PXE 服务器上的tftp服务发送获取启动文件pxelinux.0请求消息,tftp服务在接收到消息之后向客户端发送pxelinux.0大小信息,询问客户端是否同意使用,当tftp服务收到客户端发回的同意使用信息之后,正式向客户端发送pxelinux.0文件。
  3. 客户端执行接收到的pxelinux.0文件。
  4. 客户端向tftp服务器发送针对本机的配置信息文件(在tftp 服务器的pxelinux.cfg目录下),tftp服务器将配置文件发回客户端,继而客户端根据配置文件执行后续操作。
  5. 客户端向tftp服务器发送Linux内核请求信息,tftp服务器在接收到消息之后将内核文件发送给客户端
  6. 客户端向tftp服务器发送根文件请求信息,tftp服务器接收到消息之后返回Linux根文件系统给客户端。
  7. 客户端启动Linux内核
  8. 客户端下载安装源文件,读取自动化安装脚本

准备工作

准备一台主机作为网络环境内的dhcp服务器、tftp服务器、http服务器,在此主机上配置PXE环境,为网络中的其它主机提供自动化系统安装服务。

关闭防火墙

root&Centos7: ~#systemctl disable firewalld.service

#禁止防火墙开机自启动

root&Centos7: ~#systemctl stop firewalld.service

#关闭防火墙

root&Centos7: ~#iptables -vnL

关闭SELINUX

root&Centos7: ~#sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config

#禁用SElinux策略

root&Centos7: ~# grep SELINUX=disabled /etc/selinux/config

#确定SELINUX=disabled,已经将SElinux策略禁用

root&Centos7: ~#setenforce 0

#设置SELinux当前状态为Permissive

root&Centos7: ~# getenforce

#获取SELinux当前状态

将做DHCP服务器的主机设置为静态IP

root&Centos7: ~# nmcli connection show

#查看网卡名

NAME   UUID                                     TYPE             DEVICE

ens33  9611391a-9613-45dd-b981-9f1ad2ff05ba  802-3-ethernet  ens33

root&Centos7: ~#nmcli connection modify "ens33" ipv4.addresses 192.168.10.43/24 ipv4.gateway 192.168.10.1

#设置ens32网卡的IP地址为静态地址


安装相关软件包

root&Centos7: ~#yum install -y dhcp tftp-server httpd syslinux

#一次性安装完成dhcp tftp-server httpd syslinux四个软件包


配置文件共享服务和yum

启动httpd服务

root&Centos7: ~#systemctl enable httpd.service

root&Centos7: ~#systemctl start httpd.service

root&Centos7: ~#ss -ntl |grep 80

制作centos7centos6yum

root&Centos7: ~#mkdir -pv  /var/www/html/centos/{6,7}

root&Centos7: ~#mount /dev/sr0 /var/www/html/centos/6/

root&Centos7: ~#mount /dev/sr1 /var/www/html/centos/7/

root&Centos7: ~#ls /var/www/html/centos/{6,7}


制作centos6centos7kickstart文件

   生成kickstart文件的两种方法

1修改anaconda.cfg文件,此文件会在每次装完系统后生成于/root/目录下.

2使用system-config-kickstart命令生成.

复制应答文件到共享目录下的/ksdir/目录下

root&Centos7: ~#mkdir /var/www/html/ksdir/

root&Centos7: ~#cp -p /root/anaconda7-ks.cfg /var/www/html/ksdir/ks7-4.cfg

root&Centos7: ~#cp -p /root/anaconda6-ks.cfg /var/www/html/ksdir/ks6-8.cfg

root&Centos7: ~#ls /var/www/html/ksdir/

root&Centos7: ~#chmod +r /var/www/html/ksdir/*.cfg

#为应答文件增加读权限,此步骤很重要,请确保执行。

编辑centos7的应答文件

root&Centos7: ~#vim /var/www/html/ksdir/ks7-4.cfg

#version=DEVEL

# System authorization information

auth --enableshadow --passalgo=sha512

# Use CDROM installation media

cdrom

url --url=http://192.168.75.88/centos/7

# Use graphical install

graphical

#text

# Run the Setup Agent on first boot

firstboot --enable

ignoredisk --only-use=sda

# Keyboard layouts

keyboard 'us'

 

# System language

lang en_US.UTF-8

 

# Network information

network  --bootproto=dhcp --device=ens33 --onboot=on --ipv6=auto --activate

network  --hostname=localhost.localdomain

 

# Root password

rootpw --iscrypted $6$rEuaZPdxUDJ1XoEg$b7RGDIloZUqok1A1a8WM60.2RosegHs4Vh8PtgCfobWkW74IaOtyVJGdFMSBL1z5Gzn4PSJA.6TusBXGlTjgm0

# System services

services --enabled="chronyd"

# System timezone

timezone Asia/Shanghai --isUtc --ntpservers=0.centos.pool.ntp.org,1.centos.pool.ntp.org,2.centos.pool.ntp.org,3.centos.pool.ntp.org

# X Window System configuration information

# xconfig  --startxonboot

# System bootloader configuration

bootloader --append=" crashkernel=auto" --location=mbr --boot-drive=sda

# Partition clearing information

#clearpart --none --initlabel

# Disk partitioning information

#part /home --fstype="xfs" --ondisk=sda --size=17408

#part /boot --fstype="xfs" --ondisk=sda --size=1024

#part swap --fstype="swap" --ondisk=sda --size=2049

#part / --fstype="xfs" --ondisk=sda --size=61437

 

%packages

@^gnome-desktop-environment

@base

@core

@desktop-debugging

@development

@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

@security-tools

@x11

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

 

%post   #安装完成后要执行的脚本

systemctl enable autofs

rm -rf /etc/yum.repos.d/*

cat > /etc/yum.repos.d/base.repo <<eof

[base]

name=base

baseurl=file:///misc/cd

gpgcheck=0

eof

%end

编辑centos6的应答文件

root&Centos7: ~#vim /var/www/html/ksdir/ks6-8.cfg

# Kickstart file automatically generated by anaconda.

#version=DEVEL

# Install OS instead of upgrade

install

# Keyboard layouts

keyboard 'us'

# System authorization information

#auth --enableshadow --passalgo=sha512

# Use CDROM installation media

#cdrom

# Use network installation

url --url=http://192.168.75.88/centos/6

# System language

lang en_US

# Firewall configuration

firewall --disabled

# Use graphical install

graphical

zerombr

# Run the Setup Agent on first boot

#firstboot --enable

#ignoredisk --only-use=sda

# SELinux configuration

selinux --disabled

 

# Network information

network  --onboot yes --device eth0 --bootproto dhcp --noipv6

network  --hostname=localhost.localdomain

# Root password

rootpw --iscrypted $6$rEuaZPdxUDJ1XoEg$b7RGDIloZUqok1A1a8WM60.2RosegHs4Vh8PtgCfobWkW74IaOtyVJGdFMSBL1z5Gzn4PSJA.6TusBXGlTjgm0

# System services

firewall --service=ssh

authconfig --enableshadow --passalgo=sha512

#selinux --enforcing

# System timezone

timezone Asia/Shanghai --isUtc

# X Window System configuration information

#xconfig  --startxonboot

# System bootloader configuration

bootloader --append=" crashkernel=auto" --location=mbr --driveorder=sda

# Partition clearing information

#clearpart --none --initlabel

# Disk partitioning information

#part /home --fstype="xfs" --ondisk=sda --size=2048

#part /boot --fstype="ext4" --ondisk=sda --size=1024

#part swap --fstype="swap" --ondisk=sda --size=2048

#part / --fstype="xfs" --ondisk=sda --size=4096

 

%packages

#@^minimal

@^gnome-desktop-environment

@base

@core

@desktop-debugging

@development

@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

@security-tools

@x11

kexec-tools

%end

检查应答文件格式

root&Centos7: ~#yum install system-config-kickstart -y

root&Centos7: ~#ksvalidator /var/www/html/ksdir/ks7-4.cfg

root&Centos7: ~#ksvalidator /var/www/html/ksdir/ks6-8.cfg


配置tftp服务

root&Centos7: ~#systemctl enable tftp.socket

root&Centos7: ~#systemctl start tftp.socket

root&Centos7: ~#ss -nul|grep 69


配置DHCP服务

默认dhcp配置文件内没有配置信息,可以使用配置示例文件将配置文件覆盖后进行修改,dhcp服务必须在更改配置文件后才能启动。dhcp配置文件必须要dhcp服务器所在网段的地址池。

root&Centos7: ~#cp /usr/share/doc/dhcp*/dhcpd.conf.example /etc/dhcp/dhcpd.conf

#制作配置文件

root&Centos7: ~#vim /etc/dhcp/dhcpd.conf

#编辑配置文件

# DHCP server to understand the network topology.

[root@localhost tftpboot]# cat /etc/dhcp/dhcpd.conf

#

# DHCP Server Configuration file.

#   see /usr/share/doc/dhcp*/dhcpd.conf.sample

#   see 'man 5 dhcpd.conf'

#

subnet 192.168.75.0 netmask 255.255.255.0 {

        option routers 192.168.75.254;         #设置网关

        option subnet-mask  255.255.255.0;     #设置地址段

        option time-offset -18000;

        range dynamic-bootp 192.168.75.100 192.168.75.200;  #设置地址池

        default-lease-time 21600;

        max-lease-time 43200;

        next-server 192.168.75.88;#提供引导文件的服务器ip网络安装操作系统

        filename "pxelinux.0";         #指明引导文件名称

}

[root@localhost tftpboot]#

root&Centos7: ~#systemctl enable dhcpd        #dhcp服务设置为开机自启动

root&Centos7: ~#systemctl start dhcpd #启动dhcp服务

root&Centos7: ~#ss -nul|grep 67               #检查确认dhcp服务已经启动


制作PXE相关文件

root&Centos7: ~#mkdir /var/lib/tftpboot/pxelinux.cfg/

root&Centos7: ~#mkdir /var/lib/tftpboot/centos{6,7}

#cp /var/www/html/centos/7/isolinux/isolinux.msg /var/lib/tftpboot/pxelinux.cfg/default

root&Centos7: ~#cp /var/www/html/centos/7/isolinux/*.msg /var/lib/tftpboot/

root&Centos7: ~# cp /usr/share/syslinux/pxelinux.0  /var/lib/tftpboot/

root&Centos7: ~#cp /var/www/html/centos/7/isolinux/vesamenu.c32 /var/lib/tftpboot/

root&Centos7: ~#cp /usr/share/syslinux/{pxelinux.0,menu.c32} /var/lib/tftpboot/

root&Centos7: ~#cp /var/www/html/centos/7/images/pxeboot/{initrd.img,vmlinux}  /var/lib/tftpboot/

root&Centos7: ~#cp /var/www/html/centos/7/isolinux/{initrd.img,vmlinuz}  /var/lib/tftpboot/centos7

root&Centos7: ~#cp /var/www/html/centos/6/isolinux/{initrd.img,vmlinuz}  /var/lib/tftpboot/centos6

root&Centos7: ~#cp /misc/cd/isolinux/isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default

root&Centos7: ~#tree /var/lib/tftpboot/

/var/lib/tftpboot/

├── boot.msg

├── centos6

   ├── initrd.img

   └── vmlinuz

├── centos7

   ├── initrd.img  #伪文件系统文件

   └── vmlinuz     #内核文件

├── initrd.img       #伪文件系统文件

├── menu.c32         #纯文本菜单 

├── pxelinux.0       #引导文件,相当于grub

├── pxelinux.cfg

   └── default     #启动菜单文件

├── vesamenu.c32

└── vmlinuz

 

3 directories, 11 files

[root@localhost tftpboot]#

制作启动菜单

root&Centos7: ~#vim /var/lib/tftpboot/pxelinux.cfg/default

default menu.c32

timeout 600

menu title CentOS Linux  PXE Install

 

label centos7     #自动安装centos7

      menu label Auto Install CentOS Linux ^7

      kernel centos7/vmlinuz

      append initrd=centos7/initrd.img ks=http://192.168.10.43/ksdir/ks7-1.cfg  #应答文件所在路径

 

label centos6      #自动安装centos6

      menu label Auto Install CentOS Linux ^6

      kernel centos6/vmlinuz

      append initrd=centos6/initrd.img ks=http://192.168.10.43/ksdir/ks6-1.cfg

 

label manual7     #手动安装centos7

      menu label ^Manual Install CentOS Linux 7

      kernel centos7/vmlinuz

      append initrd=centos7/initrd.img inst.repo=http://192.168.10.43/centos/7

 

label manual6      #手动安装centos6

      menu label Manual ^Install CentOS Linux 6

      kernel centos6/vmlinuz

      append initrd=centos6/initrd.img inst.repo=http://192.168.10.43/centos/6

 

label local     #本地硬盘启动

     menu default    #默认启动项

     menu label Boot from ^local drive

     localboot 0xffff

    menu end

系统安装

使用网络引导启动,下图是启动后的安装界面:选择Auto Install Centos Linux6

注意:使用虚拟机安装时,centos7内存要大于1024M,否则无法成功安装。

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值