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