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