u2404部署PXE+autoinstall安装u2404和rocky9.6操作系统
PXE+user-data 安装BIOS启动的操作系统ubuntu24.04.3-server(非UEFI)
环境
服务端 Ubuntu24.04 PXE+autoinstall ip 192.168.133.204
客户端 虚拟机硬件系统 PXE网卡启动 DHCP-》ip 192.168.133.205
下载iso netboot
地址: https://releases.ubuntu.com/noble/ and https://rockylinux.org/zh-CN/download
mkdir -p /var/www/html/iso/u240403/
mkdir -p /var/www/html/iso/rocky96/
mv ubuntu-24.04.3-live-server-amd64.iso u240403.isp 上传到 /var/www/html/iso/u240403/
ubuntu-24.04.3-netboot-amd64.tar.gz 上传到 /var/www/html/iso/u240403/
mv Rocky-9.6-x86_64-minimal.iso r96.iso 上传到 /var/www/html/iso/rocky96/
安装配置服务 dnsmasq(dns tftp dhcp)apache2(http)
apt install apache2 dnsmasq
mkdir -p /var/www/tftp/networkboot/{u240403,rocky96}
vim /etc/dnsmasq.d/pxe.conf
interface=ens32
bind-interfaces
port=0
dhcp-range=ens32,192.168.133.20,192.168.133.25,255.255.255.0,12h
dhcp-boot=pxelinux.0
enable-tftp
tftp-root=/var/www/tftp
log-facility=/var/log/dnsmasq.log
systemctl start apache2 && systemctl enbale apache2
systemctl start dnsmasq && systemctl enbale dnsmasq
ss -tunlp|grep 69
udp UNCONN 0 0 127.0.0.1:69 0.0.0.0:* users:(("dnsmasq",pid=2167,fd=7))
udp UNCONN 0 0 192.168.133.204:69 0.0.0.0:* users:(("dnsmasq",pid=2167,fd=6))
udp UNCONN 0 0 [::1]:69 [::]:* users:(("dnsmasq",pid=2167,fd=9))
udp UNCONN 0 0 [fe80::20c:29ff:febb:b123]%ens32:69 [::]:* users:(("dnsmasq",pid=2167,fd=8))
ss -tunlp|grep 67
udp UNCONN 0 0 0.0.0.0%ens32:67 0.0.0.0:* users:(("dnsmasq",pid=2167,fd=4))
TFTP 的BIOS启动相关文件准备
cd /var/www/html/iso/u240403/
tar xf ubuntu-24.04.3-netboot-amd64.tar.gz
cp amd64/pxelinux.0 /var/www/tftp/
mount u240403.iso /mnt
cp /mnt/casper/{vmlinuz,initrd} /var/www/tftp/networkboot/u240403/
umount /mnt
apt install syslinux
cp /usr/lib/syslinux/modules/bios/{ldlinux.c32,libutil.c32,menu.c32} /var/www/tftp/
mkdir -p pxelinux.cfg
vim pxelinux.cfg/default
DEFAULT menu.c32
MENU TITLE << PXE Boot Menu >>
PROMPT 0
TIMEOUT 0
LABEL Ubuntu 24.04.3 BIOS Install
MENU LABEL Install Ubuntu 24.04.3 (BIOS)
KERNEL /networkboot/u240403/vmlinuz
APPEND initrd=/networkboot/u240403/initrd root=/dev/ram0 ramdisk_size=1024000 ip=dhcp biosdevname=0 net.ifnames=0 url=http://192.168.133.204/iso/u240403/u240403.iso autoinstall cloud-config-url=http://192.168.133.204/autoinstall/u240403/user-data --- quiet
LABEL Rockylinux 9 BIOS Install
MENU LABEL Install Rocky Linux 9.6 (BIOS)
kernel /networkboot/rocky96/vmlinuz
append initrd=/networkboot/rocky96/initrd.img inst.stage2=http://192.168.133.204/iso/rocky96 inst.repo=http://192.168.133.204/iso/rocky96 inst.ks=http://192.168.133.204/autoinstall/rocky96/ks.cfg biosdevname=0 net.ifnames=0 quiet
ubuntu debian系列>=20.04 应答文件cloud-init 的user-data 和 meta-data
mkdir -p /var/www/html/autoinstall/u240403/
vim /var/www/html/autoinstall/u240403/meta-data
instance-id: u240403-node01
local-hostname: u240403-node01
vim /var/www/html/autoinstall/u240403/user-data
#cloud-config
autoinstall:
version: 1
source:
id: ubuntu-server
search_drivers: false
locale: en_US.UTF-8
keyboard:
layout: us
timezone: Asia/Shanghai
kernel:
package: linux-generic
apt:
fallback: offline-install
mirror-selection:
primary:
- arches: [amd64]
uri: http://mirrors.aliyun.com/ubuntu/
- arches: [arm64]
uri: http://mirrors.aliyun.com/ubuntu-ports
preserve_sources_list: false
network:
version: 2
ethernets:
eth0:
match:
name: "en*"
set-name: eth0
dhcp4: false
dhcp6: false
addresses:
- 192.168.133.205/24
routes:
- to: default
via: 192.168.133.2
nameservers:
addresses: [8.8.8.8, 114.114.114.114]
identity:
username: kcadmin
hostname: u240403-node01
password: $6$yoUjV35x8cdqrfIr$Q8kgMdzrdoV6hyMkxE7iPKdDuGNweRnonQcFYnPi03.7mvHtWmqUlNwZEnxUkiaiTJ.4v.x8zi2WwCpfwIkcI/
ssh:
allow-pw: true
install-server: true
storage:
layout:
name: direct
updates: security
codecs:
install: false
drivers:
install: false
late-commands:
- echo "Autoinstall completed on $(date)" >> /target/root/install.log
- curtin in-target --target=/target -- systemctl disable ufw
- curtin in-target --target=/target -- systemctl stop ufw
- curtin in-target --target=/target -- bash -c "echo 'root:kc@123456' | chpasswd"
- curtin in-target --target=/target -- sed -i 's/^#\?PermitRootLogin .*/PermitRootLogin yes/' /etc/ssh/sshd_config
- curtin in-target --target=/target -- sed -i 's/^#\?PasswordAuthentication .*/PasswordAuthentication yes/' /etc/ssh/sshd_config
- curtin in-target --target=/target -- systemctl restart ssh
- curtin in-target --target=/target -- bash -c "ulimit -n 655360"
- curtin in-target --target=/target -- bash -c "printf '%s\n' '* hard nproc 655360' '* hard nofile 655360' 'root soft nproc 655360' 'root hard nofile 655360' >> /etc/security/limits.conf"
- curtin in-target --target=/target -- bash -c "printf '%s\n' 'fs.file-max = 655350' 'net.core.somaxconn = 2048' 'net.core.rmem_max = 819200000' 'vm.overcommit_memory = 1' 'vm.swappiness = 0' 'vm.zone_reclaim_mode = 0' >> /etc/sysctl.conf"
- curtin in-target --target=/target -- sysctl -p
rocky centos redhat系列应答文件ks.cfg
mkdir -p /var/www/html/autoinstall/rocky96/
vim /var/www/html/autoinstall/rocky96/ks.cfg
#version=RHEL9
#url --url="http://192.168.133.204/iso/rocky96/BaseOS"
url --url="http://192.168.133.204/iso/rocky96/minimal"
firstboot --disable
text
lang en_US.UTF-8
keyboard --vckeymap=us --xlayouts='us'
network --bootproto=static --device=eth0 --ip=192.168.133.205 --gateway=192.168.133.2 --nameserver=8.8.8.8 --netmask=255.255.255.0 --noipv6 --activate
timezone Asia/Shanghai --utc
%pre
DISK=$(lsblk -ndo NAME,TYPE | awk '$2=="disk"{print $1}' | head -n1)
echo "Detected disk: $DISK" > /tmp/disk.log
echo "ignoredisk --only-use=$DISK" > /tmp/part-include
echo "bootloader --location=mbr --boot-drive=$DISK" >> /tmp/part-include
echo "zerombr" >> /tmp/part-include
echo "clearpart --all --initlabel" >> /tmp/part-include
echo "part /boot --fstype=xfs --ondisk=$DISK --size=1024" >> /tmp/part-include
echo "part swap --fstype=swap --ondisk=$DISK --size=2048" >> /tmp/part-include
echo "part / --fstype=xfs --ondisk=$DISK --size=17407 --grow" >> /tmp/part-include
%end
%include /tmp/part-include
eula --agreed
rootpw --plaintext "Kc@123456"
firewall --disabled
selinux --disabled
reboot
%packages
@^minimal-environment
%end
%addon com_redhat_kdump --disable --reserve-mb='auto'
%end
%post --interpreter /bin/bash
sed -i 's/#PermitRootLogin.*/PermitRootLogin yes/' /etc/ssh/sshd_config
sed -i 's/#PermitEmptyPasswords.*/PermitEmptyPasswords no/' /etc/ssh/sshd_config
systemctl restart sshd
cat >/etc/security/limits.d/99-custom-limits.conf<<EOF
* soft nofile 65536
* hard nofile 131072
* soft nproc 65536
* hard nproc 131072
EOF
cat >/etc/sysctl.d/99-custom-sysctl.conf<<EOF
fs.file-max = 2097152
vm.swappiness = 10
net.core.somaxconn = 65535
net.ipv4.tcp_tw_reuse = 1
net.ipv4.ip_forward = 1
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.default.rp_filter = 1
EOF
sysctl --system
%end
docker化 PXE Dockerfile
# PXE install ubuntu24.04 rocky9.6
FROM ubuntu:24.04
LABEL maintainer="luoke <88643217@qq.com>"
LABEL description="PXE Server with dnsmasq and apache2 for Ubuntu 24.04 automated installs"
ENV DEBIAN_FRONTEND=noninteractive
# 安装必要组件
RUN apt update && apt install -y \
dnsmasq \
apache2 \
syslinux \
pxelinux \
curl \
unzip \
gettext-base && \
rm -rf /var/lib/apt/lists/*
# 启动脚本编写
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
# dnsmasq 负责PXE DHCP TFTP服务 配置文件 /etc/dnsmasq.d/pxe.conf
RUN echo "log-queries" >>/etc/dnsmasq.conf && \
echo "log-dhcp" >>/etc/dnsmasq.conf && \
echo "log-facility=/dev/stdout" >>/etc/dnsmasq.conf && \
echo "conf-dir=/etc/dnsmasq.d" >>/etc/dnsmasq.conf
RUN mkdir -p /var/www/tftp
COPY pxe.conf.template /pxe.conf.template
# apache 为PXE提供 iso文件 和 autoinstall的user-data文件和ks文件
RUN echo "ServerName localhost" >> /etc/apache2/apache2.conf && \
mkdir -p /var/www/html/{autoinstall,iso} && \
mkdir -p /var/www/html/autoinstall/{u240403,rocky} && \
mkdir -p /var/www/html/iso/{u240403,rocky96}
EXPOSE 67/udp 69/udp 53/udp 53/tcp 80/tcp
ENTRYPOINT ["/entrypoint.sh"]
dockerfile 启动文件 entrypoint.sh
#!/bin/bash
# 渲染 PXE 配置
envsubst < /pxe.conf.template > /etc/dnsmasq.d/pxe.conf
# 启动 Apache(后台)
service apache2 start
# 启动 dnsmasq(前台)
exec dnsmasq -k
dockerfile pxe.conf文件
cat pxe.conf.template
interface=${INTERFACE}
bind-interfaces
port=0
dhcp-range=${DHCP_START},${DHCP_END},#{DHCP_MASK},12h
dhcp-boot=pxelinux.0
enable-tftp
tftp-root=/var/www/tftp
docker run 提前关闭53 (systemctl stop systemd-resolved)
docker run -itd \
--name=pxe-server \
--hostname=pxe-server \
--restart=always \
--het=host \
--cap-add=NET_ADMIN \
-e INTERFACE=ens32 \
-e DHCP_START=192.168.133.200 \
-e DHCP_END=192.168.133.202 \
-e DHCP_MASK=255.255.255.0 \
-e TFTP_ROOT=/var/www/tftp \
-v ./html:/var/www/html \
-v ./tftp:/var/www/tftp \
pxe-server:u240403
# 启动后检查 67 69 53 80 端口
ss -tunlp
那么以后只关注以下几个文件
宿主机的iso html/iso/u240403/u240403.iso 镜像文件 and html/iso/rocky96/ 解压后文件
/u240403
amd64 u240403.iso ubuntu-24.04.3-netboot-amd64.tar.gz
rocky96/
BaseOS EFI images isolinux LICENSE media.repo minimal r96-mini.iso
宿主机的自动安装文件 html/autoinstall/u240403/user-data and html/autoinstall/rocky96/ks.cfg
autoinstall/
├── rocky96
│ ├── ks.cfg
│ └── ks.cfg.back
└── u240403
├── meta-data
└── user-data
宿主机的pxe启动文件 tftp/{ldlinux.c32,libutil.c32,menu.c32,networkboot,pxelinux.0,pxelinux.cfg/default}
tree
.
├── ldlinux.c32
├── libutil.c32
├── menu.c32
├── networkboot
│ ├── rocky96
│ │ ├── initrd.img
│ │ └── vmlinuz
│ └── u240403
│ ├── initrd
│ └── vmlinuz
├── pxelinux.0
└── pxelinux.cfg
└── default
本文来自博客园,站在巨人的肩膀上,坚持开源精神,遵循开源协议:Apache Licene 2.0协议。
浙公网安备 33010602011771号