#!/bin/sh

# Quick hack to mount an image file over NFS, as an alternative to NBD mounts.
#
# Requires specifying nfsroot=/opt/ltsp/images and ltsploop=ARCH.img on the
# commandline. ltsploopfs=FS can be used to specify the filesystem, defaults to
# "squashfs"
#
# Also needs a dummy /opt/ltsp/images/sbin/init-ltsp to be an executable file
# to work around a bug/feature in initramfs-tools's nfs mount checks...

for x in $(cat /proc/cmdline); do
    case "$x" in
        ltsploop=*)
            ltsploop=${x#ltsploop=}
            ;;
        ltsploopfs=*)
            ltsploopfs=${x#ltsploopfs=}
            ;;
    esac
done

test -n "$ltsploop" || exit 0
ltsploopfs=${ltsploopfs:-"squashfs"}

[ -z "${rootmnt}" ] && panic "rootmnt unknown in init-bottom"

if [ -f "${rootmnt}/${ltsploop}" ]; then
   mkdir -p /ltsploop
   mount -o move ${rootmnt} /ltsploop
   mkdir -p ${rootmnt}
   # TODO filesystem detection
   mount -o ltsploop -t ${ltsploopfs} /ltsploop/${ltsploop} ${rootmnt}
fi
