cobbler自动化安装Linux系统

cobbler简介

Cobbler是一个Linux服务器快速网络安装的服务,而且在经过调整也可以支持网络安装windows。该工具使用python开发,小巧轻便(才15k行python代码),可以通过网络启动(PXE)的方式来快速安装、重装物理服务器和虚拟机,同时还可以管理DHCP,DNS,TFTP、RSYNC以及yum仓库、构造系统ISO镜像。
Cobbler可以使用命令行方式管理,也提供了基于Web的界面管理工具(cobbler-web),还提供了API接口,可以方便二次开发使用。
Cobbler是kickstart的升级版,优点是比较容易配置,还自带web界面比较易于管理。
Cobbler客户端Koan支持虚拟机安装和操作系统重新安装,使重装系统更便捷。

cobbler安装部署

操作系统:CentOS release 7.1

防火墙、Selinux关闭

安装eple源

# yum -y install epel-release

安装cobbler所需要的软件包

# yum -y install cobbler cobbler-web pykickstart httpd dhcp tftp-server

启动服务

# /etc/init.d/httpd start
# /etc/init.d/cobblerd start

初次启动httpd会有报错

Starting httpd: httpd: apr_sockaddr_info_get() failed for Template
httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
#遇见报错需要修改apache的配置文件,添加一行
# vim /etc/httpd/conf/httpd.conf
ServerName localhost:80

启动服务后,使用cobbler check检查配置文件查看是否有问题,一般第一次启动的时候会有报错,不过不用管,重启cobbler后就会正常

# cobbler check
Traceback (most recent call last):
  File "/usr/bin/cobbler", line 36, in <module>
    sys.exit(app.main())
  File "/usr/lib/python2.6/site-packages/cobbler/cli.py", line 657, in main
    rc = cli.run(sys.argv)
  File "/usr/lib/python2.6/site-packages/cobbler/cli.py", line 270, in run
    self.token         = self.remote.login("", self.shared_secret)
  File "/usr/lib64/python2.6/xmlrpclib.py", line 1199, in __call__
    return self.__send(self.__name, args)
  File "/usr/lib64/python2.6/xmlrpclib.py", line 1489, in __request
    verbose=self.__verbose
  File "/usr/lib64/python2.6/xmlrpclib.py", line 1253, in request
    return self._parse_response(h.getfile(), sock)
  File "/usr/lib64/python2.6/xmlrpclib.py", line 1392, in _parse_response
    return u.close()
  File "/usr/lib64/python2.6/xmlrpclib.py", line 838, in close
    raise Fault(**self._stack[0])
xmlrpclib.Fault: <Fault 1: "<class 'cobbler.cexceptions.CX'>:'login failed'">

再次运行cobbler check检查配置文件,会出现如下错误,检查的时候一定要要在httpd与cobbler都启动的情况下检查

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 : change 'disable' to 'no' in /etc/xinetd.d/tftp
4 : 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.
5 : change 'disable' to 'no' in /etc/xinetd.d/rsync
6 : file /etc/xinetd.d/rsync does not exist
7 : debmirror package is not installed, it will be required to manage debian deployments and repositories
8 : 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
9 : 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.

上述问题解答方法如下:

1.需要修改/etc/cobbler/setting配置文件,将server行的IP改为本机IP
# sed -i 's#server: 127.0.0.0#server: 本机IP#g' /etc/cobbler/settings
2.需要修改/etc/cobbler/setting配置文件,将server_next行的IP改为本机IP
# sed -i 's#next_server: 127.0.0.1#next_server: 本机IP#g' /etc/cobbler/settings
3.修改/etc/xinetd.d/tftp配置文件,将disable = yes改为no
# sed -i 's#disable.*  = yes#disable                 = no #g' /etc/xinetd.d/tftp
4.运行cobbler get-loaders命令就行
5.修改/etc/xinetd.d/rsync配置文件,将disable = yes改为no
# sed -i 's#disable.*= yes# disable = no#g' /etc/xinetd.d/rsync
6.
7.安装debmirror包,如果不安装debian系统可以忽略
# yum install -y debmirror
8.使用openssl设置一个新的密码来代替默认密码
# openssl passwd -1 -salt 'random-phrase-here' 'your-password-here'"
9.安装cman fence-agents
# yum install -y cman fence-agents

 再次检测配置文件,会有关于debian的报错。注释配置文件就行

注释如下两行
# vim /etc/debmirro.conf
#@dists="sid";
#@arches="i386";

然后执行cobbler sync,同步更改后的配置文件

# cobbler sync

cobbler可以管理dhcp,tftp,rsync服务

# vim /etc/cobbler/setting
manage_dhcp  = 1
manage_tftp  = 1
manage_rsync = 1

cobbler管理dhcp后,只需修改cobbler提供的dhcp模板就行,不需要单独的修改dhcp服务的配置文件

subnet 10.0.0.0 netmask 255.255.255.0 {       #本机电脑所在的子网
     option routers             10.0.0.2;     #设置客户端默认网关
     option domain-name-servers 10.0.0.2;     #设置客户端默认DNS
     option subnet-mask         255.255.255.0;
     range dynamic-bootp        10.0.0.100 10.0.0.254;  #客户端获取到的地址范围
     default-lease-time         21600;
     max-lease-time             43200;
     next-server                $next_server;

注意:每次修改完cobbler配置信息的时候一定要使用check sync同步配置文件。

给cobbler服务器导入镜像

挂载镜像

将有镜像的分区挂载到服务上
# mount /dev/cdrom /mnt

导入镜像

# cobbler import --path=/mnt --name=CentOS-6.7-x86_64 --arch=x86_64
# --path 镜像路径
# --name 为安装源定义一个名字
# --arch 指定安装源是32位、64位、ia64, 目前支持的选项有: x86│x86_64│ia64
# 安装源的唯一标示就是根据name参数来定义,本例导入成功后,安装源的唯一标示就是:CentOS-6.6-x86_64,如果重复,系统会提示导入失败。

查看镜像

# ll /var/www/cobbler/ks_mirror/
# cobbler distro list

配置ks配置文件

# more CentOS-6.7-x86_64.ks 
#Install OS 
install 

#Install type
text

#Use network installation
url --url=http://10.0.0.222/cobbler/links/CentOS-6.7-x86_64/

#System authorization information
auth --useshadow --enablemd5 

#Root password rootpw 123456
rootpw --iscrypted $1$123456$wOSEtcyiP2N/IfIl15W6Z0

#System bootloader
bootloader --location=mbr

#Clear the Master Boot Record
zerombr

#Run the Setup Agent firs boot 
firstboot --disable

#System Keyboard
keyboard us

#System Language
lang en_US

#System Timezone
timezone --isUtc Asia/Shanghai

#Installation logging level
logging --level=info

#Firewall configuration
firewall --disable

#Selinux configuration
selinux --disable

#Disk partition information
part /boot --bytes-per-inode=4096 --fstype="ext4" --size=200
part / --asprimary --bytes-per-inode=4096 --fstype="ext4" --size=10240

reboot

#This packages is for CentOS6.7
%packages
@base
@core
@compat-libraries
@debugging
@development
@hardware-monitoring
@performance
@perl-runtime
@large-systems
lrzsz 
tree
telnet
nmap
%end

配置完成后需要cobbler sync

然后启动一台新的机器就可以使用cobbler安装系统了。

cobbler重装

由于我是选择安装7的操作系统,现在由于我对centos7的操作系统不熟悉,想要重装为6的操作系统,该如何做呢?

安装koan

# yum install -y koan

查看cobbler可用的镜像

# koan --server=10.0.0.222 --list=profiles
- looking for Cobbler at http://10.0.0.222:80/cobbler_api
CentOS-6.7-x86_64

在已经的客户端进行重装系统操作 ,这个操作一定要在客户端操作

# koan --replace-self --server=10.0.0.222 --profile=CentOS-6.7-x86_64

Cobbler使用python调用API

#!/usr/bin/python
import xmlrpclib
server = xmlrpclib.Server("http://xx.xx.xx.xx/cobbler_api")
print server.get_distros()
print server.get_profiles()
print server.get_systems()
print server.get_images()
print server.get_repos()



#!/usr/bin/env python 
# -*- coding: utf-8 -*-
import xmlrpclib 
class CobblerAPI(object):
    def __init__(self,url,user,password):
        self.cobbler_user= user
        self.cobbler_pass = password
        self.cobbler_url = url
    def add_system(self,hostname,ip_add,mac_add,profile):
        '''
        Add Cobbler System Infomation
        '''
        ret = {
            "result": True,
            "comment": [],
        }
        #get token
        remote = xmlrpclib.Server(self.cobbler_url) 
        token = remote.login(self.cobbler_user,self.cobbler_pass) 
        #add system
        system_id = remote.new_system(token) 
        remote.modify_system(system_id,"name",hostname,token) 
        remote.modify_system(system_id,"hostname",hostname,token) 
        remote.modify_system(system_id,'modify_interface', { 
            "macaddress-eth0" : mac_add, 
            "ipaddress-eth0" : ip_add, 
            "dnsname-eth0" : hostname, 
        }, token) 
        remote.modify_system(system_id,"profile",profile,token) 
        remote.save_system(system_id, token) 
        try:
            remote.sync(token)
        except Exception as e:
            ret['result'] = False
            ret['comment'].append(str(e))
        return ret
def main():
    cobbler = CobblerAPI("http://xx.xx.xx.xx/cobbler_api","cobbler","cobbler")
    ret = cobbler.add_system(hostname='cobbler-api-test',ip_add='xx.xx.xx.xx',mac_add='xx:xx:xx:xx:xx:xx',profile='CentOS-7-x86_64')
    print ret
if __name__ == '__main__':
    main()

 添加repo

# cobbler repo add --name=xxoo  --mirror=http://xxoo

同步repo

# cobbler reposync

添加repo到对应的profile

# cobbler profile --name=xxoo --repos=xxoo

修改kickstart文件,添加(写到%post %end中间)

%post

systemctl disable postfix.service

$yum_config_stanza

%end

添加定时任务,定期同步repo

# echo "1 3 * * * /usr/bin/cobbler reposync --tries=3 --no-fail" >>/va/spool/cron/root

 

posted @ 2017-08-28 12:52  雅俗丶共赏  阅读(374)  评论(0编辑  收藏  举报