#!/bin/sh
#  Author:  Joseph L. Kaiser - jlkaiser@fnal.gov
#  Date:  1/16/02
#
#  The farms.after.rpms.sh script was created in it's most workable form
#  on 1/16/02.  This script is for a FHRL 6.1.2 install and may not be
#  applicable to the 7.1 or 7.2 installs.
#  
#  The script does the following:
#  
#  1.  Formats any additional data drives.
#  2.  Checks the /proc filesystem to determine which drive is the cdrom
#      and which are IDE hard drives.  (This only works for IDE disks.)
#  3.  Get the IP address of the machine and determine the hostname via nslookup.
#  4.  Determine which CMS IO node this cluster will receive nfs mounted directories 
#      from.
#  5.  Build the bottom half of the fstab file for possible different drive configs.
#  6   Concatenate the proper bottom half of the fstab file into /etc/fstab.
#  7.  Make directories, change permissions.
#  8.  Set eth0 to full duplex
#  9.  Put in the proper rc script for fbsng.
#  10.  Put the yp host into the hosts file.
#  11.  Put in the other name server
#  12.  Fix the nsswitch file to get your hostnames via DNS
#  13.  Turn off unnecessary services.
#  
#  09/26/02
#  Put in proper rc script for ngop
#  Check for number of actual drives for proper partitioning and fstab file
#
#  06/19/03
#  Many of the config files are now being passed with voldemort.



#  Format any drives additional to the main disk and not the cdrom.
#  Put this in one large partition.

for drive in `ls /proc/ide | awk '/hd?/'`
do
mediatype=`cat /proc/ide/$drive/media`
   if [ "$drive" != "hda" -a "$mediatype" = "disk" ]
        then
        `/sbin/sfdisk -q -L /dev/${drive} << EOF
        ;`
         diskpartition=`/sbin/sfdisk -l /dev/${drive} | \
          grep Linux | awk '{print $1}'`
           /sbin/mke2fs -b 4096 -m 1 ${diskpartition}
      elif [ "$mediatype" = "cdrom" ]; then
              :
         else
         :
    fi
    done

#  Figure out what media type each drive higher than hda is - HD vs. CD-ROM

getmediatype_hdb () {
mediatype=`cat /proc/ide/hdb/media`
echo $mediatype
}

getmediatype_hdc () {
mediatype=`cat /proc/ide/hdc/media`
echo $mediatype
}

getmediatype_hdd () {
mediatype=`cat /proc/ide/hdd/media`
echo $mediatype
}


/bin/mkdir -p /fnal/ups
/bin/mkdir -p /pnfs/cms
/bin/mkdir -p /home/farms
/bin/mkdir -p /var/adm/krb5/farms
/bin/mkdir -p -m 700 /usr/spool/objy
/bin/mkdir -p -m 775 /storage/data
/bin/mkdir /data1
/bin/mount /data1

if [ `ls -ld /proc/ide/h* | wc -l ` = 4 ]
then
/bin/mkdir /data2
/bin/mount /data2
/bin/chmod 775 /data2
/bin/chown 9811.5063 /data2
/bin/umount /data2
else 
	:
fi

/bin/chown 9811.5063 /storage/data
/bin/chown 9811.5063 /data1
/bin/chmod 775 /data1
/bin/umount /data1

/usr/sbin/groupadd -g 501 farms
/usr/sbin/useradd farms -g 500 farms
/bin/chmod 1777 /tmp

/bin/ln -s /home/farms/ngop/ngop_root/worker /var/ngop

# put the yp host into the hosts file.

IPADDR=`/bin/grep IPADDR /etc/sysconfig/network-scripts/ifcfg-eth0 | cut -d= -f 2`

THISHOST=`nslookup $IPADDR | grep -i "Name:" | cut -d":" -f 2 | sed "s/^ *//g"`
NICKNAME=`echo $THISHOST | cut -d. -f 1`

/bin/cp /etc/hosts /etc/hosts.orig

/bin/echo "127.0.0.1       localhost.localdomain localhost
$IPADDR		$THISHOST	$NICKNAME	
131.225.206.41          cmsun1.fnal.gov                 cmsun1" > /etc/hosts


#  Determine which CMS IO node this cluster will receive nfs mounted directories 
#  from.
case $THISHOST in
   gyoza*)  MYHOST='ramen';;
   popcrn09*|popcrn10*|popcrn19*|popcrn20*|popcrn29*|popcrn30*|popcrn39*|popcrn40*)  MYHOST='bigmac';;
   popcrn*)  MYHOST='gallo';;
   *) MYHOST='gallo';;
esac

# Build the bottom half of the fstab file for possible different drive configs.

if [ -e /proc/ide/hdc ]
  then
fstab_hdb="
/dev/hdb1               /data1                  ext2    defaults 1 1
/dev/hdc1               /data2                  ext2    defaults 1 1
$MYHOST:/fnal/ups        /fnal/ups       nfs     rsize=8192,wsize=8192,ro,hard,intr,bg 0 0
$MYHOST:/usr/local	/usr/local     nfs     rsize=8192,wsize=8192,ro,soft,bg,intr 0 0
$MYHOST:/data     /data                           nfs     rsize=8192,wsize=8192,rw,hard,bg,intr 0 0
$MYHOST:/home/farms	 /home/farms	nfs     rsize=8192,wsize=8192,rw,hard,bg,intr 0 0
$MYHOST:/data		/storage/data nfs     rsize=8192,wsize=8192,rw,hard,bg,intr 0 0
stkensrv1:/cms          /pnfs/cms               nfs     user,intr,hard,bg,rw,noac 0 0"

fstab_hdd="
/dev/hdc1               /data1                  ext2    defaults 1 1
/dev/hdd1               /data2                  ext2    defaults 1 1
$MYHOST:/fnal/ups        /fnal/ups       nfs     rsize=8192,wsize=8192,ro,hard,intr,bg 0 0
$MYHOST:/usr/local	/usr/local     nfs     rsize=8192,wsize=8192,ro,soft,bg,intr 0 0
$MYHOST:/data     /data                           nfs     rsize=8192,wsize=8192,rw,hard,bg,intr 0 0
$MYHOST:/home/farms	 /home/farms	nfs     rsize=8192,wsize=8192,rw,hard,bg,intr 0 0
$MYHOST:/data		/storage/data nfs     rsize=8192,wsize=8192,rw,hard,bg,intr 0 0
stkensrv1:/cms          /pnfs/cms               nfs     user,intr,hard,bg,rw,noac 0 0"
else 

fstab_hdb="
/dev/hdb1               /data1                  ext2    defaults 1 1
$MYHOST:/fnal/ups        /fnal/ups       nfs     rsize=8192,wsize=8192,ro,hard,intr,bg 0 0
$MYHOST:/usr/local	/usr/local     nfs     rsize=8192,wsize=8192,ro,soft,bg,intr 0 0
$MYHOST:/data     /data                           nfs     rsize=8192,wsize=8192,rw,hard,bg,intr 0 0
$MYHOST:/home/farms	 /home/farms	nfs     rsize=8192,wsize=8192,rw,hard,bg,intr 0 0
$MYHOST:/data		/storage/data nfs     rsize=8192,wsize=8192,rw,hard,bg,intr 0 0
stkensrv1:/cms          /pnfs/cms               nfs     user,intr,hard,bg,rw,noac 0 0"

fstab_hdd="
/dev/hdd1               /data1                  ext2    defaults 1 1
$MYHOST:/fnal/ups        /fnal/ups       nfs     rsize=8192,wsize=8192,ro,hard,intr,bg 0 0
$MYHOST:/usr/local	/usr/local     nfs     rsize=8192,wsize=8192,ro,soft,bg,intr 0 0
$MYHOST:/data     /data                           nfs     rsize=8192,wsize=8192,rw,hard,bg,intr 0 0
$MYHOST:/home/farms	 /home/farms	nfs     rsize=8192,wsize=8192,rw,hard,bg,intr 0 0
$MYHOST:/data		/storage/data nfs     rsize=8192,wsize=8192,rw,hard,bg,intr 0 0
stkensrv1:/cms          /pnfs/cms               nfs     user,intr,hard,bg,rw,noac 0 0"
fi



#  Concatenate the proper bottom half of the fstab file into /etc/fstab.

if [ `getmediatype_hdb` = "disk" -a `getmediatype_hdd` = "cdrom" -o `getmediatype_hdc` = "cdrom" ]
  then
    echo "$fstab_hdb" >> /etc/fstab
elif [ `getmediatype_hdd` = "disk" -a `getmediatype_hdb` = "cdrom" -o `getmediatype_hdc` = "cdrom" ]
  then
    echo "$fstab_hdd" >> /etc/fstab
else 
    :
fi

# put in the other name server
/bin/cp /etc/resolv.conf /etc/resolv.conf.orig
/bin/echo "search fnal.gov
nameserver 131.225.8.120
nameserver 131.225.17.150" > /etc/resolv.conf

# hosts.allow and hosts.deny

/bin/cp /etc/hosts.allow /etc/hosts.allow.orig
/bin/cp /etc/hosts.deny /etc/hosts.allow.deny
/bin/echo '#
# hosts.allow   This file describes the names of the hosts which are
#               allowed to use the local INET services, as decided
#               by the '/usr/sbin/tcpd' server.
#
# 

portmap: 131.225.0.0/255.255.0.0 LOCAL 127.0.0.1
rpc.mountd: 131.225. LOCAL 127.0.0.1
ALL: .fnal.gov 131.225. LOCAL 127.0.0.1 : banners /etc/banners
sshd: ALL' > /etc/hosts.allow

/bin/echo '#
# hosts.deny    This file describes the names of the hosts which are
#               *not* allowed to use the local INET services, as decided
#               by the '/usr/sbin/tcpd' server.
#
# The portmap line is redundant, but it is left to remind you that
# the new secure portmap uses hosts.deny and hosts.allow.  In particular
# you should know that NFS uses portmap!


ALL: ALL except 127.0.0.1 : banners /etc/banners' > /etc/hosts.deny


# yp.conf

/bin/cp /etc/CMSfarm/configfiles/yp.conf /etc/yp.conf

/bin/cp /etc/CMSfarm/configfiles/k5login /root/.k5login

# fix the nsswitch file to get your hostnames via DNS

/bin/cp /etc/nsswitch.conf /etc/nsswitch.conf.orig

/bin/sed "s/\(^hosts: *\)\(files *\)\(nisplus nis \)\(dns\)/\1\4 \2/g" /etc/nsswitch.conf.orig > /etc/nsswitch.conf

#put in a usable root .bashrc.
/bin/cp /etc/CMSfarm/configfiles/root.bashrc /root/.bashrc

#
/bin/cp /etc/CMSfarm/configfiles/ngop /etc/rc.d/init.d


/bin/mv /etc/CMSfarm/configfiles/ks.cfg.worker /root/ks.cfg
/bin/mv /etc/CMSfarm/configfiles/fermi-install-6.1.2-3.i386.rpm /etc/CMSfarm

# Turn off unnecessary services.
/sbin/chkconfig --level 345 fbsng on
/sbin/chkconfig --level 345 ngop on
/sbin/chkconfig --level 345 facct on
/sbin/chkconfig --level 345 ultradma on
/sbin/chkconfig --level 345 autofs off
/sbin/chkconfig --level 345 portmap on
/sbin/chkconfig --level 345 rstatd on
/sbin/chkconfig --level 345 ypbind on

/sbin/chkconfig --del sendmail
/sbin/chkconfig --del kudzu
/sbin/chkconfig --del identd
/sbin/chkconfig --del apmd
/sbin/chkconfig --del gpm

/sbin/chkconfig --add nfslock


rm -f /usr/man/man1/rec.1

rpm -e kernel-pcmcia-cs
rpm -e netscape-common netscape-communicator

/etc/cron.daily/slocate.cron

echo "/usr/X11R6/lib" >> /etc/ld.so.conf
echo "/usr/i486-linux-libc5/lib" >> /etc/ld.so.conf
/sbin/ldconfig

rm -f /usr/man/man1/rec.1
rm -f /usr/man/man8/rpc.mountd.8.gz

cat > /etc/voldemort.conf << 'EOF'
#!/bin/sh
# VOLDEMORT version 0.5
# voldemort.conf
# Last revised 9/30/02  S. Timm
#
# This is the global configuration file for voldemort
#
# These two are here by default in pullrsync rpm
RSYNC_SERVER=131.225.206.143
RSYNC_BACKUP=131.225.206.143
RSYNC_MODULE=voldemort
# We get the hostname live from the system configuration
# If this is run during the install we presume it is part of
# the farms.after.rpms.sh script or some other chroot'ed script.
#Tue Mar  4 14:42:22 CST 2003
RSYNC_CLUSTER=
RSYNC_FLAVOR=Linux+2.4
RSYNC_NODESPEC=N
RSYNC_SUBCLUSTER=
EOF

#/usr/sbin/ntpdate -s 131.225.206.200
#/usr/krb5/sbin/kadmin -r FNAL.GOV -p host/$THISHOST@FNAL.GOV -w "YTja82znrdt" -q "ktadd host/$THISHOST@FNAL.GOV"
#/usr/krb5/sbin/kadmin -r FNAL.GOV -p ftp/$THISHOST@FNAL.GOV -w "YTja82znrdt" -q "ktadd ftp/$THISHOST@FNAL.GOV"

#added voldemort command
/sbin/pullrsync -I -H $NICKNAME > /tmp/voldemort.log 2>&1
