[OE-core] [PATCH] systemd-systemctl: add handling of template unit files

Tomas Novotny tomas at novotny.cz
Fri Dec 12 15:50:51 UTC 2014


Template unit files (those with '@' in their names) are not handled with
native version of systemctl. This is usually not a problem, as the
native systemctl fails and systemctl command is executed during first
boot. But some early boot template units may fail during first boot
because opkg configure for first boot is pulled too late for them
(although I encouter it only with some of my services, not with oe-core
ones).

Handling of template unit files is same as in original systemctl. Also
DefaultInstance directive in template is respected. As with original
systemctl, enabling of template without instance and DefaultInstance
does nothing.

Signed-off-by: Tomas Novotny <tomas at novotny.cz>
---
 .../systemd/systemd-systemctl/systemctl            | 45 ++++++++++++++++++----
 1 file changed, 37 insertions(+), 8 deletions(-)

diff --git a/meta/recipes-core/systemd/systemd-systemctl/systemctl b/meta/recipes-core/systemd/systemd-systemctl/systemctl
index b37f27a..2e632b0 100755
--- a/meta/recipes-core/systemd/systemd-systemctl/systemctl
+++ b/meta/recipes-core/systemd/systemd-systemctl/systemctl
@@ -77,18 +77,31 @@ for service in $services; do
 		exit 0
 	fi
 
-	echo "Try to find location of $service..."
+	service_base_file=`echo $service | sed 's/\(@\).*\(\.[^.]\+\)/\1\2/'`
+	if [ -z `echo $service | sed '/@/p;d'` ]; then
+		echo "Try to find location of $service..."
+		service_template=false
+	else
+		echo "Try to find location of template $service_base_file of instance $service..."
+		service_template=true
+		if [ -z `echo $service | sed 's/^.\+@\(.*\)\.[^.]\+/\1/'` ]; then
+			instance_specified=false
+		else
+			instance_specified=true
+		fi
+	fi
+
 	# find service file
 	for p in $ROOT/etc/systemd/system \
 		 $ROOT/lib/systemd/system \
 		 $ROOT/usr/lib/systemd/system; do
-		if [ -e $p/$service ]; then
-			service_file=$p/$service
+		if [ -e $p/$service_base_file ]; then
+			service_file=$p/$service_base_file
 			service_file=${service_file##$ROOT}
 		fi
 	done
 	if [ -z "$service_file" ]; then
-		echo "'$service' couldn't be found; exiting with error"
+		echo "'$service_base_file' couldn't be found; exiting with error"
 		exit 1
 	fi
 	echo "Found $service in $service_file"
@@ -115,13 +128,29 @@ for service in $services; do
 	for r in $wanted_by; do
 		echo "WantedBy=$r found in $service"
 		if [ "$action" = "enable" ]; then
+			enable_service=$service
+			if [ "$service_template" = true -a "$instance_specified" = false ]; then
+				default_instance=$(sed '/^DefaultInstance[[:space:]]*=/s,[^=]*=,,p;d' "$ROOT/$service_file")
+				if [ -z $default_instance ]; then
+					echo "Template unit without instance or DefaultInstance directive, nothing to enable"
+					continue
+				else
+					echo "Found DefaultInstance $default_instance, enabling it"
+					enable_service=$(echo $service | sed "s/@/@$default_instance/")
+				fi
+			fi
 			mkdir -p $ROOT/etc/systemd/system/$r.wants
-			ln -s $service_file $ROOT/etc/systemd/system/$r.wants
-			echo "Enabled $service for $wanted_by."
+			ln -s $service_file $ROOT/etc/systemd/system/$r.wants/$enable_service
+			echo "Enabled $enable_service for $wanted_by."
 		else
-			rm -f $ROOT/etc/systemd/system/$r.wants/$service
+			if [ "$service_template" = true -a "$instance_specified" = false ]; then
+				disable_service="$ROOT/etc/systemd/system/$r.wants/`echo $service | sed 's/@/@*/'`"
+			else
+				disable_service="$ROOT/etc/systemd/system/$r.wants/$service"
+			fi
+			rm -f $disable_service
 			rmdir --ignore-fail-on-non-empty -p $ROOT/etc/systemd/system/$r.wants
-			echo "Disabled $service for $wanted_by."
+			echo "Disabled ${disable_service##$ROOT/etc/systemd/system/$r.wants/} for $wanted_by."
 		fi
 	done
 
-- 
1.8.3.1




More information about the Openembedded-core mailing list