2024-11-08 19:24:00 +08:00

161 lines
4.3 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: "PXE 网络装机"
date: 2019-11-08T15:52:55+08:00
lastmod: 2024-11-08T18:17:00+08:00
tags: ["pxe"]
categories: ["OS"]
---
## 部署 dhcp 和 tft 服务
- 安装 dnsmasq
```bash
# rhel
yum install dnsmasq
# archlinux
pacman -S dnsmasq
```
- 修改配置 /etc/dnsmasq.conf
```ini
port=0 # 用不着 dns 功能,可以关闭
dhcp-range=10.0.86.2,10.0.86.9,255.255.255.0,1h
#dhcp-boot=pxelinux.0 # bios 引导(未测试)
dhcp-boot=grubx64.efi # efi 引导
enable-tftp
tftp-root=/var/ftp
```
- 在其中一个网卡上配置 ip: 10.0.86.1/24
- 启动 dnsmasq
```bash
systemctl start dnsmasq
```
## 挂载操作系统镜像
- 目前已测试过的操作系统
* centos 7/8/9
* rockyLinux 8/9
* 银河麒麟服务器版 V10 SP3
* 华为欧拉 24.03 LTS
- 上传操作系统镜像 iso 到 dnsmasq 服务器
- 挂载 iso 到 /mnt/iso 目录
```bash
mkdir /mnt/iso
mount -o loop xxxx.iso /mnt/iso
```
- 在系统镜像的挂载目录(/mnt)启动 http 服务
```bash
cd /mnt
python2 -m SimpleHTTPServer 10086
# 或者使用 python3
python3 -m http.server 10086
```
## 创建 kicksart 自动安装脚本
- 创建 /mnt/ks.cfg 文件,内容如下
```
# Use graphical install
graphical
# Keyboard layouts
keyboard --vckeymap=cn --xlayouts='cn'
# System language
lang zh_CN.UTF-8
# 注释其中可能存在的 U盘或光盘安装设备配置项 “harddrive”
# Use hard drive installation media
#harddrive --dir= --partition=LABEL=openEuler-24.03-LTS-x86_64
%packages
@^minimal-environment
%end
# Run the Setup Agent on first boot
firstboot --enable
# 确认系统硬盘设备名sata 或 sas 第一块盘是 sdanvme 第一块盘是 nvme0n1
# Generated using Blivet version 3.8.2
ignoredisk --only-use=sda
# Partition clearing information
clearpart --none --initlabel
# Disk partitioning information
# 创建 efi 分区,传统 BIOS 引导不需要 efi 分区
part /boot/efi --fstype="efi" --ondisk=sda --size=256 --fsoptions="umask=0077,shortname=winnt"
# 这里指定 size 是 99GB可按实际容量调整
part / --fstype="xfs" --ondisk=sda --size=101376
# System timezone
timezone Asia/Shanghai --utc
# Root password
rootpw --iscrypted $y$j9T$VB3hYFCRRHaCTsOM/DwE2KUX$Ci6f4pASC887sewVuvjFqTXHN.g5nsVsPoca9RntMdD
%addon com_redhat_kdump --disable --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
# 安装完成后的操作,这里禁用了 selinux 和自带的防火墙
%post
/usr/bin/systemctl disable firewalld
sed -i '/^SELINUX=/cSELINUX=disabled' /etc/selinux/config
%end
```
## 复制网络引导文件
### EFI 引导
- **不支持 secure boot**
- 复制镜像里的启动文件到 dnsmasq 服务器的 /var/ftp/ 下
```bash
cp -a /mnt/EFI/BOOT/* /var/ftp/
cp -a /mnt/images/pxeboot/{initrd.img,vmlinuz} /var/ftp/
```
- 编辑 /var/ftp/grub.cfg修改第一个启动项
```
menuentry ... --class gnu-linux --class gnu --class os {
linuxefi vmlinuz inst.repo=http://10.0.86.1:10086/iso inst.ks=http://10.0.86.1:10086/ks.cfg ...
initrdefi initrd.img
}
# 增加 inst.repo 和 inst.ks 这俩参数,其他不变
```
### 传统 BIOS 引导(未测试)
- 复制镜像里的启动文件到 dnsmasq 服务器的 /var/ftp/ 下
```bash
cd /var/ftp
cp /mnt/isolinux/* .
mkidr pxelinux.cfg
mv isolinux.cfg pxelinux.cfg/default
```
- 打开 /var/ftp/pxelinux.cfg/default修改第一个启动项
```
label linux
menu label ...
kernel vmlinuz
append initrd=initrd.img inst.repo=http://10.0.86.1:10086 ...
# ks 参数: inst.ks=<ks.cfg url>
```
- 在 centos7/8 上安装 syslinux
```bash
yum install syslinux # centos7
dnf install syslinux # centos 8
```
- 把 /user/share/syslinux/pxelinux.0 复制到 dnsmasq 服务器的 /var/ftp/ 下
## 装机
- 把待安装机器和 pxe 服务器接入同一个交换机(无其他 dhcp 广播)
- 启动待安装机器,选择 pxe 引导