Linux 运维手册之 PXE 自动化安装系统

后知后觉 暂无评论

PXEPre-boot Execution Environment,预启动执行环境),通过网络接口启动计算机,不依赖本地存储设备(如硬盘)或本地已安装的操作系统。

PXE 与 kickstart 安装系统,实际上 COBBLER 程序就是 PXE 的一种封装。

实验环境

属性
操作系统CentOS Linux release 7.5.1804 (Core)
网卡地址192.168.100.1/24
光盘镜像CentOS-7-x86_64-DVD-1804.iso

原理图

部署服务

部署 DHCP 服务

安装 DHCP

# yum install dhcp -y

配置 DHCP

# vim /etc/dhcp/dhcpd.conf

修改以下字段

default-lease-time 600;
max-lease-time 7200;
log-facility local7;

subnet 192.168.100.0 netmask 255.255.255.0 {
        option routers                  192.168.100.2;
        option subnet-mask              255.255.255.0;
        option domain-name-servers      192.168.100.2;
        option time-offset              -18000; # Eastern Standard Time
        range dynamic-bootp 192.168.100.60 192.168.100.100;
        default-lease-time 21600;
        max-lease-time 43200;
        next-server 192.168.100.10;
        filename "pxelinux.0";
}

参数解析

参数解析
option routers网关
option subnet-mask子网掩码
option domain-name-serversDNS 服务器
range dynamic-bootp将要部署的区段

启动 DHCP

# systemctl start dhcpd
# systemctl enable dhcpd

部署 TFTP 服务

安装 TFTP

yum install -y tftp-server tftp syslinux-tftpboot

配置 TFTP

# cp /var/ftp/pub/images/pxeboot/initrd.img  /var/lib/tftpboot/
# cp /var/ftp/pub/images/pxeboot/vmlinuz     /var/lib/tftpboot/
# mkdir /var/lib/tftpboot/pxelinux.cfg

写入配置文件

# vim /var/lib/tftpboot/pxelinux.cfg/default
default linux
prompt 1
timeout 60
display boot.msg
label linux
  kernel vmlinuz
  append initrd=initrd.img text ks=ftp://192.168.100.10/ks.cfg

部署 XINETD 服务

安装 XINETD

yum install -y xinetd

配置 XINETD

# vim /etc/xinetd.d/tftp
service tftp
{
        socket_type             = dgram
        protocol                = udp
        wait                    = yes
        user                    = root
        server                  = /usr/sbin/in.tftpd
        server_args             = -s /var/lib/tftpboot
        disable                 = no
        per_source              = 11
        cps                     = 100 2
        flags                   = IPv4
}
小贴士:disable 参数默认是 yes 的,改为 no 即可。

启动 XINETD

# systemctl start xinetd
# systemctl enable xinetd

配置 kickstart

以当前系统安装文件 anaconda-ks.cfg 为模板

# cp ~/anaconda-ks.cfg /var/ftp/ks.cfg

修改配置模板

# vim /var/ftp/ks.cfg
#version=DEVEL
# System authorization information
auth --enableshadow --passalgo=sha512
# Install OS instead of upgrade  
install  
# Use network installation  
url --url=ftp://192.168.100.147/pub
# Use graphical install
graphical
# Run the Setup Agent on first boot
firstboot --enable
# Keyboard layouts
keyboard --vckeymap=us --xlayouts='us'
# System language
lang en_US.UTF-8 --addsupport=zh_CN.UTF-8

# Network information
network  --bootproto=dhcp --device=eno16777736 --onboot=yes --ipv6=auto
network  --hostname=localhost.localdomain

# Root password
# root password rootroot
rootpw --iscrypted $6$7gdZF8XhDef10LyT$2uRiP4qFYaBBTgpggKU/BXKgMDJLWN/BriXXgBwyzkjaz9G9YP/xD08I1OJfgBcPMoURsE5inVIoX.J6aERmR0
# System services
services --disabled="chronyd"
# System timezone
timezone Asia/Shanghai --isUtc --nontp
# System bootloader configuration
bootloader --location=mbr --boot-drive=sda
autopart --type=lvm
# Partition clearing information
clearpart --none --initlabel

%packages
@^minimal
@core

%end

%addon com_redhat_kdump --disable --reserve-mb='auto'

%end

配置文件授权

# chmod +r /var/ftp/ks.cfg

测试服务

以虚拟机为例,创建新机器进行自动化安装测试。

部署服务

部署 DHCP 服务

安装 DHCP

# yum install dhcp -y

配置 DHCP

# vim /etc/dhcp/dhcpd.conf

修改以下字段

default-lease-time 600;
max-lease-time 7200;
log-facility local7;

subnet 192.168.100.0 netmask 255.255.255.0 {
        option routers                  192.168.100.2;
        option subnet-mask              255.255.255.0;
        option domain-name-servers      192.168.100.2;
        option time-offset              -18000; # Eastern Standard Time
        range dynamic-bootp 192.168.100.60 192.168.100.100;
        default-lease-time 21600;
        max-lease-time 43200;
        next-server 192.168.100.10;
        filename "pxelinux.0";
}

参数解析

参数解析
option routers网关
option subnet-mask子网掩码
option domain-name-serversDNS 服务器
range dynamic-bootp将要部署的区段

启动 DHCP

# systemctl start dhcpd
# systemctl enable dhcpd

部署 TFTP 服务

安装 TFTP

yum install -y tftp-server tftp syslinux-tftpboot

配置 TFTP

# cp /var/ftp/pub/images/pxeboot/initrd.img  /var/lib/tftpboot/
# cp /var/ftp/pub/images/pxeboot/vmlinuz     /var/lib/tftpboot/
# mkdir /var/lib/tftpboot/pxelinux.cfg

写入配置文件

# vim /var/lib/tftpboot/pxelinux.cfg/default
default linux
prompt 1
timeout 60
display boot.msg
label linux
  kernel vmlinuz
  append initrd=initrd.img text ks=ftp://192.168.100.10/ks.cfg

部署 XINETD 服务

安装 XINETD

yum install -y xinetd

配置 XINETD

# vim /etc/xinetd.d/tftp
service tftp
{
        socket_type             = dgram
        protocol                = udp
        wait                    = yes
        user                    = root
        server                  = /usr/sbin/in.tftpd
        server_args             = -s /var/lib/tftpboot
        disable                 = no
        per_source              = 11
        cps                     = 100 2
        flags                   = IPv4
}
小贴士:disable 参数默认是 yes 的,改为 no 即可。

启动 XINETD

# systemctl start xinetd
# systemctl enable xinetd

配置 kickstart

以当前系统安装文件 anaconda-ks.cfg 为模板

# cp ~/anaconda-ks.cfg /var/ftp/ks.cfg

修改配置模板

# vim /var/ftp/ks.cfg
#version=DEVEL
# System authorization information
auth --enableshadow --passalgo=sha512
# Install OS instead of upgrade  
install  
# Use network installation  
url --url=ftp://192.168.100.147/pub
# Use graphical install
graphical
# Run the Setup Agent on first boot
firstboot --enable
# Keyboard layouts
keyboard --vckeymap=us --xlayouts='us'
# System language
lang en_US.UTF-8 --addsupport=zh_CN.UTF-8

# Network information
network  --bootproto=dhcp --device=eno16777736 --onboot=yes --ipv6=auto
network  --hostname=localhost.localdomain

# Root password
# root password rootroot
rootpw --iscrypted $6$7gdZF8XhDef10LyT$2uRiP4qFYaBBTgpggKU/BXKgMDJLWN/BriXXgBwyzkjaz9G9YP/xD08I1OJfgBcPMoURsE5inVIoX.J6aERmR0
# System services
services --disabled="chronyd"
# System timezone
timezone Asia/Shanghai --isUtc --nontp
# System bootloader configuration
bootloader --location=mbr --boot-drive=sda
autopart --type=lvm
# Partition clearing information
clearpart --none --initlabel

%packages
@^minimal
@core

%end

%addon com_redhat_kdump --disable --reserve-mb='auto'

%end

配置文件授权

# chmod +r /var/ftp/ks.cfg

测试服务

以虚拟机为例,创建新机器进行自动化安装测试。

流程图

部署服务

部署 DHCP 服务

安装 DHCP

# yum install dhcp -y

配置 DHCP

# vim /etc/dhcp/dhcpd.conf

修改以下字段

default-lease-time 600;
max-lease-time 7200;
log-facility local7;

subnet 192.168.100.0 netmask 255.255.255.0 {
        option routers                  192.168.100.2;
        option subnet-mask              255.255.255.0;
        option domain-name-servers      192.168.100.2;
        option time-offset              -18000; # Eastern Standard Time
        range dynamic-bootp 192.168.100.60 192.168.100.100;
        default-lease-time 21600;
        max-lease-time 43200;
        next-server 192.168.100.10;
        filename "pxelinux.0";
}

参数解析

参数解析
option routers网关
option subnet-mask子网掩码
option domain-name-serversDNS 服务器
range dynamic-bootp将要部署的区段

启动 DHCP

# systemctl start dhcpd
# systemctl enable dhcpd

部署 TFTP 服务

安装 TFTP

yum install -y tftp-server tftp syslinux-tftpboot

配置 TFTP

# cp /var/ftp/pub/images/pxeboot/initrd.img  /var/lib/tftpboot/
# cp /var/ftp/pub/images/pxeboot/vmlinuz     /var/lib/tftpboot/
# mkdir /var/lib/tftpboot/pxelinux.cfg

写入配置文件

# vim /var/lib/tftpboot/pxelinux.cfg/default
default linux
prompt 1
timeout 60
display boot.msg
label linux
  kernel vmlinuz
  append initrd=initrd.img text ks=ftp://192.168.100.10/ks.cfg

部署 XINETD 服务

安装 XINETD

yum install -y xinetd

配置 XINETD

# vim /etc/xinetd.d/tftp
service tftp
{
        socket_type             = dgram
        protocol                = udp
        wait                    = yes
        user                    = root
        server                  = /usr/sbin/in.tftpd
        server_args             = -s /var/lib/tftpboot
        disable                 = no
        per_source              = 11
        cps                     = 100 2
        flags                   = IPv4
}
小贴士:disable 参数默认是 yes 的,改为 no 即可。

启动 XINETD

# systemctl start xinetd
# systemctl enable xinetd

配置 kickstart

以当前系统安装文件 anaconda-ks.cfg 为模板

# cp ~/anaconda-ks.cfg /var/ftp/ks.cfg

修改配置模板

# vim /var/ftp/ks.cfg
#version=DEVEL
# System authorization information
auth --enableshadow --passalgo=sha512
# Install OS instead of upgrade  
install  
# Use network installation  
url --url=ftp://192.168.100.147/pub
# Use graphical install
graphical
# Run the Setup Agent on first boot
firstboot --enable
# Keyboard layouts
keyboard --vckeymap=us --xlayouts='us'
# System language
lang en_US.UTF-8 --addsupport=zh_CN.UTF-8

# Network information
network  --bootproto=dhcp --device=eno16777736 --onboot=yes --ipv6=auto
network  --hostname=localhost.localdomain

# Root password
# root password rootroot
rootpw --iscrypted $6$7gdZF8XhDef10LyT$2uRiP4qFYaBBTgpggKU/BXKgMDJLWN/BriXXgBwyzkjaz9G9YP/xD08I1OJfgBcPMoURsE5inVIoX.J6aERmR0
# System services
services --disabled="chronyd"
# System timezone
timezone Asia/Shanghai --isUtc --nontp
# System bootloader configuration
bootloader --location=mbr --boot-drive=sda
autopart --type=lvm
# Partition clearing information
clearpart --none --initlabel

%packages
@^minimal
@core

%end

%addon com_redhat_kdump --disable --reserve-mb='auto'

%end

配置文件授权

# chmod +r /var/ftp/ks.cfg

测试服务

以虚拟机为例,创建新机器进行自动化安装测试。

网络引导

自动选择

附录

参考链接

本文撰写于一年前,如出现图片失效或有任何问题,请在下方留言。博主看到后将及时修正,谢谢!
禁用 / 当前已拒绝评论,仅可查看「历史评论」。