cbe08f
From c539cdaf0348e2a52f0947d018b3d32fc63dc1f4 Mon Sep 17 00:00:00 2001
cbe08f
From: Emanuele Giuseppe Esposito <eesposit@redhat.com>
cbe08f
Date: Tue, 15 Jun 2021 14:01:12 +0200
cbe08f
Subject: [PATCH] Fix unit failure of cloud-final.service if NetworkManager was
cbe08f
 not present.
cbe08f
cbe08f
RH-Author: Emanuele Giuseppe Esposito <eesposit@redhat.com>
cbe08f
RH-MergeRequest: 17: Fix unit failure of cloud-final.service if NetworkManager was not present.
cbe08f
RH-Commit: [1/1] 647305809fc533ef2c5b8e0e27c94a4f7c368728 (eesposit/cloud-init)
cbe08f
RH-Bugzilla: 1897616
cbe08f
RH-Acked-by: Eduardo Otubo <otubo@redhat.com>
cbe08f
RH-Acked-by: Vitaly Kuznetsov <vkuznets@redhat.com>
cbe08f
RH-Acked-by: Mohamed Gamal Morsy <mmorsy@redhat.com>
cbe08f
cbe08f
Branch: rhel-7.9
cbe08f
Brew: 37493950
cbe08f
cbe08f
commit d3889c4645a1319c3d677006164b618ee53f4c8b
cbe08f
Author: Eduardo Otubo <otubo@redhat.com>
cbe08f
Date:   Mon Dec 7 14:23:22 2020 +0100
cbe08f
cbe08f
    Fix unit failure of cloud-final.service if NetworkManager was not present.
cbe08f
cbe08f
    RH-Author: Eduardo Terrell Ferrari Otubo (eterrell)
cbe08f
    RH-MergeRequest: 27: Fix unit failure of cloud-final.service if NetworkManager was not present.
cbe08f
    RH-Commit: [1/1] 3c65a2cca140fff48df1ef32919e3cb035506a2b (eterrell/cloud-init)
cbe08f
    RH-Bugzilla: 1898943
cbe08f
cbe08f
    cloud-final.service would fail if NetworkManager was not installed.
cbe08f
cbe08f
    journal -u cloud-final.service would show:
cbe08f
cbe08f
       cloud-init[5328]: Cloud-init v. 19.4 finished at ...
cbe08f
       echo[5346]: try restart NetworkManager.service
cbe08f
       systemctl[5349]: Failed to reload-or-try-restart
cbe08f
           NetworkManager.service: Unit not found.
cbe08f
       systemd[1]: cloud-final.service: control process exited,
cbe08f
           code=exited status=5
cbe08f
       systemd[1]: Failed to start Execute cloud user/final scripts.
cbe08f
       systemd[1]: Unit cloud-final.service entered failed state.
cbe08f
       systemd[1]: cloud-final.service failed.
cbe08f
cbe08f
    The change here is to only attempt to restart NetworkManager if it is
cbe08f
    present, and its SubState is 'running'.
cbe08f
cbe08f
    The multi-line shell in a systemd unit is less than ideal, but I'm not
cbe08f
    aware of any other way of conditionally doing this.
cbe08f
cbe08f
    Note that both of 'try-reload-or-restart' and 'reload-or-try-restart'
cbe08f
    will fail if the service is not present.  So this would also affect rhel
cbe08f
    8 systems that do not use NetworkManager.
cbe08f
cbe08f
    Signed-off-by: Eduardo Otubo <otubo@redhat.com>
cbe08f
cbe08f
Signed-off-by: Emanuele Giuseppe Esposito <eesposit@redhat.com>
cbe08f
---
cbe08f
 rhel/systemd/cloud-final.service | 7 +++++--
cbe08f
 1 file changed, 5 insertions(+), 2 deletions(-)
cbe08f
cbe08f
diff --git a/rhel/systemd/cloud-final.service b/rhel/systemd/cloud-final.service
cbe08f
index 621d4f8c..9dad1051 100644
cbe08f
--- a/rhel/systemd/cloud-final.service
cbe08f
+++ b/rhel/systemd/cloud-final.service
cbe08f
@@ -11,9 +11,12 @@ ExecStart=/usr/bin/cloud-init modules --mode=final
cbe08f
 RemainAfterExit=yes
cbe08f
 TimeoutSec=0
cbe08f
 KillMode=process
cbe08f
-ExecStartPost=/bin/echo "try restart NetworkManager.service"
cbe08f
+# Restart NetworkManager if it is present and running.
cbe08f
 # TODO: try-reload-or-restart is available only on systemd >= 229
cbe08f
-ExecStartPost=/usr/bin/systemctl reload-or-try-restart NetworkManager.service
cbe08f
+ExecStartPost=/bin/sh -c 'u=NetworkManager.service; \
cbe08f
+ out=$(systemctl show --property=SubState $u) || exit; \
cbe08f
+ [ "$out" = "SubState=running" ] || exit 0; \
cbe08f
+ systemctl reload-or-try-restart $u'
cbe08f
 
cbe08f
 # Output needs to appear in instance console output
cbe08f
 StandardOutput=journal+console
cbe08f
-- 
cbe08f
2.27.0
cbe08f