Init scripts: generalize config file setup

This commit is contained in:
Calin Crisan 2019-01-11 11:43:22 +02:00
parent e78a2de15b
commit ace9bdcf2b

View File

@ -8,6 +8,7 @@ test -n "$os_debug" || source /etc/init.d/conf
source /etc/init.d/panic
msg_begin() {
echo -n " * $1: "
}
@ -24,3 +25,35 @@ msg_background() {
test -n "$1" && echo $1 || echo "pending"
}
prepare_conf() {
# $1 - actual config file
# $2 - system-provided config file
# $3 - user-provided config file
# long story short:
# * user conf file takes precedence, if present
# * system conf file is used by default, if actual file absent
actual_conf="$1"
system_conf="$2"
user_conf="$3"
if [[ -n "$user_conf" && -f "$user_conf" ]]; then
cp -f "$user_conf" "$actual_conf"
grep -E "/boot .*ro[\s,]" /proc/mounts
RO=$?
test $RO == 0 && mount -o remount,rw /boot
rm -f $user_conf
test $RO == 0 && mount -o remount,ro /boot
fi
if [[ ! -f "$actual_conf" && -f "$system_conf" ]]; then
cp "$system_conf" "$actual_conf"
fi
# we want only Unix newlines in conf files
if [[ -f "$actual_conf" ]]; then
sed -i 's/\r//g' "$actual_conf"
fi
}