#!/bin/sh
NEWROOT=$3
if [ ! -z "$imgurl" ]; then
	if [ xhttp = x${imgurl%:*} ]; then
		NFS=0
		FILENAME=${imgurl##*/}
		while [ ! -r "$FILENAME" ]; do
			echo Getting $imgurl...
			if ! wget $imgurl; then
				rm -f $FILENAME
				sleep 27
			fi
		done
	elif [ xnfs = x${imgurl%:*} ]; then
		NFS=1
		SERVER=${imgurl#nfs:}
		SERVER=${SERVER#/}
		SERVER=${SERVER#/}
		ROOTDIR=$SERVER
		SERVER=${SERVER%%/*}
		SERVER=${SERVER%:}
		ROOTDIR=/${ROOTDIR#*/} 
	fi
elif [ ! -z "$nfsroot" ]; then #fyi, done this way to tolerate nfs://
	NFSROOT=1
	SERVER=${nfsroot}
	ROOTDIR=$SERVER
	SERVER=${SERVER%%/*}
	SERVER=${SERVER%:}
	ROOTDIR=/${ROOTDIR#*/} 
elif [ ! -z $statemnt ]; then #btw, uri style might have left future options other than nfs open, will use // to detect uri in the future I guess
	NFSROOT=1
	SNAPSHOTSERVER=${statemnt%:*}
	SNAPSHOTROOT=${statemnt#*/}
	# may be that there is not server and just a directory.
	if [ -z $SNAPSHOTROOT ]
	then
		SNAPSHOTROOT=$SNAPSHOTSERVER
		SNAPSHOTSERVER=
	fi
fi

#echo 0 > /proc/sys/vm/zone_reclaim_mode #Avoid kernel bug

if [ "$NFSROOT" = "1" ]; then
	echo Setting up Statelite
	mkdir -p $NEWROOT
 	MAXTRIES=7
	ITER=0
	#ME=`hostname`
	while ! mount.nfs ${SERVER}:${ROOTDIR}/rootimg $NEWROOT -r -n -o nolock,rsize=32768,tcp,nfsvers=3,timeo=14
	do
		ITER=$(expr $ITER + 1)
		if [ "$ITER" == "$MAXTRIES" ]
      		then
				echo "Failed mounting nfs"
				echo "Possible problems:
1.  This initrd wasn't created for statelite node?  rerun genimage with the -m statelite flag, then rerun 'nodeset $ME statelite'
2.  Is DNS set up?  Maybe that's why I can't mount ${SERVER}.
3.  The nfs modules aren't set right in this initfs?"
    
			/bin/dash
			exit
		fi
		echo "Could not mount $SERVER:$ROOTDIR on $NEWROOT $RESET"
		RS=30 #`expr $RANDOM % 30`
		echo -e "Trying again in $RS seconds"
		sleep $RS
	done

# now we need to mount the rest of the system.  This is the read/write portions
#echo "Mounting Snapshot directories"

	if [ ! -e "$NEWROOT/$RWDIR" ]
	then
		echo ""
		echo "This NFS root directory doesn't have a /$RWDIR directory for me to mount a rw filesystem.  You'd better create it... ${NORMAL}"
		echo "."
		/bin/dash
	fi

	while [ ! -e "$NEWROOT/etc/init.d/statelite" ]
	do
		echo ""
		echo "$NEWROOT/etc/init.d/statelite doesn't exist.  Perhaps you didn't create this image with the -m statelite mode"
		echo ""
		/bin/dash
	done
	grep '\(shell\)' /proc/cmdline >/dev/null && /bin/dash
	mount -t tmpfs rw  -o mode= $NEWROOT/$RWDIR
	mkdir -p $NEWROOT/$RWDIR/tmpfs


	# mount the SNAPSHOT directory here for persistent use.
	if [ ! -z $SNAPSHOTSERVER ]
	then
		mkdir -p $NEWROOT/$RWDIR/persistent
 		MAXTRIES=5
		ITER=0
		while ! mount $SNAPSHOTSERVER:$SNAPSHOTROOT  $NEWROOT/$RWDIR/persistent -o nolock,rsize=32768,tcp,nfsvers=3,timeo=14
		do
			ITER=$(expr $ITER + 1)
			if [ "$ITER" == "$MAXTRIES" ]
      			then
				echo "You're dead.  rpower $ME boot to play again."
				echo "Possible problems:
1.  $SNAPSHOTSERVER is not exporting $SNAPSHOTROOT ?
2.  Is DNS set up?  Maybe that's why I can't mount $SNAPSHOTSERVER."
				shell
				exit
			fi
			echo "Can't mount $SNAPSHOTSERVER:$SNAPSHOTROOT. ${NORMAL}"
			RS=45 #`expr $RANDOM % 20`
			echo "Trying again in $RS seconds"
			sleep $RS	
		done
	fi

	grep '\(shell\)' /proc/cmdline >/dev/null && /bin/dash

	# have to preserve the initial DHCP request.  So we link it.

	if [ ! -d $NEWROOT/$RWDIR/tmpfs/var/lib/dhclient ]
	then
		mkdir -p $NEWROOT/$RWDIR/tmpfs/var/lib/dhclient
	fi
	if [ ! -d $NEWROOT/$RWDIR/tmpfs/var/lib/dhcp ]
	then
		mkdir -p $NEWROOT/$RWDIR/tmpfs/var/lib/dhcp
	fi

	[ -e /etc/ntp.conf ] && mkdir -p $NEWROOT/$RWDIR/tmpfs/etc && cp /etc/ntp.conf $NEWROOT/$RWDIR/tmpfs/etc/ 

	
	[ -e /etc/ntp/step-kickers ] && mkdir -p $NEWROOT/$RWDIR/tmpfs/etc/ntp && cp /etc/ntp/step-kickers $NEWROOT/$RWDIR/tmpfs/etc/ntp 

	
	[ -e /etc/resolv.conf ] && mkdir -p $NEWROOT/$RWDIR/tmpfs/etc && cp /etc/resolv.conf $NEWROOT/$RWDIR/tmpfs/etc/ 

	# now that everything is mounted, lets do this
	# hmmm, apparently I'm checking this twice... so I'd better
	# be really sure the file is there.
	while [ -z $NEWROOT/etc/init.d/statelite ]
	do
		echo "$NEWROOT/etc/init.d/statelite does not exist in image!"
		/bin/dash
	done

	# do all the mounts:
	$NEWROOT/etc/init.d/statelite	

	# give the debug shell just before we go if specified!
	grep '(shell)' /proc/cmdline > /dev/null && dash

	echo 0x100 > /proc/sys/kernel/real-root-dev
	keep_old_ip=yes
	export keep_old_ip
	fastboot=yes
	export fastboot
	READONLY=yes
	export READONLY
	grep '\(shell\)' /proc/cmdline >/dev/null && /bin/dash
	mount -n --bind /dev $NEWROOT/dev
	umount /sys
	umount /proc

fi
# END NFSROOT/Statelite code

# RAM root Hybrid with NFS root
if [ "$NFS" = "1" ]; then
  echo Setting up nfs with ram overlay.
  mknod /dev/loop0 b 7 0
  mkdir -p /ro
  mkdir -p /rw
  #NOTE: should prob have max count
  while [ ! -d /ro/bin ]; do
    echo mounting $SERVER:$ROOTDIR on /ro
    mount.nfs $SERVER:$ROOTDIR /ro -r -n -o nolock,rsize=32768,tcp,nfsvers=3,timeo=14
    ST=`expr $RANDOM % 5`
    sleep $ST
  done
  mount -t tmpfs rw /rw
  mkdir -p /rw/etc
  mkdir -p /rw/var/lib/dhclient
  cp /etc/resolv.conf /rw/etc/
  mount -t aufs -o dirs=/rw:/ro mergedroot  $NEWROOT
  mkdir -p $NEWROOT/ro
  mkdir -p $NEWROOT/rw
  mount --move /ro $NEWROOT/ro
  mount --move /rw $NEWROOT/rw
  cp /etc/resolv.conf $NEWROOT/etc/
  echo xcatfs / aufs rw,_netdev 0 0 >> $NEWROOT/etc/fstab
elif [ -r /rootimg.sfs ]; then
  echo Setting up squashfs with ram overlay.
  mknod /dev/loop0 b 7 0
  mkdir -p /ro
  mkdir -p /rw
  mount -t squashfs /rootimg.sfs /ro
  mount -t tmpfs rw /rw
  mount -t aufs -o dirs=/rw:/ro mergedroot $NEWROOT
  mkdir -p $NEWROOT/ro
  mkdir -p $NEWROOT/rw
  mount --move /ro $NEWROOT/ro
  mount --move /rw $NEWROOT/rw
elif [ -r /rootimg.gz ]; then
echo Setting up RAM-root tmpfs.
  mount -t tmpfs rootfs $NEWROOT
  cd $NEWROOT
  echo -n "Extracting root filesystem:"
  if [ -x /bin/cpio ]; then
  gzip -cd /rootimg.gz |/bin/cpio -idum
  else
  gzip -cd /rootimg.gz |cpio -idum
  fi
  echo Done
else
  echo -n Failed to download image, panicing in 5...
  for i in 4 3 2 1 0; do
    /bin/sleep 1
    echo -n $i...
  done
  echo
  echo "You're dead.  rpower nodename reset to play again.

* Did you packimage with -m cpio, -m squashfs, or -m nfs?
* If using -m squashfs did you include aufs.ko with geninitrd?
  e.g.:  -n tg3,squashfs,aufs,loop
* If using -m nfs did you export NFS and sync rootimg?  And
  did you include the aufs and nfs modules in the proper order:
  e.g.:  -n tg3,aufs,loop,sunrpc,lockd,nfs_acl,nfs

"
  /bin/dash
  exit
fi
cd /
for lf in /tmp/dhclient.*.lease; do
    netif=${lf#*.}
    netif=${netif%.*}
    cp $lf  "$NEWROOT/var/lib/dhclient/dhclient-$netif.leases"
done
cp /etc/resolv.conf "$NEWROOT/etc/"

if [ -d "$NEWROOT/etc/sysconfig" -a ! -e "$NEWROOT/etc/sysconfig/selinux" ]; then
	echo "SELINUX=disabled" >> "$NEWROOT/etc/sysconfig/selinux"
fi

# inject new exit_if_exists
echo 'settle_exit_if_exists="--exit-if-exists=/dev/root"; rm "$job"' > /initqueue/xcat.sh
# force udevsettle to break
> /initqueue/work
