环境准备:

一台服务器

[root@admin tftpboot]# cat /etc/redhat-release 
CentOS Linux release 7.2.1511 (Core) 
[root@admin tftpboot]# uname -a
Linux admin 3.10.0-327.el7.x86_64 #1 SMP Thu Nov 19 22:10:57 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
[root@admin tftpboot]# 

 ###########################pxe+kickstart###############################

pxe+kickstart自动装机原理:

PXE(preboot execute environment,预启动执行环境)是由Intel公司开发的最新技术,工作于Client/Server的网络模式,支持工作站通过网络从远端服务器下载映像,并由此支持通过网络启动操作系统,在启动过程中,终端要求服务器分配IP地址,再用TFTP(trivial file transfer protocol)或MTFTP(multicast trivial file transfer protocol)协议下载一个启动软件包到本机内存中执行,由这个启动软件包完成终端(客户?)基本软件设置,从而引导预先安装在服务器中的终端操作系统。PXE可以引导多种操作系统,如:Windows95/98/2000/windows2003/windows2008/winXP/win7/win8,linux等。

原理图

pxe+kickstart cobbler无人值守装机pxe+kickstart cobbler无人值守装机

 

准备条件:

一个镜像文件

httpd服务

tftp服务

dhcp服务

 

设置步骤:

挂载ISO镜像
[root@admin tftpboot]# mount /dev/cdrom /mnt/ [root@admin tftpboot]# yum install httpd dhcp tftp-server createrepo xinetd -y [root@admin tftpboot]# mkdir /var/www/html/CentOS-7.1-x86_64/ /va/www/html是httpd默认的资源目录 [root@admin tftpboot]# cp -a /mnt/* /var/www/html/CentOS-7.1-x86_64/ [root@admin tftpboot]# createrepo -pdo /var/www/html/CentOS-7.1-x86_64/ /var/www/html/CentOS-7.1-x86_64/ 制作yum仓库 [root@admin tftpboot]# createrepo -g `ls /var/www/html/CentOS-7.1-x86_64/*-comps.xml` /var/www/html/CentOS-7.1-x86_64/ [root@admin tftpboot]# vim /etc/xinetd.d/tftp disable = no [root@admin dhcp-4.2.5]# cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example /etc/dhcp/dhcpd.conf 设置dhcp服务 subnet 192.168.220.0 netmask 255.255.255.0 { range dynamic-bootp 192.168.220.10 192.168.220.90; option subnet-mask 255.255.255.0; next-server 192.168.220.165; filename "pxelinux.0"; } [root@admin dhcp-4.2.5]# service dhcpd start Redirecting to /bin/systemctl start dhcpd.service [root@admin dhcp-4.2.5]# service httpd start Redirecting to /bin/systemctl start httpd.service [root@admin dhcp-4.2.5]# service xinetd start Redirecting to /bin/systemctl start xinetd.service [root@admin dhcp-4.2.5]# 上传ks.cfg文件至/var/www/html/CentOS-7.1-x86_64/ [root@admin dhcp-4.2.5]# yum install syslinux -y 安装syslinux是为了得到 pxelinux.0文件 [root@admin dhcp-4.2.5]# cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/ [root@admin dhcp-4.2.5]# cp /mnt/isolinux/* /var/lib/tftpboot/ [root@admin tftpboot]# mkdir /var/lib/tftpboot/pxelinux.cfg [root@admin tftpboot]# cp /mnt/isolinux/isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default [root@admin pxelinux.cfg]# vim default label linux menu label ^AUTO Install CentOS 7 kernel vmlinuz append initrd=initrd.img ks=http://192.168.220.165/CentOS-7.1-x86_64/CentOS-7.1-x86_64.cfg [root@admin pxelinux.cfg]# getenforce Permissive [root@admin pxelinux.cfg]# setenforce 0 [root@admin pxelinux.cfg]# vim /etc/selinux/config SELINUX=disabled [root@admin pxelinux.cfg]# service firewalld stop Redirecting to /bin/systemctl stop firewalld.service [root@admin pxelinux.cfg]#
设置开机关闭防火墙
[root@admin pxelinux.cfg]# systemctl disable firewalld.service
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.
[root@admin pxelinux.cfg]# systemctl list-unit-files|grep firewalld
firewalld.service                           disabled
[root@admin pxelinux.cfg]# 

 所需要的kickstart配置文件

[root@admin pxelinux.cfg]# cat /var/www/html/CentOS-7.1-x86_64/CentOS-7.1-x86_64.cfg 
#Kickstart Configurator by Jason Zhao
#platform=x86, AMD64, or Intel EM64T
#System  language
lang en_US
#System keyboard
keyboard us
#Sytem timezone
timezone Asia/Shanghai
#Root password
#rootpw --iscrypted $default_password_crypted
rootpw --iscrypted $1$ops-node$7hqdpgEmIE7Z0RbtQkxW20
#Use text mode install
text
#Install OS instead of upgrade
install
#Use NFS installation Media
#url --url=$tree
url --url=http://192.168.220.165/CentOS-7.1-x86_64
#System bootloader configuration
bootloader --location=mbr
#Clear the Master Boot Record
zerombr
#Partition clearing information
clearpart --all --initlabel 
#Disk partitioning information
part /boot --fstype xfs --size 1024 --ondisk sda
part swap --size 16384 --ondisk sda
part / --fstype xfs --size 1 --grow --ondisk sda
#System authorization infomation
auth  --useshadow  --enablemd5 
#Network information
#$SNIPPET('network_config')
network --bootproto=dhcp --device=eth0 --onboot=on
# Reboot after installation
reboot
#Firewall configuration
firewall --disabled 
#SELinux configuration
selinux --disabled
#Do not configure XWindows
skipx

#Package install information
%packages
@ base
@ core
sysstat
iptraf
ntp
lrzsz
ncurses-devel
openssl-devel
zlib-devel
OpenIPMI-tools
mysql
nmap
screen
%end

%post
systemctl disable postfix.service
%end
[root@admin pxelinux.cfg]# 

 

###########################cobbler###############################

一.环境准备

yum install net-tools -y

  CentOS7需要更换epel源   详见博客: http://www.cnblogs.com/along1226/p/5317833.html

更换epel源,epel是yum的一个软件用源,包含了很多基本源中没有的软件,cobbler就属于基本源中没有的软件,所以需要安装epel源

yum源:

  wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo

epel源:

  wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo

二.安装cobbler

[root@admin ~]# yum install cobbler cobbler-web dhcp tftp-server pykickstart httpd -y

看看都装了什么东西

[root@linux-node1 ~]# rpm -ql cobbler     #查看安装了什么文件,下面列出重要部分来注释
/etc/cobbler                               #配置文件目录
/etc/cobbler/settings                    #cobbler主配置文件,这个文件是YAML格式,cobbler是Python写的程序
/etc/cobbler/dhcp.template            #DHCP服务的配置模板
/etc/cobbler/tftpd.template          #tftp服务的配置模板
/etc/cobbler/rsync.template           #rsync服务的配置模板
/etc/cobbler/iso              # iso模板配置文件目录
/etc/cobbler/pxe              # pxe模板文件目录
/etc/cobbler/power            # 电源的配置文件目录
/etc/cobbler/users.conf       # Web服务授权配置文件
/etc/cobbler/users.digest     # 用于web访问的用户名密码配置文件
/etc/cobbler/dnsmasq.template # DNS服务的配置模板
/etc/cobbler/modules.conf     # Cobbler模块配置文件

/var/lib/cobbler              # Cobbler数据目录
/var/lib/cobbler/config       # 配置文件
/var/lib/cobbler/kickstarts   # 默认存放kickstart文件
/var/lib/cobbler/loaders      # 存放的各种引导程序

/var/www/cobbler              # 系统安装镜像目录
/var/www/cobbler/ks_mirror    # 导入的系统镜像列表
/var/www/cobbler/images       # 导入的系统镜像启动文件
/var/www/cobbler/repo_mirror  # yum源存储目录

/var/log/cobbler              # 日志目录
/var/log/cobbler/install.log  # 客户端系统安装日志
/var/log/cobbler/cobbler.log  # cobbler日志

 

三.启动cobbler   

启动cobbler之前应该先启动httpd

systemctl restart httpd.servie

注意:第一次启动cobbler的时候如果报错,只需要重新启动即可

四.配置cobbler

启动之后还不能立即就用,还需要修改cobbler的配置文件,使用命令cobbler check命令查看我们需要修改哪些配置。

[root@admin yum.repos.d]# ps -ef|grep cobbler
apache    5925  5924  0 14:00 ?        00:00:00 (wsgi:cobbler_w -DFOREGROUND
root      5976     1  4 14:01 ?        00:00:00 /usr/bin/python2 /usr/bin/cobblerd -F
root      5985  2279  0 14:02 pts/1    00:00:00 grep --color=auto cobbler
[root@admin yum.repos.d]# cobbler check
The following are potential configuration items that you may want to fix:

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 *recent* version 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 : enable and start rsyncd.service with systemctl
5 : debmirror package is not installed, it will be required to manage debian deployments and repositories
6 : 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
7 : fencing tools were not found, and are required to use the (optional) power management features. install cman or fence-agents to use them

Restart cobblerd and then run 'cobbler sync' to apply changes.
[root@admin yum.repos.d]# 

 

 1 sed -i 's/server: 127.0.0.1/server: 10.0.0.6/' /etc/cobbler/settings     
 2 sed -i 's/manage_dhcp: 0/manage_dhcp: 1/' /etc/cobbler/settings
 3 sed -i 's/pxe_just_once: 0/pxe_just_once: 1/' /etc/cobbler/settings
 4 cobbler get-loaders   

[root@admin yum.repos.d]# openssl passwd -1 -salt 'chenyao' 123456 ---->  123456就是我们系统被自动安装之后的初始密码
$1$chenyao$QHgE3Wwj5X6DFyEP1YifY0       
[root@admin yum.repos.d]#

default_password_crypted: "$1$test$at615QShYKduQlx5z9Zm7/"     //修改setting中的这个一行,替换为生成的密码,

注意:如果是CentOS7中在/etc/xinetd.d/目录下是没有rsync这个文件的,需要创建并加入如下的参数,CentOS6中有rsync文件,只需修改即可

# default: off
# # description: The rsync server is a good addition to an ftp server, as it \
# #       allows crc checksumming etc.
 service rsync
 {
    disable = no
    flags           = IPv6
    socket_type     = stream
    wait            = no
    user            = root
    server          = /usr/bin/rsync
    server_args     = --daemon
    log_on_failure  += USERID
 }

 配置dhcp

 vim /etc/cobbler/dhcp.template

subnet 192.168.220.0 netmask 255.255.255.0 {
     option routers             192.168.220.2;
     option domain-name-servers 192.168.220.2;
     option subnet-mask         255.255.255.0;
     range dynamic-bootp        192.168.220.10 192.168.220.90;

执行以上操作之后,就已经改的差不多了,就算执行了cobbler check还有报错叶不用理会,当修改了cobbler的配置之后需要执行cobbler sync让配置生效。

[root@admin yum.repos.d]# cobbler sync

[root@admin yum.repos.d]# cobbler sync
task started: 2016-03-24_143129_sync
task started (id=Sync, time=Thu Mar 24 14:31:29 2016)
running pre-sync triggers
cleaning trees
removing: /var/lib/tftpboot/pxelinux.cfg/default
removing: /var/lib/tftpboot/grub/images
removing: /var/lib/tftpboot/grub/grub-x86.efi
removing: /var/lib/tftpboot/grub/grub-x86_64.efi
removing: /var/lib/tftpboot/grub/efidefault
removing: /var/lib/tftpboot/s390x/profile_list
copying bootloaders
trying hardlink /var/lib/cobbler/loaders/pxelinux.0 -> /var/lib/tftpboot/pxelinux.0
copying: /var/lib/cobbler/loaders/pxelinux.0 -> /var/lib/tftpboot/pxelinux.0
trying hardlink /var/lib/cobbler/loaders/menu.c32 -> /var/lib/tftpboot/menu.c32
copying: /var/lib/cobbler/loaders/menu.c32 -> /var/lib/tftpboot/menu.c32
trying hardlink /var/lib/cobbler/loaders/grub-x86.efi -> /var/lib/tftpboot/grub/grub-x86.efi
trying hardlink /var/lib/cobbler/loaders/grub-x86_64.efi -> /var/lib/tftpboot/grub/grub-x86_64.efi
copying distros to tftpboot
copying images
generating PXE configuration files
generating PXE menu structure
rendering DHCP files
generating /etc/dhcp/dhcpd.conf
rendering TFTPD files
generating /etc/xinetd.d/tftp
cleaning link caches
running post-sync triggers
running python triggers from /var/lib/cobbler/triggers/sync/post/*
running python trigger cobbler.modules.sync_post_restart_services
running: dhcpd -t -q
received on stdout: 
received on stderr: 
running: service dhcpd restart
received on stdout: 
received on stderr: Redirecting to /bin/systemctl restart  dhcpd.service

running shell triggers from /var/lib/cobbler/triggers/sync/post/*
running python triggers from /var/lib/cobbler/triggers/change/*
running python trigger cobbler.modules.scm_track
running shell triggers from /var/lib/cobbler/triggers/change/*
*** TASK COMPLETE ***
[root@admin yum.repos.d]#  

同步完成之后可以执行less /etc/dhcp/dhcpd.conf   查看一下dhcp的设置是否生效了

cobbler的日志

[root@admin cobbler]# ll
total 20
drwxr-xr-x 2 root root     6 Jan 24 09:40 anamon
-rw-r--r-- 1 root root 16209 Mar 24 14:31 cobbler.log
drwxr-xr-x 2 root root     6 Jan 24 09:40 kicklog
drwxr-xr-x 2 root root     6 Jan 24 09:40 syslog
drwxr-xr-x 2 root root  4096 Mar 24 14:31 tasks
[root@admin cobbler]# 

五.启动相关的服务 

因为cobbler的使用需要httpd,tftp,dhcp,xinetd,cobblerd服务的配合,所以想要成功实现自动化装机,就需要启动这么多个服务,那我们就通过一个脚本来启动,关闭,重启这些服务。

#!/bin/bash
# chkconfig: 345 80 90
# description:cobbler
case $1 in
  start)
    /etc/init.d/httpd start
    /etc/init.d/xinetd start
    /etc/init.d/dhcpd start
    /etc/init.d/cobblerd start
    ;;
  stop)
    /etc/init.d/httpd stop
    /etc/init.d/xinetd stop
    /etc/init.d/dhcpd stop
    /etc/init.d/cobblerd stop
    ;;
  restart)
    /etc/init.d/httpd restart
    /etc/init.d/xinetd restart
    /etc/init.d/dhcpd restart
    /etc/init.d/cobblerd restart
    ;;
  status)
    /etc/init.d/httpd status
    /etc/init.d/xinetd status
    /etc/init.d/dhcpd status
    /etc/init.d/cobblerd status
    ;;
  sync)
    cobbler sync
    ;;
  *)
    echo "Input error,please in put 'start|stop|restart|status|sync'!"
    exit 2
    ;;
esac
脚本

相关文章: