PXE | kickstart无人值守

134 阅读5分钟

1、PXE 预启动执行环境

1.1 PXE批量部署的优点

  • 规模化:同时装配多台服务器
  • 自动化:安装系统、配置各种服务
  • 远程实现:不需要光盘、U盘等安装介质

image.png

1.2 系统装机的三种引导方式

  1. 硬盘
  2. 光驱(u盘)
  3. 网络装机

小拓展: 光盘,涉密单位存储数据

  • 多次性的,可以反复擦写
  • 一次性的

1.3 系统安装过程

  1. 加载boot loader
  2. 加载启动安装菜单
  3. 加载内核和initrd系统(精简版的linux系统)
  4. 加载根系统
  5. 运行anaconda的安装向导

1.4 安装系统的相关文件

1、光驱安装

光驱安装操作系统,在isolinux目录下有和安装相关的必备文件。

  1. boot.cat: 相当于grub的第一阶段 446字节
  2. isolinux.bin:光盘引导程序,在mkisofs的选项中需要明确给出文件路径,这个文件属于SYSLINUX项目
  3. isolinux.cfg:启动菜单的配置文件,当光盘启动后(即运行isolinux.bin),会自动去找isolinux.cfg文件
  4. vesamenu.c32:菜单文件,是光盘启动后的启动菜单图形界面,也属于SYSLINUX项目,menu.c32提供纯文本的菜单
  5. memtest:内存检测程序
  6. splash.png:光盘启动菜单界面的背景图,默认是黑色
  7. vmlinuz:是内核映像
  8. initrd.img:ramfs文件(精简版的linux系统,文件系统驱动等)

2、PXE安装

PXE安装操作系统,只需要四大文件(按顺序):

  1. pxelinux.0:引导程序。来源于syslinux程序
  2. default:配置文件。(需要手写,可参考光盘 isolinux 目录下的 isolinux.cfg 文件)
  3. vmlinuz:内核。从光盘中获取,位于 isolinux 目录
  4. initrd.img: 系统启动镜像文件。(从光盘中获取,位于 isolinux 目录下)

2、四大服务

  • dhcp
  • tftp
  • vsftpd
  • syslinux

2.1 dhcp

提供ip地址,注意 网络 必须要通。

2.2 tftp

简单文件传输协议:内核和引导文件。

提供四大文件,vmlinuz、pxelinux.0、initrd.img、default…

2.3 syslinux

引导加载程序 引导pxelinux.0

2.4 vsftpd

安装系统镜像文件的软件包

3、PXE实现过程

3.1 PXE原理

image.png

3.2 实验一:PXE配置

实验目的:

实验环境:

实验流程:

  1. 安装软件
 [root@localhost ~]#  systemctl stop firewalld
 [root@localhost ~]#  setenforce 0
 [root@localhost ~]# yum install tftp-server   vsftpd   syslinux  dhcp   -y   //四个软件
  1. 设置dhcp
 [root@localhost ~]#  cat /etc/dhcp/dhcpd.conf
 #
 # DHCP Server Configuration file.
 #   see /usr/share/doc/dhcp*/dhcpd.conf.example
 #   see dhcpd.conf(5) man page
 #
 [root@localhost ~]#  ls /usr/share/doc/dhcp*/dhcpd.conf.example
 /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example
 [root@localhost tftpboot]#  cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example /etc/dhcp/dhcpd.conf
 cp:是否覆盖"/etc/dhcp/dhcpd.conf"yes
 [root@localhost tftpboot]#  vim /etc/dhcp/dhcpd.conf
 ​
 subnet 192.168.125.0 netmask 255.255.255.0 {
   range 192.168.125.20 192.168.125.50;
   option routers 192.168.125.130;
   next-server 192.168.125.130;
   filename "pxelinux.0";
 }
 ​
 [root@localhost ~]#  systemctl start dhcpd

image.png 3. 设置tftp:

主配置文件:/etc/xinetd.d/tftp

 [root@localhost ~]#  vim /etc/xinetd.d/tftp   //进入主配置文件
 disable                 = no   //yes改成no

image.png

tftp根目录:/var/lib/tftpboot,该位置也是四大文件存放的位置

 `vmlinuz initrd.img`  //前两个文件
 [root@localhost ~]#  mount /dev/sr0 /mnt   //为什么挂载,因为内核和镜像都在mnt文件下。
 [root@localhost ~]#  cd /var/lib/tftpboot
 [root@localhost tftpboot]#  cd /mnt/isolinux/
 [root@localhost isolinux]#  cp vmlinuz initrd.img  /var/lib/tftpboot/   //将内核、镜像复制到/var/lib/tftpboot/下
 [root@localhost isolinux]#  cd /var/lib/tftpboot/ 
 [root@localhost tftpboot]#  
 [root@localhost tftpboot]#  ls
 initrd.img  vmlinuz  //此时,存放位置已有两个文件
  1. 设置syslinux
 `pxelinux.0`   //第三个文件
 [root@localhost tftpboot]#  rpm -ql syslinux |grep "pxelinux.0"
 /usr/share/syslinux/gpxelinux.0
 /usr/share/syslinux/pxelinux.0   //选这个
 [root@localhost tftpboot]#  cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/
 [root@localhost tftpboot]#  ls
 initrd.img  pxelinux.0  vmlinuz
 //  已有三个文件
  1. 设置vsftpd
 `pxelinux.cfg`    // 第四个文件  手写
 [root@localhost tftpboot]#  mkdir pxelinux.cfg
 [root@localhost tftpboot]#  cd pxelinux.cfg
 [root@localhost pxelinux.cfg]#  vim default
 ​
 #要回车
 default auto
 prompt 1
 ​
 label auto
         kernel vmlinuz
         append initrd=initrd.img method=ftp://192.168.125.130/centos7
 ​
 label linux text
         kernel vmlinuz  //内核叫什么名字
         append text initrd =initrd.img method=ftp://192.168.125.130/centos7   //告诉你小型linux叫什么
 ​
 label linux rescue
         kernel vmlinuz
         append rescue initrd=initrd.img method=ftp://192.168.125.130/centos7  //method是路径
  
 #不用回车,用这个就行!!!!注意改ip
 default linux
 prompt 1
 ​
 label linux
         kernel vmlinuz 
         append initrd=initrd.img method=ftp://192.168.125.130/centos7
 ​
 label linux text
         kernel vmlinuz
         append text initrd =initrd.img method=ftp://192.168.125.130/centos7
 ​
 label linux rescue
         kernel vmlinuz
         append rescue initrd=initrd.img method=ftp://192.168.125.130/centos7
  1. 准备完毕,重启服务
 [root@localhost pxelinux.cfg]#  mkdir /var/ftp/centos7
 [root@localhost pxelinux.cfg]#  mount /dev/sr0 /var/ftp/centos7/
 [root@localhost pxelinux.cfg]#  cd /var/lib/tftpboot/ 
 [root@localhost tftpboot]#  ls
 initrd.img  pxelinux.0  pxelinux.cfg  vmlinuz   
 [root@localhost tftpboot]#  tree
 .
 ├── initrd.img
 ├── pxelinux.0
 ├── pxelinux.cfg
 │   └── default
 └── vmlinuz
 ​
 1 directory, 4 files             //四大文件已准备好
 ​
 [root@localhost tftpboot]#  cd ~
 [root@localhost ~]# systemctl stop firewalld.service 
 [root@localhost ~]# setenforce 0
 [root@localhost ~]# systemctl start tftp.socket
 [root@localhost ~]# systemctl start dhcpd
 [root@localhost ~]# systemctl start vsftpd
 //只开启三个,syslinux不是软件,无需开启

3.3 测试

创建空白虚拟机测试能否自动装机

image.png

image.png

image.png

image.png

测试成功!

4、kickstart 无人值守

图形化界面 自动化

安装系统的配置文件:`vim anaconda-ks.cfg

4.1 kickstart文件的组成

由三部分组成,分别是\color{red}{命令段、程序包段、脚本段。}

  1. 命令段:

指明各种安装前配置,如键盘类型等

 // 命令段中的常见命令
 keyboard: 设定键盘类型
 lang: 语言类型
 zerombr:清除mbr
 clearpart:清除分区
 part: 创建分区
 rootpw: 指明root的密码
 timezone: 时区
 text: 文本安装界面
 network:指定网络设置
 firewall:设置防火墙设置
 selinux:设置selinux设置
 reboot:安装完自动重启
 user:安装完成后为系统创建新用户
 url: 指明安装源
  1. 程序包段:

指明要安装的程序包组或程序包,不安装的程序包等

  %packages
  @^environment group: 指定环境包组,如:@^minimal-environment
  @group_name
  package
  -package
  %end
  1. 脚本段:
  • %pre: 安装前脚本
  • %post: 安装后脚本
 //系统安装后,新建用户zhangsan
 %post
 useradd zhangsan
 %end   
 //%post与%end 成对出现

4.2 实验二:

实验目的:

实验环境:

实验流程:

  1. 安装软件
 [root@localhost ~]#  systemctl stop firewalld
 [root@localhost ~]#  setenforce 0
 [root@localhost tftpboot]# yum install system-config-kickstart -y
  1. 打开虚拟机(192.168.125.130)的图形化界面

image.png

  • 基本配置

image.png

  • 安装方法

image.png

  • 引导装载程序选项

image.png

  • 分区信息

image.png

image.png

  • 网络配置,默认即可
  • 验证,默认即可
  • 防火墙配置

image.png

  • 显示配置

image.png

  • 软件包选择,目前先不用改
  • 安装后脚本

image.png

  1. 保存设置

image.png

image.png

  1. 配置
 [root@localhost ~]#  cd /var/ftp
 [root@localhost ftp]#  ll
 总用量 6
 drwxr-xr-x. 8 root root 2048 9月   5 2017 centos7
 -rw-r--r--. 1 root root  983 5月   7 00:45 ks.cfg
 drwxr-xr-x. 2 root root    6 6月  10 2021 pub
 [root@localhost ftp]#  cd /var/lib/tftpboot/pxelinux.cfg
 [root@localhost pxelinux.cfg]#  vim default

image.png

  1. 测试

重新启动新安装的虚拟机

image.png