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