#!/bin/bash
#This shell script is used to install disklesImage automaticly.
#This script uses ramdisk as a root partition and uses CPIO based initramfs.
#we want to set DHCP TFTP PXE server
TFTPDIR=/tftpboot #TFTP root directory
DHCPDFILE=/etc/dhcpd.conf #dhcp server configuration file
TFTPFILE=/etc/xinetd.d/tftp #TFTP server configuraton file
CONFDIR=`dirname $0` #the directory where the script is
USAGE()
{
cat <<\EOF
The usage of this script : diskless-install.sh [install|start|stop|restart]
diskless-install.sh install ------to install the disklessImage
diskless-install.sh start ------to start the service
diskless-install.sh stop ------to stop the service
diskless-install.sh restart ------to stop then start the service
EOF
}
case "$1" in
install)
#set DHCP server
#check out if DHCP rpm has been installed
if [ ! -e $DHCPDFILE ]
then
echo "NOW,install dhcpd server with yum......"
yum -y install dhcp
chkconfig --level 345 dhcpd on
service dhcpd start
else
echo "check dhcpd OK ."
fi
#configure DHCP service
echo "configuring dhcpd service......"
cat > /etc/dhcpd.conf << END
ddns-update-style interim;
ignore client-updates;
allow booting;
allow bootp;
next-server 192.168.56.101;
subnet 192.168.56.0 netmask 255.255.255.0 {
option routers 192.168.56.101;
range 192.168.56.1 192.168.56.254;
default-lease-time 1200;
max-lease-time 1800;
option subnet-mask 255.255.255.0;
}
host c1 {
server-name "server";
hardware ethernet 00:30:23:32:45:56:56;
fixed-address 172.18.199.102;
filename "/tftpboot/pxelinux.0";
}
END
service dhcpd restart
# check the dhcp service is started or not
ps -e | grep dhcpd >/dev/null
if [ $? == 0 ]
then
echo "The DHCPD service is OK ."
echo "dhcpd configure done."
else
echo "The DHCPD service is not started,please check the system log and fix it."
exit 113
fi
#set the TFTP service
#check the TFTP server has been installed or not
if [ -e $TFTPFILE ]
then
echo " The tftp server has been installed."
else
echo "NOW,install TFTP server with yum...."
yum install tftp
chkconfig --level 345 xinetd on
srvice xinetd restart
fi
#setup the tftp service
echo "configuring TFTP service......"
cat > /etc/xinetd.d/tftp << END
service tftp
{
disable = no
socket_type = dgram
protocol = udp
wait = yes
user = root
server = /usr/sbin/in.tftpd
server_args = -v -s /tftpboot
per_source = 11
cps = 100 2
flags = IPv4
}
END
service xinetd restart
sleep 5
# check the TFTP service is started or not
netstat -antup | grep 69 >/dev/null
if [ $? == 0 ]
then
echo "The TFTP service is OK ."
echo "TFTP configure done."
else
echo "The TFTP service is not started,please check the system log and fix it."
exit 114
fi
#setup local yum
mount /dev/cdrom /media >/dev/null
mv /etc/yum.repos.d/* /tmp/yum.back
cat > /etc/yum.repos.d/CentOS-Base.repo < [ISO]
name=ISO
baseurl=file:///media
gpgcheck=0
EOF
yum clean all
mkdir -p $TFTPDIR/installroot
yum -y --installroot=/tftpboot/installroot install basesystem filesystem bash nfs-utils-lib nfs-utils-lib-devel yum MAKEDEV SysVinit audit-libs binutils bzip2-libs centos-release centos-release-notes chkconfig coreutils cpio cracklib cracklib-dicts crontabs db4 device-mapper device-mapper-event device-mapper-multipath dmraid dmraid-events e2fsprogs e2fsprogs-libs elfutils-libelf ethtool expat findutils gawk gdbm glib2 grep gzip hmaccalc info initscripts iproute iputils keyutils-libs kpartx krb5-libs less libacl libattr libcap libselinux libsepol libstdc++ libsysfs libxml2 logrotate lvm2 m2crypto mcstrans mingetty mkinitrd module-init-tools nash ncurses net-tools nspr nss openssl pam pcre popt procps psmisc python python-elementtree python-iniparse python-sqlite pyth-urlgrabber readline rpm rpm-libs rpm-python rsyslog sed sgpio shadow-utils sqlite tar udev util-linux yum-fastestmirror yum-metadata-parser zlib bind-libs openssl perl tar sgpio mkinitrd net-tools dhcp tftp
cp /etc/sysconfig/network $TFTPDIR/installroot/etc/sysconfig
cat > $TFTPDIR/installroot/etc/sysconfig/networt-scripts/ifcfg-eth0 < DEVICE=eth0
BOOTPROTO=dhcp
ONBOOT=yes
EOF
cat > $TFTPDIR/installroot/etc/fstab < /dev/root / tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
EOF
cd $TFTPDIR/installroot
ln -s ./sbin/init ./init
find | cpio -ocv | gzip -9 > ../ramdisk.img
#create the system file directory
# echo " copy the system files to directory : $TFTPDIR"
# cp -avf ./tftpboot/ $TFTPDIR
# echo "file copy done."
;;
start)
echo "start the dhcpd service."
service dhcpd start
ps -e | grep dhcpd >/dev/null
if [ $? == 0 ]
then
echo "The DHCPD service is OK ."
else
echo "The DHCPD service is not started,please check the system log and fix it."
exit 113
fi
echo "start the TFTP service."
service xinetd start
sleep 5
netstat -antup | grep 69 >/dev/null
if [ $? == 0 ]
then
echo "The TFTP service is OK ."
else
echo "The TFTP service is not started,please check the system log and fix it."
exit 114
fi
echo "all services are started."
;;
stop)
echo "stop dhcpd service."
killall dhcpd
echo "stop xinetd service"
killall xinetd
echo "all services have been killed."
;;
restart)
$0 stop
$0 start
;;
*)
USAGE
;;
esac
#This shell script is used to install disklesImage automaticly.
#This script uses ramdisk as a root partition and uses CPIO based initramfs.
#we want to set DHCP TFTP PXE server
TFTPDIR=/tftpboot #TFTP root directory
DHCPDFILE=/etc/dhcpd.conf #dhcp server configuration file
TFTPFILE=/etc/xinetd.d/tftp #TFTP server configuraton file
CONFDIR=`dirname $0` #the directory where the script is
USAGE()
{
cat <<\EOF
The usage of this script : diskless-install.sh [install|start|stop|restart]
diskless-install.sh install ------to install the disklessImage
diskless-install.sh start ------to start the service
diskless-install.sh stop ------to stop the service
diskless-install.sh restart ------to stop then start the service
EOF
}
case "$1" in
install)
#set DHCP server
#check out if DHCP rpm has been installed
if [ ! -e $DHCPDFILE ]
then
echo "NOW,install dhcpd server with yum......"
yum -y install dhcp
chkconfig --level 345 dhcpd on
service dhcpd start
else
echo "check dhcpd OK ."
fi
#configure DHCP service
echo "configuring dhcpd service......"
cat > /etc/dhcpd.conf << END
ddns-update-style interim;
ignore client-updates;
allow booting;
allow bootp;
next-server 192.168.56.101;
subnet 192.168.56.0 netmask 255.255.255.0 {
option routers 192.168.56.101;
range 192.168.56.1 192.168.56.254;
default-lease-time 1200;
max-lease-time 1800;
option subnet-mask 255.255.255.0;
}
host c1 {
server-name "server";
hardware ethernet 00:30:23:32:45:56:56;
fixed-address 172.18.199.102;
filename "/tftpboot/pxelinux.0";
}
END
service dhcpd restart
# check the dhcp service is started or not
ps -e | grep dhcpd >/dev/null
if [ $? == 0 ]
then
echo "The DHCPD service is OK ."
echo "dhcpd configure done."
else
echo "The DHCPD service is not started,please check the system log and fix it."
exit 113
fi
#set the TFTP service
#check the TFTP server has been installed or not
if [ -e $TFTPFILE ]
then
echo " The tftp server has been installed."
else
echo "NOW,install TFTP server with yum...."
yum install tftp
chkconfig --level 345 xinetd on
srvice xinetd restart
fi
#setup the tftp service
echo "configuring TFTP service......"
cat > /etc/xinetd.d/tftp << END
service tftp
{
disable = no
socket_type = dgram
protocol = udp
wait = yes
user = root
server = /usr/sbin/in.tftpd
server_args = -v -s /tftpboot
per_source = 11
cps = 100 2
flags = IPv4
}
END
service xinetd restart
sleep 5
# check the TFTP service is started or not
netstat -antup | grep 69 >/dev/null
if [ $? == 0 ]
then
echo "The TFTP service is OK ."
echo "TFTP configure done."
else
echo "The TFTP service is not started,please check the system log and fix it."
exit 114
fi
#setup local yum
mount /dev/cdrom /media >/dev/null
mv /etc/yum.repos.d/* /tmp/yum.back
cat > /etc/yum.repos.d/CentOS-Base.repo <
name=ISO
baseurl=file:///media
gpgcheck=0
EOF
yum clean all
mkdir -p $TFTPDIR/installroot
yum -y --installroot=/tftpboot/installroot install basesystem filesystem bash nfs-utils-lib nfs-utils-lib-devel yum MAKEDEV SysVinit audit-libs binutils bzip2-libs centos-release centos-release-notes chkconfig coreutils cpio cracklib cracklib-dicts crontabs db4 device-mapper device-mapper-event device-mapper-multipath dmraid dmraid-events e2fsprogs e2fsprogs-libs elfutils-libelf ethtool expat findutils gawk gdbm glib2 grep gzip hmaccalc info initscripts iproute iputils keyutils-libs kpartx krb5-libs less libacl libattr libcap libselinux libsepol libstdc++ libsysfs libxml2 logrotate lvm2 m2crypto mcstrans mingetty mkinitrd module-init-tools nash ncurses net-tools nspr nss openssl pam pcre popt procps psmisc python python-elementtree python-iniparse python-sqlite pyth-urlgrabber readline rpm rpm-libs rpm-python rsyslog sed sgpio shadow-utils sqlite tar udev util-linux yum-fastestmirror yum-metadata-parser zlib bind-libs openssl perl tar sgpio mkinitrd net-tools dhcp tftp
cp /etc/sysconfig/network $TFTPDIR/installroot/etc/sysconfig
cat > $TFTPDIR/installroot/etc/sysconfig/networt-scripts/ifcfg-eth0 <
BOOTPROTO=dhcp
ONBOOT=yes
EOF
cat > $TFTPDIR/installroot/etc/fstab <
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
EOF
cd $TFTPDIR/installroot
ln -s ./sbin/init ./init
find | cpio -ocv | gzip -9 > ../ramdisk.img
#create the system file directory
# echo " copy the system files to directory : $TFTPDIR"
# cp -avf ./tftpboot/ $TFTPDIR
# echo "file copy done."
;;
start)
echo "start the dhcpd service."
service dhcpd start
ps -e | grep dhcpd >/dev/null
if [ $? == 0 ]
then
echo "The DHCPD service is OK ."
else
echo "The DHCPD service is not started,please check the system log and fix it."
exit 113
fi
echo "start the TFTP service."
service xinetd start
sleep 5
netstat -antup | grep 69 >/dev/null
if [ $? == 0 ]
then
echo "The TFTP service is OK ."
else
echo "The TFTP service is not started,please check the system log and fix it."
exit 114
fi
echo "all services are started."
;;
stop)
echo "stop dhcpd service."
killall dhcpd
echo "stop xinetd service"
killall xinetd
echo "all services have been killed."
;;
restart)
$0 stop
$0 start
;;
*)
USAGE
;;
esac