使用Cobbler部署操作系统

Cobbler是一个系统启动服务,可以通过网络启动的方式来快速部署虚拟机和服务器。Cobbler使用python开发,使用简单的命令即可完成PXE网络环境的配置,能够支持windows和linux的各种发行版本。。

cobbler部署环境

cobbler安装部署时,需要一台虚拟机或者物理机作为服务器(server端),通过该服务器完成对其他待部署操作系统安装。该server端需要支持PXE服务、DHCP服务、DNS服务、电源管理(可选)、Kickstart服务(可选)、TFTP服务、Apache服务。
本文以CentOS6.5为例,部署cobbler服务。
环境:CentOS6.5 64位
IP地址:192.168.40.1
子网掩码:255.255.255.0
网关:192.168.40.254
DHCP动态IP范围:192.168.40.10~192.168.40.250
网卡:eth0
服务器开启PXE网络启动

部署环境准备

首先安装一台centos6.5的机器,配好IP地址。

  • 关闭SELINUX

    vi /etc/selinux/config
    SELINUX=disabled
    setenforce 0

  • 关闭防火墙或者打开以下端口

    #关闭防火墙
    service iptables stop
    chkconfig iptables off
    #或者打开TCP 80和25151端口,UDP 69端口:
    vi /etc/sysconfig/iptables
    -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT #http服务需要此端口
    -A RH-Firewall-1-INPUT -m state --state NEW -m udp -p udp --dport 69 -j ACCEPT #tftp服务需要此端口
    -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 25151 -j ACCEPT #cobbler需要此端口
    service iptables restart
  • 修改主机名

    vi /etc/sysconfig/network
    HOSTNAME=cobbler

  • 配置yum源
    由于cobbler不在基本的yum源中,所以还需要安装epel源仓库:

    cd /usr/local/src
    wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
    #CentOS6.x 64
    rpm –ivh epel-release-6-8.noarch.rpm
    yum clean all
    yum makecache
    yum repolist

安装cobbler

yum –y install cobbler
yum install –y tftp-server xinetd dhcp http rsync pykickstart debmirror python-ctypes cman
service cobblerd start

设置http服务

  • 配置httpd配置文件

    vi /etc/httpd/conf/httpd.conf
    ServerName 127.0.0.1:80
  • 安装mod_wsgi

    yum –y install mod_wsgi
    #将/etc/httpd/conf.d/wsgi.conf中的LoadModule wsgi_module modules/mod_wsgi.so行的注释打开。
  • 启动http服务

    service httpd start
    chkconfig httpd on

设置tftp服务

vi /etc/xinetd.d/tftp
disable = no

设置rsync服务

vi /etc/xinetd.d/rsync
disable = no
//启动rsync
service xinetd start

配置cobbler相关参数

vi /etc/debmirror.conf
//注释掉下面两句:
#@dists=”sid”;
#@arches=”i386”;

配置cobbler主配置文件

在开启cobbler服务之前,需要修改一些配置文件,在修改配置文件之前最好做一些备份。

vi /etc/cobbler/settings
server:192.168.40.1 #server设置的IP为cobbler服务器指定的地址
next_server:192.168.40.1 #next_server是DHCP/PXE网络引导文件被下载的TFTP服务器的IP,这里我们和server设置为同一个IP
pxe_just_once:1 #为了防止误重装系统,可以做如下修改,PXE安装只允许一次,防止误操作

生成cobbler安装系统的root初始密码

openssl passwd -1 -salt 'random-phrase-here' '111111'
#将得到的字符串加到setting的配置文件中 default_password_crypted:”刚才得到的字符串”

修改

manage_rsync:1 #对rsync进行管理
manage_dhcp:1 #对dhcp进行管理

对cobbler管理dhcp的模板修改

修改模板文件 /etc/cobbler/dhcp.template 此文件是cobbler管理dhcp的模板,确保dhcp分配的地址和cobbler在同一网段,对于此文件,我们做如下部分的修改:

vi /etc/cobbler/dhcp.template
subnet 192.168.40.0 netmask 255.255.255.0 { #设置网段
option routers 192.168.40.254; #设置网关
option domain-name-servers 114.114.115.115; #设置dns服务器地址
option subnet-mask 255.255.255.0; #设置子网掩码
range dynamic-bootp 192.168.40.10 192.168.40.250; #设置dhcp服务器IP地址租用的范围
default-lease-time 21600; #默认租约时间
max-lease-time 43200; #最大租约时间
next-server $next_server;
class "pxeclients" {
match if substring (option vendor-class-identifier, 0, 9) = "PXEClient";
if option pxe-system-type = 00:02 {
filename "ia64/elilo.efi";
} else if option pxe-system-type = 00:06 {
filename "grub/grub-x86.efi";
} else if option pxe-system-type = 00:07 {
filename "grub/grub-x86_64.efi";
} else {
filename "pxelinux.0";
}
}
}

指定DHCP服务的网络接口

vi /etc/sysconfig/dhcpd
DHCPDARGS=eth0

加载部分缺失的boot-loaders

cobbler get-loaders #如果该步骤无法执行可以将文件下载下来保存到/var/lib/cobbler/loads/中

启动cobbler服务

执行dhcpd测试dhcp服务器配置是否正确

chkconfig dhcpd on
Chkconfig cobblerd on
Service cobblerd restart
cobbler sync #同步配置文件到dhcp服务器中
#如果出现错误时bug,重新启动一遍cobbler即可
service dhcpd start

配置cobbler相关服务启动脚本

vi /etc/rc.d/init.d/cobbler
#!/bin/sh
# chkconfig: - 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>&1 >/dev/null &
;;
esac

添加脚本的执行权限

chmod +x /etc/rc.d/init.d/cobbler
chkconfig cobbler on
service cobbler restart
cobbler check

挂载镜像

  • 新建目录,将需要的ISO镜像上传到该目录中

    mkdir /OSsoftware
  • 创建文件夹

    mkdir –p /var/www/html/os/centos6.5-86_64  #创建挂载目录
  • 将ISO镜像挂载到该目录中,如果有多个镜像需要挂载,重复即可。

    mount –t iso9660 –o loop /OSsoftware/centos6.5-DVD1.iso /var/www/html/os/ centos6.5-86_64
    vi /etc/fstab 中添加一下代码实现开机自动挂载
    /OSsoftware/centos6.5-DVD1.iso /var/www/html/os/ centos6.5-86_64 iso9660 defaults,ro,loop 0 0
    其中iso9660使用df –T查看设备 如需卸载设备执行以下命令:
    umount /var/www/html/os/ centos6.5-86_64
  • 导入系统镜像到cobbler中
    执行以下语句将镜像挂载到cobbler中

    cobbler import --path=/var/www/html/os/centos6.5-86_64 --name= centos6.5-86_64 --arch=x86_64
    #其中安装源的name即为安装时显示的名称,arch目前支持x86|x86_64|ia64这三种完成后会出现以下提示:associating kickstarts*** TASK COMPLETE ***
  • 比较大小
    最后可以通过比较两个文件的大小来判断导入是否正常:

    du -sh /var/www/cobbler/ks_mirror/centos6.5-86_64-x86_64
    du -sh /var/www/html/os/centos6.5-86_64
  • 重新导入
    重新导入之前需要将原来的删除,执行以下命令:

    cobbler remove –name=[ centos6.5-86_64]

cobbler WEB服务

yum install cobbler-web
vi /etc/cobbler/modules.conf
module = authn_configfile #修改认证方式为密码文件类型
htdigest /etc/cobbler/users.digest "Cobbler" cobbler #添加cobbler用户,提示输入2遍密码确认
cobbler sync #/输出*** TASK COMPLETE *** 表示配置无错误
/etc/init.d/httpd restart
/etc/init.d/cobblerd restart

VMWARE vsphere exsi 5.5u2

cat >>/var/lib/cobbler/kickstarts/esxi5-ks.cfg <<EOF
#Test ESXi 5 ks file
#Accept the VMware End User License Agreement
vmaccepteula
# Set the root password for the DCUI and Tech Support Mode
rootpw 1111111
clearpart --firstdisk=local --overwritevmfs
#Part disk
install --firstdisk=local --overwritevmfs
#Set the network to DHCP IP on the first network adapater
network --bootproto=dhcp --device=vmnic0
#Final reboot
reboot --noeject
EOF
cobbler sync
cobbler list
cobbler report

CentOS7

cat  > /var/lib/cobbler/kickstarts/centos7-ks.cfg <<EOF
#version=CentOS7
# (includes %end blocks)
# do not use with earlier distros
# Use network installation
url --url=http://192.168.40.1/cobbler/ks_mirror/centos7.0-86_64-x86_64

#platform=x86, AMD64, or Intel EM64T
# System authorization information
auth --useshadow --enablemd5
# System bootloader configuration
bootloader --location=mbr
# Partition clearing information
clearpart --all --initlabel
# Use text mode install
text
# Firewall configuration
firewall --enabled
# Run the Setup Agent on first boot
firstboot --disable
# System keyboard
keyboard us
# System language
lang en_US.UTF-8
# If any cobbler repo definitions were referenced in the kickstart profile, include them here.
$yum_repo_stanza
# Network information
$SNIPPET('network_config')
# Reboot after installation
reboot

#Root password
rootpw --iscrypted $default_password_crypted
# SELinux configuration
selinux --disabled
# Do not configure the X Window System
skipx
# System timezone
timezone Asia/shanghai --CST
# Install OS instead of upgrade
install
# Clear the Master Boot Record
zerombr
# Allow anaconda to partition the system as needed
autopart

%pre
$SNIPPET('log_ks_pre')
$SNIPPET('kickstart_start')
$SNIPPET('pre_install_network_config')
# Enable installation monitoring
$SNIPPET('pre_anamon')
%end

%packages
wget
vim
screen
$SNIPPET('func_install_if_enabled')
%end

%post --nochroot
$SNIPPET('log_ks_post_nochroot')
%end

%post
$SNIPPET('log_ks_post')
# Start yum configuration
$yum_config_stanza
# End yum configuration
$SNIPPET('post_install_kernel_options')
$SNIPPET('post_install_network_config')
$SNIPPET('func_register_if_enabled')
$SNIPPET('download_config_files')
$SNIPPET('koan_environment')
$SNIPPET('redhat_register')
$SNIPPET('cobbler_register')
# Enable post-install boot notification
$SNIPPET('post_anamon')
# Start final steps
$SNIPPET('kickstart_done')
# End final steps
%end
EOF

cobbler profile edit --name=centos7.0-86_64-x86_64 --kickstart=/var/lib/cobbler/kickstarts/centos7-ks.cfg
cobbler profile report

cobbler sync
cobbler list
cobbler report