#!/bin/ash
# Kiran Kamity, 05/11/2004
# Sreen Tallam, 04/04/2008
# Sreen Tallam, 08/08/2008
# Any Netapp additions to the initial sequence of events that must run on
# the RLM go in this file

case "$1" in

	start|"")

		# Unmounting the old root
		# This saves us a lot of RAM (about 9 MB)
		umount /oldroot
		rmdir /oldroot

		# Add the NFS required files; without these files NFS mounts 
		# will cease to work
		
		touch /var/lib/nfs/etab
		touch /var/lib/nfs/rmtab
		touch /var/lib/nfs/state
		touch /var/lib/nfs/xtab

		chmod 644 /var/lib/nfs/etab
		chmod 644 /var/lib/nfs/rmtab
		chmod 600 /var/lib/nfs/state
		chmod 644 /var/lib/nfs/xtab

		# Create this empty syslog file for logroatation to work
		touch /etc/init.d/syslog
		chmod 755 /etc/init.d/syslog 

		# start the watchdog
		insmod /usr/local/module/luna_wdog.ko

		# Create simlink to the GPIO device renamed in 2.6
		mkdir -p /dev/misc
		ln -s /dev/gpio0 /dev/misc/gpio
		
		# Create simlink to 'erase & eraseall' which is renamed in 2.6
		ln -s /usr/sbin/flash_erase /usr/sbin/erase
		ln -s /usr/sbin/flash_eraseall /usr/sbin/eraseall

		# Create these files that are not created in 2.6.18
		mkdir -p /dev/root
		mkdir -p /var/lock
		
		# Create two more directories for sshd purposes
		mkdir -p /var/empty
		mkdir -p /var/empty/sshd

		# Determine if DIAG or NORMAL mode
		rm -f /var/run/InTestFixture;
		/usr/local/bin/intestfixture;

		# Create dirs necessary for cron and logrotate
		mkdir -p /var/lib/logrotate;
		mkdir -p /var/spool;

		# Figure out whether we are booted from primary or backup images
		rm -f /var/run/backup_fw
		/usr/local/bin/cpld_reader;

		mkdir -p /dev/mtd
		mkdir -p /dev/mtdblock
	
		ln -s /dev/mtdblock0 /dev/mtdblock/0
		ln -s /dev/mtdblock1 /dev/mtdblock/1
		ln -s /dev/mtdblock2 /dev/mtdblock/2
		ln -s /dev/mtdblock3 /dev/mtdblock/3
		ln -s /dev/mtdblock4 /dev/mtdblock/4
		ln -s /dev/mtdblock5 /dev/mtdblock/5
		ln -s /dev/mtdblock6 /dev/mtdblock/6
		ln -s /dev/mtdblock7 /dev/mtdblock/7
		ln -s /dev/mtdblock8 /dev/mtdblock/8
		ln -s /dev/mtdblock9 /dev/mtdblock/9


		ln -s /dev/mtd0 /dev/mtd/0
		ln -s /dev/mtd1 /dev/mtd/1
		ln -s /dev/mtd2 /dev/mtd/2
		ln -s /dev/mtd3 /dev/mtd/3
		ln -s /dev/mtd4 /dev/mtd/4
		ln -s /dev/mtd5 /dev/mtd/5
		ln -s /dev/mtd6 /dev/mtd/6
		ln -s /dev/mtd7 /dev/mtd/7
		ln -s /dev/mtd8 /dev/mtd/8
		ln -s /dev/mtd9 /dev/mtd/9

		chmod 666 /dev/mtd/0
		chmod 666 /dev/mtd/1
		chmod 666 /dev/mtd/2
		chmod 666 /dev/mtd/3
		chmod 666 /dev/mtd/4
		chmod 666 /dev/mtd/5
		chmod 666 /dev/mtd/6
		chmod 666 /dev/mtd/7
		chmod 666 /dev/mtd/8
		chmod 666 /dev/mtd/9

		# Mount jffs2_logs and 
		# Mount jffs2_rootfs - primary and backup
		if [ -f /var/run/backup_fw ]; then
			mount -t jffs2 /dev/mtdblock/1 /mnt/logs;
			mount -t jffs2 /dev/mtdblock/7 /mnt/jffs2_rootfs_backup;
			mount -t jffs2 /dev/mtdblock/2 /mnt/jffs2_rootfs_primary;
		else
			mount -t jffs2 /dev/mtdblock/6 /mnt/logs;
			mount -t jffs2 /dev/mtdblock/2 /mnt/jffs2_rootfs_backup;
			mount -t jffs2 /dev/mtdblock/7 /mnt/jffs2_rootfs_primary;
		fi

		# Creating links to primary and backup metadata info
		mkdir -p /etc/rlm_install;
		
		ln -sf /mnt/jffs2_rootfs_primary/install /etc/rlm_install/primary;
		ln -sf /mnt/jffs2_rootfs_backup/install /etc/rlm_install/backup;

		if [ -f /var/run/backup_fw ] ; then
	
			# If installing the RLM FW on the backup partition had failed
			# earlier, the env var INSTALLING_BACKUP will be set to 1. 
			# If this is the case, we shouldn't boot from backup. 
			# Sleeping for 10000 seconds (a long time) will let ONTAP reboot
			# us from primary.
	
			installing_backup=`/usr/local/bin/fw_printenv | grep INSTALLING_BACKUP | sed 's/INSTALLING_BACKUP=//g'`;
			if [ "$installing_backup" -eq "1" ] ; then
				sleep 10000;
				# This should never return.
			fi

		else 

			# If installing the RLM FW on the primary partition had failed
			# earlier, the env var INSTALLING_PRIMARY will be set to 1. 
			# If this is the case, we shouldn't boot from primary. 
			# Sleeping for 10000 seconds (a long time) will let ONTAP reboot		
			# us from backup.

			installing_primary=`/usr/local/bin/fw_printenv | grep INSTALLING_PRIMARY | sed 's/INSTALLING_PRIMARY=//g'`;
			if [ "$installing_primary" -eq "1" ] ; then
				sleep 10000;
				# This should never return.
			fi
		fi

		# Start syslogd-ipv6 
		syslogd-ipv6 -m 0;

		# Start klogd
		/sbin/klogd

		# If the passwdFlag is present, it means that we got rebooted in the midst of updating the 
		# logs partition. In that case, we just use the new password file instead of synchronizing 
		# the old ONTAP users.
		if [ -f /mnt/logs/etc/passwdFlag ] ; then
			mv /mnt/logs/etc/passwd.bak /mnt/logs/etc/passwd
			rm -f /mnt/logs/etc/passwdFlag
		fi

		# Make /etc/passwd a link to the logs partition
		if [ -f /mnt/logs/etc/passwd ] ; then
			# If /mnt/logs/etc/passwd is present, sync ontapusers info from /mnt/logs/etc/passwd to /etc/passwd
			delimit=0;
			touch /etc/passwd.tmp;
			for line in `cat /etc/passwd` ; do
				if [ $delimit -eq 0 ] ; then
					echo $line >> /etc/passwd.tmp;
				fi;
				echo $line | grep -q "#ontapusers";
				if [ $? -eq 0 ]; then
					delimit=1;
				fi
			done;
			delimit=0;
			for line in `cat /mnt/logs/etc/passwd` ; do
				if [ $delimit -eq 1 ] ; then
					echo $line >> /etc/passwd.tmp;
				fi;
				echo $line | grep -q "#ontapusers";
				if [ $? -eq 0 ]; then
					delimit=1;
				fi
			done;
			rm -f /etc/passwd;
			mv /etc/passwd.tmp /etc/passwd;
			rm -f /etc/passwd.tmp;
		fi
		diff /etc/passwd /mnt/logs/etc/passwd;
		# If user credentials haven't changed, no need to write to flash
		# Results of diff: $?=0 if files are same, $?=1 if they differ,
		# $?=2 if any file is missing.
		if ! [ $? = 0 ]; then
			rm -f /mnt/logs/etc/passwd.bak;
			# Save backup copy of merged /etc/passwd file in logs partition. 
			# This copy is used if passwd file in logs partition is corrupted.
			cp -f /etc/passwd /mnt/logs/etc/passwd.bak;
			touch /mnt/logs/etc/passwdFlag
			if [ -f /mnt/logs/etc/rebootFlag ]; then
				rm /mnt/logs/etc/rebootFlag;
				reboot;
			fi
			cp -f /etc/passwd /mnt/logs/etc/passwd
			rm -f /mnt/logs/etc/passwdFlag
		fi
		# Make /etc/passwd a link to /mnt/logs/etc/passwd
		ln -sf /mnt/logs/etc/passwd /etc/passwd

		# If /mnt/logs/etc/copyStart is present, it means that keys were not 
		# correctly copied to logs partition during previous boot. We should not 
		# copy the keys over to RAM as the files could be corrupt. The corrupt 
		# files should be deleted. However, we should copy the sshd_config as 
		# that file is never touched, and hence cannot be corrupted.
		if ! [ -f /mnt/logs/etc/copyStart ]; then
			cp -a /mnt/logs/etc/ssh/*key* /etc/ssh;
		else
			rm -f /mnt/logs/etc/ssh/ssh_host_key;
			rm -f /mnt/logs/etc/ssh/ssh_host_key.pub;
			rm -f /mnt/logs/etc/ssh/ssh_host_rsa_key;
			rm -f /mnt/logs/etc/ssh/ssh_host_rsa_key.pub;
			rm -f /mnt/logs/etc/ssh/ssh_host_dsa_key;
			rm -f /mnt/logs/etc/ssh/ssh_host_dsa_key.pub;
			rm -f /mnt/logs/etc/copyStart;
		fi	

		# Create /var/log/lastlog file to remove err message
		touch /var/log/lastlog;
		chmod 644 /var/log/lastlog;
		
		# bump max number of message queues.  see burt 150196, 279043.
		echo 24 > /proc/sys/kernel/msgmni


		# We need this check to be consistent with ONTAP
		# Read more about it in burt 356390 & 353160		
		tuples="/mnt/logs/etc/rlm_config_from_filer"
		if [ -e $tuples ]
		then
			is_ipv6_disabled=`awk '/ipv6_disable/ {print $2}' $tuples`
			if [ -z $is_ipv6_disabled ]
			then
				is_ipv6_disabled=1
			fi
			if [ "$is_ipv6_disabled" -eq "0" ]
			then
				is_ipv6_ra_disabled=`awk '/ra_disable/ {print $2}' $tuples`
				if [ -z "$is_ipv6_ra_disabled" ]
				then
					is_ipv6_ra_disabled=0
				fi
				# Set this value if the RA needs to be disabled.
				# Even though this will not delete the RA IPv6 address present 
				# in ifconfig, RLM will no longer accpet any new RA 
				# addresses after this. Deletion of the RA will be done in the 
				#firewall
				if [ "$is_ipv6_ra_disabled" -eq "1" ]
				then
					if [ -e "/proc/sys/net/ipv6/conf/eth0/accept_ra" ]
					then 
						# Disable Router Assigned Address	
						echo "0" > "/proc/sys/net/ipv6/conf/eth0/accept_ra"
					else
						echo "netapp_initial_initscript - /proc/sys/net/ipv6/conf/eth0/accept_ra, file not found" >> /var/log/messages
					fi
				fi
					
			fi
		fi
		;;

	stop)

		;;

	*)

		echo "Usage: local {start|stop}" >&2
		exit 1
		;;

esac
