64 lines
1.6 KiB
Bash
64 lines
1.6 KiB
Bash
#
|
|
# Settings and functions for shell scripts.
|
|
#
|
|
|
|
CONFIG_PARTITION=mmcblk0p2
|
|
CONFIG_MOUNTPOINT=/mnt/cfg
|
|
CONFIG_DIR=/var/lib/puzzlefw/cfg
|
|
|
|
# Lock the configuration files.
|
|
lock_config() {
|
|
|
|
# Make config directory in RAM filesystem.
|
|
mkdir -p $CONFIG_DIR
|
|
|
|
# Create or open lock file.
|
|
# The file remains open until the calling script ends.
|
|
exec 3>>${CONFIG_DIR}/lockfile
|
|
|
|
# Get exclusive lock on the file.
|
|
if ! flock -n 3 ; then
|
|
echo "ERROR: Configuration locked" >&2
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
# Read configuration files from the SD card.
|
|
# This is only used during boot.
|
|
read_config() {
|
|
|
|
# Mount config filesystem read-only.
|
|
mkdir -p $CONFIG_MOUNTPOINT
|
|
mount -t ext4 -r -o noatime,data=journal /dev/${CONFIG_PARTITION} $CONFIG_MOUNTPOINT || return 1
|
|
|
|
# Copy config files to RAM filesystem.
|
|
cp -a ${CONFIG_MOUNTPOINT}/*.conf $CONFIG_DIR
|
|
|
|
umount $CONFIG_MOUNTPOINT
|
|
}
|
|
|
|
# Write changed configuration files to the SD card.
|
|
# Parameters: names of config files to synchronize
|
|
sync_config() {
|
|
|
|
# Mount config filesystem.
|
|
mkdir -p $CONFIG_MOUNTPOINT
|
|
mount -t ext4 -o noatime,data=journal /dev/${CONFIG_PARTITION} $CONFIG_MOUNTPOINT || return 1
|
|
|
|
for fname in "$@" ; do
|
|
|
|
# Copy new version of file to SD card.
|
|
cp ${CONFIG_DIR}/${fname}.new $CONFIG_MOUNTPOINT
|
|
|
|
# Atomically replace the old file on the SD card.
|
|
mv ${CONFIG_MOUNTPOINT}/${fname}.new ${CONFIG_MOUNTPOINT}/${fname}
|
|
|
|
# Replace the old file in the RAM filesystem.
|
|
mv ${CONFIG_DIR}/${fname}.new ${CONFIG_DIR}/${fname}
|
|
done
|
|
|
|
umount $CONFIG_MOUNTPOINT
|
|
sync
|
|
}
|
|
|