# NBD filesystem mounting           -*- shell-script -*-
#
#  Copyright (c) 2007 Canonical
#  Author: Oliver Grawert <ogra@canonical.com>
#
#  2008, Stephane Graber <stgraber@ubuntu.com>
#  2009, Vagrant Cascadian <vagrant@freegeek.org>
#
#  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, you can find it on the World Wide
#  Web at http://www.gnu.org/copyleft/gpl.html, or write to the Free
#  Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
#  MA 02110-1301, USA.
#


# NBD root mounting
mountroot()
{
    [ "$quiet" != "y" ] && log_begin_msg "Running /scripts/nfs-top"
    run_scripts /scripts/nfs-top
    [ "$quiet" != "y" ] && log_end_msg

    configure_networking

    if [ -n "${nbdroot}" ]; then
               NBD_ROOT_SERVER=$( echo "${nbdroot}" | sed 's/:.*//')
               NBD_ROOT_PORT=$( echo "${nbdroot}" | sed 's/.*://')
    else
               NBD_ROOT_SERVER="${ROOTSERVER}"
               NBD_ROOT_PORT=${nbdport:-"2000"}
    fi
    if [ -n "${nbdswap}" ]; then
               NBD_SWAP_SERVER=$( echo "${nbdswap}" | sed 's/:.*//')
               NBD_SWAP_PORT=$( echo "${nbdswap}" | sed 's/.*://')
    else
               NBD_SWAP_SERVER="${NBD_ROOT_SERVER}"
               NBD_SWAP_PORT=${nbdswapport:-"9572"}
    fi

    # try to get swap from the server if we dont have enough ram (less than 48M)
    min_ram=49152
    real_ram=$(cat /proc/meminfo |grep MemTotal|tr -d " [a-z][A-Z]:")

    if [ ${real_ram} -lt ${min_ram} ];then
        [ "$quiet" != "y" ] && log_begin_msg "Only found ${real_ram}k main ram, trying network swap."
        nbd-client ${NBD_SWAP_SERVER} ${NBD_SWAP_PORT} /dev/nbd1 || true
        mkswap /dev/nbd1 || true
        swapon /dev/nbd1 || true
        [ "$quiet" != "y" ] && log_end_msg
    fi

    mkdir -p /cow
    mkdir -p /rofs

    # create tmpfs for writeable files
    mount -t tmpfs -o mode=0755 tmpfs /cow

    # Make sure the loopback interface has an address
    ip addr add dev lo 127.0.0.1
    ip link set lo up

    # Start nbd-proxy and wait for it to connect
    nbd-proxy ${NBD_ROOT_SERVER} ${NBD_ROOT_PORT} ${NBD_ROOT_PORT}

    # Mount the root device
    if nbd-client 127.0.0.1 ${NBD_ROOT_PORT} /dev/nbd0 ; then
        eval $(fstype /dev/nbd0)
        if [ -z "$FSTYPE" ]; then
            echo "Warning: unable to detect filesystem, assuming squashfs."
            FSTYPE=squashfs
        fi
        mount -o ro -t $FSTYPE /dev/nbd0 /rofs
    else
        panic "Error: failed to connect to NBD server."
    fi

    # merge ro and rw filesystems
    mount -t aufs -o dirs=/cow=rw:/rofs=ro aufs ${rootmnt}

    # make the filesystems accessible
    mkdir -p ${rootmnt}/cow
    mount -o bind /cow ${rootmnt}/cow

    mkdir -p ${rootmnt}/rofs
    mount -o move /rofs ${rootmnt}/rofs

    # get the lts.conf via tftp
    tftppath="$(echo "$filename" | sed -n 's,\"*\(.*/\)[^/]*,\1,p')"
    echo "get ${tftppath:-/ltsp/i386}/lts.conf" | /bin/tftp ${NBD_ROOT_SERVER} >/dev/null 2>&1
    if [ -s ./lts.conf ]; then
        cp ./lts.conf ${rootmnt}/etc/
    fi

    # write some useful information
    mkdir -p ${rootmnt}/var/cache/ltsp/
    echo NBD_SERVER=${NBD_ROOT_SERVER} > ${rootmnt}/var/cache/ltsp/ltsp_config
    echo NBD_ROOT_PORT=${NBD_ROOT_PORT} >> ${rootmnt}/var/cache/ltsp/ltsp_config

    # make the chroot's /etc/mtab a file rather than a link
    # because mountall.conf complains loudly
    if [ -L ${rootmnt}/etc/mtab ]; then
        rm ${rootmnt}/etc/mtab
        :>${rootmnt}/etc/mtab
    fi

    [ "$quiet" != "y" ] && log_begin_msg "Running /scripts/nfs-bottom"
    run_scripts /scripts/nfs-bottom
    [ "$quiet" != "y" ] && log_end_msg
}
