[v2,3/5] systemd: create preset files instead of installing in image

Submitted by Jonas Bonn on Jan. 2, 2019, 4:11 p.m. | Patch ID: 157505

Details

Message ID 20190102161144.11264-4-jonas@norrbonn.se
State Superseded
Headers show

Commit Message

Jonas Bonn Jan. 2, 2019, 4:11 p.m.
At first boot, systemd will create the /etc/systemd/system directory
from service preset files.  As such, for a normal, writable /etc
(writable rootfs), there is no need to set up this directory at image
creation time.

This patch changes the systemd machinery to create preset files and to
rely on systemd to do the service enablement.

This breaks the read-only-rootfs case; there's a fix for this in a
follow-up patch.

Signed-off-by: Jonas Bonn <jonas@norrbonn.se>
---
 meta/classes/systemd.bbclass | 33 ++++++++++++++++-----------------
 1 file changed, 16 insertions(+), 17 deletions(-)

Patch hide | download patch | download mbox

diff --git a/meta/classes/systemd.bbclass b/meta/classes/systemd.bbclass
index c7b784dea8..cf3b5de4a1 100644
--- a/meta/classes/systemd.bbclass
+++ b/meta/classes/systemd.bbclass
@@ -16,43 +16,32 @@  python __anonymous() {
     # from doing any work so that pure-systemd images don't have redundant init
     # files.
     if bb.utils.contains('DISTRO_FEATURES', 'systemd', True, False, d):
-        d.appendVar("DEPENDS", " systemd-systemctl-native")
-        d.appendVar("PACKAGE_WRITE_DEPS", " systemd-systemctl-native")
         if not bb.utils.contains('DISTRO_FEATURES', 'sysvinit', True, False, d):
             d.setVar("INHIBIT_UPDATERCD_BBCLASS", "1")
 }
 
 systemd_postinst() {
-OPTS=""
-
 if [ -n "$D" ]; then
-    OPTS="--root=$D"
+	exit 0
 fi
 
 if type systemctl >/dev/null 2>/dev/null; then
-	if [ -z "$D" ]; then
-		systemctl daemon-reload
-	fi
-
-	systemctl $OPTS ${SYSTEMD_AUTO_ENABLE} ${SYSTEMD_SERVICE_ESCAPED}
+	systemctl daemon-reload
+	systemctl preset ${SYSTEMD_SERVICE_ESCAPED}
 
-	if [ -z "$D" -a "${SYSTEMD_AUTO_ENABLE}" = "enable" ]; then
+	if [ "${SYSTEMD_AUTO_ENABLE}" = "enable" ]; then
 		systemctl --no-block restart ${SYSTEMD_SERVICE_ESCAPED}
 	fi
 fi
 }
 
 systemd_prerm() {
-OPTS=""
-
 if [ -n "$D" ]; then
-    OPTS="--root=$D"
+	exit 0
 fi
 
 if type systemctl >/dev/null 2>/dev/null; then
-	if [ -z "$D" ]; then
-		systemctl stop ${SYSTEMD_SERVICE_ESCAPED}
-	fi
+	systemctl stop ${SYSTEMD_SERVICE_ESCAPED}
 
 	systemctl $OPTS disable ${SYSTEMD_SERVICE_ESCAPED}
 fi
@@ -177,12 +166,22 @@  python systemd_populate_packages() {
                 else:
                     bb.fatal("SYSTEMD_SERVICE_%s value %s does not exist" % (pkg_systemd, service))
 
+    def systemd_create_presets(pkg):
+        action = get_package_var(d, 'SYSTEMD_AUTO_ENABLE', pkg)
+        presetf = oe.path.join(d.getVar("PKGD"), "/lib/systemd/system-preset/98-%s.preset" % pkg)
+        bb.utils.mkdirhier(os.path.dirname(presetf))
+        with open(presetf, 'a') as fd:
+            for service in d.getVar('SYSTEMD_SERVICE_%s' % pkg).split():
+                fd.write("%s %s\n" % (action,service))
+        d.appendVar("FILES_%s" % pkg, " /lib/systemd/system-preset/98-%s.preset" % pkg)
+
     # Run all modifications once when creating package
     if os.path.exists(d.getVar("D")):
         for pkg in d.getVar('SYSTEMD_PACKAGES').split():
             systemd_check_package(pkg)
             if d.getVar('SYSTEMD_SERVICE_' + pkg):
                 systemd_generate_package_scripts(pkg)
+                systemd_create_presets(pkg)
         systemd_check_services()
 }
 

Comments

Peter Kjellerstedt Jan. 7, 2019, 4:43 p.m.
> -----Original Message-----
> From: openembedded-core-bounces@lists.openembedded.org <openembedded-
> core-bounces@lists.openembedded.org> On Behalf Of Jonas Bonn
> Sent: den 2 januari 2019 17:12
> To: openembedded-core@lists.openembedded.org
> Subject: [OE-core] [PATCH v2 3/5] systemd: create preset files instead
> of installing in image
> 
> At first boot, systemd will create the /etc/systemd/system directory
> from service preset files.  As such, for a normal, writable /etc
> (writable rootfs), there is no need to set up this directory at image
> creation time.
> 
> This patch changes the systemd machinery to create preset files and to
> rely on systemd to do the service enablement.
> 
> This breaks the read-only-rootfs case; there's a fix for this in a
> follow-up patch.
> 
> Signed-off-by: Jonas Bonn <jonas@norrbonn.se>
> ---
>  meta/classes/systemd.bbclass | 33 ++++++++++++++++-----------------
>  1 file changed, 16 insertions(+), 17 deletions(-)
> 
> diff --git a/meta/classes/systemd.bbclass
> b/meta/classes/systemd.bbclass
> index c7b784dea8..cf3b5de4a1 100644
> --- a/meta/classes/systemd.bbclass
> +++ b/meta/classes/systemd.bbclass
> @@ -16,43 +16,32 @@ python __anonymous() {
>      # from doing any work so that pure-systemd images don't have redundant init
>      # files.
>      if bb.utils.contains('DISTRO_FEATURES', 'systemd', True, False, d):
> -        d.appendVar("DEPENDS", " systemd-systemctl-native")
> -        d.appendVar("PACKAGE_WRITE_DEPS", " systemd-systemctl-native")
>          if not bb.utils.contains('DISTRO_FEATURES', 'sysvinit', True, False, d):
>              d.setVar("INHIBIT_UPDATERCD_BBCLASS", "1")
>  }
> 
>  systemd_postinst() {
> -OPTS=""
> -
>  if [ -n "$D" ]; then
> -    OPTS="--root=$D"
> +	exit 0
>  fi
> 
>  if type systemctl >/dev/null 2>/dev/null; then
> -	if [ -z "$D" ]; then
> -		systemctl daemon-reload
> -	fi
> -
> -	systemctl $OPTS ${SYSTEMD_AUTO_ENABLE} ${SYSTEMD_SERVICE_ESCAPED}
> +	systemctl daemon-reload
> +	systemctl preset ${SYSTEMD_SERVICE_ESCAPED}
> 
> -	if [ -z "$D" -a "${SYSTEMD_AUTO_ENABLE}" = "enable" ]; then
> +	if [ "${SYSTEMD_AUTO_ENABLE}" = "enable" ]; then
>  		systemctl --no-block restart ${SYSTEMD_SERVICE_ESCAPED}
>  	fi
>  fi
>  }
> 
>  systemd_prerm() {
> -OPTS=""
> -
>  if [ -n "$D" ]; then
> -    OPTS="--root=$D"
> +	exit 0
>  fi
> 
>  if type systemctl >/dev/null 2>/dev/null; then
> -	if [ -z "$D" ]; then
> -		systemctl stop ${SYSTEMD_SERVICE_ESCAPED}
> -	fi
> +	systemctl stop ${SYSTEMD_SERVICE_ESCAPED}
> 
>  	systemctl $OPTS disable ${SYSTEMD_SERVICE_ESCAPED}
>  fi
> @@ -177,12 +166,22 @@ python systemd_populate_packages() {
>                  else:
>                      bb.fatal("SYSTEMD_SERVICE_%s value %s does not exist" % (pkg_systemd, service))
> 
> +    def systemd_create_presets(pkg):
> +        action = get_package_var(d, 'SYSTEMD_AUTO_ENABLE', pkg)
> +        presetf = oe.path.join(d.getVar("PKGD"), "/lib/systemd/system-preset/98-%s.preset" % pkg)

Using "/lib" above does not take into account the usrmerge distro 
feature. My suggestion is to use ${nonarch_base_libdir} instead of 
/lib, or maybe even better is to use ${systemd_unitdir} instead of 
/lib/systemd, i.e.:

        presetf = oe.path.join(d.getVar("PKGD"), d.getVar("systemd_unitdir "), "/system-preset/98-%s.preset" % pkg)

> +        bb.utils.mkdirhier(os.path.dirname(presetf))
> +        with open(presetf, 'a') as fd:
> +            for service in d.getVar('SYSTEMD_SERVICE_%s' % pkg).split():
> +                fd.write("%s %s\n" % (action,service))
> +        d.appendVar("FILES_%s" % pkg, " /lib/systemd/system-preset/98-%s.preset" % pkg)

Same here. Use "${systemd_unitdir}" instead of "/lib/systemd".

> +
>      # Run all modifications once when creating package
>      if os.path.exists(d.getVar("D")):
>          for pkg in d.getVar('SYSTEMD_PACKAGES').split():
>              systemd_check_package(pkg)
>              if d.getVar('SYSTEMD_SERVICE_' + pkg):
>                  systemd_generate_package_scripts(pkg)
> +                systemd_create_presets(pkg)
>          systemd_check_services()
>  }
> 
> --
> 2.19.1

//Peter
Jonas Bonn Jan. 8, 2019, 2:58 p.m.
Hi Peter,


On 07/01/2019 17:43, Peter Kjellerstedt wrote:
>> -----Original Message-----
>> From: openembedded-core-bounces@lists.openembedded.org <openembedded-
>> core-bounces@lists.openembedded.org> On Behalf Of Jonas Bonn
>>
>> +    def systemd_create_presets(pkg):
>> +        action = get_package_var(d, 'SYSTEMD_AUTO_ENABLE', pkg)
>> +        presetf = oe.path.join(d.getVar("PKGD"), "/lib/systemd/system-preset/98-%s.preset" % pkg)
> 
> Using "/lib" above does not take into account the usrmerge distro
> feature. My suggestion is to use ${nonarch_base_libdir} instead of
> /lib, or maybe even better is to use ${systemd_unitdir} instead of
> /lib/systemd, i.e.:

Yes.  I already had this fixed up locally but using ${systemd_unitdir} 
is smarter than what I've got so I'll do it that way before resubmitting.

Thanks for the review!
/Jonas
Jonas Bonn Jan. 27, 2019, 8:39 p.m.
This is a system generated Comment: Patch 157505 was automatically marked as superseded by patch 158258.