#
# SUMMARY:
#           mount a read/writable partition under <mount-point>
#
# SYNOPSIS:
#           execute mount_rwdisk <mount-point> <spec>
#
# DESCRIPTION:
#
#    For running the Live-System a writable filesystem is needed.
#    This can be a ramdisk (default), but LiveBackup also supports
#    the usage of disk-partitions and directories for persistence
#    data storage.
#
#    <spec>   can be a valid expression for the mount-size argument  
#             ( i.e. mount -n -t tmpfs -o "size=$sz" /mnt )
#
#             or a block-device which contains a recognized 
#             filesystem. As options you can also specify
#             a filesystem and an existing directory. 
#
#             or you can use all available ram except an amount
#             which will remain reserved for the OS. In this case
#             the syntax of <spec> is auto[:<sizeMB>]. The default value
#             for OS RAM is 256MB. 
#
#    Examples:  
#
#                 <spec>=64M
#                 <spec>=/dev/hda2
#                 <spec>=/dev/hda2:ext3  
#                 <spec>=/dev/hda2:ext3:/home/overlay
#
#          to use all available memory and keep 512MB for OS
#
#                  <spec>=auto:512M         
#     
#
# 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
#

mount_point=$1
spec=$2

if [ "$(echo $spec | grep dev)" ]; then


    dev=$(echo $spec | cut -f 1 -d ':')
    fs=$(echo $spec | cut -s -f 2 -d ':')
    path=$(echo $spec | cut -s -f 3 -d ':')

    if [ $fs ]; then
	opt="-n -t $fs"
    else
	opt="-n"
    fi

    mount $opt $dev $mount_point
    if [ $? -ne 0 ]; then
        msg -c "ERROR: mount $opt $dev $mount_point failed"
        rescue_shell "could not mount partition"
    fi

    if [ "$path" ]; then
        if [ -d $mount_point/$path ]; then
            mount -n --bind $mount_point/$path $mount_point
        else
            msg -c "ERROR: directory $dev:$path does not exist"
            rescue_shell "please investigate and correct boot options"
        fi
    fi

else  # mount a ramdisk

    if [ "$(echo $spec | grep auto)" ]; then

        #
        # automatic ramdisk size calculation
        #
        spec=$(echo $spec | cut -s -f 2 -d ':')
        if [ $spec ]; then
            sizeKB=$(memspec2kb $spec)
        else
            sizeKB=$(memspec2kb 256M)
        fi

        usedKB=$MEM_ALLOCATED

        MemSizeKB=$(grep 'MemTotal:' /proc/meminfo | tr -s ' ' | cut -f 2 -d ' ')

        ramdskKB=$(expr $MemSizeKB - $sizeKB)
        ramdskKB=$(expr $ramdskKB  - $usedKB)
        
        if [ $ramdskKB -gt 0 ]; then
            spec=${ramdskKB}K
        else
            msg -c "ERROR: calculation of ramdisk size failed,"
            msg -c "       not enough memory to create RAM filesystem"
            msg -c "available            : $MemSizeKB KB" 
            msg -c "already used         : $usedKB KB"
            msg -c "required for ramdisk : $sizeKB KB"
            rescue_shell "please investigate and correct boot options" 
        fi
    fi

    mount -n -t tmpfs -o "size=$spec" tmpfs $mount_point
    if [ $? -ne 0 ]; then
        msg -c "ERROR: mount -n -t tmpfs -o "size=$spec" tmpfs $mount_point"
        rescue_shell "could not create ramdisk"
    fi

    MEM_ALLOCATED=$(expr $MEM_ALLOCATED + $(memspec2kb $spec)) 

    msg -i "ramdisk of size $spec mounted under $mount_point"
        
fi






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