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