#!/bin/sh
#181212 sfs
ROOT_MNT="/new_root"
OFS_DIRS="/run/archroot"
OFS_HOME="${OFS_DIRS}/root_ro"
OFS_COWSPACE="${OFS_DIRS}/cowspace"

run_hook() {
    [ "${aufs}" ] || return
    if [ ! "${rootdir}" ]; then
        err " rootdir= not defined"
        launch_interactive_shell
    fi

    mount_handler="aufs_mount_handler"
    export mount_handler
}

aufs_mount_handler(){
XXmoveroot(){
	[ -d ${OFS_HOME} ] || mkdir -p ${OFS_HOME}
	mount --move ${ROOT_MNT} ${OFS_HOME}
    	OFS_LOWER="${OFS_HOME}/${rootdir}"
}
#moveroot
    mkdir -p "${OFS_HOME}"
    default_mount_handler "${OFS_HOME}" && 
    	OFS_LOWER="${OFS_HOME}/${rootdir}"

	if [ "${aufs}" != "y" ]; then 
	    OFS_COWSPACE="${OFS_LOWER}-${aufs}"
	    mkdir -p ${OFS_COWSPACE}
	else
	    mkdir -p ${OFS_COWSPACE}
	    mount -t tmpfs cowspace ${OFS_COWSPACE}
	fi

mount -t aufs -o br=${OFS_COWSPACE}=rw:${OFS_LOWER}=ro+wh aufs ${ROOT_MNT} 
#    /bin/mount -t aufs -o nowarn_perm,br:"${src}" rootaufs "${mnt}"
     if [ $? != 0 ]; then
	err "aufs : mount -t aufs -o br=${OFS_COWSPACE}=rw:${OFS_LOWER}=ro+wh aufs ${ROOT_MNT} "
        launch_interactive_shell
#        return
     fi
simlink1(){
	    HOME="${OFS_LOWER}/mnt/home"
	    rmdir "${HOME}" 2>/dev/null
	    [ -L "${HOME}" ] && rm "${HOME}"
	    ln -sf "${OFS_LOWER}" "${HOME}"
		if [ $? != 0 ]; then
	            err " aufs err: ln -sf ${OFS_LOWER} ${HOME}"
	            launch_interactive_shell
#	            return
	        fi
}
simlink(){
LNK(){
	    rmdir "${2}" 2>/dev/null
	    [ -L "${2}" ] && rm "${2}"
	    mkdir -p "`dirname "$2"`"
	    [ -d "$1" ] && ln -sf "${1}" "${2}"
}
newroot="/new_root"
LNK "${OFS_HOME}" "${newroot}/mnt/home"
LNK "${OFS_COWSPACE}" "${newroot}/mnt/live/memory/changes"
}
simlink

}