基于PXE 和 Cobbler 自动安装Linux系统

1 概述

本文是对本地自动化安装系统的扩展。同时,本文在文档后部附上了关于自动化安装PXE系统环境的脚本。

2 基于PXE 自动化安装

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

PXE网络自动化安装:通过搭建网络yum源(http,ftp,nfs三者选一),tftp服务器,dhcp服务器,原理是,通过dhcp服务器,使得机器启动的时候,获取到相关的ip,在dhcp里指定了next-server,该next-server就是tftp服务器的ip,这样,机器获取到ip后,会到指定的tftp服务器的默认路径/var/lib/tftpboot下去下载相关的文件,首先是加载pxelinux.cfg目录下的default文件,该default文件就是安装机器的菜单选项,在这菜单里,指定kickstart文件路径和/var/lib/tftpboot下的initrd.img和vmlinuz的路径,使得需要安装的对应系统能够启动并加载kickstart文件进行安装。在kickstart文件里我们将指定了安装包的路径。

PXE工作原理

如下图

1240

原理介绍

.Client向PXE Server上的DHCP发送IP地址请求消息,DHCP检测Client是否合法(主要是检测Client的网卡MAC地址),如果合法则返回Client的IP地址,同时将启动文件pxelinux.0的位置信息一并传送给Client

.Client向PXE Server上的TFTP发送获取pxelinux.0请求消息,TFTP接收到消息之后再向Client发送pxelinux.0大小信息,试探Client是否满意,当TFTP收到Client发回的同意大小信息之后,正式向Client发送pxelinux.0

.Client执行接收到的pxelinux.0文件

.Client向TFTP Server发送针对本机的配置信息文件(在TFTP服务的pxelinux.cfg目录下,这是系统菜单文件,格式和isolinux.cfg格式一样,功能也是类似),TFTP将配置文件发回Client,继而Client根据配置文件执行后续操作。

.Client向TFTP发送Linux内核请求信息,TFTP接收到消息之后将内核文件发送给Client

.Client向TFTP发送根文件请求信息,TFTP接收到消息之后返回Linux根文件系统

.Client启动Linux内核

.Client下载安装源文件,读取自动化安装脚本

.安装前准备:关闭防火墙和SELINUX,DHCP服务器静态IP

.安装软件包

环境准备,搭建以下三个服务

httpd/nfs/ftp(三选一,用于搭建yum源)tftp-server dhcp

文件准备

通过以下两个工具生成相关文件

syslinux  : 生成menu.c32 和 pxelinux.0

system-config-kickstart:生成 kickstart文件

.配置文件共享服务:

systemctl  enable  httpd
systemctl start httpd
mkdir  /var/www/html  /centos/7
mount    /dev/sr0/  var /www/html/centos/7
 

.准备kickstart文件

/var/www/html/ks/centos7 .cfg #注意:权限,其他人要分配可读权限

安装路径,这次是用http服务器作为yum源,ks文件中url --url=http:/192.168.32.72/os/7

开机时加载网卡,onboot选项

.配置tftp服务

systemctl  enable  tftp.socket(centos6:chkconfig tftp on)
systemctl start tftp.socket(centos6:service xinetd restart)
 

.配置DHCP服务

可以直接拷贝配置文件进行修改,主要该要使用的网段range和指定tftp服务器next-server和tftp服务器上的默认的文件filename

systemctl  enable  dhcpd
vim  /etc/dhcp/dhcpd .conf
option domain-name  "sunny.com" ;
option domain-name-servers 192.168.32.61;
default-lease- time  86400;
max-lease- time  86400;
subnet 192.168.32.0 netmask 255.255.255.0 {
range 192.168.32.100 192.168.32.200;
option routers 192.168.32.1;
next-server 192.168.32.72;
filename  "pxelinux.0" ;
}
 
 
systemctl start dhcpd

配置tftp服务

.准备相关文件

tftp路径是

#pxelinux.0等文件来自syslinux这个软件包,要先进行安装
yum  install  syslinux
mkdir /var/lib/tftpboot/pxelinux .cfg/
#pxelinux.cfg把必要的文件复制到这个路径下
cp /usr/share/syslinux/ {pxelinux.0,menu.c32} /var/lib/tftpboot/
#pxelinux.0启动菜单,启动操作系统的文件
#menu.c32这个是菜单风格
cp /misc/cd/isolinux/ {vmlinuz,initrd.img} /var/lib/tftpboot/
#计算机启动所需的内核文件和虚拟根文件
cp /misc/cd/isolinux/isolinux .cfg /var/lib/tftpboot/pxelinux .cfg /default
#isolinux.cfg菜单文件,需要改名为default

当前文件列表如下:

  /var/lib/tftpboot/
├──initrd.img
├──menu.c32
├──pxelinux.0
├──pxelinux.cfg
│└──default
└──vmlinuz
 

.准备启动菜单

修改启动菜单

vim  /var/lib/tftpboot/pxelinux .cfg /default
default menu.c32
#这是启动菜单的风格,需要根据实际情况进行修改,原来是vesamenu.c32
timeout 600
menu title PXE INSTALL MENU
label autoa
menu label Auto Install CentOS 7
kernel vmlinuz
append initrd=initrd.img  ks=http: //192 .168.32.72 /ks/centos7 .cfg
#这里的应答文件是基于网络的,根据实际情况修改
label manual
menu label Manual Install CentOS 7
kernel vmlinuz
append initrd=initrd.img  inst.repo=http: //192 .168.32.72 /centos/7
#手动安装应答文件不写,但是手动安装要找yum源安装,所以要指定yum源路径
label  local
menu default
#如果不选择,默认就是menu  default所在的行
menu label ^Boot from  local  drive
localboot 0xffff
menu end

到这里,pxe安装centos7的环境已经准备完成了,这样子,当有机器需要安装系统的时候,开机,选择网络启动就会按照PXE的设置进行安装

3 基于cobbler自动化安装系统

 

cobbler 介绍

cobbler 工作流程

 以下图形是cobbler和PXE工作的对比图形

1240

cobbler 工作流程解释如下

.client裸机配置了从网络启动后,开机后会广播包请求DHCP服务器(cobbler server)发送其分配好的一个IP

.DHCP服务器(cobbler server)收到请求后发送responese,包括其ip地址

.client裸机拿到ip后再向cobbler server发送请求OS引导文件的请求

.cobbler server告诉裸机OS引导文件的名字和TFTP server的ip和port

.client裸机通过上面告知的TFTP server地址通信,下载引导文件

.client裸机执行执行该引导文件,确定加载信息,选择要安装的os,期间会再向cobbler server请求kickstart文件和os image

.cobbler server发送请求的kickstart和os iamge

.client裸机加载kickstart文件

.client裸机接收os image,安装该os image

cobbler 实现步骤

.安装包,并设置服务

.检查配置

.根据上面提示修改配置

.下载启动相关文件菜单

.配置DHCP服务

.分别导入centos的安装源,并查看

.准备kickstart文件并导入cobbler

.配置web管理界面

.测试

cobbler 相关术语

.发行版:表示一个操作系统版本,它承载了内核和initrd 的信息,以及内核参数等其他数据

.配置文件:包含一个发行版、一个kickstart 文件以及可能的存储库,还包含更多特定的内核参数等其他数据

.系统:表示要配置的主机,它包含一个配置文件或一个镜像,还包含IP 和MAC 地址、电源管理(地址、凭据、类型)以及更为专业的数据等信息

.存储库:保存一个yum 或rsync 存储库的镜像信息

.镜像:可替换一个包含不属于此类别的文件的发行版对象(例如,无法分为内核和initrd 的对象)

cobbler 各种配置目录说明

cobbler 依赖epel 源安装

yum -y install cobbler

一般是会自动安装相关的包的,如http,dns等,安装完成后,如果还有没有安装的服务,要手动安装,如dhcp。

yum -y install dhcp

.配置文件目录/etc/cobbler

/etc/cobbler/settings  : cobbler 主配置文件
/etc/cobbler/iso/ : iso模板配置文件
/etc/cobbler/pxe : pxe模板文件
/etc/cobbler/power : 电源配置文件
/etc/cobbler/user .conf: web服务授权配置文件
/etc/cobbler/users .digest: web访问的用户名密码配置文件
/etc/cobbler/dhcp .template: dhcp服务器的的配置末班
/etc/cobbler/dnsmasq .template: dns服务器的配置模板
/etc/cobbler/tftpd .template: tftp服务的配置模板
/etc/cobbler/modules .conf: 模块的配置文件

cobbler 目录介绍

.数据目录
/var/lib/cobbler/config/ : 用于存放distros,system,profiles 等信
息配置文件
/var/lib/cobbler/triggers/ : 用于存放用户定义的cobbler命令
/var/lib/cobbler/kickstart/ : 默认存放kickstart文件
/var/lib/cobbler/loaders/ : 存放各种引导程序
.镜像目录
/var/www/cobbler/ks_mirror/ : 导入的发行版系统的所有数据
/var/www/cobbler/images/  : 导入发行版的kernel和initrd镜像用于远程网络启动
/var/www/cobbler/repo_mirror/ : yum 仓库存储目录
.日志目录
/var/log/cobbler/installing : 客户端安装日志
/var/log/cobbler/cobbler .log : cobbler日志

cobbler 命令介绍

cobbler check 核对当前设置是否有问题
cobbler list 列出所有的cobbler元素
cobbler report 列出元素的详细信息
cobbler  sync  同步配置到数据目录,更改配置最好都要执行下
cobbler reposync 同步yum仓库
cobbler distro 查看导入的发行版系统信息
cobbler system 查看添加的系统信息
cobbler profile 查看配置信息

cobbler 重要的参数

./etc/cobbler/settings中重要的参数设置

.default_password_crypted: "$1$gEc7ilpP$pg5iSOj/mlxTxEslhRvyp/" 
#这里是设置新安装机器的root密码,用openssl passwd -1 生成加密密码
.manage_dhcp:1
.manage_tftpd:1
.pxe_just_once:1
#以上调整为1,表示对相关服务进行控制
.next_server:< tftp服务器的IP 地址>
.server:<cobbler服务器的IP 地址>

cobbler 环境检查

.执行Cobbler check命令常见如下异常,一般是配置文件没更改导致

.1 : The ‘server’ field  in  /etc/cobbler/settings  must be  set  to something other than localhost, or kickstarting features will not work. This should be a resolvable  hostname  or IP  for  the boot server as reachable by all machines that will use it.
.2 : For PXE to be functional, the ‘next_server’ field  in  /etc/cobbler/settings  must be  set  to something other than 127.0.0.1, and should match the IP of the boot server on the PXE network.
.3 : some network boot-loaders are missing from /var/lib/cobbler/loaders , you may run ‘cobbler get-loaders’ to download them, or,  if  you only want to handle x86 /x86_64  netbooting, you may ensure that you have installed a recentversion of the syslinux package installed and can ignore this message entirely. Files  in  this directory, should you want to support all architectures, should include pxelinux.0, menu.c32, elilo.efi, and yaboot. The ‘cobbler get-loaders’  command  is the easiest way to resolve these requirements.
.4 : change ‘disable’ to ‘no’  in  /etc/xinetd .d /rsync
.5 : comment ‘dists’ on  /etc/debmirror .conf  for  proper debian support
.6 : comment ‘arches’ on  /etc/debmirror .conf  for  proper debian support
.7 : The default password used by the sample templates  for  newly installed machines (default_password_crypted  in  /etc/cobbler/settings ) is still  set  to ‘cobbler’ and should be changed, try: “openssl  passwd  -1 -salt ‘random-phrase-here’ ‘your-password-here’” to 
generate new one
.8 : fencing tools were not found, and are required to use the (optional) power management features.  install  cman or fence-agents to use them

Cobbler check常见报错解决办法如下

.修改/etc/cobbler/settings文件中的server参数的值为提供cobbler服务的主机相应的IP地址或主机名

.修改/etc/cobbler/settings文件中的next_server参数的值为提供PXE服务的主机相应的IP地址

.如果当前节点可以访问互联网,执行“cobbler get-loaders”命令即可;否则,需要安装syslinux程序包,而后制/usr/share/syslinux/{pxelinux.0,memu.c32}等文件至/var/lib/cobbler/loaders/目录中

.执行“chkconfig rsync on”命令即可

.执行“openssl passwd -1 生成密码,并用其替换/etc/cobbler/settings文件中

default_password_crypted参数的值

cobbler 相关管理

.下载启动菜单:cobbler get-loaders

.管理distro ,这一步是导入光盘的所有文件,时间会比较长,根据光盘的大小决定时间长短

cobbler  import  --name=centos-6.9-x86_64 --path= /media/cdrom

.管理profile ,即启动菜单

cobbler profile add --name=centos-6.9-x86_64-basic --distro=centos-6.9-x86_64 --kickstart= /tmp/centos-6 .9-x86_64.cfg

.查看profiles

cobbler profile list

.查看引导文件

cat  /var/lib/tftpboot/pxelinux .cfg /default

.同步cobbler配置

.cobbler sync

多系统引导方案

导入多个ks文件,并将ks文件和yum源做管理

cobbler  import  --name=CentOS-7-x86_64 --path= /media/cdrom

cobbler的web管理实现

.cobbler-web提供cobbler的基于web管理界面,服务包来自epel源

yum  install  cobbler-web

配置文件在/etc/httpd/conf.d/cobbler_web.conf,在httpd的配置目录下,所以要使得cobbler生效,必须重启httpd服务

.认证方式

.定义认证方法:/etc/cobbler/modules.conf,这里介绍两种验证方法认证cobbler_web用户, authn_configfile和authn_pam

基于authn_configfile模块认证

.[authentication]块中module = authn_configfile,系统默认就是authn_configfile这个验证方法

.创建其认证文件/etc/cobbler/users.digest,并添加所需的用户

htdigest  -c  /etc/cobbler/users .digest  Cobbler admin

#以上语句中Cobbler是必须要有的,而且第一个字母C大写

#admin是新增加的文件,可以自己随意命名

.注意:添加第一个用户时,使用“-c”选项,后续添加其他用��时不要再使用,cobbler_web的realm只能为Cobbler

重启cobbler服务

基于authn_pam模块认证

.更改配置文件/etc/cobbler/modules.conf里的 [authentication]块中指定module = authn_pam

.创建cobbler用户:useraddcobbler

useradd  cobbleruser  -s  /sbin/nologin
echo  Pass1234 |  passwd  --stdin cobbleruser

.修改文件/etc/cobbler/users.conf,将自己创建的用户设置为管理员

[admins]

admin = "cobbleruser"其他不用更改

重启cobbler服务

测试

A.安装新的linux系统

B. 通过浏览器https://cobblerserver/cobbler_web 输入用户名和密码就可以访问

安装cobbler步骤如下

1 安装包

yum -y  install  cobbler  dhcp
systemctl  enable  cobblerd
systemctl  start  cobblerd
systemctl  enable  tftp
systemctl start  tftp
systemctl  enable  httpd
systemctl start httpd

2 根据cobbler check 提示进行修改

vim  /etc/cobbler/settings 
default_password_crypted:  "$1$8ckh4FrM$ayLsgQi85bi8Nt5Gj4Drj/" 
#openssl passwd -1 生成口令,设置新机器root的初始密码
next_server: 192.168.32.73
manage_dhcp: 1 
server: 192.168.32.73

重启服务并同步文件

systemctl restart cobblerd
cobbler  sync

2)生成dhcp模版文件

vi  /etc/cobbler/dhcp .template
subnet 192.168.32.0 netmask 255.255.255.0 {
    range dynamic-bootp        192.168.32.100 192.168.32.254;
#变更上面这行,其他行只要变更相关网络行配置就可以
}
同步文件  cobbler  sync

3) 准备启动文件和和菜单风格文件

复制菜单风格文件

 /var/lib/cobbler/loaders中如果缺少一些网络启动加载程序,可以运行'cobbler get-loaders'来下载它们,或者,如果您只想处理x86 / x86_64 netbooting,则可以确保已安装 安装

了syslinux软件包的一个新版本,可以完全忽略这个消息。 该目录中的文件,如果要支持所有架构,应包括pxelinux.0,menu.c32,elilo.efi和yaboot。 “cobbler get-loaders”

命令是解决这些要求的最简单方法。

连接internet

cobbler get-loaders

不连internet,只复制menu.c32,pxelinux.0两个文件的话,只支持安装x86/x86_64架构的系统,所以建议还是执行cobbler get-loaders,将所需的文件都下载到 /var/lib/cobbler/loaders来。

cp /var/lib/tftpboot/{menu.c32,pxelinux.0} /var/lib/cobbler/loaders

查看启动菜单的菜单选项,命令如下

cobbler profile list

如果不需要相关菜单,可以用以下命令移除,

cobbler profile remove --name=centos7.3_desk

3 导入yum源

cobbler  import  --path= /misc/cd  --name=centos7.3 --arch=x86_64
#断开光盘连接,重新换张6.9的盘,进行安装
cobbler  import  --path= /misc/cd  --name=centos6.9 --arch=x86_64
cobbler distro list
cobbler profile list

这一步执行完成后,会将当前的光盘文件全部拷贝到/var/www/cobbler/ks_mirror这个目录下,并且生成新的目录,目录名称自己指定,如centos7.3,这里这个目录的名字一旦设定好了之后,

后续就不要继续更改目录名,否则生成菜单文件会异常,因为名字不匹配

4 生成ks

#注意,这里的应答文件是根据yum源自动生成的,我们也可以自己生成生成ks文件,然后在做管理,把应答文件里的安装方式改成 url  --url=$tree,就会

自动去找到对应的目录,当然这里也可以直接指定对应的路径

这里的$tree 的路径可以通过以下命令查看,

cobbler  distro list 找到源配置文件,如centos73-x86_64

cobbler distro tree centos73-x86_64 就可以看到tree这个变量具体指哪个,如下

Kickstart Metadata             : {'tree': 'http://@@http_server@@/cblr/links/centos73-x86_64'}

注意,cobbler专门放应答文件的目录是/var/lib/cobbler/kickstarts,将新生成的ks文件拷贝到这里/var/lib/cobbler/kickstarts即可,如

cp  ks73min.cfg /var/lib/cobbler/kickstarts/

然后再将应答文件和yum源做关联

cp  centos6.cfg centos7.cfg  /var/lib/cobbler/kickstarts/
cobbler profile remove --name=centos6.9-x86_64
cobbler profile remove --name=centos7.3-x86_64
cobbler profile add --name=centos6.9_desktop --distro=centos6.9-x86_64  --kickstart= /var/lib/cobbler/kickstarts/centos6 .cfg
cobbler profile add --name=centos7.3_mini --distro=centos7.3-x86_64  --kickstart= /var/lib/cobbler/kickstarts/centos7 .cfg

#以上命令将安装的新生成的ks文件和要对应的安装的yum源关联起来,--name要新生成的菜单项的名字,--distro指定yum源(cobbler distro list

这个命令查看当前有多少个yum源,及其名称),--kickstart指定ks文件的路径

cobbler sync

5 启用网页管理

htdigest  -c  /etc/cobbler/users .digest  Cobbler sunny
service httpd restart
service cobblerd restart

到这里cobbler安装完成了

6 测试

A安装虚拟机

B打开cobbler管理页面

https://172.18.50.73/cobbler_web

4 半自动化安装pxe环境脚本实现

这里附上自动化安装PXE环境的半自动化脚本

将菜单配置文件,dhcp配置文件,ks文件打包放在附件中,有需要可以下载查看

这些配中,需要注意的是ks的文件中,对包源路径指定,如

url --url="http://192.168.32.72/os/6i386/"

还有菜单文件中,ks路径的指定

 append initrd=6i386/initrd.img ks=http://192.168.32.72/ksdir/ks65desk.cfg

还有,dhcp分配的网络段的分配,和当前的网络是可通的

在脚本中,有几个地方需要注意

共准备了三个版本的光盘,32位的6.5版本,64位的6.9版本,和64位的7版本

挂载:/var/www/html/os/{6i386  6x86_64  7 }三个文件夹分别挂载对应的光盘,这里建议手动挂载

启动引导文件:建议手动内核和虚拟根文件到/var/lib/tftpboot{ 6i386  6x86_64  7}这三个文件夹下

ks文件,建议提前准备好,也是手动复制到/var/www/html/ksdir下

虽然脚本实现了自动化的复制,但是前提是/dev/sr0 ---> 6x86_64 ,/dev/sr1 ---> 6i386 ,/dev/sr2 ---> 7

脚本内容如下

#!/bin/bash
#
#******************************************************************************
#Author:               Sunny
#Date:                 2017-09-27
#FileName:             install_pxe_env.sh
#version:              1.0
#Your change info:     
#Description:          For install PXE_environment auto
#DOC URL:               
#Copyright(C):         2017  All rihts reserved
#*****************************************************************************
restart_dhcpd() { 
service dhcpd restart &> /dev/null ;
chkconfig dhcpd on &> /dev/null ;
}
restart_tftp () {
sed  -i  's/disable.*/disble = no/g'  /etc/xinetd .d /tftp  &> /dev/null ;
chkconfig tftp on &> /dev/null ;
service xinetd restart &> /dev/null ;
}
restart_httpd(){
service httpd restart &> /dev/null ;
chkconfig httpd on &> /dev/null ;
}
min_time () {
    time =` date  +%Y%m%d%H%M`
}
ip=$( ifconfig  awk  '/inet /{print $2}' awk  -F :  '{print $NF}' head  -1)
min_time;
#install server
echo  "Now install dhcp,http,tftp server and tftp client,it might take few minites"
rpm -q httpd &> /dev/null  && restart_httpd || { yum -y  install  httpd &> /dev/null ;restart_httpd; }
rpm -q dhcp &> /dev/null  && restart_dhcpd ||  { yum -y  install  dhcp&> /dev/null ;restart_dhcpd; }
rpm -q tftp-server &> /dev/null  && restart_tftp  || { yum -y  install  tftp-server &> /dev/null ;restart_tftp; }
rpm -q tftp &> /dev/null  || yum -y  install  tftp &> /dev/null ;
#For centos 6.9,file pxelinux.0 comes from packges syslinux-nonlinux,other version is from syslinux,but if you install packge syslinux,it will also install syslinux-nonlinux packge,so just install syslinux packge is OK to get file syslinux-nonlinux.
rpm -q syslinux &> /dev/null  || yum -y  install  syslinux &> /dev/null ;
  
#set http for yum server
#mount yum source
echo  "Now you need to config  yum server in http server"
echo  "Since I do not know which different disc your disc will be displayed,such as,centos7 display as /dev/sr0,or display as/dev/sr1 or other device "
echo  "This time,I have three disks,6i386 means 386 arch for ceentos6.5,6x86_64 means 64bit for centos6.9,7 means centos7.3"
echo  "the relation of my disk  dislay in the centos as below"
echo  -e  "/dev/sr0 ---> 6x86_64 \n/dev/sr1 ---> 6i386 \n/dev/sr2 ---> 7"
mkdir  -p  /var/www/html/os/ {6i386,6x86_64,7}
read  -p  "Would you want to mount disk auto:(eg:y/n): "  automount
case  $automount  in
y)
echo  -e  "/dev/sr0 ---> 6x86_64 \n/dev/sr1 ---> 6i386 \n/dev/sr2 ---> 7"
mount  /dev/sr0    /var/www/html/os/6x86_64
mount  /dev/sr1    /var/www/html/os/6i386
mount  /dev/sr2    /var/www/html/os/7
;;
*)
echo  "Since your answer is no or other,please mount disk after the script end"
echo  "eg: run    mount /dev/sr0   /var/www/html/os/6x86_64  "
echo  "eg: if your want to mount the disk fixed,please write to /etc/fstab,eg: /dev/sr0        /var/www/html/os/6x86_64      iso9660    defaults        0 0 "
;;
esac
  
#prepare ks file in /var/www/html/ksdir
echo  "If your have put ks file in other hosts,your should input remote ,and input the remote file full path,and it will use scp cmd to copy the ks file directory to /var/www/html/ksdir"
echo  "If you put dir in local host,input local,it will will cp cmd to  copy the ks file directory to /var/www/html/ksdir"
echo  "if you input any other,you should cp ksdir to /var/www/html/ksdir"
read  -p  "Do you want to copy remote ks dir( r (remote) or l (local) or any other input ): "  ifcopy
mkdir  -p  /var/www/html/ksdir/
case  $ifcopy  in 
r|remote)
read  -p  "input the remote ksdir directory(defaults:[email protected]:/var/www/html/ksdir/*): "  ksdir 
if  [ -z ${ksdir:-} ]; then
ksdir= "[email protected]:/var/www/html/ksdir/*"
fi
read  -p  "input host user's password you have put: "  passwd
    expect -c "
    spawn  scp  $ksdir  /var/www/html/ksdir/
    expect {
            \"*assword\" { set  timeout 300; send \"$ passwd \r\"; }
            \" yes /no \" { send \" yes \r\"; exp_continue; }
    }
    expect eof"
      
    ls  /var/www/html/ksdir  while  read  ksfile;  do
    sed  -i  "s@url --url=\"http://.*/os/@url --url=\"http://$ip/os/@g"  /var/www/html/ksdir/ $ksfile
    done
  
;;
l| local  )
read  -p  "Please input your local ks directory(eg:/root/ksdir/*): "  ksdir
cp  $ksdir  /var/www/html/ksdir/
;;
*)
echo  "your input is wrong,please manual copy ksdir to /var/www/html/ksdir/"
;;
esac
  
#config dhcp
echo  "Now config dhcp server..."
read  -p  "Input your next-server ip(default is your host ip): "  nextip
if  [ -z ${nextip:-} ]; then
nextip= "$ip"
fi
echo  nextip is  "$nextip"
mv  /etc/dhcp/dhcpd .conf  /etc/dhcp/dhcpd .conf. "$time" .bak
cat  > /etc/dhcp/dhcpd .conf<<eof
option domain-name  "sunny.com" ;
option domain-name-servers 192.168.32.61;
default-lease- time  86400;
max-lease- time  86400;
subnet 192.168.32.0 netmask 255.255.255.0 {
range 192.168.32.100 192.168.32.200;
option routers 192.168.32.1;
next-server $nextip;
filename  "pxelinux.0" ;
}
log-facility local7;
eof
  
echo  "As default,the dhcp server will alocate ip 192.168.32.100--192.168.32.200,dns server is 192.168.32.61,router is 192.168.32.1,if you want to change these config ,please run 'vim /etc/dhcp/dhcpd.conf' to change /etc/dhcp/dhcpd.conf"
  
echo  "dhcp is complete config now"
  
#config tltp 
echo  "If you want to copy linux kernel file to /var/lib/tftpboot/{6i386,6x86_64,7}"
read  -p  "Please input m(means manual) or a (means auto): "  copyfile
case  $copyfile  in
a)
mkdir  -p  /var/lib/tftpboot/ {6i386,6x86_64,7}
cp  /var/www/html/os/6i386/isolinux/ {initrd.img,vmlinuz}  /var/lib/tftpboot/6i386
cp  /var/www/html/os/6x86_64/isolinux/ {initrd.img,vmlinuz}  /var/lib/tftpboot/6x86_64
cp  /var/www/html/os/7/isolinux/ {initrd.img,vmlinuz}    /var/lib/tftpboot/7
;;
*)
echo  "Your input is m or other things"
echo  "you should do two things after the scripts"
echo  "create directory under /var/lib/tftpboot/,such as mkdir /var/lib/tftpboot/6i386"
echo  "copy the kernel file to the relative dir,such as cp /var/www/html/os/6i386/isolinux/{initrd.img,vmlinuz} /var/lib/tftpboot/6i386"
echo  "If you have many other yum source,please create relative dir under /var/lib/tftpboot/6i386"
;;
esac
  
cp  /usr/share/syslinux/ {pxelinux.0,menu.c32}  /var/lib/tftpboot/
  
echo  "Now config defaults file ,you can copy the host relative disk file ,/media/isolinux/isolinux.cfg to /var/lib/tftpboot/pxelinux.cfg,and rename it to defaults,and the modify the config,run cmd 'cp /media/isolinux/isolinux.cfg /var/lib/tftpboot/    pxelinux.cfg/default' "
echo  "Now I will config /var/lib/tftpboot/pxelinux.cfg/default"
mkdir  /var/lib/tftpboot/pxelinux .cfg
cat  /var/lib/tftpboot/pxelinux .cfg /default <<eof
  
default menu.c32
#prompt 1
timeout 80
  
display boot.msg
  
menu background splash.jpg
menu title Welcome to Sunny diy  install  Linux!
menu color border 0  #ffffffff #00000000
menu color sel 7  #ffffffff #ff000000
menu color title 0  #ffffffff #00000000
menu color tabmsg 0  #ffffffff #00000000
menu color unsel 0  #ffffffff #00000000
menu color hotsel 0  #ff000000 #ffffffff
menu color hotkey 7  #ffffffff #ff000000
menu color scrollbar 0  #ffffffff #00000000
  
label desktop73
  menu label Install diy ^desktop centos 7
  menu default
  kernel 7 /vmlinuz
  append initrd=7 /initrd .img ks=http: // $ip /ksdir/ks73desk .cfg
label mini73
  menu label Install diy ^mini centos 7
  menu default
  kernel 7 /vmlinuz
  append initrd=7 /initrd .img ks=http: // $ip /ksdir/ks73min .cfg
label desktop6.5
  menu label Installed d^esktop centos 6.5 i386
  kernel 6i386 /vmlinuz
  append initrd=6i386 /initrd .img ks=http: // $ip /ksdir/ks65desk .cfg
label mini6.5
  menu label Install m^ini centos 6.5 i386 
  kernel 6i386 /vmlinuz
  append initrd=6i386 /initrd .img ks=http: // $ip /ksdir/ks65min .cfg
label desktop6.9
  menu label Installed de^sktop centos 6.9 
  kernel 6x86_64 /vmlinuz
  append initrd=6x86_64 /initrd .img ks=http: // $ip /ksdir/ks69desk .cfg
label mini6.9
  menu label Install mi^ni centos 6.9 
  kernel 6x86_64 /vmlinuz
  append initrd=6x86_64 /initrd .img ks=http: // $ip /ksdir/ks69min .cfg
eof
  
echo  "tftp is config OK"
  
#restart server
echo  "now restart server"
restart_httpd;
restart_tftp;
restart_dhcpd;
netstat  -ntulp |  grep  dhcpd |  grep  :67 &> /dev/null  &&  echo  "dhcp is running..."  ||  echo  "dhcp is not run,please check"
netstat  -ntulp |  grep  httpd |  grep  :80 &> /dev/null  &&  echo  "http is running..."  ||  echo  "http is not run,please check"
netstat  -ntulp |  grep  xinetd |  grep  69 &> /dev/null  &&  echo  "tftp is running..."  ||  echo  "tftp is not run,please check"

5 小结

关于PXE和cobbler安装,两个实现的效果一样,都是自动化安装系统,而且实现原理上也基本一致,但是,由于cobbler是PXE的改良版,所有个人感觉,cobbler的安装更加方便。

文章末尾,附上的PXE安装环境,也类似实现了自动化安装PXE,自动拷贝了脚本,当用户同样是挂载三个光盘,而且挂载的路径和文章中介绍的一样,就可以实现自动化挂载,同时,需要准备了相关的ks文件,放到指定目录下时,根据脚本的提示也可以实现自动化拷贝到相关目录下。文末附件将附上脚本中的6个ks文件,dhcp配置和菜单文件的设置。

可以到Linux公社资源站下载:

------------------------------------------分割线------------------------------------------

具体下载目录在 /2017年资料/10月/1日/基于PXE 和 Cobbler 自动安装Linux系统/

------------------------------------------分割线------------------------------------------

猜你喜欢

转载自www.linuxidc.com/Linux/2017-10/147226.htm