# Ostree mounting			-*- shell-script -*-
PERSISTENT_UUID_EXISTS=false
DISABLE_PERSISTENT_FLAGS=0

if [ -f "/conf/conf.d/persistent_uuid.conf" ]; then
    . /conf/conf.d/persistent_uuid.conf
    PERSISTENT_UUID_EXISTS=true
fi

if [ -f "/conf/conf.d/persistent_flags.conf" ]; then
    . /conf/conf.d/persistent_flags.conf
fi

. /scripts/functions

ostree_mount_root()
{
    local_mount_root

    for param in $(cat /proc/cmdline)
    do
        case "${param}" in
        root=*)
		    ROOT="${param#root=}"
		    ;;
        rootflags=*)
            ROOTFLAGS="-o ${param#rootflags=}"
            ;;
        persistent=*)
            if ! $PERSISTENT_UUID_EXISTS; then
                PERSISTENT="${param#persistent=}"
            fi
            ;;
        persistentflags=*)
            if ! $PERSISTENT_UUID_EXISTS; then
                PERSISTENT_FLAGS="${param#persistentflags=}"
            fi
            ;;
        disable_persistent_flags=*)
            DISABLE_PERSISTENT_FLAGS="${param#disable_persistent_flags=}"
            ;;
        esac
    done

    cmd_param="--root=$ROOT"
    if [ -n "$ROOTFLAGS" ];then
        cmd_param="$cmd_param --rootflags=$ROOTFLAGS"
    fi

    if [ -n "${PERSISTENT}" ]; then
        cmd_param="$cmd_param --persistent=$PERSISTENT"
        if [ -n "${PERSISTENT_FLAGS}" ] && [ $DISABLE_PERSISTENT_FLAGS -eq 0 ]; then
            cmd_param="$cmd_param --persistentflags=$PERSISTENT_FLAGS"
        fi
    fi

    # Enable kmsg logging so mount-root output is captured in the kernel
    # ring buffer (retrievable via dmesg after boot).  Append before the
    # mount-point positional arg so getopt doesn't depend on permutation.
    cmd_param="$cmd_param --kmsg $rootmnt"

    # deepin-immutable-writable 在initrd阶段会设置文件的xattr，因此需要加入到cap_file中来避免权限问题
    mount -t securityfs securityfs /sys/kernel/security
    if [ -f /sys/kernel/security/elfverify/cap_file ]; then
        echo -ne "/usr/bin/deepin-immutable-writable" > /sys/kernel/security/elfverify/cap_file
    fi

    # setup hooks
    hooks_dir=/usr/share/deepin-immutable-mount-root-hooks
    mkdir -p $hooks_dir/before_mount_usr_overlay
    cat > $hooks_dir/before_mount_usr_overlay/0-default << 'EOF'
#!/bin/sh
data_deploy_dir="$1"
lower_dir="$2"
upper_dir="$3"
if [ -f "$data_deploy_dir/merge_modification_flag" ];then
    echo "merge modification flag found"
    # timeout 10min to avoid merge modification stuck
    timeout 600 /usr/bin/deepin-immutable-init merge-modification \
        "$lower_dir" "$upper_dir" --deploy-dir "$data_deploy_dir" \
        || echo "merge modification failed" >&2
    rm -f "$data_deploy_dir/merge_modification_flag"
fi
EOF
    chmod 0755 $hooks_dir/before_mount_usr_overlay/0-default

    mkdir -p $hooks_dir/before_mount_var_overlay
    cat > $hooks_dir/before_mount_var_overlay/0-default << 'EOF'
#!/bin/sh
data_overlay_root="$1"
var_lower="$2"
if [ -f "$data_overlay_root/var_upper_confirm_rollback_flag" ];then
    echo "var_upper_confirm_rollback_flag found"
    # timeout 10min to avoid merge var upper stuck
    timeout 600 /usr/bin/deepin-immutable-init merge-var-upper "$var_lower" "$data_overlay_root/var-upper"

    rm -f "$data_overlay_root/var_upper_confirm_rollback_flag"
    rm -rf "$data_overlay_root/var-upper"
    rm -rf "$data_overlay_root/var-work"
fi
EOF
    chmod 0755 $hooks_dir/before_mount_var_overlay/0-default

    # shellcheck disable=SC2086
    if ! MOUNT_ROOT_HOOK_ENABLED=1 /usr/bin/deepin-immutable-mount-root $cmd_param; then
        panic "deepin-immutable-mount-root failed: $cmd_param"
    fi

    # Copy status.list and set permissions to read-only
    cp $rootmnt/persistent/ostree/data/status.list /run/deepin-immutable-booted-status.list
    chmod 0444 /run/deepin-immutable-booted-status.list

    printf 'sysroot-ro\x00\x00\x00\x00\x00\x00\x01\x00\x62\x0b\x14' > /run/ostree-booted
}

mountroot()
{
    ostree_mount_root
}
