Blob Blame History Raw
From d3889c4645a1319c3d677006164b618ee53f4c8b Mon Sep 17 00:00:00 2001
From: Eduardo Otubo <otubo@redhat.com>
Date: Mon, 7 Dec 2020 14:23:22 +0100
Subject: [PATCH 3/4] Fix unit failure of cloud-final.service if NetworkManager
 was not present.

RH-Author: Eduardo Terrell Ferrari Otubo (eterrell)
RH-MergeRequest: 27: Fix unit failure of cloud-final.service if NetworkManager was not present.
RH-Commit: [1/1] 3c65a2cca140fff48df1ef32919e3cb035506a2b (eterrell/cloud-init)
RH-Bugzilla: 1898943

cloud-final.service would fail if NetworkManager was not installed.

journal -u cloud-final.service would show:

   cloud-init[5328]: Cloud-init v. 19.4 finished at ...
   echo[5346]: try restart NetworkManager.service
   systemctl[5349]: Failed to reload-or-try-restart
       NetworkManager.service: Unit not found.
   systemd[1]: cloud-final.service: control process exited,
       code=exited status=5
   systemd[1]: Failed to start Execute cloud user/final scripts.
   systemd[1]: Unit cloud-final.service entered failed state.
   systemd[1]: cloud-final.service failed.

The change here is to only attempt to restart NetworkManager if it is
present, and its SubState is 'running'.

The multi-line shell in a systemd unit is less than ideal, but I'm not
aware of any other way of conditionally doing this.

Note that both of 'try-reload-or-restart' and 'reload-or-try-restart'
will fail if the service is not present.  So this would also affect rhel
8 systems that do not use NetworkManager.

Signed-off-by: Eduardo Otubo <otubo@redhat.com>
---
 rhel/systemd/cloud-final.service | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/rhel/systemd/cloud-final.service b/rhel/systemd/cloud-final.service
index 05add077..e281c0cf 100644
--- a/rhel/systemd/cloud-final.service
+++ b/rhel/systemd/cloud-final.service
@@ -11,8 +11,11 @@ ExecStart=/usr/bin/cloud-init modules --mode=final
 RemainAfterExit=yes
 TimeoutSec=0
 KillMode=process
-ExecStartPost=/bin/echo "trying to reload or restart NetworkManager.service"
-ExecStartPost=/usr/bin/systemctl try-reload-or-restart NetworkManager.service
+# Restart NetworkManager if it is present and running.
+ExecStartPost=/bin/sh -c 'u=NetworkManager.service; \
+ out=$(systemctl show --property=SubState $u) || exit; \
+ [ "$out" = "SubState=running" ] || exit 0; \
+ systemctl reload-or-try-restart $u'
 
 # Output needs to appear in instance console output
 StandardOutput=journal+console
-- 
2.18.4