#!/bin/sh -e
#
# Copyright (c) 2019 SUSE Linux GmbH, Nuernberg, Germany
#
# Add mountpoint for the /etc overlay and workaround for /var to /etc/fstab
#

# Already there?
if [ -e /etc/fstab ] && grep -qE '^/etc[[:space:]]+/etc[[:space:]]+none[[:space:]]+bind' /etc/fstab; then
	exit 0 # Do nothing
fi

# Migration from old system
echo "Migrating from /etc overlays to /etc subvolume..."
btrfs subvolume create /etc

# Add entry for /etc
sed -i '/^overlay \/etc /d' /etc/fstab
echo "/etc /etc none bind 0 0" >> /etc/fstab

# Make the /root subvolume available during ignition runs (boo#1161264)
gawk -i inplace '$2 == "/root" { $4 = $4",x-initrd.mount" } { print $0 }' /etc/fstab

exit 0
