#!/bin/sh

#----- Copyright & License -----
#
# Copyright (C) 2018-2019 AT&T Intellectual Property.
# All Rights Reserved.
#
# Copyright (c) 2015-2017 by Brocade Communications Systems, Inc.
# All rights reserved.
#
# SPDX-License-Identifier: GPL-2.0-only
#
#----- Copyright & License -----

#
# This needs to run *AFTER* 9990-aaa-fixme.sh as long as that script is
# unconditionally setting the default for LIVE_MEDIA_PATH or we have a proper
# way to hook into commandline parsing.
#
# This needs to run *AFTER* 9990-netbase.sh! It is called after the initramfs
# mounted the rootfs.
#

Netbase ()
{
    set -- ${LIVE_BOOT_CMDLINE}
    for _PARAMETER in "$@"
    do
	case "${_PARAMETER}" in
	    cloud-init)
		log_begin_msg "Configuring cloud-init (DHCP)"

		do_netsetup

		log_end_msg
		;;

	    ip=dhcp)
		log_begin_msg "Configuring DHCP"

		do_netsetup

		log_end_msg
		;;
	esac
    done
}

# DANOS keeps its persistence directory on the same partition that holds the
# root filesystem: /boot/<image>/<image>.squashfs and /boot/<image>/persistence
# sit side by side on one ext4 partition, and vyatta-union= names that
# directory. live-boot's persistence scan is built on the opposite assumption.
#
# find_persistence_media() builds a list of the devices that "are hosting the
# actual live rootfs" and refuses to look for persistence on them, to avoid
# mounting a parent directory on top of a sub-directory of the same filesystem
# in one union. In live-boot 4.x that list was accidentally always empty -- the
# loop passed the literal string "d" to what_is_mounted_on instead of "$d" -- so
# the guard never fired and this layout worked by luck. Current live-boot fixed
# the quoting and added /run/live/medium, so the guard now fires on the DANOS
# partition, the scan finds nothing, and the system runs on live-boot's
# in-memory overlay: the account the installer wrote to disk is invisible,
# live-config recreates tmpuser in its place, and nothing done afterwards
# survives a reboot.
#
# The guard is not wrong in general. What it protects against does not happen
# here: the DANOS overlay is assembled from separate directories on that
# partition and never mounts a parent over one of its own children. So the
# exemption is scoped to the one case where DANOS has said so -- vyatta-union=
# on the kernel command line, which sets VYATTA_UNION -- and every other boot,
# a live ISO included, keeps the stock behaviour.
#
# storage_devices() has exactly one caller, find_persistence_media(), so
# wrapping it here (this file is sourced after 9990-misc-helpers.sh) changes
# nothing about how the root filesystem itself is located.
Vyatta_storage_devices ()
{
    # Same scan as the stock function with the first argument -- the devices
    # hosting the live rootfs -- ignored. The white list is still honoured.
    white_listed_devices="${2}"

    for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "loop|ram|fd")
    do
        fulldevname=$(sys2dev "${sysblock}")

        if [ -n "${white_listed_devices}" ] && \
           ! is_in_space_sep_list ${fulldevname} ${white_listed_devices}
        then
            continue
        fi

        for dev in $(subdevices "${sysblock}")
        do
            echo "$(sys2dev "${dev}")"
        done
    done
}

Vyatta_union_storage ()
{
    if [ -n "${VYATTA_UNION}" ]
    then
        storage_devices () { Vyatta_storage_devices "$@"; }
    fi
}

Cmdline_vyatta ()
{
    # since this is executed when sourced (before 9990-main.sh)
    # we read the kernel cmdline directly
    set -- $(cat /proc/cmdline)
    for _PARAMETER in "$@"
    do
	case "${_PARAMETER}" in
	    vyatta-union=*)
		LIVE_MEDIA_PATH="${_PARAMETER#vyatta-union=}"
		PERSISTENCE_PATH="${LIVE_MEDIA_PATH}"
		PERSISTENCE_STORAGE="directory"
		PERSISTENCE="Yes"
		VYATTA_UNION="Yes"
		export LIVE_MEDIA_PATH PERSISTENCE_PATH PERSISTENCE_STORAGE \
		    PERSISTENCE VYATTA_UNION
		;;
	esac
    done
}

Cmdline_vyatta
Vyatta_union_storage
