shell脚本——pxe服务

我们大家对pxe应该都有一定的了解,下面看一下官方的解释:

PXE(preboot execute environment,预启动执行环境)是由Intel公司开发的最新技术,工作于Client/Server的网络模式,支持工作站通过网络从远端服务器下载映像,并由此支持通过网络启动操作系统,在启动过程中,终端要求服务器分配IP地址,再用TFTP(trivial file transfer protocol)或MTFTP(multicast trivial file transfer protocol)协议下载一个启动软件包到本机内存中执行,由这个启动软件包完成终端(客户端)基本软件设置,从而引导预先安装在服务器中的端操作系统。PXE可以引导多种操作系统

我们平时可以用一台虚拟机来实现pxe的整个功能,然后把这台机器作为服务器,其他客服端通过访问我们,实现自动安装linux操作系统(我安装的是CentOS 7系统

#!/bin/bash

#安装信息直接扔到垃圾桶,错误信息保留到pxe.error
yum install -y dhcp vsftpd tftp-server syslinux >/dev/null 2>/opt/pxe.error

#修改iP地址
ip=192.168.100.100
a=/etc/sysconfig/network-scripts/ifcfg-ens33
[ ! -e "$a.bak" ] && cp $a $a.bak
sed -i -e 's/dhcp/static/' $a

echo "
IPADDR=$ip
NETMASK=255.255.255.0
GATNWEY=192.168.100.1
" >>$a

systemctl restart network

#配置dhcp配置文件
b=/etc/dhcp/dhcpd.conf

/usr/bin/expect <<-EOF
spawn cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example $b
expect {
        "*是否覆盖*"
        {send "yes\r"}
}
expect eof
EOF

[ ! -e "$b.bak" ] && cp $b $b.bak
echo  " subnet 192.168.100.0 netmask 255.255.255.0 {
		range 192.168.100.10 192.168.100.20;
		option routers 192.168.100.100;
		option domain-name-servers 114.114.114.114;
		next-server 192.168.100.100;
		filename \"pxelinux.0\" ;
}" >> $b

#配置tftp配置文件
sed -i '/disable/s/yes/no/' /etc/xinetd.d/tftp

cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/

#配置ftp
mkdir /var/ftp/centos7
mount /dev/cdrom /var/ftp/centos7/
cp /var/ftp/centos7/images/pxeboot/vmlinuz /var/lib/tftpboot/
cp /var/ftp/centos7/images/pxeboot/initrd.img /var/lib/tftpboot/

#配置编辑default启动菜单(根据安装的系统可以更改配置文件)
mkdir /var/lib/tftpboot/pxelinux.cfg
echo "
default auto
prompt 1

label auto
        kernel vmlinuz
        append initrd=initrd.img method=ftp://192.168.100.100/centos7 ks=ftp://192.168.100.100/ks.cfg

label linux text
        kernel vmlinuz
        append text initrd=initrd.img method=ftp://192.168.100.100/centos7

label linux rescue
        kernel vmlinuz
        append rescue initrd=initrd.img method=ftp://192.168.100.100/centos7
" > /var/lib/tftpboot/pxelinux.cfg/default

echo "
#platform=x86, AMD64, 或 Intel EM64T
#version=DEVEL
# Install OS instead of upgrade
install
# Keyboard layouts
keyboard 'us'
# Root password
rootpw --plaintext 123
# System language
lang zh_CN
# Firewall configuration
firewall --disabled
# System authorization information
auth  --useshadow  --passalgo=sha512
# Use CDROM installation media
cdrom
# Use graphical install
graphical
firstboot --disable
# SELinux configuration
selinux --disabled
# Network information
network  --bootproto=dhcp --device=ens32
# Reboot after installation
reboot
# System timezone
timezone Asia/Shanghai
# System bootloader configuration
bootloader --location=mbr
# Partition clearing information
clearpart --all
# Disk partitioning information
part /boot --fstype="xfs" --size=500
part /home --fstype="xfs" --size=2048
part swap --fstype="swap" --size=2048
part / --fstype="xfs" --grow --size=1

%packages
@^gnome-desktop-environment
@base
@core
@desktop-debugging
@dial-up
@directory-client
@fonts
@gnome-desktop
@guest-agents
@guest-desktop-agents
@input-methods
@internet-browser
@java-platform
@multimedia
@network-file-system-client
@networkmanager-submodules
@print-client
@x11
chrony
kexec-tools

%end " > /var/ftp/ks.cfg

#关闭防火墙,启动服务
setenforce 0
systemctl stop firewalld
systemctl start tftp
systemctl start dhcpd
systemctl start vsftpd
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值