您的位置:首页 > 理论基础 > 计算机网络

第一次修改:pxe无人值守网络安装redhat linux系统脚本

2012-03-22 23:55 525 查看
.....................................................................................
pxe无人值守网络安装redhat linux系统脚本
.....................................................................................
要实现上面的功能,我们需要配置DHCP服务+NFS服务+TFTP服务,具体原理网上很多 ,在这我只提供一个脚本和我在实现中遇到的故障
在执行这个脚本的时候要先配置好yum源,自己主机在24掩码的网段,同时也需要把光盘镜像挂载到/mnt/cdrom下。
#!/bin/bash
myip=`ifconfig | awk -F: '/inet addr/{print $2}' | grep -v "127.0.0.1" | cut -d' ' -f1 | cut -d'.' -f4`
mynet=`ifconfig | awk -F: '/inet addr/{print $2}'| awk '{if(NR==1) print $1 }'|sed 's/\([0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\).*/\1/g'`
yum clean all &>/dev/null
yum list all &>/dev/null
if [ $? -eq 0 ];then
echo -e "\033[32m yum server is ok...\033[0m"
else
echo -e "\033[31m yum server is not working...\033[0m"
exit 1
fi
getenforce | grep Enforcing
if [ $? -eq 0 ];then
sed -i 's/SELINUX=enforcing/SELINUX=permissive/' /etc/selinux/config
fi
function setdhcp {
yum -y install dhcp
mv /etc/dhcpd.conf /etc/dhcpd.conf.bak
cat >> /etc/dhcpd.conf << 'EOF'
ddns-update-style interim;
ignore client-updates;
allow booting;
allow bootp;
next-server $mynet.$myip;
filename "pxelinux.0";
subnet $mynet.0 netmask 255.255.255.0 {
option routers $mynet.$myip;
option subnet-mask 255.255.255.0;
option domain-name-servers $mynet.$myip;
option routers $mynet.$myip;
option subnet-mask 255.255.255.0;
option domain-name "domain.org";
option domain-name-servers $mynet.$myip;
option time-offset -18000; # Eastern Standard Time
range $mynet.200 $mynet.220;
default-lease-time 21600;
max-lease-time 43200;
}
EOF
service dhcpd start
[ $? -eq 0 ] && echo -e "\033[32mthe server dhcp start is ok......\033[0m" || exit 10
chkconfig dhcpd on
sync
}
function settftp {
yum -y install tftp-server
sed -i "s@disable[[:space:]]*= yes@disable = no@g" /etc/xinetd.d/tftp
service xinetd restart
mkdir /tftpboot
}
function setpxe {
cp /usr/lib/syslinux/pxelinux.0 /tftpboot
cp /mnt/cdrom/images/pxeboot/{vmlinuz,initrd.img} /tftpboot ### 挂在镜像到/mnt/cdrom 下的作用就在这。
cp /mnt/cdrom/isolinux/*.msg /tftpboot/
mkdir /tftpboot/pxelinux.cfg
#cp /mnt/cdrom/isolinux/isolinux.cfg /tftpboot/pxelinux.cfg/default####也可以直接cp,然后用sed修改部分参数
cat >> /tftpboot/pxelinux.cfg/default << EOF
default linux
prompt 1
timeout 60
display boot.msg
F1 boot.msg
F2 options.msg
F3 general.msg
F4 param.msg
F5 rescue.msg
label linux
kernel vmlinuz
append ks=nfs:$mynet.$myip:/iso/ks.cfg initrd=initrd.img
label text
kernel vmlinuz
append initrd=initrd.img text
label ks
kernel vmlinuz
append ks initrd=initrd.img
label local
localboot 1
label memtest86
kernel memtest
append -
EOF
chmod 755 /tftpboot/*
mkdir -pv /iso/install
cp /root/redhat5.4.iso /iso/install #################需要镜像 我做实验的时候把镜像放到根目录下了,你根据实际情况修改
echo "/iso *(ro,sync)" >> /etc/exports
service nfs restart
chkconfig nfs on
}

function setks {
#cp /root/anaconda-ks.cfg /iso/ks.cfg####也可以直接cp,然后用sed修改部分参数,感觉sed修改这些参数很麻烦所以就cat一个文件
if [ ! -e /iso/ks.cfg ];then
cat >> /iso/ks.cfg << EOF
text
nfs --server=$mynet.$myip --dir=/iso/install
key --skip
lang en_US.UTF-8
keyboard us
xconfig --startxonboot --resolution 1024x768 --depth 16
network --device eth0 --bootproto dhcp
rootpw --iscrypted $1$.Nwkli1d$Yr7n8.Oa0w9gl/K3Xe21/1 #########需要换成你自己设置的密码
firewall --disabled
authconfig --useshadow --enablemd5
selinux --enforcing
timezone --utc Asia/Shanghai
bootloader --location=mbr --driveorder=sda --append="rhgb quiet"
# The following is the partition information you requested
# Note that any partitions you deleted are not expressed
# here so unless you clear all partitions first, this is
# not guaranteed to work
clearpart --all --initlabel
part /boot --fstype ext3 --size=100
part pv.100000 --size=40960 #########自己设置安装硬盘大小
part swap --size=512
volgroup vol0 --pesize=32768 pv.100000
logvol / --fstype ext3 --name=root --vgname=vol0 --size=29984
logvol /home --fstype ext3 --name=home --vgname=vol0 --size=1984
%packages
@GNOME Desktop Environment
@X Window System
@Printing Support
@Development Tools
elinks
openssh
openssh-server
openssh-clients
vim-enhanced
samba-common
samba-client
gimp
gimp-data-extras
enscript
libcap
ntp
coreutils
elfutils
elfutils-libelf
gimp-print-plugin
gnome-icon-theme
gstreamer
gstreamer-tools
libgnomeprint22
libgnomeprintui22
libgsf
libIDL
libraw1394
nautilus-cd-burner
openmotif
pyorbit
startup-notification
ttmkfdir
firefox
mutt
dialog
EOF
chmod +rx /iso/ks.cfg ######################修改权限
else
echo "the file exit......................."
fi
}
setdhcp
settftp
setpxe
setks
.....................................................................................................................
说明 这个脚本有局限性,我随后有时间会加以修改,需要注意和容易犯错的地方我已经用##号备注,使用的时候可以根据自己的实际情况修改。如果是虚拟机安装的话,一定要注意安装的版本,不然会报错。
希望大家给出更好的建议。本文出自 “面对自己” 博客,请务必保留此出处http://angus717.blog.51cto.com/1593644/814300
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: