blob: 91ec0d0b48d7601eb92b1b9e776cd9d96739e321 (
plain) (
tree)
|
|
#!/bin/ash
#
# Script to be called by systemd
#
# Downloads and appends addons per sqfs/aufs.
#
######################################################################################
#
# Two modes for this script:
# - without any arguments, it will just go through the list of addons to
# setup as given through the OpenSLX configuration file
# - with an argument, it will setup the addon given as $1
#
#
######################################################################################
# read global OpenSLX config
. /opt/openslx/config || { echo "Could not source config!"; exit 23; }
# read base slx servers from cmdline
BASE_MOUNT_POINT="/opt/openslx/mnt"
DOWNLOAD_DEST="/run/addons"
FINAL_DEST="/tmp/addons"
mkdir -p "$FINAL_DEST" || { echo "Failed to create $FINAL_DEST"; exit 1; }
######################################################################################
#
# NO ARGUMENTS
#
if [ $# -ne 1 ]; then
echo "No addon passed via command line. Pass exactly one addon."
exit 1
fi
######################################################################################
#
# WITH ARGUMENTS -> SETUP ADDON
#
ADDON="$1"
# check that is was properly downloaded
ADDON_PATH="${DOWNLOAD_DEST}/$(basename "$ADDON.sqfs")"
if [ ! -f "${ADDON_PATH}" ]; then
slxlog --echo "addon-setup-fnf" "Addon squashfs not found under: '${ADDON_PATH}'"
exit 1
fi
# do we have hdd tmp?
if grep -q -E '^/dev/\S+\s/tmp' '/proc/mounts'; then
# it's there, so move it to /tmp to save some ram
if mv -f "${ADDON_PATH}" "${FINAL_DEST}/$(basename "$ADDON.sqfs")"; then
ADDON_PATH="${FINAL_DEST}/$(basename "$ADDON.sqfs")"
else
slxlog --echo "addon-setup-mv" "Failed to move '$ADDON' from $DOWNLOAD_DEST to $FINAL_DEST. Keeping it in RAM"
fi
fi
# now mount it to $BASE_MOUNT_POINT/<addon-name>
ADDON_MOUNT_POINT="${BASE_MOUNT_POINT}/$(basename "$ADDON")"
mkdir -p "$ADDON_MOUNT_POINT"
mount -t squashfs -o ro "$ADDON_PATH" "$ADDON_MOUNT_POINT" || \
{ slxlog --echo "addon-setup-mount" "Failed to mount $ADDON_PATH."; exit 1; }
# Merge any passwd/group/... files
merguez() {
# checks for conflicts of entries and (uid|gid), assumes <entry>:x:<id>:... !
if [ "$1" = "--check-conflicts" ]; then
local cc=yes
shift
fi
local tmp="$(mktemp)"
cp -f -a -- "$1" "$tmp" # So we get the original perms/owner
if [ -n "$cc" ]; then
local errors="$(mktemp)"
cat "$@" | awk -F: '{ if ($3 in n) { if (n[$3] != $1 ) { print "Conflicting entries "$1" ("$3")" > "/dev/stderr" } } else { n[$3]=$1 ; print $0} }' 2>"$errors" > "$tmp"
if [ -s "$errors" ]; then
slxlog --sync --echo "addon-merge-usergroups" "Conflicting user/groups in ${ADDON}." "$errors"
fi
rm -f -- "$errors"
else
# just merguez!
cat "$@" | awk -F: '!n[$1]++' > "$tmp"
fi
mv -f -- "$tmp" "$1"
}
for i in group passwd; do
[ -s "${ADDON_MOUNT_POINT}/etc/$i" ] && merguez --check-conflicts "/etc/$i" "${ADDON_MOUNT_POINT}/etc/$i"
done
[ -s "${ADDON_MOUNT_POINT}/etc/shadow" ] && merguez "/etc/shadow" "${ADDON_MOUNT_POINT}/etc/shadow"
# now append it to /
echo "Appending ${ADDON_MOUNT_POINT} to /"
if ! mount -o "remount,ins:1:${ADDON_MOUNT_POINT}=rr" / ; then # ins:1 makes sure the addon is before stage32, after rw layer
slxlog --echo "addon-setup-aufs" "Failed to append ${ADDON_MOUNT_POINT} to the aufs. Cleaning up..."
umount -l "${ADDON_MOUNT_POINT}" || echo "Could not unmount ${ADDON_MOUNT_POINT}!"
exit 1
fi
# Run post-hook if available
if [ -x "$ADDON_MOUNT_POINT/addon-init" ]; then
echo "Running post-append hook"
"$ADDON_MOUNT_POINT/addon-init" || \
slxlog --echo "addon-setup-init" "Warning: Could not execute addon-init of $ADDON"
fi
if ! grep -q -F '/opt/openslx/mnt/stage4' '/proc/mounts'; then
echo "Running ldconfig"
ldconfig 2> /dev/null || ldconfig.real 2> /dev/null
fi
echo "Addon initialized."
exit 0
|