在Centos 上创建自动部署服务器的脚本

安装条件:

CentOS 系统

Centos 6.3 的 ISO 路径: /install/iso/CentOS-6.3-x86_64-bin-DVD1.iso。修改ISO为Centos 6.x版本的iso后就可以适用于6.x的各版本。就是下面这一行:

mount -o loop /install/iso/CentOS-6.3-x86_64-bin-DVD1.iso /media/cdrom

IP地址范围是172.17.80.0-172.17.95.254,掩码是255.255.240.0。部署服务器IP为172.17.80.250。可以根据需要修改相应的IP地址和DHCP配置。


install.sh

#!/bin/sh
echo "
Start Install Steven xie(mxcz.net) Install Server
		Please First Copy CentOS-6.3-x86_64-bin-DVD1.iso---/install/iso/CentOS-6.3-x86_64-bin-DVD1.iso
"
read -n 1 -p "Press any key to continue..."
setenforce 0
sed -i "s/SELINUX=enforcing.*$/SELINUX=disabled/" /etc/selinux/config
service iptables stop
chkconfig iptables off

echo "
DEVICE=eth0
BOOTPROTO=static
ONBOOT=yes
TYPE=Ethernet
IPADDR=172.17.80.250
NETMASK=255.255.240.0
GATEWAY=172.17.80.250
">/etc/sysconfig/network-scripts/ifcfg-eth0

service network restart

mount -o loop /install/iso/CentOS-6.3-x86_64-bin-DVD1.iso /media/cdrom
yum --disablerepo=\* --enablerepo=c6-media -y install tftp-server system-config-kickstart dhcp syslinux httpd

#vim /etc/xinetd.d/tftp
echo "
service tftp
{
	disable	= no
	socket_type		= dgram
	protocol		= udp
	wait			= yes
	user			= root
	server			= /usr/sbin/in.tftpd
	server_args		= -s /install/tftpboot
	per_source		= 11
	cps			= 100 2
	flags			= IPv4
}
">/etc/xinetd.d/tftp

#DHCP
echo "
ddns-update-style none;

#allow booting;
#allow bootp;

option domain-name \"mxcz.net\";
option subnet-mask 255.255.240.0;
option broadcast-address 172.17.95.255;
option domain-name-servers 172.17.80.250;
option routers 172.17.80.250;

next-server 172.17.80.250;
filename \"pxelinux.0\";

subnet 172.17.80.0 netmask 255.255.240.0 {
	range dynamic-bootp 172.17.80.1 172.17.95.254;
}
">/etc/dhcp/dhcpd.conf
/etc/init.d/dhcpd restart

mkdir -p /install/tftpboot
/etc/init.d/xinetd restart
chkconfig xinetd on
chkconfig tftp on
netstat -tulnp | grep xinetd

cp /usr/share/syslinux/menu.c32  /usr/share/syslinux/vesamenu.c32 /usr/share/syslinux/pxelinux.0 /install/tftpboot/
mkdir /install/tftpboot/pxelinux.cfg

mkdir -p /install/tftpboot/kernel/centos6.3
cp /media/cdrom/isolinux/vmlinuz /install/tftpboot/kernel/centos6.3
cp /media/cdrom/isolinux/initrd.img /install/tftpboot/kernel/centos6.3
cp /media/cdrom/isolinux/isolinux.cfg /install/tftpboot/pxelinux.cfg/

#vim /install/tftpboot/pxelinux.cfg/default
echo "
UI vesamenu.c32
TIMEOUT 0
MENU TITLE Welcome to mxcz PXE Server System
menu background ./back800.jpg
menu resolution 800 600
LABEL LOCAL
	menu hide
	MENU LABEL Boot from local drive
	MENU DEFAULT
	localboot 0

LABEL NETWORK
	menu hide
	MENU LABEL Boot from Pxe Server for Install CentOS 6.3
	kernel ./kernel/centos6.3/vmlinuz
	append initrd=./kernel/centos6.3/initrd.img
LABEL kickstart
	MENU LABEL AUTO Install CentOS 6.3
	kernel ./kernel/centos6.3/vmlinuz
	append initrd=./kernel/centos6.3/initrd.img ks=http://172.17.80.250/pc.ks ksdevice=eth0
">/install/tftpboot/pxelinux.cfg/default

#yum install httpd
/etc/init.d/httpd start
chkconfig httpd on
mkdir -p /var/www/html/install/centos6.3
#vim /etc/fstab
echo "
/install/iso/CentOS-6.3-x86_64-bin-DVD1.iso /var/www/html/install/centos6.3 iso9660 defaults,loop 0 0
">>/etc/fstab


echo "
# Kickstart file automatically generated by anaconda.

#version=DEVEL
install
text
url --url http://172.17.80.250/install/centos6.3
#url --url ftp://10.0.0.1/install/centos6.3
#nfs --server=10.0.0.1 --dir=/install/nfs/centos6.3
lang zh_CN.UTF-8
keyboard us
#network --onboot yes --device eth0 --bootproto static --ip 172.l7.80.1 --netmask 255.255.240.0 --gateway 172.17.95.254
network --onboot yes --device eth0 --bootproto dhcp --noipv6
network --onboot no --device eth1 --bootproto dhcp --noipv6
network --onboot no --device eth2 --bootproto dhcp --noipv6
network --onboot no --device eth3 --bootproto dhcp --noipv6
rootpw	password
firewall  --disabled
authconfig --enableshadow --passalgo=sha512
selinux  --permissive
timezone Asia/Shanghai

zerombr yes
bootloader --location=mbr --driveorder=sda --append=\"crashkernel=auto rhgb quiet\"

clearpart --all
part /boot --fstype=ext4 --asprimary --size=200
part / --fstype=ext4 --size=51200
part swap --size=32768
part /export --fstype=ext4 --grow --size=1

repo --name=\"mxcz\" --baseurl=http://172.17.80.250/install/centos6.3 --cost=100
#repo --name="CentOS" --baseurl=http://172.17.58.25/centos6.3 --cost=100
#repo --name="salt" --baseurl=http://172.17.58.25/salt --cost=1000

%packages
@chinese-support
@core
@server-policy
@salt
@salt-minion
%end

%post
#!/bin/sh
var=\`ifconfig eth0 |grep -i bcast |awk '{print \$2}'\` 
NUM=\`echo ${var##*.}\` 
sed -i \"s/HOSTNAME.*$/HOSTNAME=wy-80$NUM/\" /etc/sysconfig/network 
STATICIP=\`echo $var|awk -F \":\" '{print \$2}'\` 
sed -i \"s/BOOTPROTO.*$/BOOTPROTO=static/\" /etc/sysconfig/network-scripts/ifcfg-eth0 
echo \"IPADDR=$STATICIP\" >> /etc/sysconfig/network-scripts/ifcfg-eth0 
echo \"NETMASK=255.255.240.0\" >> /etc/sysconfig/network-scripts/ifcfg-eth0
echo \"GATEWAY=172.17.95.254\" >> /etc/sysconfig/network-scripts/ifcfg-eth0

#yum install salt --enablerepo=salt -y
#yum install salt-minion --enablerepo=salt -y
#echo \"master:172.17.58.25\">>/etc/salt/minion
">/var/www/html/pc.ks
echo "
install server is ok,please reboot

					mxcz.net
"


你可能感兴趣的:(shell,script,kickstart)