# Local filesystem mounting                     -*- shell-script -*-

local_mount_root()
{
        moun -t tmpfs tmpfs /live
        mkdir /live/base /live/userdata /live/work -p

        # check live boot
        if [ "$pxvdi" == "live" ]; thens
                echo "boot into live mode ..."
                if [ -f "/pxvdi.iso" ]; then
                        if ! mount -t iso9660 /pxvdi.iso /live/base; then
                                panic "mount /pxvdi.iso in pxe mode failed"
                        fi
                else
                        # check iso
                        PXVDI_ISO=`blkid -L PXVDI`
                        if [ ! -b "$PXVDI_ISO" ]; then
                                panic "No PXVDI ISO found"
                        fi

                        if ! mount -t auto $PXVDI_ISO  /live/base; then
                                panic "mount $PXVDI_ISO in live mode failed"
                        fi
                fi

        else
                PXVDIBASE=`blkid -L PXVDIBASE-$pxvdi`
                PXVDIUSERDATA=`blkid -L PXVDIUSERDATA`
                if [ -b "$PXVDIBASE" ]; then

                        if ! mount -t ext4 -o ro,loop "$PXVDIBASE" /live/base; then
                                panic "mount '$PXVDIBASE' failed"
                        fi

                        if [ -b "$PXVDIUSERDATA" ];then
                                if ! mount -t ext4 -o rw "$PXVDIUSERDATA" /live/work/; then
                                panic "mount '$PXVDIUSERDATA' failed"
                                fi
                        fi
                fi
        fi

        echo "create overlay filesystem ..."
        mkdir /live/work/userdata /live/work/base /live/work/work -p

        if ! mount -t squashfs -o rw "/live/base/pxvdi.img" /live/work/base; then
                panic "mount '$pxvdi' failed"
        fi

        if ! mount -t overlay -o lowerdir=/live/work/base,upperdir=/live/work/userdata,workdir=/live/work/work  none /root/; then
                panic "mount overlayfs failed"
        fi

        mkdir /root/pxvdi-iso
        if ! mount --bind /live/base /root/pxvdi-iso; then
                panic "bind mount iso failed"
        fi
}

local_premount()
{

}

mountroot()
{
        local_mount_root
}

mount_top()
{
        # Note, also called directly in case it's overridden.
        
}

mount_premount()
{
        # Note, also called directly in case it's overridden.
        
}

mount_bottom()
{
        # Note, also called directly in case it's overridden.
        
}