#  Copyright 2008 Nicholas A. Schembri State College PA USA
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see
#    <http://www.gnu.org/licenses/>.

# Thank you Voyage Linux for the idea, http://voyage.hk/ Great job on release 0.5
#
# Tested with 8.04.1
# tested with 9.10
#
#
# ****************************************************************************************
# 
#                                 Change log
#
# 2008.08.01  Added debugging comments in "drop to a shell" section. grub option aufs=tmpfs-debug will stop the init script.
#             reviewed *********** fix fstab on tmpfs ******************
#             rootaufs failed when system was booted with /dev/xxx and fstab had uuid= info.
#             BlaYO pointed out the best and simplest solution was to use grep -v. Grep replaces a sed one liner.
#             Add the comment block to fstab
#             
# 2009.12.07 Corrected issue caused by Apparmor.
#            Name changed to __rootaufs.
# 
#
# 2011.02.24 Adapted to double NFS mount (RO for main and RW for each node)
# 2011.06.01 Adapted to NFSROOT mount and tmpfs (RO for main and RW in tmpfs)
# 2011.09.26 Adapted to NFSROOT mount and iSCSI (RO for main and RW for each node)
# 2012.04.25 Add fsck on iSCSI mounted partition
# 2012.08.07 Change /tmp/net-eth?.conf to /run/net-eth?.conf for Wheezy support

case $1 in
prereqs)
    exit 0
    ;;
esac

export aufs

# Default iSCSI configurations
ISCSI_TARGET_GROUP=1
ISCSI_TARGET_PORT=3260
ISCSI_USERNAME=inputname 
ISCSI_PASSWORD=X2100-2nPassWD

for x in $(cat /proc/cmdline); do 
    case $x in 
    root=*)
        ROOTNAME=${x#root=}
        LABEL=${x#root=LABEL=}
        ;;
    aufs=*)
        aufs=${x#aufs=}
        case $aufs in
        tmpfs-debug)
            aufs=nfs
            aufsdebug=1
            ;;
        esac    
        ;;
    ISCSI_INITIATOR=*)
        iscsi_initiator=${x#ISCSI_INITIATOR=}
	;;
    ISCSI_TARGET_IP=*)
        iscsi_target_ip=${x#ISCSI_TARGET_IP=}
	;;
    ISCSI_TARGET_GROUP=*)
        iscsi_target_group=${x#ISCSI_TARGET_GROUP=}
	;;
    ISCSI_TARGET_PORT=*)
        iscsi_target_port=${x#ISCSI_TARGET_PORT=}
	;;
    ISCSI_USERNAME=*)
        iscsi_username=${x#ISCSI_USERNAME=}
	;;
    ISCSI_PASSWORD=*)
        iscsi_password=${x#ISCSI_PASSWORD=}
	;;

    esac
done


if [ "$aufs" != "nfs" && "$aufs" != "tmpfs" && "$aufs" != "iscsi" ]; then
    aufs=tmpfs
fi

echo 
echo "       root-aufs:  Setting up aufs on ${rootmnt} as root file system "
echo 

IPV4ADDR=$(source /run/net-eth?.conf ; echo $IPV4ADDR)
ROOTSERVER=$(source /run/net-eth?.conf ; echo $ROOTSERVER)
echo "IP address is used and is: "${IPV4ADDR}
echo "Root server is used and is: "${ROOTSERVER}
if [ ${IPV4ADDR} == ${MASTER} ]; then
   echo "No AUFS process for master"
   exit 0
fi

ls /tmp

echo "Mount the AUFS module"
#modprobe -q --use-blacklist aufs
modprobe -q aufs
if [ $? -ne 0 ]; then
    echo    root-aufs error:      Failed to load aufs.ko
    exit 0
fi

# Make the mount points on the init root file system
echo "Create local folders for operations"
mkdir /aufs
mkdir /rw
mkdir /ro

case "$aufs" in
    # NFS
    nfs) 
	echo "Mount NFSROOT privative folder"
	nfsmount -o rsize=32768,wsize=32768 ${ROOTSERVER}:/srv/nfsroot/nodes/${IPV4ADDR} /rw
	;;
    # iSCSI
    iscsi)
	echo "Mount ISCSI privative disk"
	modprobe iscsi_tcp
	modprobe crc32c
	
	iscsi_target_name=$(echo $iscsi_initiator | awk -F':' '{ print $1 }'):${IPV4ADDR}

	iscsistart -i $iscsi_initiator -t $iscsi_target_name -g $ISCSI_TARGET_GROUP -a $iscsi_target_ip -p $ISCSI_TARGET_PORT ${ISCSI_USERNAME:+-u "$ISCSI_USERNAME"} ${ISCSI_PASSWORD:+-w "$ISCSI_PASSWORD"} ${ISCSI_IN_USERNAME:+-U "$ISCSI_IN_USERNAME"} ${ISCSI_IN_PASSWORD:+-W "$ISCSI_IN_PASSWORD"}
	
	udevadm settle
	
	sleep 5
	fsck.ext3 -y LABEL="$LABEL"
	sleep 5
	mount -t ext3 -O noatime,nodiratime LABEL="$LABEL" /rw

	sleep 5

	;;
    # TMPFS
    tmpfs)
	echo "Mount TMPFS (all local changes will be destroyed on reboot)"
	mount -o uid=0,mode=755 -t tmpfs none /rw
	;;
esac

echo "Mount / and /ro"
mount --move ${rootmnt} /ro 
if [ $? -ne 0 ]; then
    echo    root-aufs error:    ${rootmnt}  failed to move to /ro
    exit 0
fi

echo "Mount RO and RW folders on AUFS folder"
mount -t aufs -o noxino,br=/rw:/ro=ro aufs /aufs
#mount -t aufs -o udba=none,noxino,br=/rw:/ro=ro aufs /aufs
#mount -t aufs -o xino=/rw/.xino,br:/rw=rw:/ro=ro aufs /aufs
#mount -t aufs -o br:/rw=rw:/ro=ro aufs /aufs
#mount -t aufs -o br:/rw=rw none /aufs
#mount -o remount,append:/ro=ro /aufs
if [ $? -ne 0 ]; then
    echo    root-aufs error:      Failed to mount /aufs files system
    exit 0
fi

#test for mount points on aufs file system
[  -d /aufs/.rootaufs ] || mkdir /aufs/.rootaufs
[  -d /aufs/.rootaufs/ro ] || mkdir /aufs/.rootaufs/ro
[  -d /aufs/.rootaufs/rw ] || mkdir /aufs/.rootaufs/rw

# the real root file system is hidden on /ro of the init file system.  move it t
o /ro 
mount --move /ro /aufs/.rootaufs/ro
if [ $? -ne 0 ]; then
    echo    root-aufs error:      Failed to move /ro /aufs/.rootaufs/ro 
    exit 0
fi

# tmpfs file system is hidden on /rw
mount --move /rw /aufs/.rootaufs/rw
if [ $? -ne 0 ]; then
    echo    root-aufs error:      Failed to move /rw /aufs/.rootaufs/rw
    exit 0
fi

echo "Mount AUFS folders on / folder"
mount --move /aufs ${rootmnt}

exit 0
