summaryrefslogblamecommitdiffstats
path: root/core/rootfs/rootfs-stage31/data/inc/activate_sysconfig
blob: b4bb5b656484849ad0397ff9cbfd33a3297c4c57 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11










                                                                         
                     


                                                                             
                                                                                  


                                                      
                                                                                    





                                                                                          
                    

                                                                                   
                                                                       






                                                                         
                     


                                                                                              
                                                         

                              
                       
                             


                         
                          
       

                
 

                                                                                                                    











                                                                                                  








                                                                                                                    

                                                                                   



                                                                                  








                                                                                    



















                                                                                                                                    









                                                              
                            
                                                                  










                                                                                 
                                                                   










                                                                       



                                                                                                  

  

                         





                                                                                                 

    
#!/bin/ash

echo "Configuring stage 3.2 ..."

# first a few variables
CONFIG="${FUTURE_ROOT}/opt/openslx/config"

#########################################################################
#
# This function downloads the config containing environment variables
#
fetch_text_config() {
	[ -e "$CONFIG" ] && grep -E '^#_RCONFIG_TAG$' "$CONFIG" > /dev/null \
		&& echo "Config already fetched." && return 0

	download "${SLX_BASE_PATH}/config$UUID_URL" "${CONFIG}-remote" || return 1

	echo "# Config fetched from $URL" >> "$CONFIG"
	echo "#_RCONFIG_TAG" >> "$CONFIG"
	CONFIG_DOWNLOAD_TIME=$(sed -r 's/^([0-9]+)\.([0-9]+).*$/\1\2/' /proc/uptime)
	cat "${CONFIG}-remote" >> "$CONFIG"
}
#########################################################################
#
# This function downloads the config.tgz and unpacks it to $TARGET_PATH <-- no it doesn't!
#
fetch_config_tgz() {
	[ -e "${CONFIG}.tgz" ] && echo "config.tgz already downloaded." && return 0

	download "${SLX_BASE_PATH}/config.tgz$UUID_URL" "${CONFIG}.tgz"
}


#########################################################################
#
# This function updates the downloaded config with the IP information
# received from /inc/setup_network <-- plus 500 other things
merge_text_config() {
	# sanity checks
	[ ! -e "${CONFIG}" ] && { echo "Cannot update. '$CONFIG' does not exist."; return 1; }

	# write IP and SERVER IP configuration to $CONFIG
cat >> "$CONFIG" <<HEREEND
# IP Config written in stage31
SLX_PXE_CLIENT_IP='$ip'
SLX_PXE_SERVER_IP='$SERVERIP'
SLX_PXE_GATEWAY='$router'
SLX_PXE_DNS='$dns'
SLX_PXE_MAC='$MAC'
SLX_SPLASH='$MUTED_OUTPUT'
HEREEND
	return 0
}

install_config_tgz() {
	[ ! -e "${CONFIG}.tgz" ] && { echo "Cannot install config.tgz. '${CONFIG}.tgz' does not exist."; return 1; }

	# setup hardware clock
	. "${CONFIG}"
	if [ "x$SLX_BIOS_CLOCK" == "xlocal" ]; then
		hwclock -s -l
	elif [ "x$SLX_BIOS_CLOCK" == "xutc" ]; then
		hwclock -s -u
	fi

	local TEMP_EXTRACT_DIR="/tmp/config.tgz.tmp"
	# TODO perserve existing directories permissions (but overwrite the permissions of files!)
	mkdir -p "${TEMP_EXTRACT_DIR}"
	# Recent versions of busybox tar bail out if two symlinks with the same name exist in archive
	if ! tar xf "${CONFIG}.tgz" -C "${TEMP_EXTRACT_DIR}" 2> /tmp/tarpit; then
		# Exit code is unclean...
		if ! [ -s /tmp/tarpit ] || grep -q -v 'create symlink' /tmp/tarpit; then
			# ...and we have en empty error log, OR any error that isn't "can't create symlink" -> abort
			echo "Could not untar ${CONFIG}.tgz to ${TEMP_EXTRACT_DIR}"
			return 1
		fi
	fi
	# Write list to stage32 for debug
	tar tvf "${CONFIG}.tgz" > "${FUTURE_ROOT}/opt/openslx/config.tgz.list" 2>&1
	chown -R 0:0 "${TEMP_EXTRACT_DIR}" 2>/dev/null
	cd "${TEMP_EXTRACT_DIR}"
	# first we look for local config.tgz files, which we merge with the common
	# config.tgz files
	if [ -n "${SLX_LOCAL_CONFIG}" ]; then
		local MOD
		for MOD in ${SLX_LOCAL_CONFIG}; do
			local LOCAL_CONFIG_DIR="openslx-configs/${MOD}"
			if [ -d "${LOCAL_CONFIG_DIR}" ]; then
				tarcopy "${LOCAL_CONFIG_DIR}" "${TEMP_EXTRACT_DIR}"
				echo "Merged local configuration files for '${MOD}'"
			fi
		done
	fi
	# purge openslx-configs/*
	rm -rf -- "openslx-configs/"
	# now just tarcopy them to future root
	tarcopy "${TEMP_EXTRACT_DIR}" "${FUTURE_ROOT}"
	# cleanup the downloaded stuff
	cd /
	rm -rf -- "${TEMP_EXTRACT_DIR}"
	[ $DEBUG -eq 0 ] && rm -f -- "${CONFIG}.tgz"
	# Display branding logo if splash screen is shown
	[ "x${MUTED_OUTPUT}" = "x1" ] && [ -e "${FUTURE_ROOT}/etc/branding.ppm" ] && fbsplash -s "${FUTURE_ROOT}/etc/branding.ppm" &
	return 0
}


#########################################################################
#
# MAIN PART
#

# Determine machine uuid, save for later
get-uuid
if [ -e "/run/system-uuid" ]; then
	UUID=$(cat "/run/system-uuid")
	if [ -n "$UUID" ]; then
		UUID_URL="?uuid=$UUID"
	fi
	cp "/run/system-uuid" "${FUTURE_ROOT}/etc/system-uuid"
fi

# Set up /opt/openslx/config
fetch_text_config || drop_shell "Could not download remote config"
if ! ash -n "${CONFIG}-remote"; then
	echo -e "\n\tFATAL: Could not download configuration!"
	echo -e "\tAborting boot since the system would be in an unusable state."
	echo -en "\tRebooting in 60 seconds"
	timeout=60
	while [ $(( timeout-- )) -gt 0 ]; do
		echo -n "."
		sleep 1
	done
	echo b > /proc/sysrq-trigger
fi
. "${CONFIG}-remote" || drop_shell "Could not source remote config"
merge_text_config || drop_shell "Could not update text based config"

# Fetch config.tgz if desired
if [ -z "$SLX_NO_CONFIG_TGZ" ]; then
	fetch_config_tgz || drop_shell "Could not download config.tgz"
	install_config_tgz || drop_shell "Could not extract config.tgz"
fi

# Change systemd target if desired
if [ -n "$SLX_SYSTEMD_TARGET" ]; then
	SLX_SYSTEMD_TARGET="${SLX_SYSTEMD_TARGET%.target}.target"
	for dir in etc lib usr/lib; do
		mkdir -p "${FUTURE_ROOT}/${dir}/systemd/system"
		ln -sf "$SLX_SYSTEMD_TARGET" "${FUTURE_ROOT}/${dir}/systemd/system/default.target"
	done
fi

run_hooks post-config-tgz

# Activate jumbo frames if requested
(
	. /run/network.conf
	[ -n "$IFACE" ] && [ "x$SLX_JUMBO_FRAMES" = "xyes" ] && ip link set dev "$IFACE" mtu 9000
)

true