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