#!/bin/sh

# This script is used to launch (busybox) init on WSL2 through network-setup process.
# The network-setup process starts the vm-switch and unshare as its sub processes. this
# is necessary since we need to do some mount namespace, since we store the data on the
# WSL shared mount (/mnt/wsl/rancher/desktop/) and that can have issues with
# lingering tmpfs mounts after we exit.  This means we need to run this script
# under unshare (to get a private mount namespace), and then we can mark various
# mount points as shared (for buildkit).  Kubelet will internally do some
# tmpfs mounts for volumes (secrets, etc.), which will stay private and go away
# once k3s exits, so that we can delete the data as necessary.

set -o errexit -o nounset -o xtrace

NETWORK_SETUP_LOG="${LOG_DIR}/network-setup.log"
VM_SWITCH_LOG="${LOG_DIR}/vm-switch.log"


if [ $$ -ne "1" ]; then
    # This is not running as PID 1; this means that this is a normal invocation
    # from WSL.
    exec /usr/local/bin/network-setup --logfile "$NETWORK_SETUP_LOG" \
    --vm-switch-path /usr/local/bin/vm-switch --vm-switch-logfile \
    "$VM_SWITCH_LOG" --unshare-arg "${0}"
fi

# Mark directories that we will need to bind mount as shared mounts.
(
    IFS=:
    for dir in / ${DISTRO_DATA_DIRS}; do
        mount --make-shared "${dir}"
    done
)

# Mount bpffs to allow containers to leverage bpf, and make both bpffs and
# cgroupfs shared mounts so the pods can mount them correctly.
mount bpffs -t bpf /sys/fs/bpf
mount --make-shared /sys/fs/bpf
mount --make-shared /sys/fs/cgroup

# Mount binfmt_misc to allow nerdctl to see which qemu-* handlers have been loaded.
# It will display a warning for foreign platforms if their handler seems missing.
mount -t binfmt_misc binfmt_misc /proc/sys/fs/binfmt_misc
mount --make-shared /proc/sys/fs/binfmt_misc

if [ -f /var/lib/resolv.conf ]; then
    ln -s -f /var/lib/resolv.conf /etc/resolv.conf
fi

# Run init (which never exits).
exec /sbin/init
