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