#!/bin/sh
# /etc/initramfs-tools/scripts/init-bottom/overlay

PREREQ=""
prereqs()
{
    echo "$PREREQ"
}

case $1 in
    prereqs)
        prereqs
        exit 0
        ;;
esac

. /scripts/functions

USERDATA="/dev/data_partition"

log_begin_msg "[SBOS_OVERLAYFS:START] --- Starting overlayfs mounting process --- \n"

# Should we run at all?

OVERLAY="no"
for arg in $(cat /proc/cmdline)
do
    case "$arg" in
        overlay=*)
            OVERLAY=${arg#overlay=}
            ;;
        *)
            ;;
    esac
done

if [ "$OVERLAY" != "yes" ]
then
    log_warning_msg "[SBOS_OVERLAYFS] Overlay not enabled, leaving the root fs alone"
    log_begin_msg "\n[SBOS_OVERLAYFS:END] --- Exited overlayfs mounting process ---"
    exit 0
fi

log_begin_msg "[SBOS_OVERLAYFS] Remounting root fs in overlay mode"

if ! modprobe -qb overlay
then
    log_failure_msg "[SBOS_OVERLAYFS] Cannot load overlay module"
    log_begin_msg "\n[SBOS_OVERLAYFS:END] --- Exited overlayfs mounting process ---"
    exit 0
fi

# initially we have:

#  /           initramfs
#  ${rootmnt}  readonly-mounted real root FS  (mounted from device ${ROOT})
#
# We want:
#
#  /                      initramfs
#  ${rootmnt}             overlayfs using ${rootmnt}/system and ${rootmnt}/user
#  ${rootmnt}/system      readonly-mounted real root FS  (mounted from device ${ROOT})
#  ${rootmnt}/user        consistent file system used for user data
#  ${rootmnt}/user/upper  overlayfs upper dir
#  ${rootmnt}/user/work   overlayfs work dir
#

# create and mount /system and /user on the initramfs, we will move them later
for dir in /system /user
do
  [ -d "${dir}" ] || mkdir -p "${dir}"
  if [ $? -ne 0 ]
  then
    log_failure_msg "[SBOS_OVERLAYFS] Cannot create ${dir}"
    log_begin_msg "\n[SBOS_OVERLAYFS:END] --- Exited overlayfs mounting process ---"
    exit 0
  fi
done


log_begin_msg "[SBOS_OVERLAYFS:DEBUG] --- DEBUG MODE --- \n"

log_begin_msg "[SBOS_OVERLAYFS:CMD] --- $ ls / ---"
ls /

log_begin_msg "[SBOS_OVERLAYFS:CMD] --- $ ls /system ---"
ls /system

log_begin_msg "[SBOS_OVERLAYFS:CMD] --- $ ls /user ---"
ls /user

log_begin_msg "[SBOS_OVERLAYFS:CMD] --- $ lsblk ---"
lsblk



# set up /user
if ! mount ${USERDATA} /user
then
    log_failure_msg "[SBOS_OVERLAYFS] Cannot mount user data (${USERDATA}) on /user"
    log_begin_msg "\n[SBOS_OVERLAYFS:END] --- Exited overlayfs mounting process ---"
    exit 0
fi

for dir in /user/upper /user/work
do
  if ! mkdir -p ${dir}
  then
      log_failure_msg "[SBOS_OVERLAYFS] overlay: Cannot create ${dir}"
      log_begin_msg "\n[SBOS_OVERLAYFS:END] --- Exited overlayfs mounting process ---"
      exit 0
  fi
done

# set up /syytem
if ! mount -o move "${rootmnt}" /system
then
    log_failure_msg "[SBOS_OVERLAYFS] Cannot move root fs to /system"
    log_begin_msg "\n[SBOS_OVERLAYFS:END] --- Exited overlayfs mounting process ---"
    exit 0
fi

# set up an overlayfs on rootmnt
if ! mount -t overlay -o lowerdir=/system,upperdir=/user/upper,workdir=/user/work rootfs "${rootmnt}"
then
    log_failure_msg "[SBOS_OVERLAYFS] can't move root fs to /system"

    # try to recover
    if ! mount -o move /system "${rootmnt}"
    then
        panic "[SBOS_OVERLAYFS] Recovering the old root fs failed, panicking"
        log_begin_msg "\n[SBOS_OVERLAYFS:END] --- Exited overlayfs mounting process ---"
        exit 0
    fi

    log_warning_msg "[SBOS_OVERLAYFS] overlay: moved regular rootfs back into place and continuing"
    log_begin_msg "\n[SBOS_OVERLAYFS:END] --- Exited overlayfs mounting process ---"
    exit 0
fi

for dir in "${rootmnt}/system" "${rootmnt}/user"
do
  [ -d "${dir}" ] || mkdir -p "${dir}"
  if [ $? -ne 0 ]
  then
    log_failure_msg "[SBOS_OVERLAYFS] Cannot create ${dir}"
    log_begin_msg "\n[SBOS_OVERLAYFS:END] --- Exited overlayfs mounting process ---"
    exit 0
  fi
done

# move /system and /user into place so we can access them later
if ! mount -o move /system "${rootmnt}/system"
then
    log_failure_msg "[SBOS_OVERLAYFS] Cannot move /system to ${rootmnt}/system"
    log_begin_msg "\n[SBOS_OVERLAYFS:END] --- Exited overlayfs mounting process ---"
    exit 0
fi

if ! mount -o move /user "${rootmnt}/user"
then
    log_failure_msg "[SBOS_OVERLAYFS] Cannot move /user to ${rootmnt}/user"
    log_begin_msg "\n[SBOS_OVERLAYFS:END] --- Exited overlayfs mounting process ---"
    exit 0
fi

# populate fstab on the new root

{
    cat <<EOF
# overlayfs mount is in use; changes to the root FS are
# written to tmpfs and will be discarded on reboot.
#
# to disable, pass "overlay=no" on the kernel command line
# (in /boot/cmdline.txt)
#
# the underlying root FS is mounted readonly on /system
# changes are written to /user

EOF

    {
        while read dev dir fstype fsopts freq pass
        do
            if [ "${dir}" = "${rootmnt}" ]
            then
                echo "${dev} / ${fstype} ${fsopts} ${freq} ${pass}"
            fi
        done
    } </proc/mounts

    echo "# original fstab follows"

    {
        while read dev dir fstype fsopts freq pass
        do
            if [ "${dir}" != "/" ]
            then
                echo "${dev} ${dir} ${fstype} ${fsopts} ${freq} ${pass}"
            fi
        done
    } <"${rootmnt}/system/etc/fstab"
} >"${rootmnt}/etc/fstab"

log_end_msg
exit 0
