summaryrefslogblamecommitdiffstats
path: root/builder/modules.d/slx-addons/scripts/setup-addons.sh
blob: 8232a9c76bfc1893e92716bebb7ea67f9984c9a8 (plain) (tree)


































                                                                                


                                                                           

                                                                   




                                                                                 


               
                              




                                                            
                                            
                                                                                            

                        

                             
 



                                                           

            

                                                                

                                                                 
                                   
                                                  

                                  



                        
         

                                                  
                                  
                                                         


          




                                                                         

                                                               



                                                                    
                                                                                        


                                
 
#!/usr/bin/env bash
#
# Stage4 addons setup script
#
###############################################################################
# Support stage4 addons residing under '/opt/openslx/addons'.
# This script will loop over all addons found, in the form of
# directories containing directory structures relative to '/'
#
# Additionally addons are expected to provide:
# * <addon_dir>/addon-required -> script to check whether the
#   addon should be installed (exit 0) or not (exit 1).
#
# * <addon_dir>/opt/openslx/etc/<addon_name>.whiteout -> list of files
#   that were removed during the addon installation and thus need to be
#   removed after the addon was installed.
#
# * <addon_dir>/opt/openslx/etc/<addon_name> ld.so.cache -> ld cache containing
#   the newly installed libraries. CAVE: multiple addons -> ld cache combination
#
### CURRENTLY NOT EXECUTED
# * <addon_dir>/addon-init -> script that will be automatically
#   be executed after the addon was installed.
#   NOTE: question remains if this should be done within stage3
#         or by chroot'ing into the stage4 before executing it.
### CURRENTLY NOT EXECUTED: Since whiteouts could be handled for _all_addons

type emergency_shell >/dev/null 2>&1 || . /lib/dracut-lib.sh

# Check if we even have any addons to process
if [ ! -d "$NEWROOT/opt/openslx/addons" ]; then
	info "No stage4 addons found."
	return 0
fi

# This just activates the ldconfig service to run during the sysinit target
# Since addons are likely to install libs, this is necessary to garantee
# that the live system "sees" the libraries.
activate_stage4_ldconfig() {
	local service_path="/opt/openslx/services/ldconfig.service"
	[ -e "$service_path" ] || return 1
	local target_dir="${NEWROOT}/etc/systemd/system/sysinit.target.wants"
	mkdir -p "$target_dir"
	cp -f "$service_path" "${NEWROOT}/etc/systemd/system/${service_path##*/}"
	ln -sf "../${service_path##*/}" "${target_dir}/${service_path##*/}"
}

setup_addon() {
	if [ ! -d "$1" ]; then
		warn "Given '$1' not a directory, skipping."
		return 1
	fi
	local addon_dir="$1"
	cd "$addon_dir"
	if ! bash addon-required 2>&1 ; then
		info "'$addon_dir/addon-required' missing or returned non-zero, skipping..."
		return 1
	fi
	# purge addon-* files
	rm -f -- addon-*

	# move all the files over
	for entry in $(find * -not -type d 2>/dev/null); do
		mkdir -p "${NEWROOT}/${entry%/*}"
		mv -f -- "$entry" "${NEWROOT}/${entry}"
	done

	# post merge: remove files marked as whiteouts
	# (e.g. they were removed during the addon installation)
	for WHITEOUT in "$NEWROOT/opt/openslx/etc/"*.whiteout; do
		[ -e "$WHITEOUT" ] || continue
		while read line; do
			rm -f "${NEWROOT}/${line}"
		done < "$WHITEOUT"
	done

	cd - &>/dev/null
}

active=()
for addon in "${NEWROOT}/opt/openslx/addons/"*; do
	if setup_addon "$addon"; then
		active+=("$addon")
		info "Activated '$addon' (@ $(date +%s))"
	fi
done

# if only one addon was installed, use its pre-generated ld.so.cache
# if more than one were installed, make sure ldconfig is called in stage4
if [ "${#active[@]}" -eq 1 ]; then
	addon_cache="${NEWROOT}/opt/openslx/etc/${active[0]}.ld.so.cache"
	if [ -e "$addon_cache" ]; then
		info "Using ld.so.cache of '${active[0]}'."
		mv -f -- "${NEWROOT}/etc/ld.so.cache"{,.stage4}
		mv -f -- "$addon_cache" "${NEWROOT}/etc/ld.so.cache"
		exit 0
	fi
elif [ "${#active[@]}" -gt 1 ]; then
	info "Activating ldconfig in stage4 due to multiple loaded addons: ${active[@]}"
	activate_stage4_ldconfig
fi

: