美文网首页Linux基础知识
一文详解pxe+cobbler自动化装机

一文详解pxe+cobbler自动化装机

作者: Stone_説 | 来源:发表于2020-05-07 00:31 被阅读0次

1.pxe自动化装机

准备工作(重要!!!):
1.一台pxe服务器(node2)用于,192.168.43.141,centos7
2.一台测试机(启动时先用光盘引导,再切换至网络引导即可)
3.相关配置:node2需要安装并开启,DHCP服务,HTTPD服务,TFTP服务,同时为了便于方便测验,需要将selinux和iptables关闭
4.将vmware中自带的dhcp服务关闭
5.需要准备好预安装操作系统的应答文件以及将相应的iso文件挂载至httpd服务的目录
6.准备好对应的内核文件vmlinuz和initrd.img

1.1 相关软件包的安装
[root@node2 ~]# yum -y install httpd tftp-server dhcp syslinux 
1.2 准备yum 源

预安装Centos6,7两套系统,需要在vmware中增加一块磁盘

[root@node2 ~]# echo "- - -" > /sys/class/scsi_host/host0/scan 
[root@node2 ~]# echo "- - -" > /sys/class/scsi_host/host1/scan 
[root@node2 ~]# echo "- - -" > /sys/class/scsi_host/host2/scan 
[root@node2 ~]# mkdir /var/www/html/centos/{6,7}/os/x86_64 -pv
[root@node2 ~]# mount /dev/sr0 /var/www/html/centos/7/os/x86_64
[root@node2 ~]# mount /dev/sr1 /var/www/html/centos/6/os/x86_64
1.3 准备ks应答文件

NOTE:
1.可以使用图形工具system-config-kickstart,也可以参考自动自带的anaconda-ks.cfg,将其修改即可,制作完成使用ksvalidator检验应答文件是否正确。
2.对于应答文件,需要将其赋予r权限

ks文件(1).png ks文件(2).png ks文件(3).png ks文件(4).png ks文件(5).png ks文件(6).png ks文件(7).png ks文件(8).png
[root@node2 ~]# system-config-kickstart 
[root@node2 ~]# mkdir  /var/www/html/ksdir/
[root@node2 ~]# ls /var/www/html/ksdir/
ks6_mini.cfg  ks7_desktop.cfg  ks7_mini.cfg
[root@node2 ~]# cat ks7_mini.cfg
#platform=x86, AMD64, or Intel EM64T
#version=DEVEL
# Install OS instead of upgrade
install
# Keyboard layouts
keyboard 'us'
# Root password
rootpw --iscrypted $1$gWbfu.So$kch//vaiKdsn.Ge2OiVV7/
# System language
lang en_US
# System authorization information
auth  --useshadow  --passalgo=sha512
# Use text mode install
text
firstboot --disable
# SELinux configuration
selinux --disabled


# Firewall configuration
firewall --disabled
# Network information
network  --bootproto=dhcp --device=eth0
# Reboot after installation
reboot
# System timezone
timezone Africa/Abidjan
# Use network installation
url --url="http://192.168.43.141/centos/7/os/x86_64"
# System bootloader configuration
bootloader --append="net.ifnames=0" --location=mbr
# Partition clearing information
zerombr
# Partition clearing information
clearpart --all
# Disk partitioning information
part / --fstype="xfs" --size=102400
part /boot --fstype="xfs" --size=1024
part swap --fstype="swap" --size=2048

%packages
@^minimal
%end

%post
mkdir /root/.ssh
chmod 700 /root/.ssh
cat > /root/.ssh/authorized_keys <<EOF
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDRdsYsTMcCAXthryGO66LlCGc85C61O6LGUtQiPtm2mPoJTvzU9fJgvZBp0dzrNQLEMpIpneDQmv4ZzdJRWZsg3gHXBFj6CJEaHsy9Xgolwhh3MSk1CChQ/B6CrwGdQpAGzmS5QPRVbINjfXZxMLphj7vEUcL6p8H5akK3vkoaak9uZRDkE/b3VJ+fUo1xLxRBUwY1lLnfb1lo/9EiYUGiMXZXjPDuHOYOqgr3XKMiyeLho9O9azJiDaKKUYAYyMCSPM3DuzxuYqf1EuRgQ76ERW+CQkPoH7uE36ISlB5vpRx29Yf6KIc9/Nc13+jrkhTU1heYr+rwrqYevPccFNsD root@node2
EOF
chmod 600 /root/.ssh/authorized_keys
mkdir /etc/yum.repos.d/bak
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak
cat > /etc/yum.repos.d/test.repo <<EOF
[base]
baseurl=http://192.168.43.141/centos/7/os/x86_64
gpgcheck=0
EOF
%end
1.4 配置dhcp服务
[root@node2 ~]# cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example /etc/dhcp/dhcpd.conf
vim /etc/dhcp/dhcpd.conf
option domain-name "magedu.com";
option domain-name-servers 114.114.114.114, 180.76.76.76;
default-lease-time 86400;
max-lease-time 864000;
subnet 192.168.43.0 netmask 255.255.255.0 {
    range 192.168.43.50 192.168.43.100;
    option routers 192.168.43.2;
    next-server 192.168.43.141;
    filename "pxelinux.0";
}
[root@node2 ~]# systemctl start dhcpd 

验证:

ks应答文件验证.png
光盘应答文件验证.png
1.5 tftp服务配置

注意点:在将菜单文件isolinux.cfg移动至pxelinux.cfg文件夹下时,一定要将文件名修改为default,否则无法识别

[root@node2 ~]# cp /usr/share/syslinux/pxelinux.0  /var/lib/tftpboot/
[root@node2 ~]# cp /usr/share/syslinux/menu.c32     /var/lib/tftpboot/
[root@node2 ~]# mkdir /var/lib/tftpboot/centos{6,7}
[root@node2 ~]# cp /var/www/html/centos/7/os/x86_64/isolinux/{vmlinuz,initrd.img} /var/lib/tftpboot/centos7
[root@node2 ~]# cp /var/www/html/centos/6/os/x86_64/isolinux/{vmlinuz,initrd.img} /var/lib/tftpboot/centos6
[root@node2 ~]# mkdir /var/lib/tftpboot/pxelinux.cfg/
[root@node2 ~]# cp /var/www/html/centos/7/os/x86_64/isolinux/isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default

[root@centos7 tftpboot]#tree /var/lib/tftpboot/
/var/lib/tftpboot/
├── centos6
│   ├── initrd.img
│   └── vmlinuz
├── centos7
│   ├── initrd.img
│   └── vmlinuz
├── menu.c32
├── pxelinux.0
└── pxelinux.cfg
    └── default

3 directories, 7 files
1.6 准备启动菜单
[root@node2 ~]# vim /var/lib/tftpboot/pxelinux.cfg/default 
default menu.c32
timeout 600
menu title CentOS Auto Install

label mini7
  menu label Install CentOS ^Mini 7
  kernel centos7/vmlinuz
  append initrd=centos7/initrd.img ks=http://192.168.43.141/ksdir/ks7_min.cfg

label desktop
  menu label Install CentOS ^Desktop 7
  kernel centos7/vmlinuz
  append initrd=centos7/initrd.img ks=http://192.168.43.141/ksdir/ks7_desktop.cfg

label mini6
  menu label Install CentOS Mi^ni 6
  kernel centos6/vmlinuz
  append initrd=centos6/initrd.img ks=http://192.168.43.141/ksdir/ks6_min.cfg

label local
  menu default
  menu label Boot from ^local drive
  localboot 0xffff
1.7 测式机网卡启动
test1.png
test2.png
1.8 总结

1.dhcp服务为网络中主机提供ip地址
2.tftp服务提供内核文件
3.httpd服务提供将应答文件以及镜像文件

2.cobbler自动化装机

2.1 准备epel 源
[root@centos7min ~]# yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
[root@centos7min ~]# yum install cobbler dhcp tftp
[root@centos7min ~]# systemctl start cobblerd httpd tftp
[root@centos7min ~]# systemctl status cobblerd httpd tftp
● cobblerd.service - Cobbler Helper Daemon
   Loaded: loaded (/usr/lib/systemd/system/cobblerd.service; disabled; vendor preset: disabled)
   Active: active (running) since Tue 2020-05-05 20:11:35 CST; 35s ago

● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: active (running) since Tue 2020-05-05 20:12:05 CST; 5s ago
     Docs: man:httpd(8)

● tftp.service - Tftp Server
   Loaded: loaded (/usr/lib/systemd/system/tftp.service; indirect; vendor preset: disabled)
   Active: active (running) since Tue 2020-05-05 20:11:55 CST; 15s ago

[root@centos7min ~]# systemctl enable cobblerd httpd tftp
2.2 查看相关服务端口
[root@centos7min ~]# ss -ntulp
Netid State      Recv-Q Send-Q              Local Address:Port                             Peer Address:Port              
udp   UNCONN     0      0                              :::69                                         :::*                   users:(("in.tftpd",pid=7536,fd=0),("systemd",pid=1,fd=42))
tcp   LISTEN     0      128                             *:22                                          *:*                   users:(("sshd",pid=6732,fd=3))
tcp   LISTEN     0      100                     127.0.0.1:25                                          *:*                   users:(("master",pid=7025,fd=13))
tcp   LISTEN     0      5                       127.0.0.1:25151                                       *:*                   users:(("cobblerd",pid=7520,fd=9))
tcp   LISTEN     0      128                            :::80                                         :::*                   users:(("httpd",pid=7541,fd=4),("httpd",pid=7540,fd=4),("httpd",pid=7539,fd=4),("httpd",pid=7538,fd=4),("httpd",pid=7537,fd=4),("httpd",pid=7535,fd=4))
tcp   LISTEN     0      128                            :::22                                         :::*                   users:(("sshd",pid=6732,fd=4))
tcp   LISTEN     0      100                           ::1:25                                         :::*                   users:(("master",pid=7025,fd=14))
2.3 关闭selinux,并执行cobbler check
[root@centos7min ~]# setenforce 0
[root@centos7min ~]# getenforce
Permissive
[root@centos7min ~]# 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.
    提示需要修改文件/etc/cobbler/settings,此文件指向cobber服务的文件
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.
    通过next_server指定tftp服务的地址
3 : SELinux is enabled. Please review the following wiki page for details on ensuring cobbler works correctly in your SELinux environment:
    https://github.com/cobbler/cobbler/wiki/Selinux
4 : change 'disable' to 'no' in /etc/xinetd.d/tftp
    因为tftp早期是非独立服务,在centos7上不需要做此服务
5 : 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.
    cobbler-get-loaders从股联网上下载bootloader,但是需要做一些前置部署,否则会不能执行
6 : enable and start rsyncd.service with systemctl  不需要
7 : debmirror package is not installed, it will be required to manage debian deployments and repositories  不需要
8 : ksvalidator was not found, install pykickstart  
    yum安装pykickstart
9 : 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
10 : 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.
2.3 修改配置文件/etc/cobbler/settings
[root@centos7min ~]# vim /etc/cobbler/settings
101 default_password_crypted: "$1$mF86/UHC$WvcIcX2t6crBz2onWxyac."   加密密码,初始密码为cobbler
    [root@centos7min ~]# openssl passwd -1
    修改口令方法:
    Password: 
    Verifying - Password: 
    $1$aScBKAHp$Q7lbbB0BFwVYW.J2hNn4h1
242 manage_dhcp: 1   参数改为1,自动生成dhcp配置文件,模板为/etc/cobbler/modules.conf
278 next_server: 192.168.43.111
390 server: 192.168.43.111
[root@centos7min ~]# systemctl restart cobblerd
2.4 执行cobbler get-loaders

准备工作:
这一步如果执行时中间卡顿,处理方法可自行搜索

[root@centos7min ~]# yum -y install syslinux
[root@centos7min ~]# cp /usr/share/syslinux/pxelinux.0 /var/lib/cobbler/loaders/
[root@centos7min ~]# cp /usr/share/syslinux/menu.c32 /var/lib/cobbler/loaders/
[root@centos7min ~]# /etc/init.d/cobblerdrestart
[root@centos7min ~]# cobbler get-loaders    #再次执行则成功
[root@centos7min ~]# cobbler sync
received on stderr: Redirecting to /bin/systemctl restart dhcpd.service
同步文件时,报错需要依赖于dhcp服务,所以先完成dhcp服务的配置
2.5 修改dhcp服务配置文件
[root@centos7min ~]# vim /etc/cobbler/dhcp.template  
subnet 192.168.43.0 netmask 255.255.255.0 {
     option routers             192.168.43.2;
     option domain-name-servers 192.168.43.2;
     option subnet-mask         255.255.255.0;
     range dynamic-bootp        192.168.43.100 192.168.43.254;
     default-lease-time         21600;
     max-lease-time             43200;
     next-server                $next_server;
[root@centos7min loaders]# cobbler sync   进行cobbler同步
task started: 2020-05-05_212840_sync
task started (id=Sync, time=Tue May  5 21:28:40 2020)
running pre-sync triggers
cleaning trees
...
running python trigger cobbler.modules.manage_genders
running python trigger cobbler.modules.scm_track
running shell triggers from /var/lib/cobbler/triggers/change/*
*** TASK COMPLETE ***
[root@centos7min ~]# vim /etc/dhcp/dhcpd.conf   查看dhcp的配置文件,以完成同步
subnet 192.168.43.0 netmask 255.255.255.0 {
     option routers             192.168.43.2;
     option domain-name-servers 192.168.43.2;
     option subnet-mask         255.255.255.0;
     range dynamic-bootp        192.168.43.100 192.168.43.254;
     default-lease-time         21600;
     max-lease-time             43200;
     next-server                192.168.43.111;
[root@centos7min ~]# systemctl restart dhcpd   重启dhcp服务
[root@centos7min ~]# tree /var/lib/tftpboot/   检查文件是否已经同步至tftp服务
/var/lib/tftpboot/
├── boot
│   └── grub
│       └── menu.lst
├── etc
├── grub
│   ├── efidefault
│   ├── grub-x86_64.efi
│   ├── grub-x86.efi
│   └── images -> ../images
├── images
├── images2
├── memdisk
├── menu.c32
├── ppc
├── pxelinux.0
├── pxelinux.cfg
│   └── default
├── s390x
│   └── profile_list
└── yaboot
2.6 检验菜单文件
[root@centos7min ~]# cat /var/lib/tftpboot/pxelinux.cfg/default 
DEFAULT menu
PROMPT 0
MENU TITLE Cobbler | http://cobbler.github.io/
TIMEOUT 200
TOTALTIMEOUT 6000
ONTIMEOUT local

LABEL local
        MENU LABEL (local)
        MENU DEFAULT
        LOCALBOOT -1
MENU end
2.7 Centos6光盘内容(Centos7光盘太大,速度较慢)
[root@centos7min ~]# echo "- - -" > /sys/class/scsi_host/host0/scan 
[root@centos7min ~]# echo "- - -" > /sys/class/scsi_host/host1/scan 
[root@centos7min ~]# echo "- - -" > /sys/class/scsi_host/host2/scan 
[root@centos7min ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT 
sr1     11:1    1 1024M  0 rom  
[root@centos7min ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sr1     11:1    1  3.7G  0 rom  
[root@centos7min ~]# mount /dev/sr1 /mnt
mount: /dev/sr1 is write-protected, mounting read-only
2.8 将光盘内容导入cobbler
[root@centos7min ~]# cobbler import --path=/mnt --name=CentOS6.10-x86_64 --arch=x86_64
task started: 2020-05-05_234618_import
task started (id=Media import, time=Tue May  5 23:46:18 2020)
*** TASK COMPLETE ***
此时可在另一终端,查看导入过程
[root@centos7min ~]# du -sh /var/www
24k     /var/www
811M    /var/www
[root@centos7min ~]# du -sh /var/www
827M    /var/www
[root@centos7min ~]# du -sh /var/www
3.8G    /var/www
同样方式导入CentOS7光盘内容
[root@centos7min ~]# cobbler import --path=/misc/cd --name=CentOS7.6-Minimal-x86_64 --arch=x86_64
task started: 2020-05-05_235524_import
task started (id=Media import, time=Tue May  5 23:55:24 2020)
*** TASK COMPLETE ***
[root@centos7min ~]# du -sh /var/www 导入centos为min版,所以大小约为1G
4.7G    /var/www
2.9 到tftp路径下查看生成的default是否自动生成菜单
[root@centos7min ~]# cd /var/lib/tftpboot/pxelinux.cfg/
[root@centos7min pxelinux.cfg]# cat default 
DEFAULT menu
PROMPT 0
MENU TITLE Cobbler | http://cobbler.github.io/
TIMEOUT 200
TOTALTIMEOUT 6000
ONTIMEOUT local

LABEL local
        MENU LABEL (local)
        MENU DEFAULT
        LOCALBOOT -1

LABEL CentOS6.10-x86_64
        kernel /images/CentOS6.10-x86_64/vmlinuz
        MENU LABEL CentOS6.10-x86_64
        append initrd=/images/CentOS6.10-x86_64/initrd.img ksdevice=bootif lang=  kssendmac text  ks=http://192.168.43.111/cblr/svc/op/ks/profile/CentOS6.10-x86_64
        ipappend 2

LABEL CentOS7.6-Minimal-x86_64
        kernel /images/CentOS7.6-Minimal-x86_64/vmlinuz
        MENU LABEL CentOS7.6-Minimal-x86_64
        append initrd=/images/CentOS7.6-Minimal-x86_64/initrd.img ksdevice=bootif lang=  kssendmac text  ks=http://192.168.43.111/cblr/svc/op/ks/profile/CentOS7.6-Minimal-x86_64
        ipappend 2

MENU end
2.10 测验cobbler自带的kickstart文件

将自己所编写的应答文件做对应修改,并与之前的yum源关联,查看系统中的两套yum源

[root@centos7min ~]# cp ks6_min.cfg  /var/lib/cobbler/kickstarts/
[root@centos7min kickstarts]# cat ks6_min.cfg 
# Kickstart file automatically generated by anaconda.

#version=DEVEL
install
text
reboot
url --url=$tree  这一行将对应的此前yum源进行修改,用cobbler的内置变量$tree进行替换
[root@centos7min kickstarts]# cobbler distro list
   CentOS6.10-x86_64
   CentOS7.6-Minimal-x86_64
[root@centos7min kickstarts]# cobbler profile add --name CentOS6.10-x86_64_mini --distro=CentOS6.10-x86_64 --kickstart=/var/lib/cobbler/kickstarts/ks6_min.cfg
    将kickstart文件,与yum源关联,--name指定菜单名字,--distro指定关联yum源(可使用cobbler distro list查看可使用yum源)名字,--kickstart指定应答文件,以绝对路径形式
        [root@centos7min ~]# cobbler distro list
            CentOS6.10-x86_64
            CentOS7.6-Minimal-x86_64
2.11 到tftp路径下查看default文件是否有新生成的菜单项
[root@centos7min pxelinux.cfg]# pwd
/var/lib/tftpboot/pxelinux.cfg
[root@centos7min pxelinux.cfg]# cat default 
DEFAULT menu
PROMPT 0
MENU TITLE Cobbler | http://cobbler.github.io/
TIMEOUT 200
TOTALTIMEOUT 6000
ONTIMEOUT local

LABEL local
        MENU LABEL (local)
        MENU DEFAULT
        LOCALBOOT -1

LABEL CentOS6.10-x86_64
        kernel /images/CentOS6.10-x86_64/vmlinuz
        MENU LABEL CentOS6.10-x86_64
        append initrd=/images/CentOS6.10-x86_64/initrd.img ksdevice=bootif lang=  kssendmac text  ks=http://192.168.43.111/cblr/svc/op/ks/profile/CentOS6.10-x86_64
        ipappend 2

LABEL CentOS6.10-x86_64_mini
        kernel /images/CentOS6.10-x86_64/vmlinuz
        MENU LABEL CentOS6.10-x86_64_mini
        append initrd=/images/CentOS6.10-x86_64/initrd.img ksdevice=bootif lang=  kssendmac text  ks=http://192.168.43.111/cblr/svc/op/ks/profile/CentOS6.10-x86_64_mini
        ipappend 2

LABEL CentOS7.6-Minimal-x86_64
        kernel /images/CentOS7.6-Minimal-x86_64/vmlinuz
        MENU LABEL CentOS7.6-Minimal-x86_64
        append initrd=/images/CentOS7.6-Minimal-x86_64/initrd.img ksdevice=bootif lang=  kssendmac text  ks=http://192.168.43.111/cblr/svc/op/ks/profile/CentOS7.6-Minimal-x86_64
        ipappend 2

MENU end
2.12 删除default文件中多余不用的菜单项
[root@centos7min kickstarts]# cobbler profile list
   CentOS6.10-x86_64
   CentOS6.10-x86_64_mini
   CentOS7.6-Minimal-x86_64
[root@centos7min kickstarts]# cobbler profile remove --name=CentOS6.10-x86_64
[root@centos7min kickstarts]# cobbler profile list
   CentOS6.10-x86_64_mini
   CentOS7.6-Minimal-x86_64

[root@centos7min ~]# yum install cobbler-web   cobbler的web页面安装
[root@centos7min ~]# rpm -ql cobbler-web
[root@centos7min ~]# systemctl restart httpd
2.13 附录:图片 cobbler1.png
cobbler2.png cobbler3.png cobbler4.png cobbler5.png cobbler6.png cobbler7.png cobbler8.png cobbler9.png cobbler10.png

相关文章

网友评论

    本文标题:一文详解pxe+cobbler自动化装机

    本文链接:https://www.haomeiwen.com/subject/tbhmwhtx.html