604c1f
From 5c99ba05086b1ec83ce7e0c64edb4add4b47d923 Mon Sep 17 00:00:00 2001
604c1f
From: Emanuele Giuseppe Esposito <eesposit@redhat.com>
604c1f
Date: Thu, 19 May 2022 11:14:39 +0200
604c1f
Subject: [PATCH 3/4] Align rhel custom files with upstream (#1431)
604c1f
604c1f
RH-Author: Emanuele Giuseppe Esposito <eesposit@redhat.com>
604c1f
RH-MergeRequest: 65: Align rhel custom files with upstream (#1431)
604c1f
RH-Commit: [1/2] 5d9067175688b1006472a477b0916b81c73d5e07
604c1f
RH-Bugzilla: 2082071
604c1f
RH-Acked-by: Mohamed Gamal Morsy <mmorsy@redhat.com>
604c1f
RH-Acked-by: Eduardo Otubo <otubo@redhat.com>
604c1f
RH-Acked-by: Vitaly Kuznetsov <vkuznets@redhat.com>
604c1f
604c1f
commit 9624758f91b61f4711e8d7b5c83075b5d23e0c43
604c1f
Author: Emanuele Giuseppe Esposito <eesposit@redhat.com>
604c1f
Date:   Wed May 18 15:18:04 2022 +0200
604c1f
604c1f
    Align rhel custom files with upstream (#1431)
604c1f
604c1f
    So far RHEL had its own custom .service and cloud.cfg files,
604c1f
    that diverged from upstream. We always replaced the generated files
604c1f
    with the ones we had.
604c1f
604c1f
    This caused only confusion and made it harder to rebase and backport
604c1f
    patches targeting these files.
604c1f
    At the same time, we are going to delete our custom downstream-only files
604c1f
    and use the ones generated by .tmpl.
604c1f
604c1f
    The mapping is:
604c1f
    config/cloud.cfg.tmpl -> rhel/cloud.cfg
604c1f
    systemd/* -> rhel/systemd/*
604c1f
604c1f
    Such rhel-specific files are open and available in the Centos repo:
604c1f
    https://gitlab.com/redhat/centos-stream/src/cloud-init
604c1f
604c1f
    With this commit, we are also introducing modules in cloud.cfg that
604c1f
    were not in the default rhel cfg file, even though they should already
604c1f
    have been there with previous rebases and releases.
604c1f
    Anyways such modules support rhel as distro, and
604c1f
    therefore should cause no harm.
604c1f
604c1f
    Signed-off-by: Emanuele Giuseppe Esposito <eesposit@redhat.com>
604c1f
604c1f
    RHBZ: 2082071
604c1f
604c1f
Signed-off-by: Emanuele Giuseppe Esposito <eesposit@redhat.com>
604c1f
---
604c1f
 config/cloud.cfg.tmpl                   | 23 +++++++++++++++++++++++
604c1f
 systemd/cloud-config.service.tmpl       |  4 ++++
604c1f
 systemd/cloud-final.service.tmpl        | 13 +++++++++++++
604c1f
 systemd/cloud-init-local.service.tmpl   | 22 +++++++++++++++++++++-
604c1f
 systemd/cloud-init.service.tmpl         |  6 +++++-
604c1f
 tests/unittests/test_render_cloudcfg.py |  1 +
604c1f
 6 files changed, 67 insertions(+), 2 deletions(-)
604c1f
604c1f
diff --git a/config/cloud.cfg.tmpl b/config/cloud.cfg.tmpl
604c1f
index 86beee3c..f4d2fd14 100644
604c1f
--- a/config/cloud.cfg.tmpl
604c1f
+++ b/config/cloud.cfg.tmpl
604c1f
@@ -34,7 +34,11 @@ disable_root: true
604c1f
 
604c1f
 {% if variant in ["almalinux", "alpine", "amazon", "centos", "cloudlinux", "eurolinux",
604c1f
                   "fedora", "miraclelinux", "openEuler", "rhel", "rocky", "virtuozzo"] %}
604c1f
+{% if variant == "rhel" %}
604c1f
+mount_default_fields: [~, ~, 'auto', 'defaults,nofail,x-systemd.requires=cloud-init.service,_netdev', '0', '2']
604c1f
+{% else %}
604c1f
 mount_default_fields: [~, ~, 'auto', 'defaults,nofail', '0', '2']
604c1f
+{% endif %}
604c1f
 {% if variant == "amazon" %}
604c1f
 resize_rootfs: noblock
604c1f
 {% endif %}
604c1f
@@ -66,6 +70,14 @@ network:
604c1f
   config: disabled
604c1f
 {% endif %}
604c1f
 
604c1f
+{% if variant == "rhel" %}
604c1f
+# Default redhat settings:
604c1f
+ssh_deletekeys:   true
604c1f
+ssh_genkeytypes:  ['rsa', 'ecdsa', 'ed25519']
604c1f
+syslog_fix_perms: ~
604c1f
+disable_vmware_customization: false
604c1f
+{% endif %}
604c1f
+
604c1f
 # The modules that run in the 'init' stage
604c1f
 cloud_init_modules:
604c1f
  - migrator
604c1f
@@ -107,10 +119,15 @@ cloud_config_modules:
604c1f
 {% endif %}
604c1f
 {% if variant not in ["photon"] %}
604c1f
  - ssh-import-id
604c1f
+{% if variant not in ["rhel"] %}
604c1f
  - keyboard
604c1f
+{% endif %}
604c1f
  - locale
604c1f
 {% endif %}
604c1f
  - set-passwords
604c1f
+{% if variant in ["rhel"] %}
604c1f
+ - rh_subscription
604c1f
+{% endif %}
604c1f
 {% if variant in ["rhel", "fedora", "photon"] %}
604c1f
 {% if variant not in ["photon"] %}
604c1f
  - spacewalk
604c1f
@@ -239,6 +256,10 @@ system_info:
604c1f
      name: ec2-user
604c1f
      lock_passwd: True
604c1f
      gecos: EC2 Default User
604c1f
+{% elif variant == "rhel" %}
604c1f
+     name: cloud-user
604c1f
+     lock_passwd: true
604c1f
+     gecos: Cloud User
604c1f
 {% else %}
604c1f
      name: {{ variant }}
604c1f
      lock_passwd: True
604c1f
@@ -254,6 +275,8 @@ system_info:
604c1f
      groups: [adm, sudo]
604c1f
 {% elif variant == "arch" %}
604c1f
      groups: [wheel, users]
604c1f
+{% elif variant == "rhel" %}
604c1f
+     groups: [adm, systemd-journal]
604c1f
 {% else %}
604c1f
      groups: [wheel, adm, systemd-journal]
604c1f
 {% endif %}
604c1f
diff --git a/systemd/cloud-config.service.tmpl b/systemd/cloud-config.service.tmpl
604c1f
index 9d928ca2..d5568a6e 100644
604c1f
--- a/systemd/cloud-config.service.tmpl
604c1f
+++ b/systemd/cloud-config.service.tmpl
604c1f
@@ -4,6 +4,10 @@ Description=Apply the settings specified in cloud-config
604c1f
 After=network-online.target cloud-config.target
604c1f
 After=snapd.seeded.service
604c1f
 Wants=network-online.target cloud-config.target
604c1f
+{% if variant == "rhel" %}
604c1f
+ConditionPathExists=!/etc/cloud/cloud-init.disabled
604c1f
+ConditionKernelCommandLine=!cloud-init=disabled
604c1f
+{% endif %}
604c1f
 
604c1f
 [Service]
604c1f
 Type=oneshot
604c1f
diff --git a/systemd/cloud-final.service.tmpl b/systemd/cloud-final.service.tmpl
604c1f
index 8207b18c..85f423ac 100644
604c1f
--- a/systemd/cloud-final.service.tmpl
604c1f
+++ b/systemd/cloud-final.service.tmpl
604c1f
@@ -7,6 +7,10 @@ After=multi-user.target
604c1f
 Before=apt-daily.service
604c1f
 {% endif %}
604c1f
 Wants=network-online.target cloud-config.service
604c1f
+{% if variant == "rhel" %}
604c1f
+ConditionPathExists=!/etc/cloud/cloud-init.disabled
604c1f
+ConditionKernelCommandLine=!cloud-init=disabled
604c1f
+{% endif %}
604c1f
 
604c1f
 
604c1f
 [Service]
604c1f
@@ -15,7 +19,16 @@ ExecStart=/usr/bin/cloud-init modules --mode=final
604c1f
 RemainAfterExit=yes
604c1f
 TimeoutSec=0
604c1f
 KillMode=process
604c1f
+{% if variant == "rhel" %}
604c1f
+# Restart NetworkManager if it is present and running.
604c1f
+ExecStartPost=/bin/sh -c 'u=NetworkManager.service; \
604c1f
+ out=$(systemctl show --property=SubState $u) || exit; \
604c1f
+ [ "$out" = "SubState=running" ] || exit 0; \
604c1f
+ systemctl reload-or-try-restart $u'
604c1f
+{% else %}
604c1f
 TasksMax=infinity
604c1f
+{% endif %}
604c1f
+
604c1f
 
604c1f
 # Output needs to appear in instance console output
604c1f
 StandardOutput=journal+console
604c1f
diff --git a/systemd/cloud-init-local.service.tmpl b/systemd/cloud-init-local.service.tmpl
604c1f
index 7166f640..a6b82650 100644
604c1f
--- a/systemd/cloud-init-local.service.tmpl
604c1f
+++ b/systemd/cloud-init-local.service.tmpl
604c1f
@@ -1,23 +1,43 @@
604c1f
 ## template:jinja
604c1f
 [Unit]
604c1f
 Description=Initial cloud-init job (pre-networking)
604c1f
-{% if variant in ["ubuntu", "unknown", "debian"] %}
604c1f
+{% if variant in ["ubuntu", "unknown", "debian", "rhel" ] %}
604c1f
 DefaultDependencies=no
604c1f
 {% endif %}
604c1f
 Wants=network-pre.target
604c1f
 After=hv_kvp_daemon.service
604c1f
 After=systemd-remount-fs.service
604c1f
+{% if variant == "rhel" %}
604c1f
+Requires=dbus.socket
604c1f
+After=dbus.socket
604c1f
+{% endif %}
604c1f
 Before=NetworkManager.service
604c1f
+{% if variant == "rhel" %}
604c1f
+Before=network.service
604c1f
+{% endif %}
604c1f
 Before=network-pre.target
604c1f
 Before=shutdown.target
604c1f
+{% if variant == "rhel" %}
604c1f
+Before=firewalld.target
604c1f
+Conflicts=shutdown.target
604c1f
+{% endif %}
604c1f
 {% if variant in ["ubuntu", "unknown", "debian"] %}
604c1f
 Before=sysinit.target
604c1f
 Conflicts=shutdown.target
604c1f
 {% endif %}
604c1f
 RequiresMountsFor=/var/lib/cloud
604c1f
+{% if variant == "rhel" %}
604c1f
+ConditionPathExists=!/etc/cloud/cloud-init.disabled
604c1f
+ConditionKernelCommandLine=!cloud-init=disabled
604c1f
+{% endif %}
604c1f
 
604c1f
 [Service]
604c1f
 Type=oneshot
604c1f
+{% if variant == "rhel" %}
604c1f
+ExecStartPre=/bin/mkdir -p /run/cloud-init
604c1f
+ExecStartPre=/sbin/restorecon /run/cloud-init
604c1f
+ExecStartPre=/usr/bin/touch /run/cloud-init/enabled
604c1f
+{% endif %}
604c1f
 ExecStart=/usr/bin/cloud-init init --local
604c1f
 ExecStart=/bin/touch /run/cloud-init/network-config-ready
604c1f
 RemainAfterExit=yes
604c1f
diff --git a/systemd/cloud-init.service.tmpl b/systemd/cloud-init.service.tmpl
604c1f
index e71e5679..c170aef7 100644
604c1f
--- a/systemd/cloud-init.service.tmpl
604c1f
+++ b/systemd/cloud-init.service.tmpl
604c1f
@@ -1,7 +1,7 @@
604c1f
 ## template:jinja
604c1f
 [Unit]
604c1f
 Description=Initial cloud-init job (metadata service crawler)
604c1f
-{% if variant not in ["photon"] %}
604c1f
+{% if variant not in ["photon", "rhel"] %}
604c1f
 DefaultDependencies=no
604c1f
 {% endif %}
604c1f
 Wants=cloud-init-local.service
604c1f
@@ -36,6 +36,10 @@ Before=shutdown.target
604c1f
 Conflicts=shutdown.target
604c1f
 {% endif %}
604c1f
 Before=systemd-user-sessions.service
604c1f
+{% if variant == "rhel" %}
604c1f
+ConditionPathExists=!/etc/cloud/cloud-init.disabled
604c1f
+ConditionKernelCommandLine=!cloud-init=disabled
604c1f
+{% endif %}
604c1f
 
604c1f
 [Service]
604c1f
 Type=oneshot
604c1f
diff --git a/tests/unittests/test_render_cloudcfg.py b/tests/unittests/test_render_cloudcfg.py
604c1f
index 30fbd1a4..9f95d448 100644
604c1f
--- a/tests/unittests/test_render_cloudcfg.py
604c1f
+++ b/tests/unittests/test_render_cloudcfg.py
604c1f
@@ -68,6 +68,7 @@ class TestRenderCloudCfg:
604c1f
         default_user_exceptions = {
604c1f
             "amazon": "ec2-user",
604c1f
             "debian": "ubuntu",
604c1f
+            "rhel": "cloud-user",
604c1f
             "unknown": "ubuntu",
604c1f
         }
604c1f
         default_user = system_cfg["system_info"]["default_user"]["name"]
604c1f
-- 
604c1f
2.35.3
604c1f