下面是自己制作的PXE安装Centos6的环境已经配置脚本
#!/bin/bash ######################################################################### # File Name: PXElinu_Centos6.sh # Author: LookBack # Email: [email protected] # Licence: GNU General Public Licence # Created Time: Fri 01 Aug 2014 12:49:44 AM CST ######################################################################### ftpurl="ftp://lookback:[email protected]/centos/6/x86_64/base/" #ftp://lookback:[email protected]/centos/6/ks.cfg checkIP() { echo "$1" | grep -oE "[0-9]{1,3}(\.[0-9]{1,3}){3}" } checkNetIP() { echo "$1" |grep -E "[0-9]{1,3}(\.[0-9]{1,3}){3}/[0-9]{1,2}" | head -1 } confdhcp() { for i in dhcp tftp-server tftp syslinux ipcalc; do yum -y install $i;done for i in 1 2; do DnsIP[$i]=$($(checkIP $DnsIP) | head -$i) [ "$i" = "2" ] && DnsIP[$i]=$($(checkIP $DnsIP) | head -$i | tail -1) done for i in 1 2; do NetIP[$i]=$(ipcalc -nm $(checkNetIP $netIP)| awk -F= "NR==$i{print$2}"); done for i in 1 2; do dhcpIP[$i]=$(checkIP $dhcpIP | awk 'NR==$i');done cat > /etc/dhcp/dhcpd.conf << EOF option domain-name "mondeolove.blog.51cto.com"; option domain-name-servers ${DnsIP[1]},${DnsIP[2]}; default-lease-time 86440; max-lease-time 100000; log-facility local7; subnet ${NetIP[2]} netmask ${NetIP[1]} { range ${dhcpIP[1]} ${dhcpIP[2]}; option routers $(checkIP $routeIP); next-server $(checkIP $pxeIP); filename "pxelinux.0"; } EOF } startservice() { service dhcpd start && chkconfig tftp on [ -n "$(ss -unl | grep ':69\b')" ] && echo "TFTP服务运行正常" [ -n "$(ss -unl | grep ':67\b')" ] && echo "DHCP服务运行正常" } cpFile() { tftpBootDir=$(awk -F'[ =]+' '/-s/{print$3}' /etc/xinetd.d/tftp) defautlFile="${tftpBootDir}/pxelinux.cfg/default" [ ! -d "${tftpBootDir}/pxelinux.cfg" ] && mkdir -p ${tftpBootDir}/pxelinux.cfg/ for I in vmlinuz initrd.img; do wget -O ${tftpBootDir}/$I ${ftpurl}images/pxeboot/$I;done for II in splash.jpg vesamenu.c32 boot.msg isolinux.cfg; do [ "$II" == "isolinux.cfg" ] && wget -O $defautlFile ${ftpurl}isolinux/$II wget -O ${tftpBootDir}/$II ${ftpurl}isolinux/$II done cp $(rpm -ql syslinux | grep '\bpxelinux.0') ${tftpBootDir}/ sed "$(($(grep -n "menu default" $defautlFile | awk -F: '{print$1}')-4))a\\\\nlabel PXElinux\n menu label ^Install or upgrade an existing system\n menu default\n kernel vmlinuz\n append initrd=initrd.img ks=$kscfgFile" $defautlFile for i in $(echo $(grep -n "menu default" $defautlFile | awk -F: 'NR>1{print$1}'));do sed -i ${i}d $defautlFile;done } dhcpInfo() { read -p "请输入DNS服务器地址,多个请用,号隔开: " DnsIP read -p "请输入一个网络地址和对应的掩码(如:172.16.0.0/16): " netIP read -p "请输入一个DHCP IP的范围(如:172.16.0.100-172.16.0.200): " dhcpIP read -p "请输入你的PXE服务器IP(172.16.1.100): " pxeIP read -p "请输入路由IP地址: " routeIP read -p "请输入ks.cfg文件地址(可是file://|http://|https://|ftp://): " kscfgFile } dhcpInfo confdhcp startservice cpFile
yum -y install dhcp tftp-server tftp
cat > /etc/dhcp/dhcpd.conf << EOF # option domain-name "dwhd.org"; #设置DHCP的DNS option domain-name-servers 172.16.0.1,114.114.114.114; #设置DHCP 分配出去的IP超时时间 单位秒 default-lease-time 86440; #设置DHCPlease-time时间 单位秒 max-lease-time 100000; log-facility local7; #设置DHCP的 IP 和掩码 subnet 192.168.1.0 netmask 255.255.255.0 { #设置DHCP对外分配IP段 range 192.168.1.50 192.168.1.70; #设置路由IP option routers 192.168.1.1; #下面的4行是根据mac分配指定IP host Centos7 { hardware ethernet 00:0c:29:ba:3c:76; fixed-address 192.168.1.65; } #如果是PXE网卡来获取到了IP 告诉网卡下一步联系的服务器IP 192.168.1.120 next-server 192.168.1.120; #PXE网卡联系了服务器之后告诉它应该去获取文件pxelinux.0 filename "pxelinux.0"; } EOF
[root@localhost pxelinux.cfg]# service dhcpd configtest Syntax: OK #检测dhcp配置,如果出现Syntax: OK则说明配置OK了 #启动DHCP服务 service dhcpd start
chkconfig tftp on[/bash] [bash highlight="9,10"][root@localhost ~]# netstat -unl Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State udp 0 0 0.0.0.0:1005 0.0.0.0:* udp 0 0 0.0.0.0:111 0.0.0.0:* udp 0 0 0.0.0.0:631 0.0.0.0:* udp 0 0 0.0.0.0:776 0.0.0.0:* udp 0 0 0.0.0.0:48819 0.0.0.0:* udp 0 0 0.0.0.0:67 0.0.0.0:* udp 0 0 0.0.0.0:69 0.0.0.0:* udp 0 0 :::1005 :::* udp 0 0 :::111 :::* udp 0 0 :::42066 :::* ##看看 67 69端口是否被监听了,如果是的就说明好了
开始为PXE启动做准备,由于我在当前Windows物理机上架设了FTP服务,所以我这里就直接用FTP服务器做源了
ftpurl="ftp://lookback:[email protected]/centos/6/x86_64/base/" tftpdir="/var/lib/tftpboot/" for I in vmlinuz initrd.img; do wget -O ${tftpdir}$I ${ftpurl}images/pxeboot/$I;done for II in splash.jpg vesamenu.c32 boot.msg isolinux.cfg; do [ "$II" == "isolinux.cfg" ] && wget -O ${tftpdir}/pxelinux.cfg/default ${ftpurl}isolinux/$II wget -O ${tftpdir}$II ${ftpurl}isolinux/$II done #安装syslinux yum -y install syslinux #复制pxelinux.0到TFTP目录下 cp $(rpm -ql syslinux | grep '\bpxelinux.0') /var/lib/tftpboot/ #最后核对下文件 tree /var/lib/tftpboot/ #结果和下图一样就对了
下面是我自己制作的ks.cfg,可根据自己的需求做改动 root密码是centos6,这个文件存放的位置需要在系统安装的时候被获取到,我这里存放在我物理机上的FTP下
#platform=x86, AMD64, or Intel EM64T #version=DEVEL # Firewall configuration firewall --disabled # Install OS instead of upgrade install # Use network installation url --url="ftp://lookback:[email protected]/centos/6/x86_64/base" repo --name="CentOS" --baseurl=ftp://lookback:[email protected]/centos/6/x86_64/base #repo --name="Fedora EPEL" --baseurl=ftp://lookback:[email protected]/fedora-epel/6/x86_64/ --cost=1000 # Root password rootpw --iscrypted $6$W6.dzk.2$fqenuNv62I4SF64gAie0OTQK5WdjUOxGbDPkIq/z/xlG42oUh7khkVhfWYDI8U0FmsIofmQtGSq9ruWuoHvy30 #root密码为centos6 # System authorization information auth --useshadow --passalgo=sha512 # Use graphical install graphical firstboot --disable # System keyboard keyboard us # System language lang en_US # SELinux configuration selinux --disabled # Installation logging level logging --level=info # Reboot after installation reboot # System timezone timezone Asia/Shanghai # Network information network --bootproto=dhcp --device=eth0 --onboot=on # System bootloader configuration bootloader --append="crashkernel=auto crashkernel=auto rhgb rhgb quiet quiet" --location=mbr --driveorder="sda" # Clear the Master Boot Record zerombr # Partition clearing information clearpart --all --initlabel # Disk partitioning information part /boot --fstype="ext4" --size=200 part pv.001 --size=61440 volgroup vg0 --pesize=8192 pv.001 logvol / --fstype=ext4 --vgname=vg0 --size=20480 --name=root logvol /var --fstype=ext4 --vgname=vg0 --size=20480 --name=var logvol swap --name=swap --vgname=vg0 --size=2048 logvol /usr --fstype=ext4 --vgname=vg0 --size=10240 --name=usr %post echo -e 'TEST Centos6\nhttp://www.000.org\n' >> /etc/issue sed -i '1,$s@id:[0-9]:initdefault:@id:3:initdefault:@g' /etc/inittab [ ! -d /root/.ssh ] && mkdir /root/.ssh && chmod og=--- /root/.ssh #下面是我自己为了方便自己使用对自动安装的系统做了免密码登录 cat >> /root/.ssh/authorized_keys << EOF ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDfhGitgDKxQidZXLHS3DFalTvmzTwQ4UH3etQJUE7/iPNwLs6ZDzbR5pqTPfK1YdhUTuJOoNQZ71Fr9qwLtF6aLdgO+ybmK/8sNrMeTvGmKyr4YQ5k02vVIbBnQIEr08eXpV1y206CMWQ7FiiMlFvaeFZhj8trchiffhAUJdZOjl/BikzDJcYdp/sRXrFA4G21yXU0ffOn9aAAvqOqRBRoDhpLnSWaovGjd419Cy/pdhu4Vuispz1x834l975fLv4PIh+3nW9WMbhrmIXzWoTsxnc8OUbT4FnRdA33G8T3JXQc1n1UjX7H4BGfzKA6eax574rspAk51cslaydby2vX== [email protected] EOF # Enable funcd sed -i 's@certmaster =.*@certmaster = 172.16.0.1@g' /etc/certmaster/minion.conf /sbin/chkconfig funcd off # Set the hostname ClientName=`ifconfig eth0 | awk '/inet addr:/{print $2}' | awk -F. '{print $NF}'` sed -i "s@HOSTNAME=.*@HOSTNAME=client$ClientName.dwhd.org@g" /etc/sysconfig/networks # set puppet agent sed -i '/\[main\]/a server=server.dwhd.org' /etc/puppet/puppet.conf /sbin/chkconfig puppet off # set hosts echo '172.16.0.1 server.dwhd.org server' >> /etc/hosts # yum repo %end %packages @base @basic-desktop @chinese-support @client-mgmt-tools @core @desktop-platform @development @fonts @general-desktop @graphical-admin-tools @legacy-x @network-file-system-client @perl-runtime @remote-desktop-clients @server-platform-devel @x11 %end
现在来修改下PXE启动后引导菜单
vi /var/lib/tftpboot/pxelinux.cfg/default
default vesamenu.c32 #prompt 1 timeout 60 display boot.msg menu background splash.jpg menu title Welcome to CentOS 6.5! This is test PXE Auto Install menu color border 0 #ffffffff #00000000 menu color sel 7 #ffffffff #ff000000 menu color title 0 #ffffffff #00000000 menu color tabmsg 0 #ffffffff #00000000 menu color unsel 0 #ffffffff #00000000 menu color hotsel 0 #ff000000 #ffffffff menu color hotkey 7 #ffffffff #ff000000 menu color scrollbar 0 #ffffffff #00000000 label pxelinux menu label ^Auto Install or upgrade an existing system menu default kernel vmlinuz append initrd=initrd.img ks=ftp://lookback:[email protected]/centos/6/ks.cfg label linux menu label ^Install or upgrade an existing system kernel vmlinuz append initrd=initrd.img label vesa menu label Install system with ^basic video driver kernel vmlinuz append initrd=initrd.img xdriver=vesa nomodeset label rescue menu label ^Rescue installed system kernel vmlinuz append initrd=initrd.img rescue label local menu label Boot from ^local drive localboot 0xffff label memtest86 menu label ^Memory test kernel memtest append -