#!/bin/bash
# SPDX-License-Identifier: MIT
# SPDX-FileCopyrightText: Copyright 2024 SUSE LLC
set -e
shopt -s nullglob

unset "${!LC_@}"
LANG="C.utf8"
export LANG

log_info()
{
	echo "$@"
}

err()
{
	echo "Error: $*" >&2
	echo "Error: $*"
	exit 1
}

####### main #######

[ -e /etc/initrd-release ] || err "No initrd environment"

# Need to write to /sysroot/etc/fstab to add new partitions
if ! [ -w /sysroot/etc ]; then
        # Transactional systems have a separate mount for /etc.
        # In the nested subvolume setup there's a bind mount that is read-only at first.
        if findmnt /sysroot/etc >/dev/null; then
                mount -o remount,rw /sysroot/etc
        else
                mount -o remount,rw /sysroot
        fi
fi

# Delete the systemd-repart marker in /sysroot/etc/fstab, or systemd-repart
# can remove entries it should not
sed -i -e 's|# .*section.*of automatically generated fstab by systemd-repart||g' /sysroot/etc/fstab

log_info "Reparting with systemd-repart:"
/usr/bin/systemd-repart --dry-run=no --append-fstab=auto --generate-fstab=/etc/fstab
log_info "Repartition completed"
