批量安装操作系统 by Cobbler

当我们需要部署某集群时,往往需要为数十台服务器安装操作系统,为了节省时间和人力,可以使用PXE技术,实现批量无人安装操作系统。

预启动执行环境(Preboot eXecution Environment,PXE)提供了一种使用网络接口(Network Interface)启动计算机的机制。

普通的从硬盘启动Linux系统最初是BIOS将MBR加载入内存,然后将控制权交给MBR中的bootloader程序(如GRUB),bootloader程序经过几个stage的加载后,最后将vmlinuz加载入内存,开始内核引导;而PXE的启动过程在内核引导之前,是由BIOS中的PXE固件开启NBP程序(比如DHCP的网络通信),然后下载vmlinuz和initrd,之后再进入内核启动过程。在内核引导完成之后,仍然会通过网络的方式(但不是TFTP协议,而是其他更加健壮的协议如NFS、iSCSI等),加载真正的完整操作系统。

具体请搜索关键词:pxe、kickstart、cobbler。

我们要做的大概流程是:在与集群网络连通的某台机器上部署cobbler服务,集群其他服务器以pxe方式boot,通过cobbler服务器上的dhcp服务获取ip,然后通过tftp、nfs等方式从cobbler服务器上拉取镜像和配置文件,进行操作系统安装。

部署cobbler服务器

部署cobbler的服务器操作系统为centos 7,内核版本3.10.
请保证部署cobbler的机器的/var目录至少有10GB的空间。

关闭防火墙&selinux

之后的安装过程中需要使用http/dhcp/tftp等服务,需要开启对应的端口。在内网环境中,为了方便,可以直接关闭防火墙和selinux。

执行下面脚本

# 临时关闭selinux,不必重启就可生效
setenforce 0  
# 永久关闭selinux。reboot后生效
[ -e /etc/sysconfig/selinux ] || cp /etc/sysconfig/selinux /etc/sysconfig/selinux.orig
sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/sysconfig/selinux
sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
# 关闭防火墙
systemctl stop firewalld
systemctl disable firewalld

安装cobbler及其他需要的软件包

yum install -y epel-release
yum install -y cobbler
yum install -y httpd dhcp rsync bind
yum install -y tftp xinetd
yum install -y pykickstart

配置cobbler

cobbler的配置文件放在/etc/cobbler。
主要配置文件/etc/cobbler/settings,要修改的位置主要有:

Default Encrypted Password

如注释所说,运行
openssl passwd -1
并用结果替换原有的密码。

# /etc/cobbler/settings

# cobbler has various sample kickstart templates stored
# in /var/lib/cobbler/kickstarts/.  This controls
# what install (root) password is set up for those
# systems that reference this variable.  The factory
# default is "cobbler" and cobbler check will warn if
# this is not changed.
# The simplest way to change the password is to run 
# openssl passwd -1
# and put the output between the "" below.
default_password_crypted: "$1$mF86/UHC$WvcIcX2t6crBz2onWxyac."

Server

cobbler server的ip,用于对外提供dhcp和http等服务,必须为一个固定内网ip地址。

# /etc/cobbler/settings

# this is the address of the cobbler server -- as it is used
# by systems during the install process, it must be the address
# or hostname of the system as those systems can see the server.
# if you have a server that appears differently to different subnets
# (dual homed, etc), you need to read the --server-override section
# of the manpage for how that works.
server: 127.0.0.1

Next_Server

next_server为tftp服务所在ip,通常是需要和server保持一致

# /etc/cobbler/settings

# if using cobbler with manage_dhcp, put the IP address
# of the cobbler server here so that PXE booting guests can find it
# if you do not set this correctly, this will be manifested in TFTP open timeouts.
next_server: 127.0.0.1

DHCP Management and DHCP Server Template

manage_dhcp: 0修改为manage_dhcp: 1

# /etc/cobbler/settings

# set to 1 to enable Cobbler's DHCP management features.
# the choice of DHCP management engine is in /etc/cobbler/modules.conf
manage_dhcp: 0

同时需要修改/etc/cobbler/dhcp.template
一般情况下,只需要修改下面这部分内容中的dhcp网段。要与本机ip在同一网段。

 # /etc/cobbler/dhcp.template

 subnet 192.168.1.0 netmask 255.255.255.0 {
      option routers             192.168.1.1;
      option subnet-mask         255.255.255.0;
      range dynamic-bootp        192.168.1.100 192.168.1.254;
      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 if option pxe-system-type = 00:09 {
                   filename "grub/grub-x86_64.efi";
           } else {
                   filename "pxelinux.0";
           }
      }
 
 }

假设本机ip为192.168.122.226/24,那么修改后的配置为:

 # /etc/cobbler/dhcp.template
 
 subnet 192.168.122.0 netmask 255.255.255.0 {
      option routers             192.168.122.1;
      option subnet-mask         255.255.255.0;
      range dynamic-bootp        192.168.122.100 192.168.122.254;
      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 if option pxe-system-type = 00:09 {
                   filename "grub/grub-x86_64.efi";
           } else {
                   filename "pxelinux.0";
           }
      }
 
 }

启动服务

tftp

sed -i '/disable/c\\tdisable\t\t\t= no' /etc/xinetd.d/tftp
systemctl start xinetd
systemctl enable xinetd

rsyncd

systemctl start rsyncd
systemctl enable rsyncd

httpd

systemctl start httpd
systemctl enable httpd

dhcpd
这个我们配置成是由cobbler管理的,留到后面通过cobbler 启动。略过

cobblerd

systemctl start cobblerd
systemctl enable cobblerd

问题自检

cobbler提供了问题检查的命令

cobbler check

根据输出解决必须解决的问题。有些问题可以忽略。
第一次运行cobbler时,肯定会缺少boot-loaders,需要run一次

cobbler get-loaders

根据check的输出,解决所有必须解决的问题后,重启cobblerd

# 修改配置后,需要重启cobblerd
systemctl restart cobblerd

然后运行一次,启动被cobbler管理的service,确保这一步没有报错后,才能继续后面的步骤

# 启动被cobbler管理的service,修改模版配置后也需要执行这个命令
cobbler sync

导入系统镜像

In order to import a distribution, you will need a DVD ISO for your distribution. NOTE: You must use a full DVD, and not a "Live CD" ISO.

根据文档,我们需要使用DVD版本的镜像(通过kickstart定制一些基础软件包的安装,要求DVD版本),下载CentOS-7-x86_64-DVD版本的镜像。

wget http://mirrors.aliyun.com/centos/7/isos/x86_64/CentOS-7-x86_64-DVD-1708.iso

下载完成后,把镜像mount到某个目录,然后import到cobbler

# 挂载iso
mkdir /mnt/iso1
mount -t iso9660 -o loop,ro CentOS-7-x86_64-DVD-1708.iso /mnt/iso1
# 导入到cobbler
cobbler import --name=centos7 --arch=x86_64 --path=/mnt/iso1

如果没有报错,import过程会生成至少一对distro/profile对象。通过list命令查看。还可以通过report命令查看某个对象的详细信息。另外要注意,cobbler中的对象配置是继承关系:distro <- profile <- system,后面的配置会覆盖前面的。

cobbler distro list
cobbler profile list
# 可以通过report命令查看对象的details
cobbler distro report --name=centos7-x86_64
cobbler profile report --name=centos7-x86_64

一切正常后,现在已经不需要iso

umount /mnt/iso1
rmdir /mnt/iso1

这里需要解释下cobbler的几个对象的含义。我们可能用到的命令主要有cobbler distro/profile/system,分别对应了三种对象:

  • distro:表示某个发行版的对象,不同的镜像导入后对应不同的distro,比如centos7-x86_64
  • profile:distro的配置文件,一个distro可对应一个或多个profile。默认导入时会为distro生成一个profile。每个profile背后关联一个ks文件,用于决定kickstart安装操作系统时的配置。当需要为不同的机器指定不同的操作系统配置时,需要为distro创建多个profile文件,对应多种配置选项。
  • system:system对象不是必须要创建的,但它可以指导cobbler进行更定制化的操作。比如说,我们有多个profile文件,需要为不同的机器安装不同配置的操作系统,没有system对象时,需要手动选择profile文件,而通过system对象可以使指定机器使用指定的profile配置。

暂时我们没有使用system对象。

修改profile

假设我们的需求是这样的:

  1. 安装一个标准的Centos7
  2. 安装一些必要的软件包
  3. 安装时只对系统盘进行分区和格式化,其他磁盘不动
  4. 为了方便使用虚拟机测试整个安装流程,需要在磁盘分区时自动适配磁盘名如vda/sda
  5. 安装完成后对一些基础配置进行设置

下面针对上面的需求,进行profile修改。

修改ks templates

cobbler基于kickstart进行操作系统的安装,kickstart使用ks文件完成安装过程的配置。如前文所说,ks文件与profile绑定,我们需要对其进行自定义来满足不同的部署要求。当系统通过PXE引导至profile选择菜单后,一旦选定了需要部署的系统,接下来就会按照该profile所对应的ks文件来执行一系列的安装操作。

在cobbler中ks文件的实例是通过cgi动态生成的,所以我们需要修改的是ks templates文件,而不是raw ks文件。

通过cobbler profile report --name=centos7-x86_64命令,得到原始的ks tempate文件。

Kickstart       :/var/lib/cobbler/kickstarts/sample_end.ks

我们要做的,就是基于原始的ks模版文件,进行修改。修改完成后,修改profile中的Kickstart文件路径,使其指向我们自己的ks模版文件。

# 复制原始文件
cp /var/lib/cobbler/kickstarts/sample_end.ks /var/lib/cobbler/kickstarts/customize_end.ks
# 修改模版文件
vim /var/lib/cobbler/kickstarts/customize_end.ks
# 修改profile指向的ks模版文件
cobbler profile edit --name=centos7-x86_64 --kickstart=/var/lib/cobbler/kickstarts/customize_end.ks 

ks template和ks文件的简单介绍:

  • ks template文件
    ks模版文件使用 Cheetah作为模版引擎,使用Snippets进行代码片的复用。

文档:
http://cobbler.github.io/manuals/2.8.0/3/5_-Kickstart_Templating.html
http://cobbler.github.io/manuals/2.8.0/3/6
-_Snippets.html

  • ks文件
    ks文件包含下面四部分:
    • 命令部分:这里应该包括必需的选项,包含语言的选择,防火墙,密码,网络,分区的设置等;
    • %packages部分:这部分选择需要安装的软件包,可以是 @core 这样的group的形式,也可以是这样 vim-* 包的形式;
    • %pre部分:安装前解析的脚本,通常用来生成特殊的ks配置,比如由一段程序决定磁盘分区等;
    • %post部分:安装后执行的脚本,通常用来做系统的初始化设置。比如启动的服务,相关的设定等。

文档:
https://access.redhat.com/documentation/zh-cn/red_hat_enterprise_linux/6/html/installation_guide/s1-kickstart2-file

修改之后的customize_end.ks文件完整如下:

# This kickstart file should only be used with EL > 5 and/or Fedora > 7.
# For older versions please use the sample.ks kickstart file.

#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 --disabled
# Run the Setup Agent on first boot
firstboot --disabled
# System keyboard
keyboard us
# System language
lang en_US
# Use network installation
url --url=$tree
# 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
# Customized
timezone  Asia/Shanghai
# Install OS instead of upgrade
install
# Clear the Master Boot Record
zerombr
# Allow anaconda to partition the system as needed
#autopart
# To support part manually
$SNIPPET('main_partition_select')

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

%packages
$SNIPPET('func_install_if_enabled')
@^minimal
@core
chrony
wget
net-tools
python-setuptools
rsync
lrzsz
expect
tcl
ntpdate
-selinux-policy*
-NetworkManager*
-kexec-tools
-snappy
-wpa_supplicant
-ppp
%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('cobbler_register')
# Enable post-install boot notification
$SNIPPET('post_anamon')
# Start final steps
$SNIPPET('kickstart_done')
# End final steps
%end

为了自动识别虚拟机和物理机,我们增加了一个snippet:pre_partition_select_vda_or_sda

所以,还需要在/var/lib/cobbler/snippets/路径下,增加我们的snippet。

vim /var/lib/cobbler/snippets/pre_partition_select_vda_or_sda

将下面内容,输入到/var/lib/cobbler/snippets/pre_partition_select_vda_or_sda文件内。

# Determine architecture-specific partitioning needs
if [ -b /dev/vda ]; then
  cat >/tmp/partinfo << EOF
clearpart --initlabel --all
ignoredisk --only-use=vda
bootloader --location=mbr --boot-drive=vda --driveorder=vda
clearpart --initlabel --drives=vda
part /boot --fstype=ext3 --ondisk=vda --size=500
part / --fstype=xfs --size=1024 --grow --ondisk=vda --asprimary
EOF
elif [ -b /dev/sda ]; then
  cat >/tmp/partinfo << EOF
clearpart --initlabel --all
ignoredisk --only-use=sda
bootloader --location=mbr --boot-drive=sda --driveorder=sda
part /boot --fstype=ext3 --ondisk=sda --size=500
part / --fstype=xfs --size=100000 --ondisk=sda --asprimary
part /data --fstype=xfs --grow --ondisk=sda
EOF
fi

校验ks 模版文件

运行如下命令,校验ks,这个命令会输出一些建议。如果输出all kickstarts seem to be ok表示一切正常,。

cobbler validateks

可以通过getks命令查看通过我们模版渲染出的完整ks文件

cobbler profile getks --name=centos7-x86_64

虚拟机测试

使用vmware虚拟机进行测试。创建一台centos 7虚拟机,部署cobbler服务器。

创建一台空白虚拟机,保证网络与之前创建的centos 7虚拟机在同一环境。启动虚拟机。
一切正常的话,会出现如下界面,选择第二项,开始安装。


批量安装操作系统 by Cobbler_第1张图片
image.png

从启动到安装完成,只需要人工干预一次----选择profile。其他步骤完全自动化。安装一台机器大概需要5-10分钟,受网络环境影响。

进行安装

现在我们的cobbler服务器已经就绪了。下一步,需要将集群中的其他机器与cobbler保证网络连通,然后进入bios选择以PXE方式启动,进行安装。

参考资料:

http://cobbler.github.io/manuals/quickstart/
http://cobbler.github.io/manuals/2.8.0/3/5_-Kickstart_Templating.html
http://cobbler.github.io/manuals/2.8.0/3/6
-_Snippets.html
https://access.redhat.com/documentation/zh-cn/red_hat_enterprise_linux/6/html/installation_guide/s1-kickstart2-file

你可能感兴趣的:(批量安装操作系统 by Cobbler)