sailesh1993 / rpms / cloud-init

Forked from rpms/cloud-init a year ago
Clone
738010
From e3f04e297ce950ce0d183ca87a434ec932ae6a86 Mon Sep 17 00:00:00 2001
738010
From: Eduardo Otubo <otubo@redhat.com>
738010
Date: Wed, 15 May 2019 12:15:29 +0200
738010
Subject: [PATCH 5/5] cc_mounts: check if mount -a on no-change fstab path
738010
738010
RH-Author: Eduardo Otubo <otubo@redhat.com>
738010
Message-id: <20190515121529.11191-6-otubo@redhat.com>
738010
Patchwork-id: 87886
738010
O-Subject: [rhel-7 cloud-init PATCHv2 5/5] cc_mounts: check if mount -a on no-change fstab path
738010
Bugzilla: 1687565
738010
RH-Acked-by: Vitaly Kuznetsov <vkuznets@redhat.com>
738010
RH-Acked-by: Mohammed Gamal <mgamal@redhat.com>
738010
738010
From: "Jason Zions (MSFT)" <jasonzio@microsoft.com>
738010
738010
BZ: 1687565
738010
BRANCH: rhel7/master-18.5
738010
UPSTREAM: acc25d8d
738010
BREW: 21696239
738010
738010
commit acc25d8d7d603313059ac35b4253b504efc560a9
738010
Author: Jason Zions (MSFT) <jasonzio@microsoft.com>
738010
Date:   Wed May 8 22:47:07 2019 +0000
738010
738010
    cc_mounts: check if mount -a on no-change fstab path
738010
738010
    Under some circumstances, cc_disk_setup may reformat volumes which
738010
    already appear in /etc/fstab (e.g. Azure ephemeral drive is reformatted
738010
    from NTFS to ext4 after service-heal). Normally, cc_mounts only calls
738010
    mount -a if it altered /etc/fstab. With this change cc_mounts will read
738010
    /proc/mounts and verify if configured mounts are already mounted and if
738010
    not raise flag to request a mount -a.  This handles the case where no
738010
    changes to fstab occur but a mount -a is required due to change in
738010
    underlying device which prevented the .mount unit from running until
738010
    after disk was reformatted.
738010
738010
    LP: #1825596
738010
738010
Signed-off-by: Eduardo Otubo <otubo@redhat.com>
738010
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
738010
---
738010
 cloudinit/config/cc_mounts.py                      | 11 ++++++++
738010
 .../unittests/test_handler/test_handler_mounts.py  | 30 +++++++++++++++++++++-
738010
 2 files changed, 40 insertions(+), 1 deletion(-)
738010
738010
diff --git a/cloudinit/config/cc_mounts.py b/cloudinit/config/cc_mounts.py
738010
index 339baba..123ffb8 100644
738010
--- a/cloudinit/config/cc_mounts.py
738010
+++ b/cloudinit/config/cc_mounts.py
738010
@@ -439,6 +439,7 @@ def handle(_name, cfg, cloud, log, _args):
738010
 
738010
     cc_lines = []
738010
     needswap = False
738010
+    need_mount_all = False
738010
     dirs = []
738010
     for line in actlist:
738010
         # write 'comment' in the fs_mntops, entry,  claiming this
738010
@@ -449,11 +450,18 @@ def handle(_name, cfg, cloud, log, _args):
738010
             dirs.append(line[1])
738010
         cc_lines.append('\t'.join(line))
738010
 
738010
+    mount_points = [v['mountpoint'] for k, v in util.mounts().items()
738010
+                    if 'mountpoint' in v]
738010
     for d in dirs:
738010
         try:
738010
             util.ensure_dir(d)
738010
         except Exception:
738010
             util.logexc(log, "Failed to make '%s' config-mount", d)
738010
+        # dirs is list of directories on which a volume should be mounted.
738010
+        # If any of them does not already show up in the list of current
738010
+        # mount points, we will definitely need to do mount -a.
738010
+        if not need_mount_all and d not in mount_points:
738010
+            need_mount_all = True
738010
 
738010
     sadds = [WS.sub(" ", n) for n in cc_lines]
738010
     sdrops = [WS.sub(" ", n) for n in fstab_removed]
738010
@@ -473,6 +481,9 @@ def handle(_name, cfg, cloud, log, _args):
738010
         log.debug("No changes to /etc/fstab made.")
738010
     else:
738010
         log.debug("Changes to fstab: %s", sops)
738010
+        need_mount_all = True
738010
+
738010
+    if need_mount_all:
738010
         activate_cmds.append(["mount", "-a"])
738010
         if uses_systemd:
738010
             activate_cmds.append(["systemctl", "daemon-reload"])
738010
diff --git a/tests/unittests/test_handler/test_handler_mounts.py b/tests/unittests/test_handler/test_handler_mounts.py
738010
index 8fea6c2..0fb160b 100644
738010
--- a/tests/unittests/test_handler/test_handler_mounts.py
738010
+++ b/tests/unittests/test_handler/test_handler_mounts.py
738010
@@ -154,7 +154,15 @@ class TestFstabHandling(test_helpers.FilesystemMockingTestCase):
738010
                        return_value=True)
738010
 
738010
         self.add_patch('cloudinit.config.cc_mounts.util.subp',
738010
-                       'mock_util_subp')
738010
+                       'm_util_subp')
738010
+
738010
+        self.add_patch('cloudinit.config.cc_mounts.util.mounts',
738010
+                       'mock_util_mounts',
738010
+                       return_value={
738010
+                           '/dev/sda1': {'fstype': 'ext4',
738010
+                                         'mountpoint': '/',
738010
+                                         'opts': 'rw,relatime,discard'
738010
+                                         }})
738010
 
738010
         self.mock_cloud = mock.Mock()
738010
         self.mock_log = mock.Mock()
738010
@@ -230,4 +238,24 @@ class TestFstabHandling(test_helpers.FilesystemMockingTestCase):
738010
             fstab_new_content = fd.read()
738010
             self.assertEqual(fstab_expected_content, fstab_new_content)
738010
 
738010
+    def test_no_change_fstab_sets_needs_mount_all(self):
738010
+        '''verify unchanged fstab entries are mounted if not call mount -a'''
738010
+        fstab_original_content = (
738010
+            'LABEL=cloudimg-rootfs / ext4 defaults 0 0\n'
738010
+            'LABEL=UEFI /boot/efi vfat defaults 0 0\n'
738010
+            '/dev/vdb /mnt auto defaults,noexec,comment=cloudconfig 0 2\n'
738010
+        )
738010
+        fstab_expected_content = fstab_original_content
738010
+        cc = {'mounts': [
738010
+                 ['/dev/vdb', '/mnt', 'auto', 'defaults,noexec']]}
738010
+        with open(cc_mounts.FSTAB_PATH, 'w') as fd:
738010
+            fd.write(fstab_original_content)
738010
+        with open(cc_mounts.FSTAB_PATH, 'r') as fd:
738010
+            fstab_new_content = fd.read()
738010
+            self.assertEqual(fstab_expected_content, fstab_new_content)
738010
+        cc_mounts.handle(None, cc, self.mock_cloud, self.mock_log, [])
738010
+        self.m_util_subp.assert_has_calls([
738010
+            mock.call(['mount', '-a']),
738010
+            mock.call(['systemctl', 'daemon-reload'])])
738010
+
738010
 # vi: ts=4 expandtab
738010
-- 
738010
1.8.3.1
738010