738010
From f8d348243bd32fe3c3f0b55c2a216e95d44c5abd Mon Sep 17 00:00:00 2001
738010
From: Eduardo Otubo <otubo@redhat.com>
738010
Date: Thu, 28 Feb 2019 12:38:36 +0100
738010
Subject: cloud-init-per: don't use dashes in sem names
738010
738010
RH-Author: Eduardo Otubo <otubo@redhat.com>
738010
Message-id: <20190228123836.17979-1-otubo@redhat.com>
738010
Patchwork-id: 84743
738010
O-Subject: [RHEL-7.7 cloud-init PATCH] This is to fix https://bugs.launchpad.net/cloud-init/+bug/1812676
738010
Bugzilla: 1664876
738010
RH-Acked-by: Mohammed Gamal <mgamal@redhat.com>
738010
RH-Acked-by: Vitaly Kuznetsov <vkuznets@redhat.com>
738010
738010
From: Vitaly Kuznetsov <vkuznets@redhat.com>
738010
738010
    It was found that when there is a dash in cloud-init-per command
738010
    name and cloud-init-per is executed through cloud-init's bootcmd, e.g:
738010
738010
    bootcmd:
738010
     - cloud-init-per instance mycmd-bootcmd /usr/bin/mycmd
738010
738010
    the command is executed on each boot. However, running the same
738010
    cloud-init-per command manually after boot doesn't reveal the issue. Turns
738010
    out the issue comes from 'migrator' cloud-init module which renames all
738010
    files in /var/lib/cloud/instance/sem/ replacing dashes with underscores. As
738010
    migrator runs before bootcmd it renames
738010
738010
    /var/lib/cloud/instance/sem/bootper.mycmd-bootcmd.instance
738010
    to
738010
    /var/lib/cloud/instance/sem/bootper.mycmd_bootcmd.instance
738010
738010
    so cloud-init-per doesn't see it and thinks that the comment was never ran
738010
    before. On next boot the sequence repeats.
738010
738010
    There are multiple ways to resolve the issue. This patch takes the
738010
    following approach: 'canonicalize' sem names by replacing dashes with
738010
    underscores (this is consistent with post-'migrator' contents of
738010
    /var/lib/cloud/instance/sem/). We, however, need to be careful: in case
738010
    someone had a command with dashes before and he had migrator module enables
738010
    we need to see the old sem file (or the command will run again and this can
738010
    be as bad as formatting a partition!) so we add a small 'migrator' part to
738010
    cloud-init-per script itself checking for legacy sem names.
738010
738010
    Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com>
738010
738010
commit 9cf9d8cdd3a8fd7d4d425f7051122d0ac8af2bbd
738010
Author: Vitaly Kuznetsov <vkuznets@redhat.com>
738010
Date:   Mon Feb 18 22:55:49 2019 +0000
738010
738010
    This is to fix https://bugs.launchpad.net/cloud-init/+bug/1812676
738010
738010
Resolves: rhbz#1664876
738010
X-downstream-only: false
738010
738010
Signed-off-by: Eduardo Otubo <otubo@redhat.com>
738010
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
738010
---
738010
 tools/cloud-init-per | 8 +++++++-
738010
 1 file changed, 7 insertions(+), 1 deletion(-)
738010
738010
diff --git a/tools/cloud-init-per b/tools/cloud-init-per
738010
index 7d6754b..eae3e93 100755
738010
--- a/tools/cloud-init-per
738010
+++ b/tools/cloud-init-per
738010
@@ -38,7 +38,7 @@ fi
738010
 [ "$1" = "-h" -o "$1" = "--help" ] && { Usage ; exit 0; }
738010
 [ $# -ge 3 ] || { Usage 1>&2; exit 1; }
738010
 freq=$1
738010
-name=$2
738010
+name=${2/-/_}
738010
 shift 2;
738010
 
738010
 [ "${name#*/}" = "${name}" ] || fail "name cannot contain a /"
738010
@@ -53,6 +53,12 @@ esac
738010
 [ -d "${sem%/*}" ] || mkdir -p "${sem%/*}" ||
738010
    fail "failed to make directory for ${sem}"
738010
 
738010
+# Rename legacy sem files with dashes in their names. Do not overwrite existing
738010
+# sem files to prevent clobbering those which may have been created from calls
738010
+# outside of cloud-init.
738010
+sem_legacy="${sem/_/-}"
738010
+[ "$sem" != "$sem_legacy" -a -e "$sem_legacy" ] && mv -n "$sem_legacy" "$sem"
738010
+
738010
 [ "$freq" != "always" -a -e "$sem" ] && exit 0
738010
 "$@"
738010
 ret=$?
738010
-- 
738010
1.8.3.1
738010