当前位置: 首页 > news >正文

红帽7基于kickstart搭建PXE环境

Kickstart 文件是一种配置文件,用于定义 Linux 系统安装过程中的各种参数,如分区、网络配置、软件包选择等。system-config-kickstart 提供了一个图形界面,方便用户快速生成这些配置文件。

用户可以通过图形界面进行系统安装的详细配置,包括:

  • 安装方法(如网络安装、本地安装等)。

  • 网络设置(如 IP 地址、网关、DNS 等)。

  • 分区信息(如分区大小、文件系统类型等)。

  • 软件包选择(如安装哪些软件包或组)。

  • 预安装和安装后脚本(用于执行自定义操作)。

配置流程

由于system-config-kickstart在redhat7.9以上就不再提供了,所以建议选择7.9以下的版本或centos7.9以下

逻辑思路:通过一台服务器httpd服务分享我们之后需要安装的主机提供光盘文件,和我们使用system-config-kickstart 生成的kickstart配置文件。新主机在安装的时候不从硬件启动,先通过DHCP获取到IP地址和根据下一步的IP去找获取共享资源的tftp服务器,这个服务器共享一个引导文件pxelinux.0,用于网络启动的引导程序文件。让系统去找pxelinux.cfg/default文件,并定义内核文件、启动参数,其中就含有我们httpd服务分享的那些资源。然后根据kickstart实现自动化安装和配置

为了省事儿我们把DHCP、kickstart、httpd和tftp服务都放在一台主机上提供。主机IP地址为:192.168.118.134

环境准备

由于kickstart它是有界面的,所以我们需要下载界面。如果读者安装rhel7是带有界面的,这一步跳过。

yum group list
Loaded plugins: langpacks, product-id, search-disabled-repos, subscription-manager

This system is not registered with an entitlement server. You can use subscription-manager to register.

Repository 'baseos' is missing name in configuration, using id
Installed Environment Groups:
   Server with GUI
Available Environment Groups:
   Minimal Install
   Infrastructure Server
   File and Print Server
   Basic Web Server
   Virtualization Host
Available Groups:
   Compatibility Libraries
   Console Internet Tools
   Development Tools
   Graphical Administration Tools
   Legacy UNIX Compatibility
   Scientific Support
   Security Tools
   Smart Card Support
   System Administration Tools
   System Management
Done

查看好我们需要的Server with GUI这个包之后,我们直接安装,顺便安装上DHCP,httpd,system-config-kickstart这几个包

yum group install "Server with GUI" -y
yum install system-config-kickstart dhcp httpd -y

准备提供服务的系统盘 

为了避免不必要的麻烦,请事先关闭selinux和防火墙:setenforce 0 && systemctl stop firewalld

我们先将系统盘挂载的目录创建一个软连接到httpd服务的目录下面

ln -s /mnt /var/www/html
[root@localhost ~]# ll /var/www/html/
total 0
lrwxrwxrwx. 1 root root 4 Feb 22 09:19 mnt -> /mnt

systemctl start httpd

 启动之后自己用浏览器查看一下

配置kickstart

咱要先进入到GUI界面,然后才能启动kickstart:init 5进入

然后自己随便配置一下,找到terminal。在里面输入system-config-kickstart

 下图中的Reboot system after installation不能勾上,要不然它之后安装好重启又会链接到DHCP,然后反复重装系统

下一步installation method选择HTTP,写上server地址,和目录即可

 第三步的boot loader options保持默认即可

第四步是磁盘分区的,我们不想安装master那样分区,选择Clear Master Boot Recoed,磁盘的标签也要初始化Initial the disk label

我们需要给上面的Layout添加一个boot分区,一个/分区和一个swap内存分区

把剩下的全部给根分区好了

 

配置好之后,第五步,是配置网络的,这一步需要添加一块网卡,要DHCP的

 跳过Authentication Configuration(配置验证方式的,默认就可以),第六步配置防火墙,关闭就好

 第七步把那个Install a graphicial environment关掉,这个是安装图形化界面的

最后一步,可以编写自己安装好了系统之后执行的脚本,我们跳过了Package Selection这一步,这一步是可以选择装系统是安装的软件包,这里没法配置。我们可以之后在生成的ks.cfg文件中添加上。

我们在Post-Installation Scrpt中写个脚本,要求它自动挂载

cat > /etc/yum.repo.d/rhel7.repo << EOF
[rhel]
basename=rhel
baseurl=http://192.168.118.134/mnt
gpgcheck=0
enabled=1
EOF

yum clean all && yum makecache

 编辑好之后保存即可,我这里不是以root用户启动界面的,所以我没法保存到root目录下,就保存到Redhat用户目录下

init 3回到终端模式

将创建好的ks.cfg给移动到http服务目录下:mv /home/redhat/ks.cfg /var/www/html/

回到终端模式之后需要重新启动一下httpd:systemctl start httpd

参考桌面的anaconda.cfg我们在里面添加上需要安装到软件包:

%packages

@base #示安装系统的基础软件包组,它提供了一个最小化的、功能完备的 Linux 系统

net-tools

vim

tree

tar

%end

使用syslinux作为引导安装

这里会带着大家一步步从官方文档中找到配置syslinux的方法

yum install syslinux -y

rpm -ql命令用于列出指定 RPM 包中包含的所有文件及其安装路径:rpm -ql syslinux

 先看看README这个文件: more /usr/share/doc/syslinux-4.05/README

 由于我们是使用的pxelinux这个功能,这里要我们去寻找pxelinux.txt文档

[root@localhost ~]# rpm -ql syslinux | grep pxelinux.txt
/usr/share/doc/syslinux-4.05/pxelinux.txt

 文中提到了需要使用到tftp服务,和需要创建/tftpboot/pxelinux.cfg,并且里面需要存放pxelinux.0文件和任何需要引导的内核和initrd镜像

 我们下载tftp-server:yum install tftp-server -y,默认情况下/var/lib/tftpboot就是它服务器的默认根目录。

根据上面图片提到的信息,我们把pxelinu.0和我们本台主机的ISO 镜像或光盘引导系统移动到这个目录下面

rpm -ql syslinux | grep pxelinux.0
/usr/share/syslinux/gpxelinux.0
/usr/share/syslinux/pxelinux.0
cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/
cp /mnt/isolinux/* /var/lib/tftpboot/

 注释:isolinux是专门用于从 ISO 镜像或光盘引导系统,就是咱们在安装系统时的初始那个引导界面

最后创建好/var/lib/tftpboot/pxelinux.cfg这个目录,在这个目录中的的配置可以从根据syslinux.txt中查看(地址为rpm -ql syslinux | grep syslinux.txt)。

不过可以偷懒: [root@localhost tftpboot]# cp isolinux.cfg ./pxelinux.cfg/default

我们修改一下里面的参数

default vesamenu.c32
timeout 30#3秒钟,选择系统的时候不用等默认的60秒

...

label linux
  menu label ^Install Red Hat Enterprise Linux 7.9
  menu default
  kernel vmlinuz
  append initrd=initrd.img repo=http://192.168.118.134/mnt/ ks=http://192.168.118.134/ks.cfg quiet

label check
  menu label Test this ^media & install Red Hat Enterprise Linux 7.9
  kernel vmlinuz
  append initrd=initrd.img inst.stage2=hd:LABEL=RHEL-7.9\x20Server.x86_64 rd.live.check quiet

....

 

默认进入安装引导的时候是:Test this ^media & install Red Hat Enterprise Linux 7.9

改为直接安装:^Install Red Hat Enterprise Linux 7.9  #这个标签可以内容可以改,自定义的。然后修改源为我们自己配置的repo=http://192.168.118.134/mnt/ ks=http://192.168.118.134/ks.cfg ,quite表示静默安装

配置完之后:wq!退出

systemctl enable --now tftp

配置DHCP服务

这一步是为了需要自动安装的主机,自动获取IP之后指定其下一步的行为。让他通过tftpboot里面的pxelinux.0进行后续的安装

yum install dhcp -y
[root@localhost pxelinux.cfg]# rpm -ql dhcp
/etc/NetworkManager
/etc/NetworkManager/dispatcher.d
/etc/NetworkManager/dispatcher.d/12-dhcpd
/etc/dhcp/dhcpd.conf
/etc/dhcp/dhcpd6.conf
/etc/dhcp/scripts
/etc/dhcp/scripts/README.scripts
/etc/openldap/schema/dhcp.schema
/etc/sysconfig/dhcpd
/usr/bin/omshell
/usr/lib/systemd/system/dhcpd.service
/usr/lib/systemd/system/dhcpd6.service
/usr/lib/systemd/system/dhcrelay.service
/usr/sbin/dhcpd
/usr/sbin/dhcrelay
/usr/share/doc/dhcp-4.2.5
/usr/share/doc/dhcp-4.2.5/dhcpd.conf.example
/usr/share/doc/dhcp-4.2.5/dhcpd6.conf.example

显然/etc/dhcp/dhcpd.conf就是我们需要配置的文件,其中/usr/share/doc/dhcp-4.2.5/dhcpd.conf.example是提供给我们的示例

cp -f /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example /etc/dhcp/dhcpd.conf

进入到这个配置文件中删除掉dhcp功能之后的所有配置,和bootp功能

option domain-name "example.org";
option domain-name-servers ns1.example.org, ns2.example.org;

default-lease-time 600;
max-lease-time 7200;

# Use this to enble / disable dynamic dns updates globally.
#ddns-update-style none;

# If this DHCP server is the official DHCP server for the local
# network, the authoritative directive should be uncommented.
#authoritative;

# Use this to send dhcp log messages to a different log file (you also
# have to hack syslog.conf to complete the redirection).
log-facility local7;

# No service will be given on this subnet, but declaring it helps the
# DHCP server to understand the network topology.

#subnet 10.152.187.0 netmask 255.255.255.0 {
#}

# This is a very basic subnet declaration.
# This declaration allows BOOTP clients to get dynamic addresses,
# which we don't really recommend.

# A slightly different configuration for an internal subnet.
subnet 192.168.118.0 netmask 255.255.255.0 {
  range 192.168.118.140 192.168.118.180;
  option domain-name-servers 114.114.114.114;
  option routers 192.168.118.2;
  next-server 192.168.118.134;
  filename "pxelinux.0";
}

当需要安装的主机在获取到了IP地址,之后,会通过tftp到192.168.118.134这个主机去找pxelinux.0引导程序。 

以上DHCP就配置好了,在启动之前需要关闭VMware的DHCP功能,选择虚拟网络编辑器

 找到118网段的网卡,关闭DHCP

启动dhcpd

systemctl enable --now dhcpd

以上就全部配置好了,现在进行测试

测试

先创建一台虚拟机壳子,直接移除掉CD/DVD,这一步可以不做,为了凸显效果,我直接移除了

右击新创建的虚拟机,点击"打开电源时进入固件"

选择从网络启动,使用shift加+将选项向上移动。之后保存退出

 然后在Exit选择Exiting Saving Changes退出即可。正在自动部署

 等待安装好了之后,重复上面的步骤把我们的从网络启动改回到从硬件启动。进入到"打开电源时进入固件",然后 选择Hard Drive硬件启动,再次启动之后就是我们定制好的系统了

 

 

相关文章:

  • 【MySQL 一 数据库基础】深入解析 MySQL 的索引(3)
  • redis-bitmap使用场景
  • CSS中的`position`属性的几种定位方式
  • 计算机视觉算法实战——三维重建(主页有源码)
  • Linux提权之suid提权(六)
  • Python--函数进阶(下)
  • SpringBoot+Vue+微信小程序的猫咖小程序平台(程序+论文+讲解+安装+调试+售后)
  • Http模块及练习
  • C++单例模板类,继承及使用
  • C语言宏定义的底层应用
  • 【SpringMVC】概述 SSM:Spring + SpringMVC + Mybats
  • 在CentOS安装Docker
  • Redis常用数据类型及其应用案例
  • 机器学习数学基础:30.Pearson相关系数及t检验教程
  • 信息安全实战04_ECC椭圆曲线加密算法原理详解
  • 蓝桥杯试题:区间次方和(前缀和)
  • Gin从入门到精通 (四)请求参数
  • 网络运维学习笔记 022 HCIA-Datacom新增知识点03园区网典型组网架构及案例实战
  • 第一届网谷杯
  • 力扣每日一题【算法学习day.133】
  • 阿斯利康中国区一季度收入增5%,或面临最高800万美元新罚单
  • 移动互联网未成年人模式正式发布
  • 张炜琳已任三明市委常委、宣传部部长
  • 顺利撤离空间站,神十九乘组踏上回家之旅
  • 当初没有珍惜巴特勒的热火,被横扫出局后才追悔莫及
  • 人民日报评论员:汇聚起工人阶级和广大劳动群众的磅礴力量