summaryrefslogtreecommitdiffstats
path: root/core/rootfs/rootfs-stage32/data/opt/openslx/scripts/systemd-setup_slx_addons
blob: d446583b9a2d2389c61e5eb78f56e920d1947932 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/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"
		echo "post-append hook completed"
	} &
fi
if ! grep -q -F '/opt/openslx/mnt/stage4' '/proc/mounts'; then
	echo "Running ldconfig"
	killall ldconfig ldconfig.real # Stop any running instance, we have new data now
	ldconfig 2> /dev/null || ldconfig.real 2> /dev/null
	echo "ldconfig completed"
fi

wait
echo "Addon initialized."
exit 0