#!/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_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() { local tmp=$(mktemp) cat "$@" | awk -F: '!n[$1]++' > "$tmp" mv -f -- "$tmp" "$1" } for i in group shadow passwd; do [ -s "${ADDON_MOUNT_POINT}/etc/$i" ] && merguez "/etc/$i" "${ADDON_MOUNT_POINT}/etc/$i" done # 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 "$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 ldconfig 2> /dev/null || ldconfig.real 2> /dev/null fi exit 0