# 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

ostree_mount_root()
{
    local_mount_root

    local OSTREE
    for param in $(cat /proc/cmdline)
    do
        case "${param}" in
        persistent=*)
            if ! $PERSISTENT_UUID_EXISTS; then
                PERSISTENT="${param#persistent=}"
            fi
            ;;
        persistentflags=*)
            if ! $PERSISTENT_UUID_EXISTS; then
                PERSISTENT_FLAGS="${param#persistentflags=}"
            fi
            ;;
        ostree=*)
            OSTREE="${param#ostree=}"
            ;;
        disable_persistent_flags=*)
            DISABLE_PERSISTENT_FLAGS="${param#disable_persistent_flags=}"
            ;;
        esac
    done

    mount -o remount,rw "${rootmnt?}"

    if [ -n "${PERSISTENT}" ]; then
        # mount persistent partition when it is existing
        # Sometimes device-mapper is the last item, so we need to manually select the last item to prioritize device-mapper.
        real_dev=$(blkid -t "${PERSISTENT}" -o device | tail -n 1)

        mkdir -p $rootmnt/persistent
        local fs_type=$(get_fstype $real_dev)
        if [ $fs_type = "unknown" ]; then
            fs_type="auto"
        fi

        local MOUNT_OPTS=""
        if [ -n "${PERSISTENT_FLAGS}" ] && [ $DISABLE_PERSISTENT_FLAGS -eq 0 ]; then
            MOUNT_OPTS="-o ${PERSISTENT_FLAGS}"
        fi

        mount -t $fs_type $MOUNT_OPTS $real_dev $rootmnt/persistent
        mount --make-private $rootmnt/persistent
    fi
    /usr/bin/deepin-mount-root.sh $rootmnt $OSTREE
    
    printf 'sysroot-ro\x00\x00\x00\x00\x00\x00\x01\x00\x62\x0b\x14' > /run/ostree-booted
}

mountroot()
{
    ostree_mount_root
}
