cobbler+pxe实现自动化装机

一、 PXE:

Preboot Excution Environment预启动执行环境 ,由intel公司研发

pxe工作模式:基于C/S的网络模式,支持远程主机通过网络从远端服务器下载映像,并由此支持通过网络启动操作系统
pxe技术的实现流程:
  1. 客户端的网卡要支持网络引导机制;如果有操作系统的话,应该调整为第一引导设备;在主机被唤醒之后,开始加载网络引导应用时,此网卡会通过在本地局域网中广播一个rarp协议,第一步获得一个ip地址;
  2. 获得ip地址的同时还会从dhcp那里获得引导文件名称和文件服务器地址;随后会去找文件服务器主机,加载对应的文件,所需要的文件加载完成后,在内存中展开,而后基于此文件,可以实现去加载一个内核文件,此内核文件也一样通过tftp文件服务器获取,内核通常依赖于initrd虚根来完成对于真实根所在设备的驱动加载;
  3. 加载完成以后,这个内核文件通常是专用于为系统安装所设定的,因此,如果配置了网络属性,这个内核文件还需要基于网络配置在内核上的ip地址,基于网络把自己扮演成某种协议的客户端,加载一个能够启动安装程序的程序包,在本地完成安装并启动这个应用程序,而此程序在已经不再tftp文件服务器上面;
  4. 网络引导安装方式中,依赖于网络上应该存在一个基础程序包镜像仓库,因此,此时还依赖于外部有一个基于ftp或http再或者nfs服务所提供的yum仓库;还要通过这个yum仓库加载安装程序,以及安装程序启动完后,很可能要读取kickstart文件,可以根据kickstart文件内容,解决依赖关系以后,基于这个yum仓库,完成后续的所有安装过程;
  • 因此,对于centos系列主机,整个pxe技术大概要依赖以上所提到的服务,dhcp服务、tftp服务、能够提供yum仓库的服务;还要提供:kickstart文件(如果不想使用自动化可以不用提供此文件。

配置示例:

  1. dhcp server:

     [root@localhost ~]# yum -y install dhcp
     [root@localhost ~]# cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example /etc/dhcp/dhcpd.conf
         #在打开的dhcpd.conf配置文件中添加如下内容
         subnet 172.16.100.0 netmask 255.255.255.0 {
                 range 172.16.100.10 172.16.100.20;
                 option routers 172.16.100.1;
                 filename "pxelinux.0";
                 next-server 172.16.100.10;
         }
     [root@localhost ~]# systemctl start dhcpd
     [root@localhost ~]# ss -unlp
         State       Recv-Q Send-Q         Local Address:Port                        Peer Address:Port              
         UNCONN      0      0                          *:67                                     *:*                   users:(("dhcpd",pid=4557,fd=7))
         UNCONN      0      0                          *:68                                     *:*                   users:(("dhclient",pid=4485,fd=6))
     #至此dhcp启动成功,添加开机自启动
     [root@localhost ~]# systemctl enable dhcpd
     Created symlink from /etc/systemd/system/multi-user.target.wants/dhcpd.service to /usr/lib/systemd/system/dhcpd.service.
     [root@localhost ~]# systemctl is-enabled dhcpd
     enabled
    
  2. tftp-server

     [root@localhost ~]# yum -y install tftp tftp-server
     [root@localhost ~]# systemctl start tftp
     [root@localhost ~]# ss -unl
     State       Recv-Q Send-Q         Local Address:Port                        Peer Address:Port              
     UNCONN      0      0                          *:67                                     *:*                  
     UNCONN      0      0                          *:68                                     *:*                  
     UNCONN      0      0                         :::69                                    :::*   
     #   tftp监听在udp69端口,至此tftp-server服务启动,默认工作目录为:
     /var/lib/tftpboot/
     [root@localhost ~]# systemctl enable tftp
     Created symlink from /etc/systemd/system/sockets.target.wants/tftp.socket to /usr/lib/systemd/system/tftp.socket.
     [root@localhost ~]# systemctl is-enabled tftp
     indirect
    
  3. vsftpd server
    此处使用vsftpd服务来提供yum源访问

    [root@localhost ~]# yum -y install vsftpd
    #vsftpd默认工作目录为:
    /var/ftp/pub/
    [root@localhost ~]# systemctl start vsftpd
    [root@localhost ~]# ss -tnlp
    ...
    LISTEN      0      32                        :::21                                    :::*                   users:(("vsftpd",pid=5338,fd=3))
    #vsftpd服务监听在tcp/21端口
    #提供光盘镜像里的yum源访问路径
    [root@localhost ~]# mount -r /dev/sr0 /var/ftp/pub/centos/7
    [root@localhost ~]# ls /var/ftp/pub/centos/7
     CentOS_BuildTag  EULA  images    LiveOS    repodata              RPM-GPG-KEY-CentOS-Testing-7
     EFI              GPL   isolinux  Packages  RPM-GPG-KEY-CentOS-7  TRANS.TBL
    
    #提供一个kickstart文件放在/var/ftp/pub/kickstart/目录下
    [root@localhost ~]# mkdir /var/ftp/pub/kickstart
    [root@localhost ~]# cd /var/ftp/pub/kickstart/
    [root@localhost kickstart]# ls
    centos7-ks.cfg
    [root@localhost kickstart]# chmod 644 centos7-ks.cfg 
    
  4. 准备安装系统需要的内核等文件,放在tftp服务工作目录中

     [root@localhost ~]# yum -y install syslinux
     [root@localhost ~]# cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/
     [root@localhost ~]# cp /usr/share/syslinux/{memdisk,mboot.c32,chain.c32,menu.c32} /var/lib/tftpboot/
     [root@localhost ~]# cp /var/ftp/pub/centos/7/images/pxeboot/{initrd.img,vmlinuz} /var/lib/tftpboot/
     #提供目录,创建专门用来引导时显示的菜单默认文件:默认配置要放pxelinux.cfg的目录,文件名为default:
     [root@localhost ~]# mkdir /var/lib/tftpboot/pxelinux.cfg
     [root@localhost ~]# vim /var/lib/tftpboot/pxelinux.cfg/default
         default menu.c32
         prompt 5
         timeout 30
         MENU TITLE CentOS 7 PXE Menu
    
         LABEL linux
         MENU LABEL Install Centos 7 x86_64
         KERNEL vmlinuz
         APPEND initrd=initrd.img ks=ftp://172.16.100.10/pub/kickstart /centos7-ks.cfg   inst.repo=ftp://172.16.100.10/pub/centos/7
    
  5. 创建一个新的虚拟机以网络自动安装系统测试


    menu.png
start.png
start2.png

至此自动安装开始 PXE测试通过
kickstart文件内容附录如下:

#version=DEVEL
# System authorization information
auth --enableshadow --passalgo=sha512
# Use CDROM installation media
url --url="ftp://172.16.100.10/pub/centos/7"
# Use graphical install
text
firewall --disabled
selinux --disabled
reboot
# Run the Setup Agent on first boot
firstboot --disable
ignoredisk --only-use=sda
# Keyboard layouts
keyboard --vckeymap=cn --xlayouts='cn'
# System language
lang zh_CN.UTF-8

timezone Asia/Shanghai --isUtc
# Network information
network  --bootproto=dhcp --device=ens33 --onboot=on --ipv6=auto --no-activate
network  --hostname=localhost.localdomain

# Root password
rootpw --iscrypted $6$CMUBzCualheXycom$I1OADgCKcXi7lK9LKgqt.efENmylM.09mCt8.aesLmsQJ8uLYgu2Ck9G.tarugXP73TN4rFxQVVsAdIU8yN/V/
# System services
services --enabled="chronyd"
# System timezone
timezone Asia/Shanghai --isUtc
user --groups=wheel --name=inspur --password=$6$bx479ICdaF3SObqw$2F9YqiKtNp/SI5wPc.QJZggrWEhIfcXSbE1t74k222jaALrna.mqkH7PfWo.19mzCGjHrO1WwP1MXMDGZIrb7/ --iscrypted --gecos="inspur"
# X Window System configuration information
xconfig  --startxonboot
# System bootloader configuration
bootloader --append=" crashkernel=auto" --location=mbr --boot-drive=sda
# Partition clearing information
clearpart --all --initlabel
zerombr
# Disk partitioning information
part /boot --fstype="xfs" --ondisk=sda --size=1024
part swap --fstype="swap" --ondisk=sda --size=2049
part /home --fstype="xfs" --ondisk=sda --size=10240
part pv.254 --fstype="lvmpv" --ondisk=sda --size=20484
volgroup centos --pesize=4096 pv.254
logvol /  --fstype="xfs" --size=20480 --name=root --vgname=centos

%packages

@base
@core
@dial-up
@directory-client
@fonts
@gnome-desktop
@guest-agents
@input-methods
@internet-browser
@java-platform
@multimedia
@network-file-system-client
@networkmanager-submodules
@x11
chrony
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

二、Cobbler

快速网络安装linux操作系统的服务,支持多种的linux发行版,也可以支持网络安装windows

  • Cobbler是PXE的二次封装,将多种参数封装到一个菜单;是用python语言编写
Cobbler的工作流程:
cobbler.png
  1. client裸机配置了从网络启动后,开机后会广播包请求DHCP服务器(cobbler server)发送其分配好的一个ip
  2. DHCP收到请求后发送responese,包括其ip
  3. client拿到ip后再向cobbler server发送请求OS引导文件的请求
  4. cobbler server告诉client 裸机OS引导文件的名字和TFTP server的ip和port
  5. client通过上面告知的tftp server地址通信,下载引导文件
  6. client执行该引导文件,确定加载的信息,选择要按住的os,期间会在想cobbler server请求kickstart文件和 os image
  7. cobbler server发送请求的kickstart 和 os image
  8. celient加载kickstart文件
  9. client接收os image,安装该os image
cobbler安装

cobbler 基于EPEL源,需要先配置好epel的yum源; cobbler服务集成:PXE,DHCP,rsync,Http,DNS,Kickstart,IPMI电源管理
......

一、安装:

[root@localhost ~]# yum install cobbler cobbler-web pykickstart debmirror
[root@localhost ~]# systemctl start httpd cobblerd 
[root@localhost ~]# 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 *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 : 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
    Restart cobblerd and then run 'cobbler sync' to apply changes.

如上各问题的解决方法如下所示:
1、修改/etc/cobbler/settings文件中的server参数的值为提供cobbler服务的主机相应的IP地址或主机名,如172.16.100.15;
2、修改/etc/cobbler/settings文件中的next_server参数的值为提供PXE服务的主机相应的IP地址,如172.16.100.16;
3、如果当前节点可以访问互联网,执行“cobbler get-loaders”命令即可;否则,需要安装syslinux程序包,而后复制/usr/share/syslinux/{pxelinux.0,memu.c32}等文件至/var/lib/cobbler/loaders/目录中;
4、执行“chkconfig rsync on”命令即可;
5、注释/etc/debmirror.conf文件中的“@dists="sid";”一行;
6、注释/etc/debmirror.conf文件中的“@arches="i386";”一行;
7、执行“openssl passwd -1 -salt $(openssl rand -hex 4)”生成密码,并用其替换/etc/cobbler/settings文件中default_password_crypted参数的值;
8、执行“yum install cman fence-agents”命令安装相应的程序包即可;

接着重启cobblerd,而后执行“cobbler sync”同步新的配置至cobbler。

二、配置及启动cobbler所依赖的各服务

  • cobbler的运行依赖于dhcp、tftp、rsync及dns服务。其中dhcp可由dhcpd(isc)提供,也可由dnsmasq提供;tftp可由tftp-server程序包提供,也可由cobbler自带的tftp功能提供;rsync由rsync程序包提供;dns可由bind提供,也可由dnsmasq提供。

  • cobbler可自行管理这些服务中的部分甚至是全部,但需要配置/etc/cobbler/settings文件中的“manage_dhcp”、“manage_tftpd”、“manage_rsync”和“manage_dns”分别进行定义。另外,由于每种服务都有着不同的实现方式,如若需要进行自定义,需要通过修改/etc/cobbler/modules.conf配置文件中各服务的模块参数的值来实现。

本文采用了独立管理的方式,即不通过cobbler来管理这些服务

  1. 配置dhcp服务
    定义好所需的“subnet”及其它参数或选项,而后启动dhcpd守护进程即可。本示例中所用的dhcpd的配置如下所示:

     option domain-name "magedu.com";
     option domain-name-servers 192.168.10.254,172.16.0.1;
     
     default-lease-time 43200;
     max-lease-time 86400;
     
     log-facility local7;
     
     subnet 172.16.0.0 netmask 255.255.0.0 {
         range 172.16.100.121 172.16.200;
         option routers 172.16.100.10;
     }
    
     next-server 172.16.100.10;
     filename="pxelinux.0";
    

接着使用“service dhcpd start”启动服务即可。

  1. 配置tftp服务

     # chkconfig tftp on
     # service xinetd restart
    

三、配置cobbler
cobbler的各主要组件间的关系如下图所示。

cobbler。1.png

  1. 管理distro
    使cobbler变得可用的第一步为定义distro,其可以通过为其指定外部的安装引导内核及ramdisk文件的方式实现。而如果已经有完整的系统安装树(如CentOS6的安装镜像)则推荐使用import直接导入的方式进行。
  • 例如,对于已经挂载至/media/cdrom目录的CentOS 6.5 x86_64的安装镜像,则可以使用类似如下命令进行导入
    cobbler import --name=centos-6.5-x86_64 --path=/media/cdrom
  • 可使用“cobbler distro list”列出所有的distro。
  • 如果有kickstart文件,也可以使用“--kickstart=/path/to/kickstart_file”进行导入,因此import会自动为导入的distro生成一个profile。
  1. 管理profile
    cobbler使用profile来为特定的需求类别提供所需要安装配置,即在distro的基础上通过提供kickstart文件来生成一个特定的系统安装配置。distro的profile可以出现在PXE的引导菜单中作为安装的选择之一。
    因此,如果需要为前面创建的centos-6.5-x86_64这个distro提供一个可引导安装条目,其用到的kickstart文件为/tmp/centos-6.5-x86_64.cfg(只提供了最基本的程序包),则可通过如下命令实现。
    cobbler profile add --name=centos-6.5-x86_64-basic --distro=centos-6.5-x86_64 --kickstart=/tmp/centos-6.5-x86_64.cfg
    可使用“cobbler profile list”查看已经创建的profile。

四、使用cobbler_web

  1. 配置cobbler_web的认证功能
    cobbler_web支持多种认证方式,如authn_configfile、authn_ldap或authn_pam等,默认为authn_denyall,即拒绝所有用户登录。下面说明两种能认证用户登录cobbler_web的方式。
  • 使用authn_pam模块认证cobbler_web用户
    首先修改modules中[authentication]段的module参数的值为authn_pam。
    接着添加系统用户,用户名和密码按需设定即可,例如下面的命令所示。
    # useradd cblradmin
    # echo 'cblrpass' | passwd --stdin cblradmin

  • 而后将cblradmin用户添加至cobbler_web的admin组中。修改/etc/cobbler/users.conf文件,将cblradmin用户名添加为admin参数的值即可,如下所示。
    [admins] admin = "cblradmin"

  • 最后重启cobblerd服务,通过http://YOUR_COBBLERD_IP/cobbler_web访问即可。

  1. 使用authn_configfile模块认证cobbler_web用户
    首先修改modules.conf中[authentication]段的module参数的值为authn_configfile。
    接着创建其认证文件/etc/cobbler/users.digest,并添加所需的用户即可。需要注意的是,添加第一个用户时,需要为htdigest命令使用“-c”选项,后续添加其他用户时不能再使用;另外,cobbler_web的realm只能为Cobbler。如下所示。
    # htdigest -c /etc/cobbler/users.digest Cobbler cblradmin

最后重启cobblerd服务,通过http://YOUR_COBBLERD_IP/cobbler_web访问即可。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,015评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,262评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,727评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,986评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,363评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,610评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,871评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,582评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,297评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,551评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,053评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,385评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,035评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,079评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,841评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,648评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,550评论 2 270

推荐阅读更多精彩内容