#
# copy system into ram
#
# 1. create ramfs
# 2. copy kernel modules (if on media)
# 3. copy $MASTER_MNT/system/$SYSTEM
#
# part of the LiveBackup project:  
#
#                     http://livebackup.sourceforge.net/
#
# Copyright 2006 by Wolfgang Rohrmoser <info@rohrmoser-engineering.de>
#
#    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 2 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, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#

if [ "$COPY2RAM" != "no" ]; then 

    sz=$(echo $COPY2RAM | cut -d ":" -f 2)
    if [ $(echo $sz | grep %) ]; then
	percent=$(expr substr $sz 1 $(expr length $sz - 1))
    else
	msg -c "WARNING: invalid size format $sz !!!"
	RAM_MNT=$MASTER_MNT;    
    fi 

    # Check if system was found. If no system is found, skip copy2ram. 
    # Maybe this can be recovered manually, error will be handled later in 
    # _mount_image
    if [ ! "$image_size" ]; then 
	msg -w "unknown size for $SYSTEM, skipping copy to ram ..."
    else
	ram_sz=$(cat /proc/meminfo | awk '/MemTotal/{print $2}')
	if [ -d $MEDIA_MNT/lib/modules/$kversion ]; then
	    mod_sz=$(du -s $MEDIA_MNT/lib/modules/$kversion | awk '{print $1}')
	else
	    mod_sz=0
	fi

	msg -i "$SYSTEM: $image_size kB, Modules: $mod_sz kB, RAM: $ram_sz kB)"

        # check sizes
	required_sz=$( expr $image_size + $mod_sz )
	required_sz=$( expr $required_sz + $required_sz / 10 )  # add 10%
	min_sz=$( expr $ram_sz \* $percent \/ 100 )

	if [ $(expr $required_sz \< $min_sz) -eq 1 ]; then

	    msg -i "copy system to RAM (using ${required_sz}K of ${ram_sz}K) ..."

	    mkdir -p $RAM_MNT

            mount -n -t tmpfs -o "size=${required_sz}K" tmpfs $RAM_MNT

	    if [ $? -ne 0 ]; then
		msg -c "WARNING: copy to ram failed !!!"
		RAM_MNT=$MASTER_MNT;    
	    fi

	    if [ -d $MEDIA_MNT/lib/modules/$kversion ]; then
		mkdir -p $RAM_MNT/lib/modules/$kversion
		cp -a $MEDIA_MNT/lib/modules/$kversion/* $RAM_MNT/lib/modules/$kversion/
	    fi

	    mkdir -p $RAM_MNT/system/
	    if [ -f $MEDIA_MNT/system/$SYSTEM.sqsh ]; then
		# rsync -P  $MEDIA_MNT/system/$SYSTEM.sqsh $RAM_MNT/system/$SYSTEM.sqsh
                bar -if $MEDIA_MNT/system/$SYSTEM.sqsh -of $RAM_MNT/system/$SYSTEM.sqsh
		if [ -f $MEDIA_MNT/system/$SYSTEM.md5 ]; then
		    cp $MEDIA_MNT/system/$SYSTEM.md5  $RAM_MNT/system/
		fi
	    else
		cp $MEDIA_MNT/system/$SYSTEM* $RAM_MNT/system/
	    fi

	    if [ $? -ne 0 ]; then
		msg -c "WARNING: copy to ram failed !!!"
		RAM_MNT=$MASTER_MNT    
	    fi

            # we cannot umount $MEDIA_MNT here, because we still need it for
            # hardware detection and configuration
	
	    MASTER_MNT=$RAM_MNT 
	    COPY2RAM="success"
            MEM_ALLOCATED=$(expr $MEM_ALLOCATED + ${required_sz}) 

	else
	    msg -i "skipping copy2ram: $required_sz kB of ram > $percent % of available ramsize $ram_sz kB" 
	fi

    fi
fi


#
# Local Variables: 
# mode: shell-script
# End: 
#
